diff options
author | Nick Desaulniers <nickdesaulniers@users.noreply.github.com> | 2024-03-05 09:49:41 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-05 09:49:41 -0800 |
commit | 041638c4294a9a8375851e0add1ab2c99412c032 (patch) | |
tree | 474e938fff09ce3e6356762d0a4979ea3d327a3e | |
parent | 233f750c3dad14e330f5358e8dbcc6c30e805edb (diff) | |
download | llvm-041638c4294a9a8375851e0add1ab2c99412c032.zip llvm-041638c4294a9a8375851e0add1ab2c99412c032.tar.gz llvm-041638c4294a9a8375851e0add1ab2c99412c032.tar.bz2 |
[libc][stdbit] implement stdc_bit_width (C23) (#83892)
22 files changed, 344 insertions, 8 deletions
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index bc10512..a6c3041 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -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 diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst index b579e9dbb..ccd1393 100644 --- a/libc/docs/stdbit.rst +++ b/libc/docs/stdbit.rst @@ -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 @@ -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 ========================= ========= diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h index e3a36d1..104418c 100644 --- a/libc/include/llvm-libc-macros/stdbit-macros.h +++ b/libc/include/llvm-libc-macros/stdbit-macros.h @@ -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), \ @@ -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 diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index fc5a2f7..cfebc60 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -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 @@ -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 >; diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt index 8bc7dd7..f077bae 100644 --- a/libc/src/stdbit/CMakeLists.txt +++ b/libc/src/stdbit/CMakeLists.txt @@ -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) diff --git a/libc/src/stdbit/stdc_bit_width_uc.cpp b/libc/src/stdbit/stdc_bit_width_uc.cpp new file mode 100644 index 0000000..2c361c1 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_uc.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_uc.h b/libc/src/stdbit/stdc_bit_width_uc.h new file mode 100644 index 0000000..70c038a --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_uc.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ui.cpp b/libc/src/stdbit/stdc_bit_width_ui.cpp new file mode 100644 index 0000000..b94452b0 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ui.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ui.h b/libc/src/stdbit/stdc_bit_width_ui.h new file mode 100644 index 0000000..9e8de3d --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ui.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ul.cpp b/libc/src/stdbit/stdc_bit_width_ul.cpp new file mode 100644 index 0000000..8004431 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ul.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ul.h b/libc/src/stdbit/stdc_bit_width_ul.h new file mode 100644 index 0000000..447a291 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ul.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ull.cpp b/libc/src/stdbit/stdc_bit_width_ull.cpp new file mode 100644 index 0000000..006fa20 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ull.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_ull.h b/libc/src/stdbit/stdc_bit_width_ull.h new file mode 100644 index 0000000..bc51897f --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_ull.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_us.cpp b/libc/src/stdbit/stdc_bit_width_us.cpp new file mode 100644 index 0000000..3d9f72b --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_us.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_width_us.h b/libc/src/stdbit/stdc_bit_width_us.h new file mode 100644 index 0000000..02cd3742 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_width_us.h @@ -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 diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp index 16dcd8e..dfb7c97 100644 --- a/libc/test/include/stdbit_test.cpp +++ b/libc/test/include/stdbit_test.cpp @@ -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" @@ -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); +} diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt index a886ee4..f7e17d7 100644 --- a/libc/test/src/stdbit/CMakeLists.txt +++ b/libc/test/src/stdbit/CMakeLists.txt @@ -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) diff --git a/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp b/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp new file mode 100644 index 0000000..63c6503 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp @@ -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); +} diff --git a/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp new file mode 100644 index 0000000..43acdde --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp @@ -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); +} diff --git a/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp new file mode 100644 index 0000000..0a28694 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp @@ -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); +} diff --git a/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp new file mode 100644 index 0000000..31475f6 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_width_ull.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitWidthUllTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ull(0U), 0U); +} + +TEST(LlvmLibcStdcBitWidthUllTest, Ones) { + for (unsigned i = 0U; i != ULLONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ull(ULLONG_MAX >> i), + ULLONG_WIDTH - i); +} diff --git a/libc/test/src/stdbit/stdc_bit_width_us_test.cpp b/libc/test/src/stdbit/stdc_bit_width_us_test.cpp new file mode 100644 index 0000000..031c502 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_width_us_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_width_us.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitWidthUsTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_us(0U), 0U); +} + +TEST(LlvmLibcStdcBitWidthUsTest, Ones) { + for (unsigned i = 0U; i != USHRT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_us(USHRT_MAX >> i), + USHRT_WIDTH - i); +} |