Skip to content

[libc][stdbit] implement stdc_bit_floor (C23) #84233

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

Merged
merged 2 commits into from
Mar 7, 2024

Conversation

nickdesaulniers
Copy link
Member

No description provided.

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

llvmbot commented Mar 6, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Patch is 20.93 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/84233.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 (+20)
  • (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_floor_uc.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_floor_ui.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_floor_ui.h (+18)
  • (added) libc/src/stdbit/stdc_bit_floor_ul.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_floor_ul.h (+18)
  • (added) libc/src/stdbit/stdc_bit_floor_ull.cpp (+21)
  • (added) libc/src/stdbit/stdc_bit_floor_ull.h (+18)
  • (added) libc/src/stdbit/stdc_bit_floor_us.cpp (+20)
  • (added) libc/src/stdbit/stdc_bit_floor_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_floor_uc_test.cpp (+22)
  • (added) libc/test/src/stdbit/stdc_bit_floor_ui_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_bit_floor_ul_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_bit_floor_ull_test.cpp (+21)
  • (added) libc/test/src/stdbit/stdc_bit_floor_us_test.cpp (+22)
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a6c3041773dfcc..3ca3b45fe274c6 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -152,6 +152,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdbit.stdc_bit_width_ui
     libc.src.stdbit.stdc_bit_width_ul
     libc.src.stdbit.stdc_bit_width_ull
+    libc.src.stdbit.stdc_bit_floor_uc
+    libc.src.stdbit.stdc_bit_floor_us
+    libc.src.stdbit.stdc_bit_floor_ui
+    libc.src.stdbit.stdc_bit_floor_ul
+    libc.src.stdbit.stdc_bit_floor_ull
 
     # stdlib.h entrypoints
     libc.src.stdlib.abs
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index ccd1393ef5d926..3ec46cf8d8ffa4 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -91,11 +91,11 @@ 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
-stdc_bit_floor_ul
-stdc_bit_floor_ull
+stdc_bit_floor_uc            |check|
+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
@@ -126,7 +126,7 @@ stdc_count_zeros           |check|
 stdc_count_ones            |check|
 stdc_has_single_bit        |check|
 stdc_bit_width             |check|
-stdc_bit_floor
+stdc_bit_floor             |check|
 stdc_bit_ceil
 =========================  =========
 
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 104418ca4856ba..5b51068f866b71 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -181,6 +181,19 @@ 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);
 }
+inline unsigned char stdc_bit_floor(unsigned char x) {
+  return stdc_bit_floor_uc(x);
+}
+inline unsigned short stdc_bit_floor(unsigned short x) {
+  return stdc_bit_floor_us(x);
+}
+inline unsigned stdc_bit_floor(unsigned x) { return stdc_bit_floor_ui(x); }
+inline unsigned long stdc_bit_floor(unsigned long x) {
+  return stdc_bit_floor_ul(x);
+}
+inline unsigned long long stdc_bit_floor(unsigned long long x) {
+  return stdc_bit_floor_ull(x);
+}
 #else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
@@ -266,6 +279,13 @@ inline unsigned stdc_bit_width(unsigned long long x) {
       unsigned: stdc_bit_width_ui,                                             \
       unsigned long: stdc_bit_width_ul,                                        \
       unsigned long long: stdc_bit_width_ull)(x)
+#define stdc_bit_floor(x)                                                      \
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_floor_ui,                                        \
+      unsigned short: stdc_bit_floor_us,                                       \
+      unsigned: stdc_bit_floor_ui,                                             \
+      unsigned long: stdc_bit_floor_ul,                                        \
+      unsigned long long: stdc_bit_floor_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index cfebc60a0a9a8e..a3a856a4ec8507 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -801,7 +801,8 @@ def StdC : StandardSpec<"stdc"> {
         Macro<"stdc_count_zeros">,
         Macro<"stdc_count_ones">,
         Macro<"stdc_has_single_bit">,
-        Macro<"std_bit_width">
+        Macro<"std_bit_width">,
+        Macro<"std_bit_floor">
       ], // Macros
       [], // Types
       [], // Enumerations
@@ -860,7 +861,12 @@ def StdC : StandardSpec<"stdc"> {
           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>]>
+          FunctionSpec<"stdc_bit_width_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
+          FunctionSpec<"stdc_bit_floor_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
+          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>]>
       ] // Functions
   >;
 
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 7d11e7d5c497e0..504974ce2c60af 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -169,7 +169,7 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
 [[nodiscard]] LIBC_INLINE constexpr T bit_floor(T value) {
   if (!value)
     return 0;
-  return T(1) << (cpp::bit_width(value) - 1);
+  return static_cast<T>(T(1) << (cpp::bit_width(value) - 1));
 }
 
 /// Returns the smallest integral power of two no smaller than value if value is
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index f077baeee6d275..7ab4fee4454a15 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -11,6 +11,7 @@ set(prefixes
   count_ones
   has_single_bit
   bit_width
+  bit_floor
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/src/stdbit/stdc_bit_floor_uc.cpp b/libc/src/stdbit/stdc_bit_floor_uc.cpp
new file mode 100644
index 00000000000000..6cb04c9eb43e62
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_uc.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_floor_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_floor_uc.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned char, stdc_bit_floor_uc, (unsigned char value)) {
+  return cpp::bit_floor(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_floor_ui.cpp b/libc/src/stdbit/stdc_bit_floor_ui.cpp
new file mode 100644
index 00000000000000..149b63f190cf37
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ui.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_floor_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_floor_ui.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned, stdc_bit_floor_ui, (unsigned value)) {
+  return cpp::bit_floor(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_floor_ui.h b/libc/src/stdbit/stdc_bit_floor_ui.h
new file mode 100644
index 00000000000000..fcc606386f86d3
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ui.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_floor_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_FLOOR_UI_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned stdc_bit_floor_ui(unsigned value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UI_H
diff --git a/libc/src/stdbit/stdc_bit_floor_ul.cpp b/libc/src/stdbit/stdc_bit_floor_ul.cpp
new file mode 100644
index 00000000000000..a29a044545684e
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ul.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_floor_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_floor_ul.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long, stdc_bit_floor_ul, (unsigned long value)) {
+  return cpp::bit_floor(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_floor_ul.h b/libc/src/stdbit/stdc_bit_floor_ul.h
new file mode 100644
index 00000000000000..08327aa60c9069
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ul.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_floor_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_FLOOR_UL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long stdc_bit_floor_ul(unsigned long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_UL_H
diff --git a/libc/src/stdbit/stdc_bit_floor_ull.cpp b/libc/src/stdbit/stdc_bit_floor_ull.cpp
new file mode 100644
index 00000000000000..d1084b63573227
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ull.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of stdc_bit_floor_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_floor_ull.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned long long, stdc_bit_floor_ull,
+                   (unsigned long long value)) {
+  return cpp::bit_floor(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_floor_ull.h b/libc/src/stdbit/stdc_bit_floor_ull.h
new file mode 100644
index 00000000000000..8f360b23855ad6
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_ull.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_floor_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_FLOOR_ULL_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned long long stdc_bit_floor_ull(unsigned long long value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_ULL_H
diff --git a/libc/src/stdbit/stdc_bit_floor_us.cpp b/libc/src/stdbit/stdc_bit_floor_us.cpp
new file mode 100644
index 00000000000000..d1357a980e3a8a
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_us.cpp
@@ -0,0 +1,20 @@
+//===-- Implementation of stdc_bit_floor_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_floor_us.h"
+
+#include "src/__support/CPP/bit.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(unsigned short, stdc_bit_floor_us, (unsigned short value)) {
+  return cpp::bit_floor(value);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdbit/stdc_bit_floor_us.h b/libc/src/stdbit/stdc_bit_floor_us.h
new file mode 100644
index 00000000000000..fcd0b9e3c549a1
--- /dev/null
+++ b/libc/src/stdbit/stdc_bit_floor_us.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for stdc_bit_floor_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_FLOOR_US_H
+#define LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_US_H
+
+namespace LIBC_NAMESPACE {
+
+unsigned short stdc_bit_floor_us(unsigned short value);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDBIT_STDC_BIT_FLOOR_US_H
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index dfb7c97e3d9ee0..20820d52fbdede 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -91,6 +91,13 @@ 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 0x4EU; }
+unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
+unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
+unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
+unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
+unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
+  return 0x5EU;
+}
 }
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
@@ -190,3 +197,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitWidth) {
   EXPECT_EQ(stdc_bit_width(1UL), 0x4DU);
   EXPECT_EQ(stdc_bit_width(1ULL), 0x4EU);
 }
+
+TEST(LlvmLibcStdbitTest, TypeGenericMacroBitFloor) {
+  EXPECT_EQ(stdc_bit_floor(static_cast<unsigned char>(0U)),
+            static_cast<unsigned char>(0x5AU));
+  EXPECT_EQ(stdc_bit_floor(static_cast<unsigned short>(0U)),
+            static_cast<unsigned short>(0x5BU));
+  EXPECT_EQ(stdc_bit_floor(0U), 0x5CU);
+  EXPECT_EQ(stdc_bit_floor(0UL), 0x5DUL);
+  EXPECT_EQ(stdc_bit_floor(0ULL), 0x5EULL);
+}
diff --git a/libc/test/src/stdbit/CMakeLists.txt b/libc/test/src/stdbit/CMakeLists.txt
index f7e17d73229935..3aed56c0e92380 100644
--- a/libc/test/src/stdbit/CMakeLists.txt
+++ b/libc/test/src/stdbit/CMakeLists.txt
@@ -13,6 +13,7 @@ set(prefixes
   count_ones
   has_single_bit
   bit_width
+  bit_floor
 )
 set(suffixes c s i l ll)
 foreach(prefix IN LISTS prefixes)
diff --git a/libc/test/src/stdbit/stdc_bit_floor_uc_test.cpp b/libc/test/src/stdbit/stdc_bit_floor_uc_test.cpp
new file mode 100644
index 00000000000000..254abd043d6e0c
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_floor_uc_test.cpp
@@ -0,0 +1,22 @@
+//===-- Unittests for stdc_bit_floor_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_floor_uc.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitfloorUcTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(0U),
+            static_cast<unsigned char>(0));
+}
+
+TEST(LlvmLibcStdcBitfloorUcTest, Ones) {
+  for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_uc(UCHAR_MAX >> i),
+              static_cast<unsigned char>(1 << (UCHAR_WIDTH - i - 1)));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_floor_ui_test.cpp b/libc/test/src/stdbit/stdc_bit_floor_ui_test.cpp
new file mode 100644
index 00000000000000..53790402a9bda9
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_floor_ui_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_floor_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_floor_ui.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitfloorUiTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(0U), 0U);
+}
+
+TEST(LlvmLibcStdcBitfloorUiTest, Ones) {
+  for (unsigned i = 0U; i != INT_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ui(UINT_MAX >> i),
+              1U << (UINT_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_floor_ul_test.cpp b/libc/test/src/stdbit/stdc_bit_floor_ul_test.cpp
new file mode 100644
index 00000000000000..1c574437e02b79
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_floor_ul_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_floor_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_floor_ul.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitfloorUlTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ul(0UL), 0UL);
+}
+
+TEST(LlvmLibcStdcBitfloorUlTest, Ones) {
+  for (unsigned i = 0U; i != ULONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ul(ULONG_MAX >> i),
+              1UL << (ULONG_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_floor_ull_test.cpp b/libc/test/src/stdbit/stdc_bit_floor_ull_test.cpp
new file mode 100644
index 00000000000000..4717d427a40a72
--- /dev/null
+++ b/libc/test/src/stdbit/stdc_bit_floor_ull_test.cpp
@@ -0,0 +1,21 @@
+//===-- Unittests for stdc_bit_floor_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_floor_ull.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStdcBitfloorUllTest, Zero) {
+  EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ull(0ULL), 0ULL);
+}
+
+TEST(LlvmLibcStdcBitfloorUllTest, Ones) {
+  for (unsigned i = 0U; i != ULLONG_WIDTH; ++i)
+    EXPECT_EQ(LIBC_NAMESPACE::stdc_bit_floor_ull(ULLONG_MAX >> i),
+              1ULL << (ULLONG_WIDTH - i - 1));
+}
diff --git a/libc/test/src/stdbit/stdc_bit_floor_us_test.cpp b/libc/test/src/stdbit/stdc_bit_floor_us_test.cpp
new file mode 100644
index 00000000000000..4df87fb079ba76
--- /de...
[truncated]

@@ -169,7 +169,7 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
[[nodiscard]] LIBC_INLINE constexpr T bit_floor(T value) {
if (!value)
return 0;
return T(1) << (cpp::bit_width(value) - 1);
return static_cast<T>(T(1) << (cpp::bit_width(value) - 1));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewers: without this change, I observe an integer truncation warning (promoted to error) for types smaller than int. They undergo implicit promotion from the binary operator <<.

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.

4 participants