Skip to content

Commit

Permalink
[libc][NFC] fix uint128 init in float properties (#75084)
Browse files Browse the repository at this point in the history
An unsigned integer was being initialized with -1 to set all its bits to
1, but this doesn't work for the BigInt class which may be used as an
integer type on systems that don't support uint128 natively. This patch
moves the initialization to use ~T(0) instead.
  • Loading branch information
michaelrj-google committed Dec 11, 2023
1 parent 12cbccc commit b1a91b7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libc/src/__support/FPUtil/FloatProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ template <typename T, size_t count> static constexpr T mask_trailing_ones() {
static_assert(cpp::is_unsigned_v<T>);
constexpr unsigned t_bits = CHAR_BIT * sizeof(T);
static_assert(count <= t_bits && "Invalid bit index");
return count == 0 ? 0 : (T(-1) >> (t_bits - count));
// It's important not to initialize T with -1, since T may be BigInt which
// will take -1 as a uint64_t and only initialize the low 64 bits.
return count == 0 ? 0 : ((~T(0)) >> (t_bits - count));
}

// Derives more properties from 'FPBaseProperties' above.
Expand Down Expand Up @@ -131,7 +133,7 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
LIBC_INLINE_VAR static constexpr UIntType FP_MASK =
mask_trailing_ones<UIntType, TOTAL_BITS>();
static_assert((SIG_MASK & EXP_MASK & SIGN_MASK_) == 0, "masks disjoint");
static_assert((SIG_MASK | EXP_MASK | SIGN_MASK_) == FP_MASK, "masks covers");
static_assert((SIG_MASK | EXP_MASK | SIGN_MASK_) == FP_MASK, "masks cover");

LIBC_INLINE static constexpr UIntType bit_at(int position) {
return UIntType(1) << position;
Expand Down

0 comments on commit b1a91b7

Please sign in to comment.