From 8a06cee8262f0f5932254261233dd4d386d9f252 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 1 Aug 2024 18:22:09 -0700 Subject: [PATCH] Optimize shortest float formatting --- include/fmt/format.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 98896bb7..2cab8939 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3194,16 +3194,6 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, exp = static_cast(e); if (e > exp) ++exp; // Compute ceil. dragon_flags = dragon::fixup; - } else if (precision < 0) { - // Use Dragonbox for the shortest format. - if (binary32) { - auto dec = dragonbox::to_decimal(static_cast(value)); - write(appender(buf), dec.significand); - return dec.exponent; - } - auto dec = dragonbox::to_decimal(static_cast(value)); - write(appender(buf), dec.significand); - return dec.exponent; } else { // Extract significand bits and exponent bits. using info = dragonbox::float_info; @@ -3497,9 +3487,18 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, specs); } - int precision = specs.precision >= 0 || specs.type == presentation_type::none - ? specs.precision - : 6; + int precision = specs.precision; + if (precision < 0) { + if (specs.type != presentation_type::none) { + precision = 6; + } else if (is_fast_float::value && !is_constant_evaluated()) { + // Use Dragonbox for the shortest format. + using floaty = conditional_t= sizeof(double), double, float>; + auto dec = dragonbox::to_decimal(static_cast(value)); + return write_float(out, dec, specs, sign, loc); + } + } + if (specs.type == presentation_type::exp) { if (precision == max_value()) report_error("number is too big"); @@ -3511,7 +3510,6 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, } else if (precision == 0) { precision = 1; } - int exp = format_float(convert_float(value), precision, specs, std::is_same(), buffer);