[libc-commits] [libc] [libc][stdbit] implement stdc_trailing_ones (C23) (PR #80459)
Nick Desaulniers via libc-commits
libc-commits at lists.llvm.org
Fri Feb 2 08:51:34 PST 2024
https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/80459
>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/8] [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/8] 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.
>From 68d1f7fbb0e8d07e89e9497f378a74de08c4f868 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 1 Feb 2024 13:58:05 -0800
Subject: [PATCH 3/8] refactor cmake; getting tired of writing these already
---
libc/src/stdbit/CMakeLists.txt | 168 +++----------------------
libc/test/src/stdbit/CMakeLists.txt | 185 +++-------------------------
2 files changed, 39 insertions(+), 314 deletions(-)
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 1c9f1b90281cd..e645d246a2dd5 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -1,149 +1,19 @@
-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
-)
-
-add_entrypoint_object(
- stdc_leading_ones_uc
- SRCS
- stdc_leading_ones_uc.cpp
- HDRS
- stdc_leading_ones_uc.h
- DEPENDS
- libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
- stdc_leading_ones_us
- SRCS
- stdc_leading_ones_us.cpp
- HDRS
- stdc_leading_ones_us.h
- DEPENDS
- libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
- stdc_leading_ones_ui
- SRCS
- stdc_leading_ones_ui.cpp
- HDRS
- stdc_leading_ones_ui.h
- DEPENDS
- libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
- stdc_leading_ones_ul
- SRCS
- stdc_leading_ones_ul.cpp
- HDRS
- stdc_leading_ones_ul.h
- DEPENDS
- libc.src.__support.CPP.bit
-)
-
-add_entrypoint_object(
- stdc_leading_ones_ull
- SRCS
- stdc_leading_ones_ull.cpp
- HDRS
- stdc_leading_ones_ull.h
- DEPENDS
- libc.src.__support.CPP.bit
-)
-
-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
-)
+list(APPEND prefixes
+ leading_zeros
+ leading_ones
+ trailing_zeros
+)
+list(APPEND suffixes c s i l ll)
+foreach(prefix IN LISTS prefixes)
+ foreach(suffix IN LISTS suffixes)
+ add_entrypoint_object(
+ stdc_${prefix}_u${suffix}
+ SRCS
+ stdc_${prefix}_u${suffix}.cpp
+ HDRS
+ stdc_${prefix}_u${suffix}.h
+ DEPENDS
+ libc.src.__support.CPP.bit
+ )
+ endforeach()
+endforeach()
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index 8a7a227eea47f..2699461fa2fbd 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -1,167 +1,22 @@
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.__support.CPP.limits
- 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.__support.CPP.limits
- 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.__support.CPP.limits
- 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.__support.CPP.limits
- 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.__support.CPP.limits
- libc.src.stdbit.stdc_leading_zeros_ull
-)
-
-add_libc_test(
- stdc_leading_ones_uc_test
- SUITE
- libc-stdbit-tests
- SRCS
- stdc_leading_ones_uc_test.cpp
- DEPENDS
- libc.src.__support.CPP.limits
- libc.src.stdbit.stdc_leading_ones_uc
-)
-
-add_libc_test(
- stdc_leading_ones_us_test
- SUITE
- libc-stdbit-tests
- SRCS
- stdc_leading_ones_us_test.cpp
- DEPENDS
- libc.src.__support.CPP.limits
- libc.src.stdbit.stdc_leading_ones_us
-)
-
-add_libc_test(
- stdc_leading_ones_ui_test
- SUITE
- libc-stdbit-tests
- SRCS
- stdc_leading_ones_ui_test.cpp
- DEPENDS
- libc.src.__support.CPP.limits
- libc.src.stdbit.stdc_leading_ones_ui
-)
-
-add_libc_test(
- stdc_leading_ones_ul_test
- SUITE
- libc-stdbit-tests
- SRCS
- stdc_leading_ones_ul_test.cpp
- DEPENDS
- libc.src.__support.CPP.limits
- libc.src.stdbit.stdc_leading_ones_ul
-)
-
-add_libc_test(
- stdc_leading_ones_ull_test
- SUITE
- libc-stdbit-tests
- SRCS
- stdc_leading_ones_ull_test.cpp
- DEPENDS
- libc.src.__support.CPP.limits
- libc.src.stdbit.stdc_leading_ones_ull
-)
-
-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
-)
-
+list(APPEND prefixes
+ leading_zeros
+ leading_ones
+ trailing_zeros
+)
+list(APPEND suffixes c s i l ll)
+foreach(prefix IN LISTS prefixes)
+ foreach(suffix IN LISTS suffixes)
+ add_libc_test(
+ stdc_${prefix}_u${suffix}_test
+ SUITE
+ libc-stdbit-tests
+ SRCS
+ stdc_${prefix}_u${suffix}_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.limits
+ libc.src.stdbit.stdc_${prefix}_u${suffix}
+ )
+ endforeach()
+endforeach()
>From ccc4daa90b3da66d1afb9a125d362cd14bc1b92f Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 2 Feb 2024 08:19:35 -0800
Subject: [PATCH 4/8] s/LLONG_WIDTH/ULLONG_WIDTH/
---
libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
index e925b785dc57a..64b93b12e6051 100644
--- a/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
+++ b/libc/test/src/stdbit/stdc_trailing_zeros_ull_test.cpp
@@ -12,7 +12,7 @@
TEST(LlvmLibcStdcTrailingZerosUllTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_zeros_ull(0U),
- static_cast<unsigned>(LLONG_WIDTH));
+ static_cast<unsigned>(ULLONG_WIDTH));
}
TEST(LlvmLibcStdcTrailingZerosUllTest, OneHot) {
>From 4cdccb50e5f41785cf1c498999f4d73d9a65066c Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 2 Feb 2024 08:21:00 -0800
Subject: [PATCH 5/8] update stdc.td
---
libc/spec/stdc.td | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 6ff2c7c613696..dc3edf3ce1b65 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -784,7 +784,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
- FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+ FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+ FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+ FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+ FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+ FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+ FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;
>From c6f1206d622286629761dd67bcbdf4fc6f2dc3cf Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Thu, 1 Feb 2024 16:00:24 -0800
Subject: [PATCH 6/8] [libc][stdbit] implement stdc_trailing_ones (C23)
---
libc/config/linux/x86_64/entrypoints.txt | 5 +++++
libc/include/llvm-libc-macros/stdbit-macros.h | 22 +++++++++++++++++++
libc/spec/stdc.td | 7 +++++-
libc/src/stdbit/CMakeLists.txt | 1 +
libc/src/stdbit/stdc_trailing_ones_uc.cpp | 20 +++++++++++++++++
libc/src/stdbit/stdc_trailing_ones_uc.h | 18 +++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ui.cpp | 20 +++++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ui.h | 18 +++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ul.cpp | 20 +++++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ul.h | 18 +++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ull.cpp | 20 +++++++++++++++++
libc/src/stdbit/stdc_trailing_ones_ull.h | 18 +++++++++++++++
libc/src/stdbit/stdc_trailing_ones_us.cpp | 20 +++++++++++++++++
libc/src/stdbit/stdc_trailing_ones_us.h | 18 +++++++++++++++
libc/test/include/stdbit_test.cpp | 13 +++++++++++
libc/test/src/stdbit/CMakeLists.txt | 1 +
.../src/stdbit/stdc_trailing_ones_uc_test.cpp | 21 ++++++++++++++++++
.../src/stdbit/stdc_trailing_ones_ui_test.cpp | 21 ++++++++++++++++++
.../src/stdbit/stdc_trailing_ones_ul_test.cpp | 21 ++++++++++++++++++
.../src/stdbit/stdc_trailing_ones_us_test.cpp | 21 ++++++++++++++++++
20 files changed, 322 insertions(+), 1 deletion(-)
create mode 100644 libc/src/stdbit/stdc_trailing_ones_uc.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_ones_uc.h
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ui.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ui.h
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ul.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ul.h
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ull.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_ones_ull.h
create mode 100644 libc/src/stdbit/stdc_trailing_ones_us.cpp
create mode 100644 libc/src/stdbit/stdc_trailing_ones_us.h
create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3ba676fc496ec..82c40a5cbcc06 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -107,6 +107,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_trailing_zeros_ui
libc.src.stdbit.stdc_trailing_zeros_ul
libc.src.stdbit.stdc_trailing_zeros_ull
+ libc.src.stdbit.stdc_trailing_ones_uc
+ libc.src.stdbit.stdc_trailing_ones_us
+ libc.src.stdbit.stdc_trailing_ones_ui
+ libc.src.stdbit.stdc_trailing_ones_ul
+ libc.src.stdbit.stdc_trailing_ones_ull
# stdlib.h entrypoints
libc.src.stdlib.abs
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 15e391288357e..840246b795635 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -55,6 +55,21 @@ inline unsigned stdc_trailing_zeros(unsigned long x) {
inline unsigned stdc_trailing_zeros(unsigned long long x) {
return stdc_trailing_zeros_ull(x);
}
+inline unsigned stdc_trailing_ones(unsigned char x) {
+ return stdc_trailing_ones_uc(x);
+}
+inline unsigned stdc_trailing_ones(unsigned short x) {
+ return stdc_trailing_ones_us(x);
+}
+inline unsigned stdc_trailing_ones(unsigned x) {
+ return stdc_trailing_ones_ui(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long x) {
+ return stdc_trailing_ones_ul(x);
+}
+inline unsigned stdc_trailing_ones(unsigned long long x) {
+ return stdc_trailing_ones_ull(x);
+}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
@@ -77,6 +92,13 @@ inline unsigned stdc_trailing_zeros(unsigned long long x) {
unsigned: stdc_trailing_zeros_ui, \
unsigned long: stdc_trailing_zeros_ul, \
unsigned long long: stdc_trailing_zeros_ull)(x)
+#define stdc_trailing_ones(x) \
+ _Generic((x), \
+ unsigned char: stdc_trailing_ones_uc, \
+ unsigned short: stdc_trailing_ones_us, \
+ unsigned: stdc_trailing_ones_ui, \
+ unsigned long: stdc_trailing_ones_ul, \
+ unsigned long long: stdc_trailing_ones_ull)(x)
#endif // __cplusplus
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index dc3edf3ce1b65..b9fd3bac317b6 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -789,7 +789,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
- FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
+ FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+ FunctionSpec<"stdc_trailing_ones_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+ FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+ FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+ FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+ FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index e645d246a2dd5..1904f6f77699f 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -2,6 +2,7 @@ list(APPEND prefixes
leading_zeros
leading_ones
trailing_zeros
+ trailing_ones
)
list(APPEND suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.cpp b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
new file mode 100644
index 0000000000000..eabb21367e285
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_uc ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_trailing_ones_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_uc, (unsigned char value)) {
+ return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_uc.h b/libc/src/stdbit/stdc_trailing_ones_uc.h
new file mode 100644
index 0000000000000..9736e21e1f280
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_uc --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UC_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.cpp b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
new file mode 100644
index 0000000000000..87eb54fe5f026
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_ui ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_trailing_ones_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ui, (unsigned value)) {
+ return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ui.h b/libc/src/stdbit/stdc_trailing_ones_ui.h
new file mode 100644
index 0000000000000..aad7fcf0daf55
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_ui --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UI_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.cpp b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
new file mode 100644
index 0000000000000..6d358a21ac337
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_ul ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_trailing_ones_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ul, (unsigned long value)) {
+ return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ul.h b/libc/src/stdbit/stdc_trailing_ones_ul.h
new file mode 100644
index 0000000000000..80589501b1ed2
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_ul --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_UL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.cpp b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
new file mode 100644
index 0000000000000..883b1f2ff5c07
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_ull --------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_trailing_ones_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ull, (unsigned long long value)) {
+ return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.h b/libc/src/stdbit/stdc_trailing_ones_ull.h
new file mode 100644
index 0000000000000..3d3f0b306789a
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_ull --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_ULL_H
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.cpp b/libc/src/stdbit/stdc_trailing_ones_us.cpp
new file mode 100644
index 0000000000000..ee7ff4f78d489
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_trailing_ones_us ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdbit/stdc_trailing_ones_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_us, (unsigned short value)) {
+ return static_cast<unsigned>(cpp::countr_one(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_trailing_ones_us.h b/libc/src/stdbit/stdc_trailing_ones_us.h
new file mode 100644
index 0000000000000..b783cd22ad2de
--- /dev/null
+++ b/libc/src/stdbit/stdc_trailing_ones_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_trailing_ones_us --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_trailing_ones_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ONES_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 858dc08bfd70c..8d5d4c162938e 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -38,6 +38,11 @@ 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; }
+unsigned stdc_trailing_ones_uc(unsigned char) noexcept { return 0xDAU; }
+unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
+unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
+unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
+unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDFU; }
}
#include "include/llvm-libc-macros/stdbit-macros.h"
@@ -65,3 +70,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingZeros) {
EXPECT_EQ(stdc_trailing_zeros(0UL), 0xCDU);
EXPECT_EQ(stdc_trailing_zeros(0ULL), 0xCFU);
}
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
+ EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned char>(0U)), 0xDAU);
+ EXPECT_EQ(stdc_trailing_ones(static_cast<unsigned short>(0U)), 0xDBU);
+ EXPECT_EQ(stdc_trailing_ones(0U), 0xDCU);
+ EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
+ EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDFU);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index 2699461fa2fbd..a5114c06b3992 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -4,6 +4,7 @@ list(APPEND prefixes
leading_zeros
leading_ones
trailing_zeros
+ trailing_ones
)
list(APPEND suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
new file mode 100644
index 0000000000000..79d4e5b8b8032
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_uc_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_uc -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_trailing_ones_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUcTest, ALL) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_uc(UCHAR_MAX),
+ static_cast<unsigned>(UCHAR_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUcTest, ZeroHot) {
+ for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_uc(~(1U << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
new file mode 100644
index 0000000000000..51e49f1e9e5fe
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ui_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_ui -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_trailing_ones_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUiTest, ALL) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ui(UINT_MAX),
+ static_cast<unsigned>(UINT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUiTest, ZeroHot) {
+ for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ui(~(1U << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
new file mode 100644
index 0000000000000..2aebe2e814ca9
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_ul -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_trailing_ones_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUlTest, ALL) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ul(ULONG_MAX),
+ static_cast<unsigned>(ULONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUlTest, ZeroHot) {
+ for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ul(~(1UL << i)), i);
+}
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp
new file mode 100644
index 0000000000000..7ab15743ed1e0
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_us_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_us -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_trailing_ones_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUsTest, ALL) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_us(USHRT_MAX),
+ static_cast<unsigned>(USHRT_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUsTest, ZeroHot) {
+ for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_us(~(1U << i)), i);
+}
>From 74d0a90c433ac9e7d1641907a6eb8068e37b7c0b Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 2 Feb 2024 08:30:00 -0800
Subject: [PATCH 7/8] format
---
libc/include/llvm-libc-macros/stdbit-macros.h | 10 +++++-----
libc/src/stdbit/stdc_trailing_ones_ull.cpp | 3 ++-
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 840246b795635..d58ab7127cbdb 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -92,12 +92,12 @@ inline unsigned stdc_trailing_ones(unsigned long long x) {
unsigned: stdc_trailing_zeros_ui, \
unsigned long: stdc_trailing_zeros_ul, \
unsigned long long: stdc_trailing_zeros_ull)(x)
-#define stdc_trailing_ones(x) \
+#define stdc_trailing_ones(x) \
_Generic((x), \
- unsigned char: stdc_trailing_ones_uc, \
- unsigned short: stdc_trailing_ones_us, \
- unsigned: stdc_trailing_ones_ui, \
- unsigned long: stdc_trailing_ones_ul, \
+ unsigned char: stdc_trailing_ones_uc, \
+ unsigned short: stdc_trailing_ones_us, \
+ unsigned: stdc_trailing_ones_ui, \
+ unsigned long: stdc_trailing_ones_ul, \
unsigned long long: stdc_trailing_ones_ull)(x)
#endif // __cplusplus
diff --git a/libc/src/stdbit/stdc_trailing_ones_ull.cpp b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
index 883b1f2ff5c07..fb5fffe07817d 100644
--- a/libc/src/stdbit/stdc_trailing_ones_ull.cpp
+++ b/libc/src/stdbit/stdc_trailing_ones_ull.cpp
@@ -13,7 +13,8 @@
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ull, (unsigned long long value)) {
+LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_ones_ull,
+ (unsigned long long value)) {
return static_cast<unsigned>(cpp::countr_one(value));
}
>From 3de55d2a1ded88b06f89e7bdf0f5dea75b04f8e4 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Fri, 2 Feb 2024 08:51:19 -0800
Subject: [PATCH 8/8] git add test file
---
.../stdbit/stdc_trailing_ones_ull_test.cpp | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
diff --git a/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp b/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
new file mode 100644
index 0000000000000..38c5100efc06e
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_trailing_ones_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_trailing_ones_ull ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/limits.h"
+#include "src/stdbit/stdc_trailing_ones_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcTrailingOnesUllTest, ALL) {
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ull(ULLONG_MAX),
+ static_cast<unsigned>(ULLONG_WIDTH));
+}
+
+TEST(LlvmLibcStdcTrailingOnesUllTest, ZeroHot) {
+ for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+ EXPECT_EQ(LIBC_NAMESPACE::stdc_trailing_ones_ull(~(1ULL << i)), i);
+}
More information about the libc-commits
mailing list