From d5a3de4aeef4f4f1c52692533ddb9fdf45aef9d3 Mon Sep 17 00:00:00 2001 From: Nick Desaulniers Date: Tue, 6 Feb 2024 06:27:03 -0800 Subject: [PATCH] [libc][stdbit] implement stdc_trailing_zeros (C23) (#80344) --- libc/config/linux/x86_64/entrypoints.txt | 5 + libc/include/llvm-libc-macros/stdbit-macros.h | 22 +++ libc/spec/stdc.td | 11 +- libc/src/stdbit/CMakeLists.txt | 118 +++------------- libc/src/stdbit/stdc_trailing_zeros_uc.cpp | 20 +++ libc/src/stdbit/stdc_trailing_zeros_uc.h | 18 +++ libc/src/stdbit/stdc_trailing_zeros_ui.cpp | 20 +++ libc/src/stdbit/stdc_trailing_zeros_ui.h | 18 +++ libc/src/stdbit/stdc_trailing_zeros_ul.cpp | 20 +++ libc/src/stdbit/stdc_trailing_zeros_ul.h | 18 +++ libc/src/stdbit/stdc_trailing_zeros_ull.cpp | 21 +++ libc/src/stdbit/stdc_trailing_zeros_ull.h | 18 +++ libc/src/stdbit/stdc_trailing_zeros_us.cpp | 20 +++ libc/src/stdbit/stdc_trailing_zeros_us.h | 18 +++ libc/test/include/stdbit_test.cpp | 13 ++ libc/test/src/stdbit/CMakeLists.txt | 130 +++--------------- .../stdbit/stdc_trailing_zeros_uc_test.cpp | 21 +++ .../stdbit/stdc_trailing_zeros_ui_test.cpp | 21 +++ .../stdbit/stdc_trailing_zeros_ul_test.cpp | 21 +++ .../stdbit/stdc_trailing_zeros_ull_test.cpp | 21 +++ .../stdbit/stdc_trailing_zeros_us_test.cpp | 21 +++ 21 files changed, 384 insertions(+), 211 deletions(-) create mode 100644 libc/src/stdbit/stdc_trailing_zeros_uc.cpp create mode 100644 libc/src/stdbit/stdc_trailing_zeros_uc.h create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ui.cpp create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ui.h create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ul.cpp create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ul.h create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ull.cpp create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ull.h create mode 100644 libc/src/stdbit/stdc_trailing_zeros_us.cpp create mode 100644 libc/src/stdbit/stdc_trailing_zeros_us.h create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index a1236817d2d18d..2ac9a7444a1ecd 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -102,6 +102,11 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.stdbit.stdc_leading_ones_ui libc.src.stdbit.stdc_leading_ones_ul libc.src.stdbit.stdc_leading_ones_ull + libc.src.stdbit.stdc_trailing_zeros_uc + libc.src.stdbit.stdc_trailing_zeros_us + libc.src.stdbit.stdc_trailing_zeros_ui + libc.src.stdbit.stdc_trailing_zeros_ul + libc.src.stdbit.stdc_trailing_zeros_ull # stdlib.h entrypoints libc.src.stdlib.abs diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h index cc964a5268b0d7..15e391288357e5 100644 --- a/libc/include/llvm-libc-macros/stdbit-macros.h +++ b/libc/include/llvm-libc-macros/stdbit-macros.h @@ -40,6 +40,21 @@ inline unsigned stdc_leading_ones(unsigned long x) { inline unsigned stdc_leading_ones(unsigned long long x) { return stdc_leading_ones_ull(x); } +inline unsigned stdc_trailing_zeros(unsigned char x) { + return stdc_trailing_zeros_uc(x); +} +inline unsigned stdc_trailing_zeros(unsigned short x) { + return stdc_trailing_zeros_us(x); +} +inline unsigned stdc_trailing_zeros(unsigned x) { + return stdc_trailing_zeros_ui(x); +} +inline unsigned stdc_trailing_zeros(unsigned long x) { + return stdc_trailing_zeros_ul(x); +} +inline unsigned stdc_trailing_zeros(unsigned long long x) { + return stdc_trailing_zeros_ull(x); +} #else #define stdc_leading_zeros(x) \ _Generic((x), \ @@ -55,6 +70,13 @@ inline unsigned stdc_leading_ones(unsigned long long x) { unsigned: stdc_leading_ones_ui, \ unsigned long: stdc_leading_ones_ul, \ unsigned long long: stdc_leading_ones_ull)(x) +#define stdc_trailing_zeros(x) \ + _Generic((x), \ + unsigned char: stdc_trailing_zeros_uc, \ + unsigned short: stdc_trailing_zeros_us, \ + unsigned: stdc_trailing_zeros_ui, \ + unsigned long: stdc_trailing_zeros_ul, \ + unsigned long long: stdc_trailing_zeros_ull)(x) #endif // __cplusplus #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td index 607dda7c9041f8..3a162b0bc76f4e 100644 --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -775,7 +775,9 @@ def StdC : StandardSpec<"stdc"> { HeaderSpec StdBit = HeaderSpec< "stdbit.h", [ - Macro<"stdc_leading_zeros"> + Macro<"stdc_leading_zeros">, + Macro<"stdc_leading_ones">, + Macro<"stdc_trailing_zeros"> ], // Macros [], // Types [], // Enumerations @@ -789,7 +791,12 @@ def StdC : StandardSpec<"stdc"> { FunctionSpec<"stdc_leading_ones_us", RetValSpec, [ArgSpec]>, FunctionSpec<"stdc_leading_ones_ui", RetValSpec, [ArgSpec]>, FunctionSpec<"stdc_leading_ones_ul", RetValSpec, [ArgSpec]>, - FunctionSpec<"stdc_leading_ones_ull", RetValSpec, [ArgSpec]> + FunctionSpec<"stdc_leading_ones_ull", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_trailing_zeros_us", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec, [ArgSpec]>, + FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec, [ArgSpec]> ] // Functions >; diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt index d7daff44dfda6d..35df7f9c41ecbc 100644 --- a/libc/src/stdbit/CMakeLists.txt +++ b/libc/src/stdbit/CMakeLists.txt @@ -1,99 +1,19 @@ -add_entrypoint_object( - stdc_leading_zeros_uc - SRCS - stdc_leading_zeros_uc.cpp - HDRS - stdc_leading_zeros_uc.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_zeros_us - SRCS - stdc_leading_zeros_us.cpp - HDRS - stdc_leading_zeros_us.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_zeros_ui - SRCS - stdc_leading_zeros_ui.cpp - HDRS - stdc_leading_zeros_ui.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_zeros_ul - SRCS - stdc_leading_zeros_ul.cpp - HDRS - stdc_leading_zeros_ul.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_zeros_ull - SRCS - stdc_leading_zeros_ull.cpp - HDRS - stdc_leading_zeros_ull.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_ones_uc - SRCS - stdc_leading_ones_uc.cpp - HDRS - stdc_leading_ones_uc.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_ones_us - SRCS - stdc_leading_ones_us.cpp - HDRS - stdc_leading_ones_us.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_ones_ui - SRCS - stdc_leading_ones_ui.cpp - HDRS - stdc_leading_ones_ui.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_ones_ul - SRCS - stdc_leading_ones_ul.cpp - HDRS - stdc_leading_ones_ul.h - DEPENDS - libc.src.__support.CPP.bit -) - -add_entrypoint_object( - stdc_leading_ones_ull - SRCS - stdc_leading_ones_ull.cpp - HDRS - stdc_leading_ones_ull.h - DEPENDS - libc.src.__support.CPP.bit -) +set(prefixes + leading_zeros + leading_ones + trailing_zeros +) +set(suffixes c s i l ll) +foreach(prefix IN LISTS prefixes) + foreach(suffix IN LISTS suffixes) + add_entrypoint_object( + stdc_${prefix}_u${suffix} + SRCS + stdc_${prefix}_u${suffix}.cpp + HDRS + stdc_${prefix}_u${suffix}.h + DEPENDS + libc.src.__support.CPP.bit + ) + endforeach() +endforeach() diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.cpp b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp new file mode 100644 index 00000000000000..36924c5a053adc --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_uc.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_uc, (unsigned char value)) { + return static_cast(cpp::countr_zero(value)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.h b/libc/src/stdbit/stdc_trailing_zeros_uc.h new file mode 100644 index 00000000000000..866201e5acea81 --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_uc.h @@ -0,0 +1,18 @@ +//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_UC_H +#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H + +namespace LIBC_NAMESPACE { + +unsigned stdc_trailing_zeros_uc(unsigned char value); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.cpp b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp new file mode 100644 index 00000000000000..a264fd97f251f7 --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_ui.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ui, (unsigned value)) { + return static_cast(cpp::countr_zero(value)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.h b/libc/src/stdbit/stdc_trailing_zeros_ui.h new file mode 100644 index 00000000000000..0642e312f4fe73 --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ui.h @@ -0,0 +1,18 @@ +//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_UI_H +#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H + +namespace LIBC_NAMESPACE { + +unsigned stdc_trailing_zeros_ui(unsigned value); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H diff --git a/libc/src/stdbit/stdc_trailing_zeros_ul.cpp b/libc/src/stdbit/stdc_trailing_zeros_ul.cpp new file mode 100644 index 00000000000000..8e0c36cb099650 --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ul.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_ul.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ul, (unsigned long value)) { + return static_cast(cpp::countr_zero(value)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/stdbit/stdc_trailing_zeros_ul.h b/libc/src/stdbit/stdc_trailing_zeros_ul.h new file mode 100644 index 00000000000000..e10b4474753a0a --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ul.h @@ -0,0 +1,18 @@ +//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_UL_H +#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H + +namespace LIBC_NAMESPACE { + +unsigned stdc_trailing_zeros_ul(unsigned long value); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H diff --git a/libc/src/stdbit/stdc_trailing_zeros_ull.cpp b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp new file mode 100644 index 00000000000000..77cb20cb1ba45d --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp @@ -0,0 +1,21 @@ +//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_ull.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ull, + (unsigned long long value)) { + return static_cast(cpp::countr_zero(value)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/stdbit/stdc_trailing_zeros_ull.h b/libc/src/stdbit/stdc_trailing_zeros_ull.h new file mode 100644 index 00000000000000..f95169d29f45ed --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_ull.h @@ -0,0 +1,18 @@ +//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_ULL_H +#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H + +namespace LIBC_NAMESPACE { + +unsigned stdc_trailing_zeros_ull(unsigned long long value); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H diff --git a/libc/src/stdbit/stdc_trailing_zeros_us.cpp b/libc/src/stdbit/stdc_trailing_zeros_us.cpp new file mode 100644 index 00000000000000..a5b9f4a7d84985 --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_us.cpp @@ -0,0 +1,20 @@ +//===-- Implementation of stdc_trailing_zeros_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_trailing_zeros_us.h" + +#include "src/__support/CPP/bit.h" +#include "src/__support/common.h" + +namespace LIBC_NAMESPACE { + +LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_us, (unsigned short value)) { + return static_cast(cpp::countr_zero(value)); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/stdbit/stdc_trailing_zeros_us.h b/libc/src/stdbit/stdc_trailing_zeros_us.h new file mode 100644 index 00000000000000..ddbdf0d647abdd --- /dev/null +++ b/libc/src/stdbit/stdc_trailing_zeros_us.h @@ -0,0 +1,18 @@ +//===-- Implementation header for stdc_trailing_zeros_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_TRAILING_ZEROS_US_H +#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H + +namespace LIBC_NAMESPACE { + +unsigned stdc_trailing_zeros_us(unsigned short value); + +} // namespace LIBC_NAMESPACE + +#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp index 6e48b0f2f75944..858dc08bfd70c2 100644 --- a/libc/test/include/stdbit_test.cpp +++ b/libc/test/include/stdbit_test.cpp @@ -33,6 +33,11 @@ unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; } unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; } unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; } unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBFU; } +unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; } +unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; } +unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; } +unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; } +unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCFU; } } #include "include/llvm-libc-macros/stdbit-macros.h" @@ -52,3 +57,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) { EXPECT_EQ(stdc_leading_ones(0UL), 0xBDU); EXPECT_EQ(stdc_leading_ones(0ULL), 0xBFU); } + +TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) { + EXPECT_EQ(stdc_trailing_zeros(static_cast(0U)), 0xCAU); + EXPECT_EQ(stdc_trailing_zeros(static_cast(0U)), 0xCBU); + EXPECT_EQ(stdc_trailing_zeros(0U), 0xCCU); + EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU); + EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCFU); +} diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt index a8c3c9f883532d..64d66037d0f6ca 100644 --- a/libc/test/src/stdbit/CMakeLists.txt +++ b/libc/test/src/stdbit/CMakeLists.txt @@ -1,112 +1,22 @@ add_custom_target(libc-stdbit-tests) -add_libc_test( - stdc_leading_zeros_uc_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_zeros_uc_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_zeros_uc -) - -add_libc_test( - stdc_leading_zeros_us_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_zeros_us_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_zeros_us -) - -add_libc_test( - stdc_leading_zeros_ui_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_zeros_ui_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_zeros_ui -) - -add_libc_test( - stdc_leading_zeros_ul_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_zeros_ul_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_zeros_ul -) - -add_libc_test( - stdc_leading_zeros_ull_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_zeros_ull_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_zeros_ull -) - -add_libc_test( - stdc_leading_ones_uc_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_ones_uc_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_ones_uc -) - -add_libc_test( - stdc_leading_ones_us_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_ones_us_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_ones_us -) - -add_libc_test( - stdc_leading_ones_ui_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_ones_ui_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_ones_ui -) - -add_libc_test( - stdc_leading_ones_ul_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_ones_ul_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_ones_ul -) - -add_libc_test( - stdc_leading_ones_ull_test - SUITE - libc-stdbit-tests - SRCS - stdc_leading_ones_ull_test.cpp - DEPENDS - libc.src.__support.CPP.limits - libc.src.stdbit.stdc_leading_ones_ull -) - +set(prefixes + leading_zeros + leading_ones + trailing_zeros +) +set(suffixes c s i l ll) +foreach(prefix IN LISTS prefixes) + foreach(suffix IN LISTS suffixes) + add_libc_test( + stdc_${prefix}_u${suffix}_test + SUITE + libc-stdbit-tests + SRCS + stdc_${prefix}_u${suffix}_test.cpp + DEPENDS + libc.src.__support.CPP.limits + libc.src.stdbit.stdc_${prefix}_u${suffix} + ) + endforeach() +endforeach() diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp new file mode 100644 index 00000000000000..c02b518865d9f7 --- /dev/null +++ b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for stdc_trailing_zeros_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_trailing_zeros_uc.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcTrailingZerosUcTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_uc(0U), + static_cast(UCHAR_WIDTH)); +} + +TEST(LlvmLibcStdcTrailingZerosUcTest, OneHot) { + for (unsigned i = 0U; i != UCHAR_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_uc(1U << i), i); +} diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp new file mode 100644 index 00000000000000..ad9b126335172f --- /dev/null +++ b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for stdc_trailing_zeros_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_trailing_zeros_ui.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcTrailingZerosUiTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ui(0U), + static_cast(UINT_WIDTH)); +} + +TEST(LlvmLibcStdcTrailingZerosUiTest, OneHot) { + for (unsigned i = 0U; i != UINT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ui(1U << i), i); +} diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp new file mode 100644 index 00000000000000..6d7f4b3cb093df --- /dev/null +++ b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for stdc_trailing_zeros_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_trailing_zeros_ul.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcTrailingZerosUlTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ul(0U), + static_cast(ULONG_WIDTH)); +} + +TEST(LlvmLibcStdcTrailingZerosUlTest, OneHot) { + for (unsigned i = 0U; i != ULONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ul(1UL << i), i); +} diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp new file mode 100644 index 00000000000000..64b93b12e6051e --- /dev/null +++ b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for stdc_trailing_zeros_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_trailing_zeros_ull.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcTrailingZerosUllTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ull(0U), + static_cast(ULLONG_WIDTH)); +} + +TEST(LlvmLibcStdcTrailingZerosUllTest, OneHot) { + for (unsigned i = 0U; i != ULLONG_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ull(1ULL << i), i); +} diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp new file mode 100644 index 00000000000000..a9f8327dfd9140 --- /dev/null +++ b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp @@ -0,0 +1,21 @@ +//===-- Unittests for stdc_trailing_zeros_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_trailing_zeros_us.h" +#include "test/UnitTest/Test.h" + +TEST(LlvmLibcStdcTrailingZerosUsTest, Zero) { + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_us(0U), + static_cast(USHRT_WIDTH)); +} + +TEST(LlvmLibcStdcTrailingZerosUsTest, OneHot) { + for (unsigned i = 0U; i != USHRT_WIDTH; ++i) + EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_us(1U << i), i); +}