[libc] [llvm] [libc] Add more functions in CPP/bit.h (PR #73814)

Clement Courbet via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 09:18:28 PST 2023


================
@@ -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.
----------------
legrosbuffle wrote:

"representable power of 2". The largest value would be 0xff...ff

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


More information about the llvm-commits mailing list