Skip to content

Commit

Permalink
[libc] Fix undefined behavior of left shifting signed integer in exp2…
Browse files Browse the repository at this point in the history
…f.cpp.

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

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D152336
  • Loading branch information
lntue committed Jun 7, 2023
1 parent ce8fa36 commit c0a751a
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions libc/src/math/generic/exp2f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c0a751a

Please sign in to comment.