[libc-commits] [libc] d5a3de4 - [libc][stdbit] implement stdc_trailing_zeros (C23) (#80344)

via libc-commits libc-commits at lists.llvm.org
Tue Feb 6 06:27:08 PST 2024


Author: Nick Desaulniers
Date: 2024-02-06T06:27:03-08:00
New Revision: d5a3de4aeef4f4f1c52692533ddb9fdf45aef9d3

URL: https://github.com/llvm/llvm-project/commit/d5a3de4aeef4f4f1c52692533ddb9fdf45aef9d3
DIFF: https://github.com/llvm/llvm-project/commit/d5a3de4aeef4f4f1c52692533ddb9fdf45aef9d3.diff

LOG: [libc][stdbit] implement stdc_trailing_zeros (C23) (#80344)

Added: 
    libc/src/stdbit/stdc_trailing_zeros_uc.cpp
    libc/src/stdbit/stdc_trailing_zeros_uc.h
    libc/src/stdbit/stdc_trailing_zeros_ui.cpp
    libc/src/stdbit/stdc_trailing_zeros_ui.h
    libc/src/stdbit/stdc_trailing_zeros_ul.cpp
    libc/src/stdbit/stdc_trailing_zeros_ul.h
    libc/src/stdbit/stdc_trailing_zeros_ull.cpp
    libc/src/stdbit/stdc_trailing_zeros_ull.h
    libc/src/stdbit/stdc_trailing_zeros_us.cpp
    libc/src/stdbit/stdc_trailing_zeros_us.h
    libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
    libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
    libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
    libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
    libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/llvm-libc-macros/stdbit-macros.h
    libc/spec/stdc.td
    libc/src/stdbit/CMakeLists.txt
    libc/test/include/stdbit_test.cpp
    libc/test/src/stdbit/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a1236817d2d18..2ac9a7444a1ec 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 cc964a5268b0d..15e391288357e 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 607dda7c9041f..3a162b0bc76f4 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<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 

diff  --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index d7daff44dfda6..35df7f9c41ecb 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 0000000000000..36924c5a053ad
--- /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<unsigned>(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 0000000000000..866201e5acea8
--- /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 0000000000000..a264fd97f251f
--- /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<unsigned>(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 0000000000000..0642e312f4fe7
--- /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 0000000000000..8e0c36cb09965
--- /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<unsigned>(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 0000000000000..e10b4474753a0
--- /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 0000000000000..77cb20cb1ba45
--- /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<unsigned>(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 0000000000000..f95169d29f45e
--- /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 0000000000000..a5b9f4a7d8498
--- /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<unsigned>(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 0000000000000..ddbdf0d647abd
--- /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 6e48b0f2f7594..858dc08bfd70c 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<unsigned char>(0U)), 0xCAU);
+  EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned short>(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 a8c3c9f883532..64d66037d0f6c 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 0000000000000..c02b518865d9f
--- /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<unsigned>(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 0000000000000..ad9b126335172
--- /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<unsigned>(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 0000000000000..6d7f4b3cb093d
--- /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<unsigned>(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 0000000000000..64b93b12e6051
--- /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<unsigned>(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 0000000000000..a9f8327dfd914
--- /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<unsigned>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUsTest, OneHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_us(1U << i), i);
+}


        


More information about the libc-commits mailing list