Zserio C++17 runtime library  0.5.0
Built for Zserio 2.17.0
Types.cpp
Go to the documentation of this file.
1 #include <array>
2 
3 #include "zserio/Types.h"
4 
5 namespace zserio
6 {
7 
8 namespace detail
9 {
10 
11 static const std::array<uint64_t, 2> VARINT16_MAX_VALUES = {
12  (UINT64_C(1) << (6U)) - 1,
13  (UINT64_C(1) << (6U + 8U)) - 1,
14 };
15 
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,
21 };
22 
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,
32 };
33 
34 static const std::array<uint64_t, 2> VARUINT16_MAX_VALUES = {
35  (UINT64_C(1) << (7U)) - 1,
36  (UINT64_C(1) << (7U + 8U)) - 1,
37 };
38 
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,
44 };
45 
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,
55 };
56 
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,
67 };
68 
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,
78  UINT64_MAX,
79 };
80 
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,
87 };
88 
89 template <std::size_t SIZE>
90 static BitSize bitSizeOfVarIntImpl(
91  uint64_t value, const std::array<uint64_t, SIZE>& maxValues, const char* varIntName)
92 {
93  BitSize byteSize = 1;
94  for (uint64_t maxValue : maxValues)
95  {
96  if (value <= maxValue)
97  {
98  break;
99  }
100  byteSize++;
101  }
102 
103  if (byteSize > maxValues.size())
104  {
105  throw CppRuntimeException("BitSizeOfCalculator: Value '")
106  << value << "' is out of range for " << varIntName << "!";
107  }
108 
109  return byteSize * 8;
110 }
111 
112 template <typename T>
113 static uint64_t convertToAbsValue(typename T::ValueType value)
114 {
115  return static_cast<uint64_t>((value < 0) ? -value : value);
116 }
117 
118 BitSize bitSizeOf(VarInt16 value, BitSize)
119 {
120  return bitSizeOfVarIntImpl(convertToAbsValue<VarInt16>(value), VARINT16_MAX_VALUES, "varint16");
121 }
122 
123 BitSize bitSizeOf(VarInt32 value, BitSize)
124 {
125  return bitSizeOfVarIntImpl(convertToAbsValue<VarInt32>(value), VARINT32_MAX_VALUES, "varint32");
126 }
127 
128 BitSize bitSizeOf(VarInt64 value, BitSize)
129 {
130  return bitSizeOfVarIntImpl(convertToAbsValue<VarInt64>(value), VARINT64_MAX_VALUES, "varint64");
131 }
132 
133 BitSize bitSizeOf(VarUInt16 value, BitSize)
134 {
135  return bitSizeOfVarIntImpl(value, VARUINT16_MAX_VALUES, "varuint16");
136 }
137 
138 BitSize bitSizeOf(VarUInt32 value, BitSize)
139 {
140  return bitSizeOfVarIntImpl(value, VARUINT32_MAX_VALUES, "varuint32");
141 }
142 
143 BitSize bitSizeOf(VarUInt64 value, BitSize)
144 {
145  return bitSizeOfVarIntImpl(value, VARUINT64_MAX_VALUES, "varuint64");
146 }
147 
148 BitSize bitSizeOf(VarInt value, BitSize)
149 {
150  if (value == INT64_MIN)
151  {
152  return 8; // INT64_MIN is stored as -0
153  }
154 
155  return bitSizeOfVarIntImpl(convertToAbsValue<VarInt>(value), VARINT_MAX_VALUES, "varint");
156 }
157 
158 BitSize bitSizeOf(VarUInt value, BitSize)
159 {
160  return bitSizeOfVarIntImpl(value, VARUINT_MAX_VALUES, "varuint");
161 }
162 
163 BitSize bitSizeOf(VarSize value, BitSize)
164 {
165  return bitSizeOfVarIntImpl(value, VARSIZE_MAX_VALUES, "varsize");
166 }
167 
168 BitSize initializeOffsets(VarInt16 value, BitSize bitPosition)
169 {
170  return bitSizeOf(value, bitPosition);
171 }
172 
173 BitSize initializeOffsets(VarInt32 value, BitSize bitPosition)
174 {
175  return bitSizeOf(value, bitPosition);
176 }
177 
178 BitSize initializeOffsets(VarInt64 value, BitSize bitPosition)
179 {
180  return bitSizeOf(value, bitPosition);
181 }
182 
183 BitSize initializeOffsets(VarUInt16 value, BitSize bitPosition)
184 {
185  return bitSizeOf(value, bitPosition);
186 }
187 
188 BitSize initializeOffsets(VarUInt32 value, BitSize bitPosition)
189 {
190  return bitSizeOf(value, bitPosition);
191 }
192 
193 BitSize initializeOffsets(VarUInt64 value, BitSize bitPosition)
194 {
195  return bitSizeOf(value, bitPosition);
196 }
197 
198 BitSize initializeOffsets(VarInt value, BitSize bitPosition)
199 {
200  return bitSizeOf(value, bitPosition);
201 }
202 
203 BitSize initializeOffsets(VarUInt value, BitSize bitPosition)
204 {
205  return bitSizeOf(value, bitPosition);
206 }
207 
208 BitSize initializeOffsets(VarSize value, BitSize bitPosition)
209 {
210  return bitSizeOf(value, bitPosition);
211 }
212 
213 } // namespace detail
214 
215 } // namespace zserio
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
Definition: Types.h:888
unsigned int BitSize
Definition: BitSize.h:8
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
Definition: Types.h:881
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
Definition: Types.h:885
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
Definition: Types.h:887
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
Definition: Types.h:880
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
Definition: Types.h:886
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
Definition: Types.h:883
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
Definition: Types.h:882
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize
Definition: Types.h:890