Skip to content

Commit

Permalink
[libc][stdbit] implement stdc_bit_width (C23) (llvm#83892)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdesaulniers authored Mar 5, 2024
1 parent 233f750 commit 041638c
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 8 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 @@ -147,6 +147,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_has_single_bit_ui
libc.src.stdbit.stdc_has_single_bit_ul
libc.src.stdbit.stdc_has_single_bit_ull
libc.src.stdbit.stdc_bit_width_uc
libc.src.stdbit.stdc_bit_width_us
libc.src.stdbit.stdc_bit_width_ui
libc.src.stdbit.stdc_bit_width_ul
libc.src.stdbit.stdc_bit_width_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 @@ -86,11 +86,11 @@ stdc_has_single_bit_us |check|
stdc_has_single_bit_ui |check|
stdc_has_single_bit_ul |check|
stdc_has_single_bit_ull |check|
stdc_bit_width_uc
stdc_bit_width_us
stdc_bit_width_ui
stdc_bit_width_ul
stdc_bit_width_ull
stdc_bit_width_uc |check|
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
Expand Down Expand Up @@ -125,7 +125,7 @@ stdc_first_trailing_one |check|
stdc_count_zeros |check|
stdc_count_ones |check|
stdc_has_single_bit |check|
stdc_bit_width
stdc_bit_width |check|
stdc_bit_floor
stdc_bit_ceil
========================= =========
Expand Down
16 changes: 16 additions & 0 deletions libc/include/llvm-libc-macros/stdbit-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ inline bool stdc_has_single_bit(unsigned long x) {
inline bool stdc_has_single_bit(unsigned long long x) {
return stdc_has_single_bit_ull(x);
}
inline unsigned stdc_bit_width(unsigned char x) { return stdc_bit_width_uc(x); }
inline unsigned stdc_bit_width(unsigned short x) {
return stdc_bit_width_us(x);
}
inline unsigned stdc_bit_width(unsigned x) { return stdc_bit_width_ui(x); }
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);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -250,6 +259,13 @@ inline bool stdc_has_single_bit(unsigned long long x) {
unsigned: stdc_has_single_bit_ui, \
unsigned long: stdc_has_single_bit_ul, \
unsigned long long: stdc_has_single_bit_ull)(x)
#define stdc_bit_width(x) \
_Generic((x), \
unsigned char: stdc_bit_width_ui, \
unsigned short: stdc_bit_width_us, \
unsigned: stdc_bit_width_ui, \
unsigned long: stdc_bit_width_ul, \
unsigned long long: stdc_bit_width_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 @@ -800,7 +800,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_first_trailing_one">,
Macro<"stdc_count_zeros">,
Macro<"stdc_count_ones">,
Macro<"stdc_has_single_bit">
Macro<"stdc_has_single_bit">,
Macro<"std_bit_width">
], // Macros
[], // Types
[], // Enumerations
Expand Down Expand Up @@ -854,7 +855,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_has_single_bit_us", RetValSpec<BoolType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_has_single_bit_ui", RetValSpec<BoolType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_has_single_bit_ul", RetValSpec<BoolType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_bit_width_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
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>]>
] // Functions
>;

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 @@ -10,6 +10,7 @@ set(prefixes
count_zeros
count_ones
has_single_bit
bit_width
)
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_width_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_bit_width_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_width_uc.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_uc, (unsigned char value)) {
return static_cast<unsigned>(cpp::bit_width(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_bit_width_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::bit_width(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_bit_width_ui(unsigned value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ul, (unsigned long value)) {
return static_cast<unsigned>(cpp::bit_width(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_bit_width_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ull, (unsigned long long value)) {
return static_cast<unsigned>(cpp::bit_width(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_bit_width_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_us, (unsigned short value)) {
return static_cast<unsigned>(cpp::bit_width(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_bit_width_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_US_H
13 changes: 13 additions & 0 deletions libc/test/include/stdbit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
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; }
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -177,3 +182,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroHasSingleBit) {
EXPECT_EQ(stdc_has_single_bit(1UL), false);
EXPECT_EQ(stdc_has_single_bit(1ULL), false);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
EXPECT_EQ(stdc_bit_width(static_cast<unsigned char>(1U)), 0x4AU);
EXPECT_EQ(stdc_bit_width(static_cast<unsigned short>(1U)), 0x4BU);
EXPECT_EQ(stdc_bit_width(1U), 0x4CU);
EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
}
1 change: 1 addition & 0 deletions libc/test/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(prefixes
count_zeros
count_ones
has_single_bit
bit_width
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
21 changes: 21 additions & 0 deletions libc/test/src/stdbit/stdc_bit_width_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_bit_width_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_width_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitWidthUcTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(0U), 0U);
}

TEST(LlvmLibcStdcBitWidthUcTest, Ones) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(UCHAR_MAX >> i),
UCHAR_WIDTH - i);
}
20 changes: 20 additions & 0 deletions libc/test/src/stdbit/stdc_bit_width_ui_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Unittests for stdc_bit_width_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_width_ui.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitWidthUiTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(0U), 0U);
}

TEST(LlvmLibcStdcBitWidthUiTest, Ones) {
for (unsigned i = 0U; i != UINT_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(UINT_MAX >> i), UINT_WIDTH - i);
}
21 changes: 21 additions & 0 deletions libc/test/src/stdbit/stdc_bit_width_ul_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_bit_width_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/__support/CPP/limits.h"
#include "src/stdbit/stdc_bit_width_ul.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcBitWidthUlTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(0U), 0U);
}

TEST(LlvmLibcStdcBitWidthUlTest, Ones) {
for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(ULONG_MAX >> i),
ULONG_WIDTH - i);
}
Loading

0 comments on commit 041638c

Please sign in to comment.