From 4c346e97e1a771930c188b8d38eaf65b6d651ca9 Mon Sep 17 00:00:00 2001 From: Greg Sjaardema Date: Wed, 20 Sep 2023 12:27:08 -0600 Subject: [PATCH] Refactor proposed intel fix. Moved variable out of function to avoid specialization on Float. Made it a separate function that is called from format_float. --- include/fmt/format.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index bf766f26..40c95abf 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3258,14 +3258,11 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, int precision, format_hexfloat(static_cast(value), precision, specs, buf); } -template -FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, - buffer& buf) -> int { - +FMT_CONSTEXPR uint32_t fractional_part_rounding_threshold(int index) { // For checking rounding thresholds. // The kth entry is chosen to be the smallest integer such that the // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k. - static constexpr uint32_t fractional_part_rounding_thresholds[8] = { + constexpr uint32_t thresholds[8] = { 2576980378U, // ceil(2^31 + 2^32/10^1) 2190433321U, // ceil(2^31 + 2^32/10^2) 2151778616U, // ceil(2^31 + 2^32/10^3) @@ -3275,7 +3272,13 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, 2147484078U, // ceil(2^31 + 2^32/10^7) 2147483691U // ceil(2^31 + 2^32/10^8) }; + return thresholds[index]; +} +template +FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, + buffer& buf) -> int { + // float is passed as double to reduce the number of instantiations. static_assert(!std::is_same::value, ""); FMT_ASSERT(value >= 0, "value is negative"); @@ -3479,7 +3482,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, uint32_t fractional_part = static_cast(prod); should_round_up = fractional_part >= fractional_part_rounding_thresholds - [8 - number_of_digits_to_print] || + (8 - number_of_digits_to_print) || ((fractional_part >> 31) & ((digits & 1) | (second_third_subsegments != 0) | has_more_segments)) != 0; @@ -3519,7 +3522,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, uint32_t fractional_part = static_cast(prod); should_round_up = fractional_part >= fractional_part_rounding_thresholds - [8 - number_of_digits_to_print] || + (8 - number_of_digits_to_print) || ((fractional_part >> 31) & ((digits & 1) | (third_subsegment != 0) | has_more_segments)) != 0;