[libcxx-commits] [libcxx] [libc++] Optimize bitset::to_string (PR #128832)
Peng Liu via libcxx-commits
libcxx-commits at lists.llvm.org
Thu May 15 08:32:06 PDT 2025
================
@@ -529,6 +555,21 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long __
return __first_;
}
+template <size_t _Size>
+template <bool _Spare, class _CharT, class _Traits, class _Allocator>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 basic_string<_CharT, _Traits, _Allocator>
+__bitset<1, _Size>::__to_string(_CharT __zero, _CharT __one) const {
+ basic_string<_CharT, _Traits, _Allocator> __r(_Size, __zero);
+ __storage_type __word = std::__invert_if<!_Spare>(__first_);
+ if (_Size < __bits_per_word)
+ __word &= (__storage_type(1) << _Size) - 1;
+ for (; __word; __word &= (__word - 1)) {
+ size_t __pos = std::__countr_zero(__word);
+ __r[_Size - 1 - __pos] = __one;
----------------
winner245 wrote:
Thanks. I've inlined the selection of `__zero` and `__one` within the `__to_string` function. So the optimization logic is now in a single place.
https://github.com/llvm/llvm-project/pull/128832
More information about the libcxx-commits
mailing list