[libc-commits] [libc] [libc] rename cpp::count_ones to cpp::popcount to better mirror std:: (PR #84388)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Thu Mar 7 13:52:02 PST 2024
https://github.com/nickdesaulniers created https://github.com/llvm/llvm-project/pull/84388
libc/src/__support/CPP/bit.h and cpp:: is meant to mirror std::. Fix the TODO.
>From 64c92d1485979ad40bbd477ae556985b50854d5c Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 7 Mar 2024 13:50:27 -0800
Subject: [PATCH] [libc] rename cpp::count_ones to cpp::popcount to better
mirror std::
libc/src/__support/CPP/bit.h and cpp:: is meant to mirror std::. Fix the TODO.
---
libc/src/__support/CPP/bit.h | 10 ++++------
libc/src/stdbit/stdc_count_ones_uc.cpp | 2 +-
libc/src/stdbit/stdc_count_ones_ui.cpp | 2 +-
libc/src/stdbit/stdc_count_ones_ul.cpp | 2 +-
libc/src/stdbit/stdc_count_ones_ull.cpp | 2 +-
libc/src/stdbit/stdc_count_ones_us.cpp | 2 +-
6 files changed, 9 insertions(+), 11 deletions(-)
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 6b625b0c97a365..015a55cc53a214 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/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
More information about the libc-commits
mailing list