diff --git a/include/xsimd/arch/common/xsimd_common_math.hpp b/include/xsimd/arch/common/xsimd_common_math.hpp index ad8a2741a..8ab7e0e59 100644 --- a/include/xsimd/arch/common/xsimd_common_math.hpp +++ b/include/xsimd/arch/common/xsimd_common_math.hpp @@ -2323,9 +2323,9 @@ namespace xsimd real_batch ze(0.); return select(y == ze, - select(x == ze, - batch_type(ze, ze), - select(x < ze, batch_type(ze, sqrt_x), batch_type(sqrt_x, ze))), + select(x < ze, + batch_type(ze, copysign(sqrt_x, y)), + batch_type(sqrt_x, y)), select(x == ze, select(y > ze, batch_type(sqrt_hy, sqrt_hy), batch_type(sqrt_hy, -sqrt_hy)), resg)); diff --git a/test/test_complex_power.cpp b/test/test_complex_power.cpp index 99bb82819..dd99b7fc3 100644 --- a/test/test_complex_power.cpp +++ b/test/test_complex_power.cpp @@ -160,6 +160,7 @@ struct complex_power_test detail::load_batch(ref, expected, i); CHECK_BATCH_EQ(ref, out); } + test_sqrt_real_axis(true, true); } void test_sqrt_pn() @@ -175,6 +176,7 @@ struct complex_power_test detail::load_batch(ref, expected, i); CHECK_BATCH_EQ(ref, out); } + test_sqrt_real_axis(false, true); } void test_sqrt_np() @@ -190,6 +192,7 @@ struct complex_power_test detail::load_batch(ref, expected, i); CHECK_BATCH_EQ(ref, out); } + test_sqrt_real_axis(true, false); } void test_sqrt_pp() @@ -205,6 +208,50 @@ struct complex_power_test detail::load_batch(ref, expected, i); CHECK_BATCH_EQ(ref, out); } + test_sqrt_real_axis(false, false); + } + + void test_sqrt_real_axis(bool negative_real, bool negative_imag) + { + const real_value_type zero(0); + const std::array real_parts = { + zero, std::numeric_limits::denorm_min(), + real_value_type(1), real_value_type(4), std::numeric_limits::max() + }; + std::array inputs, results; + std::array magnitudes; + + for (size_t start = 0; start < real_parts.size(); start += size) + { + for (size_t lane = 0; lane < size; ++lane) + { + const auto x = real_parts[(start + lane) % real_parts.size()]; + const bool negative_y = negative_imag != ((start + lane) % 2 != 0); + inputs[lane] = value_type(negative_real ? -x : x, negative_y ? -zero : zero); + } + + const auto batch_inputs = batch_type::load_unaligned(inputs.data()); + xsimd::sqrt(batch_inputs).store_unaligned(results.data()); + // Preserve the real backend's rounding and underflow behavior. + xsimd::sqrt(xsimd::abs(batch_inputs.real())).store_unaligned(magnitudes.data()); + + for (size_t lane = 0; lane < size; ++lane) + { + const auto x = inputs[lane].real(); + const auto y = inputs[lane].imag(); + const auto magnitude = magnitudes[lane]; + const value_type ref = x < zero + ? value_type(zero, std::copysign(magnitude, y)) + : value_type(magnitude, y); + CHECK_EQ(results[lane].real(), ref.real()); + CHECK_EQ(results[lane].imag(), ref.imag()); +#ifndef __FAST_MATH__ + // Fast-math does not preserve the side of the real-axis branch cut. + CHECK_EQ(std::signbit(results[lane].real()), std::signbit(ref.real())); + CHECK_EQ(std::signbit(results[lane].imag()), std::signbit(ref.imag())); +#endif + } + } } };