diff --git a/include/fmt/format.h b/include/fmt/format.h index f6712bc6..de9f6ebf 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3149,12 +3149,6 @@ FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs, format_hexfloat(static_cast(value), specs, buf); } -// pre C++23 does not allow static constexpr inside of constexpr functions -// so we must declare this outside of fractional_part_rounding_thresholds -inline constexpr uint32_t rounding_thresholds[8] = { - 0x9999999au, 0x828f5c29u, 0x80418938u, 0x80068db9, - 0x8000a7c6u, 0x800010c7u, 0x800001aeu, 0x8000002bu}; - constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t { // For checking rounding thresholds. // The kth entry is chosen to be the smallest integer such that the @@ -3163,7 +3157,27 @@ constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t { // These are stored in a string literal because we cannot have static arrays // in constexpr functions and non-static ones are poorly optimized. - return rounding_thresholds[index]; + // while in C++23 we can use static constexpr, and in c++17 we can use out of + // function defintion of inline constexpr, in C++11 we have to rely on string + // literals in order to avoid duplicating constant definitions across + // translation units. We take the following uint32 array definition: + // {0x9999999au, 0x828f5c29u, 0x80418938u, 0x80068db9, + // 0x8000a7c6u, 0x800010c7u, 0x800001aeu, 0x8000002bu}; + // and convert that into a series of char hexidecimal literals in a char array: + // "\x99\x99\x99\x9a \x82\x8f\x5c\x29 \x80\x41\x89\x38 \x80\x06\x8d\xb9 + // \x80\x00\xa7\xc6 \x80\x00\x10\xc7 \x80\x00\x01\xae \x80\x00\x00\x2b" + // Then we split this up into four seperate arrays of bytes, so the bytes can + // be properly recombined into endian-correct uint32_t. + + + uint32_t byte_3 = static_cast("\x99\x82\x80\x80\x80\x80\x80\x80"[index]); + uint32_t byte_2 = static_cast("\x99\x8f\x41\x06\x00\x00\x00\x00"[index]); + uint32_t byte_1 = static_cast("\x99\x5c\x89\x8d\xa7\x10\x01\x00"[index]); + uint32_t byte_0 = static_cast("\x9a\x29\x38\xb9\xc6\xc7\xae\x2b"[index]); + + //recombine as uint32, this should eliminate endian issues, as now we are shifting + //btyes as uint32 which should match platform endian. + return byte_3 << 24u | byte_2 << 16u | byte_1 << 8u | byte_0; } template