Skip to content

Commit 101a13d

Browse files
[libc][stdbit] implement stdc_bit_floor (C23) (llvm#84233)
1 parent 904a6ae commit 101a13d

23 files changed

+357
-9
lines changed

libc/config/linux/x86_64/entrypoints.txt

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ set(TARGET_LIBC_ENTRYPOINTS
152152
libc.src.stdbit.stdc_bit_width_ui
153153
libc.src.stdbit.stdc_bit_width_ul
154154
libc.src.stdbit.stdc_bit_width_ull
155+
libc.src.stdbit.stdc_bit_floor_uc
156+
libc.src.stdbit.stdc_bit_floor_us
157+
libc.src.stdbit.stdc_bit_floor_ui
158+
libc.src.stdbit.stdc_bit_floor_ul
159+
libc.src.stdbit.stdc_bit_floor_ull
155160

156161
# stdlib.h entrypoints
157162
libc.src.stdlib.abs

libc/docs/stdbit.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ stdc_bit_width_us |check|
9191
stdc_bit_width_ui |check|
9292
stdc_bit_width_ul |check|
9393
stdc_bit_width_ull |check|
94-
stdc_bit_floor_uc
95-
stdc_bit_floor_us
96-
stdc_bit_floor_ui
97-
stdc_bit_floor_ul
98-
stdc_bit_floor_ull
94+
stdc_bit_floor_uc |check|
95+
stdc_bit_floor_us |check|
96+
stdc_bit_floor_ui |check|
97+
stdc_bit_floor_ul |check|
98+
stdc_bit_floor_ull |check|
9999
stdc_bit_ceil_uc
100100
stdc_bit_ceil_us
101101
stdc_bit_ceil_ui
@@ -126,7 +126,7 @@ stdc_count_zeros |check|
126126
stdc_count_ones |check|
127127
stdc_has_single_bit |check|
128128
stdc_bit_width |check|
129-
stdc_bit_floor
129+
stdc_bit_floor |check|
130130
stdc_bit_ceil
131131
========================= =========
132132

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

+20
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ inline unsigned stdc_bit_width(unsigned long x) { return stdc_bit_width_ul(x); }
181181
inline unsigned stdc_bit_width(unsigned long long x) {
182182
return stdc_bit_width_ull(x);
183183
}
184+
inline unsigned char stdc_bit_floor(unsigned char x) {
185+
return stdc_bit_floor_uc(x);
186+
}
187+
inline unsigned short stdc_bit_floor(unsigned short x) {
188+
return stdc_bit_floor_us(x);
189+
}
190+
inline unsigned stdc_bit_floor(unsigned x) { return stdc_bit_floor_ui(x); }
191+
inline unsigned long stdc_bit_floor(unsigned long x) {
192+
return stdc_bit_floor_ul(x);
193+
}
194+
inline unsigned long long stdc_bit_floor(unsigned long long x) {
195+
return stdc_bit_floor_ull(x);
196+
}
184197
#else
185198
#define stdc_leading_zeros(x) \
186199
_Generic((x), \
@@ -266,6 +279,13 @@ inline unsigned stdc_bit_width(unsigned long long x) {
266279
unsigned: stdc_bit_width_ui, \
267280
unsigned long: stdc_bit_width_ul, \
268281
unsigned long long: stdc_bit_width_ull)(x)
282+
#define stdc_bit_floor(x) \
283+
_Generic((x), \
284+
unsigned char: stdc_bit_floor_ui, \
285+
unsigned short: stdc_bit_floor_us, \
286+
unsigned: stdc_bit_floor_ui, \
287+
unsigned long: stdc_bit_floor_ul, \
288+
unsigned long long: stdc_bit_floor_ull)(x)
269289
#endif // __cplusplus
270290

271291
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

libc/spec/stdc.td

+8-2
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,8 @@ def StdC : StandardSpec<"stdc"> {
801801
Macro<"stdc_count_zeros">,
802802
Macro<"stdc_count_ones">,
803803
Macro<"stdc_has_single_bit">,
804-
Macro<"std_bit_width">
804+
Macro<"std_bit_width">,
805+
Macro<"std_bit_floor">
805806
], // Macros
806807
[], // Types
807808
[], // Enumerations
@@ -860,7 +861,12 @@ def StdC : StandardSpec<"stdc"> {
860861
FunctionSpec<"stdc_bit_width_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
861862
FunctionSpec<"stdc_bit_width_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
862863
FunctionSpec<"stdc_bit_width_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
863-
FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
864+
FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
865+
FunctionSpec<"stdc_bit_floor_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
866+
FunctionSpec<"stdc_bit_floor_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
867+
FunctionSpec<"stdc_bit_floor_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
868+
FunctionSpec<"stdc_bit_floor_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
869+
FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
864870
] // Functions
865871
>;
866872

libc/src/__support/CPP/bit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ template <typename T>
178178
bit_floor(T value) {
179179
if (!value)
180180
return 0;
181-
return T(1) << (cpp::bit_width(value) - 1);
181+
return static_cast<T>(T(1) << (cpp::bit_width(value) - 1));
182182
}
183183

184184
/// Returns the smallest integral power of two no smaller than value if value is

libc/src/stdbit/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(prefixes
1111
count_ones
1212
has_single_bit
1313
bit_width
14+
bit_floor
1415
)
1516
set(suffixes c s i l ll)
1617
foreach(prefix IN LISTS prefixes)

libc/src/stdbit/stdc_bit_floor_uc.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_floor_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_floor_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_floor_uc, (unsigned char value)) {
17+
return cpp::bit_floor(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_floor_uc.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned char stdc_bit_floor_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UC_H

libc/src/stdbit/stdc_bit_floor_ui.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_floor_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_floor_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_floor_ui, (unsigned value)) {
17+
return cpp::bit_floor(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_floor_ui.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_bit_floor_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H

libc/src/stdbit/stdc_bit_floor_ul.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_floor_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_floor_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_floor_ul, (unsigned long value)) {
17+
return cpp::bit_floor(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_floor_ul.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long stdc_bit_floor_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of stdc_bit_floor_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_floor_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_floor_ull,
17+
(unsigned long long value)) {
18+
return cpp::bit_floor(value);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_floor_ull.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_floor_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_FLOOR_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long long stdc_bit_floor_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H

libc/src/stdbit/stdc_bit_floor_us.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_bit_floor_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_floor_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_floor_us, (unsigned short value)) {
17+
return cpp::bit_floor(value);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/stdbit/stdc_bit_floor_us.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_bit_floor_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_FLOOR_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned short stdc_bit_floor_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_US_H

libc/test/include/stdbit_test.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
9191
unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
9292
unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
9393
unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
94+
unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
95+
unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
96+
unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
97+
unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
98+
unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
99+
return 0x5EU;
100+
}
94101
}
95102

96103
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -190,3 +197,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
190197
EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
191198
EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
192199
}
200+
201+
TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
202+
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned char>(0U)),
203+
static_cast<unsigned char>(0x5AU));
204+
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned short>(0U)),
205+
static_cast<unsigned short>(0x5BU));
206+
EXPECT_EQ(stdc_bit_floor(0U), 0x5CU);
207+
EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
208+
EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
209+
}

libc/test/src/stdbit/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(prefixes
1313
count_ones
1414
has_single_bit
1515
bit_width
16+
bit_floor
1617
)
1718
set(suffixes c s i l ll)
1819
foreach(prefix IN LISTS prefixes)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Unittests for stdc_bit_floor_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_floor_uc.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitfloorUcTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(0U),
15+
static_cast<unsigned char>(0));
16+
}
17+
18+
TEST(LlvmLibcStdcBitfloorUcTest, Ones) {
19+
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
20+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(UCHAR_MAX >> i),
21+
static_cast<unsigned char>(1 << (UCHAR_WIDTH - i - 1)));
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Unittests for stdc_bit_floor_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_floor_ui.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
TEST(LlvmLibcStdcBitfloorUiTest, Zero) {
14+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(0U), 0U);
15+
}
16+
17+
TEST(LlvmLibcStdcBitfloorUiTest, Ones) {
18+
for (unsigned i = 0U; i != INT_WIDTH; ++i)
19+
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(UINT_MAX >> i),
20+
1U << (UINT_WIDTH - i - 1));
21+
}

0 commit comments

Comments
 (0)