[libc-commits] [libc] 36b6f77 - [libc][NFC] Unify `FPBits` implementations (#76033)
via libc-commits
libc-commits at lists.llvm.org
Thu Dec 21 04:16:55 PST 2023
Author: Guillaume Chatelet
Date: 2023-12-21T13:16:51+01:00
New Revision: 36b6f77565c9d3d75c03600df4b8719bb518bdc0
URL: https://github.com/llvm/llvm-project/commit/36b6f77565c9d3d75c03600df4b8719bb518bdc0
DIFF: https://github.com/llvm/llvm-project/commit/36b6f77565c9d3d75c03600df4b8719bb518bdc0.diff
LOG: [libc][NFC] Unify `FPBits` implementations (#76033)
`FPBits` is currently implemented as a general case and a specialization
for `long double` when `long double` is x86 80-bit extended precision.
This patch is a first of a series to provide an implementation based on
`FPType` instead of the C++ float type (which implementation is
architecture dependent).
Added:
Modified:
libc/src/__support/FPUtil/FPBits.h
libc/src/__support/FPUtil/FloatProperties.h
libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
Removed:
################################################################################
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index b13ce80f94f6e6..790449cc0080c8 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -21,41 +21,37 @@
namespace LIBC_NAMESPACE {
namespace fputil {
-// A generic class to represent single precision, double precision, and quad
-// precision IEEE 754 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. This format is an IEEE 754 extension format.
-// It is handled as an explicit specialization of this class.
-template <typename T> struct FPBits : private FloatProperties<T> {
- static_assert(cpp::is_floating_point_v<T>,
- "FPBits instantiated with invalid type.");
- using typename FloatProperties<T>::StorageType;
- using FloatProperties<T>::TOTAL_LEN;
+namespace internal {
-private:
- using FloatProperties<T>::EXP_SIG_MASK;
-
-public:
- using FloatProperties<T>::EXP_MASK;
- using FloatProperties<T>::EXP_BIAS;
- using FloatProperties<T>::EXP_LEN;
- using FloatProperties<T>::FRACTION_MASK;
- using FloatProperties<T>::FRACTION_LEN;
+// This is a temporary class to unify common methods and properties between
+// FPBits and FPBits<long double>.
+template <FPType fp_type> struct FPBitsCommon : private FPProperties<fp_type> {
+ using UP = FPProperties<fp_type>;
+ using typename UP::StorageType;
+ using UP::TOTAL_LEN;
-private:
- using FloatProperties<T>::QUIET_NAN_MASK;
+protected:
+ using UP::EXP_SIG_MASK;
+ using UP::QUIET_NAN_MASK;
public:
- using FloatProperties<T>::SIGN_MASK;
+ using UP::EXP_BIAS;
+ using UP::EXP_LEN;
+ using UP::EXP_MASK;
+ using UP::EXP_MASK_SHIFT;
+ using UP::FP_MASK;
+ using UP::FRACTION_LEN;
+ using UP::FRACTION_MASK;
+ using UP::SIGN_MASK;
// Reinterpreting bits as an integer value and interpreting the bits of an
// integer value as a floating point value is used in tests. So, a convenient
// type is provided for such reinterpretations.
StorageType bits;
+ LIBC_INLINE constexpr FPBitsCommon() : bits(0) {}
+ LIBC_INLINE explicit constexpr FPBitsCommon(StorageType bits) : bits(bits) {}
+
LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) {
mantVal &= FRACTION_MASK;
bits &= ~FRACTION_MASK;
@@ -66,16 +62,89 @@ template <typename T> struct FPBits : private FloatProperties<T> {
return bits & FRACTION_MASK;
}
- LIBC_INLINE constexpr void set_biased_exponent(StorageType expVal) {
- expVal = (expVal << FRACTION_LEN) & EXP_MASK;
+ LIBC_INLINE constexpr void set_sign(bool signVal) {
+ if (get_sign() != signVal)
+ bits ^= SIGN_MASK;
+ }
+
+ LIBC_INLINE constexpr bool get_sign() const {
+ return (bits & SIGN_MASK) != 0;
+ }
+
+ LIBC_INLINE constexpr void set_biased_exponent(StorageType biased) {
+ // clear exponent bits
bits &= ~EXP_MASK;
- bits |= expVal;
+ // set exponent bits
+ bits |= (biased << EXP_MASK_SHIFT) & EXP_MASK;
}
LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
- return uint16_t((bits & EXP_MASK) >> FRACTION_LEN);
+ return uint16_t((bits & EXP_MASK) >> EXP_MASK_SHIFT);
+ }
+
+ 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 bool is_zero() const {
+ return (bits & EXP_SIG_MASK) == 0;
+ }
+};
+
+} // namespace internal
+
+// A generic class to represent single precision, double precision, and quad
+// precision IEEE 754 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. This format is an IEEE 754 extension format.
+// It is handled as an explicit specialization of this class.
+template <typename T>
+struct FPBits : public internal::FPBitsCommon<get_fp_type<T>()> {
+ static_assert(cpp::is_floating_point_v<T>,
+ "FPBits instantiated with invalid type.");
+ using UP = internal::FPBitsCommon<get_fp_type<T>()>;
+ using StorageType = typename UP::StorageType;
+ using UP::bits;
+
+private:
+ using UP::EXP_SIG_MASK;
+ using UP::QUIET_NAN_MASK;
+
+public:
+ using UP::EXP_BIAS;
+ using UP::EXP_LEN;
+ using UP::EXP_MASK;
+ using UP::EXP_MASK_SHIFT;
+ using UP::FRACTION_LEN;
+ using UP::FRACTION_MASK;
+ using UP::SIGN_MASK;
+ using UP::TOTAL_LEN;
+
+ using UP::get_biased_exponent;
+ using UP::is_zero;
+
// The function return mantissa with the implicit bit set iff the current
// value is a valid normal number.
LIBC_INLINE constexpr StorageType get_explicit_mantissa() {
@@ -85,19 +154,6 @@ template <typename T> struct FPBits : private FloatProperties<T> {
(FRACTION_MASK & bits);
}
- LIBC_INLINE constexpr void set_sign(bool signVal) {
- bits |= SIGN_MASK;
- if (!signVal)
- bits -= SIGN_MASK;
- }
-
- LIBC_INLINE constexpr bool get_sign() const {
- return (bits & SIGN_MASK) != 0;
- }
-
- static_assert(sizeof(T) == sizeof(StorageType),
- "Data type and integral representation have
diff erent sizes.");
-
static constexpr int MAX_BIASED_EXPONENT = (1 << EXP_LEN) - 1;
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
static constexpr StorageType MAX_SUBNORMAL = FRACTION_MASK;
@@ -109,49 +165,21 @@ template <typename T> struct FPBits : private FloatProperties<T> {
// type match.
template <typename XType, cpp::enable_if_t<cpp::is_same_v<T, XType>, int> = 0>
LIBC_INLINE constexpr explicit FPBits(XType x)
- : bits(cpp::bit_cast<StorageType>(x)) {}
+ : UP(cpp::bit_cast<StorageType>(x)) {}
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<XType, StorageType>, int> = 0>
- LIBC_INLINE constexpr explicit FPBits(XType x) : bits(x) {}
-
- LIBC_INLINE constexpr FPBits() : bits(0) {}
+ LIBC_INLINE constexpr explicit FPBits(XType x) : UP(x) {}
- LIBC_INLINE constexpr T get_val() const { return cpp::bit_cast<T>(bits); }
+ LIBC_INLINE constexpr FPBits() : UP() {}
LIBC_INLINE constexpr void set_val(T value) {
bits = cpp::bit_cast<StorageType>(value);
}
- LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
-
- LIBC_INLINE constexpr StorageType uintval() const { return bits; }
-
- 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 T get_val() const { return cpp::bit_cast<T>(bits); }
- LIBC_INLINE constexpr bool is_zero() const {
- // Remove sign bit by shift
- return (bits << 1) == 0;
- }
+ LIBC_INLINE constexpr explicit operator T() const { return get_val(); }
LIBC_INLINE constexpr bool is_inf() const {
return (bits & EXP_SIG_MASK) == EXP_MASK;
diff --git a/libc/src/__support/FPUtil/FloatProperties.h b/libc/src/__support/FPUtil/FloatProperties.h
index bcf1f7cfabd34b..6bf75b7167d32d 100644
--- a/libc/src/__support/FPUtil/FloatProperties.h
+++ b/libc/src/__support/FPUtil/FloatProperties.h
@@ -111,7 +111,7 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
(1U << (EXP_LEN - 1U)) - 1U;
static_assert(EXP_BIAS > 0);
-private:
+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;
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index b2b016adb661a7..1011e61f03fd6b 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -26,20 +26,27 @@
namespace LIBC_NAMESPACE {
namespace fputil {
-template <> struct FPBits<long double> : private FloatProperties<long double> {
- using typename FloatProperties<long double>::StorageType;
- using FloatProperties<long double>::TOTAL_LEN;
- using FloatProperties<long double>::EXP_MASK;
- using FloatProperties<long double>::EXP_BIAS;
- using FloatProperties<long double>::EXP_LEN;
- using FloatProperties<long double>::FRACTION_MASK;
- using FloatProperties<long double>::FRACTION_LEN;
+template <>
+struct FPBits<long double>
+ : public internal::FPBitsCommon<FPType::X86_Binary80> {
+ using UP = internal::FPBitsCommon<FPType::X86_Binary80>;
+ using StorageType = typename UP::StorageType;
+ using UP::bits;
private:
- using FloatProperties<long double>::QUIET_NAN_MASK;
+ using UP::EXP_SIG_MASK;
+ using UP::QUIET_NAN_MASK;
public:
- using FloatProperties<long double>::SIGN_MASK;
+ using UP::EXP_BIAS;
+ using UP::EXP_LEN;
+ using UP::EXP_MASK;
+ using UP::EXP_MASK_SHIFT;
+ using UP::FP_MASK;
+ using UP::FRACTION_LEN;
+ using UP::FRACTION_MASK;
+ using UP::SIGN_MASK;
+ using UP::TOTAL_LEN;
static constexpr int MAX_BIASED_EXPONENT = 0x7FFF;
static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
@@ -51,18 +58,6 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
(StorageType(MAX_BIASED_EXPONENT - 1) << (FRACTION_LEN + 1)) |
(StorageType(1) << FRACTION_LEN) | MAX_SUBNORMAL;
- StorageType bits;
-
- LIBC_INLINE constexpr void set_mantissa(StorageType mantVal) {
- mantVal &= FRACTION_MASK;
- bits &= ~FRACTION_MASK;
- bits |= mantVal;
- }
-
- LIBC_INLINE constexpr StorageType get_mantissa() const {
- return bits & FRACTION_MASK;
- }
-
LIBC_INLINE constexpr StorageType get_explicit_mantissa() const {
// The x86 80 bit float represents the leading digit of the mantissa
// explicitly. This is the mask for that bit.
@@ -70,16 +65,6 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
return bits & (FRACTION_MASK | EXPLICIT_BIT_MASK);
}
- LIBC_INLINE constexpr void set_biased_exponent(StorageType expVal) {
- expVal = (expVal << (TOTAL_LEN - 1 - EXP_LEN)) & EXP_MASK;
- bits &= ~EXP_MASK;
- bits |= expVal;
- }
-
- LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
- return uint16_t((bits & EXP_MASK) >> (TOTAL_LEN - 1 - EXP_LEN));
- }
-
LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) {
bits &= ~(StorageType(1) << FRACTION_LEN);
bits |= (StorageType(implicitVal) << FRACTION_LEN);
@@ -89,22 +74,12 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
return bool((bits & (StorageType(1) << FRACTION_LEN)) >> FRACTION_LEN);
}
- LIBC_INLINE constexpr void set_sign(bool signVal) {
- bits &= ~SIGN_MASK;
- StorageType sign1 = StorageType(signVal) << (TOTAL_LEN - 1);
- bits |= sign1;
- }
-
- LIBC_INLINE constexpr bool get_sign() const {
- return bool((bits & SIGN_MASK) >> (TOTAL_LEN - 1));
- }
-
- LIBC_INLINE constexpr FPBits() : bits(0) {}
+ LIBC_INLINE constexpr FPBits() : UP() {}
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<long double, XType>, int> = 0>
LIBC_INLINE constexpr explicit FPBits(XType x)
- : bits(cpp::bit_cast<StorageType>(x)) {
+ : UP(cpp::bit_cast<StorageType>(x)) {
// bits starts uninitialized, and setting it to a long double only
// overwrites the first 80 bits. This clears those upper bits.
bits = bits & ((StorageType(1) << 80) - 1);
@@ -112,47 +87,16 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<XType, StorageType>, int> = 0>
- LIBC_INLINE constexpr explicit FPBits(XType x) : bits(x) {}
+ LIBC_INLINE constexpr explicit FPBits(XType x) : UP(x) {}
LIBC_INLINE constexpr operator long double() {
return cpp::bit_cast<long double>(bits);
}
- LIBC_INLINE constexpr StorageType uintval() {
- // We zero the padding bits as they can contain garbage.
- return bits & FP_MASK;
- }
-
LIBC_INLINE constexpr long double get_val() const {
return cpp::bit_cast<long double>(bits);
}
- 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 bool is_zero() const {
- return get_biased_exponent() == 0 && get_mantissa() == 0 &&
- get_implicit_bit() == 0;
- }
-
LIBC_INLINE constexpr bool is_inf() const {
return get_biased_exponent() == MAX_BIASED_EXPONENT &&
get_mantissa() == 0 && get_implicit_bit() == 1;
More information about the libc-commits
mailing list