summaryrefslogtreecommitdiff
path: root/absl/strings/charconv.cc
diff options
context:
space:
mode:
authorGravatar Thomas Köppe <tkoeppe@google.com>2022-08-04 06:14:14 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-08-04 06:15:05 -0700
commit4b551344e6ba3243636d841d5a2d944a20a3a956 (patch)
tree95f8c347117f509f5aa9fab0b05f3c55d1ce73cf /absl/strings/charconv.cc
parent751ade00ee347abef5dac7248db851e3f2012e14 (diff)
Revert change "Fix "unsafe narrowing" warnings in absl, 4/n.".
The change breaks existing code by changing the return type of absl::bit_width. PiperOrigin-RevId: 465295951 Change-Id: Id4ce7c2ac3699ce22aa2b4851a949f9e0104a3d7
Diffstat (limited to 'absl/strings/charconv.cc')
-rw-r--r--absl/strings/charconv.cc31
1 files changed, 10 insertions, 21 deletions
diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc
index 65d1beb1..fefcfc90 100644
--- a/absl/strings/charconv.cc
+++ b/absl/strings/charconv.cc
@@ -65,8 +65,6 @@ struct FloatTraits;
template <>
struct FloatTraits<double> {
- using mantissa_t = uint64_t;
-
// The number of mantissa bits in the given float type. This includes the
// implied high bit.
static constexpr int kTargetMantissaBits = 53;
@@ -105,7 +103,7 @@ struct FloatTraits<double> {
// a normal value is made, or it must be less narrow than that, in which case
// `exponent` must be exactly kMinNormalExponent, and a subnormal value is
// made.
- static double Make(mantissa_t mantissa, int exponent, bool sign) {
+ static double Make(uint64_t mantissa, int exponent, bool sign) {
#ifndef ABSL_BIT_PACK_FLOATS
// Support ldexp no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std.
@@ -118,10 +116,8 @@ struct FloatTraits<double> {
if (mantissa > kMantissaMask) {
// Normal value.
// Adjust by 1023 for the exponent representation bias, and an additional
- // 52 due to the implied decimal point in the IEEE mantissa
- // representation.
- dbl += static_cast<uint64_t>(exponent + 1023 + kTargetMantissaBits - 1)
- << 52;
+ // 52 due to the implied decimal point in the IEEE mantissa represenation.
+ dbl += uint64_t{exponent + 1023u + kTargetMantissaBits - 1} << 52;
mantissa &= kMantissaMask;
} else {
// subnormal value
@@ -138,20 +134,16 @@ struct FloatTraits<double> {
// members and methods.
template <>
struct FloatTraits<float> {
- using mantissa_t = uint32_t;
-
static constexpr int kTargetMantissaBits = 24;
static constexpr int kMaxExponent = 104;
static constexpr int kMinNormalExponent = -149;
-
static float MakeNan(const char* tagp) {
// Support nanf no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std.
using namespace std; // NOLINT
return nanf(tagp);
}
-
- static float Make(mantissa_t mantissa, int exponent, bool sign) {
+ static float Make(uint32_t mantissa, int exponent, bool sign) {
#ifndef ABSL_BIT_PACK_FLOATS
// Support ldexpf no matter which namespace it's in. Some platforms
// incorrectly don't put it in namespace std.
@@ -165,8 +157,7 @@ struct FloatTraits<float> {
// Normal value.
// Adjust by 127 for the exponent representation bias, and an additional
// 23 due to the implied decimal point in the IEEE mantissa represenation.
- flt += static_cast<uint32_t>(exponent + 127 + kTargetMantissaBits - 1)
- << 23;
+ flt += uint32_t{exponent + 127u + kTargetMantissaBits - 1} << 23;
mantissa &= kMantissaMask;
} else {
// subnormal value
@@ -251,9 +242,9 @@ struct CalculatedFloat {
// Returns the bit width of the given uint128. (Equivalently, returns 128
// minus the number of leading zero bits.)
-int BitWidth(uint128 value) {
+unsigned BitWidth(uint128 value) {
if (Uint128High64(value) == 0) {
- return bit_width(Uint128Low64(value));
+ return static_cast<unsigned>(bit_width(Uint128Low64(value)));
}
return 128 - countl_zero(Uint128High64(value));
}
@@ -346,10 +337,8 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
*value = negative ? -0.0 : 0.0;
return;
}
- *value = FloatTraits<FloatType>::Make(
- static_cast<typename FloatTraits<FloatType>::mantissa_t>(
- calculated.mantissa),
- calculated.exponent, negative);
+ *value = FloatTraits<FloatType>::Make(calculated.mantissa,
+ calculated.exponent, negative);
}
// Returns the given uint128 shifted to the right by `shift` bits, and rounds
@@ -530,7 +519,7 @@ CalculatedFloat CalculateFromParsedHexadecimal(
const strings_internal::ParsedFloat& parsed_hex) {
uint64_t mantissa = parsed_hex.mantissa;
int exponent = parsed_hex.exponent;
- int mantissa_width = bit_width(mantissa);
+ auto mantissa_width = static_cast<unsigned>(bit_width(mantissa));
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
bool result_exact;
exponent += shift;