[libclc] [libclc] Fix double NAN_MASK in __clc_nan (PR #163522)

Wenju He via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 15 18:06:18 PDT 2025


https://github.com/wenju-he updated https://github.com/llvm/llvm-project/pull/163522

>From 9a49cb5e37f308fd889dfc5ee96981139c3221d2 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Wed, 15 Oct 2025 10:29:34 +0200
Subject: [PATCH 1/2] [libclc] Fix double NAN_MASK

0x7ff0000000000000 is +inf. Change it to quiet nan 0x7ff8000000000000.
---
 libclc/clc/lib/generic/math/clc_nan.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libclc/clc/lib/generic/math/clc_nan.inc b/libclc/clc/lib/generic/math/clc_nan.inc
index 46e828ba48c7e..47527088960da 100644
--- a/libclc/clc/lib/generic/math/clc_nan.inc
+++ b/libclc/clc/lib/generic/math/clc_nan.inc
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #if __CLC_FPSIZE == 64
-#define NAN_MASK 0x7ff0000000000000ul
+#define NAN_MASK 0x7ff8000000000000ul
 #elif __CLC_FPSIZE == 32
 #define NAN_MASK 0x7fc00000
 #elif __CLC_FPSIZE == 16

>From de867d4ef5b0c33bc48473d9b32703d7b5c4fd72 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Thu, 16 Oct 2025 03:05:01 +0200
Subject: [PATCH 2/2] use __builtin_nan

---
 libclc/clc/lib/generic/math/clc_nan.cl  | 14 +++++++++++++-
 libclc/clc/lib/generic/math/clc_nan.inc | 18 ++++++++++--------
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/libclc/clc/lib/generic/math/clc_nan.cl b/libclc/clc/lib/generic/math/clc_nan.cl
index 0d480175b9c5e..0019ce28739be 100644
--- a/libclc/clc/lib/generic/math/clc_nan.cl
+++ b/libclc/clc/lib/generic/math/clc_nan.cl
@@ -7,7 +7,19 @@
 //===----------------------------------------------------------------------===//
 
 #include <clc/internal/clc.h>
-#include <clc/utils.h>
+#include <clc/math/clc_nan.h>
 
 #define __CLC_BODY <clc_nan.inc>
 #include <clc/math/gentype.inc>
+
+#define __CLC_FUNCTION __clc_nan
+
+#define __CLC_ARG1_TYPE __CLC_S_GENTYPE
+#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#include <clc/math/gentype.inc>
+#undef __CLC_ARG1_TYPE
+
+#define __CLC_ARG1_TYPE __CLC_U_GENTYPE
+#define __CLC_BODY <clc/shared/unary_def_scalarize.inc>
+#include <clc/math/gentype.inc>
+#undef __CLC_ARG1_TYPE
diff --git a/libclc/clc/lib/generic/math/clc_nan.inc b/libclc/clc/lib/generic/math/clc_nan.inc
index 47527088960da..a8c01955e2784 100644
--- a/libclc/clc/lib/generic/math/clc_nan.inc
+++ b/libclc/clc/lib/generic/math/clc_nan.inc
@@ -6,22 +6,24 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if __CLC_VECSIZE_OR_1 == 1
+
 #if __CLC_FPSIZE == 64
-#define NAN_MASK 0x7ff8000000000000ul
+#define __CLC_IMPL_FUNCTION __builtin_nan
 #elif __CLC_FPSIZE == 32
-#define NAN_MASK 0x7fc00000
+#define __CLC_IMPL_FUNCTION __builtin_nanf
 #elif __CLC_FPSIZE == 16
-#define NAN_MASK 0x7e00
+#define __CLC_IMPL_FUNCTION __builtin_nanf16
 #endif
 
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_nan(__CLC_U_GENTYPE code) {
-  const __CLC_U_GENTYPE mask = NAN_MASK;
-  const __CLC_U_GENTYPE res = code | mask;
-  return __CLC_AS_GENTYPE(res);
+  return __CLC_IMPL_FUNCTION("");
 }
 
 _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_nan(__CLC_S_GENTYPE code) {
-  return __clc_nan(__CLC_AS_U_GENTYPE(code));
+  return __CLC_IMPL_FUNCTION("");
 }
 
-#undef NAN_MASK
+#undef __CLC_IMPL_FUNCTION
+
+#endif // __CLC_VECSIZE_OR_1 == 1



More information about the cfe-commits mailing list