/** *ABitStringCharisalight-weightwrappertoread/writeasinglecharacter *intoaBitString,whilerestrictingthebitlength. * *Thisisonlyintendedforreading/writingintotemporaries,astherepresentationis *inefficientformemory(itusesawordforthecharacterandanotherwordforthebitlength). * *SeealsoBitStringbelow.
*/ struct BitStringChar { using StorageType = uint32_t;
static_assert(std::is_unsigned_v<StorageType>, "BitStringChar::StorageType must be unsigned");
// BitStringChars are always zero-initialized by default. Equivalent to BitStringChar(0,0).
BitStringChar() : data_(0u), bitlength_(0u) { }
// Create a new BitStringChar whose data bits can be at most bitlength.
BitStringChar(StorageType data, size_t bitlength)
: data_(data), bitlength_(bitlength) { // All bits higher than bitlength must be set to 0.
DCHECK_EQ(0u, data & ~MaskLeastSignificant(bitlength_))
<< "BitStringChar data out of range, data: " << data << ", bitlength: " << bitlength;
}
// What is the bitlength constraint for this character? // (Data could use less bits, but this is the maximum bit capacity at that BitString position).
size_t GetBitLength() const { return bitlength_;
}
// Is there any capacity in this BitStringChar to store any data? bool IsEmpty() const { return bitlength_ == 0;
}
// Compare equality against another BitStringChar. Note: bitlength is ignored. booloperator==(const BitStringChar& other) const { return data_ == other.data_;
}
// Compare non-equality against another BitStringChar. Note: bitlength is ignored. booloperator!=(const BitStringChar& other) const { return !(*this == other);
}
// Add a BitStringChar with an integer. The resulting BitStringChar's data must still fit within // this BitStringChar's bit length.
BitStringChar operator+(StorageType storage) const {
DCHECK_LE(storage, MaximumValue().data_ - data_) << "Addition would overflow " << *this; return BitStringChar(data_ + storage, bitlength_);
}
// Get the maximum representible value with the same bitlength. // (Useful to figure out the maximum value for this BitString position.)
BitStringChar MaximumValue() const {
StorageType maximimum_data = MaxInt<StorageType>(bitlength_); return BitStringChar(maximimum_data, bitlength_);
}
private:
StorageType data_; // Unused bits (outside of bitlength) are 0.
size_t bitlength_; // Logically const. Physically non-const so operator= still works.
};
// Print e.g. "BitStringChar<10>(123)" where 10=bitlength, 123=data. inline std::ostream& operator<<(std::ostream& os, const BitStringChar& bc) {
os << "BitStringChar<" << bc.GetBitLength() << ">("
<< static_cast<BitStringChar::StorageType>(bc) << ")"; return os;
}
/** *BitString * *MSB(mostsignificantbit)LSB *+------------+-----+------------+------------+------------+ *|||||| *|CharN|...|Char2|Char1|Char0| *|||||| *+------------+-----+------------+------------+------------+ *<-len[N]->...<-len[2]-><-len[1]-><-len[0]-> * *Storesupto"N+1"charactersinasubsetofamachineword.Eachcharacterhasadifferent *bitlength,asdefinedbylen[pos].ThisBitStringcanbenestedinsideofaBitStruct *(seee.g.SubtypeCheckBitsAndStatus). * *Definitions: * *"ABCDE...K":=[A,B,C,D,E,...K]+[0]*(N-idx(K))s.t.N>=K. *// Padded with trailing 0s to fit (N+1) bitstring chars. *MaxBitstringLen:=N+1 *StrLen(Bitstring):=Is.t.(I==0ORChar(I-1)!=0) *ANDforallcharinCharI..CharN:char==0 *// = Maximum length - the # of consecutive trailing zeroes. *Bitstring[N]:=CharN *Bitstring[I..N):=[CharI,CharI+1,...CharN-1] * *(TheseareusedbytheSubtypeCheckInfodefinitionsandinvariants,seesubtype_check_info.h)
*/ struct BitString { using StorageType = BitStringChar::StorageType;
// As this is meant to be used only with "SubtypeCheckInfo", // the bitlengths and the maximum string length is tuned by maximizing the coverage of "Assigned" // bitstrings for instance-of and check-cast targets during Optimizing compilation. static constexpr size_t kBitSizeAtPosition[] = {12, 4, 11}; // len[] from header docs. static constexpr size_t kCapacity = arraysize(kBitSizeAtPosition); // MaxBitstringLen above.
// How many bits are needed to represent BitString[0..position)? static constexpr size_t GetBitLengthTotalAtPosition(size_t position) {
size_t idx = 0;
size_t sum = 0; while (idx < position && idx < kCapacity) {
sum += kBitSizeAtPosition[idx];
++idx;
} // TODO: precompute with CreateArray helper.
return sum;
}
// What is the least-significant-bit for a position? // (e.g. to use with BitField{Insert,Extract,Clear}.) static constexpr size_t GetLsbForPosition(size_t position) {
DCHECK_GE(kCapacity, position); return GetBitLengthTotalAtPosition(position);
}
// How many bits are needed for a BitStringChar at the position? // Returns 0 if the position is out of range. static constexpr size_t MaybeGetBitLengthAtPosition(size_t position) { if (position >= kCapacity) { return0;
} return kBitSizeAtPosition[position];
}
// Read a bitchar at some index within the capacity. // See also "BitString[N]" in the doc header.
BitStringChar operator[](size_t idx) const {
DCHECK_LT(idx, kCapacity);
StorageType data = BitFieldExtract(storage_, GetLsbForPosition(idx), kBitSizeAtPosition[idx]);
// Overwrite a bitchar at a position with a new one. // // The `bitchar` bitlength must be no more than the maximum bitlength for that position. void SetAt(size_t idx, BitStringChar bitchar) {
DCHECK_LT(idx, kCapacity);
DCHECK_LE(bitchar.GetBitLength(), kBitSizeAtPosition[idx]);
// Read the bitchar: Bits > bitlength in bitchar are defined to be 0.
storage_ = BitFieldInsert(storage_, static_cast<StorageType>(bitchar),
GetLsbForPosition(idx),
kBitSizeAtPosition[idx]);
}
// How many characters are there in this bitstring? // Trailing 0s are ignored, but 0s in-between are counted. // See also "StrLen(BitString)" in the doc header.
size_t Length() const {
size_t num_trailing_zeros = 0;
size_t i; for (i = kCapacity - 1u; ; --i) {
BitStringChar bc = (*this)[i]; if (bc != 0u) { break; // Found first trailing non-zero.
}
++num_trailing_zeros; if (i == 0u) { break; // No more bitchars remaining: don't underflow.
}
}
return kCapacity - num_trailing_zeros;
}
// Cast to the underlying integral storage type. explicitoperator StorageType() const { return storage_;
}
// Get the # of bits this would use if it was nested inside of a BitStruct. static constexpr size_t BitStructSizeOf() { return GetBitLengthTotalAtPosition(kCapacity);
}
BitString() = default;
// Efficient O(1) comparison: Equal if both bitstring words are the same. booloperator==(const BitString& other) const { return storage_ == other.storage_;
}
// Efficient O(1) negative comparison: Not-equal if both bitstring words are different. booloperator!=(const BitString& other) const { return !(*this == other);
}
// Does this bitstring contain exactly 0 characters? bool IsEmpty() const { return (*this) == BitString{};
}
// Remove all BitStringChars starting at end. // Returns the BitString[0..end) substring as a copy. // See also "BitString[I..N)" in the doc header.
BitString Truncate(size_t end) {
DCHECK_GE(kCapacity, end);
BitString copy = *this;
// Data is stored with the first character in the least-significant-bit. // Unused bits are zero.
StorageType storage_;
};
static_assert(BitSizeOf<BitString::StorageType>() >=
BitString::GetBitLengthTotalAtPosition(BitString::kCapacity), "Storage type is too small for the # of bits requested");
os << "BitString["; for (size_t i = 0; i < length; ++i) {
BitStringChar bc = bit_string[i]; if (i != 0) {
os << ",";
}
os << static_cast<BitString::StorageType>(bc);
}
os << "]"; return os;
}
} // namespace art
#endif// ART_LIBARTBASE_BASE_BIT_STRING_H_
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.