[libcxx-commits] [libcxx] [libc++] Optimize `to_chars` for bases 2, 8 and 16 (PR #223222)
David Bayer via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Sep 13 02:42:36 PDT 2026
================
@@ -197,21 +224,36 @@ struct _LIBCPP_HIDDEN __integral<16> {
if (__n > __cap)
return {__last, errc::value_too_large};
- __last = __first + __n;
- char* __p = __last;
- unsigned __divisor = 256;
- while (__value > __divisor) {
- unsigned __c = __value % __divisor;
+ __last = __first + __n;
+ char* __p = __last;
+
+ constexpr auto __ncs_per_word = 8u;
+ constexpr auto __divisor = uint64_t{1} << (4 * __ncs_per_word);
+ constexpr auto __dep_mask = uint64_t{0x0f0f'0f0f'0f0f'0f0f};
+ constexpr auto __add_six_mask = uint64_t{0x0606'0606'0606'0606};
+ constexpr auto __high_bit_mask = uint64_t{0x1010'1010'1010'1010};
+ constexpr auto __c0_offset_mask = uint64_t{0x3030'3030'3030'3030};
+ while (__value >= __divisor) {
+ const auto __digits = __builtin_elementwise_pdep(static_cast<uint64_t>(__value), __dep_mask);
+ const auto __alpha = ((__digits + __add_six_mask) & __high_bit_mask) >> 4;
+ auto __cs = __digits + __c0_offset_mask + __alpha * 0x27;
----------------
davebayer wrote:
For base 16, `'0'` and `'a'` have different offsets, so we need to account for that
https://github.com/llvm/llvm-project/pull/223222
More information about the libcxx-commits
mailing list