Skip to content

Commit 43fbde3

Browse files
authored
[SYCL] Fix sycl::remquo truncation error (intel#10105)
Certain quotient values are off by 1 since the conversion to `int` truncates the decimal value. Use `std::round` to round to the nearest integer before converting to `int`. Signed-off-by: Michael Aziz <[email protected]>
1 parent 5b5c50a commit 43fbde3

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

sycl/source/detail/builtins_math.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,19 +872,19 @@ MAKE_1V_2V(sycl_host_remainder, s::cl_half, s::cl_half, s::cl_half)
872872
__SYCL_EXPORT s::cl_float sycl_host_remquo(s::cl_float x, s::cl_float y,
873873
s::cl_int *quo) __NOEXC {
874874
s::cl_float rem = std::remainder(x, y);
875-
*quo = static_cast<int>((x - rem) / y);
875+
*quo = static_cast<int>(std::round((x - rem) / y));
876876
return rem;
877877
}
878878
__SYCL_EXPORT s::cl_double sycl_host_remquo(s::cl_double x, s::cl_double y,
879879
s::cl_int *quo) __NOEXC {
880880
s::cl_double rem = std::remainder(x, y);
881-
*quo = static_cast<int>((x - rem) / y);
881+
*quo = static_cast<int>(std::round((x - rem) / y));
882882
return rem;
883883
}
884884
__SYCL_EXPORT s::cl_half sycl_host_remquo(s::cl_half x, s::cl_half y,
885885
s::cl_int *quo) __NOEXC {
886886
s::cl_half rem = std::remainder(x, y);
887-
*quo = static_cast<int>((x - rem) / y);
887+
*quo = static_cast<int>(std::round((x - rem) / y));
888888
return rem;
889889
}
890890
MAKE_1V_2V_3P(sycl_host_remquo, s::cl_float, s::cl_float, s::cl_float,

sycl/test-e2e/Basic/built-ins/host_math.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ void testRemquo() {
5252
assert(quo == -3);
5353
assert(rem == -1);
5454
}
55+
56+
{
57+
int quo = 0;
58+
float rem = sycl::remquo(
59+
0.552879f, 0.219282f,
60+
sycl::multi_ptr<int, sycl::access::address_space::global_space>{&quo});
61+
assert(quo == 3);
62+
assert(rem == -0.10496702790260315f);
63+
}
5564
}
5665

5766
int main() {

0 commit comments

Comments
 (0)