[libclc] 32604e2 - [libclc] Fix fmod returning NaN for subnormal inputs (#225351)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 16 21:59:41 PST 2026
Author: Lurie97
Date: 2026-11-16T21:59:36-08:00
New Revision: 32604e2eca9f5ab63e803809dbafeb79e51d0096
URL: https://github.com/llvm/llvm-project/commit/32604e2eca9f5ab63e803809dbafeb79e51d0096
DIFF: https://github.com/llvm/llvm-project/commit/32604e2eca9f5ab63e803809dbafeb79e51d0096.diff
LOG: [libclc] Fix fmod returning NaN for subnormal inputs (#225351)
#124145 changed these functions to use __builtin_isfpclass for better
vectorization. However, some targets mislower isfpclass for subnormals
when denormals are enabled, causing isfinite(subnormal) to return false.
This breaks fmod (#222369), whose |x| < |y| path can return NaN instead
of the subnormal input, e.g.
fmod(-0x1.26e13p-128, -0x1.2afc64p+16) -> nan, expected -0x1.26e13p-128
Replace is_fpclass with simple comparisons:
isnan(x) -> x != x
isinf(x) -> fabs(x) == INFINITY
isfinite(x) -> fabs(x) < INFINITY
These forms still vectorize while avoiding the isfpclass issue.
isnormal/issubnormal continue to use isfpclass because they need to
distinguish subnormal values. clc_fmod.cl is left unchanged.
Verified on i.MX95 Mali-G310 (Panfrost/Rusticl) advertising
CL_FP_DENORM:
OpenCL-CTS math_brute_force fmod passes 2 of 2 sub-tests (0.00 ulp for
fp32 and fp16) with no change to clc_fmod.cl.
AI tool disclosure: I used an AI assistant to help draft the code change
and the PR description. All changes were reviewed and tested by me against
the OpenCL CTS math_brute_force suite (fmod) on i.MX95 Mali-G310
(Panfrost/Rusticl), and I take responsibility for their correctness.
Signed-off-by: jiajia Qian <jiajia.qian at nxp.com>
Added:
Modified:
libclc/clc/include/clc/relational/relational.h
libclc/clc/lib/generic/relational/clc_isfinite.cl
libclc/clc/lib/generic/relational/clc_isinf.cl
libclc/clc/lib/generic/relational/clc_isnan.cl
libclc/test/math/cos.cl
Removed:
################################################################################
diff --git a/libclc/clc/include/clc/relational/relational.h b/libclc/clc/include/clc/relational/relational.h
index 32ed9f431ddbdf..1d04d0bb05d1e4 100644
--- a/libclc/clc/include/clc/relational/relational.h
+++ b/libclc/clc/include/clc/relational/relational.h
@@ -40,6 +40,30 @@
return _CLC_RELATIONAL_OP(x, y); \
}
+// Define a unary relational builtin from an ordinary expression in terms of x.
+// The scalar version returns 1/0 and the vector versions return -1/0, matching
+// the OpenCL relational convention that ordinary comparison operators already
+// produce, so EXPR should be a plain comparison rather than __builtin_isfpclass
+// (which some targets mislower for subnormals when denormals are supported).
+#define _CLC_DEFINE_RELATIONAL_UNARY(RET_TYPE, VEC_RET_TYPE, __CLC_FUNCTION, \
+ EXPR, ARG_TYPE) \
+ _CLC_DEF _CLC_OVERLOAD RET_TYPE __CLC_FUNCTION(ARG_TYPE x) { return EXPR; } \
+ _CLC_DEF _CLC_OVERLOAD VEC_RET_TYPE##2 __CLC_FUNCTION(ARG_TYPE##2 x) { \
+ return EXPR; \
+ } \
+ _CLC_DEF _CLC_OVERLOAD VEC_RET_TYPE##3 __CLC_FUNCTION(ARG_TYPE##3 x) { \
+ return EXPR; \
+ } \
+ _CLC_DEF _CLC_OVERLOAD VEC_RET_TYPE##4 __CLC_FUNCTION(ARG_TYPE##4 x) { \
+ return EXPR; \
+ } \
+ _CLC_DEF _CLC_OVERLOAD VEC_RET_TYPE##8 __CLC_FUNCTION(ARG_TYPE##8 x) { \
+ return EXPR; \
+ } \
+ _CLC_DEF _CLC_OVERLOAD VEC_RET_TYPE##16 __CLC_FUNCTION(ARG_TYPE##16 x) { \
+ return EXPR; \
+ }
+
#define fcNan (__FPCLASS_SNAN | __FPCLASS_QNAN)
#define fcInf (__FPCLASS_POSINF | __FPCLASS_NEGINF)
#define fcNormal (__FPCLASS_POSNORMAL | __FPCLASS_NEGNORMAL)
diff --git a/libclc/clc/lib/generic/relational/clc_isfinite.cl b/libclc/clc/lib/generic/relational/clc_isfinite.cl
index dbd181e29aa172..3b9577b1029887 100644
--- a/libclc/clc/lib/generic/relational/clc_isfinite.cl
+++ b/libclc/clc/lib/generic/relational/clc_isfinite.cl
@@ -6,10 +6,13 @@
//
//===----------------------------------------------------------------------===//
+#include "clc/float/definitions.h"
#include "clc/internal/clc.h"
+#include "clc/math/clc_fabs.h"
#include "clc/relational/relational.h"
-_CLC_DEFINE_ISFPCLASS(int, int, __clc_isfinite, fcFinite, float)
+_CLC_DEFINE_RELATIONAL_UNARY(int, int, __clc_isfinite,
+ (__clc_fabs(x) < (float)INFINITY), float)
#ifdef cl_khr_fp64
@@ -17,7 +20,8 @@ _CLC_DEFINE_ISFPCLASS(int, int, __clc_isfinite, fcFinite, float)
// The scalar version of __clc_isfinite(double) returns an int, but the vector
// versions return long.
-_CLC_DEFINE_ISFPCLASS(int, long, __clc_isfinite, fcFinite, double)
+_CLC_DEFINE_RELATIONAL_UNARY(int, long, __clc_isfinite,
+ (__clc_fabs(x) < (double)INFINITY), double)
#endif
@@ -27,6 +31,7 @@ _CLC_DEFINE_ISFPCLASS(int, long, __clc_isfinite, fcFinite, double)
// The scalar version of __clc_isfinite(half) returns an int, but the vector
// versions return short.
-_CLC_DEFINE_ISFPCLASS(int, short, __clc_isfinite, fcFinite, half)
+_CLC_DEFINE_RELATIONAL_UNARY(int, short, __clc_isfinite,
+ (__clc_fabs(x) < (half)INFINITY), half)
#endif
diff --git a/libclc/clc/lib/generic/relational/clc_isinf.cl b/libclc/clc/lib/generic/relational/clc_isinf.cl
index 62bb8ed063459f..5aa88c88b4cc8d 100644
--- a/libclc/clc/lib/generic/relational/clc_isinf.cl
+++ b/libclc/clc/lib/generic/relational/clc_isinf.cl
@@ -6,10 +6,13 @@
//
//===----------------------------------------------------------------------===//
+#include "clc/float/definitions.h"
#include "clc/internal/clc.h"
+#include "clc/math/clc_fabs.h"
#include "clc/relational/relational.h"
-_CLC_DEFINE_ISFPCLASS(int, int, __clc_isinf, fcInf, float)
+_CLC_DEFINE_RELATIONAL_UNARY(int, int, __clc_isinf,
+ (__clc_fabs(x) == (float)INFINITY), float)
#ifdef cl_khr_fp64
@@ -17,7 +20,8 @@ _CLC_DEFINE_ISFPCLASS(int, int, __clc_isinf, fcInf, float)
// The scalar version of __clc_isinf(double) returns an int, but the vector
// versions return long.
-_CLC_DEFINE_ISFPCLASS(int, long, __clc_isinf, fcInf, double)
+_CLC_DEFINE_RELATIONAL_UNARY(int, long, __clc_isinf,
+ (__clc_fabs(x) == (double)INFINITY), double)
#endif
@@ -27,6 +31,7 @@ _CLC_DEFINE_ISFPCLASS(int, long, __clc_isinf, fcInf, double)
// The scalar version of __clc_isinf(half) returns an int, but the vector
// versions return short.
-_CLC_DEFINE_ISFPCLASS(int, short, __clc_isinf, fcInf, half)
+_CLC_DEFINE_RELATIONAL_UNARY(int, short, __clc_isinf,
+ (__clc_fabs(x) == (half)INFINITY), half)
#endif
diff --git a/libclc/clc/lib/generic/relational/clc_isnan.cl b/libclc/clc/lib/generic/relational/clc_isnan.cl
index ba97f88ce76f7b..ac05ea813a131d 100644
--- a/libclc/clc/lib/generic/relational/clc_isnan.cl
+++ b/libclc/clc/lib/generic/relational/clc_isnan.cl
@@ -9,7 +9,7 @@
#include "clc/internal/clc.h"
#include "clc/relational/relational.h"
-_CLC_DEFINE_ISFPCLASS(int, int, __clc_isnan, fcNan, float)
+_CLC_DEFINE_RELATIONAL_UNARY(int, int, __clc_isnan, (x != x), float)
#ifdef cl_khr_fp64
@@ -17,7 +17,7 @@ _CLC_DEFINE_ISFPCLASS(int, int, __clc_isnan, fcNan, float)
// The scalar version of __clc_isnan(double) returns an int, but the vector
// versions return a long.
-_CLC_DEFINE_ISFPCLASS(int, long, __clc_isnan, fcNan, double)
+_CLC_DEFINE_RELATIONAL_UNARY(int, long, __clc_isnan, (x != x), double)
#endif
@@ -27,6 +27,6 @@ _CLC_DEFINE_ISFPCLASS(int, long, __clc_isnan, fcNan, double)
// The scalar version of __clc_isnan(half) returns an int, but the vector
// versions return a short.
-_CLC_DEFINE_ISFPCLASS(int, short, __clc_isnan, fcNan, half)
+_CLC_DEFINE_RELATIONAL_UNARY(int, short, __clc_isnan, (x != x), half)
#endif
diff --git a/libclc/test/math/cos.cl b/libclc/test/math/cos.cl
index dedeea6059ff69..8cd3f5cf6be70f 100644
--- a/libclc/test/math/cos.cl
+++ b/libclc/test/math/cos.cl
@@ -7,7 +7,7 @@
// AMDGCN-SAME: float noundef [[X:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
// AMDGCN-NEXT: [[ENTRY:.*:]]
// AMDGCN-NEXT: [[TMP0:%.*]] = tail call float @llvm.fabs.f32(float [[X]])
-// AMDGCN-NEXT: [[TMP1:%.*]] = fcmp une float [[TMP0]], +inf
+// AMDGCN-NEXT: [[TMP1:%.*]] = fcmp{{.*}} une float [[TMP0]], +inf
// AMDGCN-NEXT: [[TMP2:%.*]] = select contract i1 [[TMP1]], float [[TMP0]], float +qnan
// AMDGCN-NEXT: [[TMP3:%.*]] = fcmp ult float [[TMP2]], 1.310720e+05
// AMDGCN-NEXT: br i1 [[TMP3]], label %[[BB109:.*]], label %[[BB4:.*]]
@@ -129,7 +129,7 @@
// AMDGCN-NEXT: [[TMP114:%.*]] = tail call contract noundef float @llvm.fma.f32(float [[TMP111]], float f0xA7C234C4, float [[TMP113]])
// AMDGCN-NEXT: [[TMP115:%.*]] = fptosi float [[TMP111]] to i32
// AMDGCN-NEXT: [[TMP116:%.*]] = and i32 [[TMP115]], 3
-// AMDGCN-NEXT: [[TMP117:%.*]] = fcmp ord float [[TMP111]], 0.000000e+00
+// AMDGCN-NEXT: [[TMP117:%.*]] = fcmp{{.*}} ord float [[TMP111]], 0.000000e+00
// AMDGCN-NEXT: [[TMP118:%.*]] = select i1 [[TMP117]], i32 [[TMP116]], i32 0
// AMDGCN-NEXT: br label %[[_Z3COSF_EXIT]]
// AMDGCN: [[_Z3COSF_EXIT]]:
More information about the cfe-commits
mailing list