Skip to content

Commit 16e7d68

Browse files
[libc][stdbit] implement stdc_first_trailing_zero (C23) (#81526)
1 parent 307cd88 commit 16e7d68

24 files changed

+366
-8
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ set(TARGET_LIBC_ENTRYPOINTS
122122
libc.src.stdbit.stdc_first_leading_one_ui
123123
libc.src.stdbit.stdc_first_leading_one_ul
124124
libc.src.stdbit.stdc_first_leading_one_ull
125+
libc.src.stdbit.stdc_first_trailing_zero_uc
126+
libc.src.stdbit.stdc_first_trailing_zero_us
127+
libc.src.stdbit.stdc_first_trailing_zero_ui
128+
libc.src.stdbit.stdc_first_trailing_zero_ul
129+
libc.src.stdbit.stdc_first_trailing_zero_ull
125130

126131
# stdlib.h entrypoints
127132
libc.src.stdlib.abs

libc/docs/stdbit.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ stdc_first_leading_one_us |check|
6161
stdc_first_leading_one_ui |check|
6262
stdc_first_leading_one_ul |check|
6363
stdc_first_leading_one_ull |check|
64-
stdc_first_trailing_zero_uc
65-
stdc_first_trailing_zero_us
66-
stdc_first_trailing_zero_ui
67-
stdc_first_trailing_zero_ul
68-
stdc_first_trailing_zero_ull
64+
stdc_first_trailing_zero_uc |check|
65+
stdc_first_trailing_zero_us |check|
66+
stdc_first_trailing_zero_ui |check|
67+
stdc_first_trailing_zero_ul |check|
68+
stdc_first_trailing_zero_ull |check|
6969
stdc_first_trailing_one_uc
7070
stdc_first_trailing_one_us
7171
stdc_first_trailing_one_ui
@@ -120,7 +120,7 @@ stdc_trailing_zeros |check|
120120
stdc_trailing_ones |check|
121121
stdc_first_leading_zero |check|
122122
stdc_first_leading_one |check|
123-
stdc_first_trailing_zero
123+
stdc_first_trailing_zero |check|
124124
stdc_first_trailing_one
125125
stdc_count_zeros
126126
stdc_count_ones

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ inline unsigned stdc_first_leading_one(unsigned long x) {
101101
inline unsigned stdc_first_leading_one(unsigned long long x) {
102102
return stdc_first_leading_one_ull(x);
103103
}
104+
inline unsigned stdc_first_trailing_zero(unsigned char x) {
105+
return stdc_first_trailing_zero_uc(x);
106+
}
107+
inline unsigned stdc_first_trailing_zero(unsigned short x) {
108+
return stdc_first_trailing_zero_us(x);
109+
}
110+
inline unsigned stdc_first_trailing_zero(unsigned x) {
111+
return stdc_first_trailing_zero_ui(x);
112+
}
113+
inline unsigned stdc_first_trailing_zero(unsigned long x) {
114+
return stdc_first_trailing_zero_ul(x);
115+
}
116+
inline unsigned stdc_first_trailing_zero(unsigned long long x) {
117+
return stdc_first_trailing_zero_ull(x);
118+
}
104119
#else
105120
#define stdc_leading_zeros(x) \
106121
_Generic((x), \
@@ -144,6 +159,13 @@ inline unsigned stdc_first_leading_one(unsigned long long x) {
144159
unsigned: stdc_first_leading_one_ui, \
145160
unsigned long: stdc_first_leading_one_ul, \
146161
unsigned long long: stdc_first_leading_one_ull)(x)
162+
#define stdc_first_trailing_zero(x) \
163+
_Generic((x), \
164+
unsigned char: stdc_first_trailing_zero_uc, \
165+
unsigned short: stdc_first_trailing_zero_us, \
166+
unsigned: stdc_first_trailing_zero_ui, \
167+
unsigned long: stdc_first_trailing_zero_ul, \
168+
unsigned long long: stdc_first_trailing_zero_ull)(x)
147169
#endif // __cplusplus
148170

149171
#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
@@ -782,7 +782,8 @@ def StdC : StandardSpec<"stdc"> {
782782
Macro<"stdc_trailing_zeros">,
783783
Macro<"stdc_trailing_ones">,
784784
Macro<"stdc_first_leading_zero">,
785-
Macro<"stdc_first_leading_one">
785+
Macro<"stdc_first_leading_one">,
786+
Macro<"stdc_first_trailing_zero">
786787
], // Macros
787788
[], // Types
788789
[], // Enumerations
@@ -816,7 +817,12 @@ def StdC : StandardSpec<"stdc"> {
816817
FunctionSpec<"stdc_first_leading_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
817818
FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
818819
FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
819-
FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
820+
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>]>,
820826
] // Functions
821827
>;
822828

libc/src/__support/CPP/bit.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
236236
return first_leading_zero(static_cast<T>(~value));
237237
}
238238

239+
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
240+
[[nodiscard]] LIBC_INLINE constexpr int first_trailing_zero(T value) {
241+
return value == cpp::numeric_limits<T>::max()
242+
? 0
243+
: countr_zero(static_cast<T>(~value)) + 1;
244+
}
245+
239246
} // namespace LIBC_NAMESPACE::cpp
240247

241248
#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
@@ -5,6 +5,7 @@ set(prefixes
55
trailing_ones
66
first_leading_zero
77
first_leading_one
8+
first_trailing_zero
89
)
910
set(suffixes c s i l ll)
1011
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_zero_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_zero_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_zero_uc,
17+
(unsigned char value)) {
18+
return static_cast<unsigned>(cpp::first_trailing_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_first_trailing_zero_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_ZERO_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_zero_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_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_zero_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_zero_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_zero_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::first_trailing_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_first_trailing_zero_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_ZERO_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_zero_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_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_zero_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_zero_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_zero_ul,
17+
(unsigned long value)) {
18+
return static_cast<unsigned>(cpp::first_trailing_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_first_trailing_zero_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_ZERO_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_zero_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_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_zero_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_zero_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_zero_ull,
17+
(unsigned long long value)) {
18+
return static_cast<unsigned>(cpp::first_trailing_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_first_trailing_zero_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_ZERO_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_zero_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_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_zero_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_zero_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_zero_us,
17+
(unsigned short value)) {
18+
return static_cast<unsigned>(cpp::first_trailing_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_first_trailing_zero_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_ZERO_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_first_trailing_zero_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ZERO_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
5757
unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
5858
return 0xFFU;
5959
}
60+
unsigned stdc_first_trailing_zero_uc(unsigned char) noexcept { return 0x0AU; }
61+
unsigned stdc_first_trailing_zero_us(unsigned short) noexcept { return 0x0BU; }
62+
unsigned stdc_first_trailing_zero_ui(unsigned) noexcept { return 0x0CU; }
63+
unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
64+
unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
65+
return 0x0FU;
66+
}
6067
}
6168

6269
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -108,3 +115,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingOne) {
108115
EXPECT_EQ(stdc_first_leading_one(0UL), 0xFDU);
109116
EXPECT_EQ(stdc_first_leading_one(0ULL), 0xFFU);
110117
}
118+
119+
TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
120+
EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned char>(0U)), 0x0AU);
121+
EXPECT_EQ(stdc_first_trailing_zero(static_cast<unsigned short>(0U)), 0x0BU);
122+
EXPECT_EQ(stdc_first_trailing_zero(0U), 0x0CU);
123+
EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
124+
EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0FU);
125+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,10 @@ TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypes) {
220220
cpp::numeric_limits<T>::digits - i);
221221
}
222222

223+
TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypes) {
224+
EXPECT_EQ(first_trailing_zero<T>(cpp::numeric_limits<T>::max()), 0);
225+
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
226+
EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
227+
}
228+
223229
} // namespace LIBC_NAMESPACE::cpp

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(prefixes
77
trailing_ones
88
first_leading_zero
99
first_leading_one
10+
first_trailing_zero
1011
)
1112
set(suffixes c s i l ll)
1213
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_zero_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_zero_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcFirstTrailingZeroUcTest, ALL) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_uc(UCHAR_MAX), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcFirstTrailingZeroUcTest, ZeroHot) {
18+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_zero_uc(~(1U << i)), i + 1);
20+
}

0 commit comments

Comments
 (0)