PR #2165: Use floating-point to_chars in RoundTripDoubleToBuffer when available Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/2165 `RoundTripDoubleToBuffer` previously used `snprintf` and a `strtod` parse-back check for finite doubles. On standard libraries that provide floating-point `std::to_chars`, use `std::chars_format::general` with the existing 15/17-digit precision policy and `absl::from_chars` for the retry check. The fast path is gated by standard-library and deployment-target support rather than compiler version (`__cpp_lib_to_chars >= 201611L`, or `libc++` 14+ when floating-point `std::to_chars` is available on the target platform). The existing `snprintf` implementation remains as a fallback for unsupported standard libraries and older Apple deployment targets. Non-finite values (`NaN`, `inf`, `-inf`), signed zero, locale-independent output, the `DBL_MAX` guard, buffer sizing, and round-trip retry semantics are preserved. This work originated from protobuf/protobuf#29763, where a Protobuf maintainer suggested moving the optimization into Abseil so downstream users can share the same formatting primitive. Merge c26dc3f2d1c810f2e0df5197777e3b904fd4ffad into 68e60c4c212c0e856621e34df30ecd55ae69af25 Merging this change closes #2165 PiperOrigin-RevId: 982494515
diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index c574dee..8a17f1d 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc
@@ -21,6 +21,7 @@ #include <array> #include <cassert> #include <cfloat> // for DBL_DIG and FLT_DIG +#include <charconv> #include <clocale> // for localeconv #include <cmath> // for HUGE_VAL #include <cstdint> @@ -411,6 +412,25 @@ static constexpr double kDoublePrecisionCheckMax = std::numeric_limits<double>::max() / 1.000000000000001; +// __cpp_lib_to_chars is defined only when both integer and floating-point +// <charconv> overloads are available. libc++ 14+ provides floating-point +// std::to_chars for double, but leaves __cpp_lib_to_chars undefined because +// floating-point std::from_chars is still incomplete. On Apple platforms, +// floating-point std::to_chars also requires macOS 13.3+ / iOS 16.3+ dylib +// support (tracked via _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT in +// libc++ 16+ / Xcode 14.3+, and marked unavailable on Apple in libc++ 14-15). +#if defined(__cpp_lib_to_chars) && __cpp_lib_to_chars >= 201611L +#define ABSL_INTERNAL_HAVE_STD_TO_CHARS_FLOAT 1 +#elif defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 14000 && \ + ((defined(_LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT) && \ + _LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT) || \ + (!defined(_LIBCPP_AVAILABILITY_HAS_TO_CHARS_FLOATING_POINT) && \ + !defined(__APPLE__))) +#define ABSL_INTERNAL_HAVE_STD_TO_CHARS_FLOAT 1 +#else +#define ABSL_INTERNAL_HAVE_STD_TO_CHARS_FLOAT 0 +#endif + char* absl_nonnull numbers_internal::RoundTripDoubleToBuffer( double d, char* absl_nonnull buffer) { // DBL_DIG is 15 for IEEE-754 doubles, which are used on almost all @@ -420,12 +440,49 @@ static_assert(std::numeric_limits<double>::digits10 < 20, "std::numeric_limits<double>::digits10 is too big"); - // We avoid snprintf for NaNs because it inconsistently outputs "-nan" - // or "nan" when given a nan with the sign bit set. - if (std::isnan(d)) { - strcpy(buffer, "nan"); // NOLINT(runtime/printf) + if (ABSL_PREDICT_FALSE(!std::isfinite(d))) { + // Avoid snprintf/to_chars for NaNs because they can output "-nan" when + // given a NaN with the sign bit set. + if (std::isnan(d)) { + std::memcpy(buffer, "nan", sizeof("nan")); + } else if (std::signbit(d)) { + std::memcpy(buffer, "-inf", sizeof("-inf")); + } else { + std::memcpy(buffer, "inf", sizeof("inf")); + } return buffer; } + +#if ABSL_INTERNAL_HAVE_STD_TO_CHARS_FLOAT + // std::to_chars is locale-independent, rounding-mode-independent (matching + // absl::SimpleAtod), and substantially faster than the snprintf/strtod + // fallback below. + auto to_chars = [buffer, d](int precision) { + std::to_chars_result result = + std::to_chars(buffer, buffer + numbers_internal::kFastToBufferSize - 1, + d, std::chars_format::general, precision); + ABSL_ASSERT(result.ec == std::errc()); + *result.ptr = '\0'; + return result.ptr; + }; + + bool full_precision_needed = true; + if (std::abs(d) <= kDoublePrecisionCheckMax) { + char* end = to_chars(std::numeric_limits<double>::digits10); + double parsed_value = 0; + absl::from_chars_result parse_result = + absl::from_chars(buffer, end, parsed_value); + full_precision_needed = parse_result.ec != std::errc() || + parse_result.ptr != end || parsed_value != d; + } + + if (full_precision_needed) { + to_chars(std::numeric_limits<double>::digits10 + 2); + } + return buffer; +#else + // TODO(b/526633099) - Once all supported standard libraries ship + // std::to_chars with floating-point support, remove this fallback. bool full_precision_needed = true; if (std::abs(d) <= kDoublePrecisionCheckMax) { int snprintf_result = @@ -457,8 +514,6 @@ // SimpleAtod() only accepts '.', so rewrite the radix back to '.' to keep // absl::HighPrecision(double) locale-independent and round-trippable through // SimpleAtod(). - // TODO: b/526633099 - Once all supported compilers ship std::to_chars with - // floating-point support, use it here for inherent locale independence. const char* radix = localeconv()->decimal_point; // Skip an empty decimal_point (some minimal environments leave it ""), which // would otherwise match the beginning of the buffer and corrupt it. @@ -474,8 +529,11 @@ } } return buffer; +#endif } +#undef ABSL_INTERNAL_HAVE_STD_TO_CHARS_FLOAT + namespace { // This table is used to quickly calculate the base-ten exponent of a given
diff --git a/absl/strings/numbers_benchmark.cc b/absl/strings/numbers_benchmark.cc index 9511024..367586c 100644 --- a/absl/strings/numbers_benchmark.cc +++ b/absl/strings/numbers_benchmark.cc
@@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include <cstddef> #include <cstdint> #include <limits> #include <random> @@ -43,6 +44,36 @@ BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int32_t)->Range(0, 1 << 15); BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int64_t)->Range(0, 1 << 30); +struct RoundTripDoubleBenchmarkCase { + const char* label; + double value; +}; + +void BM_RoundTripDoubleToBuffer(benchmark::State& state) { + static const RoundTripDoubleBenchmarkCase kCases[] = { + {"common", 3.141592653589793}, + {"small", 0.00000123456789012345}, + {"large", 1.2345678901234568e+300}, + {"subnormal", std::numeric_limits<double>::denorm_min()}, + {"retry", 1.2345678901234567}, + {"fixed_max", 1e14}, + {"scientific_min", 1e15}, + }; + const RoundTripDoubleBenchmarkCase& test_case = + kCases[static_cast<size_t>(state.range(0))]; + state.SetLabel(test_case.label); + + char buffer[absl::numbers_internal::kFastToBufferSize]; + for (auto _ : state) { + double value = test_case.value; + benchmark::DoNotOptimize(value); + benchmark::DoNotOptimize( + absl::numbers_internal::RoundTripDoubleToBuffer(value, buffer)); + benchmark::DoNotOptimize(buffer); + } +} +BENCHMARK(BM_RoundTripDoubleToBuffer)->DenseRange(0, 6); + // Creates an integer that would be printed as `num_digits` repeated 7s in the // given `base`. `base` must be greater than or equal to 8. int64_t RepeatedSevens(int num_digits, int base) {
diff --git a/absl/strings/numbers_test.cc b/absl/strings/numbers_test.cc index 6f0cde8..5a87cb5 100644 --- a/absl/strings/numbers_test.cc +++ b/absl/strings/numbers_test.cc
@@ -1750,6 +1750,59 @@ EXPECT_EQ(parsed, 0.1); } +TEST_F(SimpleDtoaTest, RoundTripDoubleToBufferEdgeCases) { + struct TestCase { + double value; + const char* expected; + }; + const TestCase test_cases[] = { + {0.0, "0"}, + {-0.0, "-0"}, + {0.1, "0.1"}, + {-0.1, "-0.1"}, + {0.0001, "0.0001"}, + {0.00001, "1e-05"}, + {1e14, "100000000000000"}, + {1e15, "1e+15"}, + {1000000000000000.25, "1000000000000000.2"}, + {1.2345678901234567, "1.2345678901234567"}, + {1e20, "1e+20"}, + {1e21, "1e+21"}, + {std::numeric_limits<double>::denorm_min(), "4.94065645841247e-324"}, + {std::numeric_limits<double>::min(), "2.2250738585072014e-308"}, + {std::nextafter(std::numeric_limits<double>::max(), 0.0), + "1.7976931348623155e+308"}, + {std::numeric_limits<double>::max(), "1.7976931348623157e+308"}, + {-std::numeric_limits<double>::max(), "-1.7976931348623157e+308"}, + {std::numeric_limits<double>::infinity(), "inf"}, + {-std::numeric_limits<double>::infinity(), "-inf"}, + }; + + for (const TestCase& test_case : test_cases) { + char buffer[absl::numbers_internal::kFastToBufferSize]; + EXPECT_EQ(buffer, absl::numbers_internal::RoundTripDoubleToBuffer( + test_case.value, buffer)); + EXPECT_STREQ(test_case.expected, buffer); + + double parsed = 0; + EXPECT_TRUE(absl::SimpleAtod(buffer, &parsed)); + EXPECT_EQ(test_case.value, parsed); + EXPECT_EQ(std::signbit(test_case.value), std::signbit(parsed)); + } + + for (double nan_val : {std::numeric_limits<double>::quiet_NaN(), + -std::numeric_limits<double>::quiet_NaN()}) { + char buffer[absl::numbers_internal::kFastToBufferSize]; + EXPECT_EQ(buffer, + absl::numbers_internal::RoundTripDoubleToBuffer(nan_val, buffer)); + EXPECT_STREQ("nan", buffer); + + double parsed = 0; + EXPECT_TRUE(absl::SimpleAtod(buffer, &parsed)); + EXPECT_TRUE(std::isnan(parsed)); + } +} + // Run the given runnable functor for "cases" test cases, chosen over the // available range of float. pi and e and 1/e are seeded, and then all // available integer powers of 2 and 10 are multiplied against them. In