Skip to content

Commit

Permalink
[libc][stdbit][c23] adds implementation of stdc_bit_ceil functions (l…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Flanders authored Mar 10, 2024
1 parent 862c7e0 commit 75b0d38
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 9 deletions.
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_bit_floor_ui
libc.src.stdbit.stdc_bit_floor_ul
libc.src.stdbit.stdc_bit_floor_ull
libc.src.stdbit.stdc_bit_ceil_uc
libc.src.stdbit.stdc_bit_ceil_us
libc.src.stdbit.stdc_bit_ceil_ui
libc.src.stdbit.stdc_bit_ceil_ul
libc.src.stdbit.stdc_bit_ceil_ull

# stdlib.h entrypoints
libc.src.stdlib.abs
Expand Down
12 changes: 6 additions & 6 deletions libc/docs/stdbit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ stdc_bit_floor_us |check|
stdc_bit_floor_ui |check|
stdc_bit_floor_ul |check|
stdc_bit_floor_ull |check|
stdc_bit_ceil_uc
stdc_bit_ceil_us
stdc_bit_ceil_ui
stdc_bit_ceil_ul
stdc_bit_ceil_ull
stdc_bit_ceil_uc |check|
stdc_bit_ceil_us |check|
stdc_bit_ceil_ui |check|
stdc_bit_ceil_ul |check|
stdc_bit_ceil_ull |check|
============================ =========


Expand All @@ -127,7 +127,7 @@ stdc_count_ones |check|
stdc_has_single_bit |check|
stdc_bit_width |check|
stdc_bit_floor |check|
stdc_bit_ceil
stdc_bit_ceil |check|
========================= =========

Standards
Expand Down
20 changes: 20 additions & 0 deletions libc/include/llvm-libc-macros/stdbit-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ inline unsigned long stdc_bit_floor(unsigned long x) {
inline unsigned long long stdc_bit_floor(unsigned long long x) {
return stdc_bit_floor_ull(x);
}
inline unsigned char stdc_bit_ceil(unsigned char x) {
return stdc_bit_ceil_uc(x);
}
inline unsigned short stdc_bit_ceil(unsigned short x) {
return stdc_bit_ceil_us(x);
}
inline unsigned stdc_bit_ceil(unsigned x) { return stdc_bit_ceil_ui(x); }
inline unsigned long stdc_bit_ceil(unsigned long x) {
return stdc_bit_ceil_ul(x);
}
inline unsigned long long stdc_bit_ceil(unsigned long long x) {
return stdc_bit_ceil_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -286,6 +299,13 @@ inline unsigned long long stdc_bit_floor(unsigned long long x) {
unsigned: stdc_bit_floor_ui, \
unsigned long: stdc_bit_floor_ul, \
unsigned long long: stdc_bit_floor_ull)(x)
#define stdc_bit_ceil(x) \
_Generic((x), \
unsigned char: stdc_bit_ceil_uc, \
unsigned short: stdc_bit_ceil_us, \
unsigned: stdc_bit_ceil_ui, \
unsigned long: stdc_bit_ceil_ul, \
unsigned long long: stdc_bit_ceil_ull)(x)
#endif // __cplusplus

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
10 changes: 8 additions & 2 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_count_ones">,
Macro<"stdc_has_single_bit">,
Macro<"std_bit_width">,
Macro<"std_bit_floor">
Macro<"std_bit_floor">,
Macro<"std_bit_ceil">
], // Macros
[], // Types
[], // Enumerations
Expand Down Expand Up @@ -880,7 +881,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_bit_floor_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_bit_floor_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_bit_floor_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_bit_ceil_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_bit_ceil_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_bit_ceil_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_bit_ceil_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_bit_ceil_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ template <typename T>
bit_ceil(T value) {
if (value < 2)
return 1;
return T(1) << cpp::bit_width<T>(value - 1u);
return static_cast<T>(T(1) << cpp::bit_width<T>(value - 1u));
}

// Rotate algorithms make use of "Safe, Efficient, and Portable Rotate in C/C++"
Expand Down
1 change: 1 addition & 0 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(prefixes
has_single_bit
bit_width
bit_floor
bit_ceil
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_ceil_uc --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_bit_ceil_uc.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned char, stdc_bit_ceil_uc, (unsigned char value)) {
return cpp::bit_ceil(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_uc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_ceil_uc --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H

namespace LIBC_NAMESPACE {

unsigned char stdc_bit_ceil_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_ceil_ui --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_bit_ceil_ui.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_ceil_ui, (unsigned value)) {
return cpp::bit_ceil(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_ceil_ui --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H

namespace LIBC_NAMESPACE {

unsigned stdc_bit_ceil_ui(unsigned value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_ceil_ul --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_bit_ceil_ul.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned long, stdc_bit_ceil_ul, (unsigned long value)) {
return cpp::bit_ceil(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_ceil_ul --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H

namespace LIBC_NAMESPACE {

unsigned long stdc_bit_ceil_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_bit_ceil_ull -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_bit_ceil_ull.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned long long, stdc_bit_ceil_ull,
(unsigned long long value)) {
return cpp::bit_ceil(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_ull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_ceil_ull -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H

namespace LIBC_NAMESPACE {

unsigned long long stdc_bit_ceil_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_us.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_ceil_us --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_bit_ceil_us.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned short, stdc_bit_ceil_us, (unsigned short value)) {
return cpp::bit_ceil(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_ceil_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_ceil_us --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H

namespace LIBC_NAMESPACE {

unsigned short stdc_bit_ceil_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H
17 changes: 17 additions & 0 deletions libc/test/include/stdbit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
return 0x5EU;
}
unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
return 0x6EU;
}
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -207,3 +214,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned char>(0U)),
static_cast<unsigned char>(0x6AU));
EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned short>(0U)),
static_cast<unsigned short>(0x6BU));
EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU);
EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
}
1 change: 1 addition & 0 deletions libc/test/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(prefixes
has_single_bit
bit_width
bit_floor
bit_ceil
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
34 changes: 34 additions & 0 deletions libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===-- Unittests for stdc_bit_ceil_uc ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/stdbit/stdc_bit_ceil_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitceilUcTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(0U),
static_cast<unsigned char>(1));
}

TEST(LlvmLibcStdcBitceilUcTest, Ones) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(1U << i),
static_cast<unsigned char>(1U << i));
}

TEST(LlvmLibcStdcBitceilUcTest, OneLessThanPowsTwo) {
for (unsigned i = 2U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) - 1),
static_cast<unsigned char>(1U << i));
}

TEST(LlvmLibcStdcBitceilUcTest, OneMoreThanPowsTwo) {
for (unsigned i = 1U; i != UCHAR_WIDTH - 1; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) + 1),
static_cast<unsigned char>(1U << (i + 1)));
}
Loading

0 comments on commit 75b0d38

Please sign in to comment.