Skip to content

Commit 75b0d38

Browse files
author
Michael Flanders
authored
[libc][stdbit][c23] adds implementation of stdc_bit_ceil functions (#84657)
Closes #84652. Based on #84233.
1 parent 862c7e0 commit 75b0d38

23 files changed

+409
-9
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ set(TARGET_LIBC_ENTRYPOINTS
158158
libc.src.stdbit.stdc_bit_floor_ui
159159
libc.src.stdbit.stdc_bit_floor_ul
160160
libc.src.stdbit.stdc_bit_floor_ull
161+
libc.src.stdbit.stdc_bit_ceil_uc
162+
libc.src.stdbit.stdc_bit_ceil_us
163+
libc.src.stdbit.stdc_bit_ceil_ui
164+
libc.src.stdbit.stdc_bit_ceil_ul
165+
libc.src.stdbit.stdc_bit_ceil_ull
161166

162167
# stdlib.h entrypoints
163168
libc.src.stdlib.abs

libc/docs/stdbit.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ stdc_bit_floor_us |check|
9696
stdc_bit_floor_ui |check|
9797
stdc_bit_floor_ul |check|
9898
stdc_bit_floor_ull |check|
99-
stdc_bit_ceil_uc
100-
stdc_bit_ceil_us
101-
stdc_bit_ceil_ui
102-
stdc_bit_ceil_ul
103-
stdc_bit_ceil_ull
99+
stdc_bit_ceil_uc |check|
100+
stdc_bit_ceil_us |check|
101+
stdc_bit_ceil_ui |check|
102+
stdc_bit_ceil_ul |check|
103+
stdc_bit_ceil_ull |check|
104104
============================ =========
105105

106106

@@ -127,7 +127,7 @@ stdc_count_ones |check|
127127
stdc_has_single_bit |check|
128128
stdc_bit_width |check|
129129
stdc_bit_floor |check|
130-
stdc_bit_ceil
130+
stdc_bit_ceil |check|
131131
========================= =========
132132

133133
Standards

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ inline unsigned long stdc_bit_floor(unsigned long x) {
194194
inline unsigned long long stdc_bit_floor(unsigned long long x) {
195195
return stdc_bit_floor_ull(x);
196196
}
197+
inline unsigned char stdc_bit_ceil(unsigned char x) {
198+
return stdc_bit_ceil_uc(x);
199+
}
200+
inline unsigned short stdc_bit_ceil(unsigned short x) {
201+
return stdc_bit_ceil_us(x);
202+
}
203+
inline unsigned stdc_bit_ceil(unsigned x) { return stdc_bit_ceil_ui(x); }
204+
inline unsigned long stdc_bit_ceil(unsigned long x) {
205+
return stdc_bit_ceil_ul(x);
206+
}
207+
inline unsigned long long stdc_bit_ceil(unsigned long long x) {
208+
return stdc_bit_ceil_ull(x);
209+
}
197210
#else
198211
#define stdc_leading_zeros(x) \
199212
_Generic((x), \
@@ -286,6 +299,13 @@ inline unsigned long long stdc_bit_floor(unsigned long long x) {
286299
unsigned: stdc_bit_floor_ui, \
287300
unsigned long: stdc_bit_floor_ul, \
288301
unsigned long long: stdc_bit_floor_ull)(x)
302+
#define stdc_bit_ceil(x) \
303+
_Generic((x), \
304+
unsigned char: stdc_bit_ceil_uc, \
305+
unsigned short: stdc_bit_ceil_us, \
306+
unsigned: stdc_bit_ceil_ui, \
307+
unsigned long: stdc_bit_ceil_ul, \
308+
unsigned long long: stdc_bit_ceil_ull)(x)
289309
#endif // __cplusplus
290310

291311
#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
@@ -816,7 +816,8 @@ def StdC : StandardSpec<"stdc"> {
816816
Macro<"stdc_count_ones">,
817817
Macro<"stdc_has_single_bit">,
818818
Macro<"std_bit_width">,
819-
Macro<"std_bit_floor">
819+
Macro<"std_bit_floor">,
820+
Macro<"std_bit_ceil">
820821
], // Macros
821822
[], // Types
822823
[], // Enumerations
@@ -880,7 +881,12 @@ def StdC : StandardSpec<"stdc"> {
880881
FunctionSpec<"stdc_bit_floor_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
881882
FunctionSpec<"stdc_bit_floor_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
882883
FunctionSpec<"stdc_bit_floor_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
883-
FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
884+
FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>,
885+
FunctionSpec<"stdc_bit_ceil_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
886+
FunctionSpec<"stdc_bit_ceil_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
887+
FunctionSpec<"stdc_bit_ceil_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
888+
FunctionSpec<"stdc_bit_ceil_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
889+
FunctionSpec<"stdc_bit_ceil_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
884890
] // Functions
885891
>;
886892

libc/src/__support/CPP/bit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ template <typename T>
193193
bit_ceil(T value) {
194194
if (value < 2)
195195
return 1;
196-
return T(1) << cpp::bit_width<T>(value - 1u);
196+
return static_cast<T>(T(1) << cpp::bit_width<T>(value - 1u));
197197
}
198198

199199
// Rotate algorithms make use of "Safe, Efficient, and Portable Rotate in C/C++"

libc/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
has_single_bit
1313
bit_width
1414
bit_floor
15+
bit_ceil
1516
)
1617
set(suffixes c s i l ll)
1718
foreach(prefix IN LISTS prefixes)

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_ceil_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 char, stdc_bit_ceil_uc, (unsigned char value)) {
17+
return cpp::bit_ceil(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_CEIL_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned char stdc_bit_ceil_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_ceil_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_ceil_ui, (unsigned value)) {
17+
return cpp::bit_ceil(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_CEIL_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_ceil_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_ceil_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 long, stdc_bit_ceil_ul, (unsigned long value)) {
17+
return cpp::bit_ceil(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_CEIL_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long stdc_bit_ceil_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H

libc/src/stdbit/stdc_bit_ceil_ull.cpp

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

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_CEIL_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long long stdc_bit_ceil_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_ceil_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 short, stdc_bit_ceil_us, (unsigned short value)) {
17+
return cpp::bit_ceil(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_ceil_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_ceil_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_CEIL_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned short stdc_bit_ceil_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H

libc/test/include/stdbit_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
9898
unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
9999
return 0x5EU;
100100
}
101+
unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
102+
unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
103+
unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
104+
unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
105+
unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
106+
return 0x6EU;
107+
}
101108
}
102109

103110
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -207,3 +214,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
207214
EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
208215
EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
209216
}
217+
218+
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
219+
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned char>(0U)),
220+
static_cast<unsigned char>(0x6AU));
221+
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned short>(0U)),
222+
static_cast<unsigned short>(0x6BU));
223+
EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU);
224+
EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
225+
EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
226+
}

libc/test/src/stdbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(prefixes
1414
has_single_bit
1515
bit_width
1616
bit_floor
17+
bit_ceil
1718
)
1819
set(suffixes c s i l ll)
1920
foreach(prefix IN LISTS prefixes)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- Unittests for stdc_bit_ceil_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_ceil_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitceilUcTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(0U),
15+
static_cast<unsigned char>(1));
16+
}
17+
18+
TEST(LlvmLibcStdcBitceilUcTest, Ones) {
19+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
20+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(1U << i),
21+
static_cast<unsigned char>(1U << i));
22+
}
23+
24+
TEST(LlvmLibcStdcBitceilUcTest, OneLessThanPowsTwo) {
25+
for (unsigned i = 2U; i != UCHAR_WIDTH; ++i)
26+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) - 1),
27+
static_cast<unsigned char>(1U << i));
28+
}
29+
30+
TEST(LlvmLibcStdcBitceilUcTest, OneMoreThanPowsTwo) {
31+
for (unsigned i = 1U; i != UCHAR_WIDTH - 1; ++i)
32+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) + 1),
33+
static_cast<unsigned char>(1U << (i + 1)));
34+
}

0 commit comments

Comments
 (0)