Skip to content

Commit 87645e9

Browse files
authored
[libc][math][c23] Fix undefined behavior in expxf16.h (#112734)
Fixes the left-shifting of potentially negative signed integers.
1 parent feedb35 commit 87645e9

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

libc/src/math/generic/expxf16.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
108108
float xf = x;
109109
float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
110110
int x_hi_mid = static_cast<int>(kf);
111-
int x_hi = x_hi_mid >> 3;
112-
int x_mid = x_hi_mid & 0x7;
111+
unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
112+
unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
113113
// lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x
114114
float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf);
115115

@@ -155,8 +155,8 @@ LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) {
155155
float xf = x;
156156
float kf = fputil::nearest_integer(xf * (LOG2F_10 * 0x1.0p+3f));
157157
int x_hi_mid = static_cast<int>(kf);
158-
int x_hi = x_hi_mid >> 3;
159-
int x_mid = x_hi_mid & 0x7;
158+
unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
159+
unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
160160
// lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x
161161
float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf);
162162

0 commit comments

Comments
 (0)