[libc-commits] [libc] [libc][stdbit] implement stdc_bit_width (C23) (PR #83892)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 4 10:52:04 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

<details>
<summary>Changes</summary>



---

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


22 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 (+16) 
- (modified) libc/spec/stdc.td (+8-2) 
- (modified) libc/src/stdbit/CMakeLists.txt (+1) 
- (added) libc/src/stdbit/stdc_bit_width_uc.cpp (+20) 
- (added) libc/src/stdbit/stdc_bit_width_uc.h (+18) 
- (added) libc/src/stdbit/stdc_bit_width_ui.cpp (+20) 
- (added) libc/src/stdbit/stdc_bit_width_ui.h (+18) 
- (added) libc/src/stdbit/stdc_bit_width_ul.cpp (+20) 
- (added) libc/src/stdbit/stdc_bit_width_ul.h (+18) 
- (added) libc/src/stdbit/stdc_bit_width_ull.cpp (+20) 
- (added) libc/src/stdbit/stdc_bit_width_ull.h (+18) 
- (added) libc/src/stdbit/stdc_bit_width_us.cpp (+20) 
- (added) libc/src/stdbit/stdc_bit_width_us.h (+18) 
- (modified) libc/test/include/stdbit_test.cpp (+13) 
- (modified) libc/test/src/stdbit/CMakeLists.txt (+1) 
- (added) libc/test/src/stdbit/stdc_bit_width_uc_test.cpp (+21) 
- (added) libc/test/src/stdbit/stdc_bit_width_ui_test.cpp (+20) 
- (added) libc/test/src/stdbit/stdc_bit_width_ul_test.cpp (+21) 
- (added) libc/test/src/stdbit/stdc_bit_width_ull_test.cpp (+21) 
- (added) libc/test/src/stdbit/stdc_bit_width_us_test.cpp (+21) 


``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bc10512d942fa7..a6c3041773dfcc 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -147,6 +147,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_has_single_bit_ui
     libc.src.stdbit.stdc_has_single_bit_ul
     libc.src.stdbit.stdc_has_single_bit_ull
+    libc.src.stdbit.stdc_bit_width_uc
+    libc.src.stdbit.stdc_bit_width_us
+    libc.src.stdbit.stdc_bit_width_ui
+    libc.src.stdbit.stdc_bit_width_ul
+    libc.src.stdbit.stdc_bit_width_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index b579e9dbbc2f51..ccd1393ef5d926 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -86,11 +86,11 @@ stdc_has_single_bit_us       |check|
 stdc_has_single_bit_ui       |check|
 stdc_has_single_bit_ul       |check|
 stdc_has_single_bit_ull      |check|
-stdc_bit_width_uc
-stdc_bit_width_us
-stdc_bit_width_ui
-stdc_bit_width_ul
-stdc_bit_width_ull
+stdc_bit_width_uc            |check|
+stdc_bit_width_us            |check|
+stdc_bit_width_ui            |check|
+stdc_bit_width_ul            |check|
+stdc_bit_width_ull           |check|
 stdc_bit_floor_uc
 stdc_bit_floor_us
 stdc_bit_floor_ui
@@ -125,7 +125,7 @@ stdc_first_trailing_one    |check|
 stdc_count_zeros           |check|
 stdc_count_ones            |check|
 stdc_has_single_bit        |check|
-stdc_bit_width
+stdc_bit_width             |check|
 stdc_bit_floor
 stdc_bit_ceil
 =========================  =========
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index e3a36d10ed92ab..104418ca4856ba 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -172,6 +172,15 @@ inline bool stdc_has_single_bit(unsigned long x) {
 inline bool stdc_has_single_bit(unsigned long long x) {
   return stdc_has_single_bit_ull(x);
 }
+inline unsigned stdc_bit_width(unsigned char x) { return stdc_bit_width_uc(x); }
+inline unsigned stdc_bit_width(unsigned short x) {
+  return stdc_bit_width_us(x);
+}
+inline unsigned stdc_bit_width(unsigned x) { return stdc_bit_width_ui(x); }
+inline unsigned stdc_bit_width(unsigned long x) { return stdc_bit_width_ul(x); }
+inline unsigned stdc_bit_width(unsigned long long x) {
+  return stdc_bit_width_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -250,6 +259,13 @@ inline bool stdc_has_single_bit(unsigned long long x) {
       unsigned: stdc_has_single_bit_ui,                                        \
       unsigned long: stdc_has_single_bit_ul,                                   \
       unsigned long long: stdc_has_single_bit_ull)(x)
+#define stdc_bit_width(x)                                                      \
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_width_ui,                                        \
+      unsigned short: stdc_bit_width_us,                                       \
+      unsigned: stdc_bit_width_ui,                                             \
+      unsigned long: stdc_bit_width_ul,                                        \
+      unsigned long long: stdc_bit_width_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 94ac62966f3ba5..f4c91185695387 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -800,7 +800,8 @@ def StdC : StandardSpec<"stdc"> {
         Macro<"stdc_first_trailing_one">,
         Macro<"stdc_count_zeros">,
         Macro<"stdc_count_ones">,
-        Macro<"stdc_has_single_bit">
+        Macro<"stdc_has_single_bit">,
+        Macro<"std_bit_width">
       ], // Macros
       [], // Types
       [], // Enumerations
@@ -854,7 +855,12 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"stdc_has_single_bit_us", RetValSpec<BoolType>, [ArgSpec<UnsignedShortType>]>,
           FunctionSpec<"stdc_has_single_bit_ui", RetValSpec<BoolType>, [ArgSpec<UnsignedIntType>]>,
           FunctionSpec<"stdc_has_single_bit_ul", RetValSpec<BoolType>, [ArgSpec<UnsignedLongType>]>,
-          FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>
+          FunctionSpec<"stdc_has_single_bit_ull", RetValSpec<BoolType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_bit_width_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
+          FunctionSpec<"stdc_bit_width_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
+          FunctionSpec<"stdc_bit_width_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
+          FunctionSpec<"stdc_bit_width_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
+          FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
       ] // Functions
   >;
 
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 8bc7dd7852bbca..f077baeee6d275 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -10,6 +10,7 @@ set(prefixes
   count_zeros
   count_ones
   has_single_bit
+  bit_width
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_bit_width_uc.cpp b/libc/src/stdbit/stdc_bit_width_uc.cpp
new file mode 100644
index 00000000000000..2c361c1bbb1cfe
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_width_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_width_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_uc, (unsigned char value)) {
+  return static_cast<unsigned>(cpp::bit_width(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_width_uc.h b/libc/src/stdbit/stdc_bit_width_uc.h
new file mode 100644
index 00000000000000..70c038aaf1dfe5
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_uc.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_width_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_WIDTH_UC_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UC_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_width_uc(unsigned char value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UC_H
diff --git a/libc/src/stdbit/stdc_bit_width_ui.cpp b/libc/src/stdbit/stdc_bit_width_ui.cpp
new file mode 100644
index 00000000000000..b94452b09bd506
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_width_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_width_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ui, (unsigned value)) {
+  return static_cast<unsigned>(cpp::bit_width(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_width_ui.h b/libc/src/stdbit/stdc_bit_width_ui.h
new file mode 100644
index 00000000000000..9e8de3d6ef46e4
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_width_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_WIDTH_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_width_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UI_H
diff --git a/libc/src/stdbit/stdc_bit_width_ul.cpp b/libc/src/stdbit/stdc_bit_width_ul.cpp
new file mode 100644
index 00000000000000..80044314e4b21e
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_width_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_width_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ul, (unsigned long value)) {
+  return static_cast<unsigned>(cpp::bit_width(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_width_ul.h b/libc/src/stdbit/stdc_bit_width_ul.h
new file mode 100644
index 00000000000000..447a2918e2f2bb
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_width_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_WIDTH_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_width_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_UL_H
diff --git a/libc/src/stdbit/stdc_bit_width_ull.cpp b/libc/src/stdbit/stdc_bit_width_ull.cpp
new file mode 100644
index 00000000000000..006fa20b2de5e7
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ull.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_width_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_width_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_ull, (unsigned long long value)) {
+  return static_cast<unsigned>(cpp::bit_width(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_width_ull.h b/libc/src/stdbit/stdc_bit_width_ull.h
new file mode 100644
index 00000000000000..bc51897f448fc3
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_width_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_WIDTH_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_width_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_ULL_H
diff --git a/libc/src/stdbit/stdc_bit_width_us.cpp b/libc/src/stdbit/stdc_bit_width_us.cpp
new file mode 100644
index 00000000000000..3d9f72bf5d0602
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_width_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_width_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_width_us, (unsigned short value)) {
+  return static_cast<unsigned>(cpp::bit_width(value));
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_width_us.h b/libc/src/stdbit/stdc_bit_width_us.h
new file mode 100644
index 00000000000000..02cd37426eb4c1
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_width_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_width_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_WIDTH_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_width_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_WIDTH_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index acb79ca0f3ff11..b8919d463779f2 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -86,6 +86,11 @@ bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
 bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
 bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
 bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
+unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
+unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
+unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
+unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
+unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4FU; }
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -177,3 +182,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroHasSingleBit) {
   EXPECT_EQ(stdc_has_single_bit(1UL), false);
   EXPECT_EQ(stdc_has_single_bit(1ULL), false);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
+  EXPECT_EQ(stdc_bit_width(static_cast<unsigned char>(1U)), 0x4AU);
+  EXPECT_EQ(stdc_bit_width(static_cast<unsigned short>(1U)), 0x4BU);
+  EXPECT_EQ(stdc_bit_width(1U), 0x4CU);
+  EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
+  EXPECT_EQ(stdc_bit_width(1ULL), 0x4FU);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index a886ee4a35325d..f7e17d73229935 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -12,6 +12,7 @@ set(prefixes
   count_zeros
   count_ones
   has_single_bit
+  bit_width
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp b/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp
new file mode 100644
index 00000000000000..63c6503542b1f7
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_width_uc_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_width_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_width_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitWidthUcTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(0U), 0U);
+}
+
+TEST(LlvmLibcStdcBitWidthUcTest, Ones) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_uc(UCHAR_MAX >> i),
+              UCHAR_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp
new file mode 100644
index 00000000000000..43acdde5dd2077
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_width_ui_test.cpp
@@ -0,0 +1,20 @@
+//===-- Unittests for stdc_bit_width_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_width_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitWidthUiTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(0U), 0U);
+}
+
+TEST(LlvmLibcStdcBitWidthUiTest, Ones) {
+  for (unsigned i = 0U; i != UINT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ui(UINT_MAX >> i), UINT_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp
new file mode 100644
index 00000000000000..0a286942655f61
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_width_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_width_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_width_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitWidthUlTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(0U), 0U);
+}
+
+TEST(LlvmLibcStdcBitWidthUlTest, Ones) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ul(ULONG_MAX >> i),
+              ULONG_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp
new file mode 100644
index 00000000000000..31475f6e6541be
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_width_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_width_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_width_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitWidthUllTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ull(0U), 0U);
+}
+
+TEST(LlvmLibcStdcBitWidthUllTest, Ones) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_width_ull(ULLONG_MAX >> i),
+              ULLONG_WIDTH - i);
+}
diff --git a/libc/test/src/stdbit/stdc_bit_width_us_test.cpp b/libc/test/src/stdbit/st...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/83892


More information about the libc-commits mailing list