Skip to content

Commit d5a3de4

Browse files
[libc][stdbit] implement stdc_trailing_zeros (C23) (#80344)
1 parent 62c352e commit d5a3de4

21 files changed

+384
-211
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/spec/stdc.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ def StdC : StandardSpec<"stdc"> {
775775
HeaderSpec StdBit = HeaderSpec<
776776
"stdbit.h",
777777
[
778-
Macro<"stdc_leading_zeros">
778+
Macro<"stdc_leading_zeros">,
779+
Macro<"stdc_leading_ones">,
780+
Macro<"stdc_trailing_zeros">
779781
], // Macros
780782
[], // Types
781783
[], // Enumerations
@@ -789,7 +791,12 @@ def StdC : StandardSpec<"stdc"> {
789791
FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
790792
FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
791793
FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
792-
FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
794+
FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
795+
FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
796+
FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
797+
FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
798+
FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
799+
FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
793800
] // Functions
794801
>;
795802

libc/src/stdbit/CMakeLists.txt

Lines changed: 19 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,19 @@
1-
add_entrypoint_object(
2-
stdc_leading_zeros_uc
3-
SRCS
4-
stdc_leading_zeros_uc.cpp
5-
HDRS
6-
stdc_leading_zeros_uc.h
7-
DEPENDS
8-
libc.src.__support.CPP.bit
9-
)
10-
11-
add_entrypoint_object(
12-
stdc_leading_zeros_us
13-
SRCS
14-
stdc_leading_zeros_us.cpp
15-
HDRS
16-
stdc_leading_zeros_us.h
17-
DEPENDS
18-
libc.src.__support.CPP.bit
19-
)
20-
21-
add_entrypoint_object(
22-
stdc_leading_zeros_ui
23-
SRCS
24-
stdc_leading_zeros_ui.cpp
25-
HDRS
26-
stdc_leading_zeros_ui.h
27-
DEPENDS
28-
libc.src.__support.CPP.bit
29-
)
30-
31-
add_entrypoint_object(
32-
stdc_leading_zeros_ul
33-
SRCS
34-
stdc_leading_zeros_ul.cpp
35-
HDRS
36-
stdc_leading_zeros_ul.h
37-
DEPENDS
38-
libc.src.__support.CPP.bit
39-
)
40-
41-
add_entrypoint_object(
42-
stdc_leading_zeros_ull
43-
SRCS
44-
stdc_leading_zeros_ull.cpp
45-
HDRS
46-
stdc_leading_zeros_ull.h
47-
DEPENDS
48-
libc.src.__support.CPP.bit
49-
)
50-
51-
add_entrypoint_object(
52-
stdc_leading_ones_uc
53-
SRCS
54-
stdc_leading_ones_uc.cpp
55-
HDRS
56-
stdc_leading_ones_uc.h
57-
DEPENDS
58-
libc.src.__support.CPP.bit
59-
)
60-
61-
add_entrypoint_object(
62-
stdc_leading_ones_us
63-
SRCS
64-
stdc_leading_ones_us.cpp
65-
HDRS
66-
stdc_leading_ones_us.h
67-
DEPENDS
68-
libc.src.__support.CPP.bit
69-
)
70-
71-
add_entrypoint_object(
72-
stdc_leading_ones_ui
73-
SRCS
74-
stdc_leading_ones_ui.cpp
75-
HDRS
76-
stdc_leading_ones_ui.h
77-
DEPENDS
78-
libc.src.__support.CPP.bit
79-
)
80-
81-
add_entrypoint_object(
82-
stdc_leading_ones_ul
83-
SRCS
84-
stdc_leading_ones_ul.cpp
85-
HDRS
86-
stdc_leading_ones_ul.h
87-
DEPENDS
88-
libc.src.__support.CPP.bit
89-
)
90-
91-
add_entrypoint_object(
92-
stdc_leading_ones_ull
93-
SRCS
94-
stdc_leading_ones_ull.cpp
95-
HDRS
96-
stdc_leading_ones_ull.h
97-
DEPENDS
98-
libc.src.__support.CPP.bit
99-
)
1+
set(prefixes
2+
leading_zeros
3+
leading_ones
4+
trailing_zeros
5+
)
6+
set(suffixes c s i l ll)
7+
foreach(prefix IN LISTS prefixes)
8+
foreach(suffix IN LISTS suffixes)
9+
add_entrypoint_object(
10+
stdc_${prefix}_u${suffix}
11+
SRCS
12+
stdc_${prefix}_u${suffix}.cpp
13+
HDRS
14+
stdc_${prefix}_u${suffix}.h
15+
DEPENDS
16+
libc.src.__support.CPP.bit
17+
)
18+
endforeach()
19+
endforeach()
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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,
17+
(unsigned long long value)) {
18+
return static_cast<unsigned>(cpp::countr_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_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

0 commit comments

Comments
 (0)