From 52ff51158d1b08c8a2c42494ab00bc3a44f8b119 Mon Sep 17 00:00:00 2001 From: serge-sans-paille Date: Mon, 22 Dec 2025 22:36:39 +0100 Subject: [PATCH] Fix limit behavior of atan2 under -ffast-math Fix #1231 --- include/xsimd/arch/common/xsimd_common_trigo.hpp | 8 +++++++- test/test_xsimd_api.cpp | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/xsimd/arch/common/xsimd_common_trigo.hpp b/include/xsimd/arch/common/xsimd_common_trigo.hpp index 7f90cc559..78c1ea30e 100644 --- a/include/xsimd/arch/common/xsimd_common_trigo.hpp +++ b/include/xsimd/arch/common/xsimd_common_trigo.hpp @@ -354,8 +354,14 @@ namespace xsimd XSIMD_INLINE batch atan2(batch const& self, batch const& other, requires_arch) noexcept { using batch_type = batch; +#ifdef __FAST_MATH__ + const batch_type q = abs(self / other); + const batch_type q_p = abs(other / self); +#else const batch_type q = abs(self / other); - const batch_type z = detail::kernel_atan(q, batch_type(1.) / q); + const batch_type q_p = 1. / q; +#endif + const batch_type z = detail::kernel_atan(q, q_p); return select(other > batch_type(0.), z, constants::pi() - z) * signnz(self); } diff --git a/test/test_xsimd_api.cpp b/test/test_xsimd_api.cpp index 2c177b009..d39d600d5 100644 --- a/test/test_xsimd_api.cpp +++ b/test/test_xsimd_api.cpp @@ -502,9 +502,17 @@ struct xsimd_api_float_types_functions } void test_atan2() { - value_type val0(0); - value_type val1(1); - CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1)); + { + value_type val0(0); + value_type val1(1); + CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1)); + } + + { + value_type val0(1); + value_type val1(0); + CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1)); + } } void test_atanh() {