Skip to content

Commit 457a41d

Browse files
fixup math extras
1 parent 3d400c6 commit 457a41d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

libc/src/__support/UInt.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,49 @@ LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() {
12291229
return out;
12301230
}
12311231

1232+
// Specialization of count_zeros ('math_extras.h') for BigInt.
1233+
template <typename T>
1234+
[[nodiscard]]
1235+
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
1236+
count_zeros(T value) {
1237+
return cpp::popcount(~value);
1238+
}
1239+
1240+
// Specialization of first_leading_zero ('math_extras.h') for BigInt.
1241+
template <typename T>
1242+
[[nodiscard]]
1243+
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
1244+
first_leading_zero(T value) {
1245+
return value == cpp::numeric_limits<T>::max() ? 0
1246+
: cpp::countl_one(value) + 1;
1247+
}
1248+
1249+
// Specialization of first_leading_one ('math_extras.h') for BigInt.
1250+
template <typename T>
1251+
[[nodiscard]]
1252+
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
1253+
first_leading_one(T value) {
1254+
return first_leading_zero(~value);
1255+
}
1256+
1257+
// Specialization of first_trailing_zero ('math_extras.h') for BigInt.
1258+
template <typename T>
1259+
[[nodiscard]]
1260+
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
1261+
first_trailing_zero(T value) {
1262+
return value == cpp::numeric_limits<T>::max() ? 0
1263+
: cpp::countr_zero(~value) + 1;
1264+
}
1265+
1266+
// Specialization of first_trailing_one ('math_extras.h') for BigInt.
1267+
template <typename T>
1268+
[[nodiscard]]
1269+
LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
1270+
first_trailing_one(T value) {
1271+
return value == cpp::numeric_limits<T>::max() ? 0
1272+
: cpp::countr_zero(value) + 1;
1273+
}
1274+
12321275
} // namespace LIBC_NAMESPACE
12331276

12341277
#endif // LLVM_LIBC_SRC___SUPPORT_UINT_H

libc/test/src/__support/math_extras_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/UInt128.h" // UInt128
9+
#include "src/__support/UInt128.h" // UInt<128>
1010
#include "src/__support/integer_literals.h"
1111
#include "src/__support/math_extras.h"
1212
#include "test/UnitTest/Test.h"
@@ -19,7 +19,7 @@ using UnsignedTypesNoBigInt = testing::TypeList<
1919
__uint128_t,
2020
#endif // LIBC_TYPES_HAS_INT128
2121
unsigned char, unsigned short, unsigned int, unsigned long,
22-
unsigned long long>;
22+
unsigned long long, UInt<128>>;
2323

2424
TEST(LlvmLibcBlockMathExtrasTest, mask_trailing_ones) {
2525
EXPECT_EQ(0_u8, (mask_leading_ones<uint8_t, 0>()));

0 commit comments

Comments
 (0)