[libc-commits] [libc] [libc] implement stdc_leading_zeros_u* for stdbit.h (PR #79669)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Fri Jan 26 16:09:34 PST 2024
https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/79669
>From d6c96e36ab3ec447bfef3a383191821ded8f0c0c Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 26 Jan 2024 12:46:38 -0800
Subject: [PATCH 1/2] [libc] implement stdc_leading_zeros_u* for stdbit.h
- 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
---
libc/config/linux/x86_64/entrypoints.txt | 7 +++
libc/config/linux/x86_64/headers.txt | 1 +
libc/include/CMakeLists.txt | 9 ++++
libc/include/llvm-libc-macros/CMakeLists.txt | 6 +++
libc/include/llvm-libc-macros/stdbit-macros.h | 19 +++++++
libc/include/stdbit.h.def | 17 +++++++
libc/spec/spec.td | 1 +
libc/spec/stdc.td | 19 ++++++-
libc/src/CMakeLists.txt | 5 +-
libc/src/stdbit/CMakeLists.txt | 39 ++++++++++++++
libc/src/stdbit/stdc_leading_zeros_uc.cpp | 20 ++++++++
libc/src/stdbit/stdc_leading_zeros_uc.h | 18 +++++++
libc/src/stdbit/stdc_leading_zeros_ui.cpp | 20 ++++++++
libc/src/stdbit/stdc_leading_zeros_ui.h | 18 +++++++
libc/src/stdbit/stdc_leading_zeros_ul.cpp | 20 ++++++++
libc/src/stdbit/stdc_leading_zeros_ul.h | 18 +++++++
libc/src/stdbit/stdc_leading_zeros_ull.cpp | 20 ++++++++
libc/src/stdbit/stdc_leading_zeros_ull.h | 18 +++++++
libc/src/stdbit/stdc_leading_zeros_us.cpp | 20 ++++++++
libc/src/stdbit/stdc_leading_zeros_us.h | 18 +++++++
libc/test/include/CMakeLists.txt | 19 +++++++
libc/test/include/stdbit_test.cpp | 22 ++++++++
libc/test/src/CMakeLists.txt | 11 ++--
libc/test/src/stdbit/CMakeLists.txt | 51 +++++++++++++++++++
.../src/stdbit/stdc_leading_zeros_uc_test.cpp | 22 ++++++++
.../src/stdbit/stdc_leading_zeros_ui_test.cpp | 23 +++++++++
.../src/stdbit/stdc_leading_zeros_ul_test.cpp | 23 +++++++++
.../stdbit/stdc_leading_zeros_ull_test.cpp | 24 +++++++++
.../src/stdbit/stdc_leading_zeros_us_test.cpp | 22 ++++++++
29 files changed, 522 insertions(+), 8 deletions(-)
create mode 100644 libc/include/llvm-libc-macros/stdbit-macros.h
create mode 100644 libc/include/stdbit.h.def
create mode 100644 libc/src/stdbit/CMakeLists.txt
create mode 100644 libc/src/stdbit/stdc_leading_zeros_uc.cpp
create mode 100644 libc/src/stdbit/stdc_leading_zeros_uc.h
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ui.cpp
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ui.h
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ul.cpp
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ul.h
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ull.cpp
create mode 100644 libc/src/stdbit/stdc_leading_zeros_ull.h
create mode 100644 libc/src/stdbit/stdc_leading_zeros_us.cpp
create mode 100644 libc/src/stdbit/stdc_leading_zeros_us.h
create mode 100644 libc/test/include/stdbit_test.cpp
create mode 100644 libc/test/src/stdbit/CMakeLists.txt
create mode 100644 libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
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..183328877c3da8
--- /dev/null
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -0,0 +1,19 @@
+//===-- 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..5747a8c2f35318
--- /dev/null
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -0,0 +1,39 @@
+add_entrypoint_object(
+ stdc_leading_zeros_uc
+ SRCS
+ stdc_leading_zeros_uc.cpp
+ HDRS
+ stdc_leading_zeros_uc.h
+)
+
+add_entrypoint_object(
+ stdc_leading_zeros_us
+ SRCS
+ stdc_leading_zeros_us.cpp
+ HDRS
+ stdc_leading_zeros_us.h
+)
+
+add_entrypoint_object(
+ stdc_leading_zeros_ui
+ SRCS
+ stdc_leading_zeros_ui.cpp
+ HDRS
+ stdc_leading_zeros_ui.h
+)
+
+add_entrypoint_object(
+ stdc_leading_zeros_ul
+ SRCS
+ stdc_leading_zeros_ul.cpp
+ HDRS
+ stdc_leading_zeros_ul.h
+)
+
+add_entrypoint_object(
+ stdc_leading_zeros_ull
+ SRCS
+ stdc_leading_zeros_ull.cpp
+ HDRS
+ stdc_leading_zeros_ull.h
+)
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..a985700274d259
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_uc.cpp
@@ -0,0 +1,20 @@
+//===-- 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..4ce56be04ff358
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ul.cpp
@@ -0,0 +1,20 @@
+//===-- 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..5abd6a6e7aab9f
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_ull.cpp
@@ -0,0 +1,20 @@
+//===-- 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..0c14d5374a9737
--- /dev/null
+++ b/libc/src/stdbit/stdc_leading_zeros_us.cpp
@@ -0,0 +1,20 @@
+//===-- 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..69a08822b06dc6 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -14,3 +14,22 @@ add_libc_test(
# This is needed because the __containerof macro uses statement expression.
-Wno-gnu-statement-expression-from-macro-expansion
)
+
+add_libc_test(
+ stdbit_test
+ SUITE
+ libc_include_tests
+ SRCS
+ stdbit_test.cpp
+ DEPENDS
+ libc.__support.CPP.limits
+ libc.include.llvm-libc-macros.stdbit_macros
+ libc.src.stdbit.stdc_leading_zeros_uc
+ 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_zeros_us
+ COMPILE_OPTIONS
+ # stdbit is full of type generic macros implemented via C11 _Generic.
+ -Wno-c11-extensions
+)
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
new file mode 100644
index 00000000000000..c2e019d1096ea1
--- /dev/null
+++ b/libc/test/include/stdbit_test.cpp
@@ -0,0 +1,22 @@
+//===-- 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 "llvm-libc-macros/stdbit-macros.h"
+#include "src/__support/CPP/limits.h" // UINT_WIDTH
+#include "src/stdbit/stdc_leading_zeros_uc.h"
+#include "src/stdbit/stdc_leading_zeros_ui.h"
+#include "src/stdbit/stdc_leading_zeros_ul.h"
+#include "src/stdbit/stdc_leading_zeros_ull.h"
+#include "src/stdbit/stdc_leading_zeros_us.h"
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
+ using namespace LIBC_NAMESPACE;
+ EXPECT_EQ(stdc_leading_zeros(0U), static_cast<unsigned>(UINT_WIDTH));
+}
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..76f32dea1896e7
--- /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/stdbit/stdc_leading_zeros_uc.h"
+#include "src/__support/CPP/limits.h"
+#include "test/UnitTest/Test.h"
+
+#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_uc((x))
+
+TEST(LlvmLibcStdcLeadingZerosUcTest, Zero) {
+ EXPECT_EQ(LZ(0U), static_cast<unsigned char>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUcTest, OneHot) {
+ for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+ EXPECT_EQ(LZ(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..e1d6653fd3316f
--- /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/stdbit/stdc_leading_zeros_ui.h"
+#include "src/__support/CPP/limits.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_ui((x))
+
+TEST(LlvmLibcStdcLeadingZerosUiTest, Zero) {
+ EXPECT_EQ(LZ(0U), static_cast<unsigned>(INT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUiTest, OneHot) {
+ for (unsigned i = 0U; i != INT_WIDTH; ++i)
+ EXPECT_EQ(LZ(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..fe085533dcb4a3
--- /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/stdbit/stdc_leading_zeros_ul.h"
+#include "src/__support/CPP/limits.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_ul((x))
+
+TEST(LlvmLibcStdcLeadingZerosUlTest, Zero) {
+ EXPECT_EQ(LZ(0UL), static_cast<unsigned long>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUlTest, OneHot) {
+ for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+ EXPECT_EQ(LZ(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..c6ef95dab804b5
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
@@ -0,0 +1,24 @@
+//===-- 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/stdbit/stdc_leading_zeros_ull.h"
+#include "src/__support/CPP/limits.h"
+#include "test/UnitTest/Test.h"
+#include <stddef.h>
+
+#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_ull((x))
+
+TEST(LlvmLibcStdcLeadingZerosUllTest, Zero) {
+ EXPECT_EQ(LZ(0ULL), static_cast<unsigned long long>(ULLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUllTest, OneHot) {
+ for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+ EXPECT_EQ(LZ(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..0e50eeae8af383
--- /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/stdbit/stdc_leading_zeros_us.h"
+#include "src/__support/CPP/limits.h"
+#include "test/UnitTest/Test.h"
+
+#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_us((x))
+
+TEST(LlvmLibcStdcLeadingZerosUsTest, Zero) {
+ EXPECT_EQ(LZ(0U), static_cast<unsigned short>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcLeadingZerosUsTest, OneHot) {
+ for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+ EXPECT_EQ(LZ(1U << i), static_cast<unsigned short>(USHRT_WIDTH - i - 1));
+}
>From 29581cf1bdadc9768a31223b506bd207d44a1d2d Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 26 Jan 2024 16:09:22 -0800
Subject: [PATCH 2/2] reformat
---
libc/include/llvm-libc-macros/stdbit-macros.h | 13 +++++++------
libc/src/stdbit/stdc_leading_zeros_uc.cpp | 3 ++-
libc/src/stdbit/stdc_leading_zeros_ul.cpp | 3 ++-
libc/src/stdbit/stdc_leading_zeros_ull.cpp | 3 ++-
libc/src/stdbit/stdc_leading_zeros_us.cpp | 3 ++-
libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp | 2 +-
libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp | 2 +-
libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp | 2 +-
.../test/src/stdbit/stdc_leading_zeros_ull_test.cpp | 5 +++--
libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp | 2 +-
10 files changed, 22 insertions(+), 16 deletions(-)
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 183328877c3da8..febe95fe0a1e3c 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -9,11 +9,12 @@
#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)
+#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/src/stdbit/stdc_leading_zeros_uc.cpp b/libc/src/stdbit/stdc_leading_zeros_uc.cpp
index a985700274d259..8c27043e055bf9 100644
--- a/libc/src/stdbit/stdc_leading_zeros_uc.cpp
+++ b/libc/src/stdbit/stdc_leading_zeros_uc.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned char, stdc_leading_zeros_uc, (unsigned char value)) {
+LLVM_LIBC_FUNCTION(unsigned char, stdc_leading_zeros_uc,
+ (unsigned char value)) {
return static_cast<unsigned char>(cpp::countl_zero(value));
}
diff --git a/libc/src/stdbit/stdc_leading_zeros_ul.cpp b/libc/src/stdbit/stdc_leading_zeros_ul.cpp
index 4ce56be04ff358..cf21ff449a8bf5 100644
--- a/libc/src/stdbit/stdc_leading_zeros_ul.cpp
+++ b/libc/src/stdbit/stdc_leading_zeros_ul.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned long, stdc_leading_zeros_ul, (unsigned long value)) {
+LLVM_LIBC_FUNCTION(unsigned long, stdc_leading_zeros_ul,
+ (unsigned long value)) {
return static_cast<unsigned long>(cpp::countl_zero(value));
}
diff --git a/libc/src/stdbit/stdc_leading_zeros_ull.cpp b/libc/src/stdbit/stdc_leading_zeros_ull.cpp
index 5abd6a6e7aab9f..d488559bbd14c2 100644
--- a/libc/src/stdbit/stdc_leading_zeros_ull.cpp
+++ b/libc/src/stdbit/stdc_leading_zeros_ull.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned long long, stdc_leading_zeros_ull, (unsigned long long value)) {
+LLVM_LIBC_FUNCTION(unsigned long long, stdc_leading_zeros_ull,
+ (unsigned long long value)) {
return static_cast<unsigned long long>(cpp::countl_zero(value));
}
diff --git a/libc/src/stdbit/stdc_leading_zeros_us.cpp b/libc/src/stdbit/stdc_leading_zeros_us.cpp
index 0c14d5374a9737..84df2b842d6950 100644
--- a/libc/src/stdbit/stdc_leading_zeros_us.cpp
+++ b/libc/src/stdbit/stdc_leading_zeros_us.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned short, stdc_leading_zeros_us, (unsigned short value)) {
+LLVM_LIBC_FUNCTION(unsigned short, stdc_leading_zeros_us,
+ (unsigned short value)) {
return static_cast<unsigned short>(cpp::countl_zero(value));
}
diff --git a/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
index 76f32dea1896e7..ad87c14804e39c 100644
--- a/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
+++ b/libc/test/src/stdbit/stdc_leading_zeros_uc_test.cpp
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdbit/stdc_leading_zeros_uc.h"
#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_uc.h"
#include "test/UnitTest/Test.h"
#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_uc((x))
diff --git a/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
index e1d6653fd3316f..a3dde44a8c6630 100644
--- a/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ui_test.cpp
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdbit/stdc_leading_zeros_ui.h"
#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ui.h"
#include "test/UnitTest/Test.h"
#include <stddef.h>
diff --git a/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
index fe085533dcb4a3..1a1df653c88049 100644
--- a/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ul_test.cpp
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdbit/stdc_leading_zeros_ul.h"
#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ul.h"
#include "test/UnitTest/Test.h"
#include <stddef.h>
diff --git a/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
index c6ef95dab804b5..fe5058c4d2f0d3 100644
--- a/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
+++ b/libc/test/src/stdbit/stdc_leading_zeros_ull_test.cpp
@@ -1,4 +1,5 @@
-//===-- Unittests for stdc_leading_zeros_ull -------------------------------===//
+//===-- 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.
@@ -6,8 +7,8 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdbit/stdc_leading_zeros_ull.h"
#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_ull.h"
#include "test/UnitTest/Test.h"
#include <stddef.h>
diff --git a/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp b/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
index 0e50eeae8af383..8324f3a96922d2 100644
--- a/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
+++ b/libc/test/src/stdbit/stdc_leading_zeros_us_test.cpp
@@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
-#include "src/stdbit/stdc_leading_zeros_us.h"
#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_leading_zeros_us.h"
#include "test/UnitTest/Test.h"
#define LZ(x) LIBC_NAMESPACE::stdc_leading_zeros_us((x))
More information about the libc-commits
mailing list