Skip to content

Commit 6656029

Browse files
[libc][nfc] update get_explicit_mantissa
The get_explicit_mantissa function returns the mantissa of an FPBits floating point value with the implicit leading 1, if appropriate. This function existed previously, but did not handle non-normal numbers properly. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D129241
1 parent 8aa5965 commit 6656029

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

libc/src/__support/FPUtil/FPBits.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ template <typename T> struct FPBits {
5959

6060
UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
6161

62-
// The function return mantissa with implicit bit set for normal values.
63-
constexpr UIntType get_explicit_mantissa() {
64-
return (FloatProp::MANTISSA_MASK + 1) | (FloatProp::MANTISSA_MASK & bits);
65-
}
66-
6762
void set_unbiased_exponent(UIntType expVal) {
6863
expVal = (expVal << (FloatProp::MANTISSA_WIDTH)) & FloatProp::EXPONENT_MASK;
6964
bits &= ~(FloatProp::EXPONENT_MASK);
@@ -75,6 +70,15 @@ template <typename T> struct FPBits {
7570
(FloatProp::MANTISSA_WIDTH));
7671
}
7772

73+
// The function return mantissa with the implicit bit set iff the current
74+
// value is a valid normal number.
75+
constexpr UIntType get_explicit_mantissa() {
76+
return ((get_unbiased_exponent() > 0 && !is_inf_or_nan())
77+
? (FloatProp::MANTISSA_MASK + 1)
78+
: 0) |
79+
(FloatProp::MANTISSA_MASK & bits);
80+
}
81+
7882
void set_sign(bool signVal) {
7983
bits |= FloatProp::SIGN_MASK;
8084
if (!signVal)

libc/src/__support/FPUtil/x86_64/LongDoubleBits.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ template <> struct FPBits<long double> {
6060

6161
UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
6262

63+
UIntType get_explicit_mantissa() const {
64+
return bits & (FloatProp::MANTISSA_MASK | FloatProp::EXPLICIT_BIT_MASK);
65+
}
66+
6367
void set_unbiased_exponent(UIntType expVal) {
6468
expVal =
6569
(expVal << (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH)) &

0 commit comments

Comments
 (0)