[libc-commits] [libc] c0a751a - [libc] Fix undefined behavior of left shifting signed integer in exp2f.cpp.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Tue Jun 6 22:15:35 PDT 2023


Author: Tue Ly
Date: 2023-06-07T01:15:18-04:00
New Revision: c0a751ae3da372fdedd8ace4deabd6862dbcc660

URL: https://github.com/llvm/llvm-project/commit/c0a751ae3da372fdedd8ace4deabd6862dbcc660
DIFF: https://github.com/llvm/llvm-project/commit/c0a751ae3da372fdedd8ace4deabd6862dbcc660.diff

LOG: [libc] Fix undefined behavior of left shifting signed integer in exp2f.cpp.

Fix undefined behavior of left shifting signed integer in exp2f.cpp.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D152336

Added: 
    

Modified: 
    libc/src/math/generic/exp2f.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/math/generic/exp2f.cpp b/libc/src/math/generic/exp2f.cpp
index 15f35d0da82f5..f68d553c65deb 100644
--- a/libc/src/math/generic/exp2f.cpp
+++ b/libc/src/math/generic/exp2f.cpp
@@ -107,8 +107,9 @@ LLVM_LIBC_FUNCTION(float, exp2f, (float x)) {
   int k = static_cast<int>(kf);
   // hi = floor(kf * 2^(-4))
   // exp_hi = shift hi to the exponent field of double precision.
-  int64_t exp_hi = static_cast<int64_t>(k >> ExpBase::MID_BITS)
-                   << fputil::FloatProperties<double>::MANTISSA_WIDTH;
+  int64_t exp_hi =
+      static_cast<int64_t>(static_cast<uint64_t>(k >> ExpBase::MID_BITS)
+                           << fputil::FloatProperties<double>::MANTISSA_WIDTH);
   // mh = 2^hi * 2^mid
   // mh_bits = bit field of mh
   int64_t mh_bits = ExpBase::EXP_2_MID[k & ExpBase::MID_MASK] + exp_hi;


        


More information about the libc-commits mailing list