[libc-commits] [libc] 7bc5eaa - [libc] implement stdc_leading_zeros_u* for stdbit.h (#79669)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 29 15:48:00 PST 2024


Author: Nick Desaulniers
Date: 2024-01-29T15:47:55-08:00
New Revision: 7bc5eaa02f6d74410f3131b7d2ab21ee8209430f

URL: https://github.com/llvm/llvm-project/commit/7bc5eaa02f6d74410f3131b7d2ab21ee8209430f
DIFF: https://github.com/llvm/llvm-project/commit/7bc5eaa02f6d74410f3131b7d2ab21ee8209430f.diff

LOG: [libc] implement stdc_leading_zeros_u* for stdbit.h (#79669)


- stdbit.stdc_leading_zeros_uc
- stdbit.stdc_leading_zeros_us
- stdbit.stdc_leading_zeros_ui
- stdbit.stdc_leading_zeros_ul
- stdbit.stdc_leading_zeros_ull

Test via:
$ ninja libc-stdbit-tests libc_include_tests

Added: 
    libc/include/llvm-libc-macros/stdbit-macros.h
    libc/include/stdbit.h.def
    libc/src/stdbit/CMakeLists.txt
    libc/src/stdbit/stdc_leading_zeros_uc.cpp
    libc/src/stdbit/stdc_leading_zeros_uc.h
    libc/src/stdbit/stdc_leading_zeros_ui.cpp
    libc/src/stdbit/stdc_leading_zeros_ui.h
    libc/src/stdbit/stdc_leading_zeros_ul.cpp
    libc/src/stdbit/stdc_leading_zeros_ul.h
    libc/src/stdbit/stdc_leading_zeros_ull.cpp
    libc/src/stdbit/stdc_leading_zeros_ull.h
    libc/src/stdbit/stdc_leading_zeros_us.cpp
    libc/src/stdbit/stdc_leading_zeros_us.h
    libc/test/include/stdbit_test.cpp
    libc/test/src/stdbit/CMakeLists.txt
    libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
    libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
    libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
    libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
    libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp

Modified: 
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/linux/x86_64/headers.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt
    libc/spec/spec.td
    libc/spec/stdc.td
    libc/src/CMakeLists.txt
    libc/test/include/CMakeLists.txt
    libc/test/src/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3812d9586121e7..e714f0f6cc5ac5 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -91,6 +91,13 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.inttypes.strtoimax
     libc.src.inttypes.strtoumax
 
+    # stdbit.h entrypoints
+    libc.src.stdbit.stdc_leading_zeros_uc
+    libc.src.stdbit.stdc_leading_zeros_us
+    libc.src.stdbit.stdc_leading_zeros_ui
+    libc.src.stdbit.stdc_leading_zeros_ul
+    libc.src.stdbit.stdc_leading_zeros_ull
+
     # stdlib.h entrypoints
     libc.src.stdlib.abs
     libc.src.stdlib.atoi

diff  --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index d103176897a74a..4009501bc45ce8 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.signal
     libc.include.spawn
     libc.include.setjmp
+    libc.include.stdbit
     libc.include.stdio
     libc.include.stdlib
     libc.include.string

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 89fffd1022758e..9474bdd3791264 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -223,6 +223,15 @@ else()
   message(STATUS "Skipping header signal.h as the target config is missing")
 endif()
 
+add_gen_header(
+  stdbit
+  DEF_FILE stdbit.h.def
+  GEN_HDR stdbit.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-macros.stdbit_macros
+)
+
 add_gen_header(
   stdio
   DEF_FILE stdio.h.def

diff  --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 96ee29d7327224..c5a7e742cadd13 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -111,6 +111,12 @@ add_macro_header(
     signal-macros.h
 )
 
+add_macro_header(
+  stdbit_macros
+  HDR
+    stdbit-macros.h
+)
+
 add_macro_header(
   stdio_macros
   HDR

diff  --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
new file mode 100644
index 00000000000000..febe95fe0a1e3c
--- /dev/null
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -0,0 +1,20 @@
+//===-- Definition of macros to be used with stdbit functions ----------===//
+//
+// 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_MACROS_STDBIT_MACROS_H
+#define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
+
+#define stdc_leading_zeros(x)                                                  \
+  _Generic((x),                                                                \
+      unsigned char: stdc_leading_zeros_uc,                                    \
+      unsigned short: stdc_leading_zeros_us,                                   \
+      unsigned: stdc_leading_zeros_ui,                                         \
+      unsigned long: stdc_leading_zeros_ul,                                    \
+      unsigned long long: stdc_leading_zeros_ull)(x)
+
+#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

diff  --git a/libc/include/stdbit.h.def b/libc/include/stdbit.h.def
new file mode 100644
index 00000000000000..cb79ac1caf049f
--- /dev/null
+++ b/libc/include/stdbit.h.def
@@ -0,0 +1,17 @@
+//===-- C standard library header stdbit.h --------------------------------===//
+//
+// 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_STDBIT_H
+#define LLVM_LIBC_STDBIT_H
+
+#include <__llvm-libc-common.h>
+#include <llvm-libc-macros/stdbit-macros.h>
+
+%%public_api()
+
+#endif // LLVM_LIBC_STDBIT_H

diff  --git a/libc/spec/spec.td b/libc/spec/spec.td
index 818cfaee6b61c2..16abf97bf0b1cb 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -50,6 +50,7 @@ def DoubleType : NamedType<"double">;
 def LongDoubleType : NamedType<"long double">;
 def CharType : NamedType<"char">;
 def UnsignedCharType : NamedType<"unsigned char">;
+def UnsignedShortType : NamedType<"unsigned short">;
 
 // TODO: Add compatibility layer to use C23 type _Float128 if possible.
 def Float128Type : NamedType<"__float128">;

diff  --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 43b81c4abaa0e6..b21f620d0766af 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -767,6 +767,22 @@ def StdC : StandardSpec<"stdc"> {
       ]
   >;
 
+  HeaderSpec StdBit = HeaderSpec<
+      "stdbit.h",
+      [
+        Macro<"stdc_leading_zeros">
+      ], // Macros
+      [], // Types
+      [], // Enumerations
+      [
+          FunctionSpec<"stdc_leading_zeros_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
+          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>]>
+      ] // Functions
+  >;
+
   HeaderSpec StdLib = HeaderSpec<
       "stdlib.h",
       [], // Macros
@@ -1141,7 +1157,7 @@ def StdC : StandardSpec<"stdc"> {
       "wchar.h",
       [ // Macros
         Macro<"WEOF">,
-      ], 
+      ],
       [ //Types
         SizeTType,
         WIntType,
@@ -1167,6 +1183,7 @@ def StdC : StandardSpec<"stdc"> {
     Limits,
     Math,
     String,
+    StdBit,
     StdIO,
     StdLib,
     IntTypes,

diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 492f9c5bd50f9b..5211db7bc1f993 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -5,9 +5,10 @@ add_subdirectory(errno)
 add_subdirectory(fenv)
 add_subdirectory(inttypes)
 add_subdirectory(math)
-add_subdirectory(string)
-add_subdirectory(stdlib)
+add_subdirectory(stdbit)
 add_subdirectory(stdio)
+add_subdirectory(stdlib)
+add_subdirectory(string)
 add_subdirectory(wchar)
 
 if(${LIBC_TARGET_OS} STREQUAL "linux")

diff  --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
new file mode 100644
index 00000000000000..d72bf8bc42dec3
--- /dev/null
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -0,0 +1,49 @@
+add_entrypoint_object(
+  stdc_leading_zeros_uc
+  SRCS
+    stdc_leading_zeros_uc.cpp
+  HDRS
+    stdc_leading_zeros_uc.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_leading_zeros_us
+  SRCS
+    stdc_leading_zeros_us.cpp
+  HDRS
+    stdc_leading_zeros_us.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_leading_zeros_ui
+  SRCS
+    stdc_leading_zeros_ui.cpp
+  HDRS
+    stdc_leading_zeros_ui.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_leading_zeros_ul
+  SRCS
+    stdc_leading_zeros_ul.cpp
+  HDRS
+    stdc_leading_zeros_ul.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+  stdc_leading_zeros_ull
+  SRCS
+    stdc_leading_zeros_ull.cpp
+  HDRS
+    stdc_leading_zeros_ull.h
+  DEPENDS
+    libc.src.__support.CPP.bit
+)

diff  --git a/libc/src/stdbit/stdc_leading_zeros_uc.cpp b/libc/src/stdbit/stdc_leading_zeros_uc.cpp
new file mode 100644
index 00000000000000..8c27043e055bf9
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_uc.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_zeros_uc ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_leading_zeros_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned char, stdc_leading_zeros_uc,
+                   (unsigned char value)) {
+  return static_cast<unsigned char>(cpp::countl_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_leading_zeros_uc.h b/libc/src/stdbit/stdc_leading_zeros_uc.h
new file mode 100644
index 00000000000000..0622e72b0683ee
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_zeros_uc ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned char stdc_leading_zeros_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ui.cpp b/libc/src/stdbit/stdc_leading_zeros_ui.cpp
new file mode 100644
index 00000000000000..99bc0eb70f33bf
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_leading_zeros_ui ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_leading_zeros_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_leading_zeros_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::countl_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ui.h b/libc/src/stdbit/stdc_leading_zeros_ui.h
new file mode 100644
index 00000000000000..14113d486b54a0
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_zeros_ui ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_leading_zeros_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ul.cpp b/libc/src/stdbit/stdc_leading_zeros_ul.cpp
new file mode 100644
index 00000000000000..cf21ff449a8bf5
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ul.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_zeros_ul ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_leading_zeros_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long, stdc_leading_zeros_ul,
+                   (unsigned long value)) {
+  return static_cast<unsigned long>(cpp::countl_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ul.h b/libc/src/stdbit/stdc_leading_zeros_ul.h
new file mode 100644
index 00000000000000..a1f36299931dc2
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_zeros_ul ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long stdc_leading_zeros_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ull.cpp b/libc/src/stdbit/stdc_leading_zeros_ull.cpp
new file mode 100644
index 00000000000000..d488559bbd14c2
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_zeros_ull --------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_leading_zeros_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long long, stdc_leading_zeros_ull,
+                   (unsigned long long value)) {
+  return static_cast<unsigned long long>(cpp::countl_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_leading_zeros_ull.h b/libc/src/stdbit/stdc_leading_zeros_ull.h
new file mode 100644
index 00000000000000..b05855b296af0b
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_zeros_ull --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long long stdc_leading_zeros_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H

diff  --git a/libc/src/stdbit/stdc_leading_zeros_us.cpp b/libc/src/stdbit/stdc_leading_zeros_us.cpp
new file mode 100644
index 00000000000000..84df2b842d6950
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_us.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_leading_zeros_us ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_leading_zeros_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned short, stdc_leading_zeros_us,
+                   (unsigned short value)) {
+  return static_cast<unsigned short>(cpp::countl_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/stdbit/stdc_leading_zeros_us.h b/libc/src/stdbit/stdc_leading_zeros_us.h
new file mode 100644
index 00000000000000..c0f62e2be2f02b
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_leading_zeros_us ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned short stdc_leading_zeros_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H

diff  --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 183e7ecde719e5..ecc32380c596e0 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -14,3 +14,26 @@ add_libc_test(
     # This is needed because the __containerof macro uses statement expression.
     -Wno-gnu-statement-expression-from-macro-expansion
 )
+
+# stdbit_test only tests our generated stdbit.h, which is not generated in
+# overlay mode.
+if (LLVM_LIBC_FULL_BUILD)
+  add_libc_test(
+    stdbit_test
+    SUITE
+      libc_include_tests
+    SRCS
+      stdbit_test.cpp
+    DEPENDS
+      libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.stdbit
+      # Intentionally do not depend on libc.src.stdbit.*. The include test is
+      # simply testing the macros provided by stdbit.h, not the implementation
+      # of the underlying functions which the type generic macros may dispatch
+      # to.
+    COMPILE_OPTIONS
+      # stdbit.h is full of type generic macros implemented via C11 _Generic.
+      # Clang will produce -Wno-c11-extensions when using _Generic in C++ mode.
+      -Wno-c11-extensions
+  )
+endif()

diff  --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
new file mode 100644
index 00000000000000..d20005cc31afa0
--- /dev/null
+++ b/libc/test/include/stdbit_test.cpp
@@ -0,0 +1,35 @@
+//===-- Unittests for stdbit ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/UnitTest/Test.h"
+
+#include <stdbit.h>
+
+/*
+ * The intent of this test is validate that:
+ * 1. We provide the definition of the various type generic macros of stdbit.h.
+ * 2. It dispatches to the correct underlying function.
+ * Because unit tests build without public packaging, the object files produced
+ * do not contain non-namespaced symbols.
+ */
+
+unsigned char stdc_leading_zeros_uc(unsigned char) { return 0xAA; }
+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; }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
+  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)),
+            static_cast<unsigned short>(0xAB));
+  EXPECT_EQ(stdc_leading_zeros(0U), static_cast<unsigned>(0xAC));
+  EXPECT_EQ(stdc_leading_zeros(0UL), static_cast<unsigned long>(0xAD));
+  EXPECT_EQ(stdc_leading_zeros(0ULL), static_cast<unsigned long long>(0xAF));
+}

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 6bd8ace9ea71af..1f899c729e0abc 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -40,13 +40,14 @@ add_subdirectory(__support)
 add_subdirectory(ctype)
 add_subdirectory(errno)
 add_subdirectory(fenv)
-add_subdirectory(math)
-add_subdirectory(string)
-add_subdirectory(stdlib)
 add_subdirectory(inttypes)
+add_subdirectory(math)
+add_subdirectory(search)
+add_subdirectory(stdbit)
 add_subdirectory(stdio)
+add_subdirectory(stdlib)
+add_subdirectory(string)
 add_subdirectory(wchar)
-add_subdirectory(search)
 
 if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_subdirectory(fcntl)
@@ -113,7 +114,7 @@ add_custom_target(libc-api-test)
 add_dependencies(check-libc libc-api-test)
 
 set(
-  allocator_entrypoints 
+  allocator_entrypoints
     libc.src.stdlib.malloc
     libc.src.stdlib.calloc
     libc.src.stdlib.realloc

diff  --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
new file mode 100644
index 00000000000000..73b32e4ec0caea
--- /dev/null
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -0,0 +1,51 @@
+add_custom_target(libc-stdbit-tests)
+
+add_libc_test(
+  stdc_leading_zeros_uc_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_leading_zeros_uc_test.cpp
+  DEPENDS
+    libc.src.stdbit.stdc_leading_zeros_uc
+)
+
+add_libc_test(
+  stdc_leading_zeros_us_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_leading_zeros_us_test.cpp
+  DEPENDS
+    libc.src.stdbit.stdc_leading_zeros_us
+)
+
+add_libc_test(
+  stdc_leading_zeros_ui_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_leading_zeros_ui_test.cpp
+  DEPENDS
+    libc.src.stdbit.stdc_leading_zeros_ui
+)
+
+add_libc_test(
+  stdc_leading_zeros_ul_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_leading_zeros_ul_test.cpp
+  DEPENDS
+    libc.src.stdbit.stdc_leading_zeros_ul
+)
+
+add_libc_test(
+  stdc_leading_zeros_ull_test
+  SUITE
+    libc-stdbit-tests
+  SRCS
+    stdc_leading_zeros_ull_test.cpp
+  DEPENDS
+    libc.src.stdbit.stdc_leading_zeros_ull
+)

diff  --git a/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
new file mode 100644
index 00000000000000..4e2e1db3bd72b9
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_zeros_uc -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingZerosUcTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_uc(0U),
+            static_cast<unsigned char>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUcTest, OneHot) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_uc(1U << i),
+              static_cast<unsigned char>(UCHAR_WIDTH - i - 1));
+}

diff  --git a/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
new file mode 100644
index 00000000000000..2bd6bb586d7de4
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
@@ -0,0 +1,23 @@
+//===-- Unittests for stdc_leading_zeros_ui -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ui.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+TEST(LlvmLibcStdcLeadingZerosUiTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ui(0U),
+            static_cast<unsigned>(INT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUiTest, OneHot) {
+  for (unsigned i = 0U; i != INT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ui(1U << i),
+              INT_WIDTH - i - 1);
+}

diff  --git a/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
new file mode 100644
index 00000000000000..9a73aece89acc5
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
@@ -0,0 +1,23 @@
+//===-- Unittests for stdc_leading_zeros_ul -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ul.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+TEST(LlvmLibcStdcLeadingZerosUlTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ul(0UL),
+            static_cast<unsigned long>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUlTest, OneHot) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ul(1UL << i),
+              static_cast<unsigned long>(ULONG_WIDTH - i - 1));
+}

diff  --git a/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
new file mode 100644
index 00000000000000..9df2f015f9b6fb
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
@@ -0,0 +1,23 @@
+//===-- Unittests for stdc_leading_zeros_ull ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ull.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+TEST(LlvmLibcStdcLeadingZerosUllTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ull(0ULL),
+            static_cast<unsigned long long>(ULLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUllTest, OneHot) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_ull(1ULL << i),
+              static_cast<unsigned long long>(ULLONG_WIDTH - i - 1));
+}

diff  --git a/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
new file mode 100644
index 00000000000000..b396a740e43b7c
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_leading_zeros_us -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcLeadingZerosUsTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_us(0U),
+            static_cast<unsigned short>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUsTest, OneHot) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_leading_zeros_us(1U << i),
+              static_cast<unsigned short>(USHRT_WIDTH - i - 1));
+}


        


More information about the libc-commits mailing list