Skip to content

Commit

Permalink
[SYCL][ESIMD] fix atan2 implementation to work correctly with fast-ma…
Browse files Browse the repository at this point in the history
…th switch (#13186)
  • Loading branch information
fineg74 authored Apr 24, 2024
1 parent 425dbd7 commit 1f72a47
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
22 changes: 11 additions & 11 deletions sycl/include/sycl/ext/intel/experimental/esimd/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1445,22 +1445,22 @@ template <> ESIMD_INLINE float atan2_fast(float y, float x) {
template <int N>
ESIMD_INLINE __ESIMD_NS::simd<float, N> atan2(__ESIMD_NS::simd<float, N> y,
__ESIMD_NS::simd<float, N> x) {
__ESIMD_NS::simd<float, N> v_distance;
__ESIMD_NS::simd<float, N> atan2;
__ESIMD_NS::simd_mask<N> mask;
__ESIMD_NS::simd<float, N> atan = esimd::atan(y / x);

constexpr float CONST_DBL_EPSILON = 0.00001f;

mask = (x < -CONST_DBL_EPSILON && y < CONST_DBL_EPSILON && y >= 0.f);
atan2.merge(float(detail::__ESIMD_CONST_PI), 0.f, mask);
mask = (x < -CONST_DBL_EPSILON && y > -CONST_DBL_EPSILON && y < 0);
atan2.merge(float(-detail::__ESIMD_CONST_PI), mask);
mask = (x < CONST_DBL_EPSILON && __ESIMD_NS::abs(y) > CONST_DBL_EPSILON);
v_distance = __ESIMD_NS::sqrt(x * x + y * y);
atan2.merge(2.0f * esimd::atan((v_distance - x) / y), mask);

mask = (x > 0.f);
atan2.merge(2.0f * esimd::atan(y / (v_distance + x)), mask);
mask = (__ESIMD_NS::abs(x) < CONST_DBL_EPSILON && y < -CONST_DBL_EPSILON);
atan2.merge(float(-detail::__ESIMD_CONST_PI) / 2.f, 0.f, mask);
mask = (__ESIMD_NS::abs(x) < CONST_DBL_EPSILON && y > CONST_DBL_EPSILON);
atan2.merge(float(detail::__ESIMD_CONST_PI) / 2.f, mask);
mask = (x < -CONST_DBL_EPSILON && y < -CONST_DBL_EPSILON);
atan2.merge(atan - float(detail::__ESIMD_CONST_PI), mask);
mask = (x < -CONST_DBL_EPSILON && y >= -CONST_DBL_EPSILON);
atan2.merge(atan + float(detail::__ESIMD_CONST_PI), mask);
mask = (x > CONST_DBL_EPSILON);
atan2.merge(atan, mask);

return atan2;
}
Expand Down
5 changes: 1 addition & 4 deletions sycl/test-e2e/ESIMD/regression/math_const_fix_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// TODO: remove fno-fast-math option once the issue is investigated and the test
// is fixed.
// DEFINE: %{mathflags} = %if cl_options %{/clang:-fno-fast-math%} %else %{-fno-fast-math%}
// RUN: %{build} %{mathflags} -o %t.out
// RUN: %{build} -o %t.out
// RUN: %{run} %t.out
//==- math_const_fix_test.cpp - Test to verify math functions correctness-==//
//
Expand Down

0 comments on commit 1f72a47

Please sign in to comment.