[libc-commits] [libc] [libc][math][c23] Fix undefined behavior in expxf16.h (PR #112734)

via libc-commits libc-commits at lists.llvm.org
Thu Oct 17 08:54:49 PDT 2024


================
@@ -108,8 +108,8 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
   float xf = x;
   float kf = fputil::nearest_integer(xf * 0x1.0p+3f);
   int x_hi_mid = static_cast<int>(kf);
-  int x_hi = x_hi_mid >> 3;
-  int x_mid = x_hi_mid & 0x7;
+  unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3;
+  unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7;
----------------
overmighty wrote:

Casting `kf` (potentially negative `float`) to `unsigned` directly would also be UB: https://eel.is/c++draft/conv.fpint#1.

We really only need `x_hi` to be `unsigned`. I made `x_mid` `unsigned` too for consistency.

https://github.com/llvm/llvm-project/pull/112734


More information about the libc-commits mailing list