[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
This commit is contained in:
Michael Jones 2022-07-06 16:51:12 -07:00
parent 8aa596584a
commit 6656029a49
2 changed files with 13 additions and 5 deletions

View File

@ -59,11 +59,6 @@ template <typename T> struct FPBits {
UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
// The function return mantissa with implicit bit set for normal values.
constexpr UIntType get_explicit_mantissa() {
return (FloatProp::MANTISSA_MASK + 1) | (FloatProp::MANTISSA_MASK & bits);
}
void set_unbiased_exponent(UIntType expVal) {
expVal = (expVal << (FloatProp::MANTISSA_WIDTH)) & FloatProp::EXPONENT_MASK;
bits &= ~(FloatProp::EXPONENT_MASK);
@ -75,6 +70,15 @@ template <typename T> struct FPBits {
(FloatProp::MANTISSA_WIDTH));
}
// The function return mantissa with the implicit bit set iff the current
// value is a valid normal number.
constexpr UIntType get_explicit_mantissa() {
return ((get_unbiased_exponent() > 0 && !is_inf_or_nan())
? (FloatProp::MANTISSA_MASK + 1)
: 0) |
(FloatProp::MANTISSA_MASK & bits);
}
void set_sign(bool signVal) {
bits |= FloatProp::SIGN_MASK;
if (!signVal)

View File

@ -60,6 +60,10 @@ template <> struct FPBits<long double> {
UIntType get_mantissa() const { return bits & FloatProp::MANTISSA_MASK; }
UIntType get_explicit_mantissa() const {
return bits & (FloatProp::MANTISSA_MASK | FloatProp::EXPLICIT_BIT_MASK);
}
void set_unbiased_exponent(UIntType expVal) {
expVal =
(expVal << (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH)) &