[libc-commits] [libc] 6a3fde6 - [libc] implement stdc_leading_ones (C23) (#80082)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 1 10:14:26 PST 2024


Author: Nick Desaulniers
Date: 2024-02-01T10:14:22-08:00
New Revision: 6a3fde6d600cccd2ffbede6dd54519036cc4089c

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

LOG: [libc] implement stdc_leading_ones (C23) (#80082)

Added: 
    libc/src/stdbit/stdc_leading_ones_uc.cpp
    libc/src/stdbit/stdc_leading_ones_uc.h
    libc/src/stdbit/stdc_leading_ones_ui.cpp
    libc/src/stdbit/stdc_leading_ones_ui.h
    libc/src/stdbit/stdc_leading_ones_ul.cpp
    libc/src/stdbit/stdc_leading_ones_ul.h
    libc/src/stdbit/stdc_leading_ones_ull.cpp
    libc/src/stdbit/stdc_leading_ones_ull.h
    libc/src/stdbit/stdc_leading_ones_us.cpp
    libc/src/stdbit/stdc_leading_ones_us.h
    libc/test/src/stdbit/stdc_leading_ones_uc_test.cpp
    libc/test/src/stdbit/stdc_leading_ones_ui_test.cpp
    libc/test/src/stdbit/stdc_leading_ones_ul_test.cpp
    libc/test/src/stdbit/stdc_leading_ones_ull_test.cpp
    libc/test/src/stdbit/stdc_leading_ones_us_test.cpp

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

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 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/docs/stdbit.rst b/libc/docs/stdbit.rst
index ea08b9f4e6c41..2dd91107dcc45 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -36,11 +36,11 @@ stdc_leading_zeros_us        |check|
 stdc_leading_zeros_ui        |check|
 stdc_leading_zeros_ul        |check|
 stdc_leading_zeros_ull       |check|
-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
@@ -115,7 +115,7 @@ __STDC_ENDIAN_LITTLE__
 __STDC_ENDIAN_BIG__
 __STDC_ENDIAN_NATIVE__
 stdc_leading_zeros         |check|
-stdc_leading_ones
+stdc_leading_ones          |check|
 stdc_trailing_zeros
 stdc_trailing_ones
 stdc_first_leading_zero

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..2a73c2ccfa49b
--- /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 static_cast<unsigned>(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..c381eb50e9732
--- /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..583a479f79a81
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_ones_us.h
@@ -0,0 +1,18 @@
+//===-- 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.
+// 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; 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));
+}


        


More information about the libc-commits mailing list