diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h --- a/libc/src/__support/FPUtil/FPBits.h +++ b/libc/src/__support/FPUtil/FPBits.h @@ -59,11 +59,6 @@ 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 @@ (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) diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h --- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h +++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h @@ -60,6 +60,10 @@ 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)) &