@@ -1000,15 +1000,15 @@ countl_zero(const T &value) {
1000
1000
template <typename T>
1001
1001
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1002
1002
countl_one (T value) {
1003
- // TODO : Implement a faster version.
1003
+ // TODO : Implement a faster version not involving operator~ .
1004
1004
return cpp::countl_zero<T>(~value);
1005
1005
}
1006
1006
1007
1007
// Specialization of cpp::countr_one ('bit.h') for BigInt.
1008
1008
template <typename T>
1009
1009
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1010
1010
countr_one (T value) {
1011
- // TODO : Implement a faster version.
1011
+ // TODO : Implement a faster version not involving operator~ .
1012
1012
return cpp::countr_zero<T>(~value);
1013
1013
}
1014
1014
@@ -1028,7 +1028,6 @@ rotr(T value, int rotate);
1028
1028
template <typename T>
1029
1029
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, T>
1030
1030
rotl (T value, int rotate) {
1031
- // TODO : Implement a faster version.
1032
1031
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1033
1032
rotate = rotate % N;
1034
1033
if (!rotate)
@@ -1042,7 +1041,6 @@ rotl(T value, int rotate) {
1042
1041
template <typename T>
1043
1042
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, T>
1044
1043
rotr (T value, int rotate) {
1045
- // TODO : Implement a faster version.
1046
1044
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1047
1045
rotate = rotate % N;
1048
1046
if (!rotate)
@@ -1063,13 +1061,15 @@ first_leading_zero(T value) {
1063
1061
template <typename T>
1064
1062
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1065
1063
first_leading_one (T value) {
1064
+ // TODO : Implement a faster version not involving operator~.
1066
1065
return first_leading_zero (static_cast <T>(~value));
1067
1066
}
1068
1067
1069
1068
// Specialization of cpp::first_trailing_zero ('bit.h') for BigInt.
1070
1069
template <typename T>
1071
1070
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1072
1071
first_trailing_zero (T value) {
1072
+ // TODO : Implement a faster version not involving operator~.
1073
1073
return value == cpp::numeric_limits<T>::max ()
1074
1074
? 0
1075
1075
: countr_zero (static_cast <T>(~value)) + 1 ;
@@ -1086,19 +1086,20 @@ first_trailing_one(T value) {
1086
1086
template <typename T>
1087
1087
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1088
1088
count_ones (T value) {
1089
- // TODO : Implement a faster version.
1090
1089
int count = 0 ;
1091
- for (int i = 0 ; i != cpp::numeric_limits<T>::digits; ++i)
1092
- if ((value >> i) & 0x1 )
1093
- ++count;
1090
+ for (auto word : value.val )
1091
+ count += count_ones (word);
1094
1092
return count;
1095
1093
}
1096
1094
1097
1095
// Specialization of cpp::count_zeros ('bit.h') for BigInt.
1098
1096
template <typename T>
1099
1097
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_big_int_v<T>, int >
1100
1098
count_zeros (T value) {
1101
- return count_ones<T>(static_cast <T>(~value));
1099
+ int count = 0 ;
1100
+ for (auto word : value.val )
1101
+ count += count_zeros (word);
1102
+ return count;
1102
1103
}
1103
1104
1104
1105
} // namespace LIBC_NAMESPACE::cpp
0 commit comments