[libc] [llvm] [libc] Add more functions in CPP/bit.h (PR #73814)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 29 08:32:13 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Guillaume Chatelet (gchatelet)
<details>
<summary>Changes</summary>
Once this is submitted we can remove `include/__support/bit.h` that duplicates
some of this functionnality.
---
Patch is 24.42 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/73814.diff
7 Files Affected:
- (modified) libc/src/__support/CPP/CMakeLists.txt (+1)
- (modified) libc/src/__support/CPP/bit.h (+223-19)
- (modified) libc/src/__support/CPP/limits.h (+17-1)
- (modified) libc/test/src/__support/CPP/CMakeLists.txt (+11)
- (added) libc/test/src/__support/CPP/bit_test.cpp (+216)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+1)
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/__support/CPP/BUILD.bazel (+9)
``````````diff
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index 10bcebf9b04f61d..5c47b2a815356b9 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -15,6 +15,7 @@ add_header_library(
HDRS
bit.h
DEPENDS
+ .limits
.type_traits
libc.src.__support.macros.attributes
libc.src.__support.macros.config
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index f6f3131c1ccfd81..f744d804398e213 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -1,57 +1,261 @@
-//===-- Freestanding version of bit_cast -----------------------*- C++ -*-===//
+//===-- Implementation of the C++20 bit header -----------------*- 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
//
//===----------------------------------------------------------------------===//
+// This is inspired from LLVM ADT/bit.h header.
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
#define LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
+#include "src/__support/CPP/limits.h" // numeric_limits
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/__support/macros/sanitizer.h"
-namespace LIBC_NAMESPACE::cpp {
+#include <stdint.h>
-#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
-#define LLVM_LIBC_HAS_BUILTIN_BIT_CAST
-#endif
+namespace LIBC_NAMESPACE::cpp {
#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
#endif
-// This function guarantees the bitcast to be optimized away by the compiler for
-// GCC >= 8 and Clang >= 6.
-template <class To, class From>
+// This implementation of bit_cast requires trivially-constructible To, to avoid
+// UB in the implementation.
+template <
+ typename To, typename From,
+ typename = cpp::enable_if_t<sizeof(To) == sizeof(From)>,
+ typename = cpp::enable_if_t<cpp::is_trivially_constructible<To>::value>,
+ typename = cpp::enable_if_t<cpp::is_trivially_copyable<To>::value>,
+ typename = cpp::enable_if_t<cpp::is_trivially_copyable<From>::value>>
LIBC_INLINE constexpr To bit_cast(const From &from) {
- static_assert(sizeof(To) == sizeof(From), "To and From must be of same size");
- static_assert(cpp::is_trivially_copyable<To>::value &&
- cpp::is_trivially_copyable<From>::value,
- "Cannot bit-cast instances of non-trivially copyable classes.");
MSAN_UNPOISON(&from, sizeof(From));
-#if defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
+#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
return __builtin_bit_cast(To, from);
#else
- static_assert(cpp::is_trivially_constructible<To>::value,
- "This implementation additionally requires destination type to "
- "be trivially constructible");
To to;
char *dst = reinterpret_cast<char *>(&to);
const char *src = reinterpret_cast<const char *>(&from);
-#if defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE)
+#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
__builtin_memcpy_inline(dst, src, sizeof(To));
#else
for (unsigned i = 0; i < sizeof(To); ++i)
dst[i] = src[i];
-#endif // defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE)
+#endif // LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
return to;
-#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
+#endif // LIBC_HAS_BUILTIN(__builtin_bit_cast)
+}
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr bool has_single_bit(T value) {
+ return (value != 0) && ((value & (value - 1)) == 0);
+}
+
+// A temporary macro to add template function specialization when compiler
+// builtin is available.
+#define ADD_SPECIALIZATION(NAME, TYPE, BUILTIN) \
+ template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
+ static_assert(cpp::is_unsigned_v<TYPE>); \
+ return value == 0 ? cpp::numeric_limits<TYPE>::digits : BUILTIN(value); \
+ }
+
+/// Count number of 0's from the least significant bit to the most
+/// stopping at the first 1.
+///
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of 0.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countr_zero(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ if (!value)
+ return cpp::numeric_limits<T>::digits;
+ if (value & 0x1)
+ return 0;
+ // Bisection method.
+ unsigned zero_bits = 0;
+ T shift = cpp::numeric_limits<T>::digits >> 1;
+ T mask = cpp::numeric_limits<T>::max() >> shift;
+ while (shift) {
+ if ((value & mask) == 0) {
+ value >>= shift;
+ zero_bits |= shift;
+ }
+ shift >>= 1;
+ mask >>= shift;
+ }
+ return zero_bits;
+}
+#if LIBC_HAS_BUILTIN(__builtin_ctzs)
+ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctz)
+ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctzl)
+ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctzll)
+ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
+#endif
+
+/// Count number of 0's from the most significant bit to the least
+/// stopping at the first 1.
+///
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of 0.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countl_zero(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ if (!value)
+ return cpp::numeric_limits<T>::digits;
+ // Bisection method.
+ unsigned zero_bits = 0;
+ for (T shift = cpp::numeric_limits<T>::digits >> 1; shift; shift >>= 1) {
+ T tmp = value >> shift;
+ if (tmp)
+ value = tmp;
+ else
+ zero_bits |= shift;
+ }
+ return zero_bits;
+}
+#if LIBC_HAS_BUILTIN(__builtin_clzs)
+ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clz)
+ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzl)
+ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzll)
+ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)
+#endif
+
+#undef ADD_SPECIALIZATION
+
+/// Count the number of ones from the most significant bit to the first
+/// zero bit.
+///
+/// Ex. countl_one(0xFF0FFF00) == 8.
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of all ones.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countl_one(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ return cpp::countl_zero<T>(~value);
+}
+
+/// Count the number of ones from the least significant bit to the first
+/// zero bit.
+///
+/// Ex. countr_one(0x00FF00FF) == 8.
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of all ones.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countr_one(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ return cpp::countr_zero<T>(~value);
+}
+
+/// Returns the number of bits needed to represent value if value is nonzero.
+/// Returns 0 otherwise.
+///
+/// Ex. bit_width(5) == 3.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int bit_width(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
+}
+
+/// Returns the largest integral power of two no greater than value if value is
+/// nonzero. Returns 0 otherwise.
+///
+/// Ex. bit_floor(5) == 4.
+template <typename T> [[nodiscard]] LIBC_INLINE constexpr T bit_floor(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ if (!value)
+ return 0;
+ return T(1) << (cpp::bit_width(value) - 1);
+}
+
+/// Returns the smallest integral power of two no smaller than value if value is
+/// nonzero. Returns 1 otherwise.
+///
+/// Ex. bit_ceil(5) == 8.
+///
+/// The return value is undefined if the input is larger than the largest power
+/// of two representable in T.
+template <typename T> [[nodiscard]] LIBC_INLINE constexpr T bit_ceil(T value) {
+ static_assert(cpp::is_unsigned_v<T>);
+ if (value < 2)
+ return 1;
+ return T(1) << cpp::bit_width<T>(value - 1u);
+}
+
+/// Count the number of set bits in a value.
+/// Ex. popcount(0xF000F000) = 8
+/// Returns 0 if the word is zero.
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr int popcount(T value) {
+ if constexpr (sizeof(T) == 8) {
+#if LIBC_HAS_BUILTIN(__builtin_popcountll)
+ return (int)__builtin_popcountll(value);
+#endif
+ uint64_t v = value;
+ v = v - ((v >> 1) & 0x5555555555555555ULL);
+ v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
+ v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
+ return int((uint64_t)(v * 0x0101010101010101ULL) >> 56);
+ } else if constexpr (sizeof(T) <= 4) {
+#if LIBC_HAS_BUILTIN(__builtin_popcount)
+ return (int)__builtin_popcount(value);
+#endif
+ uint32_t v = value;
+ v = v - ((v >> 1) & 0x55555555);
+ v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
+ return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
+ } else {
+ static_assert(cpp::always_false<T>);
+ }
+}
+
+// Forward-declare rotr so that rotl can use it.
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr T rotr(T value, int rotate);
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr T rotl(T value, int rotate) {
+ constexpr unsigned N = cpp::numeric_limits<T>::digits;
+ rotate = rotate % N;
+ if (!rotate)
+ return value;
+ if (rotate < 0)
+ return cpp::rotr(value, -rotate);
+ return (value << rotate) | (value >> (N - rotate));
+}
+
+template <typename T, typename>
+[[nodiscard]] LIBC_INLINE constexpr T rotr(T value, int rotate) {
+ constexpr unsigned N = cpp::numeric_limits<T>::digits;
+ rotate = rotate % N;
+ if (!rotate)
+ return value;
+ if (rotate < 0)
+ return cpp::rotl(value, -rotate);
+ return (value >> rotate) | (value << (N - rotate));
}
+// TODO: Do we need this function at all? How is it different from
+// 'static_cast'?
template <class To, class From>
LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
if constexpr (sizeof(To) == sizeof(From)) {
diff --git a/libc/src/__support/CPP/limits.h b/libc/src/__support/CPP/limits.h
index b1a1203df79674a..b56f2557a737cab 100644
--- a/libc/src/__support/CPP/limits.h
+++ b/libc/src/__support/CPP/limits.h
@@ -11,7 +11,7 @@
#include "src/__support/macros/attributes.h" // LIBC_INLINE
-#include <limits.h>
+#include <limits.h> // CHAR_BIT
namespace LIBC_NAMESPACE {
namespace cpp {
@@ -36,41 +36,53 @@ template <> class numeric_limits<int> {
public:
LIBC_INLINE static constexpr int max() { return INT_MAX; }
LIBC_INLINE static constexpr int min() { return INT_MIN; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT * sizeof(int) - 1;
};
template <> class numeric_limits<unsigned int> {
public:
LIBC_INLINE static constexpr unsigned int max() { return UINT_MAX; }
LIBC_INLINE static constexpr unsigned int min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT * sizeof(unsigned int);
};
template <> class numeric_limits<long> {
public:
LIBC_INLINE static constexpr long max() { return LONG_MAX; }
LIBC_INLINE static constexpr long min() { return LONG_MIN; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT * sizeof(long) - 1;
};
template <> class numeric_limits<unsigned long> {
public:
LIBC_INLINE static constexpr unsigned long max() { return ULONG_MAX; }
LIBC_INLINE static constexpr unsigned long min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits =
+ CHAR_BIT * sizeof(unsigned long);
};
template <> class numeric_limits<long long> {
public:
LIBC_INLINE static constexpr long long max() { return LLONG_MAX; }
LIBC_INLINE static constexpr long long min() { return LLONG_MIN; }
+ LIBC_INLINE_VAR static constexpr int digits =
+ CHAR_BIT * sizeof(long long) - 1;
};
template <> class numeric_limits<unsigned long long> {
public:
LIBC_INLINE static constexpr unsigned long long max() { return ULLONG_MAX; }
LIBC_INLINE static constexpr unsigned long long min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits =
+ CHAR_BIT * sizeof(unsigned long long);
};
template <> class numeric_limits<short> {
public:
LIBC_INLINE static constexpr short max() { return SHRT_MAX; }
LIBC_INLINE static constexpr short min() { return SHRT_MIN; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT * sizeof(short) - 1;
};
template <> class numeric_limits<unsigned short> {
public:
LIBC_INLINE static constexpr unsigned short max() { return USHRT_MAX; }
LIBC_INLINE static constexpr unsigned short min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits =
+ CHAR_BIT * sizeof(unsigned short);
};
template <> class numeric_limits<char> {
public:
@@ -81,11 +93,13 @@ template <> class numeric_limits<signed char> {
public:
LIBC_INLINE static constexpr signed char max() { return SCHAR_MAX; }
LIBC_INLINE static constexpr signed char min() { return SCHAR_MIN; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT - 1;
};
template <> class numeric_limits<unsigned char> {
public:
LIBC_INLINE static constexpr unsigned char max() { return UCHAR_MAX; }
LIBC_INLINE static constexpr unsigned char min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits = CHAR_BIT;
};
#ifdef __SIZEOF_INT128__
// On platform where UInt128 resolves to __uint128_t, this specialization
@@ -94,6 +108,8 @@ template <> class numeric_limits<__uint128_t> {
public:
LIBC_INLINE static constexpr __uint128_t max() { return ~__uint128_t(0); }
LIBC_INLINE static constexpr __uint128_t min() { return 0; }
+ LIBC_INLINE_VAR static constexpr int digits =
+ CHAR_BIT * sizeof(__uint128_t) - 1;
};
#endif
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index 5772a83a65cad13..6927579289bc2f3 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -1,5 +1,16 @@
add_custom_target(libc-cpp-utils-tests)
+add_libc_test(
+ bit_test
+ SUITE
+ libc-cpp-utils-tests
+ SRCS
+ bit_test.cpp
+ DEPENDS
+ libc.src.__support.CPP.bit
+ libc.src.__support.uint
+)
+
add_libc_test(
bitset_test
SUITE
diff --git a/libc/test/src/__support/CPP/bit_test.cpp b/libc/test/src/__support/CPP/bit_test.cpp
new file mode 100644
index 000000000000000..caec621c5a6f64a
--- /dev/null
+++ b/libc/test/src/__support/CPP/bit_test.cpp
@@ -0,0 +1,216 @@
+//===-- Unittests for Bit -------------------------------------------------===//
+//
+// 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/bit.h"
+#include "src/__support/UInt.h"
+#include "test/UnitTest/Test.h"
+
+#include <stdint.h>
+
+namespace LIBC_NAMESPACE::cpp {
+
+using UnsignedTypes =
+ testing::TypeList<unsigned char, unsigned short, unsigned int,
+ unsigned long, unsigned long long>;
+
+TYPED_TEST(LlvmLibcBitTest, HasSingleBit, UnsignedTypes) {
+ EXPECT_FALSE(has_single_bit<T>(T(0)));
+ EXPECT_FALSE(has_single_bit<T>(~T(0)));
+ for (T value = 1; value; value <<= 1)
+ EXPECT_TRUE(has_single_bit<T>(value));
+}
+
+TYPED_TEST(LlvmLibcBitTest, CountLZero, UnsignedTypes) {
+ EXPECT_EQ(countl_zero<T>(T(0)), cpp::numeric_limits<T>::digits);
+ int expected = 0;
+ for (T value = ~T(0); value; value >>= 1, ++expected)
+ EXPECT_EQ(countl_zero<T>(value), expected);
+}
+
+TYPED_TEST(LlvmLibcBitTest, CountRZero, UnsignedTypes) {
+ EXPECT_EQ(countr_zero<T>(T(0)), cpp::numeric_limits<T>::digits);
+ int expected = 0;
+ for (T value = ~T(0); value; value <<= 1, ++expected)
+ EXPECT_EQ(countr_zero<T>(value), expected);
+}
+
+TYPED_TEST(LlvmLibcBitTest, CountLOne, UnsignedTypes) {
+ EXPECT_EQ(countl_one<T>(T(0)), 0);
+ int expected = cpp::numeric_limits<T>::digits;
+ for (T value = ~T(0); value; value <<= 1, --expected)
+ EXPECT_EQ(countl_one<T>(value), expected);
+}
+
+TYPED_TEST(LlvmLibcBitTest, CountROne, UnsignedTypes) {
+ EXPECT_EQ(countr_one<T>(T(0)), 0);
+ int expected = cpp::numeric_limits<T>::digits;
+ for (T value = ~T(0); value; value >>= 1, --expected)
+ EXPECT_EQ(countr_one<T>(value), expected);
+}
+
+TYPED_TEST(LlvmLibcBitTest, BitWidth, UnsignedTypes) {
+ EXPECT_EQ(bit_width<T>(T(0)), 0);
+ int one_index = 0;
+ for (T value = 1; value; value <<= 1, ++one_index)
+ EXPECT_EQ(bit_width<T>(value), one_index + 1);
+}
+
+TEST(LlvmLibcBitTest, BitCeil) {
+ EXPECT_EQ(uint8_t(1), bit_ceil(uint8_t(0)));
+ EXPECT_EQ(uint16_t(1), bit_ceil(uint16_t(0)));
+ EXPECT_EQ(uint32_t(1), bit_ceil(uint32_t(0)));
+ EXPECT_EQ(uint64_t(1), bit_ceil(uint64_t(0)));
+
+ EXPECT_EQ(uint8_t(1), bit_ceil(uint8_t(1)));
+ EXPECT_EQ(uint16_t(1), bit_ceil(uint16_t(1)));
+ EXPECT_EQ(uint32_t(1), bit_ceil(uint32_t(1)));
+ EXPECT_EQ(uint64_t(1), bit_ceil(uint64_t(1)));
+
+ EXPECT_EQ(uint8_t(2), bit_ceil(uint8_t(2)));
+ EXPECT_EQ(uint16_t(2), bit_ceil(uint16_t(2)));
+ EXPECT_EQ(uint32_t(2), bit_ceil(uint32_t(2)));
+ EXPECT_EQ(uint64_t(2), bit_ceil(uint64_t(2)));
+
+ EXPECT_EQ(uint8_t(4), bit_ceil(uint8_t(3)));
+ EXPECT_EQ(uint16_t(4), bit_ceil(uint16_t(3)));
+ EXPECT_EQ(uint32_t(4), bit_ceil(uint32_t(3)));
+ EXPECT_EQ(uint64_t(4), bit_ceil(uint64_t(3)));
+
+ EXPECT_EQ(uint8_t(4), bit_ceil(uint8_t(4)));
+ EXPECT_EQ(uint16_t(4), bit_ceil(uint16_t(4)));
+ EXPECT_EQ(uint32_t(4), bit_ceil(uint32_t(4)));
+ EXPECT_EQ(uint64_t(4), bit_ceil(uint64_t(4)));
+
+ // The result is the largest representable value for each type.
+ EXPECT_EQ(uint8_t(0x80), bit_ceil(uint8_t(0x7f)));
+ EXPECT_EQ(uint16_t(0x8000), bit_ceil(uint16_t(0x7fff)));
+ EXPECT_EQ(uint32_t(0x80000000), bit_ceil(uint32_t(0x7fffffff)));
+ EXPECT_EQ(uint64_t(0x8000000000000000),
+ bit_ceil(uint64_t(0x7fffffffffffffff)));
+}
+
+TEST(LlvmLibcBitTest, BitFloor) {
+ EXPECT_EQ(uint8_t(0), bit_floor(uint8_t(0)));
+ EXPECT_EQ(uint16_t(0), bit_floor(uint16_t(0)));
+ EXPECT_EQ(uint32_t(0), bit_floor(uint32_t(0)));
+ EXPECT_EQ(uint64_t(0), bit_floor(uint64_t(0)));
+
+ EXPECT_EQ(uint8_t(1), bit_floor(uint8_t(1)));
+ EXPECT_EQ(uint16_t(1), bit_floor(uint16_t(1)));
+ EXPECT_EQ(uint32_t(1), bit_floor(uint32_t(1)));
+ EXPECT_EQ(uint64_t(1), bit_floor(uint64_t(1)));
+
+ EXPECT_EQ(uint8_t(2), bit_floor(uint8_t(2)));
+ EXPECT_EQ(uint16_t(2), bit_floor(uint16_t(2)));
+ EXPECT_EQ(uint32_t(2), bit_floor(uint32_t(2)));
+ EXPECT_EQ(uint64_t(2), bit_floor(uint64_t(2)));
+
+ EXPECT_EQ(uint8_t(2), bit_floor(uint8_t(3)));
+ EXPECT_EQ(uint16_t(2), bit_floor(uint16_t(3)));
+ EXPECT_EQ(uint32_t(2), bit_floor(uint32_t(3)));
+ EXPECT_EQ(uint64_t(2), bit_floor(uint64_t(3)));
+
+ EXPECT_EQ(uint8_t(4), bit_floor(uint8_t(4)));
+ EXPECT_EQ(uint16_t(4), bit_floor(uint16_t(4)));
+ EXPECT_EQ(uint32_t(4), bit_floor(uint32_t(4)));
+ EXPECT_EQ(uint64_t(4), bit_floor(uint64_t(4)));
+
+ EXPECT_EQ(uint8_t(0x40), bit_floor(uint8_t(0x7f)));
+ EXPECT_EQ(uint16_t(0x4000), bit_floor(uint16_t(0x7fff)));
+ EXPECT_EQ(uint32_t(0x40000000), bit_floor(uint32_t(0x7fffffff)));
+ EXPECT_EQ(uint64_t(0x4000000000000000),
+ bit_floor(uint64_t(0x7fffffffffffffffull)));
+
+ EXPECT_EQ(uint8_t(0x80), bit_floor(uint8_t(0x80)));
+ EXPECT_EQ(uint16_t(0x8000), bit_floor(uint16_t(0x8000)));
+ EXPECT_EQ(uint32_t(0x80000000), bit_floor(uint32_t(0x80000000)));
+ EXPECT_EQ(uint64_t(0x8000000000000000),
+ bit_floor(uint64_t(0x8000000000000000)));
+
+ EXPECT_EQ(uint8_t(0x80), bit_floor(uint8_t(0xff)));
+ EXPECT_EQ(u...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/73814
More information about the llvm-commits
mailing list