diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index a7894af4b9ca6..b51227e5f25d7 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -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 diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst index 3ec46cf8d8ffa..9b4974cf1479b 100644 --- a/libc/docs/stdbit.rst +++ b/libc/docs/stdbit.rst @@ -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| ============================ ========= @@ -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 diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h index e3eb6630afddb..10c0fac3c8dd8 100644 --- a/libc/include/llvm-libc-macros/stdbit-macros.h +++ b/libc/include/llvm-libc-macros/stdbit-macros.h @@ -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), \ @@ -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 diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 766668c51e3e0..d91f5c1f72334 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -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 @@ -880,7 +881,12 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"stdc_bit_floor_us", RetValSpec, [ArgSpec]>, FunctionSpec<"stdc_bit_floor_ui", RetValSpec, [ArgSpec]>, FunctionSpec<"stdc_bit_floor_ul", RetValSpec, [ArgSpec]>, - FunctionSpec<"stdc_bit_floor_ull", RetValSpec, [ArgSpec]> + FunctionSpec<"stdc_bit_floor_ull", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_bit_ceil_uc", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_bit_ceil_us", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_bit_ceil_ui", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_bit_ceil_ul", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_bit_ceil_ull", RetValSpec, [ArgSpec]> ] // Functions >; diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h index 9c74a346949f0..4464703e4b064 100644 --- a/libc/src/__support/CPP/bit.h +++ b/libc/src/__support/CPP/bit.h @@ -193,7 +193,7 @@ template bit_ceil(T value) { if (value < 2) return 1; - return T(1) << cpp::bit_width(value - 1u); + return static_cast(T(1) << cpp::bit_width(value - 1u)); } // Rotate algorithms make use of "Safe, Efficient, and Portable Rotate in C/C++" diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt index 7ab4fee4454a1..2aef2029f2df0 100644 --- a/libc/src/stdbit/CMakeLists.txt +++ b/libc/src/stdbit/CMakeLists.txt @@ -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) diff --git a/libc/src/stdbit/stdc_bit_ceil_uc.cpp b/libc/src/stdbit/stdc_bit_ceil_uc.cpp new file mode 100644 index 0000000000000..675ae4a0edb02 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_uc.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_uc.h b/libc/src/stdbit/stdc_bit_ceil_uc.h new file mode 100644 index 0000000000000..204261e410812 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_uc.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ui.cpp b/libc/src/stdbit/stdc_bit_ceil_ui.cpp new file mode 100644 index 0000000000000..a8ac9726179bc --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ui.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ui.h b/libc/src/stdbit/stdc_bit_ceil_ui.h new file mode 100644 index 0000000000000..db66c336e3662 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ui.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ul.cpp b/libc/src/stdbit/stdc_bit_ceil_ul.cpp new file mode 100644 index 0000000000000..18a9c38b5b4cf --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ul.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ul.h b/libc/src/stdbit/stdc_bit_ceil_ul.h new file mode 100644 index 0000000000000..f8393a42fcbfc --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ul.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ull.cpp b/libc/src/stdbit/stdc_bit_ceil_ull.cpp new file mode 100644 index 0000000000000..0989f36ab7685 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ull.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_ull.h b/libc/src/stdbit/stdc_bit_ceil_ull.h new file mode 100644 index 0000000000000..e65f537efb172 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_ull.h @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_us.cpp b/libc/src/stdbit/stdc_bit_ceil_us.cpp new file mode 100644 index 0000000000000..f86a216bb840e --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_us.cpp @@ -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 diff --git a/libc/src/stdbit/stdc_bit_ceil_us.h b/libc/src/stdbit/stdc_bit_ceil_us.h new file mode 100644 index 0000000000000..16a14e51b7433 --- /dev/null +++ b/libc/src/stdbit/stdc_bit_ceil_us.h @@ -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 diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp index 20820d52fbded..6c12665c4454d 100644 --- a/libc/test/include/stdbit_test.cpp +++ b/libc/test/include/stdbit_test.cpp @@ -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" @@ -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(0U)), + static_cast(0x6AU)); + EXPECT_EQ(stdc_bit_ceil(static_cast(0U)), + static_cast(0x6BU)); + EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU); + EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL); + EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL); +} diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt index 3aed56c0e9238..c3f8059d2d9b2 100644 --- a/libc/test/src/stdbit/CMakeLists.txt +++ b/libc/test/src/stdbit/CMakeLists.txt @@ -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) diff --git a/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp new file mode 100644 index 0000000000000..1ef87b0d44de6 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp @@ -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(1)); +} + +TEST(LlvmLibcStdcBitceilUcTest, Ones) { + for (unsigned i = 0U; i != UCHAR_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(1U << i), + static_cast(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(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(1U << (i + 1))); +} diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp new file mode 100644 index 0000000000000..3b6f2a564ff15 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp @@ -0,0 +1,30 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_ceil_ui.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitceilUiTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(0U), 1U); +} + +TEST(LlvmLibcStdcBitceilUiTest, Ones) { + for (unsigned i = 0U; i != UINT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(1U << i), 1U << i); +} + +TEST(LlvmLibcStdcBitceilUiTest, OneLessThanPowsTwo) { + for (unsigned i = 2U; i != UINT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) - 1), 1U << i); +} + +TEST(LlvmLibcStdcBitceilUiTest, OneMoreThanPowsTwo) { + for (unsigned i = 1U; i != UINT_WIDTH - 1; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) + 1), 1U << (i + 1)); +} diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp new file mode 100644 index 0000000000000..d4dbb38ea02af --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp @@ -0,0 +1,30 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_ceil_ul.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitceilUlTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(0UL), 1UL); +} + +TEST(LlvmLibcStdcBitceilUlTest, Ones) { + for (unsigned i = 0U; i != ULONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(1UL << i), 1UL << i); +} + +TEST(LlvmLibcStdcBitceilUlTest, OneLessThanPowsTwo) { + for (unsigned i = 2U; i != ULONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) - 1), 1UL << i); +} + +TEST(LlvmLibcStdcBitceilUlTest, OneMoreThanPowsTwo) { + for (unsigned i = 1U; i != ULONG_WIDTH - 1; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) + 1), 1UL << (i + 1)); +} diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp new file mode 100644 index 0000000000000..762f4f0627e6a --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp @@ -0,0 +1,31 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_ceil_ull.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitceilUllTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(0ULL), 1ULL); +} + +TEST(LlvmLibcStdcBitceilUllTest, Ones) { + for (unsigned i = 0U; i != ULLONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(1ULL << i), 1ULL << i); +} + +TEST(LlvmLibcStdcBitceilUllTest, OneLessThanPowsTwo) { + for (unsigned i = 2U; i != ULLONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) - 1), 1ULL << i); +} + +TEST(LlvmLibcStdcBitceilUllTest, OneMoreThanPowsTwo) { + for (unsigned i = 1U; i != ULLONG_WIDTH - 1; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) + 1), + 1ULL << (i + 1)); +} diff --git a/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp new file mode 100644 index 0000000000000..56873c51828f1 --- /dev/null +++ b/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp @@ -0,0 +1,34 @@ +//===-- Unittests for 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/__support/CPP/limits.h" +#include "src/stdbit/stdc_bit_ceil_us.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcBitceilUsTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us(0U), + static_cast(1)); +} + +TEST(LlvmLibcStdcBitceilUsTest, Ones) { + for (unsigned i = 0U; i != USHRT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us(1U << i), + static_cast(1U << i)); +} + +TEST(LlvmLibcStdcBitceilUsTest, OneLessThanPowsTwo) { + for (unsigned i = 2U; i != USHRT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us((1U << i) - 1), + static_cast(1U << i)); +} + +TEST(LlvmLibcStdcBitceilUsTest, OneMoreThanPowsTwo) { + for (unsigned i = 1U; i != USHRT_WIDTH - 1; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us((1U << i) + 1), + static_cast(1U << (i + 1))); +}