[libc-commits] [libc] b9be9f6 - [libc] Make the bit header compatible with uint128 types (#75161)

via libc-commits libc-commits at lists.llvm.org
Tue Dec 12 02:54:58 PST 2023


Author: Guillaume Chatelet
Date: 2023-12-12T11:54:54+01:00
New Revision: b9be9f66c72893feb95300c1564b789773280a01

URL: https://github.com/llvm/llvm-project/commit/b9be9f66c72893feb95300c1564b789773280a01
DIFF: https://github.com/llvm/llvm-project/commit/b9be9f66c72893feb95300c1564b789773280a01.diff

LOG: [libc] Make the bit header compatible with uint128 types (#75161)

Added: 
    

Modified: 
    libc/src/__support/CPP/bit.h
    libc/src/__support/UInt.h
    libc/test/src/__support/CPP/bit_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 2091c3662d5076..4de142b56165b6 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 f72b995f8788db..cfd495c5861851 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 b7cd99d169fc52..fef551bc1f0e2d 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)));


        


More information about the libc-commits mailing list