Skip to content

[libc][stdbit] implement stdc_bit_floor (C23) #84233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -152,6 +152,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_bit_width_ui
libc.src.stdbit.stdc_bit_width_ul
libc.src.stdbit.stdc_bit_width_ull
libc.src.stdbit.stdc_bit_floor_uc
libc.src.stdbit.stdc_bit_floor_us
libc.src.stdbit.stdc_bit_floor_ui
libc.src.stdbit.stdc_bit_floor_ul
libc.src.stdbit.stdc_bit_floor_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 @@ -91,11 +91,11 @@ stdc_bit_width_us |check|
stdc_bit_width_ui |check|
stdc_bit_width_ul |check|
stdc_bit_width_ull |check|
stdc_bit_floor_uc
stdc_bit_floor_us
stdc_bit_floor_ui
stdc_bit_floor_ul
stdc_bit_floor_ull
stdc_bit_floor_uc |check|
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
Expand Down Expand Up @@ -126,7 +126,7 @@ stdc_count_zeros |check|
stdc_count_ones |check|
stdc_has_single_bit |check|
stdc_bit_width |check|
stdc_bit_floor
stdc_bit_floor |check|
stdc_bit_ceil
========================= =========

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 @@ -181,6 +181,19 @@ inline unsigned stdc_bit_width(unsigned long x) { return stdc_bit_width_ul(x); }
inline unsigned stdc_bit_width(unsigned long long x) {
return stdc_bit_width_ull(x);
}
inline unsigned char stdc_bit_floor(unsigned char x) {
return stdc_bit_floor_uc(x);
}
inline unsigned short stdc_bit_floor(unsigned short x) {
return stdc_bit_floor_us(x);
}
inline unsigned stdc_bit_floor(unsigned x) { return stdc_bit_floor_ui(x); }
inline unsigned long stdc_bit_floor(unsigned long x) {
return stdc_bit_floor_ul(x);
}
inline unsigned long long stdc_bit_floor(unsigned long long x) {
return stdc_bit_floor_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -266,6 +279,13 @@ inline unsigned stdc_bit_width(unsigned long long x) {
unsigned: stdc_bit_width_ui, \
unsigned long: stdc_bit_width_ul, \
unsigned long long: stdc_bit_width_ull)(x)
#define stdc_bit_floor(x) \
_Generic((x), \
unsigned char: stdc_bit_floor_ui, \
unsigned short: stdc_bit_floor_us, \
unsigned: stdc_bit_floor_ui, \
unsigned long: stdc_bit_floor_ul, \
unsigned long long: stdc_bit_floor_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 @@ -801,7 +801,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_count_zeros">,
Macro<"stdc_count_ones">,
Macro<"stdc_has_single_bit">,
Macro<"std_bit_width">
Macro<"std_bit_width">,
Macro<"std_bit_floor">
], // Macros
[], // Types
[], // Enumerations
Expand Down Expand Up @@ -860,7 +861,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_bit_width_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_bit_width_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_bit_width_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_bit_floor_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
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>]>
] // 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 @@ -169,7 +169,7 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
[[nodiscard]] LIBC_INLINE constexpr T bit_floor(T value) {
if (!value)
return 0;
return T(1) << (cpp::bit_width(value) - 1);
return static_cast<T>(T(1) << (cpp::bit_width(value) - 1));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: without this change, I observe an integer truncation warning (promoted to error) for types smaller than int. They undergo implicit promotion from the binary operator <<.

}

/// Returns the smallest integral power of two no smaller than value if value is
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 @@ -11,6 +11,7 @@ set(prefixes
count_ones
has_single_bit
bit_width
bit_floor
)
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_floor_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_floor_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_floor_uc.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned char, stdc_bit_floor_uc, (unsigned char value)) {
return cpp::bit_floor(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_uc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UC_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UC_H

namespace LIBC_NAMESPACE {

unsigned char stdc_bit_floor_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UC_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_floor_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_floor_ui.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_floor_ui, (unsigned value)) {
return cpp::bit_floor(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UI_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H

namespace LIBC_NAMESPACE {

unsigned stdc_bit_floor_ui(unsigned value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_floor_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_floor_ul.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned long, stdc_bit_floor_ul, (unsigned long value)) {
return cpp::bit_floor(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_floor_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_FLOOR_UL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H

namespace LIBC_NAMESPACE {

unsigned long stdc_bit_floor_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_bit_floor_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_floor_ull.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned long long, stdc_bit_floor_ull,
(unsigned long long value)) {
return cpp::bit_floor(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_ull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_floor_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_FLOOR_ULL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H

namespace LIBC_NAMESPACE {

unsigned long long stdc_bit_floor_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_us.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_floor_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_floor_us.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned short, stdc_bit_floor_us, (unsigned short value)) {
return cpp::bit_floor(value);
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_bit_floor_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_bit_floor_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_FLOOR_US_H
#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_US_H

namespace LIBC_NAMESPACE {

unsigned short stdc_bit_floor_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_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 @@ -91,6 +91,13 @@ unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
return 0x5EU;
}
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -190,3 +197,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned char>(0U)),
static_cast<unsigned char>(0x5AU));
EXPECT_EQ(stdc_bit_floor(static_cast<unsigned short>(0U)),
static_cast<unsigned short>(0x5BU));
EXPECT_EQ(stdc_bit_floor(0U), 0x5CU);
EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
}
1 change: 1 addition & 0 deletions libc/test/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(prefixes
count_ones
has_single_bit
bit_width
bit_floor
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
22 changes: 22 additions & 0 deletions libc/test/src/stdbit/stdc_bit_floor_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Unittests for stdc_bit_floor_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_floor_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitfloorUcTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(0U),
static_cast<unsigned char>(0));
}

TEST(LlvmLibcStdcBitfloorUcTest, Ones) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(UCHAR_MAX >> i),
static_cast<unsigned char>(1 << (UCHAR_WIDTH - i - 1)));
}
21 changes: 21 additions & 0 deletions libc/test/src/stdbit/stdc_bit_floor_ui_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_bit_floor_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/__support/CPP/limits.h"
#include "src/stdbit/stdc_bit_floor_ui.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitfloorUiTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(0U), 0U);
}

TEST(LlvmLibcStdcBitfloorUiTest, Ones) {
for (unsigned i = 0U; i != INT_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(UINT_MAX >> i),
1U << (UINT_WIDTH - i - 1));
}
Loading