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

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Tue Dec 12 02:42:35 PST 2023


https://github.com/gchatelet created https://github.com/llvm/llvm-project/pull/75161

None

>From 2fc4bb77f70590a2e51f51b5bfb841f5f8dff303 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 12 Dec 2023 10:42:12 +0000
Subject: [PATCH] [libc] Make the bit header compatible with uint128 types

---
 libc/src/__support/CPP/bit.h             | 5 +++--
 libc/src/__support/UInt.h                | 2 ++
 libc/test/src/__support/CPP/bit_test.cpp | 6 +++++-
 3 files changed, 10 insertions(+), 3 deletions(-)

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