Skip to content

Commit 79ffbae

Browse files
Fix limit behavior of atan2 under -ffast-math
Fix #1231
1 parent f5e485e commit 79ffbae

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

include/xsimd/arch/common/xsimd_common_trigo.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,14 @@ namespace xsimd
354354
XSIMD_INLINE batch<T, A> atan2(batch<T, A> const& self, batch<T, A> const& other, requires_arch<common>) noexcept
355355
{
356356
using batch_type = batch<T, A>;
357+
#ifdef __FAST_MATH__
358+
const batch_type q = abs(self / other);
359+
const batch_type q_p = abs(other / self);
360+
#else
357361
const batch_type q = abs(self / other);
358-
const batch_type z = detail::kernel_atan(q, batch_type(1.) / q);
362+
const batch_type q_p = 1. / q;
363+
#endif
364+
const batch_type z = detail::kernel_atan(q, q_p);
359365
return select(other > batch_type(0.), z, constants::pi<batch_type>() - z) * signnz(self);
360366
}
361367

test/test_xsimd_api.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,17 @@ struct xsimd_api_float_types_functions
502502
}
503503
void test_atan2()
504504
{
505-
value_type val0(0);
506-
value_type val1(1);
507-
CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1));
505+
{
506+
value_type val0(0);
507+
value_type val1(1);
508+
CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1));
509+
}
510+
511+
{
512+
value_type val0(1);
513+
value_type val1(0);
514+
CHECK_EQ(extract(xsimd::atan2(T(val0), T(val1))), std::atan2(val0, val1));
515+
}
508516
}
509517
void test_atanh()
510518
{

0 commit comments

Comments
 (0)