Skip to content

Commit 198238a

Browse files
authored
[libc][NFC] Remove remaining specializations in FloatProperties (#75165)
This patch sinks `EXPLICIT_BIT_MASK` into `get_explicit_mantissa` - the only function using it. Then it sinks the content of `FPCommonProperties` directly into `FPProperties`.
1 parent b00e445 commit 198238a

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

libc/src/__support/FPUtil/FloatProperties.h

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,12 @@ template <typename T, size_t count> static constexpr T mask_trailing_ones() {
9090
return count == 0 ? 0 : ((~T(0)) >> (t_bits - count));
9191
}
9292

93-
// Derives more properties from 'FPBaseProperties' above.
94-
// This class serves as a halfway point between 'FPBaseProperties' and
95-
// 'FPProperties' below.
93+
} // namespace internal
94+
9695
template <FPType fp_type>
97-
struct FPCommonProperties : private FPBaseProperties<fp_type> {
96+
struct FPProperties : public internal::FPBaseProperties<fp_type> {
9897
private:
99-
using UP = FPBaseProperties<fp_type>;
98+
using UP = internal::FPBaseProperties<fp_type>;
10099
using UP::EXP_BITS;
101100
using UP::SIG_BITS;
102101
using UP::TOTAL_BITS;
@@ -123,15 +122,15 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
123122

124123
// Masks
125124
LIBC_INLINE_VAR static constexpr UIntType SIG_MASK =
126-
mask_trailing_ones<UIntType, SIG_BITS>() << SIG_MASK_SHIFT;
125+
internal::mask_trailing_ones<UIntType, SIG_BITS>() << SIG_MASK_SHIFT;
127126
LIBC_INLINE_VAR static constexpr UIntType EXP_MASK =
128-
mask_trailing_ones<UIntType, EXP_BITS>() << EXP_MASK_SHIFT;
127+
internal::mask_trailing_ones<UIntType, EXP_BITS>() << EXP_MASK_SHIFT;
129128
// Trailing underscore on SIGN_MASK_ is temporary - it will be removed
130129
// once we can replace the public part below with the private one.
131130
LIBC_INLINE_VAR static constexpr UIntType SIGN_MASK_ =
132-
mask_trailing_ones<UIntType, SIGN_BITS>() << SIGN_MASK_SHIFT;
131+
internal::mask_trailing_ones<UIntType, SIGN_BITS>() << SIGN_MASK_SHIFT;
133132
LIBC_INLINE_VAR static constexpr UIntType FP_MASK =
134-
mask_trailing_ones<UIntType, TOTAL_BITS>();
133+
internal::mask_trailing_ones<UIntType, TOTAL_BITS>();
135134
static_assert((SIG_MASK & EXP_MASK & SIGN_MASK_) == 0, "masks disjoint");
136135
static_assert((SIG_MASK | EXP_MASK | SIGN_MASK_) == FP_MASK, "masks cover");
137136

@@ -140,19 +139,19 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
140139
}
141140

142141
LIBC_INLINE_VAR static constexpr UIntType QNAN_MASK =
143-
UP::ENCODING == FPEncoding::X86_ExtendedPrecision
142+
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
144143
? bit_at(SIG_BITS - 1) | bit_at(SIG_BITS - 2) // 0b1100...
145144
: bit_at(SIG_BITS - 1); // 0b1000...
146145

147146
LIBC_INLINE_VAR static constexpr UIntType SNAN_MASK =
148-
UP::ENCODING == FPEncoding::X86_ExtendedPrecision
147+
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
149148
? bit_at(SIG_BITS - 1) | bit_at(SIG_BITS - 3) // 0b1010...
150149
: bit_at(SIG_BITS - 2); // 0b0100...
151150

152151
// The number of bits after the decimal dot when the number if in normal form.
153152
LIBC_INLINE_VAR static constexpr int FRACTION_BITS =
154-
UP::ENCODING == FPEncoding::X86_ExtendedPrecision ? SIG_BITS - 1
155-
: SIG_BITS;
153+
UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision ? SIG_BITS - 1
154+
: SIG_BITS;
156155

157156
public:
158157
// Public facing API to keep the change local to this file.
@@ -163,7 +162,7 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
163162
LIBC_INLINE_VAR static constexpr uint32_t MANTISSA_PRECISION =
164163
MANTISSA_WIDTH + 1;
165164
LIBC_INLINE_VAR static constexpr BitsType MANTISSA_MASK =
166-
mask_trailing_ones<UIntType, MANTISSA_WIDTH>();
165+
internal::mask_trailing_ones<UIntType, MANTISSA_WIDTH>();
167166
LIBC_INLINE_VAR static constexpr uint32_t EXPONENT_WIDTH = EXP_BITS;
168167
LIBC_INLINE_VAR static constexpr int32_t EXPONENT_BIAS = EXP_BIAS;
169168
LIBC_INLINE_VAR static constexpr BitsType SIGN_MASK = SIGN_MASK_;
@@ -176,29 +175,6 @@ struct FPCommonProperties : private FPBaseProperties<fp_type> {
176175
static constexpr BitsType QUIET_NAN_MASK = QNAN_MASK;
177176
};
178177

179-
} // namespace internal
180-
181-
template <FPType fp_type>
182-
struct FPProperties : public internal::FPCommonProperties<fp_type> {};
183-
184-
// ----------------
185-
// Work In Progress
186-
// ----------------
187-
// The 'FPProperties' template specializations below are being slowly replaced
188-
// with properties from 'FPCommonProperties' above. Once specializations are
189-
// empty, 'FPProperties' declaration can be fully replace with
190-
// 'FPCommonProperties' implementation.
191-
192-
// Properties for numbers represented in 80 bits long double on non-Windows x86
193-
// platforms.
194-
template <>
195-
struct FPProperties<FPType::X86_Binary80>
196-
: public internal::FPCommonProperties<FPType::X86_Binary80> {
197-
// The x86 80 bit float represents the leading digit of the mantissa
198-
// explicitly. This is the mask for that bit.
199-
static constexpr BitsType EXPLICIT_BIT_MASK = (BitsType(1) << MANTISSA_WIDTH);
200-
};
201-
202178
//-----------------------------------------------------------------------------
203179
template <typename FP> LIBC_INLINE static constexpr FPType get_fp_type() {
204180
if constexpr (cpp::is_same_v<FP, float> && __FLT_MANT_DIG__ == 24)

libc/src/__support/FPUtil/x86_64/LongDoubleBits.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ template <> struct FPBits<long double> {
6868
}
6969

7070
LIBC_INLINE constexpr UIntType get_explicit_mantissa() const {
71-
return bits & (FloatProp::MANTISSA_MASK | FloatProp::EXPLICIT_BIT_MASK);
71+
// The x86 80 bit float represents the leading digit of the mantissa
72+
// explicitly. This is the mask for that bit.
73+
constexpr UIntType EXPLICIT_BIT_MASK =
74+
(UIntType(1) << FloatProp::MANTISSA_WIDTH);
75+
return bits & (FloatProp::MANTISSA_MASK | EXPLICIT_BIT_MASK);
7276
}
7377

7478
LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {

0 commit comments

Comments
 (0)