Skip to content

Conversation

@Alcaro
Copy link
Contributor

@Alcaro Alcaro commented Mar 26, 2025

Clang translates most implementations of has_single_bit to (v ^ (v-1)) > v-1 - except the one definition libc++ actually uses.

Proof of correctness: https://godbolt.org/z/d61bxW4r1

(Could also be fixed by teaching Clang to optimize better, but making source match output feels clearer to me. And it improves unoptimized performance.)

@Alcaro Alcaro requested a review from a team as a code owner March 26, 2025 09:59
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-libcxx

Author: None (Alcaro)

Changes

Clang translates most implementations of has_single_bit to (v ^ (v-1)) > v-1 - except the one definition libc++ actually uses.

Proof of correctness: https://godbolt.org/z/d61bxW4r1

(Could also be fixed by teaching Clang to optimize better, but making source match output feels clearer to me. And it improves unoptimized performance.)


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

1 Files Affected:

  • (modified) libcxx/include/__bit/has_single_bit.h (+1-1)
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

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you need to rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants