[libc-commits] [libc] e2d7975 - [libc][NFC] Use STL case for bitset and simplify implementation
Guillaume Chatelet via libc-commits
libc-commits at lists.llvm.org
Fri Aug 19 14:39:31 PDT 2022
Author: Guillaume Chatelet
Date: 2022-08-19T21:39:14Z
New Revision: e2d79758436b690d3cc5c3bf79cba2c2b280ce80
URL: https://github.com/llvm/llvm-project/commit/e2d79758436b690d3cc5c3bf79cba2c2b280ce80
DIFF: https://github.com/llvm/llvm-project/commit/e2d79758436b690d3cc5c3bf79cba2c2b280ce80.diff
LOG: [libc][NFC] Use STL case for bitset and simplify implementation
Added:
libc/src/__support/CPP/bitset.h
Modified:
libc/src/__support/CPP/CMakeLists.txt
libc/src/string/string_utils.h
libc/src/string/strspn.cpp
libc/test/src/__support/CPP/bitset_test.cpp
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
libc/src/__support/CPP/Bitset.h
################################################################################
diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index 0364bc0df246c..1a03710c6f70d 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -35,7 +35,7 @@ add_header_library(
add_header_library(
bitset
HDRS
- Bitset.h
+ bitset.h
)
add_header_library(
diff --git a/libc/src/__support/CPP/Bitset.h b/libc/src/__support/CPP/bitset.h
similarity index 61%
rename from libc/src/__support/CPP/Bitset.h
rename to libc/src/__support/CPP/bitset.h
index db0569c74c389..0791e26d2c906 100644
--- a/libc/src/__support/CPP/Bitset.h
+++ b/libc/src/__support/CPP/bitset.h
@@ -10,31 +10,31 @@
#define LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
#include <stddef.h> // For size_t.
-#include <stdint.h> // For uintptr_t.
-namespace __llvm_libc {
-namespace cpp {
+namespace __llvm_libc::cpp {
-template <size_t NumberOfBits> struct Bitset {
+template <size_t NumberOfBits> struct bitset {
static_assert(NumberOfBits != 0,
- "Cannot create a __llvm_libc::cpp::Bitset of size 0.");
+ "Cannot create a __llvm_libc::cpp::bitset of size 0.");
constexpr void set(size_t Index) {
- Data[Index / BITS_PER_UNIT] |= (uintptr_t{1} << (Index % BITS_PER_UNIT));
+ Data[Index / BITS_PER_UNIT] |= mask(Index);
}
constexpr bool test(size_t Index) const {
- return Data[Index / BITS_PER_UNIT] &
- (uintptr_t{1} << (Index % BITS_PER_UNIT));
+ return Data[Index / BITS_PER_UNIT] & mask(Index);
}
private:
static constexpr size_t BITS_PER_BYTE = 8;
- static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(uintptr_t);
- uintptr_t Data[(NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT] = {0};
+ static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(size_t);
+
+ static inline size_t mask(size_t Index) {
+ return size_t{1} << (Index % BITS_PER_UNIT);
+ }
+ size_t Data[(NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT] = {0};
};
-} // namespace cpp
-} // namespace __llvm_libc
+} // namespace __llvm_libc::cpp
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_BITSET_H
diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index 3d854d3c881fc..708475e4e97f5 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -9,7 +9,7 @@
#ifndef LIBC_SRC_STRING_STRING_UTILS_H
#define LIBC_SRC_STRING_STRING_UTILS_H
-#include "src/__support/CPP/Bitset.h"
+#include "src/__support/CPP/bitset.h"
#include "src/__support/common.h"
#include "src/string/memory_utils/memcpy_implementations.h"
#include "src/string/memory_utils/memset_implementations.h"
@@ -40,7 +40,7 @@ static inline void *find_first_character(const unsigned char *src,
// 'segment'. If no characters are found, returns the length of 'src'.
static inline size_t complementary_span(const char *src, const char *segment) {
const char *initial = src;
- cpp::Bitset<256> bitset;
+ cpp::bitset<256> bitset;
for (; *segment; ++segment)
bitset.set(*segment);
@@ -65,7 +65,7 @@ static inline char *string_token(char *__restrict src,
if (unlikely(src == nullptr && ((src = *saveptr) == nullptr)))
return nullptr;
- cpp::Bitset<256> delimiter_set;
+ cpp::bitset<256> delimiter_set;
for (; *delimiter_string != '\0'; ++delimiter_string)
delimiter_set.set(*delimiter_string);
diff --git a/libc/src/string/strspn.cpp b/libc/src/string/strspn.cpp
index 879850500d34a..60f16df8acb1e 100644
--- a/libc/src/string/strspn.cpp
+++ b/libc/src/string/strspn.cpp
@@ -8,7 +8,7 @@
#include "src/string/strspn.h"
-#include "src/__support/CPP/Bitset.h"
+#include "src/__support/CPP/bitset.h"
#include "src/__support/common.h"
#include <stddef.h>
@@ -16,7 +16,7 @@ namespace __llvm_libc {
LLVM_LIBC_FUNCTION(size_t, strspn, (const char *src, const char *segment)) {
const char *initial = src;
- cpp::Bitset<256> bitset;
+ cpp::bitset<256> bitset;
for (; *segment; ++segment)
bitset.set(*segment);
diff --git a/libc/test/src/__support/CPP/bitset_test.cpp b/libc/test/src/__support/CPP/bitset_test.cpp
index 4bcddad5715cc..6db824d8ce445 100644
--- a/libc/test/src/__support/CPP/bitset_test.cpp
+++ b/libc/test/src/__support/CPP/bitset_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for Bitset ----------------------------------------------===//
+//===-- Unittests for bitset ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,18 +6,18 @@
//
//===----------------------------------------------------------------------===//
-#include "src/__support/CPP/Bitset.h"
+#include "src/__support/CPP/bitset.h"
#include "utils/UnitTest/Test.h"
TEST(LlvmLibcBitsetTest, SetBitForSizeEqualToOne) {
- __llvm_libc::cpp::Bitset<1> bitset;
+ __llvm_libc::cpp::bitset<1> bitset;
EXPECT_FALSE(bitset.test(0));
bitset.set(0);
EXPECT_TRUE(bitset.test(0));
}
TEST(LlvmLibcBitsetTest, SetsBitsForSizeEqualToTwo) {
- __llvm_libc::cpp::Bitset<2> bitset;
+ __llvm_libc::cpp::bitset<2> bitset;
bitset.set(0);
EXPECT_TRUE(bitset.test(0));
bitset.set(1);
@@ -25,7 +25,7 @@ TEST(LlvmLibcBitsetTest, SetsBitsForSizeEqualToTwo) {
}
TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanEight) {
- __llvm_libc::cpp::Bitset<7> bitset;
+ __llvm_libc::cpp::bitset<7> bitset;
for (size_t i = 0; i < 7; ++i)
bitset.set(i);
// Verify all bits are now set.
@@ -34,7 +34,7 @@ TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanEight) {
}
TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanSixteen) {
- __llvm_libc::cpp::Bitset<15> bitset;
+ __llvm_libc::cpp::bitset<15> bitset;
for (size_t i = 0; i < 15; ++i)
bitset.set(i);
// Verify all bits are now set.
@@ -43,7 +43,7 @@ TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanSixteen) {
}
TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanThirtyTwo) {
- __llvm_libc::cpp::Bitset<31> bitset;
+ __llvm_libc::cpp::bitset<31> bitset;
for (size_t i = 0; i < 31; ++i)
bitset.set(i);
// Verify all bits are now set.
@@ -52,12 +52,12 @@ TEST(LlvmLibcBitsetTest, SetsAllBitsForSizeLessThanThirtyTwo) {
}
TEST(LlvmLibcBitsetTest, DefaultHasNoSetBits) {
- __llvm_libc::cpp::Bitset<64> bitset;
+ __llvm_libc::cpp::bitset<64> bitset;
for (size_t i = 0; i < 64; ++i) {
EXPECT_FALSE(bitset.test(i));
}
// Same for odd number.
- __llvm_libc::cpp::Bitset<65> odd_bitset;
+ __llvm_libc::cpp::bitset<65> odd_bitset;
for (size_t i = 0; i < 65; ++i) {
EXPECT_FALSE(odd_bitset.test(i));
}
@@ -65,8 +65,8 @@ TEST(LlvmLibcBitsetTest, DefaultHasNoSetBits) {
TEST(LlvmLibcBitsetTest, SettingBitXDoesNotSetBitY) {
for (size_t i = 0; i < 256; ++i) {
- // Initialize within the loop to start with a fresh Bitset.
- __llvm_libc::cpp::Bitset<256> bitset;
+ // Initialize within the loop to start with a fresh bitset.
+ __llvm_libc::cpp::bitset<256> bitset;
bitset.set(i);
for (size_t neighbor = 0; neighbor < 256; ++neighbor) {
@@ -79,7 +79,7 @@ TEST(LlvmLibcBitsetTest, SettingBitXDoesNotSetBitY) {
// Same for odd number.
for (size_t i = 0; i < 255; ++i) {
- __llvm_libc::cpp::Bitset<255> bitset;
+ __llvm_libc::cpp::bitset<255> bitset;
bitset.set(i);
for (size_t neighbor = 0; neighbor < 255; ++neighbor) {
@@ -92,7 +92,7 @@ TEST(LlvmLibcBitsetTest, SettingBitXDoesNotSetBitY) {
}
TEST(LlvmLibcBitsetTest, SettingBitXDoesNotResetBitY) {
- __llvm_libc::cpp::Bitset<128> bitset;
+ __llvm_libc::cpp::bitset<128> bitset;
for (size_t i = 0; i < 128; ++i)
bitset.set(i);
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 26a0db4ca5290..3bafb3c21595e 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -59,7 +59,7 @@ cc_library(
cc_library(
name = "__support_cpp_bitset",
- hdrs = ["src/__support/CPP/Bitset.h"],
+ hdrs = ["src/__support/CPP/bitset.h"],
deps = [":libc_root"],
)
More information about the libc-commits
mailing list