Skip to content

Commit

Permalink
Optimize shortest float formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 2, 2024
1 parent 1db2274 commit d25e403
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3194,16 +3194,6 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision,
exp = static_cast<int>(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<float>(value));
write<char>(appender(buf), dec.significand);
return dec.exponent;
}
auto dec = dragonbox::to_decimal(static_cast<double>(value));
write<char>(appender(buf), dec.significand);
return dec.exponent;
} else {
// Extract significand bits and exponent bits.
using info = dragonbox::float_info<double>;
Expand Down Expand Up @@ -3512,6 +3502,12 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs,
precision = 1;
}

if (is_fast_float<T>::value && precision < 0) {
// Use Dragonbox for the shortest format.
using floaty = conditional_t<sizeof(T) >= sizeof(double), double, float>;
auto dec = dragonbox::to_decimal(static_cast<floaty>(value));
return write_float<Char>(out, dec, specs, sign, loc);
}
int exp = format_float(convert_float(value), precision, specs,
std::is_same<T, float>(), buffer);

Expand Down

0 comments on commit d25e403

Please sign in to comment.