[libc-commits] [libc] [libc][stdbit][c23] adds implementation of `stdc_bit_ceil` functions (PR #84657)

Michael Flanders via libc-commits libc-commits at lists.llvm.org
Sat Mar 9 15:46:27 PST 2024


https://github.com/Flandini updated https://github.com/llvm/llvm-project/pull/84657

>From 6d82de97ec6af956cd54f0721fa13279421c244d Mon Sep 17 00:00:00 2001
From: Michael Flanders <mkf727 at cs.washington.edu>
Date: Sat, 9 Mar 2024 23:21:49 +0000
Subject: [PATCH 1/2] [libc][stdbit][c23] adds implementation of
 `stdc_bit_ceil` functions in stdbit

---
 libc/config/linux/x86_64/entrypoints.txt      |  5 +++
 libc/docs/stdbit.rst                          | 12 +++----
 libc/include/llvm-libc-macros/stdbit-macros.h | 22 ++++++++++++
 libc/spec/stdc.td                             | 10 ++++--
 libc/src/__support/CPP/bit.h                  |  2 +-
 libc/src/stdbit/CMakeLists.txt                |  1 +
 libc/src/stdbit/stdc_bit_ceil_uc.cpp          | 20 +++++++++++
 libc/src/stdbit/stdc_bit_ceil_uc.h            | 18 ++++++++++
 libc/src/stdbit/stdc_bit_ceil_ui.cpp          | 20 +++++++++++
 libc/src/stdbit/stdc_bit_ceil_ui.h            | 18 ++++++++++
 libc/src/stdbit/stdc_bit_ceil_ul.cpp          | 20 +++++++++++
 libc/src/stdbit/stdc_bit_ceil_ul.h            | 18 ++++++++++
 libc/src/stdbit/stdc_bit_ceil_ull.cpp         | 21 ++++++++++++
 libc/src/stdbit/stdc_bit_ceil_ull.h           | 18 ++++++++++
 libc/src/stdbit/stdc_bit_ceil_us.cpp          | 20 +++++++++++
 libc/src/stdbit/stdc_bit_ceil_us.h            | 18 ++++++++++
 libc/test/include/stdbit_test.cpp             | 17 ++++++++++
 libc/test/src/stdbit/CMakeLists.txt           |  1 +
 .../test/src/stdbit/stdc_bit_ceil_uc_test.cpp | 34 +++++++++++++++++++
 .../test/src/stdbit/stdc_bit_ceil_ui_test.cpp | 33 ++++++++++++++++++
 .../test/src/stdbit/stdc_bit_ceil_ul_test.cpp | 33 ++++++++++++++++++
 .../src/stdbit/stdc_bit_ceil_ull_test.cpp     | 33 ++++++++++++++++++
 .../test/src/stdbit/stdc_bit_ceil_us_test.cpp | 34 +++++++++++++++++++
 23 files changed, 419 insertions(+), 9 deletions(-)
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_uc.cpp
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_uc.h
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ui.cpp
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ui.h
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ul.cpp
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ul.h
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ull.cpp
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_ull.h
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_us.cpp
 create mode 100644 libc/src/stdbit/stdc_bit_ceil_us.h
 create mode 100644 libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
 create mode 100644 libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0880c372b37390..41b051ec914bfb 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -158,6 +158,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_bit_floor_ui
     libc.src.stdbit.stdc_bit_floor_ul
     libc.src.stdbit.stdc_bit_floor_ull
+    libc.src.stdbit.stdc_bit_ceil_uc
+    libc.src.stdbit.stdc_bit_ceil_us
+    libc.src.stdbit.stdc_bit_ceil_ui
+    libc.src.stdbit.stdc_bit_ceil_ul
+    libc.src.stdbit.stdc_bit_ceil_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index 3ec46cf8d8ffa4..9b4974cf1479b1 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -96,11 +96,11 @@ stdc_bit_floor_us            |check|
 stdc_bit_floor_ui            |check|
 stdc_bit_floor_ul            |check|
 stdc_bit_floor_ull           |check|
-stdc_bit_ceil_uc
-stdc_bit_ceil_us
-stdc_bit_ceil_ui
-stdc_bit_ceil_ul
-stdc_bit_ceil_ull
+stdc_bit_ceil_uc             |check|
+stdc_bit_ceil_us             |check|
+stdc_bit_ceil_ui             |check|
+stdc_bit_ceil_ul             |check|
+stdc_bit_ceil_ull            |check|
 ============================ =========
 
 
@@ -127,7 +127,7 @@ stdc_count_ones            |check|
 stdc_has_single_bit        |check|
 stdc_bit_width             |check|
 stdc_bit_floor             |check|
-stdc_bit_ceil
+stdc_bit_ceil              |check|
 =========================  =========
 
 Standards
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 5b51068f866b71..50ce272860290f 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -194,6 +194,21 @@ inline unsigned long stdc_bit_floor(unsigned long x) {
 inline unsigned long long stdc_bit_floor(unsigned long long x) {
   return stdc_bit_floor_ull(x);
 }
+inline unsigned char stdc_bit_ceil(unsigned char x) {
+  return stdc_bit_ceil_uc(x);
+}
+inline unsigned short stdc_bit_ceil(unsigned short x) {
+  return stdc_bit_ceil_us(x);
+}
+inline unsigned stdc_bit_ceil(unsigned x) {
+  return stdc_bit_ceil_ui(x);
+}
+inline unsigned long stdc_bit_ceil(unsigned long x) {
+  return stdc_bit_ceil_ul(x);
+}
+inline unsigned long long stdc_bit_ceil(unsigned long long x) {
+  return stdc_bit_ceil_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -286,6 +301,13 @@ inline unsigned long long stdc_bit_floor(unsigned long long x) {
       unsigned: stdc_bit_floor_ui,                                             \
       unsigned long: stdc_bit_floor_ul,                                        \
       unsigned long long: stdc_bit_floor_ull)(x)
+#define stdc_bit_ceil(x)
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_ceil_ui,                                         \
+      unsigned short: stdc_bit_ceil_us,                                        \
+      unsigned: stdc_bit_ceil_ui,                                              \
+      unsigned long: stdc_bit_ceil_ul,                                         \
+      unsigned long long: stdc_bit_ceil_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index cc845a93a3330b..b15857dfa8acf8 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -815,7 +815,8 @@ def StdC : StandardSpec<"stdc"> {
         Macro<"stdc_count_ones">,
         Macro<"stdc_has_single_bit">,
         Macro<"std_bit_width">,
-        Macro<"std_bit_floor">
+        Macro<"std_bit_floor">,
+        Macro<"std_bit_ceil">
       ], // Macros
       [], // Types
       [], // Enumerations
@@ -879,7 +880,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_bit_floor_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_bit_floor_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_bit_floor_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_bit_floor_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_bit_ceil_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_bit_ceil_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_bit_ceil_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_bit_ceil_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_bit_ceil_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 9c74a346949f0c..4464703e4b064e 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -193,7 +193,7 @@ template <typename T>
 bit_ceil(T value) {
   if (value < 2)
     return 1;
-  return T(1) << cpp::bit_width<T>(value - 1u);
+  return static_cast<T>(T(1) << cpp::bit_width<T>(value - 1u));
 }
 
 // Rotate algorithms make use of "Safe, Efficient, and Portable Rotate in C/C++"
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 7ab4fee4454a15..2aef2029f2df0b 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -12,6 +12,7 @@ set(prefixes
   has_single_bit
   bit_width
   bit_floor
+  bit_ceil
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_bit_ceil_uc.cpp b/libc/src/stdbit/stdc_bit_ceil_uc.cpp
new file mode 100644
index 00000000000000..675ae4a0edb021
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_ceil_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_bit_ceil_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned char, stdc_bit_ceil_uc, (unsigned char value)) {
+  return cpp::bit_ceil(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_ceil_uc.h b/libc/src/stdbit/stdc_bit_ceil_uc.h
new file mode 100644
index 00000000000000..204261e4108121
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_ceil_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_BIT_CEIL_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned char stdc_bit_ceil_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UC_H
diff --git a/libc/src/stdbit/stdc_bit_ceil_ui.cpp b/libc/src/stdbit/stdc_bit_ceil_ui.cpp
new file mode 100644
index 00000000000000..a8ac9726179bc7
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_ceil_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_bit_ceil_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_ceil_ui, (unsigned value)) {
+  return cpp::bit_ceil(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_ceil_ui.h b/libc/src/stdbit/stdc_bit_ceil_ui.h
new file mode 100644
index 00000000000000..db66c336e36624
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_ceil_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_BIT_CEIL_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_ceil_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UI_H
diff --git a/libc/src/stdbit/stdc_bit_ceil_ul.cpp b/libc/src/stdbit/stdc_bit_ceil_ul.cpp
new file mode 100644
index 00000000000000..18a9c38b5b4cf6
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_ceil_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_bit_ceil_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long, stdc_bit_ceil_ul, (unsigned long value)) {
+  return cpp::bit_ceil(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_ceil_ul.h b/libc/src/stdbit/stdc_bit_ceil_ul.h
new file mode 100644
index 00000000000000..f8393a42fcbfc1
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_ceil_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_BIT_CEIL_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long stdc_bit_ceil_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_UL_H
diff --git a/libc/src/stdbit/stdc_bit_ceil_ull.cpp b/libc/src/stdbit/stdc_bit_ceil_ull.cpp
new file mode 100644
index 00000000000000..8d97ba05c72ff1
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_bit_ceil_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_bit_ceil_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long long, stdc_bit_ceil_ull,
+		   (unsigned long long value)) {
+  return cpp::bit_ceil(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_ceil_ull.h b/libc/src/stdbit/stdc_bit_ceil_ull.h
new file mode 100644
index 00000000000000..e65f537efb172f
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_ceil_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_BIT_CEIL_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long long stdc_bit_ceil_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_ULL_H
diff --git a/libc/src/stdbit/stdc_bit_ceil_us.cpp b/libc/src/stdbit/stdc_bit_ceil_us.cpp
new file mode 100644
index 00000000000000..f86a216bb840e9
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_ceil_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_bit_ceil_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned short, stdc_bit_ceil_us, (unsigned short value)) {
+  return cpp::bit_ceil(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_ceil_us.h b/libc/src/stdbit/stdc_bit_ceil_us.h
new file mode 100644
index 00000000000000..16a14e51b74334
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_ceil_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_ceil_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_BIT_CEIL_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned short stdc_bit_ceil_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_CEIL_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 20820d52fbdede..6c12665c4454d0 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -98,6 +98,13 @@ unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
 unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
   return 0x5EU;
 }
+unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
+unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
+unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
+unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
+unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
+  return 0x6EU;
+}
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -207,3 +214,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
   EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
   EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
+  EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned char>(0U)),
+            static_cast<unsigned char>(0x6AU));
+  EXPECT_EQ(stdc_bit_ceil(static_cast<unsigned short>(0U)),
+            static_cast<unsigned short>(0x6BU));
+  EXPECT_EQ(stdc_bit_ceil(0U), 0x6CU);
+  EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
+  EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index 3aed56c0e92380..c3f8059d2d9b2a 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -14,6 +14,7 @@ set(prefixes
   has_single_bit
   bit_width
   bit_floor
+  bit_ceil
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp
new file mode 100644
index 00000000000000..a6c62e0a5dd44e
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp
@@ -0,0 +1,34 @@
+//===-- Unittests for stdc_bit_ceil_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_bit_ceil_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitceilUcTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(0U),
+            static_cast<unsigned char>(1));
+}
+
+TEST(LlvmLibcStdcBitceilUcTest, Ones) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc(1U << i),
+              static_cast<unsigned char>(1U << i));
+}
+
+TEST(LlvmLibcStdcBitceilUcTest, OneLessThanPowsTwo) {
+  for (unsigned i = 2U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) - 1),
+              static_cast<unsigned char>(1U << i));
+}
+
+TEST(LlvmLibcStdcBitceilUcTest, OneMoreThanPowsTwo) {
+  for (unsigned i = 2U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_uc((1U << i) + 1),
+              static_cast<unsigned char>(1U << (i + 1)));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
new file mode 100644
index 00000000000000..82015a342aa9a0
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
@@ -0,0 +1,33 @@
+//===-- Unittests for stdc_bit_ceil_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_bit_ceil_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitceilUiTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(0U), 1U);
+}
+
+TEST(LlvmLibcStdcBitceilUiTest, Ones) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(1U << i),
+              1U << i);
+}
+
+TEST(LlvmLibcStdcBitceilUiTest, OneLessThanPowsTwo) {
+  for (unsigned i = 2U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) - 1),
+              1U << i);
+}
+
+TEST(LlvmLibcStdcBitceilUiTest, OneMoreThanPowsTwo) {
+  for (unsigned i = 2U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) + 1),
+              1U << (i + 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
new file mode 100644
index 00000000000000..aa70596946e245
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
@@ -0,0 +1,33 @@
+//===-- Unittests for stdc_bit_ceil_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_bit_ceil_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitceilUlTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(0UL), 1UL);
+}
+
+TEST(LlvmLibcStdcBitceilUlTest, Ones) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(1UL << i),
+              1UL << i);
+}
+
+TEST(LlvmLibcStdcBitceilUlTest, OneLessThanPowsTwo) {
+  for (unsigned i = 2U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) - 1),
+              1UL << i);
+}
+
+TEST(LlvmLibcStdcBitceilUlTest, OneMoreThanPowsTwo) {
+  for (unsigned i = 2U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) + 1),
+              1UL << (i + 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
new file mode 100644
index 00000000000000..b8141dc3dc0b62
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
@@ -0,0 +1,33 @@
+//===-- Unittests for stdc_bit_ceil_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_bit_ceil_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitceilUllTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(0ULL), 1ULL);
+}
+
+TEST(LlvmLibcStdcBitceilUllTest, Ones) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(1ULL << i),
+              1ULL << i);
+}
+
+TEST(LlvmLibcStdcBitceilUllTest, OneLessThanPowsTwo) {
+  for (unsigned i = 2U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) - 1),
+              1ULL << i);
+}
+
+TEST(LlvmLibcStdcBitceilUllTest, OneMoreThanPowsTwo) {
+  for (unsigned i = 2U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) + 1),
+              1ULL << (i + 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp
new file mode 100644
index 00000000000000..127a9c739385fa
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp
@@ -0,0 +1,34 @@
+//===-- Unittests for stdc_bit_ceil_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_bit_ceil_us.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitceilUsTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us(0U),
+            static_cast<unsigned short>(1));
+}
+
+TEST(LlvmLibcStdcBitceilUsTest, Ones) {
+  for (unsigned i = 0U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us(1U << i),
+              static_cast<unsigned short>(1U << i));
+}
+
+TEST(LlvmLibcStdcBitceilUsTest, OneLessThanPowsTwo) {
+  for (unsigned i = 2U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us((1U << i) - 1),
+              static_cast<unsigned short>(1U << i));
+}
+
+TEST(LlvmLibcStdcBitceilUsTest, OneMoreThanPowsTwo) {
+  for (unsigned i = 2U; i != USHRT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_us((1U << i) + 1),
+              static_cast<unsigned short>(1U << (i + 1)));
+}

>From d2aa4229ad5e72c120a619cbf39bcd0c5551b910 Mon Sep 17 00:00:00 2001
From: Michael Flanders <mkf727 at cs.washington.edu>
Date: Sat, 9 Mar 2024 23:39:32 +0000
Subject: [PATCH 2/2] forgot to run clang-format; ignore auto generic select
 formatting based on prev bit_floor formatting

---
 libc/include/llvm-libc-macros/stdbit-macros.h   | 16 +++++++---------
 libc/src/stdbit/stdc_bit_ceil_ull.cpp           |  2 +-
 libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp  |  9 +++------
 libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp  |  9 +++------
 libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp |  6 ++----
 5 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 50ce272860290f..6403e6af986224 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -200,9 +200,7 @@ inline unsigned char stdc_bit_ceil(unsigned char x) {
 inline unsigned short stdc_bit_ceil(unsigned short x) {
   return stdc_bit_ceil_us(x);
 }
-inline unsigned stdc_bit_ceil(unsigned x) {
-  return stdc_bit_ceil_ui(x);
-}
+inline unsigned stdc_bit_ceil(unsigned x) { return stdc_bit_ceil_ui(x); }
 inline unsigned long stdc_bit_ceil(unsigned long x) {
   return stdc_bit_ceil_ul(x);
 }
@@ -301,12 +299,12 @@ inline unsigned long long stdc_bit_ceil(unsigned long long x) {
       unsigned: stdc_bit_floor_ui,                                             \
       unsigned long: stdc_bit_floor_ul,                                        \
       unsigned long long: stdc_bit_floor_ull)(x)
-#define stdc_bit_ceil(x)
-  _Generic((x),                                                                \
-      unsigned char: stdc_bit_ceil_ui,                                         \
-      unsigned short: stdc_bit_ceil_us,                                        \
-      unsigned: stdc_bit_ceil_ui,                                              \
-      unsigned long: stdc_bit_ceil_ul,                                         \
+#define stdc_bit_ceil(x)                                                      \
+  _Generic((x),                                                               \
+      unsigned char: stdc_bit_ceil_uc,                                        \
+      unsigned short: stdc_bit_ceil_us,                                       \
+      unsigned: stdc_bit_ceil_ui,                                             \
+      unsigned long: stdc_bit_ceil_ul,                                        \
       unsigned long long: stdc_bit_ceil_ull)(x)
 #endif // __cplusplus
 
diff --git a/libc/src/stdbit/stdc_bit_ceil_ull.cpp b/libc/src/stdbit/stdc_bit_ceil_ull.cpp
index 8d97ba05c72ff1..0989f36ab7685a 100644
--- a/libc/src/stdbit/stdc_bit_ceil_ull.cpp
+++ b/libc/src/stdbit/stdc_bit_ceil_ull.cpp
@@ -14,7 +14,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(unsigned long long, stdc_bit_ceil_ull,
-		   (unsigned long long value)) {
+                   (unsigned long long value)) {
   return cpp::bit_ceil(value);
 }
 
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
index 82015a342aa9a0..b71d3c07bf4daa 100644
--- a/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp
@@ -16,18 +16,15 @@ TEST(LlvmLibcStdcBitceilUiTest, Zero) {
 
 TEST(LlvmLibcStdcBitceilUiTest, Ones) {
   for (unsigned i = 0U; i != UINT_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(1U << i),
-              1U << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui(1U << i), 1U << i);
 }
 
 TEST(LlvmLibcStdcBitceilUiTest, OneLessThanPowsTwo) {
   for (unsigned i = 2U; i != UINT_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) - 1),
-              1U << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) - 1), 1U << i);
 }
 
 TEST(LlvmLibcStdcBitceilUiTest, OneMoreThanPowsTwo) {
   for (unsigned i = 2U; i != UINT_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) + 1),
-              1U << (i + 1));
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ui((1U << i) + 1), 1U << (i + 1));
 }
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
index aa70596946e245..2acf138334f435 100644
--- a/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp
@@ -16,18 +16,15 @@ TEST(LlvmLibcStdcBitceilUlTest, Zero) {
 
 TEST(LlvmLibcStdcBitceilUlTest, Ones) {
   for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(1UL << i),
-              1UL << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul(1UL << i), 1UL << i);
 }
 
 TEST(LlvmLibcStdcBitceilUlTest, OneLessThanPowsTwo) {
   for (unsigned i = 2U; i != ULONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) - 1),
-              1UL << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) - 1), 1UL << i);
 }
 
 TEST(LlvmLibcStdcBitceilUlTest, OneMoreThanPowsTwo) {
   for (unsigned i = 2U; i != ULONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) + 1),
-              1UL << (i + 1));
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ul((1UL << i) + 1), 1UL << (i + 1));
 }
diff --git a/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
index b8141dc3dc0b62..4d91a78092372d 100644
--- a/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
+++ b/libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp
@@ -16,14 +16,12 @@ TEST(LlvmLibcStdcBitceilUllTest, Zero) {
 
 TEST(LlvmLibcStdcBitceilUllTest, Ones) {
   for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(1ULL << i),
-              1ULL << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull(1ULL << i), 1ULL << i);
 }
 
 TEST(LlvmLibcStdcBitceilUllTest, OneLessThanPowsTwo) {
   for (unsigned i = 2U; i != ULLONG_WIDTH; ++i)
-    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) - 1),
-              1ULL << i);
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_ceil_ull((1ULL << i) - 1), 1ULL << i);
 }
 
 TEST(LlvmLibcStdcBitceilUllTest, OneMoreThanPowsTwo) {



More information about the libc-commits mailing list