Skip to content

Commit b1a91b7

Browse files
[libc][NFC] fix uint128 init in float properties (#75084)
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.
1 parent 12cbccc commit b1a91b7

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

libc/src/__support/FPUtil/FloatProperties.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ template <typename T, size_t count> static constexpr T mask_trailing_ones() {
8585
static_assert(cpp::is_unsigned_v<T>);
8686
constexpr unsigned t_bits = CHAR_BIT * sizeof(T);
8787
static_assert(count <= t_bits && "Invalid bit index");
88-
return count == 0 ? 0 : (T(-1) >> (t_bits - count));
88+
// It's important not to initialize T with -1, since T may be BigInt which
89+
// will take -1 as a uint64_t and only initialize the low 64 bits.
90+
return count == 0 ? 0 : ((~T(0)) >> (t_bits - count));
8991
}
9092

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

136138
LIBC_INLINE static constexpr UIntType bit_at(int position) {
137139
return UIntType(1) << position;

0 commit comments

Comments
 (0)