[libcxx-commits] [libcxx] [libc++] Optimize std::has_single_bit (PR #133063)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 5 04:02:44 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;
----------------
Alcaro wrote:

If you don't care about MSVC compat, sure.

That one is indeed ugly - functional (as evidenced by compiler output), but human readers would think they'd wandered into the programming republic of Perl.

https://github.com/llvm/llvm-project/pull/133063


More information about the libcxx-commits mailing list