11 static const std::array<uint64_t, 2> VARINT16_MAX_VALUES = {
12 (UINT64_C(1) << (6U)) - 1,
13 (UINT64_C(1) << (6U + 8U)) - 1,
16 static const std::array<uint64_t, 4> VARINT32_MAX_VALUES = {
17 (UINT64_C(1) << (6U)) - 1,
18 (UINT64_C(1) << (6U + 7U)) - 1,
19 (UINT64_C(1) << (6U + 7U + 7U)) - 1,
20 (UINT64_C(1) << (6U + 7U + 7U + 8U)) - 1,
23 static const std::array<uint64_t, 8> VARINT64_MAX_VALUES = {
24 (UINT64_C(1) << (6U)) - 1,
25 (UINT64_C(1) << (6U + 7U)) - 1,
26 (UINT64_C(1) << (6U + 7U + 7U)) - 1,
27 (UINT64_C(1) << (6U + 7U + 7U + 7U)) - 1,
28 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U)) - 1,
29 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U)) - 1,
30 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
31 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U + 7U + 8U)) - 1,
34 static const std::array<uint64_t, 2> VARUINT16_MAX_VALUES = {
35 (UINT64_C(1) << (7U)) - 1,
36 (UINT64_C(1) << (7U + 8U)) - 1,
39 static const std::array<uint64_t, 4> VARUINT32_MAX_VALUES = {
40 (UINT64_C(1) << (7U)) - 1,
41 (UINT64_C(1) << (7U + 7U)) - 1,
42 (UINT64_C(1) << (7U + 7U + 7U)) - 1,
43 (UINT64_C(1) << (7U + 7U + 7U + 8U)) - 1,
46 static const std::array<uint64_t, 8> VARUINT64_MAX_VALUES = {
47 (UINT64_C(1) << (7U)) - 1,
48 (UINT64_C(1) << (7U + 7U)) - 1,
49 (UINT64_C(1) << (7U + 7U + 7U)) - 1,
50 (UINT64_C(1) << (7U + 7U + 7U + 7U)) - 1,
51 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U)) - 1,
52 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U)) - 1,
53 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
54 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U + 7U + 8)) - 1,
57 static const std::array<uint64_t, 9> VARINT_MAX_VALUES = {
58 (UINT64_C(1) << (6U)) - 1,
59 (UINT64_C(1) << (6U + 7U)) - 1,
60 (UINT64_C(1) << (6U + 7U + 7U)) - 1,
61 (UINT64_C(1) << (6U + 7U + 7U + 7U)) - 1,
62 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U)) - 1,
63 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U)) - 1,
64 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
65 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
66 (UINT64_C(1) << (6U + 7U + 7U + 7U + 7U + 7U + 7U + 7U + 8)) - 1,
69 static const std::array<uint64_t, 9> VARUINT_MAX_VALUES = {
70 (UINT64_C(1) << (7U)) - 1,
71 (UINT64_C(1) << (7U + 7U)) - 1,
72 (UINT64_C(1) << (7U + 7U + 7U)) - 1,
73 (UINT64_C(1) << (7U + 7U + 7U + 7U)) - 1,
74 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U)) - 1,
75 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U)) - 1,
76 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
77 (UINT64_C(1) << (7U + 7U + 7U + 7U + 7U + 7U + 7U + 7U)) - 1,
81 static const std::array<uint64_t, 5> VARSIZE_MAX_VALUES = {
82 (UINT64_C(1) << (7U)) - 1,
83 (UINT64_C(1) << (7U + 7U)) - 1,
84 (UINT64_C(1) << (7U + 7U + 7U)) - 1,
85 (UINT64_C(1) << (7U + 7U + 7U + 7U)) - 1,
86 (UINT64_C(1) << (2U + 7U + 7U + 7U + 8U)) - 1,
89 template <std::
size_t SIZE>
90 static BitSize bitSizeOfVarIntImpl(
91 uint64_t value,
const std::array<uint64_t, SIZE>& maxValues,
const char* varIntName)
94 for (uint64_t maxValue : maxValues)
96 if (value <= maxValue)
103 if (byteSize > maxValues.size())
105 throw CppRuntimeException(
"BitSizeOfCalculator: Value '")
106 << value <<
"' is out of range for " << varIntName <<
"!";
112 template <
typename T>
113 static uint64_t convertToAbsValue(
typename T::ValueType value)
115 return static_cast<uint64_t
>((value < 0) ? -value : value);
120 return bitSizeOfVarIntImpl(convertToAbsValue<VarInt16>(value), VARINT16_MAX_VALUES,
"varint16");
125 return bitSizeOfVarIntImpl(convertToAbsValue<VarInt32>(value), VARINT32_MAX_VALUES,
"varint32");
130 return bitSizeOfVarIntImpl(convertToAbsValue<VarInt64>(value), VARINT64_MAX_VALUES,
"varint64");
135 return bitSizeOfVarIntImpl(value, VARUINT16_MAX_VALUES,
"varuint16");
140 return bitSizeOfVarIntImpl(value, VARUINT32_MAX_VALUES,
"varuint32");
145 return bitSizeOfVarIntImpl(value, VARUINT64_MAX_VALUES,
"varuint64");
150 if (value == INT64_MIN)
155 return bitSizeOfVarIntImpl(convertToAbsValue<VarInt>(value), VARINT_MAX_VALUES,
"varint");
160 return bitSizeOfVarIntImpl(value, VARUINT_MAX_VALUES,
"varuint");
165 return bitSizeOfVarIntImpl(value, VARSIZE_MAX_VALUES,
"varsize");
170 return bitSizeOf(value, bitPosition);
175 return bitSizeOf(value, bitPosition);
180 return bitSizeOf(value, bitPosition);
185 return bitSizeOf(value, bitPosition);
190 return bitSizeOf(value, bitPosition);
195 return bitSizeOf(value, bitPosition);
200 return bitSizeOf(value, bitPosition);
205 return bitSizeOf(value, bitPosition);
210 return bitSizeOf(value, bitPosition);
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize