Skip to content

Commit 2ebc479

Browse files
[SYCL] Fix abs_diff host implementation (#12859)
In #12856 I misunderstood what the spec meant by > When the inputs are scalars, returns |x - y|. Otherwise, returns > |x[i]- y[i]| for each element of x and y. The subtraction is done > without modulo overflow. The behavior is undefined if the result > cannot be represented by the return type. Fix it here. We still differ from non-preview mode in return type - it used to be `unsigned` prior to SYCL2020 revision 8, I believe.
1 parent 287fd37 commit 2ebc479

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

sycl/source/builtins/integer_functions.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ BUILTIN_GENINT(ONE_ARG, abs, [](auto x) -> decltype(x) {
9090
})
9191

9292
BUILTIN_GENINT_SU(TWO_ARGS, abs_diff, [](auto x, auto y) -> decltype(x) {
93-
// From SYCL 2020 revision 8:
94-
//
95-
// > The subtraction is done without modulo overflow. The behavior is
96-
// > undefined if the result cannot be represented by the return type.
97-
return sycl::abs(x - y);
93+
if constexpr (std::is_signed_v<decltype(x)>)
94+
if ((x < 0) != (y < 0))
95+
return std::abs(x) + std::abs(y);
96+
97+
return std::max(x, y) - std::min(x, y);
9898
})
9999

100100
BUILTIN_GENINT_SU(TWO_ARGS, add_sat, [](auto x, auto y) -> decltype(x) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@ int main() {
4545
marray<uint8_t, 2>{3, 2});
4646
}
4747

48+
{
49+
// Test abs_diff:
50+
auto AbsDiff = [](auto... xs) { return abs_diff(xs...); };
51+
Test(AbsDiff, marray<unsigned, 2>{1, 1}, marray<unsigned, 2>{0, 1},
52+
marray<unsigned, 2>{1, 0});
53+
}
54+
4855
return 0;
4956
}

0 commit comments

Comments
 (0)