Skip to content

Commit 221d267

Browse files
[libc][stdbit] implement stdc_first_leading_one (C23)
1 parent d2d6b36 commit 221d267

24 files changed

+405
-22
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ set(TARGET_LIBC_ENTRYPOINTS
117117
libc.src.stdbit.stdc_first_leading_zero_ui
118118
libc.src.stdbit.stdc_first_leading_zero_ul
119119
libc.src.stdbit.stdc_first_leading_zero_ull
120+
libc.src.stdbit.stdc_first_leading_one_uc
121+
libc.src.stdbit.stdc_first_leading_one_us
122+
libc.src.stdbit.stdc_first_leading_one_ui
123+
libc.src.stdbit.stdc_first_leading_one_ul
124+
libc.src.stdbit.stdc_first_leading_one_ull
120125

121126
# stdlib.h entrypoints
122127
libc.src.stdlib.abs

libc/docs/stdbit.rst

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ stdc_leading_ones_us |check|
4141
stdc_leading_ones_ui |check|
4242
stdc_leading_ones_ul |check|
4343
stdc_leading_ones_ull |check|
44-
stdc_trailing_zeros_uc
45-
stdc_trailing_zeros_us
46-
stdc_trailing_zeros_ui
47-
stdc_trailing_zeros_ul
48-
stdc_trailing_zeros_ull
49-
stdc_trailing_ones_uc
50-
stdc_trailing_ones_us
51-
stdc_trailing_ones_ui
52-
stdc_trailing_ones_ul
53-
stdc_trailing_ones_ull
54-
stdc_first_leading_zero_uc
55-
stdc_first_leading_zero_us
56-
stdc_first_leading_zero_ui
57-
stdc_first_leading_zero_ul
58-
stdc_first_leading_zero_ull
59-
stdc_first_leading_one_uc
60-
stdc_first_leading_one_us
61-
stdc_first_leading_one_ui
62-
stdc_first_leading_one_ul
63-
stdc_first_leading_one_ull
44+
stdc_trailing_zeros_uc |check|
45+
stdc_trailing_zeros_us |check|
46+
stdc_trailing_zeros_ui |check|
47+
stdc_trailing_zeros_ul |check|
48+
stdc_trailing_zeros_ull |check|
49+
stdc_trailing_ones_uc |check|
50+
stdc_trailing_ones_us |check|
51+
stdc_trailing_ones_ui |check|
52+
stdc_trailing_ones_ul |check|
53+
stdc_trailing_ones_ull |check|
54+
stdc_first_leading_zero_uc |check|
55+
stdc_first_leading_zero_us |check|
56+
stdc_first_leading_zero_ui |check|
57+
stdc_first_leading_zero_ul |check|
58+
stdc_first_leading_zero_ull |check|
59+
stdc_first_leading_one_uc |check|
60+
stdc_first_leading_one_us |check|
61+
stdc_first_leading_one_ui |check|
62+
stdc_first_leading_one_ul |check|
63+
stdc_first_leading_one_ull |check|
6464
stdc_first_trailing_zero_uc
6565
stdc_first_trailing_zero_us
6666
stdc_first_trailing_zero_ui

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ inline unsigned stdc_first_leading_zero(unsigned long x) {
8686
inline unsigned stdc_first_leading_zero(unsigned long long x) {
8787
return stdc_first_leading_zero_ull(x);
8888
}
89+
inline unsigned stdc_first_leading_one(unsigned char x) {
90+
return stdc_first_leading_one_uc(x);
91+
}
92+
inline unsigned stdc_first_leading_one(unsigned short x) {
93+
return stdc_first_leading_one_us(x);
94+
}
95+
inline unsigned stdc_first_leading_one(unsigned x) {
96+
return stdc_first_leading_one_ui(x);
97+
}
98+
inline unsigned stdc_first_leading_one(unsigned long x) {
99+
return stdc_first_leading_one_ul(x);
100+
}
101+
inline unsigned stdc_first_leading_one(unsigned long long x) {
102+
return stdc_first_leading_one_ull(x);
103+
}
89104
#else
90105
#define stdc_leading_zeros(x) \
91106
_Generic((x), \
@@ -122,6 +137,13 @@ inline unsigned stdc_first_leading_zero(unsigned long long x) {
122137
unsigned: stdc_first_leading_zero_ui, \
123138
unsigned long: stdc_first_leading_zero_ul, \
124139
unsigned long long: stdc_first_leading_zero_ull)(x)
140+
#define stdc_first_leading_one(x) \
141+
_Generic((x), \
142+
unsigned char: stdc_first_leading_one_uc, \
143+
unsigned short: stdc_first_leading_one_us, \
144+
unsigned: stdc_first_leading_one_ui, \
145+
unsigned long: stdc_first_leading_one_ul, \
146+
unsigned long long: stdc_first_leading_one_ull)(x)
125147
#endif // __cplusplus
126148

127149
#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
@@ -781,7 +781,8 @@ def StdC : StandardSpec<"stdc"> {
781781
Macro<"stdc_leading_ones">,
782782
Macro<"stdc_trailing_zeros">,
783783
Macro<"stdc_trailing_ones">,
784-
Macro<"stdc_first_leading_zero">
784+
Macro<"stdc_first_leading_zero">,
785+
Macro<"stdc_first_leading_one">
785786
], // Macros
786787
[], // Types
787788
[], // Enumerations
@@ -810,7 +811,12 @@ def StdC : StandardSpec<"stdc"> {
810811
FunctionSpec<"stdc_first_leading_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
811812
FunctionSpec<"stdc_first_leading_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
812813
FunctionSpec<"stdc_first_leading_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
813-
FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
814+
FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
815+
FunctionSpec<"stdc_first_leading_one_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
816+
FunctionSpec<"stdc_first_leading_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
817+
FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
818+
FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
819+
FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
814820
] // Functions
815821
>;
816822

libc/src/__support/CPP/bit.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,35 @@ SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
268268

269269
#undef SPECIALIZE_FLZ
270270

271+
#define SPECIALIZE_FLO(NAME, TYPE, BUILTIN) \
272+
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
273+
static_assert(cpp::is_unsigned_v<TYPE>); \
274+
return value == static_cast<TYPE>(0) \
275+
? 0 \
276+
: BUILTIN(static_cast<TYPE>(value)) + 1; \
277+
}
278+
279+
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
280+
[[nodiscard]] LIBC_INLINE constexpr int first_leading_one(T value) {
281+
return value == static_cast<T>(0) ? 0
282+
: countl_zero<T>(static_cast<T>(value)) + 1;
283+
}
284+
285+
#if LIBC_HAS_BUILTIN(__builtin_clzs)
286+
SPECIALIZE_FLO(first_leading_one, unsigned short, __builtin_clzs)
287+
#endif
288+
#if LIBC_HAS_BUILTIN(__builtin_clz)
289+
SPECIALIZE_FLO(first_leading_one, unsigned int, __builtin_clz)
290+
#endif
291+
#if LIBC_HAS_BUILTIN(__builtin_clzl)
292+
SPECIALIZE_FLO(first_leading_one, unsigned long, __builtin_clzl)
293+
#endif
294+
#if LIBC_HAS_BUILTIN(__builtin_clzll)
295+
SPECIALIZE_FLO(first_leading_one, unsigned long long, __builtin_clzll)
296+
#endif
297+
298+
#undef SPECIALIZE_FLO
299+
271300
} // namespace LIBC_NAMESPACE::cpp
272301

273302
#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
@@ -4,6 +4,7 @@ set(prefixes
44
trailing_zeros
55
trailing_ones
66
first_leading_zero
7+
first_leading_one
78
)
89
set(suffixes c s i l ll)
910
foreach(prefix IN LISTS prefixes)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_first_leading_one_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_one_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_one_uc, (unsigned char value)) {
17+
return static_cast<unsigned>(cpp::first_leading_one(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_one_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_ONE_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_one_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_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_one_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_one_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_one_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::first_leading_one(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_one_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_ONE_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_one_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UI_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_one_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_one_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_one_ul, (unsigned long value)) {
17+
return static_cast<unsigned>(cpp::first_leading_one(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_one_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_ONE_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_one_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_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_one_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_one_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_one_ull,
17+
(unsigned long long value)) {
18+
return static_cast<unsigned>(cpp::first_leading_one(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_one_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_ONE_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_one_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_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_one_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_one_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_one_us,
17+
(unsigned short value)) {
18+
return static_cast<unsigned>(cpp::first_leading_one(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_one_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_ONE_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_leading_one_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ONE_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
5050
unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
5151
return 0xEFU;
5252
}
53+
unsigned stdc_first_leading_one_uc(unsigned char) noexcept { return 0xFAU; }
54+
unsigned stdc_first_leading_one_us(unsigned short) noexcept { return 0xFBU; }
55+
unsigned stdc_first_leading_one_ui(unsigned) noexcept { return 0xFCU; }
56+
unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
57+
unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
58+
return 0xFFU;
59+
}
5360
}
5461

5562
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -93,3 +100,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
93100
EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
94101
EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEFU);
95102
}
103+
104+
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
105+
EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned char>(0U)), 0xFAU);
106+
EXPECT_EQ(stdc_first_leading_one(static_cast<unsigned short>(0U)), 0xFBU);
107+
EXPECT_EQ(stdc_first_leading_one(0U), 0xFCU);
108+
EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
109+
EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFFU);
110+
}

0 commit comments

Comments
 (0)