[libc-commits] [PATCH] D136881: [libc] add features to bitset
Michael Jones via Phabricator via libc-commits
libc-commits at lists.llvm.org
Thu Oct 27 13:31:42 PDT 2022
michaelrj created this revision.
michaelrj added reviewers: sivachandra, lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
michaelrj requested review of this revision.
This patch adds the flip, set_range, and operator== functions to bitset.
These will be used in scanf.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D136881
Files:
libc/src/__support/CPP/bitset.h
Index: libc/src/__support/CPP/bitset.h
===================================================================
--- libc/src/__support/CPP/bitset.h
+++ libc/src/__support/CPP/bitset.h
@@ -25,14 +25,53 @@
return Data[Index / BITS_PER_UNIT] & mask(Index);
}
+ constexpr void flip() {
+ for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)
+ Data[i] = ~Data[i];
+ }
+
+ // This function sets all bits in the range from Start to End (inclusive) to
+ // true. It assumes that Start < End.
+ constexpr void set_range(size_t Start, size_t End) {
+ size_t start_index = Start / BITS_PER_UNIT;
+ size_t end_index = End / BITS_PER_UNIT;
+
+ if (start_index == end_index) {
+ size_t bit_mask = ((size_t(1) << (1 + End - Start)) - 1)
+ << (Start - (start_index * BITS_PER_UNIT));
+ Data[start_index] |= bit_mask;
+ } else {
+ size_t low_bit_mask =
+ ~((size_t(1) << (1 + Start - (start_index * BITS_PER_UNIT))) - 1);
+ Data[start_index] |= low_bit_mask;
+
+ for (size_t i = start_index + 1; i < end_index; ++i)
+ Data[i] = ~size_t(0);
+
+ size_t high_bit_mask =
+ (size_t(1) << (1 + End - (end_index * BITS_PER_UNIT))) - 1;
+ Data[end_index] |= high_bit_mask;
+ }
+ }
+
+ constexpr bool operator==(const bitset<NumberOfBits> &other) {
+ for (size_t i = 0; i < NUMBER_OF_UNITS; ++i) {
+ if (Data[i] != other.Data[i])
+ return false;
+ }
+ return true;
+ }
+
private:
static constexpr size_t BITS_PER_BYTE = 8;
static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(size_t);
+ static constexpr size_t NUMBER_OF_UNITS =
+ (NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
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};
+ size_t Data[NUMBER_OF_UNITS] = {0};
};
} // namespace __llvm_libc::cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136881.471280.patch
Type: text/x-patch
Size: 1966 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221027/6e6d5400/attachment.bin>
More information about the libc-commits
mailing list