[libc-commits] [lld] [llvm] [flang] [libc] [libcxxabi] [clang] [clang-tools-extra] [libcxx] [libc] implement stdc_leading_ones (C23) (PR #80082)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Feb 1 10:13:46 PST 2024


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

>From de314dcc670093d860830cf7e9c49978e001c3a0 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 30 Jan 2024 15:30:41 -0800
Subject: [PATCH 1/6] [libc] implement stdc_leading_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                | 50 +++++++++++++++++
 libc/src/stdbit/stdc_leading_ones_uc.cpp      | 20 +++++++
 libc/src/stdbit/stdc_leading_ones_uc.h        | 18 ++++++
 libc/src/stdbit/stdc_leading_ones_ui.cpp      | 20 +++++++
 libc/src/stdbit/stdc_leading_ones_ui.h        | 18 ++++++
 libc/src/stdbit/stdc_leading_ones_ul.cpp      | 20 +++++++
 libc/src/stdbit/stdc_leading_ones_ul.h        | 18 ++++++
 libc/src/stdbit/stdc_leading_ones_ull.cpp     | 21 +++++++
 libc/src/stdbit/stdc_leading_ones_ull.h       | 18 ++++++
 libc/src/stdbit/stdc_leading_ones_us.cpp      | 21 +++++++
 libc/src/stdbit/stdc_leading_ones_us.h        | 18 ++++++
 libc/test/include/stdbit_test.cpp             | 17 +++++-
 libc/test/src/stdbit/CMakeLists.txt           | 56 +++++++++++++++++++
 .../src/stdbit/stdc_leading_ones_uc_test.cpp  | 22 ++++++++
 .../src/stdbit/stdc_leading_ones_ui_test.cpp  | 22 ++++++++
 .../src/stdbit/stdc_leading_ones_ul_test.cpp  | 22 ++++++++
 .../src/stdbit/stdc_leading_ones_ull_test.cpp | 22 ++++++++
 .../src/stdbit/stdc_leading_ones_us_test.cpp  | 22 ++++++++
 21 files changed, 457 insertions(+), 2 deletions(-)
 create mode 100644 libc/src/stdbit/stdc_leading_ones_uc.cpp
 create mode 100644 libc/src/stdbit/stdc_leading_ones_uc.h
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ui.cpp
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ui.h
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ul.cpp
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ul.h
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ull.cpp
 create mode 100644 libc/src/stdbit/stdc_leading_ones_ull.h
 create mode 100644 libc/src/stdbit/stdc_leading_ones_us.cpp
 create mode 100644 libc/src/stdbit/stdc_leading_ones_us.h
 create mode 100644 libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_leading_ones_us_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index c3aa7e72ebce2..9946b93c346ce 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -97,6 +97,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_leading_zeros_ui
     libc.src.stdbit.stdc_leading_zeros_ul
     libc.src.stdbit.stdc_leading_zeros_ull
+    libc.src.stdbit.stdc_leading_ones_uc
+    libc.src.stdbit.stdc_leading_ones_us
+    libc.src.stdbit.stdc_leading_ones_ui
+    libc.src.stdbit.stdc_leading_ones_ul
+    libc.src.stdbit.stdc_leading_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 da0fb1a578f13..5355be2832fbb 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -25,6 +25,21 @@ inline unsigned long stdc_leading_zeros(unsigned long x) {
 inline unsigned long long stdc_leading_zeros(unsigned long long x) {
   return stdc_leading_zeros_ull(x);
 }
+inline unsigned char stdc_leading_ones(unsigned char x) {
+  return stdc_leading_ones_uc(x);
+}
+inline unsigned short stdc_leading_ones(unsigned short x) {
+  return stdc_leading_ones_us(x);
+}
+inline unsigned stdc_leading_ones(unsigned x) {
+  return stdc_leading_ones_ui(x);
+}
+inline unsigned long stdc_leading_ones(unsigned long x) {
+  return stdc_leading_ones_ul(x);
+}
+inline unsigned long long stdc_leading_ones(unsigned long long x) {
+  return stdc_leading_ones_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -33,6 +48,13 @@ inline unsigned long long stdc_leading_zeros(unsigned long long x) {
       unsigned: stdc_leading_zeros_ui,                                         \
       unsigned long: stdc_leading_zeros_ul,                                    \
       unsigned long long: stdc_leading_zeros_ull)(x)
+#define stdc_leading_ones(x)                                                   \
+  _Generic((x),                                                                \
+      unsigned char: stdc_leading_ones_uc,                                     \
+      unsigned short: stdc_leading_ones_us,                                    \
+      unsigned: stdc_leading_ones_ui,                                          \
+      unsigned long: stdc_leading_ones_ul,                                     \
+      unsigned long long: stdc_leading_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 b21f620d0766a..cb23a6700d913 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -779,7 +779,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_leading_zeros_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_leading_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_leading_zeros_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_leading_zeros_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_leading_zeros_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_leading_ones_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index d72bf8bc42dec..d7daff44dfda6 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -47,3 +47,53 @@ add_entrypoint_object(
   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
+)
diff --git a/libc/src/stdbit/stdc_leading_ones_uc.cpp b/libc/src/stdbit/stdc_leading_ones_uc.cpp
new file mode 100644
index 0000000000000..beae134eeb91a
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_leading_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_leading_ones_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned char, stdc_leading_ones_uc, (unsigned char value)) {
+  return static_cast<unsigned char>(cpp::countl_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_leading_ones_uc.h b/libc/src/stdbit/stdc_leading_ones_uc.h
new file mode 100644
index 0000000000000..8303f3f7bd26e
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_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_LEADING_ONES_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned char stdc_leading_ones_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UC_H
diff --git a/libc/src/stdbit/stdc_leading_ones_ui.cpp b/libc/src/stdbit/stdc_leading_ones_ui.cpp
new file mode 100644
index 0000000000000..ca378d0371b26
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_leading_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_leading_ones_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_leading_ones_ui, (unsigned value)) {
+  return cpp::countl_one(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_leading_ones_ui.h b/libc/src/stdbit/stdc_leading_ones_ui.h
new file mode 100644
index 0000000000000..527567a4776e9
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_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_LEADING_ONES_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_leading_ones_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_Ui_H
diff --git a/libc/src/stdbit/stdc_leading_ones_ul.cpp b/libc/src/stdbit/stdc_leading_ones_ul.cpp
new file mode 100644
index 0000000000000..ccfd9b9594e0b
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_leading_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_leading_ones_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long, stdc_leading_ones_ul, (unsigned long value)) {
+  return static_cast<unsigned long>(cpp::countl_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_leading_ones_ul.h b/libc/src/stdbit/stdc_leading_ones_ul.h
new file mode 100644
index 0000000000000..7643bd1ba81d9
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_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_LEADING_ONES_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long stdc_leading_ones_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UL_H
diff --git a/libc/src/stdbit/stdc_leading_ones_ull.cpp b/libc/src/stdbit/stdc_leading_ones_ull.cpp
new file mode 100644
index 0000000000000..8cb1ff08c7b00
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_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_leading_ones_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long long, stdc_leading_ones_ull,
+                   (unsigned long long value)) {
+  return static_cast<unsigned long long>(cpp::countl_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_leading_ones_ull.h b/libc/src/stdbit/stdc_leading_ones_ull.h
new file mode 100644
index 0000000000000..8ae8145109671
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_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_LEADING_ONES_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long long stdc_leading_ones_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_ULL_H
diff --git a/libc/src/stdbit/stdc_leading_ones_us.cpp b/libc/src/stdbit/stdc_leading_ones_us.cpp
new file mode 100644
index 0000000000000..4b58cf5cee433
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_us.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_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_leading_ones_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned short, stdc_leading_ones_us,
+                   (unsigned short value)) {
+  return static_cast<unsigned short>(cpp::countl_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_leading_ones_us.h b/libc/src/stdbit/stdc_leading_ones_us.h
new file mode 100644
index 0000000000000..5d1cb3b4680d0
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_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_LEADING_ONES_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned short stdc_leading_ones_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index d20005cc31afa..61e241c8f68a9 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -23,8 +23,13 @@ unsigned short stdc_leading_zeros_us(unsigned short) { return 0xAB; }
 unsigned stdc_leading_zeros_ui(unsigned) { return 0xAC; }
 unsigned long stdc_leading_zeros_ul(unsigned long) { return 0xAD; }
 unsigned long long stdc_leading_zeros_ull(unsigned long long) { return 0xAF; }
+unsigned char stdc_leading_ones_uc(unsigned char) { return 0xBA; }
+unsigned short stdc_leading_ones_us(unsigned short) { return 0xBB; }
+unsigned stdc_leading_ones_ui(unsigned) { return 0xBC; }
+unsigned long stdc_leading_ones_ul(unsigned long) { return 0xBD; }
+unsigned long long stdc_leading_ones_ull(unsigned long long) { return 0xBF; }
 
-TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
+TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingZeros) {
   EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned char>(0U)),
             static_cast<unsigned char>(0xAA));
   EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned short>(0U)),
@@ -33,3 +38,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
   EXPECT_EQ(stdc_leading_zeros(0UL), static_cast<unsigned long>(0xAD));
   EXPECT_EQ(stdc_leading_zeros(0ULL), static_cast<unsigned long long>(0xAF));
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
+  EXPECT_EQ(stdc_leading_ones(static_cast<unsigned char>(0U)),
+            static_cast<unsigned char>(0xBA));
+  EXPECT_EQ(stdc_leading_ones(static_cast<unsigned short>(0U)),
+            static_cast<unsigned short>(0xBB));
+  EXPECT_EQ(stdc_leading_ones(0U), static_cast<unsigned>(0xBC));
+  EXPECT_EQ(stdc_leading_ones(0UL), static_cast<unsigned long>(0xBD));
+  EXPECT_EQ(stdc_leading_ones(0ULL), static_cast<unsigned long long>(0xBF));
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index 73b32e4ec0cae..a86466777c747 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -49,3 +49,59 @@ add_libc_test(
   DEPENDS
     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
+)
+
diff --git a/libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp b/libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp
new file mode 100644
index 0000000000000..2883065df3771
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_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_leading_ones_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingOnesUcTest, All) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_uc(UCHAR_MAX),
+            static_cast<unsigned char>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingOnesUcTest, ZeroHot) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_uc(~(1U << i)),
+              static_cast<unsigned char>(UCHAR_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp b/libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp
new file mode 100644
index 0000000000000..bae00573927ac
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_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_leading_ones_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingOnesUiTest, All) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ui(UINT_MAX),
+            static_cast<unsigned>(UINT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingOnesUiTest, ZeroHot) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ui(~(1U << i)),
+              static_cast<unsigned>(UINT_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp b/libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp
new file mode 100644
index 0000000000000..bb7b9755ebcc5
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_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_leading_ones_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingOnesUlTest, All) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ul(ULONG_MAX),
+            static_cast<unsigned long>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingOnesUlTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ul(~(1UL << i)),
+              static_cast<unsigned long>(ULONG_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp b/libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp
new file mode 100644
index 0000000000000..a8497a62bdd17
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_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_leading_ones_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingOnesUllTest, All) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ull(ULLONG_MAX),
+            static_cast<unsigned long long>(ULLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingOnesUllTest, ZeroHot) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_ull(~(1ULL << i)),
+              static_cast<unsigned long long>(ULLONG_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_leading_ones_us_test.cpp b/libc/test/src/stdbit/stdc_leading_ones_us_test.cpp
new file mode 100644
index 0000000000000..40c34126b76dd
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_ones_us_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_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_leading_ones_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingOnesUsTest, All) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_us(USHRT_MAX),
+            static_cast<unsigned short>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingOnesUsTest, ZeroHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_ones_us(~(1U << i)),
+              static_cast<unsigned short>(USHRT_WIDTH - i - 1));
+}

>From 3d94ce2202797bda7699aae6f02d58a4c123003e Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 30 Jan 2024 15:10:50 -0800
Subject: [PATCH 2/6] [libc][test][stdbit] fix dependencies

---
 libc/test/src/stdbit/CMakeLists.txt | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index a86466777c747..a8c3c9f883532 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -7,6 +7,7 @@ add_libc_test(
   SRCS
     stdc_leading_zeros_uc_test.cpp
   DEPENDS
+    libc.src.__support.CPP.limits
     libc.src.stdbit.stdc_leading_zeros_uc
 )
 
@@ -17,6 +18,7 @@ add_libc_test(
   SRCS
     stdc_leading_zeros_us_test.cpp
   DEPENDS
+    libc.src.__support.CPP.limits
     libc.src.stdbit.stdc_leading_zeros_us
 )
 
@@ -27,6 +29,7 @@ add_libc_test(
   SRCS
     stdc_leading_zeros_ui_test.cpp
   DEPENDS
+    libc.src.__support.CPP.limits
     libc.src.stdbit.stdc_leading_zeros_ui
 )
 
@@ -37,6 +40,7 @@ add_libc_test(
   SRCS
     stdc_leading_zeros_ul_test.cpp
   DEPENDS
+    libc.src.__support.CPP.limits
     libc.src.stdbit.stdc_leading_zeros_ul
 )
 
@@ -47,6 +51,7 @@ add_libc_test(
   SRCS
     stdc_leading_zeros_ull_test.cpp
   DEPENDS
+    libc.src.__support.CPP.limits
     libc.src.stdbit.stdc_leading_zeros_ull
 )
 

>From 68052db55a847f70df279280fa40a41303fa94b6 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Tue, 30 Jan 2024 16:02:13 -0800
Subject: [PATCH 3/6] fix typo

---
 libc/src/stdbit/stdc_leading_ones_ui.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdbit/stdc_leading_ones_ui.h b/libc/src/stdbit/stdc_leading_ones_ui.h
index 527567a4776e9..c381eb50e9732 100644
--- a/libc/src/stdbit/stdc_leading_ones_ui.h
+++ b/libc/src/stdbit/stdc_leading_ones_ui.h
@@ -15,4 +15,4 @@ unsigned stdc_leading_ones_ui(unsigned value);
 
 } // namespace LIBC_NAMESPACE
 
-#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_Ui_H
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_UI_H

>From 58b63c0ec8f41392e69ea1066e5cb1066d25c49f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Wed, 31 Jan 2024 15:51:09 -0800
Subject: [PATCH 4/6] add explicit cast

---
 libc/src/stdbit/stdc_leading_ones_ui.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdbit/stdc_leading_ones_ui.cpp b/libc/src/stdbit/stdc_leading_ones_ui.cpp
index ca378d0371b26..2a73c2ccfa49b 100644
--- a/libc/src/stdbit/stdc_leading_ones_ui.cpp
+++ b/libc/src/stdbit/stdc_leading_ones_ui.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned, stdc_leading_ones_ui, (unsigned value)) {
-  return cpp::countl_one(value);
+  return static_cast<unsigned>(cpp::countl_one(value));
 }
 
 } // namespace LIBC_NAMESPACE

>From 64912cb60bbbb6a87ce5962083352f5e8df519ed Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Wed, 31 Jan 2024 15:51:41 -0800
Subject: [PATCH 5/6] fix copy pasta

---
 libc/src/stdbit/stdc_leading_ones_us.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdbit/stdc_leading_ones_us.h b/libc/src/stdbit/stdc_leading_ones_us.h
index 5d1cb3b4680d0..583a479f79a81 100644
--- a/libc/src/stdbit/stdc_leading_ones_us.h
+++ b/libc/src/stdbit/stdc_leading_ones_us.h
@@ -1,4 +1,4 @@
-//===-- Implementation header for stdc_leading_ones_uc ----------*- C++ -*-===//
+//===-- Implementation header for stdc_leading_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.

>From deafd4640296ea04a78604a9cd9068366a1fab7d Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Wed, 31 Jan 2024 15:53:07 -0800
Subject: [PATCH 6/6] update docs

---
 libc/docs/stdbit.rst | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index c37f809601988..31c869e476492 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -35,11 +35,11 @@ stdc_leading_zeros_us
 stdc_leading_zeros_ui
 stdc_leading_zeros_ul
 stdc_leading_zeros_ull
-stdc_leading_ones_uc
-stdc_leading_ones_us
-stdc_leading_ones_ui
-stdc_leading_ones_ul
-stdc_leading_ones_ull
+stdc_leading_ones_uc         |check|
+stdc_leading_ones_us         |check|
+stdc_leading_ones_ui         |check|
+stdc_leading_ones_ul         |check|
+stdc_leading_ones_ull        |check|
 stdc_trailing_zeros_uc
 stdc_trailing_zeros_us
 stdc_trailing_zeros_ui
@@ -114,7 +114,7 @@ __STDC_ENDIAN_LITTLE__
 __STDC_ENDIAN_BIG__
 __STDC_ENDIAN_NATIVE__
 stdc_leading_zeros
-stdc_leading_ones
+stdc_leading_ones          |check|
 stdc_trailing_zeros
 stdc_trailing_ones
 stdc_first_leading_zero



More information about the libc-commits mailing list