Skip to content

Commit de7fdc5

Browse files
authored
Revert "[libc][NFC] Remove __support/bit.h and use __support/CPP/bit.h instead" (#74444)
Reverts #73939 This broke libc-aarch64-ubuntu build bot https://lab.llvm.org/buildbot/#/builders/138/builds/56186
1 parent b140948 commit de7fdc5

File tree

31 files changed

+393
-107
lines changed

31 files changed

+393
-107
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 4 additions & 5 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
139138
libc.src.__support.CPP.limits
140139
libc.src.__support.CPP.optional
141140
libc.src.__support.FPUtil.dyadic_float
142141
libc.src.__support.FPUtil.fenv_impl
143142
libc.src.__support.FPUtil.fp_bits
144143
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
191192
.math_extras
192193
.number_pair
193194
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
203204
.integer_utils
204205
.math_extras
205206
.number_pair
206207
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,9 +231,8 @@ add_header_library(
231231
HDRS
232232
hash.h
233233
DEPENDS
234+
.bit
234235
.uint128
235-
libc.src.__support.CPP.bit
236-
libc.src.__support.CPP.limits
237236
libc.src.__support.macros.attributes
238237
)
239238

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ 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
4445
)
4546

4647
add_header_library(
@@ -145,6 +146,7 @@ add_header_library(
145146
libc.src.__support.common
146147
libc.src.__support.CPP.bit
147148
libc.src.__support.CPP.type_traits
149+
libc.src.__support.bit
148150
libc.src.__support.uint128
149151
)
150152

libc/src/__support/FPUtil/FPBits.h

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

1212
#include "src/__support/CPP/bit.h"
1313
#include "src/__support/CPP/type_traits.h"
14+
#include "src/__support/bit.h"
1415
#include "src/__support/common.h"
1516
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1617

@@ -221,7 +222,7 @@ template <typename T> struct FPBits {
221222
LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
222223
FPBits<T> result;
223224
// offset: +1 for sign, but -1 for implicit first bit
224-
int lz = cpp::countl_zero(number) - FloatProp::EXPONENT_WIDTH;
225+
int lz = unsafe_clz(number) - FloatProp::EXPONENT_WIDTH;
225226
number <<= lz;
226227
ep -= lz;
227228

libc/src/__support/FPUtil/Hypot.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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"
1920
#include "src/__support/common.h"
2021

2122
namespace LIBC_NAMESPACE {
@@ -27,7 +28,7 @@ template <typename T>
2728
LIBC_INLINE T find_leading_one(T mant, int &shift_length) {
2829
shift_length = 0;
2930
if (mant > 0) {
30-
shift_length = (sizeof(mant) * 8) - 1 - cpp::countl_zero(mant);
31+
shift_length = (sizeof(mant) * 8) - 1 - unsafe_clz(mant);
3132
}
3233
return T(1) << shift_length;
3334
}
@@ -36,13 +37,9 @@ LIBC_INLINE T find_leading_one(T mant, int &shift_length) {
3637

3738
template <typename T> struct DoubleLength;
3839

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

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

4744
template <> struct DoubleLength<uint64_t> {
4845
using Type = UInt128;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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
1415
libc.src.__support.uint128
1516
)
1617

@@ -20,13 +21,13 @@ add_header_library(
2021
FMA.h
2122
DEPENDS
2223
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
3031
libc.src.__support.uint128
3132
)
3233

@@ -36,12 +37,12 @@ add_header_library(
3637
FMod.h
3738
DEPENDS
3839
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
4647
libc.src.math.generic.math_utils
4748
)

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"
1312
#include "src/__support/CPP/type_traits.h"
1413
#include "src/__support/FPUtil/FEnvImpl.h"
1514
#include "src/__support/FPUtil/FPBits.h"
1615
#include "src/__support/FPUtil/FloatProperties.h"
1716
#include "src/__support/FPUtil/rounding_mode.h"
1817
#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 =
213-
prod_hi ? cpp::countl_zero(prod_hi)
214-
: 64 + cpp::countl_zero(static_cast<uint64_t>(prod_mant));
212+
int lead_zeros = prod_hi
213+
? unsafe_clz(prod_hi)
214+
: 64 + unsafe_clz(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"
1312
#include "src/__support/CPP/limits.h"
1413
#include "src/__support/CPP/type_traits.h"
1514
#include "src/__support/FPUtil/FEnvImpl.h"
1615
#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 = cpp::countl_zero(m_y);
267+
lead_zeros_m_y = unsafe_clz(m_y);
268268
}
269269

270270
// Assume hy != 0
271-
int tail_zeros_m_y = cpp::countr_zero(m_y);
271+
int tail_zeros_m_y = unsafe_ctz(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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
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" // countl_zero
13+
#include "src/__support/CPP/bit.h"
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"
1920
#include "src/__support/common.h"
2021

2122
namespace LIBC_NAMESPACE {
@@ -36,7 +37,7 @@ template <> struct SpecialLongDouble<long double> {
3637
template <typename T>
3738
LIBC_INLINE void normalize(int &exponent,
3839
typename FPBits<T>::UIntType &mantissa) {
39-
const int shift = cpp::countl_zero(mantissa) -
40+
const int shift = unsafe_clz(mantissa) -
4041
(8 * sizeof(mantissa) - 1 - MantissaWidth<T>::VALUE);
4142
exponent -= shift;
4243
mantissa <<= shift;
@@ -51,9 +52,9 @@ LIBC_INLINE void normalize<long double>(int &exponent, uint64_t &mantissa) {
5152
template <>
5253
LIBC_INLINE void normalize<long double>(int &exponent, UInt128 &mantissa) {
5354
const uint64_t hi_bits = static_cast<uint64_t>(mantissa >> 64);
54-
const int shift =
55-
hi_bits ? (cpp::countl_zero(hi_bits) - 15)
56-
: (cpp::countl_zero(static_cast<uint64_t>(mantissa)) + 49);
55+
const int shift = hi_bits
56+
? (unsafe_clz(hi_bits) - 15)
57+
: (unsafe_clz(static_cast<uint64_t>(mantissa)) + 49);
5758
exponent -= shift;
5859
mantissa <<= shift;
5960
}
@@ -136,7 +137,7 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> sqrt(T x) {
136137

137138
// We compute one more iteration in order to round correctly.
138139
bool lsb = static_cast<bool>(y & 1); // Least significant bit
139-
bool rb = false; // Round bit
140+
bool rb = false; // Round bit
140141
r <<= 2;
141142
UIntType tmp = (y << 2) + 1;
142143
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"
1312
#include "src/__support/FPUtil/FEnvImpl.h"
1413
#include "src/__support/FPUtil/FPBits.h"
1514
#include "src/__support/FPUtil/rounding_mode.h"
1615
#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-
cpp::countl_zero(static_cast<uint64_t>(mantissa)) -
25+
unsafe_clz(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.CPP.bit
9+
libc.src.__support.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.include.llvm-libc-types.ENTRY
29-
libc.src.__support.CPP.bit
30-
libc.src.__support.CPP.new
28+
libc.src.__support.memory_size
29+
libc.src.__support.bit
3130
libc.src.__support.CPP.type_traits
32-
libc.src.__support.hash
31+
libc.src.__support.CPP.new
3332
libc.src.__support.macros.attributes
3433
libc.src.__support.macros.optimization
35-
libc.src.__support.memory_size
34+
libc.src.__support.hash
3635
libc.src.string.memset
3736
libc.src.string.strcmp
3837
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/CPP/bit.h"
12+
#include "src/__support/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 cpp::countr_zero<T>(word) / WORD_STRIDE;
48+
return unsafe_ctz<T>(word) / WORD_STRIDE;
4949
}
5050
};
5151

libc/src/__support/HashTable/table.h

Lines changed: 3 additions & 4 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
1413
#include "src/__support/CPP/new.h"
1514
#include "src/__support/CPP/type_traits.h"
1615
#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 cpp::bit_ceil(cap * 8 / 7);
73+
return next_power_of_two(cap * 8 / 7);
7474
}
7575

7676
// The heap memory layout for N buckets HashTable is as follows:
@@ -98,8 +98,7 @@ 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 +
102-
SafeMemSize::offset_to(entries_size, table_alignment());
101+
return entries_size + offset_to(entries_size, table_alignment());
103102
}
104103

105104
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
1413
#include "src/__support/CPP/limits.h"
1514
#include "src/__support/CPP/optional.h"
1615
#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 += countl_zero(val[i - 1]);
560+
leading_zeroes += unsafe_clz(val[i - 1]);
561561
break;
562562
}
563563
}

0 commit comments

Comments
 (0)