Skip to content

Commit 48b0bc8

Browse files
committed
Remove non standard bit functions specializations, add documentation about non standard functions in the bit header.
1 parent 71a1599 commit 48b0bc8

File tree

3 files changed

+24
-63
lines changed

3 files changed

+24
-63
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,21 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
239239
}
240240
}
241241

242+
// TODO: remove from 'bit.h' as it is not a standard function.
242243
template <typename T>
243244
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
244245
first_leading_zero(T value) {
245246
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
246247
}
247248

249+
// TODO: remove from 'bit.h' as it is not a standard function.
248250
template <typename T>
249251
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
250252
first_leading_one(T value) {
251253
return first_leading_zero(static_cast<T>(~value));
252254
}
253255

256+
// TODO: remove from 'bit.h' as it is not a standard function.
254257
template <typename T>
255258
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
256259
first_trailing_zero(T value) {
@@ -259,6 +262,7 @@ first_trailing_zero(T value) {
259262
: countr_zero(static_cast<T>(~value)) + 1;
260263
}
261264

265+
// TODO: remove from 'bit.h' as it is not a standard function.
262266
template <typename T>
263267
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
264268
first_trailing_one(T value) {
@@ -268,6 +272,8 @@ first_trailing_one(T value) {
268272
/// Count number of 1's aka population count or hamming weight.
269273
///
270274
/// Only unsigned integral types are allowed.
275+
// TODO: rename as 'popcount' to follow the standard
276+
// https://en.cppreference.com/w/cpp/numeric/popcount
271277
template <typename T>
272278
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
273279
count_ones(T value) {
@@ -290,6 +296,7 @@ ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
290296
// TODO: 128b specializations?
291297
#undef ADD_SPECIALIZATION
292298

299+
// TODO: remove from 'bit.h' as it is not a standard function.
293300
template <typename T>
294301
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
295302
count_zeros(T value) {

libc/src/__support/UInt.h

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,58 +1050,6 @@ rotr(T value, int rotate) {
10501050
return (value >> rotate) | (value << (N - rotate));
10511051
}
10521052

1053-
// Specialization of cpp::first_leading_zero ('bit.h') for BigInt.
1054-
template <typename T>
1055-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1056-
first_leading_zero(T value) {
1057-
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
1058-
}
1059-
1060-
// Specialization of cpp::first_leading_one ('bit.h') for BigInt.
1061-
template <typename T>
1062-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1063-
first_leading_one(T value) {
1064-
// TODO : Implement a faster version not involving operator~.
1065-
return first_leading_zero(static_cast<T>(~value));
1066-
}
1067-
1068-
// Specialization of cpp::first_trailing_zero ('bit.h') for BigInt.
1069-
template <typename T>
1070-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1071-
first_trailing_zero(T value) {
1072-
// TODO : Implement a faster version not involving operator~.
1073-
return value == cpp::numeric_limits<T>::max()
1074-
? 0
1075-
: countr_zero(static_cast<T>(~value)) + 1;
1076-
}
1077-
1078-
// Specialization of cpp::first_trailing_one ('bit.h') for BigInt.
1079-
template <typename T>
1080-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1081-
first_trailing_one(T value) {
1082-
return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
1083-
}
1084-
1085-
// Specialization of cpp::count_ones ('bit.h') for BigInt.
1086-
template <typename T>
1087-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1088-
count_ones(T value) {
1089-
int count = 0;
1090-
for (auto word : value.val)
1091-
count += count_ones(word);
1092-
return count;
1093-
}
1094-
1095-
// Specialization of cpp::count_zeros ('bit.h') for BigInt.
1096-
template <typename T>
1097-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, int>
1098-
count_zeros(T value) {
1099-
int count = 0;
1100-
for (auto word : value.val)
1101-
count += count_zeros(word);
1102-
return count;
1103-
}
1104-
11051053
} // namespace LIBC_NAMESPACE::cpp
11061054

11071055
#endif // LLVM_LIBC_SRC___SUPPORT_UINT_H

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@
1616

1717
namespace LIBC_NAMESPACE::cpp {
1818

19-
using UnsignedTypes =
20-
testing::TypeList<unsigned char, unsigned short, unsigned int,
21-
unsigned long, unsigned long long,
19+
using UnsignedTypesNoBigInt = testing::TypeList<
2220
#if defined(__SIZEOF_INT128__)
23-
__uint128_t,
21+
__uint128_t,
2422
#endif
25-
cpp::UInt<128>>;
23+
unsigned char, unsigned short, unsigned int, unsigned long,
24+
unsigned long long>;
25+
26+
using UnsignedTypes = testing::TypeList<
27+
#if defined(__SIZEOF_INT128__)
28+
__uint128_t,
29+
#endif
30+
unsigned char, unsigned short, unsigned int, unsigned long,
31+
unsigned long long, cpp::UInt<128>>;
2632

2733
TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
2834
constexpr auto ZERO = T(0);
@@ -223,39 +229,39 @@ TEST(LlvmLibcBitTest, Rotr) {
223229
rotr<uint64_t>(0x12345678deadbeefULL, -19));
224230
}
225231

226-
TYPED_TEST(LlvmLibcBitTest, FirstLeadingZero, UnsignedTypes) {
232+
TYPED_TEST(LlvmLibcBitTest, FirstLeadingZero, UnsignedTypesNoBigInt) {
227233
EXPECT_EQ(first_leading_zero<T>(cpp::numeric_limits<T>::max()), 0);
228234
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
229235
EXPECT_EQ(first_leading_zero<T>(~(T(1) << i)),
230236
cpp::numeric_limits<T>::digits - i);
231237
}
232238

233-
TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypes) {
239+
TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypesNoBigInt) {
234240
EXPECT_EQ(first_leading_one<T>(static_cast<T>(0)), 0);
235241
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
236242
EXPECT_EQ(first_leading_one<T>(T(1) << i),
237243
cpp::numeric_limits<T>::digits - i);
238244
}
239245

240-
TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypes) {
246+
TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) {
241247
EXPECT_EQ(first_trailing_zero<T>(cpp::numeric_limits<T>::max()), 0);
242248
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
243249
EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
244250
}
245251

246-
TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypes) {
252+
TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) {
247253
EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 0);
248254
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
249255
EXPECT_EQ(first_trailing_one<T>(T(1) << i), i + 1);
250256
}
251257

252-
TYPED_TEST(LlvmLibcBitTest, CountZeros, UnsignedTypes) {
258+
TYPED_TEST(LlvmLibcBitTest, CountZeros, UnsignedTypesNoBigInt) {
253259
EXPECT_EQ(count_zeros(T(0)), cpp::numeric_limits<T>::digits);
254260
for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
255261
EXPECT_EQ(count_zeros<T>(cpp::numeric_limits<T>::max() >> i), i);
256262
}
257263

258-
TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypes) {
264+
TYPED_TEST(LlvmLibcBitTest, CountOnes, UnsignedTypesNoBigInt) {
259265
EXPECT_EQ(count_ones(T(0)), 0);
260266
for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
261267
EXPECT_EQ(count_ones<T>(cpp::numeric_limits<T>::max() >> i),

0 commit comments

Comments
 (0)