Skip to content

[libc][NFC] Make QNAN_MASK an implementation detail of FPBits #75945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libc/src/__support/FPUtil/FPBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ template <typename T> struct FPBits : private FloatProperties<T> {
using FloatProperties<T>::EXP_LEN;
using FloatProperties<T>::FRACTION_MASK;
using FloatProperties<T>::FRACTION_LEN;

private:
using FloatProperties<T>::QUIET_NAN_MASK;

public:
using FloatProperties<T>::SIGN_MASK;

// Reinterpreting bits as an integer value and interpreting the bits of an
Expand Down Expand Up @@ -90,7 +94,6 @@ template <typename T> struct FPBits : private FloatProperties<T> {
"Data type and integral representation have different sizes.");

static constexpr int MAX_EXPONENT = (1 << EXP_LEN) - 1;

static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
static constexpr StorageType MAX_SUBNORMAL = FRACTION_MASK;
static constexpr StorageType MIN_NORMAL = (StorageType(1) << FRACTION_LEN);
Expand Down
26 changes: 13 additions & 13 deletions libc/src/__support/FPUtil/FloatProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,6 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
return StorageType(1) << position;
}

LIBC_INLINE_VAR static constexpr StorageType QNAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 2) // 0b1100...
: bit_at(SIG_LEN - 1); // 0b1000...

LIBC_INLINE_VAR static constexpr StorageType SNAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
: bit_at(SIG_LEN - 2); // 0b0100...

public:
// The number of bits after the decimal dot when the number is in normal form.
LIBC_INLINE_VAR static constexpr int FRACTION_LEN =
Expand All @@ -165,10 +155,20 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
LIBC_INLINE_VAR static constexpr StorageType EXP_MANT_MASK =
EXP_MASK | SIG_MASK;

protected:
// If a number x is a NAN, then it is a quiet NAN if:
// QuietNaNMask & bits(x) != 0
// Else, it is a signalling NAN.
static constexpr StorageType QUIET_NAN_MASK = QNAN_MASK;
// QUIET_NAN_MASK & bits(x) != 0
LIBC_INLINE_VAR static constexpr StorageType QUIET_NAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 2) // 0b1100...
: bit_at(SIG_LEN - 1); // 0b1000...

// If a number x is a NAN, then it is a signalling NAN if:
// SIGNALING_NAN_MASK & bits(x) != 0
LIBC_INLINE_VAR static constexpr StorageType SIGNALING_NAN_MASK =
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
: bit_at(SIG_LEN - 2); // 0b0100...
};

//-----------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
using FloatProperties<long double>::EXP_LEN;
using FloatProperties<long double>::FRACTION_MASK;
using FloatProperties<long double>::FRACTION_LEN;

private:
using FloatProperties<long double>::QUIET_NAN_MASK;

public:
using FloatProperties<long double>::SIGN_MASK;

static constexpr int MAX_EXPONENT = 0x7FFF;
Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,6 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
index = left_paren;
}
}
nan_mantissa |= fputil::FloatProperties<T>::QUIET_NAN_MASK;
if (result.get_sign()) {
result = FPBits(result.build_quiet_nan(nan_mantissa));
result.set_sign(true);
Expand Down