diff --git a/libc/src/__support/CPP/bitset.h b/libc/src/__support/CPP/bitset.h --- a/libc/src/__support/CPP/bitset.h +++ b/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 &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