| Index: src/utils.h
|
| diff --git a/src/utils.h b/src/utils.h
|
| index b2c2ff109847ee5bedec0d1848df61e01fcdc277..90d7f3cdc6eec3fb18e015e1c90b1f06e43bf256 100644
|
| --- a/src/utils.h
|
| +++ b/src/utils.h
|
| @@ -242,42 +242,52 @@ inline int StrLength(const char* string) {
|
| // ----------------------------------------------------------------------------
|
| // BitField is a help template for encoding and decode bitfield with
|
| // unsigned content.
|
| -template<class T, int shift, int size>
|
| -class BitField {
|
| +
|
| +template<class T, int shift, int size, class U>
|
| +class BitFieldBase {
|
| public:
|
| - // A uint32_t mask of bit field. To use all bits of a uint32 in a
|
| - // bitfield without compiler warnings we have to compute 2^32 without
|
| - // using a shift count of 32.
|
| - static const uint32_t kMask = ((1U << shift) << size) - (1U << shift);
|
| - static const uint32_t kShift = shift;
|
| - static const uint32_t kSize = size;
|
| + // A type U mask of bit field. To use all bits of a type U of x bits
|
| + // in a bitfield without compiler warnings we have to compute 2^x
|
| + // without using a shift count of x in the computation.
|
| + static const U kOne = static_cast<U>(1U);
|
| + static const U kMask = ((kOne << shift) << size) - (kOne << shift);
|
| + static const U kShift = shift;
|
| + static const U kSize = size;
|
|
|
| // Value for the field with all bits set.
|
| static const T kMax = static_cast<T>((1U << size) - 1);
|
|
|
| // Tells whether the provided value fits into the bit field.
|
| static bool is_valid(T value) {
|
| - return (static_cast<uint32_t>(value) & ~static_cast<uint32_t>(kMax)) == 0;
|
| + return (static_cast<U>(value) & ~static_cast<U>(kMax)) == 0;
|
| }
|
|
|
| - // Returns a uint32_t with the bit field value encoded.
|
| - static uint32_t encode(T value) {
|
| + // Returns a type U with the bit field value encoded.
|
| + static U encode(T value) {
|
| ASSERT(is_valid(value));
|
| - return static_cast<uint32_t>(value) << shift;
|
| + return static_cast<U>(value) << shift;
|
| }
|
|
|
| - // Returns a uint32_t with the bit field value updated.
|
| - static uint32_t update(uint32_t previous, T value) {
|
| + // Returns a type U with the bit field value updated.
|
| + static U update(U previous, T value) {
|
| return (previous & ~kMask) | encode(value);
|
| }
|
|
|
| // Extracts the bit field from the value.
|
| - static T decode(uint32_t value) {
|
| + static T decode(U value) {
|
| return static_cast<T>((value & kMask) >> shift);
|
| }
|
| };
|
|
|
|
|
| +template<class T, int shift, int size>
|
| +class BitField : public BitFieldBase<T, shift, size, uint32_t> { };
|
| +
|
| +
|
| +template<class T, int shift, int size>
|
| +class BitField64 : public BitFieldBase<T, shift, size, uint64_t> { };
|
| +
|
| +
|
| // ----------------------------------------------------------------------------
|
| // Hash function.
|
|
|
|
|