Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 6, 2024

Conversation

nickdesaulniers
Copy link
Member

No description provided.

@llvmbot llvmbot added the libc label Feb 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 1, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Patch is 20.91 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/80344.diff

20 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+5)
  • (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+22)
  • (modified) libc/src/stdbit/CMakeLists.txt (+50)
  • (added) libc/src/stdbit/stdc_trailing_zeros_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_uc.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ui.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ul.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ull.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_ull.h (+18)
  • (added) libc/src/stdbit/stdc_trailing_zeros_us.cpp (+20)
  • (added) libc/src/stdbit/stdc_trailing_zeros_us.h (+18)
  • (modified) libc/test/include/stdbit_test.cpp (+13)
  • (modified) libc/test/src/stdbit/CMakeLists.txt (+55)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp (+21)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9946b93c346ce..3ba676fc496ec 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..a36d221b3c835 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/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index d7daff44dfda6..1c9f1b90281cd 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -97,3 +97,53 @@ add_entrypoint_object(
   DEPENDS
     libc.src.__support.CPP.bit
 )
+
+add_entrypoint_object(
+  stdc_trailing_zeros_uc
+  SRCS
+    stdc_trailing_zeros_uc.cpp
+  HDRS
+    stdc_trailing_zeros_uc.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_trailing_zeros_us
+  SRCS
+    stdc_trailing_zeros_us.cpp
+  HDRS
+    stdc_trailing_zeros_us.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_trailing_zeros_ui
+  SRCS
+    stdc_trailing_zeros_ui.cpp
+  HDRS
+    stdc_trailing_zeros_ui.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_trailing_zeros_ul
+  SRCS
+    stdc_trailing_zeros_ul.cpp
+  HDRS
+    stdc_trailing_zeros_ul.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_trailing_zeros_ull
+  SRCS
+    stdc_trailing_zeros_ull.cpp
+  HDRS
+    stdc_trailing_zeros_ull.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
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..9e61eccf2e4ef
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
@@ -0,0 +1,20 @@
+//===-- 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..8a7a227eea47f 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -110,3 +110,58 @@ add_libc_test(
     libc.src.stdbit.stdc_leading_ones_ull
 )
 
+add_libc_test(
+  stdc_trailing_zeros_uc_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_trailing_zeros_uc_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.limits
+    libc.src.stdbit.stdc_trailing_zeros_uc
+)
+
+add_libc_test(
+  stdc_trailing_zeros_us_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_trailing_zeros_us_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.limits
+    libc.src.stdbit.stdc_trailing_zeros_us
+)
+
+add_libc_test(
+  stdc_trailing_zeros_ui_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_trailing_zeros_ui_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.limits
+    libc.src.stdbit.stdc_trailing_zeros_ui
+)
+
+add_libc_test(
+  stdc_trailing_zeros_ul_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_trailing_zeros_ul_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.limits
+    libc.src.stdbit.stdc_trailing_zeros_ul
+)
+
+add_libc_test(
+  stdc_trailing_zeros_ull_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_trailing_zeros_ull_test.cpp
+  DEPENDS
+    libc.src.__support.CPP.limits
+    libc.src.stdbit.stdc_trailing_zeros_ull
+)
+
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..725b276d9de4c
--- /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..1ed7e66bf9097
--- /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..c46d48b49ec43
--- /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..e925b785dc57a
--- /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>(LLONG_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..6e56...
[truncated]

Copy link

github-actions bot commented Feb 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@nickdesaulniers
Copy link
Member Author

update docs!

@nickdesaulniers nickdesaulniers marked this pull request as draft February 1, 2024 23:52
@nickdesaulniers
Copy link
Member Author

needs to update libc/spec/stdc.td, too.

@nickdesaulniers nickdesaulniers marked this pull request as ready for review February 2, 2024 16:21
@nickdesaulniers nickdesaulniers marked this pull request as draft February 2, 2024 17:32
@nickdesaulniers nickdesaulniers marked this pull request as ready for review February 2, 2024 19:31
libc.src.stdbit.stdc_leading_ones_ull
)

list(APPEND prefixes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should set(prefixes ...) and set(suffixes ...) instead of appending, just in case they were set somewhere before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5d94dd3 PTAL

DEPENDS
libc.src.__support.CPP.bit
)
list(APPEND prefixes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should set(prefixes ...) and set(suffixes ...) instead of appending, just in case they were set somewhere before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5d94dd3 PTAL

@nickdesaulniers nickdesaulniers merged commit d5a3de4 into llvm:main Feb 6, 2024
@nickdesaulniers nickdesaulniers deleted the trailing_zeros branch February 6, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants