@@ -65,6 +65,8 @@ struct FloatTraits;
65
65
66
66
template <>
67
67
struct FloatTraits <double > {
68
+ using mantissa_t = uint64_t ;
69
+
68
70
// The number of mantissa bits in the given float type. This includes the
69
71
// implied high bit.
70
72
static constexpr int kTargetMantissaBits = 53 ;
@@ -103,7 +105,7 @@ struct FloatTraits<double> {
103
105
// a normal value is made, or it must be less narrow than that, in which case
104
106
// `exponent` must be exactly kMinNormalExponent, and a subnormal value is
105
107
// made.
106
- static double Make (uint64_t mantissa, int exponent, bool sign) {
108
+ static double Make (mantissa_t mantissa, int exponent, bool sign) {
107
109
#ifndef ABSL_BIT_PACK_FLOATS
108
110
// Support ldexp no matter which namespace it's in. Some platforms
109
111
// incorrectly don't put it in namespace std.
@@ -116,8 +118,10 @@ struct FloatTraits<double> {
116
118
if (mantissa > kMantissaMask ) {
117
119
// Normal value.
118
120
// Adjust by 1023 for the exponent representation bias, and an additional
119
- // 52 due to the implied decimal point in the IEEE mantissa represenation.
120
- dbl += uint64_t {exponent + 1023u + kTargetMantissaBits - 1 } << 52 ;
121
+ // 52 due to the implied decimal point in the IEEE mantissa
122
+ // representation.
123
+ dbl += static_cast <uint64_t >(exponent + 1023 + kTargetMantissaBits - 1 )
124
+ << 52 ;
121
125
mantissa &= kMantissaMask ;
122
126
} else {
123
127
// subnormal value
@@ -134,16 +138,20 @@ struct FloatTraits<double> {
134
138
// members and methods.
135
139
template <>
136
140
struct FloatTraits <float > {
141
+ using mantissa_t = uint32_t ;
142
+
137
143
static constexpr int kTargetMantissaBits = 24 ;
138
144
static constexpr int kMaxExponent = 104 ;
139
145
static constexpr int kMinNormalExponent = -149 ;
146
+
140
147
static float MakeNan (const char * tagp) {
141
148
// Support nanf no matter which namespace it's in. Some platforms
142
149
// incorrectly don't put it in namespace std.
143
150
using namespace std ; // NOLINT
144
151
return nanf (tagp);
145
152
}
146
- static float Make (uint32_t mantissa, int exponent, bool sign) {
153
+
154
+ static float Make (mantissa_t mantissa, int exponent, bool sign) {
147
155
#ifndef ABSL_BIT_PACK_FLOATS
148
156
// Support ldexpf no matter which namespace it's in. Some platforms
149
157
// incorrectly don't put it in namespace std.
@@ -157,7 +165,8 @@ struct FloatTraits<float> {
157
165
// Normal value.
158
166
// Adjust by 127 for the exponent representation bias, and an additional
159
167
// 23 due to the implied decimal point in the IEEE mantissa represenation.
160
- flt += uint32_t {exponent + 127u + kTargetMantissaBits - 1 } << 23 ;
168
+ flt += static_cast <uint32_t >(exponent + 127 + kTargetMantissaBits - 1 )
169
+ << 23 ;
161
170
mantissa &= kMantissaMask ;
162
171
} else {
163
172
// subnormal value
@@ -242,9 +251,9 @@ struct CalculatedFloat {
242
251
243
252
// Returns the bit width of the given uint128. (Equivalently, returns 128
244
253
// minus the number of leading zero bits.)
245
- unsigned BitWidth (uint128 value) {
254
+ int BitWidth (uint128 value) {
246
255
if (Uint128High64 (value) == 0 ) {
247
- return static_cast < unsigned >( bit_width (Uint128Low64 (value) ));
256
+ return bit_width (Uint128Low64 (value));
248
257
}
249
258
return 128 - countl_zero (Uint128High64 (value));
250
259
}
@@ -337,8 +346,10 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
337
346
*value = negative ? -0.0 : 0.0 ;
338
347
return ;
339
348
}
340
- *value = FloatTraits<FloatType>::Make (calculated.mantissa ,
341
- calculated.exponent , negative);
349
+ *value = FloatTraits<FloatType>::Make (
350
+ static_cast <typename FloatTraits<FloatType>::mantissa_t >(
351
+ calculated.mantissa ),
352
+ calculated.exponent , negative);
342
353
}
343
354
344
355
// Returns the given uint128 shifted to the right by `shift` bits, and rounds
@@ -519,7 +530,7 @@ CalculatedFloat CalculateFromParsedHexadecimal(
519
530
const strings_internal::ParsedFloat& parsed_hex) {
520
531
uint64_t mantissa = parsed_hex.mantissa ;
521
532
int exponent = parsed_hex.exponent ;
522
- auto mantissa_width = static_cast < unsigned >( bit_width (mantissa) );
533
+ int mantissa_width = bit_width (mantissa);
523
534
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
524
535
bool result_exact;
525
536
exponent += shift;
0 commit comments