From 4c90efbcf5ec76a09588c850a9b42d6fc0e48541 Mon Sep 17 00:00:00 2001 From: Roman Koshelev Date: Sun, 12 Sep 2021 22:34:49 +0300 Subject: [PATCH] Speeding up write_significand() --- include/fmt/format.h | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 2621cf8480031..156eb2bea2426 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1708,14 +1708,25 @@ inline auto write_significand(Char* out, UInt significand, int significand_size, int integral_size, Char decimal_point) -> Char* { if (!decimal_point) return format_decimal(out, significand, significand_size).end; - auto end = format_decimal(out + 1, significand, significand_size).end; - if (integral_size == 1) { - out[0] = out[1]; + out += significand_size + 1; + Char* end = out; + int floating_size = significand_size - integral_size; + for(int i = (floating_size + 1) / 2; i > 0; --i) { + out -= 2; + copy2(out, digits2(significand % 100)); + significand /= 100; + } + if (floating_size % 2 != 0) { + Char tmp = *out; + *out = decimal_point; + *--out = tmp; + --integral_size; } else { - std::uninitialized_copy_n(out + 1, integral_size, - make_checked(out, to_unsigned(integral_size))); + *--out = decimal_point; } - out[integral_size] = decimal_point; + if (integral_size > 0) + format_decimal(out - integral_size, significand, integral_size); + return end; }