From 549d11d551f8efbc572e4a847f5dfaf0da08709a Mon Sep 17 00:00:00 2001 From: Alcaro Date: Wed, 26 Mar 2025 10:46:19 +0100 Subject: [PATCH 1/2] [libc++] Optimize std::has_single_bit --- libcxx/include/__bit/has_single_bit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcxx/include/__bit/has_single_bit.h b/libcxx/include/__bit/has_single_bit.h index 52f5853a1bc8a..ac434092a148f 100644 --- a/libcxx/include/__bit/has_single_bit.h +++ b/libcxx/include/__bit/has_single_bit.h @@ -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; } _LIBCPP_END_NAMESPACE_STD From 144f83e43eca05545f7bb1f42f4f36d4d6d141ca Mon Sep 17 00:00:00 2001 From: Alcaro Date: Wed, 5 Nov 2025 13:03:01 +0100 Subject: [PATCH 2/2] [libc++] Optimize std::has_single_bit v2.0 --- libcxx/include/__bit/has_single_bit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcxx/include/__bit/has_single_bit.h b/libcxx/include/__bit/has_single_bit.h index ac434092a148f..5584259288e0c 100644 --- a/libcxx/include/__bit/has_single_bit.h +++ b/libcxx/include/__bit/has_single_bit.h @@ -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 ^ (__t - 1)) > __t - 1; + return __builtin_popcountg(__t) == 1; } _LIBCPP_END_NAMESPACE_STD