Skip to content

Commit 6297479

Browse files
[libc][stdbit] implement stdc_first_trailing_one (C23) (llvm#81768)
1 parent de16a05 commit 6297479

24 files changed

+362
-12
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ set(TARGET_LIBC_ENTRYPOINTS
127127
libc.src.stdbit.stdc_first_trailing_zero_ui
128128
libc.src.stdbit.stdc_first_trailing_zero_ul
129129
libc.src.stdbit.stdc_first_trailing_zero_ull
130+
libc.src.stdbit.stdc_first_trailing_one_uc
131+
libc.src.stdbit.stdc_first_trailing_one_us
132+
libc.src.stdbit.stdc_first_trailing_one_ui
133+
libc.src.stdbit.stdc_first_trailing_one_ul
134+
libc.src.stdbit.stdc_first_trailing_one_ull
130135

131136
# stdlib.h entrypoints
132137
libc.src.stdlib.abs

libc/docs/stdbit.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ stdc_first_trailing_zero_us |check|
6666
stdc_first_trailing_zero_ui |check|
6767
stdc_first_trailing_zero_ul |check|
6868
stdc_first_trailing_zero_ull |check|
69-
stdc_first_trailing_one_uc
70-
stdc_first_trailing_one_us
71-
stdc_first_trailing_one_ui
72-
stdc_first_trailing_one_ul
73-
stdc_first_trailing_one_ull
69+
stdc_first_trailing_one_uc |check|
70+
stdc_first_trailing_one_us |check|
71+
stdc_first_trailing_one_ui |check|
72+
stdc_first_trailing_one_ul |check|
73+
stdc_first_trailing_one_ull |check|
7474
stdc_count_zeros_uc
7575
stdc_count_zeros_us
7676
stdc_count_zeros_ui
@@ -121,7 +121,7 @@ stdc_trailing_ones |check|
121121
stdc_first_leading_zero |check|
122122
stdc_first_leading_one |check|
123123
stdc_first_trailing_zero |check|
124-
stdc_first_trailing_one
124+
stdc_first_trailing_one |check|
125125
stdc_count_zeros
126126
stdc_count_ones
127127
stdc_has_single_bit

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ inline unsigned stdc_first_trailing_zero(unsigned long x) {
116116
inline unsigned stdc_first_trailing_zero(unsigned long long x) {
117117
return stdc_first_trailing_zero_ull(x);
118118
}
119+
inline unsigned stdc_first_trailing_one(unsigned char x) {
120+
return stdc_first_trailing_one_uc(x);
121+
}
122+
inline unsigned stdc_first_trailing_one(unsigned short x) {
123+
return stdc_first_trailing_one_us(x);
124+
}
125+
inline unsigned stdc_first_trailing_one(unsigned x) {
126+
return stdc_first_trailing_one_ui(x);
127+
}
128+
inline unsigned stdc_first_trailing_one(unsigned long x) {
129+
return stdc_first_trailing_one_ul(x);
130+
}
131+
inline unsigned stdc_first_trailing_one(unsigned long long x) {
132+
return stdc_first_trailing_one_ull(x);
133+
}
119134
#else
120135
#define stdc_leading_zeros(x) \
121136
_Generic((x), \
@@ -166,6 +181,13 @@ inline unsigned stdc_first_trailing_zero(unsigned long long x) {
166181
unsigned: stdc_first_trailing_zero_ui, \
167182
unsigned long: stdc_first_trailing_zero_ul, \
168183
unsigned long long: stdc_first_trailing_zero_ull)(x)
184+
#define stdc_first_trailing_one(x) \
185+
_Generic((x), \
186+
unsigned char: stdc_first_trailing_one_uc, \
187+
unsigned short: stdc_first_trailing_one_us, \
188+
unsigned: stdc_first_trailing_one_ui, \
189+
unsigned long: stdc_first_trailing_one_ul, \
190+
unsigned long long: stdc_first_trailing_one_ull)(x)
169191
#endif // __cplusplus
170192

171193
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

libc/spec/stdc.td

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,8 @@ def StdC : StandardSpec<"stdc"> {
783783
Macro<"stdc_trailing_ones">,
784784
Macro<"stdc_first_leading_zero">,
785785
Macro<"stdc_first_leading_one">,
786-
Macro<"stdc_first_trailing_zero">
786+
Macro<"stdc_first_trailing_zero">,
787+
Macro<"stdc_first_trailing_one">
787788
], // Macros
788789
[], // Types
789790
[], // Enumerations
@@ -818,11 +819,11 @@ def StdC : StandardSpec<"stdc"> {
818819
FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
819820
FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
820821
FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
821-
FunctionSpec<"stdc_first_trailing_zero_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
822-
FunctionSpec<"stdc_first_trailing_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
823-
FunctionSpec<"stdc_first_trailing_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
824-
FunctionSpec<"stdc_first_trailing_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
825-
FunctionSpec<"stdc_first_trailing_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
822+
FunctionSpec<"stdc_first_trailing_one_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
823+
FunctionSpec<"stdc_first_trailing_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
824+
FunctionSpec<"stdc_first_trailing_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
825+
FunctionSpec<"stdc_first_trailing_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
826+
FunctionSpec<"stdc_first_trailing_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
826827
] // Functions
827828
>;
828829

libc/src/__support/CPP/bit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
243243
: countr_zero(static_cast<T>(~value)) + 1;
244244
}
245245

246+
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
247+
[[nodiscard]] LIBC_INLINE constexpr int first_trailing_one(T value) {
248+
return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
249+
}
250+
246251
} // namespace LIBC_NAMESPACE::cpp
247252

248253
#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
@@ -6,6 +6,7 @@ set(prefixes
66
first_leading_zero
77
first_leading_one
88
first_trailing_zero
9+
first_trailing_one
910
)
1011
set(suffixes c s i l ll)
1112
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_trailing_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_trailing_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_trailing_one_uc,
17+
(unsigned char value)) {
18+
return static_cast<unsigned>(cpp::first_trailing_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_trailing_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_TRAILING_ONE_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_one_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_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_trailing_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_trailing_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_trailing_one_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::first_trailing_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_trailing_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_TRAILING_ONE_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_one_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UI_H

0 commit comments

Comments
 (0)