[libc-commits] [libc] 293ec48 - [libc] rename cpp::count_ones to cpp::popcount to better mirror std:: (#84388)

via libc-commits libc-commits at lists.llvm.org
Thu Mar 7 15:38:20 PST 2024


Author: Nick Desaulniers
Date: 2024-03-07T15:38:16-08:00
New Revision: 293ec4865bfcb6df2091ef4bcce706a566794b5c

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

LOG: [libc] rename cpp::count_ones to cpp::popcount to better mirror std:: (#84388)

libc/src/__support/CPP/bit.h and cpp:: is meant to mirror std::. Fix the
TODO.

Added: 
    

Modified: 
    libc/src/__support/CPP/bit.h
    libc/src/__support/UInt.h
    libc/src/stdbit/stdc_count_ones_uc.cpp
    libc/src/stdbit/stdc_count_ones_ui.cpp
    libc/src/stdbit/stdc_count_ones_ul.cpp
    libc/src/stdbit/stdc_count_ones_ull.cpp
    libc/src/stdbit/stdc_count_ones_us.cpp
    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 6b625b0c97a365..9c74a346949f0c 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -269,14 +269,12 @@ first_trailing_one(T value) {
   return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
 }
 
-/// Count number of 1's aka population count or hamming weight.
+/// Count number of 1's aka population count or Hamming weight.
 ///
 /// Only unsigned integral types are allowed.
-// TODO: rename as 'popcount' to follow the standard
-// https://en.cppreference.com/w/cpp/numeric/popcount
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
-count_ones(T value) {
+popcount(T value) {
   int count = 0;
   for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
     if ((value >> i) & 0x1)
@@ -285,7 +283,7 @@ count_ones(T value) {
 }
 #define ADD_SPECIALIZATION(TYPE, BUILTIN)                                      \
   template <>                                                                  \
-  [[nodiscard]] LIBC_INLINE constexpr int count_ones<TYPE>(TYPE value) {       \
+  [[nodiscard]] LIBC_INLINE constexpr int popcount<TYPE>(TYPE value) {         \
     return BUILTIN(value);                                                     \
   }
 ADD_SPECIALIZATION(unsigned char, __builtin_popcount)
@@ -300,7 +298,7 @@ ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
 count_zeros(T value) {
-  return count_ones<T>(static_cast<T>(~value));
+  return popcount<T>(static_cast<T>(~value));
 }
 
 } // namespace LIBC_NAMESPACE::cpp

diff  --git a/libc/src/__support/UInt.h b/libc/src/__support/UInt.h
index 925de8764715da..e899a79684b739 100644
--- a/libc/src/__support/UInt.h
+++ b/libc/src/__support/UInt.h
@@ -979,7 +979,7 @@ has_single_bit(T value) {
   for (auto word : value.val) {
     if (word == 0)
       continue;
-    bits += count_ones(word);
+    bits += popcount(word);
     if (bits > 1)
       return false;
   }

diff  --git a/libc/src/stdbit/stdc_count_ones_uc.cpp b/libc/src/stdbit/stdc_count_ones_uc.cpp
index 5a7314caa3baa0..1e998ff521b7db 100644
--- a/libc/src/stdbit/stdc_count_ones_uc.cpp
+++ b/libc/src/stdbit/stdc_count_ones_uc.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_uc, (unsigned char value)) {
-  return static_cast<unsigned>(cpp::count_ones(value));
+  return static_cast<unsigned>(cpp::popcount(value));
 }
 
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_count_ones_ui.cpp b/libc/src/stdbit/stdc_count_ones_ui.cpp
index 289f4bac31f7b8..e457dd793db33d 100644
--- a/libc/src/stdbit/stdc_count_ones_ui.cpp
+++ b/libc/src/stdbit/stdc_count_ones_ui.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ui, (unsigned value)) {
-  return static_cast<unsigned>(cpp::count_ones(value));
+  return static_cast<unsigned>(cpp::popcount(value));
 }
 
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_count_ones_ul.cpp b/libc/src/stdbit/stdc_count_ones_ul.cpp
index 83f3279d791937..ed86653fc7ee2e 100644
--- a/libc/src/stdbit/stdc_count_ones_ul.cpp
+++ b/libc/src/stdbit/stdc_count_ones_ul.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ul, (unsigned long value)) {
-  return static_cast<unsigned>(cpp::count_ones(value));
+  return static_cast<unsigned>(cpp::popcount(value));
 }
 
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_count_ones_ull.cpp b/libc/src/stdbit/stdc_count_ones_ull.cpp
index 104788aaf21265..c5ecc3cda6477a 100644
--- a/libc/src/stdbit/stdc_count_ones_ull.cpp
+++ b/libc/src/stdbit/stdc_count_ones_ull.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ull, (unsigned long long value)) {
-  return static_cast<unsigned>(cpp::count_ones(value));
+  return static_cast<unsigned>(cpp::popcount(value));
 }
 
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_count_ones_us.cpp b/libc/src/stdbit/stdc_count_ones_us.cpp
index 4b6ff0b94b626a..465c5c374e7c64 100644
--- a/libc/src/stdbit/stdc_count_ones_us.cpp
+++ b/libc/src/stdbit/stdc_count_ones_us.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_us, (unsigned short value)) {
-  return static_cast<unsigned>(cpp::count_ones(value));
+  return static_cast<unsigned>(cpp::popcount(value));
 }
 
 } // namespace LIBC_NAMESPACE

diff  --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
index 25a80ca9209c2f..d3f56d5bad83d3 100644
--- a/libc/test/src/__support/CPP/bit_test.cpp
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -260,9 +260,9 @@ TYPED_TEST(LlvmLibcBitTest, CountZeros, UnsignedTypesNoBigInt) {
 }
 
 TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypesNoBigInt) {
-  EXPECT_EQ(count_ones(T(0)), 0);
+  EXPECT_EQ(popcount(T(0)), 0);
   for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
-    EXPECT_EQ(count_ones<T>(cpp::numeric_limits<T>::max() >> i),
+    EXPECT_EQ(popcount<T>(cpp::numeric_limits<T>::max() >> i),
               cpp::numeric_limits<T>::digits - i);
 }
 


        


More information about the libc-commits mailing list