Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/bitsizeof.py: 100%
48 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-10-29 13:10 +0000
1"""
2The module provides help methods to calculate size of nontrivial types.
3"""
5import typing
7from zserio.bitbuffer import BitBuffer
8from zserio.exception import PythonRuntimeException
9from zserio.limits import INT64_MIN
12def bitsizeof_varint16(value: int) -> int:
13 """
14 Gets bit size of variable 16-bit signed integer value.
16 :param value: Value to use for bit size calculation.
17 :returns: Bit size of the value.
18 :raises PythonRuntimeException: Throws if given value is out of range for varint16 type.
19 """
21 return _bitsizeof_varnum(abs(value), VARINT16_MAX_VALUES, "varint16")
24def bitsizeof_varint32(value: int) -> int:
25 """
26 Gets bit size of variable 32-bit signed integer value.
28 :param value: Value to use for bit size calculation.
29 :returns: Bit size of the value.
30 :raises PythonRuntimeException: Throws if given value is out of range for varint32 type.
31 """
33 return _bitsizeof_varnum(abs(value), VARINT32_MAX_VALUES, "varint32")
36def bitsizeof_varint64(value: int) -> int:
37 """
38 Gets bit size of variable 64-bit signed integer value.
40 :param value: Value to use for bit size calculation.
41 :returns: Bit size of the value.
42 :raises PythonRuntimeException: Throws if given value is out of range for varint64 type.
43 """
45 return _bitsizeof_varnum(abs(value), VARINT64_MAX_VALUES, "varint64")
48def bitsizeof_varint(value: int) -> int:
49 """
50 Gets bit size of variable signed integer value (up to 9 bytes).
52 :param value: Value to use for bit size calculation.
53 :returns: Bit size of the value.
54 :raises PythonRuntimeException: Throws if given value is out of range for varint type.
55 """
57 if value == INT64_MIN:
58 return 8 # INT64_MIN is stored as -0
59 return _bitsizeof_varnum(abs(value), VARINT_MAX_VALUES, "varint")
62def bitsizeof_varuint16(value: int) -> int:
63 """
64 Gets bit size of variable 16-bit unsigned integer value.
66 :param value: Value to use for bit size calculation.
67 :returns: Bit size of the value.
68 :raises PythonRuntimeException: Throws if given value is out of range for varuint16 type.
69 """
71 return _bitsizeof_varnum(value, VARUINT16_MAX_VALUES, "varuint16")
74def bitsizeof_varuint32(value: int) -> int:
75 """
76 Gets bit size of variable 32-bit unsigned integer value.
78 :param value: Value to use for bit size calculation.
79 :returns: Bit size of the value.
80 :raises PythonRuntimeException: Throws if given value is out of range for varuint32 type.
81 """
83 return _bitsizeof_varnum(value, VARUINT32_MAX_VALUES, "varuint32")
86def bitsizeof_varuint64(value: int) -> int:
87 """
88 Gets bit size of variable 64-bit unsigned integer value.
90 :param value: Value to use for bit size calculation.
91 :returns: Bit size of the value.
92 :raises PythonRuntimeException: Throws if given value is out of range for varuint64 type.
93 """
95 return _bitsizeof_varnum(value, VARUINT64_MAX_VALUES, "varuint64")
98def bitsizeof_varuint(value: int) -> int:
99 """
100 Gets bit size of variable unsigned integer value (up to 9 bytes).
102 :param value: Value to use for bit size calculation.
103 :returns: Bit size of the value.
104 :raises PythonRuntimeException: Throws if given value is out of range for varuint type.
105 """
107 return _bitsizeof_varnum(value, VARUINT_MAX_VALUES, "varuint")
110def bitsizeof_varsize(value: int) -> int:
111 """
112 Gets bit size of variable size integer value.
114 :param value: Value to use for bit size calculation.
115 :returns: Bit size of the value.
116 :raises PythonRuntimeException: Throws if given value is out of range for varsize type.
117 """
119 return _bitsizeof_varnum(value, VARSIZE_MAX_VALUES, "varsize")
122def bitsizeof_bytes(value: bytearray) -> int:
123 """
124 Gets bit size of bytes.
126 :param value: Bytes value to use for bit size calculation.
127 :raises PythonRuntimeException: Throws if given string is too long.
128 """
130 return bitsizeof_varsize(len(value)) + len(value) * 8
133def bitsizeof_string(string: str) -> int:
134 """
135 Gets bit size of string.
137 :param string: String value to use for bit size calculation.
138 :raises PythonRuntimeException: Throws if given string is too long.
139 """
141 string_bytes = string.encode("utf-8")
142 return bitsizeof_varsize(len(string_bytes)) + len(string_bytes) * 8
145def bitsizeof_bitbuffer(bitbuffer: BitBuffer) -> int:
146 """
147 Gets the bit size of bit buffer which is stored in bit stream.
149 :param bitbuffer: Bit buffer for calculation.
150 :returns: Length of bit buffer in bits.
151 :raises PythonRuntimeException: Throws if given bit buffer is too long.
152 """
153 bitbuffer_size = bitbuffer.bitsize
155 # bit buffer consists of varsize for bit size followed by the bits
156 return bitsizeof_varsize(bitbuffer_size) + bitbuffer_size
159def _bitsizeof_varnum(value: int, max_values: typing.Sequence[int], varint_name: str) -> int:
160 if value >= 0:
161 abs_value = abs(value)
162 for i, max_value in enumerate(max_values):
163 if abs_value <= max_value:
164 return (i + 1) * 8
166 raise PythonRuntimeException(f"bitsizeof: Value '{value}' is out of range for '{varint_name}'!")
169VARINT16_MAX_VALUES = [(1 << (6)) - 1, (1 << (6 + 8)) - 1]
171VARINT32_MAX_VALUES = [
172 (1 << (6)) - 1,
173 (1 << (6 + 7)) - 1,
174 (1 << (6 + 7 + 7)) - 1,
175 (1 << (6 + 7 + 7 + 8)) - 1,
176]
178VARINT64_MAX_VALUES = [
179 (1 << (6)) - 1,
180 (1 << (6 + 7)) - 1,
181 (1 << (6 + 7 + 7)) - 1,
182 (1 << (6 + 7 + 7 + 7)) - 1,
183 (1 << (6 + 7 + 7 + 7 + 7)) - 1,
184 (1 << (6 + 7 + 7 + 7 + 7 + 7)) - 1,
185 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
186 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1,
187]
189VARINT_MAX_VALUES = [
190 (1 << (6)) - 1,
191 (1 << (6 + 7)) - 1,
192 (1 << (6 + 7 + 7)) - 1,
193 (1 << (6 + 7 + 7 + 7)) - 1,
194 (1 << (6 + 7 + 7 + 7 + 7)) - 1,
195 (1 << (6 + 7 + 7 + 7 + 7 + 7)) - 1,
196 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
197 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
198 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1,
199]
201VARUINT16_MAX_VALUES = [(1 << (7)) - 1, (1 << (7 + 8)) - 1]
203VARUINT32_MAX_VALUES = [
204 (1 << (7)) - 1,
205 (1 << (7 + 7)) - 1,
206 (1 << (7 + 7 + 7)) - 1,
207 (1 << (7 + 7 + 7 + 8)) - 1,
208]
210VARUINT64_MAX_VALUES = [
211 (1 << (7)) - 1,
212 (1 << (7 + 7)) - 1,
213 (1 << (7 + 7 + 7)) - 1,
214 (1 << (7 + 7 + 7 + 7)) - 1,
215 (1 << (7 + 7 + 7 + 7 + 7)) - 1,
216 (1 << (7 + 7 + 7 + 7 + 7 + 7)) - 1,
217 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
218 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1,
219]
221VARUINT_MAX_VALUES = [
222 (1 << (7)) - 1,
223 (1 << (7 + 7)) - 1,
224 (1 << (7 + 7 + 7)) - 1,
225 (1 << (7 + 7 + 7 + 7)) - 1,
226 (1 << (7 + 7 + 7 + 7 + 7)) - 1,
227 (1 << (7 + 7 + 7 + 7 + 7 + 7)) - 1,
228 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
229 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1,
230 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1,
231]
233VARSIZE_MAX_VALUES = [
234 (1 << (7)) - 1,
235 (1 << (7 + 7)) - 1,
236 (1 << (7 + 7 + 7)) - 1,
237 (1 << (7 + 7 + 7 + 7)) - 1,
238 (1 << (2 + 7 + 7 + 7 + 8)) - 1,
239]