Skip to content

Commit 0ebf511

Browse files
[libc] move non <bit> functions to math_extras (llvm#84818)
As per TODOs added in llvm@48b0bc8.
1 parent 261e564 commit 0ebf511

31 files changed

+154
-139
lines changed

libc/src/__support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_header_library(
3434
HDRS
3535
math_extras.h
3636
DEPENDS
37+
libc.src.__support.CPP.bit
3738
libc.src.__support.CPP.limits
3839
libc.src.__support.CPP.type_traits
3940
libc.src.__support.macros.attributes

libc/src/__support/CPP/bit.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -239,36 +239,6 @@ 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.
243-
template <typename T>
244-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
245-
first_leading_zero(T value) {
246-
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
247-
}
248-
249-
// TODO: remove from 'bit.h' as it is not a standard function.
250-
template <typename T>
251-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
252-
first_leading_one(T value) {
253-
return first_leading_zero(static_cast<T>(~value));
254-
}
255-
256-
// TODO: remove from 'bit.h' as it is not a standard function.
257-
template <typename T>
258-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
259-
first_trailing_zero(T value) {
260-
return value == cpp::numeric_limits<T>::max()
261-
? 0
262-
: countr_zero(static_cast<T>(~value)) + 1;
263-
}
264-
265-
// TODO: remove from 'bit.h' as it is not a standard function.
266-
template <typename T>
267-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
268-
first_trailing_one(T value) {
269-
return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
270-
}
271-
272242
/// Count number of 1's aka population count or Hamming weight.
273243
///
274244
/// Only unsigned integral types are allowed.
@@ -294,13 +264,6 @@ ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
294264
// TODO: 128b specializations?
295265
#undef ADD_SPECIALIZATION
296266

297-
// TODO: remove from 'bit.h' as it is not a standard function.
298-
template <typename T>
299-
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
300-
count_zeros(T value) {
301-
return popcount<T>(static_cast<T>(~value));
302-
}
303-
304267
} // namespace LIBC_NAMESPACE::cpp
305268

306269
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H

libc/src/__support/math_extras.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
1111
#define LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H
1212

13-
#include "src/__support/CPP/limits.h" // CHAR_BIT
13+
#include "src/__support/CPP/bit.h" // countl_one, countr_zero
14+
#include "src/__support/CPP/limits.h" // CHAR_BIT, numeric_limits
1415
#include "src/__support/CPP/type_traits.h" // is_unsigned_v
1516
#include "src/__support/macros/attributes.h" // LIBC_INLINE
1617
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
@@ -226,6 +227,40 @@ sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
226227

227228
#endif // LIBC_HAS_BUILTIN(__builtin_subc)
228229

230+
template <typename T>
231+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
232+
first_leading_zero(T value) {
233+
return value == cpp::numeric_limits<T>::max() ? 0
234+
: cpp::countl_one(value) + 1;
235+
}
236+
237+
template <typename T>
238+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
239+
first_leading_one(T value) {
240+
return first_leading_zero(static_cast<T>(~value));
241+
}
242+
243+
template <typename T>
244+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
245+
first_trailing_zero(T value) {
246+
return value == cpp::numeric_limits<T>::max()
247+
? 0
248+
: cpp::countr_zero(static_cast<T>(~value)) + 1;
249+
}
250+
251+
template <typename T>
252+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
253+
first_trailing_one(T value) {
254+
return value == cpp::numeric_limits<T>::max() ? 0
255+
: cpp::countr_zero(value) + 1;
256+
}
257+
258+
template <typename T>
259+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
260+
count_zeros(T value) {
261+
return cpp::popcount<T>(static_cast<T>(~value));
262+
}
263+
229264
} // namespace LIBC_NAMESPACE
230265

231266
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_EXTRAS_H

libc/src/stdbit/CMakeLists.txt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1+
function(declare_dependencies prefixes dependencies)
2+
set(suffixes c s i l ll)
3+
foreach(prefix ${prefixes})
4+
foreach(suffix IN LISTS suffixes)
5+
add_entrypoint_object(
6+
stdc_${prefix}_u${suffix}
7+
SRCS
8+
stdc_${prefix}_u${suffix}.cpp
9+
HDRS
10+
stdc_${prefix}_u${suffix}.h
11+
DEPENDS
12+
${dependencies}
13+
)
14+
endforeach()
15+
endforeach()
16+
endfunction()
17+
18+
119
set(prefixes
220
leading_zeros
321
leading_ones
422
trailing_zeros
523
trailing_ones
6-
first_leading_zero
7-
first_leading_one
8-
first_trailing_zero
9-
first_trailing_one
10-
count_zeros
1124
count_ones
1225
has_single_bit
1326
bit_width
1427
bit_floor
1528
bit_ceil
1629
)
17-
set(suffixes c s i l ll)
18-
foreach(prefix IN LISTS prefixes)
19-
foreach(suffix IN LISTS suffixes)
20-
add_entrypoint_object(
21-
stdc_${prefix}_u${suffix}
22-
SRCS
23-
stdc_${prefix}_u${suffix}.cpp
24-
HDRS
25-
stdc_${prefix}_u${suffix}.h
26-
DEPENDS
27-
libc.src.__support.CPP.bit
28-
)
29-
endforeach()
30-
endforeach()
30+
declare_dependencies("${prefixes}" libc.src.__support.CPP.bit)
31+
set(prefixes
32+
first_leading_zero
33+
first_leading_one
34+
first_trailing_zero
35+
first_trailing_one
36+
count_zeros
37+
)
38+
declare_dependencies("${prefixes}" libc.src.__support.math_extras)

libc/src/stdbit/stdc_count_zeros_uc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_count_zeros_uc.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_uc, (unsigned char value)) {
17-
return static_cast<unsigned>(cpp::count_zeros(value));
17+
return static_cast<unsigned>(count_zeros(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_zeros_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_count_zeros_ui.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_ui, (unsigned value)) {
17-
return static_cast<unsigned>(cpp::count_zeros(value));
17+
return static_cast<unsigned>(count_zeros(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_zeros_ul.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_count_zeros_ul.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_ul, (unsigned long value)) {
17-
return static_cast<unsigned>(cpp::count_zeros(value));
17+
return static_cast<unsigned>(count_zeros(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_zeros_ull.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_count_zeros_ull.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_ull, (unsigned long long value)) {
17-
return static_cast<unsigned>(cpp::count_zeros(value));
17+
return static_cast<unsigned>(count_zeros(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_count_zeros_us.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_count_zeros_us.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_us, (unsigned short value)) {
17-
return static_cast<unsigned>(cpp::count_zeros(value));
17+
return static_cast<unsigned>(count_zeros(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_one_uc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_first_leading_one_uc.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_uc, (unsigned char value)) {
17-
return static_cast<unsigned>(cpp::first_leading_one(value));
17+
return static_cast<unsigned>(first_leading_one(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_one_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_first_leading_one_ui.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ui, (unsigned value)) {
17-
return static_cast<unsigned>(cpp::first_leading_one(value));
17+
return static_cast<unsigned>(first_leading_one(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_one_ul.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_first_leading_one_ul.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ul, (unsigned long value)) {
17-
return static_cast<unsigned>(cpp::first_leading_one(value));
17+
return static_cast<unsigned>(first_leading_one(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_one_ull.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#include "src/stdbit/stdc_first_leading_one_ull.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_ull,
1717
(unsigned long long value)) {
18-
return static_cast<unsigned>(cpp::first_leading_one(value));
18+
return static_cast<unsigned>(first_leading_one(value));
1919
}
2020

2121
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_one_us.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#include "src/stdbit/stdc_first_leading_one_us.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_one_us,
1717
(unsigned short value)) {
18-
return static_cast<unsigned>(cpp::first_leading_one(value));
18+
return static_cast<unsigned>(first_leading_one(value));
1919
}
2020

2121
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_zero_uc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#include "src/stdbit/stdc_first_leading_zero_uc.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_uc,
1717
(unsigned char value)) {
18-
return static_cast<unsigned>(cpp::first_leading_zero(value));
18+
return static_cast<unsigned>(first_leading_zero(value));
1919
}
2020

2121
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_zero_ui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
#include "src/stdbit/stdc_first_leading_zero_ui.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ui, (unsigned value)) {
17-
return static_cast<unsigned>(cpp::first_leading_zero(value));
17+
return static_cast<unsigned>(first_leading_zero(value));
1818
}
1919

2020
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_zero_ul.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#include "src/stdbit/stdc_first_leading_zero_ul.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ul,
1717
(unsigned long value)) {
18-
return static_cast<unsigned>(cpp::first_leading_zero(value));
18+
return static_cast<unsigned>(first_leading_zero(value));
1919
}
2020

2121
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_first_leading_zero_ull.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
#include "src/stdbit/stdc_first_leading_zero_ull.h"
1010

11-
#include "src/__support/CPP/bit.h"
1211
#include "src/__support/common.h"
12+
#include "src/__support/math_extras.h"
1313

1414
namespace LIBC_NAMESPACE {
1515

1616
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ull,
1717
(unsigned long long value)) {
18-
return static_cast<unsigned>(cpp::first_leading_zero(value));
18+
return static_cast<unsigned>(first_leading_zero(value));
1919
}
2020

2121
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)