Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/bitset.h
213799 views
//===-- A self contained equivalent of std::bitset --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H1011#include "src/__support/macros/attributes.h"12#include "src/__support/macros/config.h"13#include <stddef.h> // For size_t.1415namespace LIBC_NAMESPACE_DECL {16namespace cpp {1718template <size_t NumberOfBits> struct bitset {19static_assert(NumberOfBits != 0,20"Cannot create a LIBC_NAMESPACE::cpp::bitset of size 0.");2122LIBC_INLINE constexpr void set(size_t Index) {23Data[Index / BITS_PER_UNIT] |= mask(Index);24}2526LIBC_INLINE constexpr void reset() {27for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)28Data[i] = 0;29}3031LIBC_INLINE constexpr bool test(size_t Index) const {32return Data[Index / BITS_PER_UNIT] & mask(Index);33}3435LIBC_INLINE constexpr void flip() {36for (size_t i = 0; i < NUMBER_OF_UNITS; ++i)37Data[i] = ~Data[i];38}3940// This function sets all bits in the range from Start to End (inclusive) to41// true. It assumes that Start <= End.42LIBC_INLINE constexpr void set_range(size_t Start, size_t End) {43size_t start_index = Start / BITS_PER_UNIT;44size_t end_index = End / BITS_PER_UNIT;4546if (start_index == end_index) {47// The reason the left shift is split into two parts (instead of just left48// shifting by End - Start + 1) is because when a number is shifted left49// by 64 then it wraps around to doing nothing, but shifting by 63 and the50// shifting by 1 correctly shifts away all of the bits.51size_t bit_mask = (((size_t(1) << (End - Start)) << 1) - 1)52<< (Start - (start_index * BITS_PER_UNIT));53Data[start_index] |= bit_mask;54} else {55size_t low_bit_mask =56~((size_t(1) << (Start - (start_index * BITS_PER_UNIT))) - 1);57Data[start_index] |= low_bit_mask;5859for (size_t i = start_index + 1; i < end_index; ++i)60Data[i] = ~size_t(0);6162// Same as above, by splitting the shift the behavior is more consistent.63size_t high_bit_mask =64((size_t(1) << (End - (end_index * BITS_PER_UNIT))) << 1) - 1;65Data[end_index] |= high_bit_mask;66}67}6869LIBC_INLINE constexpr bool70operator==(const bitset<NumberOfBits> &other) const {71for (size_t i = 0; i < NUMBER_OF_UNITS; ++i) {72if (Data[i] != other.Data[i])73return false;74}75return true;76}7778private:79static constexpr size_t BITS_PER_BYTE = 8;80static constexpr size_t BITS_PER_UNIT = BITS_PER_BYTE * sizeof(size_t);81static constexpr size_t NUMBER_OF_UNITS =82(NumberOfBits + BITS_PER_UNIT - 1) / BITS_PER_UNIT;8384LIBC_INLINE static constexpr size_t mask(size_t Index) {85return size_t{1} << (Index % BITS_PER_UNIT);86}87size_t Data[NUMBER_OF_UNITS] = {0};88};8990} // namespace cpp91} // namespace LIBC_NAMESPACE_DECL9293#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BITSET_H949596