Skip to content

Commit 6297479

Browse files
[libc][stdbit] implement stdc_first_trailing_one (C23) (#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
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_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_trailing_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_trailing_one_ul,
17+
(unsigned long 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_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_TRAILING_ONE_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_one_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_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_trailing_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_trailing_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_trailing_one_ull,
17+
(unsigned long long 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_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_TRAILING_ONE_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_one_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_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_trailing_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_trailing_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_trailing_one_us,
17+
(unsigned short 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_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_TRAILING_ONE_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_one_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
6464
unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
6565
return 0x0FU;
6666
}
67+
unsigned stdc_first_trailing_one_uc(unsigned char) noexcept { return 0x1AU; }
68+
unsigned stdc_first_trailing_one_us(unsigned short) noexcept { return 0x1BU; }
69+
unsigned stdc_first_trailing_one_ui(unsigned) noexcept { return 0x1CU; }
70+
unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
71+
unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
72+
return 0x1FU;
73+
}
6774
}
6875

6976
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -123,3 +130,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
123130
EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
124131
EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0FU);
125132
}
133+
134+
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingOne) {
135+
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned char>(0U)), 0x1AU);
136+
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned short>(0U)), 0x1BU);
137+
EXPECT_EQ(stdc_first_trailing_one(0U), 0x1CU);
138+
EXPECT_EQ(stdc_first_trailing_one(0UL), 0x1DU);
139+
EXPECT_EQ(stdc_first_trailing_one(0ULL), 0x1FU);
140+
}

libc/test/src/__support/CPP/bit_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,10 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypes) {
226226
EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
227227
}
228228

229+
TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypes) {
230+
EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 0);
231+
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
232+
EXPECT_EQ(first_trailing_one<T>(T(1) << i), i + 1);
233+
}
234+
229235
} // namespace LIBC_NAMESPACE::cpp

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(prefixes
88
first_leading_zero
99
first_leading_one
1010
first_trailing_zero
11+
first_trailing_one
1112
)
1213
set(suffixes c s i l ll)
1314
foreach(prefix IN LISTS prefixes)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Unittests for 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/__support/CPP/limits.h"
10+
#include "src/stdbit/stdc_first_trailing_one_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcFirstTrailingOneUcTest, ALL) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(UCHAR_MAX), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcFirstTrailingOneUcTest, OneHot) {
18+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(1U << i), i + 1);
20+
}

0 commit comments

Comments
 (0)