[libcxx-commits] [libcxx] [libc++] Optimize std::has_single_bit (PR #133063)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 5 03:36:01 PST 2025
================
@@ -25,7 +25,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <__libcpp_unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
- return __t != 0 && (((__t & (__t - 1)) == 0));
+ return (__t ^ (__t - 1)) > __t - 1;
----------------
philnik777 wrote:
I think `__builtin_popcountg(__t) == 1` would be better. That results in identical code and is obviously correct (and if some backend wants to handle this case specially it's trivial to match the IR).
https://github.com/llvm/llvm-project/pull/133063
More information about the libcxx-commits
mailing list