Skip to content

Commit 7a076bd

Browse files
authored
[ESIMD] Make esimd implementation of fmod compatible with std::fmod (intel#6242)
* Make esimd implementation of fmod compatible with std::fmod
1 parent 49f72fa commit 7a076bd

File tree

1 file changed

+15
-12
lines changed
  • sycl/include/sycl/ext/intel/experimental/esimd

1 file changed

+15
-12
lines changed

sycl/include/sycl/ext/intel/experimental/esimd/math.hpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,22 +1371,25 @@ template <> ESIMD_INLINE float atan2(float y, float x) {
13711371
template <int N>
13721372
ESIMD_INLINE __ESIMD_NS::simd<float, N> fmod(__ESIMD_NS::simd<float, N> y,
13731373
__ESIMD_NS::simd<float, N> x) {
1374-
__ESIMD_NS::simd<int, N> v_quot;
1375-
__ESIMD_NS::simd<float, N> fmod;
1374+
__ESIMD_NS::simd<float, N> abs_x = __ESIMD_NS::abs(x);
1375+
__ESIMD_NS::simd<float, N> abs_y = __ESIMD_NS::abs(y);
1376+
auto fmod_sign_mask = (y.template bit_cast_view<int32_t>()) & 0x80000000;
13761377

1377-
v_quot = convert<int>(y / x);
1378-
fmod = y - x * convert<float>(v_quot);
1379-
return fmod;
1378+
__ESIMD_NS::simd<float, N> reminder =
1379+
abs_y - abs_x * __ESIMD_NS::trunc<float>(abs_y / abs_x);
1380+
1381+
abs_x.merge(0.0, reminder >= 0);
1382+
__ESIMD_NS::simd<float, N> fmod = reminder + abs_x;
1383+
__ESIMD_NS::simd<float, N> fmod_abs = __ESIMD_NS::abs(fmod);
1384+
1385+
auto fmod_bits =
1386+
(fmod_abs.template bit_cast_view<int32_t>()) | fmod_sign_mask;
1387+
return fmod_bits.template bit_cast_view<float>();
13801388
}
13811389

1382-
// For Scalar Input
1390+
// For Scalar Input
13831391
template <> ESIMD_INLINE float fmod(float y, float x) {
1384-
int v_quot;
1385-
__ESIMD_NS::simd<float, 1> fmod;
1386-
1387-
v_quot = (int)(y / x);
1388-
fmod = y - x * v_quot;
1389-
return fmod[0];
1392+
return fmod(__ESIMD_NS::simd<float, 1>(y), __ESIMD_NS::simd<float, 1>(x))[0];
13901393
}
13911394

13921395
// sin_emu - EU emulation for sin(x)

0 commit comments

Comments
 (0)