Skip to content

Commit 54dddb4

Browse files
jgstarIntelbader
authored andcommitted
[SYCL] Fix final result saturation in mad_sat host implementation (#1025)
Signed-off-by: Joey Genfi <[email protected]>
1 parent 659efdf commit 54dddb4

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sycl/source/detail/builtins_integer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ template <typename T> inline T __s_long_mad_hi(T a, T b, T c) {
153153
template <typename T> inline T __s_mad_sat(T a, T b, T c) {
154154
using UPT = typename d::make_larger<T>::type;
155155
UPT mul = UPT(a) * UPT(b);
156+
UPT res = mul + UPT(c);
156157
const UPT max = d::max_v<T>();
157158
const UPT min = d::min_v<T>();
158-
mul = std::min(std::max(mul, min), max);
159-
return __s_add_sat(T(mul), c);
159+
res = std::min(std::max(res, min), max);
160+
return T(res);
160161
}
161162

162163
template <typename T> inline T __s_long_mad_sat(T a, T b, T c) {

sycl/test/built-ins/scalar_integer.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,28 @@ int main() {
287287
assert(r == 0x7FFFFFFF);
288288
}
289289

290+
// mad_sat test two
291+
{
292+
char r(0);
293+
char exp(120);
294+
{
295+
cl::sycl::buffer<char, 1> buf(&r, cl::sycl::range<1>(1));
296+
cl::sycl::queue q;
297+
q.submit([&](cl::sycl::handler &cgh) {
298+
auto acc = buf.get_access<cl::sycl::access::mode::write>(cgh);
299+
cgh.single_task<class kernel>([=]() {
300+
signed char inputData_0(-17);
301+
signed char inputData_1(-10);
302+
signed char inputData_2(-50);
303+
acc[0] = cl::sycl::mad_sat(inputData_0, inputData_1, inputData_2);
304+
});
305+
});
306+
}
307+
assert(r == exp); // Should return the real number of i0*i1+i2 in CPU
308+
// Only fails in vector, but passes in scalar.
309+
310+
}
311+
290312
// mul_hi
291313
{
292314
s::cl_int r{ 0 };

0 commit comments

Comments
 (0)