Skip to content

Commit 3a7051d

Browse files
bbrezillontstellar
authored andcommitted
libclc: Fix FP_ILOGBNAN definition
Fix FP_ILOGBNAN definition to match the opencl-c-base.h one and guarantee that FP_ILOGBNAN and FP_ILOGB0 are different. Doing that implies fixing ilogb() implementation to return the right value. Signed-off-by: Boris Brezillon <[email protected]> Reviewed By: jvesely Differential Revision: https://reviews.llvm.org/D83473
1 parent 45cc86b commit 3a7051d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

libclc/generic/include/clc/float/definitions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define FLT_EPSILON 0x1.0p-23f
1616

1717
#define FP_ILOGB0 (-2147483647 - 1)
18-
#define FP_ILOGBNAN (-2147483647 - 1)
18+
#define FP_ILOGBNAN 2147483647
1919

2020
#define M_E_F 0x1.5bf0a8p+1f
2121
#define M_LOG2E_F 0x1.715476p+0f

libclc/generic/lib/math/ilogb.cl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ _CLC_OVERLOAD _CLC_DEF int ilogb(float x) {
3131
int rs = -118 - (int) clz(ux & MANTBITS_SP32);
3232
int r = (int) (ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
3333
r = ax < 0x00800000U ? rs : r;
34-
r = ax > EXPBITS_SP32 | ax == 0 ? 0x80000000 : r;
34+
r = ax == 0 ? FP_ILOGB0 : r;
35+
36+
// We could merge those 2 tests and have:
37+
//
38+
// r = ax >= EXPBITS_SP32 ? 0x7fffffff : r
39+
//
40+
// since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
41+
// FP_ILOGBNAN can change without requiring changes to ilogb() code.
42+
r = ax > EXPBITS_SP32 ? FP_ILOGBNAN : r;
3543
r = ax == EXPBITS_SP32 ? 0x7fffffff : r;
3644
return r;
3745
}
@@ -47,7 +55,15 @@ _CLC_OVERLOAD _CLC_DEF int ilogb(double x) {
4755
int r = (int) (ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64;
4856
int rs = -1011 - (int) clz(ax & MANTBITS_DP64);
4957
r = ax < 0x0010000000000000UL ? rs : r;
50-
r = ax > 0x7ff0000000000000UL | ax == 0UL ? 0x80000000 : r;
58+
r = ax == 0UL ? FP_ILOGB0 : r;
59+
60+
// We could merge those 2 tests and have:
61+
//
62+
// r = ax >= 0x7ff0000000000000UL ? 0x7fffffff : r
63+
//
64+
// since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and
65+
// FP_ILOGBNAN can change without requiring changes to ilogb() code.
66+
r = ax > 0x7ff0000000000000UL ? FP_ILOGBNAN : r;
5167
r = ax == 0x7ff0000000000000UL ? 0x7fffffff : r;
5268
return r;
5369
}

0 commit comments

Comments
 (0)