From c36d825d9a5443f81d2656685ae021d6326da90c Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Sun, 7 Feb 2021 17:35:10 -0800 Subject: Export of internal Abseil changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -- 756156bf03da050e8b27539a8247d9af7e44c6a2 by Abseil Team : Fix a typo in cord.h: "accomodate" => "accommodate" PiperOrigin-RevId: 356168875 -- 638befdb342b608ec28910ee931ee200fdbe1fef by Samuel Benzaquen : Fix float conversion for PPC. In PPC `long double` is a double-double representation which behaves weirdly wrt numeric_limits. Don't take `long double` into account when we are not handling `long double` natively anyway. Fix the convert test to always run the conversion even if we are not going to compare against libc's printf result. This allows exercising the code itself to make sure we don't trigger assertions or UB found by sanitizers. PiperOrigin-RevId: 355857729 -- ff5f893319fa76b273c7785b76ef6c95b1791076 by Abseil Team : Example usage tweak PiperOrigin-RevId: 355695750 -- 0efc454f90023fa651b226e5e3ba7395a3b60c6d by Benjamin Barenblat : Remove endian-sensitivity from Abseil’s RNG Ensure that the Abseil random number generator produces identical output on both big- and little-endian platforms by byte-swapping appropriately on big-endian systems. PiperOrigin-RevId: 355635051 GitOrigin-RevId: 756156bf03da050e8b27539a8247d9af7e44c6a2 Change-Id: Iaaa69767b8e85d626742b9ba56fefb75f07c69ee --- .../internal/str_format/float_conversion.cc | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'absl/strings/internal/str_format/float_conversion.cc') diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index 2aa41aa7..2b1fd8cb 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -112,12 +112,22 @@ inline uint64_t DivideBy10WithCarry(uint64_t *v, uint64_t carry) { return next_carry % divisor; } +constexpr bool IsDoubleDouble() { + // This is the `double-double` representation of `long double`. + // We do not handle it natively. Fallback to snprintf. + return std::numeric_limits::digits == + 2 * std::numeric_limits::digits; +} + +using MaxFloatType = + typename std::conditional::type; + // Generates the decimal representation for an integer of the form `v * 2^exp`, // where `v` and `exp` are both positive integers. // It generates the digits from the left (ie the most significant digit first) // to allow for direct printing into the sink. // -// Requires `0 <= exp` and `exp <= numeric_limits::max_exponent`. +// Requires `0 <= exp` and `exp <= numeric_limits::max_exponent`. class BinaryToDecimal { static constexpr int ChunksNeeded(int exp) { // We will left shift a uint128 by `exp` bits, so we need `128+exp` total @@ -132,10 +142,10 @@ class BinaryToDecimal { static void RunConversion(uint128 v, int exp, absl::FunctionRef f) { assert(exp > 0); - assert(exp <= std::numeric_limits::max_exponent); + assert(exp <= std::numeric_limits::max_exponent); static_assert( static_cast(StackArray::kMaxCapacity) >= - ChunksNeeded(std::numeric_limits::max_exponent), + ChunksNeeded(std::numeric_limits::max_exponent), ""); StackArray::RunWithCapacity( @@ -232,14 +242,14 @@ class BinaryToDecimal { // Converts a value of the form `x * 2^-exp` into a sequence of decimal digits. // Requires `-exp < 0` and -// `-exp >= limits::min_exponent - limits::digits`. +// `-exp >= limits::min_exponent - limits::digits`. class FractionalDigitGenerator { public: // Run the conversion for `v * 2^exp` and call `f(generator)`. // This function will allocate enough stack space to perform the conversion. static void RunConversion( uint128 v, int exp, absl::FunctionRef f) { - using Limits = std::numeric_limits; + using Limits = std::numeric_limits; assert(-exp < 0); assert(-exp >= Limits::min_exponent - 128); static_assert(StackArray::kMaxCapacity >= @@ -871,10 +881,10 @@ void FormatA(const HexFloatTypeParams float_traits, Int mantissa, int exp, // This buffer holds the "0x1.ab1de3" portion of "0x1.ab1de3pe+2". Compute the // size with long double which is the largest of the floats. constexpr size_t kBufSizeForHexFloatRepr = - 2 // 0x - + std::numeric_limits::digits / 4 // number of hex digits - + 1 // round up - + 1; // "." (dot) + 2 // 0x + + std::numeric_limits::digits / 4 // number of hex digits + + 1 // round up + + 1; // "." (dot) char digits_buffer[kBufSizeForHexFloatRepr]; char *digits_iter = digits_buffer; const char *const digits = @@ -1393,10 +1403,7 @@ bool FloatToSink(const Float v, const FormatConversionSpecImpl &conv, bool ConvertFloatImpl(long double v, const FormatConversionSpecImpl &conv, FormatSinkImpl *sink) { - if (std::numeric_limits::digits == - 2 * std::numeric_limits::digits) { - // This is the `double-double` representation of `long double`. - // We do not handle it natively. Fallback to snprintf. + if (IsDoubleDouble()) { return FallbackToSnprintf(v, conv, sink); } -- cgit v1.2.3