// Copyright 2019 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_BASE_BIT_FIELD_H_ #define V8_BASE_BIT_FIELD_H_ #include <stdint.h> #include <algorithm> #include "src/base/macros.h" namespace v8 { namespace base { // ---------------------------------------------------------------------------- // BitField is a help template for encoding and decode bitfield with // unsigned content. // Instantiate them via 'using', which is cheaper than deriving a new class: // using MyBitField = base::BitField<MyEnum, 4, 2>; // The BitField class is final to enforce this style over derivation. template <class T, int shift, int size, class U = uint32_t> class BitField final { … }; // ---------------------------------------------------------------------------- // BitFieldUnion can be used to combine two linear BitFields. // So far only the static mask is computed. Encoding and decoding tbd. // Can be used for example as a quick combined check: // `if (BitFieldUnion<BFA, BFB>::kMask & bitfield) ...` template <typename A, typename B> class BitFieldUnion final { … }; BitField8; BitField16; BitField64; // Helper macros for defining a contiguous sequence of bit fields. Example: // (backslashes at the ends of respective lines of this multi-line macro // definition are omitted here to please the compiler) // // #define MAP_BIT_FIELD1(V, _) // V(IsAbcBit, bool, 1, _) // V(IsBcdBit, bool, 1, _) // V(CdeBits, int, 5, _) // V(DefBits, MutableMode, 1, _) // // DEFINE_BIT_FIELDS(MAP_BIT_FIELD1) // or // DEFINE_BIT_FIELDS_64(MAP_BIT_FIELD1) // #define DEFINE_BIT_FIELD_RANGE_TYPE(Name, Type, Size, _) … #define DEFINE_BIT_RANGES(LIST_MACRO) … #define DEFINE_BIT_FIELD_TYPE(Name, Type, Size, RangesName) … #define DEFINE_BIT_FIELD_64_TYPE(Name, Type, Size, RangesName) … #define DEFINE_BIT_FIELDS(LIST_MACRO) … #define DEFINE_BIT_FIELDS_64(LIST_MACRO) … // ---------------------------------------------------------------------------- // BitSetComputer is a help template for encoding and decoding information for // a variable number of items in an array. // // To encode boolean data in a smi array you would use: // using BoolComputer = BitSetComputer<bool, 1, kSmiValueSize, uint32_t>; // template <class T, int kBitsPerItem, int kBitsPerWord, class U> class BitSetComputer { … }; } // namespace base } // namespace v8 #endif // V8_BASE_BIT_FIELD_H_