diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h index be700285de828..2465158bb2cdf 100644 --- a/libc/src/__support/FPUtil/FPBits.h +++ b/libc/src/__support/FPUtil/FPBits.h @@ -64,38 +64,46 @@ LIBC_INLINE_VAR constexpr Sign Sign::POS = Sign(false); // └─────────▲─────────┘ // │ // ┌─────────┴─────────┐ -// │ FPRepBase │ +// │ FPStorage │ // └─────────▲─────────┘ // │ // ┌────────────┴─────────────┐ // │ │ -// ┌────────┴──────┐ ┌─────────────┴──────────────┐ -// │ FPRep │ │ FPRep │ │ FPRepSem │ +// └───────────┘ +// │ +// ┌─────┴─────┐ // │ FPBits │ // └───────────┘ // -// - 'FPLayout' defines only a few constants, namely the 'StorageType' and the -// length of the sign, the exponent and significand parts. -// - 'FPRepBase' builds more constants on top of those from 'FPLayout' like -// exponent bias, shifts and masks. It also defines tools to assemble or test +// - 'FPLayout' defines only a few constants, namely the 'StorageType' and +// length of the sign, the exponent, fraction and significand parts. +// - 'FPStorage' builds more constants on top of those from 'FPLayout' like +// exponent bias and masks. It also holds the bit representation of the +// floating point as a 'StorageType' type and defines tools to assemble or test // these parts. -// - 'FPRep' defines functions to interact with the floating point -// representation. The default implementation is the one for 'IEEE754', a -// specialization is provided for X86 Extended Precision that has a different -// encoding. -// - 'FPBits' is templated on the platform floating point types. Contrary to -// 'FPRep' that is platform agnostic 'FPBits' is architecture dependent. +// - 'FPRepSem' defines functions to interact semantically with the floating +// point representation. The default implementation is the one for 'IEEE754', a +// specialization is provided for X86 Extended Precision. +// - 'FPRep' derives from 'FPRepSem' and adds functions that are common to all +// implementations. +// - 'FPBits' exposes all functions from 'FPRep' but operates on the native C++ +// floating point type instead of 'FPType'. namespace internal { // Defines the layout (sign, exponent, significand) of a floating point type in // memory. It also defines its associated StorageType, i.e., the unsigned // integer type used to manipulate its representation. +// Additionally we provide the fractional part length, i.e., the number of bits +// after the decimal dot when the number is in normal form. template struct FPLayout {}; template <> struct FPLayout { @@ -103,6 +111,7 @@ template <> struct FPLayout { LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1; LIBC_INLINE_VAR static constexpr int EXP_LEN = 5; LIBC_INLINE_VAR static constexpr int SIG_LEN = 10; + LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SIG_LEN; }; template <> struct FPLayout { @@ -110,6 +119,7 @@ template <> struct FPLayout { LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1; LIBC_INLINE_VAR static constexpr int EXP_LEN = 8; LIBC_INLINE_VAR static constexpr int SIG_LEN = 23; + LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SIG_LEN; }; template <> struct FPLayout { @@ -117,6 +127,7 @@ template <> struct FPLayout { LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1; LIBC_INLINE_VAR static constexpr int EXP_LEN = 11; LIBC_INLINE_VAR static constexpr int SIG_LEN = 52; + LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SIG_LEN; }; template <> struct FPLayout { @@ -124,6 +135,7 @@ template <> struct FPLayout { LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1; LIBC_INLINE_VAR static constexpr int EXP_LEN = 15; LIBC_INLINE_VAR static constexpr int SIG_LEN = 112; + LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SIG_LEN; }; template <> struct FPLayout { @@ -131,23 +143,22 @@ template <> struct FPLayout { LIBC_INLINE_VAR static constexpr int SIGN_LEN = 1; LIBC_INLINE_VAR static constexpr int EXP_LEN = 15; LIBC_INLINE_VAR static constexpr int SIG_LEN = 64; + LIBC_INLINE_VAR static constexpr int FRACTION_LEN = SIG_LEN - 1; }; -} // namespace internal - -// FPRepBase derives useful constants from the FPLayout. -template -struct FPRepBase : public internal::FPLayout { -private: - using UP = internal::FPLayout; +// FPStorage derives useful constants from the FPLayout above. +template struct FPStorage : public FPLayout { + using UP = FPLayout; -public: using UP::EXP_LEN; // The number of bits for the *exponent* part using UP::SIG_LEN; // The number of bits for the *significand* part using UP::SIGN_LEN; // The number of bits for the *sign* part // For convenience, the sum of `SIG_LEN`, `EXP_LEN`, and `SIGN_LEN`. LIBC_INLINE_VAR static constexpr int TOTAL_LEN = SIGN_LEN + EXP_LEN + SIG_LEN; + // The number of bits after the decimal dot when the number is in normal form. + using UP::FRACTION_LEN; + // An unsigned integer that is wide enough to contain all of the floating // point bits. using StorageType = typename UP::StorageType; @@ -162,41 +173,30 @@ struct FPRepBase : public internal::FPLayout { (1U << (EXP_LEN - 1U)) - 1U; static_assert(EXP_BIAS > 0); -protected: - // The shift amount to get the *significand* part to the least significant - // bit. Always `0` but kept for consistency. - LIBC_INLINE_VAR static constexpr int SIG_MASK_SHIFT = 0; - // The shift amount to get the *exponent* part to the least significant bit. - LIBC_INLINE_VAR static constexpr int EXP_MASK_SHIFT = SIG_LEN; - // The shift amount to get the *sign* part to the least significant bit. - LIBC_INLINE_VAR static constexpr int SIGN_MASK_SHIFT = SIG_LEN + EXP_LEN; - // The bit pattern that keeps only the *significand* part. LIBC_INLINE_VAR static constexpr StorageType SIG_MASK = - mask_trailing_ones() << SIG_MASK_SHIFT; - -public: + mask_trailing_ones(); // The bit pattern that keeps only the *exponent* part. LIBC_INLINE_VAR static constexpr StorageType EXP_MASK = - mask_trailing_ones() << EXP_MASK_SHIFT; + mask_trailing_ones() << SIG_LEN; // The bit pattern that keeps only the *sign* part. LIBC_INLINE_VAR static constexpr StorageType SIGN_MASK = - mask_trailing_ones() << SIGN_MASK_SHIFT; + mask_trailing_ones() << (EXP_LEN + SIG_LEN); // The bit pattern that keeps only the *exponent + significand* part. LIBC_INLINE_VAR static constexpr StorageType EXP_SIG_MASK = mask_trailing_ones(); // The bit pattern that keeps only the *sign + exponent + significand* part. LIBC_INLINE_VAR static constexpr StorageType FP_MASK = mask_trailing_ones(); + // The bit pattern that keeps only the *fraction* part. + // i.e., the *significand* without the leading one. + LIBC_INLINE_VAR static constexpr StorageType FRACTION_MASK = + mask_trailing_ones(); static_assert((SIG_MASK & EXP_MASK & SIGN_MASK) == 0, "masks disjoint"); static_assert((SIG_MASK | EXP_MASK | SIGN_MASK) == FP_MASK, "masks cover"); protected: - LIBC_INLINE static constexpr StorageType bit_at(int position) { - return StorageType(1) << position; - } - // A stongly typed integer that prevents mixing and matching integers with // different semantics. template struct TypedInt { @@ -248,7 +248,7 @@ struct FPRepBase : public internal::FPLayout { // An opaque type to store a floating point significand. // We define special values but it is valid to create arbitrary values as long - // as they are in the range [BITS_ALL_ZEROES, BITS_ALL_ONES]. + // as they are in the range [ZERO, BITS_ALL_ONES]. // Note that the semantics of the Significand are implementation dependent. // Values greater than BITS_ALL_ONES are truncated. struct Significand : public TypedInt { @@ -277,10 +277,8 @@ struct FPRepBase : public internal::FPLayout { return Significand(StorageType(1)); } LIBC_INLINE static constexpr auto MSB() { - return Significand(StorageType(bit_at(SIG_LEN - 1))); + return Significand(StorageType(1) << (SIG_LEN - 1)); } - // Aliases - LIBC_INLINE static constexpr auto BITS_ALL_ZEROES() { return ZERO(); } LIBC_INLINE static constexpr auto BITS_ALL_ONES() { return Significand(SIG_MASK); } @@ -306,204 +304,112 @@ struct FPRepBase : public internal::FPLayout { return encode(exp, sig); } + // The floating point number representation as an unsigned integer. + StorageType bits{}; + + LIBC_INLINE constexpr FPStorage() : bits(0) {} + LIBC_INLINE constexpr FPStorage(StorageType value) : bits(value) {} + + // Observers LIBC_INLINE constexpr StorageType exp_bits() const { return bits & EXP_MASK; } LIBC_INLINE constexpr StorageType sig_bits() const { return bits & SIG_MASK; } LIBC_INLINE constexpr StorageType exp_sig_bits() const { return bits & EXP_SIG_MASK; } - -private: - // Merge bits from 'a' and 'b' values according to 'mask'. - // Use 'a' bits when corresponding 'mask' bits are zeroes and 'b' bits when - // corresponding bits are ones. - LIBC_INLINE static constexpr StorageType merge(StorageType a, StorageType b, - StorageType mask) { - // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge - return a ^ ((a ^ b) & mask); - } - -protected: - // The number of bits after the decimal dot when the number is in normal form. - LIBC_INLINE_VAR static constexpr int FRACTION_LEN = - fp_type == FPType::X86_Binary80 ? SIG_LEN - 1 : SIG_LEN; - LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_PRECISION = - FRACTION_LEN + 1; - LIBC_INLINE_VAR static constexpr StorageType FRACTION_MASK = - mask_trailing_ones(); - - // The floating point number representation as an unsigned integer. - StorageType bits = 0; - -public: - LIBC_INLINE constexpr Sign sign() const { - return (bits & SIGN_MASK) ? Sign::NEG : Sign::POS; - } - - LIBC_INLINE constexpr void set_sign(Sign signVal) { - if (sign() != signVal) - bits ^= SIGN_MASK; - } - - LIBC_INLINE constexpr StorageType get_mantissa() const { - return bits & FRACTION_MASK; - } - - LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) { - bits = merge(bits, mantVal, FRACTION_MASK); - } - - LIBC_INLINE constexpr uint16_t get_biased_exponent() const { - return uint16_t((bits & EXP_MASK) >> EXP_MASK_SHIFT); - } - - LIBC_INLINE constexpr void set_biased_exponent(StorageType biased) { - bits = merge(bits, biased << EXP_MASK_SHIFT, EXP_MASK); - } - - LIBC_INLINE constexpr int get_exponent() const { - return int(get_biased_exponent()) - EXP_BIAS; - } - - // If the number is subnormal, the exponent is treated as if it were the - // minimum exponent for a normal number. This is to keep continuity between - // the normal and subnormal ranges, but it causes problems for functions where - // values are calculated from the exponent, since just subtracting the bias - // will give a slightly incorrect result. Additionally, zero has an exponent - // of zero, and that should actually be treated as zero. - LIBC_INLINE constexpr int get_explicit_exponent() const { - const int biased_exp = int(get_biased_exponent()); - if (is_zero()) { - return 0; - } else if (biased_exp == 0) { - return 1 - EXP_BIAS; - } else { - return biased_exp - EXP_BIAS; - } - } - - LIBC_INLINE constexpr StorageType uintval() const { return bits & FP_MASK; } - LIBC_INLINE constexpr void set_uintval(StorageType value) { - bits = (value & FP_MASK); - } - - LIBC_INLINE constexpr bool is_zero() const { return exp_sig_bits() == 0; } - - LIBC_INLINE - constexpr bool is_subnormal() const { - return exp_bits() == encode(BiasedExponent::BITS_ALL_ZEROES()); - } - - LIBC_INLINE constexpr bool is_neg() const { return sign().is_neg(); } - LIBC_INLINE constexpr bool is_pos() const { return sign().is_pos(); } }; -namespace internal { - -// Manipulates the representation of a floating point number defined by its -// FPType. This layer is architecture agnostic and does not handle C++ floating -// point types directly ('float', 'double' and 'long double'). Use the FPBits -// below if needed. -// -// TODO: Specialize this class for FPType::X86_Binary80 and remove ad-hoc logic -// from FPRepBase. -template struct FPRep : public FPRepBase { - using UP = FPRepBase; +// This layer defines all functions that are specific to how the the floating +// point type is encoded. It enables constructions, modification and observation +// of values manipulated as 'StorageType'. +template +struct FPRepSem : public FPStorage { + using UP = FPStorage; using typename UP::StorageType; using UP::FRACTION_LEN; using UP::FRACTION_MASK; - using UP::MANTISSA_PRECISION; protected: - using typename UP::BiasedExponent; - using typename UP::Exponent; - using typename UP::Significand; + using BiasedExp = typename UP::BiasedExponent; + using Exp = typename UP::Exponent; + using Sig = typename UP::Significand; using UP::encode; using UP::exp_bits; using UP::exp_sig_bits; using UP::sig_bits; + using UP::UP; public: - LIBC_INLINE constexpr bool is_nan() const { - return exp_sig_bits() > - encode(BiasedExponent::BITS_ALL_ONES(), Significand::ZERO()); + // Builders + LIBC_INLINE static constexpr RetT one(Sign sign = Sign::POS) { + return RetT(encode(sign, Exp::ZERO(), Sig::ZERO())); } - LIBC_INLINE constexpr bool is_quiet_nan() const { - return exp_sig_bits() >= - encode(BiasedExponent::BITS_ALL_ONES(), Significand::MSB()); + LIBC_INLINE static constexpr RetT min_subnormal(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ZEROES(), Sig::LSB())); } - LIBC_INLINE constexpr bool is_signaling_nan() const { - return is_nan() && !is_quiet_nan(); + LIBC_INLINE static constexpr RetT max_subnormal(Sign sign = Sign::POS) { + return RetT( + encode(sign, BiasedExp::BITS_ALL_ZEROES(), Sig::BITS_ALL_ONES())); } - LIBC_INLINE constexpr bool is_inf() const { - return exp_sig_bits() == - encode(BiasedExponent::BITS_ALL_ONES(), Significand::ZERO()); + LIBC_INLINE static constexpr RetT min_normal(Sign sign = Sign::POS) { + return RetT(encode(sign, Exp::MIN(), Sig::ZERO())); } - LIBC_INLINE constexpr bool is_finite() const { - return exp_bits() != encode(BiasedExponent::BITS_ALL_ONES()); + LIBC_INLINE static constexpr RetT max_normal(Sign sign = Sign::POS) { + return RetT(encode(sign, Exp::MAX(), Sig::BITS_ALL_ONES())); } - LIBC_INLINE constexpr bool is_normal() const { - return is_finite() && !UP::is_subnormal(); + LIBC_INLINE static constexpr RetT inf(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), Sig::ZERO())); } - - LIBC_INLINE static constexpr StorageType zero(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::ZERO()); + LIBC_INLINE static constexpr RetT build_nan(Sign sign = Sign::POS, + StorageType v = 0) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), + (v ? Sig(v) : (Sig::MSB() >> 1)))); } - LIBC_INLINE static constexpr StorageType one(Sign sign = Sign::POS) { - return encode(sign, Exponent::ZERO(), Significand::ZERO()); + LIBC_INLINE static constexpr RetT build_quiet_nan(Sign sign = Sign::POS, + StorageType v = 0) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), Sig::MSB() | Sig(v))); } - LIBC_INLINE static constexpr StorageType - min_subnormal(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::LSB()); + + // Observers + LIBC_INLINE constexpr bool is_nan() const { + return exp_sig_bits() > encode(BiasedExp::BITS_ALL_ONES(), Sig::ZERO()); } - LIBC_INLINE static constexpr StorageType - max_subnormal(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), - Significand::BITS_ALL_ONES()); + LIBC_INLINE constexpr bool is_quiet_nan() const { + return exp_sig_bits() >= encode(BiasedExp::BITS_ALL_ONES(), Sig::MSB()); } - LIBC_INLINE static constexpr StorageType min_normal(Sign sign = Sign::POS) { - return encode(sign, Exponent::MIN(), Significand::ZERO()); + LIBC_INLINE constexpr bool is_signaling_nan() const { + return is_nan() && !is_quiet_nan(); } - LIBC_INLINE static constexpr StorageType max_normal(Sign sign = Sign::POS) { - return encode(sign, Exponent::MAX(), Significand::BITS_ALL_ONES()); + LIBC_INLINE constexpr bool is_inf() const { + return exp_sig_bits() == encode(BiasedExp::BITS_ALL_ONES(), Sig::ZERO()); } - LIBC_INLINE static constexpr StorageType inf(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), Significand::ZERO()); + LIBC_INLINE constexpr bool is_finite() const { + return exp_bits() != encode(BiasedExp::BITS_ALL_ONES()); } - LIBC_INLINE static constexpr StorageType build_nan(Sign sign = Sign::POS, - StorageType v = 0) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), - (v ? Significand(v) : (Significand::MSB() >> 1))); + LIBC_INLINE + constexpr bool is_subnormal() const { + return exp_bits() == encode(BiasedExp::BITS_ALL_ZEROES()); } - LIBC_INLINE static constexpr StorageType - build_quiet_nan(Sign sign = Sign::POS, StorageType v = 0) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), - Significand::MSB() | Significand(v)); + LIBC_INLINE constexpr bool is_normal() const { + return is_finite() && !UP::is_subnormal(); } - - // The function return mantissa with the implicit bit set iff the current + // Returns the mantissa with the implicit bit set iff the current // value is a valid normal number. LIBC_INLINE constexpr StorageType get_explicit_mantissa() { - if (UP::is_subnormal()) + if (is_subnormal()) return sig_bits(); return (StorageType(1) << UP::SIG_LEN) | sig_bits(); } }; // Specialization for the X86 Extended Precision type. -template <> -struct FPRep : public FPRepBase { - using UP = FPRepBase; +template +struct FPRepSem + : public FPStorage { + using UP = FPStorage; using typename UP::StorageType; using UP::FRACTION_LEN; using UP::FRACTION_MASK; - using UP::MANTISSA_PRECISION; - -protected: - using typename UP::BiasedExponent; - using typename UP::Significand; - using UP::encode; -public: // The x86 80 bit float represents the leading digit of the mantissa // explicitly. This is the mask for that bit. static constexpr StorageType EXPLICIT_BIT_MASK = StorageType(1) @@ -515,6 +421,45 @@ struct FPRep : public FPRepBase { "the explicit bit and the fractional part should cover the " "whole significand"); +protected: + using BiasedExp = typename UP::BiasedExponent; + using Sig = typename UP::Significand; + using UP::encode; + using UP::UP; + +public: + // Builders + LIBC_INLINE static constexpr RetT one(Sign sign = Sign::POS) { + return RetT(encode(sign, Exponent::ZERO(), Sig::MSB())); + } + LIBC_INLINE static constexpr RetT min_subnormal(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ZEROES(), Sig::LSB())); + } + LIBC_INLINE static constexpr RetT max_subnormal(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ZEROES(), + Sig::BITS_ALL_ONES() ^ Sig::MSB())); + } + LIBC_INLINE static constexpr RetT min_normal(Sign sign = Sign::POS) { + return RetT(encode(sign, Exponent::MIN(), Sig::MSB())); + } + LIBC_INLINE static constexpr RetT max_normal(Sign sign = Sign::POS) { + return RetT(encode(sign, Exponent::MAX(), Sig::BITS_ALL_ONES())); + } + LIBC_INLINE static constexpr RetT inf(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), Sig::MSB())); + } + LIBC_INLINE static constexpr RetT build_nan(Sign sign = Sign::POS, + StorageType v = 0) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), + Sig::MSB() | (v ? Sig(v) : (Sig::MSB() >> 2)))); + } + LIBC_INLINE static constexpr RetT build_quiet_nan(Sign sign = Sign::POS, + StorageType v = 0) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ONES(), + Sig::MSB() | (Sig::MSB() >> 1) | Sig(v))); + } + + // Observers LIBC_INLINE constexpr bool is_nan() const { // Most encoding forms from the table found in // https://en.wikipedia.org/wiki/Extended_precision#x86_extended_precision_format @@ -527,85 +472,183 @@ struct FPRep : public FPRepBase { // - Quiet Not a Number // - Unnormal // This can be reduced to the following logic: - if (exp_bits() == encode(BiasedExponent::BITS_ALL_ONES())) + if (exp_bits() == encode(BiasedExp::BITS_ALL_ONES())) return !is_inf(); - if (exp_bits() != encode(BiasedExponent::BITS_ALL_ZEROES())) - return (sig_bits() & encode(Significand::MSB())) == 0; + if (exp_bits() != encode(BiasedExp::BITS_ALL_ZEROES())) + return (sig_bits() & encode(Sig::MSB())) == 0; return false; } LIBC_INLINE constexpr bool is_quiet_nan() const { return exp_sig_bits() >= - encode(BiasedExponent::BITS_ALL_ONES(), - Significand::MSB() | (Significand::MSB() >> 1)); + encode(BiasedExp::BITS_ALL_ONES(), Sig::MSB() | (Sig::MSB() >> 1)); } LIBC_INLINE constexpr bool is_signaling_nan() const { return is_nan() && !is_quiet_nan(); } LIBC_INLINE constexpr bool is_inf() const { - return exp_sig_bits() == - encode(BiasedExponent::BITS_ALL_ONES(), Significand::MSB()); + return exp_sig_bits() == encode(BiasedExp::BITS_ALL_ONES(), Sig::MSB()); } LIBC_INLINE constexpr bool is_finite() const { return !is_inf() && !is_nan(); } + LIBC_INLINE + constexpr bool is_subnormal() const { + return exp_bits() == encode(BiasedExp::BITS_ALL_ZEROES()); + } LIBC_INLINE constexpr bool is_normal() const { const auto exp = exp_bits(); - if (exp == encode(BiasedExponent::BITS_ALL_ZEROES()) || - exp == encode(BiasedExponent::BITS_ALL_ONES())) + if (exp == encode(BiasedExp::BITS_ALL_ZEROES()) || + exp == encode(BiasedExp::BITS_ALL_ONES())) return false; return get_implicit_bit(); } + LIBC_INLINE constexpr StorageType get_explicit_mantissa() const { + return sig_bits(); + } + + // This functions is specific to FPRepSem. + // TODO: Remove if possible. + LIBC_INLINE constexpr bool get_implicit_bit() const { + return static_cast(bits & EXPLICIT_BIT_MASK); + } + + // This functions is specific to FPRepSem. + // TODO: Remove if possible. + LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) { + if (get_implicit_bit() != implicitVal) + bits ^= EXPLICIT_BIT_MASK; + } +}; + +// 'FPRep' is the bottom of the class hierarchy that only deals with 'FPType'. +// The operations dealing with specific float semantics are implemented by +// 'FPRepSem' above and specialized when needed. +// +// The 'RetT' type is being propagated up to 'FPRepSem' so that the functions +// creating new values (Builders) can return the appropriate type. That is, when +// creating a value through 'FPBits' below the builder will return an 'FPBits' +// value: +// i.e., FPBits::zero() // returns an FPBits +// When we don't care about specific C++ floating point type we can use 'FPRep' +// directly and 'RetT' defaults to 'StorageType': +// i.e., FPRep::zero() // returns an 'uint32_t' +template ::StorageType> +struct FPRep : public FPRepSem { + using UP = FPRepSem; + using StorageType = typename UP::StorageType; + +protected: + using UP::bits; + using UP::encode; + using UP::exp_bits; + using UP::exp_sig_bits; + + using BiasedExp = typename UP::BiasedExponent; + using Sig = typename UP::Significand; - LIBC_INLINE static constexpr StorageType zero(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::ZERO()); + using UP::FP_MASK; + using UP::SIG_LEN; + +public: + using UP::EXP_BIAS; + using UP::EXP_MASK; + using UP::FRACTION_MASK; + using UP::SIGN_MASK; + + // Representation + LIBC_INLINE constexpr StorageType uintval() const { return bits & FP_MASK; } + LIBC_INLINE constexpr void set_uintval(StorageType value) { + bits = (value & FP_MASK); } - LIBC_INLINE static constexpr StorageType one(Sign sign = Sign::POS) { - return encode(sign, Exponent::ZERO(), Significand::MSB()); + + // Builders + LIBC_INLINE static constexpr RetT zero(Sign sign = Sign::POS) { + return RetT(encode(sign, BiasedExp::BITS_ALL_ZEROES(), Sig::ZERO())); } - LIBC_INLINE static constexpr StorageType - min_subnormal(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), Significand::LSB()); + using UP::build_nan; + using UP::build_quiet_nan; + using UP::inf; + using UP::max_normal; + using UP::max_subnormal; + using UP::min_normal; + using UP::min_subnormal; + using UP::one; + + // Modifiers + LIBC_INLINE constexpr RetT abs() const { + return RetT(bits & UP::EXP_SIG_MASK); } - LIBC_INLINE static constexpr StorageType - max_subnormal(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ZEROES(), - Significand::BITS_ALL_ONES() ^ Significand::MSB()); + + // Observers + using UP::get_explicit_mantissa; + LIBC_INLINE constexpr bool is_zero() const { return exp_sig_bits() == 0; } + LIBC_INLINE constexpr bool is_inf_or_nan() const { return !is_finite(); } + using UP::is_finite; + using UP::is_inf; + using UP::is_nan; + using UP::is_normal; + using UP::is_quiet_nan; + using UP::is_signaling_nan; + using UP::is_subnormal; + LIBC_INLINE constexpr bool is_neg() const { return sign().is_neg(); } + LIBC_INLINE constexpr bool is_pos() const { return sign().is_pos(); } + + // Parts + LIBC_INLINE constexpr Sign sign() const { + return (bits & SIGN_MASK) ? Sign::NEG : Sign::POS; } - LIBC_INLINE static constexpr StorageType min_normal(Sign sign = Sign::POS) { - return encode(sign, Exponent::MIN(), Significand::MSB()); + + LIBC_INLINE constexpr void set_sign(Sign signVal) { + if (sign() != signVal) + bits ^= SIGN_MASK; } - LIBC_INLINE static constexpr StorageType max_normal(Sign sign = Sign::POS) { - return encode(sign, Exponent::MAX(), Significand::BITS_ALL_ONES()); + + LIBC_INLINE constexpr uint16_t get_biased_exponent() const { + return uint16_t((bits & UP::EXP_MASK) >> UP::SIG_LEN); } - LIBC_INLINE static constexpr StorageType inf(Sign sign = Sign::POS) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), Significand::MSB()); + + LIBC_INLINE constexpr void set_biased_exponent(StorageType biased) { + bits = merge(bits, biased << SIG_LEN, EXP_MASK); } - LIBC_INLINE static constexpr StorageType build_nan(Sign sign = Sign::POS, - StorageType v = 0) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), - Significand::MSB() | - (v ? Significand(v) : (Significand::MSB() >> 2))); + + LIBC_INLINE constexpr int get_exponent() const { + return int(get_biased_exponent()) - EXP_BIAS; } - LIBC_INLINE static constexpr StorageType - build_quiet_nan(Sign sign = Sign::POS, StorageType v = 0) { - return encode(sign, BiasedExponent::BITS_ALL_ONES(), - Significand::MSB() | (Significand::MSB() >> 1) | - Significand(v)); + + // If the number is subnormal, the exponent is treated as if it were the + // minimum exponent for a normal number. This is to keep continuity between + // the normal and subnormal ranges, but it causes problems for functions where + // values are calculated from the exponent, since just subtracting the bias + // will give a slightly incorrect result. Additionally, zero has an exponent + // of zero, and that should actually be treated as zero. + LIBC_INLINE constexpr int get_explicit_exponent() const { + const int biased_exp = int(get_biased_exponent()); + if (is_zero()) { + return 0; + } else if (biased_exp == 0) { + return 1 - EXP_BIAS; + } else { + return biased_exp - EXP_BIAS; + } } - LIBC_INLINE constexpr StorageType get_explicit_mantissa() const { - return sig_bits(); + LIBC_INLINE constexpr StorageType get_mantissa() const { + return bits & FRACTION_MASK; } - // The following functions are specific to FPRep. - // TODO: Remove if possible. - LIBC_INLINE constexpr bool get_implicit_bit() const { - return static_cast(bits & EXPLICIT_BIT_MASK); + LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) { + bits = merge(bits, mantVal, FRACTION_MASK); } - LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) { - if (get_implicit_bit() != implicitVal) - bits ^= EXPLICIT_BIT_MASK; +private: + // Merge bits from 'a' and 'b' values according to 'mask'. + // Use 'a' bits when corresponding 'mask' bits are zeroes and 'b' bits when + // corresponding bits are ones. + LIBC_INLINE static constexpr StorageType merge(StorageType a, StorageType b, + StorageType mask) { + // https://graphics.stanford.edu/~seander/bithacks.html#MaskedMerge + return a ^ ((a ^ b) & mask); } }; @@ -642,29 +685,31 @@ template LIBC_INLINE static constexpr FPType get_fp_type() { static_assert(cpp::always_false, "Unsupported type"); } -// A generic class to represent floating point formats. -// On most platforms, the 'float' type corresponds to single precision -// floating point numbers, the 'double' type corresponds to double precision -// floating point numers, and the 'long double' type corresponds to the quad -// precision floating numbers. On x86 platforms however, the 'long double' -// type maps to an x87 floating point format. -template struct FPBits : public internal::FPRep()> { +// A generic class to manipulate floating point formats. +// It derives most of its functionality to FPRep above. +template +struct FPBits final : public internal::FPRep(), FPBits> { static_assert(cpp::is_floating_point_v, "FPBits instantiated with invalid type."); - using UP = internal::FPRep()>; + using UP = internal::FPRep(), FPBits>; using Rep = UP; using StorageType = typename UP::StorageType; using UP::bits; - using UP::EXP_LEN; - using UP::UP; // Constants. - static constexpr int MAX_BIASED_EXPONENT = (1 << EXP_LEN) - 1; - static constexpr StorageType MIN_NORMAL = UP::min_normal(Sign::POS); - static constexpr StorageType MAX_NORMAL = UP::max_normal(Sign::POS); - static constexpr StorageType MIN_SUBNORMAL = UP::min_subnormal(Sign::POS); - static constexpr StorageType MAX_SUBNORMAL = UP::max_subnormal(Sign::POS); + LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_PRECISION = + UP::FRACTION_LEN + 1; + LIBC_INLINE_VAR static constexpr StorageType MIN_NORMAL = + UP::min_normal(Sign::POS).uintval(); + LIBC_INLINE_VAR static constexpr StorageType MAX_NORMAL = + UP::max_normal(Sign::POS).uintval(); + LIBC_INLINE_VAR static constexpr StorageType MIN_SUBNORMAL = + UP::min_subnormal(Sign::POS).uintval(); + LIBC_INLINE_VAR static constexpr StorageType MAX_SUBNORMAL = + UP::max_subnormal(Sign::POS).uintval(); + LIBC_INLINE_VAR static constexpr int MAX_BIASED_EXPONENT = + (1 << UP::EXP_LEN) - 1; // Constructors. LIBC_INLINE constexpr FPBits() = default; @@ -686,49 +731,35 @@ template struct FPBits : public internal::FPRep()> { LIBC_INLINE constexpr explicit operator T() const { return get_val(); } - LIBC_INLINE constexpr bool is_inf_or_nan() const { return !UP::is_finite(); } - - LIBC_INLINE constexpr FPBits abs() const { - return FPBits(bits & UP::EXP_SIG_MASK); - } - // Methods below this are used by tests. - + // TODO: inline and remove. LIBC_INLINE static constexpr T one(Sign sign = Sign::POS) { - return FPBits(UP::one(sign)).get_val(); + return T(UP::one(sign)); } - LIBC_INLINE static constexpr T zero(Sign sign = Sign::POS) { - return FPBits(UP::zero(sign)).get_val(); + return T(UP::zero(sign)); } - LIBC_INLINE static constexpr T inf(Sign sign = Sign::POS) { - return FPBits(UP::inf(sign)).get_val(); + return T(UP::inf(sign)); } - LIBC_INLINE static constexpr T min_normal() { - return FPBits(UP::min_normal(Sign::POS)).get_val(); + return T(UP::min_normal(Sign::POS)); } - LIBC_INLINE static constexpr T max_normal() { - return FPBits(UP::max_normal(Sign::POS)).get_val(); + return T(UP::max_normal(Sign::POS)); } - LIBC_INLINE static constexpr T min_denormal() { - return FPBits(UP::min_subnormal(Sign::POS)).get_val(); + return T(UP::min_subnormal(Sign::POS)); } - LIBC_INLINE static constexpr T max_denormal() { - return FPBits(UP::max_subnormal(Sign::POS)).get_val(); + return T(UP::max_subnormal(Sign::POS)); } - LIBC_INLINE static constexpr T build_nan(StorageType v) { - return FPBits(UP::build_nan(Sign::POS, v)).get_val(); + return T(UP::build_nan(Sign::POS, v)); } - LIBC_INLINE static constexpr T build_quiet_nan(StorageType v, Sign sign = Sign::POS) { - return FPBits(UP::build_quiet_nan(sign, v)).get_val(); + return T(UP::build_quiet_nan(sign, v)); } // TODO: Use an uint32_t for 'biased_exp'. @@ -757,7 +788,7 @@ template struct FPBits : public internal::FPRep()> { "This function is not tested for X86 Extended Precision"); FPBits result; // offset: +1 for sign, but -1 for implicit first bit - int lz = cpp::countl_zero(number) - EXP_LEN; + int lz = cpp::countl_zero(number) - UP::EXP_LEN; number <<= lz; ep -= lz; diff --git a/libc/test/src/__support/FPUtil/fpbits_test.cpp b/libc/test/src/__support/FPUtil/fpbits_test.cpp index f0b155085dabf..e6b6d7d9ec780 100644 --- a/libc/test/src/__support/FPUtil/fpbits_test.cpp +++ b/libc/test/src/__support/FPUtil/fpbits_test.cpp @@ -17,69 +17,72 @@ TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary16) { using LIBC_NAMESPACE::fputil::FPType; using LIBC_NAMESPACE::fputil::internal::FPRep; using Rep = FPRep; - using u16 = uint16_t; - - EXPECT_EQ(u16(0b0'00000'0000000000), Rep::zero()); - EXPECT_EQ(u16(0b0'01111'0000000000), Rep::one()); - EXPECT_EQ(u16(0b0'00000'0000000001), Rep::min_subnormal()); - EXPECT_EQ(u16(0b0'00000'1111111111), Rep::max_subnormal()); - EXPECT_EQ(u16(0b0'00001'0000000000), Rep::min_normal()); - EXPECT_EQ(u16(0b0'11110'1111111111), Rep::max_normal()); - EXPECT_EQ(u16(0b0'11111'0000000000), Rep::inf()); - EXPECT_EQ(u16(0b0'11111'0100000000), Rep::build_nan()); - EXPECT_EQ(u16(0b0'11111'1000000000), Rep::build_quiet_nan()); + using u16 = typename Rep::StorageType; + + EXPECT_EQ(u16(0b0'00000'0000000000), u16(Rep::zero())); + EXPECT_EQ(u16(0b0'01111'0000000000), u16(Rep::one())); + EXPECT_EQ(u16(0b0'00000'0000000001), u16(Rep::min_subnormal())); + EXPECT_EQ(u16(0b0'00000'1111111111), u16(Rep::max_subnormal())); + EXPECT_EQ(u16(0b0'00001'0000000000), u16(Rep::min_normal())); + EXPECT_EQ(u16(0b0'11110'1111111111), u16(Rep::max_normal())); + EXPECT_EQ(u16(0b0'11111'0000000000), u16(Rep::inf())); + EXPECT_EQ(u16(0b0'11111'0100000000), u16(Rep::build_nan())); + EXPECT_EQ(u16(0b0'11111'1000000000), u16(Rep::build_quiet_nan())); } TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary32) { using LIBC_NAMESPACE::fputil::FPType; using LIBC_NAMESPACE::fputil::internal::FPRep; using Rep = FPRep; - using u32 = uint32_t; - - EXPECT_EQ(u32(0b0'00000000'00000000000000000000000), Rep::zero()); - EXPECT_EQ(u32(0b0'01111111'00000000000000000000000), Rep::one()); - EXPECT_EQ(u32(0b0'00000000'00000000000000000000001), Rep::min_subnormal()); - EXPECT_EQ(u32(0b0'00000000'11111111111111111111111), Rep::max_subnormal()); - EXPECT_EQ(u32(0b0'00000001'00000000000000000000000), Rep::min_normal()); - EXPECT_EQ(u32(0b0'11111110'11111111111111111111111), Rep::max_normal()); - EXPECT_EQ(u32(0b0'11111111'00000000000000000000000), Rep::inf()); - EXPECT_EQ(u32(0b0'11111111'01000000000000000000000), Rep::build_nan()); - EXPECT_EQ(u32(0b0'11111111'10000000000000000000000), Rep::build_quiet_nan()); + using u32 = typename Rep::StorageType; + + EXPECT_EQ(u32(0b0'00000000'00000000000000000000000), u32(Rep::zero())); + EXPECT_EQ(u32(0b0'01111111'00000000000000000000000), u32(Rep::one())); + EXPECT_EQ(u32(0b0'00000000'00000000000000000000001), + u32(Rep::min_subnormal())); + EXPECT_EQ(u32(0b0'00000000'11111111111111111111111), + u32(Rep::max_subnormal())); + EXPECT_EQ(u32(0b0'00000001'00000000000000000000000), u32(Rep::min_normal())); + EXPECT_EQ(u32(0b0'11111110'11111111111111111111111), u32(Rep::max_normal())); + EXPECT_EQ(u32(0b0'11111111'00000000000000000000000), u32(Rep::inf())); + EXPECT_EQ(u32(0b0'11111111'01000000000000000000000), u32(Rep::build_nan())); + EXPECT_EQ(u32(0b0'11111111'10000000000000000000000), + u32(Rep::build_quiet_nan())); } TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary64) { using LIBC_NAMESPACE::fputil::FPType; using LIBC_NAMESPACE::fputil::internal::FPRep; using Rep = FPRep; - using u64 = uint64_t; + using u64 = typename Rep::StorageType; EXPECT_EQ( u64(0b0'00000000000'0000000000000000000000000000000000000000000000000000), - Rep::zero()); + u64(Rep::zero())); EXPECT_EQ( u64(0b0'01111111111'0000000000000000000000000000000000000000000000000000), - Rep::one()); + u64(Rep::one())); EXPECT_EQ( u64(0b0'00000000000'0000000000000000000000000000000000000000000000000001), - Rep::min_subnormal()); + u64(Rep::min_subnormal())); EXPECT_EQ( u64(0b0'00000000000'1111111111111111111111111111111111111111111111111111), - Rep::max_subnormal()); + u64(Rep::max_subnormal())); EXPECT_EQ( u64(0b0'00000000001'0000000000000000000000000000000000000000000000000000), - Rep::min_normal()); + u64(Rep::min_normal())); EXPECT_EQ( u64(0b0'11111111110'1111111111111111111111111111111111111111111111111111), - Rep::max_normal()); + u64(Rep::max_normal())); EXPECT_EQ( u64(0b0'11111111111'0000000000000000000000000000000000000000000000000000), - Rep::inf()); + u64(Rep::inf())); EXPECT_EQ( u64(0b0'11111111111'0100000000000000000000000000000000000000000000000000), - Rep::build_nan()); + u64(Rep::build_nan())); EXPECT_EQ( u64(0b0'11111111111'1000000000000000000000000000000000000000000000000000), - Rep::build_quiet_nan()); + u64(Rep::build_quiet_nan())); } static constexpr UInt128 u128(uint64_t hi, uint64_t lo) { @@ -90,6 +93,49 @@ static constexpr UInt128 u128(uint64_t hi, uint64_t lo) { #endif } +TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary128) { + using LIBC_NAMESPACE::fputil::FPType; + using LIBC_NAMESPACE::fputil::internal::FPRep; + using Rep = FPRep; + + EXPECT_EQ( + u128(0b0'000000000000000'000000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::zero())); + EXPECT_EQ( + u128(0b0'011111111111111'000000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::one())); + EXPECT_EQ( + u128(0b0'000000000000000'000000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000001), + UInt128(Rep::min_subnormal())); + EXPECT_EQ( + u128(0b0'000000000000000'111111111111111111111111111111111111111111111111, + 0b1111111111111111111111111111111111111111111111111111111111111111), + UInt128(Rep::max_subnormal())); + EXPECT_EQ( + u128(0b0'000000000000001'000000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::min_normal())); + EXPECT_EQ( + u128(0b0'111111111111110'111111111111111111111111111111111111111111111111, + 0b1111111111111111111111111111111111111111111111111111111111111111), + UInt128(Rep::max_normal())); + EXPECT_EQ( + u128(0b0'111111111111111'000000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::inf())); + EXPECT_EQ( + u128(0b0'111111111111111'010000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::build_nan())); + EXPECT_EQ( + u128(0b0'111111111111111'100000000000000000000000000000000000000000000000, + 0b0000000000000000000000000000000000000000000000000000000000000000), + UInt128(Rep::build_quiet_nan())); +} + TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80) { using LIBC_NAMESPACE::fputil::FPType; using LIBC_NAMESPACE::fputil::internal::FPRep; @@ -98,39 +144,39 @@ TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80) { EXPECT_EQ( u128(0b0'000000000000000, 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::zero()); + UInt128(Rep::zero())); EXPECT_EQ( u128(0b0'011111111111111, 0b1000000000000000000000000000000000000000000000000000000000000000), - Rep::one()); + UInt128(Rep::one())); EXPECT_EQ( u128(0b0'000000000000000, 0b0000000000000000000000000000000000000000000000000000000000000001), - Rep::min_subnormal()); + UInt128(Rep::min_subnormal())); EXPECT_EQ( u128(0b0'000000000000000, 0b0111111111111111111111111111111111111111111111111111111111111111), - Rep::max_subnormal()); + UInt128(Rep::max_subnormal())); EXPECT_EQ( u128(0b0'000000000000001, 0b1000000000000000000000000000000000000000000000000000000000000000), - Rep::min_normal()); + UInt128(Rep::min_normal())); EXPECT_EQ( u128(0b0'111111111111110, 0b1111111111111111111111111111111111111111111111111111111111111111), - Rep::max_normal()); + UInt128(Rep::max_normal())); EXPECT_EQ( u128(0b0'111111111111111, 0b1000000000000000000000000000000000000000000000000000000000000000), - Rep::inf()); + UInt128(Rep::inf())); EXPECT_EQ( u128(0b0'111111111111111, 0b1010000000000000000000000000000000000000000000000000000000000000), - Rep::build_nan()); + UInt128(Rep::build_nan())); EXPECT_EQ( u128(0b0'111111111111111, 0b1100000000000000000000000000000000000000000000000000000000000000), - Rep::build_quiet_nan()); + UInt128(Rep::build_quiet_nan())); } TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80_IsNan) { @@ -183,49 +229,6 @@ TEST(LlvmLibcFPBitsTest, FPType_X86_Binary80_IsNan) { 0b1000000000000000000000000000000000000000000000000000000000000000)); } -TEST(LlvmLibcFPBitsTest, FPType_IEEE754_Binary128) { - using LIBC_NAMESPACE::fputil::FPType; - using LIBC_NAMESPACE::fputil::internal::FPRep; - using Rep = FPRep; - - EXPECT_EQ( - u128(0b0'000000000000000'000000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::zero()); - EXPECT_EQ( - u128(0b0'011111111111111'000000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::one()); - EXPECT_EQ( - u128(0b0'000000000000000'000000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000001), - Rep::min_subnormal()); - EXPECT_EQ( - u128(0b0'000000000000000'111111111111111111111111111111111111111111111111, - 0b1111111111111111111111111111111111111111111111111111111111111111), - Rep::max_subnormal()); - EXPECT_EQ( - u128(0b0'000000000000001'000000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::min_normal()); - EXPECT_EQ( - u128(0b0'111111111111110'111111111111111111111111111111111111111111111111, - 0b1111111111111111111111111111111111111111111111111111111111111111), - Rep::max_normal()); - EXPECT_EQ( - u128(0b0'111111111111111'000000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::inf()); - EXPECT_EQ( - u128(0b0'111111111111111'010000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::build_nan()); - EXPECT_EQ( - u128(0b0'111111111111111'100000000000000000000000000000000000000000000000, - 0b0000000000000000000000000000000000000000000000000000000000000000), - Rep::build_quiet_nan()); -} - TEST(LlvmLibcFPBitsTest, FloatType) { using FloatBits = FPBits;