Skip to content

Commit 995f36d

Browse files
[libc][stdbit] implement stdc_trailing_zeros (C23)
1 parent edbd93d commit 995f36d

20 files changed

+440
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ set(TARGET_LIBC_ENTRYPOINTS
102102
libc.src.stdbit.stdc_leading_ones_ui
103103
libc.src.stdbit.stdc_leading_ones_ul
104104
libc.src.stdbit.stdc_leading_ones_ull
105+
libc.src.stdbit.stdc_trailing_zeros_uc
106+
libc.src.stdbit.stdc_trailing_zeros_us
107+
libc.src.stdbit.stdc_trailing_zeros_ui
108+
libc.src.stdbit.stdc_trailing_zeros_ul
109+
libc.src.stdbit.stdc_trailing_zeros_ull
105110

106111
# stdlib.h entrypoints
107112
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
@@ -40,6 +40,21 @@ inline unsigned stdc_leading_ones(unsigned long x) {
4040
inline unsigned stdc_leading_ones(unsigned long long x) {
4141
return stdc_leading_ones_ull(x);
4242
}
43+
inline unsigned stdc_trailing_zeros(unsigned char x) {
44+
return stdc_trailing_zeros_uc(x);
45+
}
46+
inline unsigned stdc_trailing_zeros(unsigned short x) {
47+
return stdc_trailing_zeros_us(x);
48+
}
49+
inline unsigned stdc_trailing_zeros(unsigned x) {
50+
return stdc_trailing_zeros_ui(x);
51+
}
52+
inline unsigned stdc_trailing_zeros(unsigned long x) {
53+
return stdc_trailing_zeros_ul(x);
54+
}
55+
inline unsigned stdc_trailing_zeros(unsigned long long x) {
56+
return stdc_trailing_zeros_ull(x);
57+
}
4358
#else
4459
#define stdc_leading_zeros(x) \
4560
_Generic((x), \
@@ -55,6 +70,13 @@ inline unsigned stdc_leading_ones(unsigned long long x) {
5570
unsigned: stdc_leading_ones_ui, \
5671
unsigned long: stdc_leading_ones_ul, \
5772
unsigned long long: stdc_leading_ones_ull)(x)
73+
#define stdc_trailing_zeros(x) \
74+
_Generic((x), \
75+
unsigned char: stdc_trailing_zeros_uc, \
76+
unsigned short: stdc_trailing_zeros_us, \
77+
unsigned: stdc_trailing_zeros_ui, \
78+
unsigned long: stdc_trailing_zeros_ul, \
79+
unsigned long long: stdc_trailing_zeros_ull)(x)
5880
#endif // __cplusplus
5981

6082
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

libc/src/stdbit/CMakeLists.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,53 @@ add_entrypoint_object(
9797
DEPENDS
9898
libc.src.__support.CPP.bit
9999
)
100+
101+
add_entrypoint_object(
102+
stdc_trailing_zeros_uc
103+
SRCS
104+
stdc_trailing_zeros_uc.cpp
105+
HDRS
106+
stdc_trailing_zeros_uc.h
107+
DEPENDS
108+
libc.src.__support.CPP.bit
109+
)
110+
111+
add_entrypoint_object(
112+
stdc_trailing_zeros_us
113+
SRCS
114+
stdc_trailing_zeros_us.cpp
115+
HDRS
116+
stdc_trailing_zeros_us.h
117+
DEPENDS
118+
libc.src.__support.CPP.bit
119+
)
120+
121+
add_entrypoint_object(
122+
stdc_trailing_zeros_ui
123+
SRCS
124+
stdc_trailing_zeros_ui.cpp
125+
HDRS
126+
stdc_trailing_zeros_ui.h
127+
DEPENDS
128+
libc.src.__support.CPP.bit
129+
)
130+
131+
add_entrypoint_object(
132+
stdc_trailing_zeros_ul
133+
SRCS
134+
stdc_trailing_zeros_ul.cpp
135+
HDRS
136+
stdc_trailing_zeros_ul.h
137+
DEPENDS
138+
libc.src.__support.CPP.bit
139+
)
140+
141+
add_entrypoint_object(
142+
stdc_trailing_zeros_ull
143+
SRCS
144+
stdc_trailing_zeros_ull.cpp
145+
HDRS
146+
stdc_trailing_zeros_ull.h
147+
DEPENDS
148+
libc.src.__support.CPP.bit
149+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_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_trailing_zeros_uc, (unsigned char value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_zeros_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_TRAILING_ZEROS_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_zeros_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_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_trailing_zeros_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_zeros_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_TRAILING_ZEROS_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_zeros_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_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_trailing_zeros_ul, (unsigned long value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_zeros_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_TRAILING_ZEROS_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_zeros_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_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_trailing_zeros_ull, (unsigned long long value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_zeros_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_TRAILING_ZEROS_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_zeros_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_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_trailing_zeros_us, (unsigned short value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_zeros_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_TRAILING_ZEROS_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_zeros_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; }
3333
unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; }
3434
unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; }
3535
unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBFU; }
36+
unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; }
37+
unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; }
38+
unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; }
39+
unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; }
40+
unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCFU; }
3641
}
3742

3843
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -52,3 +57,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
5257
EXPECT_EQ(stdc_leading_ones(0UL), 0xBDU);
5358
EXPECT_EQ(stdc_leading_ones(0ULL), 0xBFU);
5459
}
60+
61+
TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
62+
EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned char>(0U)), 0xCAU);
63+
EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned short>(0U)), 0xCBU);
64+
EXPECT_EQ(stdc_trailing_zeros(0U), 0xCCU);
65+
EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
66+
EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCFU);
67+
}

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,58 @@ add_libc_test(
110110
libc.src.stdbit.stdc_leading_ones_ull
111111
)
112112

113+
add_libc_test(
114+
stdc_trailing_zeros_uc_test
115+
SUITE
116+
libc-stdbit-tests
117+
SRCS
118+
stdc_trailing_zeros_uc_test.cpp
119+
DEPENDS
120+
libc.src.__support.CPP.limits
121+
libc.src.stdbit.stdc_trailing_zeros_uc
122+
)
123+
124+
add_libc_test(
125+
stdc_trailing_zeros_us_test
126+
SUITE
127+
libc-stdbit-tests
128+
SRCS
129+
stdc_trailing_zeros_us_test.cpp
130+
DEPENDS
131+
libc.src.__support.CPP.limits
132+
libc.src.stdbit.stdc_trailing_zeros_us
133+
)
134+
135+
add_libc_test(
136+
stdc_trailing_zeros_ui_test
137+
SUITE
138+
libc-stdbit-tests
139+
SRCS
140+
stdc_trailing_zeros_ui_test.cpp
141+
DEPENDS
142+
libc.src.__support.CPP.limits
143+
libc.src.stdbit.stdc_trailing_zeros_ui
144+
)
145+
146+
add_libc_test(
147+
stdc_trailing_zeros_ul_test
148+
SUITE
149+
libc-stdbit-tests
150+
SRCS
151+
stdc_trailing_zeros_ul_test.cpp
152+
DEPENDS
153+
libc.src.__support.CPP.limits
154+
libc.src.stdbit.stdc_trailing_zeros_ul
155+
)
156+
157+
add_libc_test(
158+
stdc_trailing_zeros_ull_test
159+
SUITE
160+
libc-stdbit-tests
161+
SRCS
162+
stdc_trailing_zeros_ull_test.cpp
163+
DEPENDS
164+
libc.src.__support.CPP.limits
165+
libc.src.stdbit.stdc_trailing_zeros_ull
166+
)
167+

0 commit comments

Comments
 (0)