[libc-commits] [libc] [libc][stdbit] implement stdc_trailing_ones (C23) (PR #80459)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Feb 6 06:56:23 PST 2024


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/80459

>From 0e30138877ecf4920ba97240d8d23e7e86e6ad6f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 1 Feb 2024 16:00:24 -0800
Subject: [PATCH 1/3] [libc][stdbit] implement stdc_trailing_ones (C23)

---
 libc/config/linux/x86_64/entrypoints.txt      |  5 +++++
 libc/include/llvm-libc-macros/stdbit-macros.h | 22 +++++++++++++++++++
 libc/spec/stdc.td                             |  7 +++++-
 libc/src/stdbit/CMakeLists.txt                |  1 +
 libc/src/stdbit/stdc_trailing_ones_uc.cpp     | 20 +++++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_uc.h       | 18 +++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ui.cpp     | 20 +++++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ui.h       | 18 +++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ul.cpp     | 20 +++++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ul.h       | 18 +++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ull.cpp    | 21 ++++++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_ull.h      | 18 +++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_us.cpp     | 20 +++++++++++++++++
 libc/src/stdbit/stdc_trailing_ones_us.h       | 18 +++++++++++++++
 libc/test/include/stdbit_test.cpp             | 13 +++++++++++
 libc/test/src/stdbit/CMakeLists.txt           |  1 +
 .../src/stdbit/stdc_trailing_ones_uc_test.cpp | 21 ++++++++++++++++++
 .../src/stdbit/stdc_trailing_ones_ui_test.cpp | 21 ++++++++++++++++++
 .../src/stdbit/stdc_trailing_ones_ul_test.cpp | 21 ++++++++++++++++++
 .../stdbit/stdc_trailing_ones_ull_test.cpp    | 21 ++++++++++++++++++
 .../src/stdbit/stdc_trailing_ones_us_test.cpp | 21 ++++++++++++++++++
 21 files changed, 344 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_uc.cpp
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_uc.h
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ui.cpp
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ui.h
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ul.cpp
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ul.h
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ull.cpp
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_ull.h
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_us.cpp
 create mode 100644 libc/src/stdbit/stdc_trailing_ones_us.h
 create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2ac9a7444a1ec..b35fc9fc6866b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -107,6 +107,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_trailing_zeros_ui
     libc.src.stdbit.stdc_trailing_zeros_ul
     libc.src.stdbit.stdc_trailing_zeros_ull
+    libc.src.stdbit.stdc_trailing_ones_uc
+    libc.src.stdbit.stdc_trailing_ones_us
+    libc.src.stdbit.stdc_trailing_ones_ui
+    libc.src.stdbit.stdc_trailing_ones_ul
+    libc.src.stdbit.stdc_trailing_ones_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 15e391288357e..d58ab7127cbdb 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -55,6 +55,21 @@ inline unsigned stdc_trailing_zeros(unsigned long x) {
 inline unsigned stdc_trailing_zeros(unsigned long long x) {
   return stdc_trailing_zeros_ull(x);
 }
+inline unsigned stdc_trailing_ones(unsigned char x) {
+  return stdc_trailing_ones_uc(x);
+}
+inline unsigned stdc_trailing_ones(unsigned short x) {
+  return stdc_trailing_ones_us(x);
+}
+inline unsigned stdc_trailing_ones(unsigned x) {
+  return stdc_trailing_ones_ui(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long x) {
+  return stdc_trailing_ones_ul(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long long x) {
+  return stdc_trailing_ones_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -77,6 +92,13 @@ inline unsigned stdc_trailing_zeros(unsigned long long x) {
       unsigned: stdc_trailing_zeros_ui,                                        \
       unsigned long: stdc_trailing_zeros_ul,                                   \
       unsigned long long: stdc_trailing_zeros_ull)(x)
+#define stdc_trailing_ones(x)                                                  \
+  _Generic((x),                                                                \
+      unsigned char: stdc_trailing_ones_uc,                                    \
+      unsigned short: stdc_trailing_ones_us,                                   \
+      unsigned: stdc_trailing_ones_ui,                                         \
+      unsigned long: stdc_trailing_ones_ul,                                    \
+      unsigned long long: stdc_trailing_ones_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 3a162b0bc76f4..ac7b7bcbeae47 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -796,7 +796,12 @@ def StdC : StandardSpec<"stdc"> {
           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>]>
+          FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_trailing_ones_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 35df7f9c41ecb..129621f139289 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -2,6 +2,7 @@ set(prefixes
   leading_zeros
   leading_ones
   trailing_zeros
+  trailing_ones
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.cpp b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
new file mode 100644
index 0000000000000..eabb21367e285
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_ones_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_uc, (unsigned char value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.h b/libc/src/stdbit/stdc_trailing_ones_uc.h
new file mode 100644
index 0000000000000..9736e21e1f280
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_ONES_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.cpp b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
new file mode 100644
index 0000000000000..87eb54fe5f026
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_ones_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.h b/libc/src/stdbit/stdc_trailing_ones_ui.h
new file mode 100644
index 0000000000000..aad7fcf0daf55
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_ONES_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.cpp b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
new file mode 100644
index 0000000000000..6d358a21ac337
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_ones_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ul, (unsigned long value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.h b/libc/src/stdbit/stdc_trailing_ones_ul.h
new file mode 100644
index 0000000000000..80589501b1ed2
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_ONES_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.cpp b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
new file mode 100644
index 0000000000000..fb5fffe07817d
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_trailing_ones_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_ones_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ull,
+                   (unsigned long long value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.h b/libc/src/stdbit/stdc_trailing_ones_ull.h
new file mode 100644
index 0000000000000..3d3f0b306789a
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_ONES_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.cpp b/libc/src/stdbit/stdc_trailing_ones_us.cpp
new file mode 100644
index 0000000000000..ee7ff4f78d489
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_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_ones_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_us, (unsigned short value)) {
+  return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.h b/libc/src/stdbit/stdc_trailing_ones_us.h
new file mode 100644
index 0000000000000..b783cd22ad2de
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_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_ONES_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 858dc08bfd70c..8d5d4c162938e 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -38,6 +38,11 @@ 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; }
+unsigned stdc_trailing_ones_uc(unsigned char) noexcept { return 0xDAU; }
+unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
+unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
+unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
+unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDFU; }
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -65,3 +70,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
   EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
   EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCFU);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
+  EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned char>(0U)), 0xDAU);
+  EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned short>(0U)), 0xDBU);
+  EXPECT_EQ(stdc_trailing_ones(0U), 0xDCU);
+  EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
+  EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDFU);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index 64d66037d0f6c..fab5f67bbd9be 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -4,6 +4,7 @@ set(prefixes
   leading_zeros
   leading_ones
   trailing_zeros
+  trailing_ones
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
new file mode 100644
index 0000000000000..79d4e5b8b8032
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_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_ones_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUcTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_uc(UCHAR_MAX),
+            static_cast<unsigned>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUcTest, ZeroHot) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_uc(~(1U << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
new file mode 100644
index 0000000000000..51e49f1e9e5fe
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_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_ones_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUiTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ui(UINT_MAX),
+            static_cast<unsigned>(UINT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUiTest, ZeroHot) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ui(~(1U << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
new file mode 100644
index 0000000000000..2aebe2e814ca9
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_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_ones_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUlTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ul(ULONG_MAX),
+            static_cast<unsigned>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUlTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ul(~(1UL << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
new file mode 100644
index 0000000000000..38c5100efc06e
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_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_ones_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUllTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ull(ULLONG_MAX),
+            static_cast<unsigned>(ULLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUllTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ull(~(1ULL << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp
new file mode 100644
index 0000000000000..7ab15743ed1e0
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_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_ones_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUsTest, ALL) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_us(USHRT_MAX),
+            static_cast<unsigned>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUsTest, ZeroHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_us(~(1U << i)), i);
+}

>From 83c1e891221299b77f8a7c97a8826b1f403bcbb7 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 6 Feb 2024 06:53:16 -0800
Subject: [PATCH 2/3] add macro to spec.td

---
 libc/spec/stdc.td | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index ac7b7bcbeae47..97dabbc5cf07a 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -777,7 +777,8 @@ def StdC : StandardSpec<"stdc"> {
       [
         Macro<"stdc_leading_zeros">,
         Macro<"stdc_leading_ones">,
-        Macro<"stdc_trailing_zeros">
+        Macro<"stdc_trailing_zeros">,
+        Macro<"stdc_trailing_ones">
       ], // Macros
       [], // Types
       [], // Enumerations

>From 9a4eac509ec4d3fa3fc27f5a0d3cfe0ae9c39e5c Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 6 Feb 2024 06:56:08 -0800
Subject: [PATCH 3/3] add todo

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index d58ab7127cbdb..c55529e5c0e94 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -9,6 +9,7 @@
 #ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 #define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 
+// TODO(https://github.com/llvm/llvm-project/issues/80509): support _BitInt().
 #ifdef __cplusplus
 inline unsigned stdc_leading_zeros(unsigned char x) {
   return stdc_leading_zeros_uc(x);



More information about the libc-commits mailing list