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
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_ul ----*- 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_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_zero_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UL_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_ull ---------------------===//
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_ull.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_ull,
17+
(unsigned long long 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_ull ---*- 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_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_zero_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_ULL_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_us ----------------------===//
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_us.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_us,
17+
(unsigned short 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_us ----*- 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_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_zero_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
4343
unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
4444
unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
4545
unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDFU; }
46+
unsigned stdc_first_leading_zero_uc(unsigned char) noexcept { return 0xEAU; }
47+
unsigned stdc_first_leading_zero_us(unsigned short) noexcept { return 0xEBU; }
48+
unsigned stdc_first_leading_zero_ui(unsigned) noexcept { return 0xECU; }
49+
unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
50+
unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
51+
return 0xEFU;
52+
}
4653
}
4754

4855
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -78,3 +85,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
7885
EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
7986
EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDFU);
8087
}
88+
89+
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
90+
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned char>(0U)), 0xEAU);
91+
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned short>(0U)), 0xEBU);
92+
EXPECT_EQ(stdc_first_leading_zero(0U), 0xECU);
93+
EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
94+
EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEFU);
95+
}

libc/test/src/__support/CPP/bit_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,11 @@ TEST(LlvmLibcBitTest, Rotr) {
206206
rotr<uint64_t>(0x12345678deadbeefULL, -19));
207207
}
208208

209+
TYPED_TEST(LlvmLibcBitTest, FirstLeadingZero, UnsignedTypes) {
210+
EXPECT_EQ(first_leading_zero<T>(cpp::numeric_limits<T>::max()), 0);
211+
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
212+
EXPECT_EQ(first_leading_zero<T>(~(T(1) << i)),
213+
cpp::numeric_limits<T>::digits - i);
214+
}
215+
209216
} // namespace LIBC_NAMESPACE::cpp

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(prefixes
55
leading_ones
66
trailing_zeros
77
trailing_ones
8+
first_leading_zero
89
)
910
set(suffixes c s i l ll)
1011
foreach(prefix IN LISTS prefixes)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Unittests for 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/__support/CPP/limits.h"
10+
#include "src/stdbit/stdc_first_leading_zero_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcFirstLeadingZeroUcTest, ALL) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_zero_uc(UCHAR_MAX), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcFirstLeadingZeroUcTest, ZeroHot) {
18+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_zero_uc(~(1U << i)),
20+
UCHAR_WIDTH - i);
21+
}

0 commit comments

Comments
 (0)