[libc-commits] [libc] [libc] Make the bit header compatible with uint128 types (PR #75161)
via libc-commits
libc-commits at lists.llvm.org
Tue Dec 12 02:43:07 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Guillaume Chatelet (gchatelet)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/75161.diff
3 Files Affected:
- (modified) libc/src/__support/CPP/bit.h (+3-2)
- (modified) libc/src/__support/UInt.h (+2)
- (modified) libc/test/src/__support/CPP/bit_test.cpp (+5-1)
``````````diff
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 2091c3662d507..4de142b56165b 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -78,7 +78,7 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
return 0;
// Bisection method.
unsigned zero_bits = 0;
- T shift = cpp::numeric_limits<T>::digits >> 1;
+ unsigned shift = cpp::numeric_limits<T>::digits >> 1;
T mask = cpp::numeric_limits<T>::max() >> shift;
while (shift) {
if ((value & mask) == 0) {
@@ -115,7 +115,8 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
return cpp::numeric_limits<T>::digits;
// Bisection method.
unsigned zero_bits = 0;
- for (T shift = cpp::numeric_limits<T>::digits >> 1; shift; shift >>= 1) {
+ for (unsigned shift = cpp::numeric_limits<T>::digits >> 1; shift;
+ shift >>= 1) {
T tmp = value >> shift;
if (tmp)
value = tmp;
diff --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index f72b995f8788d..cfd495c586185 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -903,6 +903,7 @@ template <> class numeric_limits<UInt<128>> {
return UInt<128>({0xffff'ffff'ffff'ffff, 0xffff'ffff'ffff'ffff});
}
LIBC_INLINE static constexpr UInt<128> min() { return UInt<128>(0); }
+ LIBC_INLINE_VAR static constexpr int digits = 128;
};
template <> class numeric_limits<Int<128>> {
@@ -913,6 +914,7 @@ template <> class numeric_limits<Int<128>> {
LIBC_INLINE static constexpr Int<128> min() {
return Int<128>({0, 0x8000'0000'0000'0000});
}
+ LIBC_INLINE_VAR static constexpr int digits = 128;
};
// Provides is_integral of U/Int<128>, U/Int<192>, U/Int<256>.
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index b7cd99d169fc5..fef551bc1f0e2 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -16,7 +16,11 @@ namespace LIBC_NAMESPACE::cpp {
using UnsignedTypes =
testing::TypeList<unsigned char, unsigned short, unsigned int,
- unsigned long, unsigned long long>;
+ unsigned long, unsigned long long,
+#if defined(__SIZEOF_INT128__)
+ __uint128_t,
+#endif
+ cpp::UInt<128>>;
TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
EXPECT_FALSE(has_single_bit<T>(T(0)));
``````````
</details>
https://github.com/llvm/llvm-project/pull/75161
More information about the libc-commits
mailing list