Skip to content

Commit 1d89478

Browse files
authored
[reland][libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead (#73939) (#74446)
Same as #73939 but also fix `libc/src/string/memory_utils/op_aarch64.h` that was still using `deferred_static_assert`.
1 parent de7fdc5 commit 1d89478

File tree

32 files changed

+110
-395
lines changed

32 files changed

+110
-395
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ add_header_library(
135135
.str_to_num_result
136136
.uint128
137137
libc.src.__support.common
138+
libc.src.__support.CPP.bit
138139
libc.src.__support.CPP.limits
139140
libc.src.__support.CPP.optional
140141
libc.src.__support.FPUtil.dyadic_float
141142
libc.src.__support.FPUtil.fenv_impl
142143
libc.src.__support.FPUtil.fp_bits
143144
libc.src.__support.FPUtil.rounding_mode
144-
libc.src.__support.bit
145145
libc.src.errno.errno
146146
)
147147

@@ -188,10 +188,10 @@ add_header_library(
188188
HDRS
189189
integer_utils.h
190190
DEPENDS
191-
.bit
192191
.math_extras
193192
.number_pair
194193
libc.src.__support.common
194+
libc.src.__support.CPP.bit
195195
libc.src.__support.CPP.type_traits
196196
)
197197

@@ -200,11 +200,11 @@ add_header_library(
200200
HDRS
201201
UInt.h
202202
DEPENDS
203-
.bit
204203
.integer_utils
205204
.math_extras
206205
.number_pair
207206
libc.src.__support.CPP.array
207+
libc.src.__support.CPP.bit
208208
libc.src.__support.CPP.type_traits
209209
libc.src.__support.macros.optimization
210210
)
@@ -231,8 +231,9 @@ add_header_library(
231231
HDRS
232232
hash.h
233233
DEPENDS
234-
.bit
235234
.uint128
235+
libc.src.__support.CPP.bit
236+
libc.src.__support.CPP.limits
236237
libc.src.__support.macros.attributes
237238
)
238239

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ add_header_library(
4141
libc.src.__support.common
4242
libc.src.__support.CPP.bit
4343
libc.src.__support.CPP.type_traits
44-
libc.src.__support.bit
4544
)
4645

4746
add_header_library(
@@ -146,7 +145,6 @@ add_header_library(
146145
libc.src.__support.common
147146
libc.src.__support.CPP.bit
148147
libc.src.__support.CPP.type_traits
149-
libc.src.__support.bit
150148
libc.src.__support.uint128
151149
)
152150

libc/src/__support/FPUtil/FPBits.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/CPP/type_traits.h"
14-
#include "src/__support/bit.h"
1514
#include "src/__support/common.h"
1615
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1716

@@ -222,7 +221,7 @@ template <typename T> struct FPBits {
222221
LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
223222
FPBits<T> result;
224223
// offset: +1 for sign, but -1 for implicit first bit
225-
int lz = unsafe_clz(number) - FloatProp::EXPONENT_WIDTH;
224+
int lz = cpp::countl_zero(number) - FloatProp::EXPONENT_WIDTH;
226225
number <<= lz;
227226
ep -= lz;
228227

libc/src/__support/FPUtil/Hypot.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "src/__support/CPP/bit.h"
1717
#include "src/__support/CPP/type_traits.h"
1818
#include "src/__support/UInt128.h"
19-
#include "src/__support/bit.h"
2019
#include "src/__support/common.h"
2120

2221
namespace LIBC_NAMESPACE {
@@ -28,7 +27,7 @@ template <typename T>
2827
LIBC_INLINE T find_leading_one(T mant, int &shift_length) {
2928
shift_length = 0;
3029
if (mant > 0) {
31-
shift_length = (sizeof(mant) * 8) - 1 - unsafe_clz(mant);
30+
shift_length = (sizeof(mant) * 8) - 1 - cpp::countl_zero(mant);
3231
}
3332
return T(1) << shift_length;
3433
}
@@ -37,9 +36,13 @@ LIBC_INLINE T find_leading_one(T mant, int &shift_length) {
3736

3837
template <typename T> struct DoubleLength;
3938

40-
template <> struct DoubleLength<uint16_t> { using Type = uint32_t; };
39+
template <> struct DoubleLength<uint16_t> {
40+
using Type = uint32_t;
41+
};
4142

42-
template <> struct DoubleLength<uint32_t> { using Type = uint64_t; };
43+
template <> struct DoubleLength<uint32_t> {
44+
using Type = uint64_t;
45+
};
4346

4447
template <> struct DoubleLength<uint64_t> {
4548
using Type = UInt128;

libc/src/__support/FPUtil/generic/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ add_header_library(
1111
libc.src.__support.FPUtil.fenv_impl
1212
libc.src.__support.FPUtil.fp_bits
1313
libc.src.__support.FPUtil.rounding_mode
14-
libc.src.__support.bit
1514
libc.src.__support.uint128
1615
)
1716

@@ -21,13 +20,13 @@ add_header_library(
2120
FMA.h
2221
DEPENDS
2322
libc.src.__support.common
23+
libc.src.__support.CPP.bit
2424
libc.src.__support.CPP.type_traits
2525
libc.src.__support.FPUtil.fenv_impl
2626
libc.src.__support.FPUtil.float_properties
2727
libc.src.__support.FPUtil.fp_bits
2828
libc.src.__support.FPUtil.rounding_mode
2929
libc.src.__support.macros.optimization
30-
libc.src.__support.bit
3130
libc.src.__support.uint128
3231
)
3332

@@ -37,12 +36,12 @@ add_header_library(
3736
FMod.h
3837
DEPENDS
3938
libc.src.__support.common
39+
libc.src.__support.CPP.bit
4040
libc.src.__support.CPP.type_traits
4141
libc.src.__support.FPUtil.fenv_impl
4242
libc.src.__support.FPUtil.float_properties
4343
libc.src.__support.FPUtil.fp_bits
4444
libc.src.__support.FPUtil.rounding_mode
4545
libc.src.__support.macros.optimization
46-
libc.src.__support.bit
4746
libc.src.math.generic.math_utils
4847
)

libc/src/__support/FPUtil/generic/FMA.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMA_H
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMA_H
1111

12+
#include "src/__support/CPP/bit.h"
1213
#include "src/__support/CPP/type_traits.h"
1314
#include "src/__support/FPUtil/FEnvImpl.h"
1415
#include "src/__support/FPUtil/FPBits.h"
1516
#include "src/__support/FPUtil/FloatProperties.h"
1617
#include "src/__support/FPUtil/rounding_mode.h"
1718
#include "src/__support/UInt128.h"
18-
#include "src/__support/bit.h"
1919
#include "src/__support/macros/attributes.h" // LIBC_INLINE
2020
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
2121

@@ -209,9 +209,9 @@ template <> LIBC_INLINE double fma<double>(double x, double y, double z) {
209209
// Normalize the result.
210210
if (prod_mant != 0) {
211211
uint64_t prod_hi = static_cast<uint64_t>(prod_mant >> 64);
212-
int lead_zeros = prod_hi
213-
? unsafe_clz(prod_hi)
214-
: 64 + unsafe_clz(static_cast<uint64_t>(prod_mant));
212+
int lead_zeros =
213+
prod_hi ? cpp::countl_zero(prod_hi)
214+
: 64 + cpp::countl_zero(static_cast<uint64_t>(prod_mant));
215215
// Move the leading 1 to the most significant bit.
216216
prod_mant <<= lead_zeros;
217217
// The lower 64 bits are always sticky bits after moving the leading 1 to

libc/src/__support/FPUtil/generic/FMod.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMOD_H
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_FMOD_H
1111

12+
#include "src/__support/CPP/bit.h"
1213
#include "src/__support/CPP/limits.h"
1314
#include "src/__support/CPP/type_traits.h"
1415
#include "src/__support/FPUtil/FEnvImpl.h"
1516
#include "src/__support/FPUtil/FPBits.h"
16-
#include "src/__support/bit.h"
1717
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
1818
#include "src/math/generic/math_utils.h"
1919

@@ -229,7 +229,7 @@ class FMod {
229229

230230
if (LIBC_LIKELY(sx.uintval() <= sy.uintval())) {
231231
if (sx.uintval() < sy.uintval())
232-
return sx; // |x|<|y| return x
232+
return sx; // |x|<|y| return x
233233
return FPB(FPB::zero()); // |x|=|y| return 0.0
234234
}
235235

@@ -264,11 +264,11 @@ class FMod {
264264
e_y--;
265265
} else {
266266
m_y = sy.get_mantissa();
267-
lead_zeros_m_y = unsafe_clz(m_y);
267+
lead_zeros_m_y = cpp::countl_zero(m_y);
268268
}
269269

270270
// Assume hy != 0
271-
int tail_zeros_m_y = unsafe_ctz(m_y);
271+
int tail_zeros_m_y = cpp::countr_zero(m_y);
272272
int sides_zeroes_count = lead_zeros_m_y + tail_zeros_m_y;
273273
// n > 0 by conditions above
274274
int exp_diff = e_x - e_y;

libc/src/__support/FPUtil/generic/sqrt.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_H
1111

1212
#include "sqrt_80_bit_long_double.h"
13-
#include "src/__support/CPP/bit.h"
13+
#include "src/__support/CPP/bit.h" // countl_zero
1414
#include "src/__support/CPP/type_traits.h"
1515
#include "src/__support/FPUtil/FEnvImpl.h"
1616
#include "src/__support/FPUtil/FPBits.h"
1717
#include "src/__support/FPUtil/rounding_mode.h"
1818
#include "src/__support/UInt128.h"
19-
#include "src/__support/bit.h"
2019
#include "src/__support/common.h"
2120

2221
namespace LIBC_NAMESPACE {
@@ -37,7 +36,7 @@ template <> struct SpecialLongDouble<long double> {
3736
template <typename T>
3837
LIBC_INLINE void normalize(int &exponent,
3938
typename FPBits<T>::UIntType &mantissa) {
40-
const int shift = unsafe_clz(mantissa) -
39+
const int shift = cpp::countl_zero(mantissa) -
4140
(8 * sizeof(mantissa) - 1 - MantissaWidth<T>::VALUE);
4241
exponent -= shift;
4342
mantissa <<= shift;
@@ -52,9 +51,9 @@ LIBC_INLINE void normalize<long double>(int &exponent, uint64_t &mantissa) {
5251
template <>
5352
LIBC_INLINE void normalize<long double>(int &exponent, UInt128 &mantissa) {
5453
const uint64_t hi_bits = static_cast<uint64_t>(mantissa >> 64);
55-
const int shift = hi_bits
56-
? (unsafe_clz(hi_bits) - 15)
57-
: (unsafe_clz(static_cast<uint64_t>(mantissa)) + 49);
54+
const int shift =
55+
hi_bits ? (cpp::countl_zero(hi_bits) - 15)
56+
: (cpp::countl_zero(static_cast<uint64_t>(mantissa)) + 49);
5857
exponent -= shift;
5958
mantissa <<= shift;
6059
}
@@ -137,7 +136,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
137136

138137
// We compute one more iteration in order to round correctly.
139138
bool lsb = static_cast<bool>(y & 1); // Least significant bit
140-
bool rb = false; // Round bit
139+
bool rb = false; // Round bit
141140
r <<= 2;
142141
UIntType tmp = (y << 2) + 1;
143142
if (r >= tmp) {

libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_SQRT_80_BIT_LONG_DOUBLE_H
1111

12+
#include "src/__support/CPP/bit.h"
1213
#include "src/__support/FPUtil/FEnvImpl.h"
1314
#include "src/__support/FPUtil/FPBits.h"
1415
#include "src/__support/FPUtil/rounding_mode.h"
1516
#include "src/__support/UInt128.h"
16-
#include "src/__support/bit.h"
1717
#include "src/__support/common.h"
1818

1919
namespace LIBC_NAMESPACE {
@@ -22,7 +22,7 @@ namespace x86 {
2222

2323
LIBC_INLINE void normalize(int &exponent, UInt128 &mantissa) {
2424
const unsigned int shift = static_cast<unsigned int>(
25-
unsafe_clz(static_cast<uint64_t>(mantissa)) -
25+
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
2626
(8 * sizeof(uint64_t) - 1 - MantissaWidth<long double>::VALUE));
2727
exponent -= shift;
2828
mantissa <<= shift;
@@ -101,7 +101,7 @@ LIBC_INLINE long double sqrt(long double x) {
101101

102102
// We compute one more iteration in order to round correctly.
103103
bool lsb = static_cast<bool>(y & 1); // Least significant bit
104-
bool rb = false; // Round bit
104+
bool rb = false; // Round bit
105105
r <<= 2;
106106
UIntType tmp = (y << 2) + 1;
107107
if (r >= tmp) {

libc/src/__support/HashTable/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_header_library(
66
PREFER_GENERIC
77
DEPENDS
88
libc.src.__support.common
9-
libc.src.__support.bit
9+
libc.src.__support.CPP.bit
1010
libc.src.__support.macros.properties.cpu_features
1111
)
1212

@@ -25,17 +25,17 @@ add_header_library(
2525
table.h
2626
DEPENDS
2727
.bitmask
28-
libc.src.__support.memory_size
29-
libc.src.__support.bit
30-
libc.src.__support.CPP.type_traits
28+
libc.include.llvm-libc-types.ENTRY
29+
libc.src.__support.CPP.bit
3130
libc.src.__support.CPP.new
31+
libc.src.__support.CPP.type_traits
32+
libc.src.__support.hash
3233
libc.src.__support.macros.attributes
3334
libc.src.__support.macros.optimization
34-
libc.src.__support.hash
35+
libc.src.__support.memory_size
3536
libc.src.string.memset
3637
libc.src.string.strcmp
3738
libc.src.string.strlen
38-
libc.include.llvm-libc-types.ENTRY
3939
)
4040

4141
add_header_library(

libc/src/__support/HashTable/bitmask.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
1010
#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
1111

12-
#include "src/__support/bit.h"
12+
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/macros/properties/cpu_features.h"
1414
#include <stddef.h> // size_t
1515
#include <stdint.h> // uint8_t, uint64_t
@@ -45,7 +45,7 @@ template <typename T, T WORD_MASK, size_t WORD_STRIDE> struct BitMaskAdaptor {
4545
// Count trailing zeros with respect to stride. (Assume the bitmask is none
4646
// zero.)
4747
LIBC_INLINE constexpr size_t lowest_set_bit_nonzero() const {
48-
return unsafe_ctz<T>(word) / WORD_STRIDE;
48+
return cpp::countr_zero<T>(word) / WORD_STRIDE;
4949
}
5050
};
5151

libc/src/__support/HashTable/table.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_table_H
1111

1212
#include "include/llvm-libc-types/ENTRY.h"
13+
#include "src/__support/CPP/bit.h" // bit_ceil
1314
#include "src/__support/CPP/new.h"
1415
#include "src/__support/CPP/type_traits.h"
1516
#include "src/__support/HashTable/bitmask.h"
16-
#include "src/__support/bit.h"
1717
#include "src/__support/hash.h"
1818
#include "src/__support/macros/attributes.h"
1919
#include "src/__support/macros/optimization.h"
@@ -70,7 +70,7 @@ LIBC_INLINE size_t capacity_to_entries(size_t cap) {
7070
if (cap < sizeof(Group))
7171
cap = sizeof(Group);
7272
// overflow is always checked in allocate()
73-
return next_power_of_two(cap * 8 / 7);
73+
return cpp::bit_ceil(cap * 8 / 7);
7474
}
7575

7676
// The heap memory layout for N buckets HashTable is as follows:
@@ -98,7 +98,8 @@ struct HashTable {
9898

9999
LIBC_INLINE size_t offset_from_entries() const {
100100
size_t entries_size = num_of_entries() * sizeof(ENTRY);
101-
return entries_size + offset_to(entries_size, table_alignment());
101+
return entries_size +
102+
SafeMemSize::offset_to(entries_size, table_alignment());
102103
}
103104

104105
LIBC_INLINE constexpr static size_t table_alignment() {

libc/src/__support/UInt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_UINT_H
1111

1212
#include "src/__support/CPP/array.h"
13+
#include "src/__support/CPP/bit.h" // countl_zero
1314
#include "src/__support/CPP/limits.h"
1415
#include "src/__support/CPP/optional.h"
1516
#include "src/__support/CPP/type_traits.h"
16-
#include "src/__support/bit.h" // unsafe_clz
1717
#include "src/__support/integer_utils.h"
1818
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1919
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
@@ -557,7 +557,7 @@ template <size_t Bits, bool Signed> struct BigInt {
557557
if (val[i - 1] == 0) {
558558
leading_zeroes += sizeof(uint64_t) * 8;
559559
} else {
560-
leading_zeroes += unsafe_clz(val[i - 1]);
560+
leading_zeroes += countl_zero(val[i - 1]);
561561
break;
562562
}
563563
}

0 commit comments

Comments
 (0)