Skip to content

Commit 03cfb86

Browse files
authored
Workaround a CUDA issue in handing UTF-32 literals (#4719)
1 parent 4ccf1d4 commit 03cfb86

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

include/fmt/format.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,8 +3161,25 @@ constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t {
31613161
// It is equal to ceil(2^31 + 2^32/10^(k + 1)).
31623162
// These are stored in a string literal because we cannot have static arrays
31633163
// in constexpr functions and non-static ones are poorly optimized.
3164-
return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7"
3165-
U"\x800001ae\x8000002b"[index];
3164+
3165+
// while in C++23 we can use static constexpr, and in c++17 we can use out of
3166+
// function definition of inline constexpr, in C++11 we have to rely on string
3167+
// literals in order to avoid duplicating constant definitions across
3168+
// translation units. We take the following uint32 array definition:
3169+
// {0x9999999au, 0x828f5c29u, 0x80418938u, 0x80068db9,
3170+
// 0x8000a7c6u, 0x800010c7u, 0x800001aeu, 0x8000002b};
3171+
// and convert that into a series of char hexidecimal literals in a char16_t
3172+
// array:
3173+
// "\x9999\x999a \x828f\x5c29 \x8041\x8938 \x8006\x8db9
3174+
// \x8000\xa7c6 \x8000\x10c7 \x8000\x01ae \x8000\x002b";
3175+
// Then we split this up into two separate arrays of char16_ts, so they can
3176+
// be properly recombined into uint32_t.
3177+
3178+
return static_cast<uint32_t>(
3179+
u"\x9999\x828f\x8041\x8006\x8000\x8000\x8000\x8000"[index])
3180+
<< 16u |
3181+
static_cast<uint32_t>(
3182+
u"\x999a\x5c29\x8938\x8db9\xa7c6\x10c7\x01ae\x002b"[index]);
31663183
}
31673184

31683185
template <typename Float>

0 commit comments

Comments
 (0)