Skip to content

Commit 041638c

Browse files
[libc][stdbit] implement stdc_bit_width (C23) (llvm#83892)
1 parent 233f750 commit 041638c

22 files changed

+344
-8
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ set(TARGET_LIBC_ENTRYPOINTS
147147
libc.src.stdbit.stdc_has_single_bit_ui
148148
libc.src.stdbit.stdc_has_single_bit_ul
149149
libc.src.stdbit.stdc_has_single_bit_ull
150+
libc.src.stdbit.stdc_bit_width_uc
151+
libc.src.stdbit.stdc_bit_width_us
152+
libc.src.stdbit.stdc_bit_width_ui
153+
libc.src.stdbit.stdc_bit_width_ul
154+
libc.src.stdbit.stdc_bit_width_ull
150155

151156
# stdlib.h entrypoints
152157
libc.src.stdlib.abs

libc/docs/stdbit.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ stdc_has_single_bit_us |check|
8686
stdc_has_single_bit_ui |check|
8787
stdc_has_single_bit_ul |check|
8888
stdc_has_single_bit_ull |check|
89-
stdc_bit_width_uc
90-
stdc_bit_width_us
91-
stdc_bit_width_ui
92-
stdc_bit_width_ul
93-
stdc_bit_width_ull
89+
stdc_bit_width_uc |check|
90+
stdc_bit_width_us |check|
91+
stdc_bit_width_ui |check|
92+
stdc_bit_width_ul |check|
93+
stdc_bit_width_ull |check|
9494
stdc_bit_floor_uc
9595
stdc_bit_floor_us
9696
stdc_bit_floor_ui
@@ -125,7 +125,7 @@ stdc_first_trailing_one |check|
125125
stdc_count_zeros |check|
126126
stdc_count_ones |check|
127127
stdc_has_single_bit |check|
128-
stdc_bit_width
128+
stdc_bit_width |check|
129129
stdc_bit_floor
130130
stdc_bit_ceil
131131
========================= =========

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ inline bool stdc_has_single_bit(unsigned long x) {
172172
inline bool stdc_has_single_bit(unsigned long long x) {
173173
return stdc_has_single_bit_ull(x);
174174
}
175+
inline unsigned stdc_bit_width(unsigned char x) { return stdc_bit_width_uc(x); }
176+
inline unsigned stdc_bit_width(unsigned short x) {
177+
return stdc_bit_width_us(x);
178+
}
179+
inline unsigned stdc_bit_width(unsigned x) { return stdc_bit_width_ui(x); }
180+
inline unsigned stdc_bit_width(unsigned long x) { return stdc_bit_width_ul(x); }
181+
inline unsigned stdc_bit_width(unsigned long long x) {
182+
return stdc_bit_width_ull(x);
183+
}
175184
#else
176185
#define stdc_leading_zeros(x) \
177186
_Generic((x), \
@@ -250,6 +259,13 @@ inline bool stdc_has_single_bit(unsigned long long x) {
250259
unsigned: stdc_has_single_bit_ui, \
251260
unsigned long: stdc_has_single_bit_ul, \
252261
unsigned long long: stdc_has_single_bit_ull)(x)
262+
#define stdc_bit_width(x) \
263+
_Generic((x), \
264+
unsigned char: stdc_bit_width_ui, \
265+
unsigned short: stdc_bit_width_us, \
266+
unsigned: stdc_bit_width_ui, \
267+
unsigned long: stdc_bit_width_ul, \
268+
unsigned long long: stdc_bit_width_ull)(x)
253269
#endif // __cplusplus
254270

255271
#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
@@ -800,7 +800,8 @@ def StdC : StandardSpec<"stdc"> {
800800
Macro<"stdc_first_trailing_one">,
801801
Macro<"stdc_count_zeros">,
802802
Macro<"stdc_count_ones">,
803-
Macro<"stdc_has_single_bit">
803+
Macro<"stdc_has_single_bit">,
804+
Macro<"std_bit_width">
804805
], // Macros
805806
[], // Types
806807
[], // Enumerations
@@ -854,7 +855,12 @@ def StdC : StandardSpec<"stdc"> {
854855
FunctionSpec<"stdc_has_single_bit_us", RetValSpec<BoolType>, [ArgSpec<UnsignedShortType>]>,
855856
FunctionSpec<"stdc_has_single_bit_ui", RetValSpec<BoolType>, [ArgSpec<UnsignedIntType>]>,
856857
FunctionSpec<"stdc_has_single_bit_ul", RetValSpec<BoolType>, [ArgSpec<UnsignedLongType>]>,
857-
FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>
858+
FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>,
859+
FunctionSpec<"stdc_bit_width_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
860+
FunctionSpec<"stdc_bit_width_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
861+
FunctionSpec<"stdc_bit_width_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
862+
FunctionSpec<"stdc_bit_width_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
863+
FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
858864
] // Functions
859865
>;
860866

libc/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(prefixes
1010
count_zeros
1111
count_ones
1212
has_single_bit
13+
bit_width
1314
)
1415
set(suffixes c s i l ll)
1516
foreach(prefix IN LISTS prefixes)

libc/src/stdbit/stdc_bit_width_uc.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_width_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_bit_width_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_bit_width_uc, (unsigned char value)) {
17+
return static_cast<unsigned>(cpp::bit_width(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_width_uc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_width_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_BIT_WIDTH_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_width_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UC_H

libc/src/stdbit/stdc_bit_width_ui.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_width_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_bit_width_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_bit_width_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::bit_width(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_width_ui.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_width_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_BIT_WIDTH_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_width_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UI_H

libc/src/stdbit/stdc_bit_width_ul.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_width_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_bit_width_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_bit_width_ul, (unsigned long value)) {
17+
return static_cast<unsigned>(cpp::bit_width(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_width_ul.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_width_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_BIT_WIDTH_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_width_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UL_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_width_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_bit_width_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_bit_width_ull, (unsigned long long value)) {
17+
return static_cast<unsigned>(cpp::bit_width(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_width_ull.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_width_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_BIT_WIDTH_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_width_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_ULL_H

libc/src/stdbit/stdc_bit_width_us.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_width_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_bit_width_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_bit_width_us, (unsigned short value)) {
17+
return static_cast<unsigned>(cpp::bit_width(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_width_us.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_width_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_BIT_WIDTH_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_width_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
8686
bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
8787
bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
8888
bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
89+
unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
90+
unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
91+
unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
92+
unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
93+
unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
8994
}
9095

9196
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -177,3 +182,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroHasSingleBit) {
177182
EXPECT_EQ(stdc_has_single_bit(1UL), false);
178183
EXPECT_EQ(stdc_has_single_bit(1ULL), false);
179184
}
185+
186+
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
187+
EXPECT_EQ(stdc_bit_width(static_cast<unsigned char>(1U)), 0x4AU);
188+
EXPECT_EQ(stdc_bit_width(static_cast<unsigned short>(1U)), 0x4BU);
189+
EXPECT_EQ(stdc_bit_width(1U), 0x4CU);
190+
EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
191+
EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
192+
}

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ set(prefixes
1212
count_zeros
1313
count_ones
1414
has_single_bit
15+
bit_width
1516
)
1617
set(suffixes c s i l ll)
1718
foreach(prefix IN LISTS prefixes)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Unittests for stdc_bit_width_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_bit_width_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitWidthUcTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(0U), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcBitWidthUcTest, Ones) {
18+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(UCHAR_MAX >> i),
20+
UCHAR_WIDTH - i);
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Unittests for stdc_bit_width_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/__support/CPP/limits.h"
10+
#include "src/stdbit/stdc_bit_width_ui.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitWidthUiTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(0U), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcBitWidthUiTest, Ones) {
18+
for (unsigned i = 0U; i != UINT_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(UINT_MAX >> i), UINT_WIDTH - i);
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Unittests for stdc_bit_width_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/__support/CPP/limits.h"
10+
#include "src/stdbit/stdc_bit_width_ul.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitWidthUlTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(0U), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcBitWidthUlTest, Ones) {
18+
for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(ULONG_MAX >> i),
20+
ULONG_WIDTH - i);
21+
}

0 commit comments

Comments
 (0)