[libc-commits] [libc] [libc][stdbit] implement stdc_trailing_zeros (C23) (PR #80344)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Thu Feb 1 13:53:48 PST 2024
https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/80344
>From 995f36dadd927ee7c911b208946d92ef8b6d0396 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 1 Feb 2024 12:24:18 -0800
Subject: [PATCH 1/2] [libc][stdbit] implement stdc_trailing_zeros (C23)
---
libc/config/linux/x86_64/entrypoints.txt | 5 ++
libc/include/llvm-libc-macros/stdbit-macros.h | 22 ++++++++
libc/src/stdbit/CMakeLists.txt | 50 +++++++++++++++++
libc/src/stdbit/stdc_trailing_zeros_uc.cpp | 20 +++++++
libc/src/stdbit/stdc_trailing_zeros_uc.h | 18 ++++++
libc/src/stdbit/stdc_trailing_zeros_ui.cpp | 20 +++++++
libc/src/stdbit/stdc_trailing_zeros_ui.h | 18 ++++++
libc/src/stdbit/stdc_trailing_zeros_ul.cpp | 20 +++++++
libc/src/stdbit/stdc_trailing_zeros_ul.h | 18 ++++++
libc/src/stdbit/stdc_trailing_zeros_ull.cpp | 20 +++++++
libc/src/stdbit/stdc_trailing_zeros_ull.h | 18 ++++++
libc/src/stdbit/stdc_trailing_zeros_us.cpp | 20 +++++++
libc/src/stdbit/stdc_trailing_zeros_us.h | 18 ++++++
libc/test/include/stdbit_test.cpp | 13 +++++
libc/test/src/stdbit/CMakeLists.txt | 55 +++++++++++++++++++
.../stdbit/stdc_trailing_zeros_uc_test.cpp | 21 +++++++
.../stdbit/stdc_trailing_zeros_ui_test.cpp | 21 +++++++
.../stdbit/stdc_trailing_zeros_ul_test.cpp | 21 +++++++
.../stdbit/stdc_trailing_zeros_ull_test.cpp | 21 +++++++
.../stdbit/stdc_trailing_zeros_us_test.cpp | 21 +++++++
20 files changed, 440 insertions(+)
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_uc.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_uc.h
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ui.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ui.h
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ul.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ul.h
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ull.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_ull.h
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_us.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_zeros_us.h
create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 9946b93c346ce..3ba676fc496ec 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -102,6 +102,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_leading_ones_ui
libc.src.stdbit.stdc_leading_ones_ul
libc.src.stdbit.stdc_leading_ones_ull
+ libc.src.stdbit.stdc_trailing_zeros_uc
+ libc.src.stdbit.stdc_trailing_zeros_us
+ libc.src.stdbit.stdc_trailing_zeros_ui
+ libc.src.stdbit.stdc_trailing_zeros_ul
+ libc.src.stdbit.stdc_trailing_zeros_ull
# stdlib.h entrypoints
libc.src.stdlib.abs
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index cc964a5268b0d..a36d221b3c835 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -40,6 +40,21 @@ inline unsigned stdc_leading_ones(unsigned long x) {
inline unsigned stdc_leading_ones(unsigned long long x) {
return stdc_leading_ones_ull(x);
}
+inline unsigned stdc_trailing_zeros(unsigned char x) {
+ return stdc_trailing_zeros_uc(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned short x) {
+ return stdc_trailing_zeros_us(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned x) {
+ return stdc_trailing_zeros_ui(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned long x) {
+ return stdc_trailing_zeros_ul(x);
+}
+inline unsigned stdc_trailing_zeros(unsigned long long x) {
+ return stdc_trailing_zeros_ull(x);
+}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
@@ -55,6 +70,13 @@ inline unsigned stdc_leading_ones(unsigned long long x) {
unsigned: stdc_leading_ones_ui, \
unsigned long: stdc_leading_ones_ul, \
unsigned long long: stdc_leading_ones_ull)(x)
+#define stdc_trailing_zeros(x) \
+ _Generic((x), \
+ unsigned char: stdc_trailing_zeros_uc, \
+ unsigned short: stdc_trailing_zeros_us, \
+ unsigned: stdc_trailing_zeros_ui, \
+ unsigned long: stdc_trailing_zeros_ul, \
+ unsigned long long: stdc_trailing_zeros_ull)(x)
#endif // __cplusplus
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index d7daff44dfda6..1c9f1b90281cd 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -97,3 +97,53 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.CPP.bit
)
+
+add_entrypoint_object(
+ stdc_trailing_zeros_uc
+ SRCS
+ stdc_trailing_zeros_uc.cpp
+ HDRS
+ stdc_trailing_zeros_uc.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+ stdc_trailing_zeros_us
+ SRCS
+ stdc_trailing_zeros_us.cpp
+ HDRS
+ stdc_trailing_zeros_us.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+ stdc_trailing_zeros_ui
+ SRCS
+ stdc_trailing_zeros_ui.cpp
+ HDRS
+ stdc_trailing_zeros_ui.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+ stdc_trailing_zeros_ul
+ SRCS
+ stdc_trailing_zeros_ul.cpp
+ HDRS
+ stdc_trailing_zeros_ul.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+)
+
+add_entrypoint_object(
+ stdc_trailing_zeros_ull
+ SRCS
+ stdc_trailing_zeros_ull.cpp
+ HDRS
+ stdc_trailing_zeros_ull.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+)
diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.cpp b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp
new file mode 100644
index 0000000000000..36924c5a053ad
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_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_trailing_zeros_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_uc, (unsigned char value)) {
+ return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_uc.h b/libc/src/stdbit/stdc_trailing_zeros_uc.h
new file mode 100644
index 0000000000000..866201e5acea8
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_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_TRAILING_ZEROS_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UC_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.cpp b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp
new file mode 100644
index 0000000000000..a264fd97f251f
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_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_trailing_zeros_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ui, (unsigned value)) {
+ return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ui.h b/libc/src/stdbit/stdc_trailing_zeros_ui.h
new file mode 100644
index 0000000000000..0642e312f4fe7
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_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_TRAILING_ZEROS_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UI_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ul.cpp b/libc/src/stdbit/stdc_trailing_zeros_ul.cpp
new file mode 100644
index 0000000000000..8e0c36cb09965
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_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_trailing_zeros_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ul, (unsigned long value)) {
+ return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ul.h b/libc/src/stdbit/stdc_trailing_zeros_ul.h
new file mode 100644
index 0000000000000..e10b4474753a0
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_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_TRAILING_ZEROS_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_UL_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ull.cpp b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
new file mode 100644
index 0000000000000..9e61eccf2e4ef
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_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_trailing_zeros_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ull, (unsigned long long value)) {
+ return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ull.h b/libc/src/stdbit/stdc_trailing_zeros_ull.h
new file mode 100644
index 0000000000000..f95169d29f45e
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_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_TRAILING_ZEROS_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_ULL_H
diff --git a/libc/src/stdbit/stdc_trailing_zeros_us.cpp b/libc/src/stdbit/stdc_trailing_zeros_us.cpp
new file mode 100644
index 0000000000000..a5b9f4a7d8498
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_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_trailing_zeros_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_us, (unsigned short value)) {
+ return static_cast<unsigned>(cpp::countr_zero(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_zeros_us.h b/libc/src/stdbit/stdc_trailing_zeros_us.h
new file mode 100644
index 0000000000000..ddbdf0d647abd
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_zeros_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_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_TRAILING_ZEROS_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_zeros_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 6e48b0f2f7594..858dc08bfd70c 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -33,6 +33,11 @@ unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; }
unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; }
unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; }
unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBFU; }
+unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; }
+unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; }
+unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; }
+unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; }
+unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCFU; }
}
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -52,3 +57,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
EXPECT_EQ(stdc_leading_ones(0UL), 0xBDU);
EXPECT_EQ(stdc_leading_ones(0ULL), 0xBFU);
}
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
+ EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned char>(0U)), 0xCAU);
+ EXPECT_EQ(stdc_trailing_zeros(static_cast<unsigned short>(0U)), 0xCBU);
+ EXPECT_EQ(stdc_trailing_zeros(0U), 0xCCU);
+ EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
+ EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCFU);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index a8c3c9f883532..8a7a227eea47f 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -110,3 +110,58 @@ add_libc_test(
libc.src.stdbit.stdc_leading_ones_ull
)
+add_libc_test(
+ stdc_trailing_zeros_uc_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_trailing_zeros_uc_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_trailing_zeros_uc
+)
+
+add_libc_test(
+ stdc_trailing_zeros_us_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_trailing_zeros_us_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_trailing_zeros_us
+)
+
+add_libc_test(
+ stdc_trailing_zeros_ui_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_trailing_zeros_ui_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_trailing_zeros_ui
+)
+
+add_libc_test(
+ stdc_trailing_zeros_ul_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_trailing_zeros_ul_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_trailing_zeros_ul
+)
+
+add_libc_test(
+ stdc_trailing_zeros_ull_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_trailing_zeros_ull_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_trailing_zeros_ull
+)
+
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
new file mode 100644
index 0000000000000..725b276d9de4c
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_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_trailing_zeros_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingZerosUcTest, Zero) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_uc(0U),
+ static_cast<unsigned>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUcTest, OneHot) {
+ for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_uc(1U << i), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
new file mode 100644
index 0000000000000..1ed7e66bf9097
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_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_trailing_zeros_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingZerosUiTest, Zero) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ui(0U),
+ static_cast<unsigned>(UINT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUiTest, OneHot) {
+ for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ui(1U << i), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
new file mode 100644
index 0000000000000..c46d48b49ec43
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_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_trailing_zeros_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingZerosUlTest, Zero) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ul(0U),
+ static_cast<unsigned>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUlTest, OneHot) {
+ for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ul(1UL << i), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
new file mode 100644
index 0000000000000..e925b785dc57a
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_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_trailing_zeros_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingZerosUllTest, Zero) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ull(0U),
+ static_cast<unsigned>(LLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUllTest, OneHot) {
+ for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ull(1ULL << i), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
new file mode 100644
index 0000000000000..6e56a99fedcc1
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_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_trailing_zeros_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingZerosUsTest, Zero) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_us(0U),
+ static_cast<unsigned>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingZerosUsTest, OneHot) {
+ for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_us(1U << i), i);
+}
>From 984693dbb0ed71e04fb4e9579913ae389eb1c31d Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 1 Feb 2024 13:52:46 -0800
Subject: [PATCH 2/2] format
---
libc/include/llvm-libc-macros/stdbit-macros.h | 10 +++++-----
libc/src/stdbit/stdc_trailing_zeros_ull.cpp | 3 ++-
libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp | 2 +-
libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp | 2 +-
libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp | 2 +-
libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp | 2 +-
6 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index a36d221b3c835..15e391288357e 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -70,12 +70,12 @@ inline unsigned stdc_trailing_zeros(unsigned long long x) {
unsigned: stdc_leading_ones_ui, \
unsigned long: stdc_leading_ones_ul, \
unsigned long long: stdc_leading_ones_ull)(x)
-#define stdc_trailing_zeros(x) \
+#define stdc_trailing_zeros(x) \
_Generic((x), \
- unsigned char: stdc_trailing_zeros_uc, \
- unsigned short: stdc_trailing_zeros_us, \
- unsigned: stdc_trailing_zeros_ui, \
- unsigned long: stdc_trailing_zeros_ul, \
+ unsigned char: stdc_trailing_zeros_uc, \
+ unsigned short: stdc_trailing_zeros_us, \
+ unsigned: stdc_trailing_zeros_ui, \
+ unsigned long: stdc_trailing_zeros_ul, \
unsigned long long: stdc_trailing_zeros_ull)(x)
#endif // __cplusplus
diff --git a/libc/src/stdbit/stdc_trailing_zeros_ull.cpp b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
index 9e61eccf2e4ef..77cb20cb1ba45 100644
--- a/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
+++ b/libc/src/stdbit/stdc_trailing_zeros_ull.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ull, (unsigned long long value)) {
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ull,
+ (unsigned long long value)) {
return static_cast<unsigned>(cpp::countr_zero(value));
}
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
index 725b276d9de4c..c02b518865d9f 100644
--- a/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_uc_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_trailing_zeros_uc --------------------------------===//
+//===-- Unittests for stdc_trailing_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.
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
index 1ed7e66bf9097..ad9b126335172 100644
--- a/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ui_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_trailing_zeros_ui --------------------------------===//
+//===-- Unittests for stdc_trailing_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.
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
index c46d48b49ec43..6d7f4b3cb093d 100644
--- a/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ul_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_trailing_zeros_ul --------------------------------===//
+//===-- Unittests for stdc_trailing_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.
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
index 6e56a99fedcc1..a9f8327dfd914 100644
--- a/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_us_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdc_trailing_zeros_us --------------------------------===//
+//===-- Unittests for stdc_trailing_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.
More information about the libc-commits
mailing list