Skip to content

Commit d2d6b36

Browse files
[libc][stdbit] implement stdc_first_leading_zero (C23) (#81340)
1 parent ab70251 commit d2d6b36

23 files changed

+388
-2
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ set(TARGET_LIBC_ENTRYPOINTS
112112
libc.src.stdbit.stdc_trailing_ones_ui
113113
libc.src.stdbit.stdc_trailing_ones_ul
114114
libc.src.stdbit.stdc_trailing_ones_ull
115+
libc.src.stdbit.stdc_first_leading_zero_uc
116+
libc.src.stdbit.stdc_first_leading_zero_us
117+
libc.src.stdbit.stdc_first_leading_zero_ui
118+
libc.src.stdbit.stdc_first_leading_zero_ul
119+
libc.src.stdbit.stdc_first_leading_zero_ull
115120

116121
# stdlib.h entrypoints
117122
libc.src.stdlib.abs

libc/include/llvm-libc-macros/stdbit-macros.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ inline unsigned stdc_trailing_ones(unsigned long x) {
7171
inline unsigned stdc_trailing_ones(unsigned long long x) {
7272
return stdc_trailing_ones_ull(x);
7373
}
74+
inline unsigned stdc_first_leading_zero(unsigned char x) {
75+
return stdc_first_leading_zero_uc(x);
76+
}
77+
inline unsigned stdc_first_leading_zero(unsigned short x) {
78+
return stdc_first_leading_zero_us(x);
79+
}
80+
inline unsigned stdc_first_leading_zero(unsigned x) {
81+
return stdc_first_leading_zero_ui(x);
82+
}
83+
inline unsigned stdc_first_leading_zero(unsigned long x) {
84+
return stdc_first_leading_zero_ul(x);
85+
}
86+
inline unsigned stdc_first_leading_zero(unsigned long long x) {
87+
return stdc_first_leading_zero_ull(x);
88+
}
7489
#else
7590
#define stdc_leading_zeros(x) \
7691
_Generic((x), \
@@ -100,6 +115,13 @@ inline unsigned stdc_trailing_ones(unsigned long long x) {
100115
unsigned: stdc_trailing_ones_ui, \
101116
unsigned long: stdc_trailing_ones_ul, \
102117
unsigned long long: stdc_trailing_ones_ull)(x)
118+
#define stdc_first_leading_zero(x) \
119+
_Generic((x), \
120+
unsigned char: stdc_first_leading_zero_uc, \
121+
unsigned short: stdc_first_leading_zero_us, \
122+
unsigned: stdc_first_leading_zero_ui, \
123+
unsigned long: stdc_first_leading_zero_ul, \
124+
unsigned long long: stdc_first_leading_zero_ull)(x)
103125
#endif // __cplusplus
104126

105127
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

libc/spec/stdc.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,8 @@ def StdC : StandardSpec<"stdc"> {
780780
Macro<"stdc_leading_zeros">,
781781
Macro<"stdc_leading_ones">,
782782
Macro<"stdc_trailing_zeros">,
783-
Macro<"stdc_trailing_ones">
783+
Macro<"stdc_trailing_ones">,
784+
Macro<"stdc_first_leading_zero">
784785
], // Macros
785786
[], // Types
786787
[], // Enumerations
@@ -804,7 +805,12 @@ def StdC : StandardSpec<"stdc"> {
804805
FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
805806
FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
806807
FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
807-
FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
808+
FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
809+
FunctionSpec<"stdc_first_leading_zero_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
810+
FunctionSpec<"stdc_first_leading_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
811+
FunctionSpec<"stdc_first_leading_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
812+
FunctionSpec<"stdc_first_leading_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
813+
FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
808814
] // Functions
809815
>;
810816

libc/src/__support/CPP/bit.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,36 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
238238
}
239239
}
240240

241+
#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN) \
242+
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
243+
static_assert(cpp::is_unsigned_v<TYPE>); \
244+
return value == cpp::numeric_limits<TYPE>::max() \
245+
? 0 \
246+
: BUILTIN(static_cast<TYPE>(~value)) + 1; \
247+
}
248+
249+
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
250+
[[nodiscard]] LIBC_INLINE constexpr int first_leading_zero(T value) {
251+
return value == cpp::numeric_limits<T>::max()
252+
? 0
253+
: countl_zero(static_cast<T>(~value)) + 1;
254+
}
255+
256+
#if LIBC_HAS_BUILTIN(__builtin_clzs)
257+
SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
258+
#endif
259+
#if LIBC_HAS_BUILTIN(__builtin_clz)
260+
SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
261+
#endif
262+
#if LIBC_HAS_BUILTIN(__builtin_clzl)
263+
SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
264+
#endif
265+
#if LIBC_HAS_BUILTIN(__builtin_clzll)
266+
SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
267+
#endif
268+
269+
#undef SPECIALIZE_FLZ
270+
241271
} // namespace LIBC_NAMESPACE::cpp
242272

243273
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H

libc/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(prefixes
33
leading_ones
44
trailing_zeros
55
trailing_ones
6+
first_leading_zero
67
)
78
set(suffixes c s i l ll)
89
foreach(prefix IN LISTS prefixes)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of stdc_first_leading_zero_uc ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_first_leading_zero_uc.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_uc,
17+
(unsigned char value)) {
18+
return static_cast<unsigned>(cpp::first_leading_zero(value));
19+
}
20+
21+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_first_leading_zero_uc ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_zero_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UC_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_first_leading_zero_ui ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_first_leading_zero_ui.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::first_leading_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_first_leading_zero_ui ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_zero_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UI_H
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of stdc_first_leading_zero_ul ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_first_leading_zero_ul.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ul,
17+
(unsigned long value)) {
18+
return static_cast<unsigned>(cpp::first_leading_zero(value));
19+
}
20+
21+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)