Skip to content

Commit 7760006

Browse files
[libc][stdbit] implement stdc_trailing_ones (C23) (#80459)
1 parent eeb60e3 commit 7760006

21 files changed

+347
-2
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ set(TARGET_LIBC_ENTRYPOINTS
107107
libc.src.stdbit.stdc_trailing_zeros_ui
108108
libc.src.stdbit.stdc_trailing_zeros_ul
109109
libc.src.stdbit.stdc_trailing_zeros_ull
110+
libc.src.stdbit.stdc_trailing_ones_uc
111+
libc.src.stdbit.stdc_trailing_ones_us
112+
libc.src.stdbit.stdc_trailing_ones_ui
113+
libc.src.stdbit.stdc_trailing_ones_ul
114+
libc.src.stdbit.stdc_trailing_ones_ull
110115

111116
# stdlib.h entrypoints
112117
libc.src.stdlib.abs

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
1010
#define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
1111

12+
// TODO(https://github.com/llvm/llvm-project/issues/80509): support _BitInt().
1213
#ifdef __cplusplus
1314
inline unsigned stdc_leading_zeros(unsigned char x) {
1415
return stdc_leading_zeros_uc(x);
@@ -55,6 +56,21 @@ inline unsigned stdc_trailing_zeros(unsigned long x) {
5556
inline unsigned stdc_trailing_zeros(unsigned long long x) {
5657
return stdc_trailing_zeros_ull(x);
5758
}
59+
inline unsigned stdc_trailing_ones(unsigned char x) {
60+
return stdc_trailing_ones_uc(x);
61+
}
62+
inline unsigned stdc_trailing_ones(unsigned short x) {
63+
return stdc_trailing_ones_us(x);
64+
}
65+
inline unsigned stdc_trailing_ones(unsigned x) {
66+
return stdc_trailing_ones_ui(x);
67+
}
68+
inline unsigned stdc_trailing_ones(unsigned long x) {
69+
return stdc_trailing_ones_ul(x);
70+
}
71+
inline unsigned stdc_trailing_ones(unsigned long long x) {
72+
return stdc_trailing_ones_ull(x);
73+
}
5874
#else
5975
#define stdc_leading_zeros(x) \
6076
_Generic((x), \
@@ -77,6 +93,13 @@ inline unsigned stdc_trailing_zeros(unsigned long long x) {
7793
unsigned: stdc_trailing_zeros_ui, \
7894
unsigned long: stdc_trailing_zeros_ul, \
7995
unsigned long long: stdc_trailing_zeros_ull)(x)
96+
#define stdc_trailing_ones(x) \
97+
_Generic((x), \
98+
unsigned char: stdc_trailing_ones_uc, \
99+
unsigned short: stdc_trailing_ones_us, \
100+
unsigned: stdc_trailing_ones_ui, \
101+
unsigned long: stdc_trailing_ones_ul, \
102+
unsigned long long: stdc_trailing_ones_ull)(x)
80103
#endif // __cplusplus
81104

82105
#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
@@ -777,7 +777,8 @@ def StdC : StandardSpec<"stdc"> {
777777
[
778778
Macro<"stdc_leading_zeros">,
779779
Macro<"stdc_leading_ones">,
780-
Macro<"stdc_trailing_zeros">
780+
Macro<"stdc_trailing_zeros">,
781+
Macro<"stdc_trailing_ones">
781782
], // Macros
782783
[], // Types
783784
[], // Enumerations
@@ -796,7 +797,12 @@ def StdC : StandardSpec<"stdc"> {
796797
FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
797798
FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
798799
FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
799-
FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
800+
FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
801+
FunctionSpec<"stdc_trailing_ones_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
802+
FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
803+
FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
804+
FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
805+
FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
800806
] // Functions
801807
>;
802808

libc/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(prefixes
22
leading_zeros
33
leading_ones
44
trailing_zeros
5+
trailing_ones
56
)
67
set(suffixes c s i l ll)
78
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_trailing_ones_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_ones_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_ones_uc, (unsigned char value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_ones_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_ONES_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_ones_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_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_ones_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_ones_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_ones_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_ones_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_ONES_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_ones_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_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_ones_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_ones_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_ones_ul, (unsigned long value)) {
17+
return static_cast<unsigned>(cpp::countr_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_trailing_ones_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_ONES_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_trailing_ones_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H

0 commit comments

Comments
 (0)