[libclc] ebb0852 - [libclc] Fix __clc_mad_sat for 64-bit signed integers (#246034)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 01:51:09 PST 2026


Author: Jozef Sztabinski
Date: 2026-12-02T17:51:03+08:00
New Revision: ebb0852fb9e4915258ca5c373e328d5b24930a61

URL: https://github.com/llvm/llvm-project/commit/ebb0852fb9e4915258ca5c373e328d5b24930a61
DIFF: https://github.com/llvm/llvm-project/commit/ebb0852fb9e4915258ca5c373e328d5b24930a61.diff

LOG: [libclc] Fix __clc_mad_sat for 64-bit signed integers (#246034)

The signed 64-bit implementation predicted overflow from the signs of
the operands and returned wrong results for several inputs, e.g.
mad_sat(0, -5, 7) returned LONG_MIN. Compute x * y + z exactly as a
128-bit value and saturate only if it does not fit in 64 bits.

Added: 
    libclc/test/conformance/integer/mad_sat.cl

Modified: 
    libclc/clc/lib/generic/integer/clc_mad_sat.cl

Removed: 
    


################################################################################
diff  --git a/libclc/clc/lib/generic/integer/clc_mad_sat.cl b/libclc/clc/lib/generic/integer/clc_mad_sat.cl
index 77470d17306e86..c3e705878d31a8 100644
--- a/libclc/clc/lib/generic/integer/clc_mad_sat.cl
+++ b/libclc/clc/lib/generic/integer/clc_mad_sat.cl
@@ -84,18 +84,12 @@ __CLC_DEFINE_SINT_MAD_SAT_ALL_TYS(int, uint, long)
   _CLC_OVERLOAD _CLC_DEF SLONGTY __clc_mad_sat(SLONGTY x, SLONGTY y,           \
                                                SLONGTY z) {                    \
     SLONGTY hi = __clc_mul_hi(x, y);                                           \
-    ULONGTY ulo = __clc_as_##ULONGTY(x * y);                                   \
-    SLONGTY max1 = (x < 0) == (y < 0) && hi != 0;                              \
-    SLONGTY max2 = hi == 0 && ulo >= LONG_MAX &&                               \
-                   (z > 0 || (ulo + __clc_as_##ULONGTY(z)) > LONG_MAX);        \
-    SLONGTY min1 = (((x < 0) != (y < 0)) && hi != -1);                         \
-    SLONGTY min2 =                                                             \
-        hi == -1 && ulo <= ((ULONGTY)LONG_MAX + 1UL) &&                        \
-        (z < 0 || __clc_as_##ULONGTY(z) < ((ULONGTY)LONG_MAX - ulo));          \
-    SLONGTY ret = __clc_as_##SLONGTY(ulo + __clc_as_##ULONGTY(z));             \
-    ret = __clc_select(ret, (SLONGTY)LONG_MAX, (SLONGTY)(max1 || max2));       \
-    ret = __clc_select(ret, (SLONGTY)LONG_MIN, (SLONGTY)(min1 || min2));       \
-    return ret;                                                                \
+    ULONGTY lo = __clc_as_##ULONGTY(x) * __clc_as_##ULONGTY(y);                \
+    ULONGTY sum = lo + __clc_as_##ULONGTY(z);                                  \
+    hi += (z >> 63) + ((SLONGTY)(sum < lo) & 1);                               \
+    SLONGTY ret = __clc_as_##SLONGTY(sum);                                     \
+    SLONGTY sat = (hi >> 63) ^ LONG_MAX;                                       \
+    return __clc_select(ret, sat, (SLONGTY)(hi != (ret >> 63)));               \
   }
 
 #define __CLC_DEFINE_SLONG_MAD_SAT_ALL_TYS(SLONGTY, ULONGTY)                   \

diff  --git a/libclc/test/conformance/integer/mad_sat.cl b/libclc/test/conformance/integer/mad_sat.cl
new file mode 100644
index 00000000000000..6e34870f7e9f36
--- /dev/null
+++ b/libclc/test/conformance/integer/mad_sat.cl
@@ -0,0 +1,53 @@
+// RUN: %libclc-compile
+// RUN: %libclc-run --kernel mad_sat_i64 --threads-x 1 %t
+// RUN: %libclc-run --kernel mad_sat_i64_vec --threads-x 1 %t
+
+#include "conformance.h"
+
+// OpenCL C v3.0.19, Sec. 6.15.3: mad_sat() saturates a * b + c.
+__kernel void mad_sat_i64(void) {
+  CHECK_EQ(
+      mad_sat(TEST_INPUT(long, 3L), TEST_INPUT(long, 4L), TEST_INPUT(long, 5L)),
+      17L);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, 0L), TEST_INPUT(long, -5L),
+                   TEST_INPUT(long, 7L)),
+           7L);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MAX), TEST_INPUT(long, 2L),
+                   TEST_INPUT(long, LONG_MIN)),
+           LONG_MAX - 1);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MAX), TEST_INPUT(long, 2L),
+                   TEST_INPUT(long, 0L)),
+           LONG_MAX);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MIN), TEST_INPUT(long, 2L),
+                   TEST_INPUT(long, 0L)),
+           LONG_MIN);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, 1L), TEST_INPUT(long, 1L),
+                   TEST_INPUT(long, LONG_MAX)),
+           LONG_MAX);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, -1L), TEST_INPUT(long, 1L),
+                   TEST_INPUT(long, LONG_MIN)),
+           LONG_MIN);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MIN), TEST_INPUT(long, -1L),
+                   TEST_INPUT(long, -1L)),
+           LONG_MAX);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MAX), TEST_INPUT(long, 1L),
+                   TEST_INPUT(long, LONG_MIN)),
+           -1L);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MIN), TEST_INPUT(long, 1L),
+                   TEST_INPUT(long, 5L)),
+           LONG_MIN + 5);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MAX), TEST_INPUT(long, LONG_MAX),
+                   TEST_INPUT(long, 0L)),
+           LONG_MAX);
+  CHECK_EQ(mad_sat(TEST_INPUT(long, LONG_MIN), TEST_INPUT(long, LONG_MAX),
+                   TEST_INPUT(long, 0L)),
+           LONG_MIN);
+}
+
+// Every lane carries into the high half.
+__kernel void mad_sat_i64_vec(void) {
+  long4 x = TEST_INPUT(long4, (long4)(-1L, LONG_MIN, -1L, -2L));
+  long4 y = TEST_INPUT(long4, (long4)(1L, -1L, -1L, 3L));
+  long4 z = TEST_INPUT(long4, (long4)(LONG_MIN, -1L, -1L, 7L));
+  CHECK(all(mad_sat(x, y, z) == (long4)(LONG_MIN, LONG_MAX, 0L, 1L)));
+}


        


More information about the cfe-commits mailing list