Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][stdbit][c23] adds implementation of stdc_bit_ceil functions #84657

Merged
merged 4 commits into from
Mar 10, 2024

Conversation

Flandini
Copy link
Contributor

@Flandini Flandini commented Mar 9, 2024

Closes #84652.

Based on #84233.

Copy link

github-actions bot commented Mar 9, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc label Mar 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 9, 2024

@llvm/pr-subscribers-libc

Author: Michael Flanders (Flandini)

Changes

Closes #84652.

Based on #84233.


Patch is 23.32 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/84657.diff

23 Files Affected:

  • (modified) libc/config/linux/x86_64/entrypoints.txt (+5)
  • (modified) libc/docs/stdbit.rst (+6-6)
  • (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+22)
  • (modified) libc/spec/stdc.td (+8-2)
  • (modified) libc/src/__support/CPP/bit.h (+1-1)
  • (modified) libc/src/stdbit/CMakeLists.txt (+1)
  • (added) libc/src/stdbit/stdc_bit_ceil_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_ceil_uc.h (+18)
  • (added) libc/src/stdbit/stdc_bit_ceil_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_ceil_ui.h (+18)
  • (added) libc/src/stdbit/stdc_bit_ceil_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_ceil_ul.h (+18)
  • (added) libc/src/stdbit/stdc_bit_ceil_ull.cpp (+21)
  • (added) libc/src/stdbit/stdc_bit_ceil_ull.h (+18)
  • (added) libc/src/stdbit/stdc_bit_ceil_us.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_ceil_us.h (+18)
  • (modified) libc/test/include/stdbit_test.cpp (+17)
  • (modified) libc/test/src/stdbit/CMakeLists.txt (+1)
  • (added) libc/test/src/stdbit/stdc_bit_ceil_uc_test.cpp (+34)
  • (added) libc/test/src/stdbit/stdc_bit_ceil_ui_test.cpp (+33)
  • (added) libc/test/src/stdbit/stdc_bit_ceil_ul_test.cpp (+33)
  • (added) libc/test/src/stdbit/stdc_bit_ceil_ull_test.cpp (+33)
  • (added) libc/test/src/stdbit/stdc_bit_ceil_us_test.cpp (+34)
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(...
[truncated]

Copy link

github-actions bot commented Mar 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Flandini
Copy link
Contributor Author

Flandini commented Mar 9, 2024

Pushed formatting changes, forgot to do this the first time. I am skipping the recommended auto formatting for the generic selection because it seems very different from the formatting of the other type-generic macros. It just looked weird in my terminal, pushed the changes.

@Flandini
Copy link
Contributor Author

Adding @nickdesaulniers as a reviewer

@lntue lntue merged commit 75b0d38 into llvm:main Mar 10, 2024
5 checks passed
Copy link

@Flandini Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

lntue pushed a commit that referenced this pull request Mar 10, 2024
After #84657 was merged,
[buildbot](https://lab.llvm.org/buildbot/#/builders/250/builds/19808) is
reporting two errors for libc-x86_64-debian-gcc-fullbuild-dbg. This PR
addresses the truncation error for `CPP::bit_ceil<unsigned char>` and
`CPP::bit_ceil<unsigned short>`.

The errors are:

```
FAILED: projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_uc.dir/stdc_bit_ceil_uc.cpp.o
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/stdbit -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-vir!
 tual-dtor
  -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -fpie -ffreestanding -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_uc.dir/stdc_bit_ceil_uc.cpp.o -MF projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_uc.dir/stdc_bit_ceil_uc.cpp.o.d -o projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_uc.dir/stdc_bit_ceil_uc.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_uc.cpp
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_uc.cpp:11:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/CPP/bit.h: In instantiation of ‘constexpr __llvm_libc_19_0_0_git::cpp::enable_if_t<is_unsigned_v<T>, T> __llvm_libc_19_0_0_git::cpp::bit_ceil(T) [with T = unsigned char; enable_if_t<is_unsigned_v<T>, T> = unsigned char]’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_uc.cpp:17:23:   required from here
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/CPP/bit.h:196:57: error: conversion from ‘unsigned int’ to ‘unsigned char’ may change value [-Werror=conversion]
  196 |   return static_cast<T>(T(1) << cpp::bit_width<T>(value - 1u));
      |                                                   ~~~~~~^~~~
cc1plus: all warnings being treated as errors
[138/466] Building CXX object projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_us.dir/stdc_bit_ceil_us.cpp.o
FAILED: projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_us.dir/stdc_bit_ceil_us.cpp.o
/usr/bin/g++ -DLIBC_NAMESPACE=__llvm_libc_19_0_0_git -D_DEBUG -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/src/stdbit -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit -I/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc -isystem /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/build/projects/libc/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-vir!
 tual-dtor
  -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -g -fpie -ffreestanding -fno-builtin -fno-exceptions -fno-lax-vector-conversions -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-rtti -ftrivial-auto-var-init=pattern -Wall -Wextra -Werror -Wconversion -Wno-sign-conversion -Wimplicit-fallthrough -Wwrite-strings -Wextra-semi -DLIBC_COPT_PUBLIC_PACKAGING -std=c++17 -MD -MT projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_us.dir/stdc_bit_ceil_us.cpp.o -MF projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_us.dir/stdc_bit_ceil_us.cpp.o.d -o projects/libc/src/stdbit/CMakeFiles/libc.src.stdbit.stdc_bit_ceil_us.dir/stdc_bit_ceil_us.cpp.o -c /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_us.cpp
In file included from /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_us.cpp:11:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/CPP/bit.h: In instantiation of ‘constexpr __llvm_libc_19_0_0_git::cpp::enable_if_t<is_unsigned_v<T>, T> __llvm_libc_19_0_0_git::cpp::bit_ceil(T) [with T = short unsigned int; enable_if_t<is_unsigned_v<T>, T> = short unsigned int]’:
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/stdbit/stdc_bit_ceil_us.cpp:17:23:   required from here
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/CPP/bit.h:196:57: error: conversion from ‘unsigned int’ to ‘short unsigned int’ may change value [-Werror=conversion]
  196 |   return static_cast<T>(T(1) << cpp::bit_width<T>(value - 1u));
      |                                                   ~~~~~~^~~~
cc1plus: all warnings being treated as errors
```
@Flandini Flandini deleted the bit_ceil branch April 12, 2024 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc][stdbit][c23] implement the stdc_bit_ceil functions
3 participants