[PATCH] D159275: libclc: Fix signed interger underflow in abs_diff

Fraser Cormack via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 31 05:47:54 PDT 2023


frasercrmck created this revision.
frasercrmck added reviewers: kpet, rjodinchr, aarongreig.
Herald added subscribers: jvesely, Anastasia.
Herald added a project: All.
frasercrmck requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We noticed this same issue in our own implementation of abs_diff, and
the same issue also came up in the abs_diff reference function in the
OpenCL CTS.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159275

Files:
  libclc/generic/lib/integer/abs_diff.inc


Index: libclc/generic/lib/integer/abs_diff.inc
===================================================================
--- libclc/generic/lib/integer/abs_diff.inc
+++ libclc/generic/lib/integer/abs_diff.inc
@@ -1,3 +1,5 @@
 _CLC_OVERLOAD _CLC_DEF __CLC_U_GENTYPE abs_diff(__CLC_GENTYPE x, __CLC_GENTYPE y) {
-  return __builtin_astype((__CLC_GENTYPE)(x > y ? x-y : y-x), __CLC_U_GENTYPE);
+  __CLC_U_GENTYPE ux = __builtin_astype(x, __CLC_U_GENTYPE);
+  __CLC_U_GENTYPE uy = __builtin_astype(y, __CLC_U_GENTYPE);
+  return x > y ? ux - uy : uy - ux;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159275.555000.patch
Type: text/x-patch
Size: 549 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230831/2e7a0a35/attachment.bin>


More information about the llvm-commits mailing list