Skip to content
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

[libc] implement stdc_leading_ones (C23) #80082

Merged
merged 7 commits into from
Feb 1, 2024

Conversation

nickdesaulniers
Copy link
Member

  • [libc] implement stdc_leading_ones (C23)
  • [libc][test][stdbit] fix dependencies

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 31, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes
  • [libc] implement stdc_leading_ones (C23)
  • [libc][test][stdbit] fix dependencies

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

21 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+5)
  • (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+22)
  • (modified) libc/spec/stdc.td (+6-1)
  • (modified) libc/src/stdbit/CMakeLists.txt (+50)
  • (added) libc/src/stdbit/stdc_leading_ones_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_leading_ones_uc.h (+18)
  • (added) libc/src/stdbit/stdc_leading_ones_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_leading_ones_ui.h (+18)
  • (added) libc/src/stdbit/stdc_leading_ones_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_leading_ones_ul.h (+18)
  • (added) libc/src/stdbit/stdc_leading_ones_ull.cpp (+21)
  • (added) libc/src/stdbit/stdc_leading_ones_ull.h (+18)
  • (added) libc/src/stdbit/stdc_leading_ones_us.cpp (+21)
  • (added) libc/src/stdbit/stdc_leading_ones_us.h (+18)
  • (modified) libc/test/include/stdbit_test.cpp (+16-1)
  • (modified) libc/test/src/stdbit/CMakeLists.txt (+61)
  • (added) libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp (+22)
  • (added) libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp (+22)
  • (added) libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp (+22)
  • (added) libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp (+22)
  • (added) libc/test/src/stdbit/stdc_leading_ones_us_test.cpp (+22)
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..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,5 +51,62 @@ add_libc_test(
   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
+)
+
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...
[truncated]

@nickdesaulniers nickdesaulniers merged commit 6a3fde6 into llvm:main Feb 1, 2024
4 of 5 checks passed
@nickdesaulniers nickdesaulniers deleted the leading_ones branch February 1, 2024 18:14
@nickdesaulniers
Copy link
Member Author

d'oh, this broke post submit tests on non-x86 targets. I see the problem. Fixing.

@nickdesaulniers
Copy link
Member Author

Fix: #80318

carlosgalvezp pushed a commit to carlosgalvezp/llvm-project that referenced this pull request Feb 1, 2024
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
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.

None yet

3 participants