Skip to content

Commit 293ec48

Browse files
[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.
1 parent 3712edb commit 293ec48

File tree

8 files changed

+12
-14
lines changed

8 files changed

+12
-14
lines changed

libc/src/__support/CPP/bit.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,12 @@ first_trailing_one(T value) {
269269
return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
270270
}
271271

272-
/// Count number of 1's aka population count or hamming weight.
272+
/// Count number of 1's aka population count or Hamming weight.
273273
///
274274
/// Only unsigned integral types are allowed.
275-
// TODO: rename as 'popcount' to follow the standard
276-
// https://en.cppreference.com/w/cpp/numeric/popcount
277275
template <typename T>
278276
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
279-
count_ones(T value) {
277+
popcount(T value) {
280278
int count = 0;
281279
for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
282280
if ((value >> i) & 0x1)
@@ -285,7 +283,7 @@ count_ones(T value) {
285283
}
286284
#define ADD_SPECIALIZATION(TYPE, BUILTIN) \
287285
template <> \
288-
[[nodiscard]] LIBC_INLINE constexpr int count_ones<TYPE>(TYPE value) { \
286+
[[nodiscard]] LIBC_INLINE constexpr int popcount<TYPE>(TYPE value) { \
289287
return BUILTIN(value); \
290288
}
291289
ADD_SPECIALIZATION(unsigned char, __builtin_popcount)
@@ -300,7 +298,7 @@ ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
300298
template <typename T>
301299
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
302300
count_zeros(T value) {
303-
return count_ones<T>(static_cast<T>(~value));
301+
return popcount<T>(static_cast<T>(~value));
304302
}
305303

306304
} // namespace LIBC_NAMESPACE::cpp

libc/src/__support/UInt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ has_single_bit(T value) {
979979
for (auto word : value.val) {
980980
if (word == 0)
981981
continue;
982-
bits += count_ones(word);
982+
bits += popcount(word);
983983
if (bits > 1)
984984
return false;
985985
}

libc/src/stdbit/stdc_count_ones_uc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_uc, (unsigned char value)) {
17-
return static_cast<unsigned>(cpp::count_ones(value));
17+
return static_cast<unsigned>(cpp::popcount(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_ones_ui.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ui, (unsigned value)) {
17-
return static_cast<unsigned>(cpp::count_ones(value));
17+
return static_cast<unsigned>(cpp::popcount(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_ones_ul.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ul, (unsigned long value)) {
17-
return static_cast<unsigned>(cpp::count_ones(value));
17+
return static_cast<unsigned>(cpp::popcount(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_ones_ull.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_ull, (unsigned long long value)) {
17-
return static_cast<unsigned>(cpp::count_ones(value));
17+
return static_cast<unsigned>(cpp::popcount(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_ones_us.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_ones_us, (unsigned short value)) {
17-
return static_cast<unsigned>(cpp::count_ones(value));
17+
return static_cast<unsigned>(cpp::popcount(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/test/src/__support/CPP/bit_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ TYPED_TEST(LlvmLibcBitTest, CountZeros, UnsignedTypesNoBigInt) {
260260
}
261261

262262
TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypesNoBigInt) {
263-
EXPECT_EQ(count_ones(T(0)), 0);
263+
EXPECT_EQ(popcount(T(0)), 0);
264264
for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
265-
EXPECT_EQ(count_ones<T>(cpp::numeric_limits<T>::max() >> i),
265+
EXPECT_EQ(popcount<T>(cpp::numeric_limits<T>::max() >> i),
266266
cpp::numeric_limits<T>::digits - i);
267267
}
268268

0 commit comments

Comments
 (0)