Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/src/zserio/bitsizeof.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-12-13 15:12 +0000

1""" 

2The module provides help methods to calculate size of nontrivial types. 

3""" 

4 

5import typing 

6 

7from zserio.bitbuffer import BitBuffer 

8from zserio.exception import PythonRuntimeException 

9from zserio.limits import INT64_MIN 

10 

11def bitsizeof_varint16(value: int) -> int: 

12 """ 

13 Gets bit size of variable 16-bit signed integer value. 

14 

15 :param value: Value to use for bit size calculation. 

16 :returns: Bit size of the value. 

17 :raises PythonRuntimeException: Throws if given value is out of range for varint16 type. 

18 """ 

19 

20 return _bitsizeof_varnum(abs(value), VARINT16_MAX_VALUES, "varint16") 

21 

22def bitsizeof_varint32(value: int) -> int: 

23 """ 

24 Gets bit size of variable 32-bit signed integer value. 

25 

26 :param value: Value to use for bit size calculation. 

27 :returns: Bit size of the value. 

28 :raises PythonRuntimeException: Throws if given value is out of range for varint32 type. 

29 """ 

30 

31 return _bitsizeof_varnum(abs(value), VARINT32_MAX_VALUES, "varint32") 

32 

33def bitsizeof_varint64(value: int) -> int: 

34 """ 

35 Gets bit size of variable 64-bit signed integer value. 

36 

37 :param value: Value to use for bit size calculation. 

38 :returns: Bit size of the value. 

39 :raises PythonRuntimeException: Throws if given value is out of range for varint64 type. 

40 """ 

41 

42 return _bitsizeof_varnum(abs(value), VARINT64_MAX_VALUES, "varint64") 

43 

44def bitsizeof_varint(value: int) -> int: 

45 """ 

46 Gets bit size of variable signed integer value (up to 9 bytes). 

47 

48 :param value: Value to use for bit size calculation. 

49 :returns: Bit size of the value. 

50 :raises PythonRuntimeException: Throws if given value is out of range for varint type. 

51 """ 

52 

53 if value == INT64_MIN: 

54 return 8 # INT64_MIN is stored as -0 

55 return _bitsizeof_varnum(abs(value), VARINT_MAX_VALUES, "varint") 

56 

57def bitsizeof_varuint16(value: int) -> int: 

58 """ 

59 Gets bit size of variable 16-bit unsigned integer value. 

60 

61 :param value: Value to use for bit size calculation. 

62 :returns: Bit size of the value. 

63 :raises PythonRuntimeException: Throws if given value is out of range for varuint16 type. 

64 """ 

65 

66 return _bitsizeof_varnum(value, VARUINT16_MAX_VALUES, "varuint16") 

67 

68def bitsizeof_varuint32(value: int) -> int: 

69 """ 

70 Gets bit size of variable 32-bit unsigned integer value. 

71 

72 :param value: Value to use for bit size calculation. 

73 :returns: Bit size of the value. 

74 :raises PythonRuntimeException: Throws if given value is out of range for varuint32 type. 

75 """ 

76 

77 return _bitsizeof_varnum(value, VARUINT32_MAX_VALUES, "varuint32") 

78 

79def bitsizeof_varuint64(value: int) -> int: 

80 """ 

81 Gets bit size of variable 64-bit unsigned integer value. 

82 

83 :param value: Value to use for bit size calculation. 

84 :returns: Bit size of the value. 

85 :raises PythonRuntimeException: Throws if given value is out of range for varuint64 type. 

86 """ 

87 

88 return _bitsizeof_varnum(value, VARUINT64_MAX_VALUES, "varuint64") 

89 

90def bitsizeof_varuint(value: int) -> int: 

91 """ 

92 Gets bit size of variable unsigned integer value (up to 9 bytes). 

93 

94 :param value: Value to use for bit size calculation. 

95 :returns: Bit size of the value. 

96 :raises PythonRuntimeException: Throws if given value is out of range for varuint type. 

97 """ 

98 

99 return _bitsizeof_varnum(value, VARUINT_MAX_VALUES, "varuint") 

100 

101def bitsizeof_varsize(value: int) -> int: 

102 """ 

103 Gets bit size of variable size integer value. 

104 

105 :param value: Value to use for bit size calculation. 

106 :returns: Bit size of the value. 

107 :raises PythonRuntimeException: Throws if given value is out of range for varsize type. 

108 """ 

109 

110 return _bitsizeof_varnum(value, VARSIZE_MAX_VALUES, "varsize") 

111 

112def bitsizeof_bytes(value: bytearray) -> int: 

113 """ 

114 Gets bit size of bytes. 

115 

116 :param value: Bytes value to use for bit size calculation. 

117 :raises PythonRuntimeException: Throws if given string is too long. 

118 """ 

119 

120 return bitsizeof_varsize(len(value)) + len(value) * 8 

121 

122def bitsizeof_string(string: str) -> int: 

123 """ 

124 Gets bit size of string. 

125 

126 :param string: String value to use for bit size calculation. 

127 :raises PythonRuntimeException: Throws if given string is too long. 

128 """ 

129 

130 string_bytes = string.encode("utf-8") 

131 return bitsizeof_varsize(len(string_bytes)) + len(string_bytes) * 8 

132 

133def bitsizeof_bitbuffer(bitbuffer: BitBuffer) -> int: 

134 """ 

135 Gets the bit size of bit buffer which is stored in bit stream. 

136 

137 :param bitbuffer: Bit buffer for calculation. 

138 :returns: Length of bit buffer in bits. 

139 :raises PythonRuntimeException: Throws if given bit buffer is too long. 

140 """ 

141 bitbuffer_size = bitbuffer.bitsize 

142 

143 # bit buffer consists of varsize for bit size followed by the bits 

144 return bitsizeof_varsize(bitbuffer_size) + bitbuffer_size 

145 

146def _bitsizeof_varnum(value: int, max_values: typing.Sequence[int], varint_name: str) -> int: 

147 if value >= 0: 

148 abs_value = abs(value) 

149 for i, max_value in enumerate(max_values): 

150 if abs_value <= max_value: 

151 return (i + 1) * 8 

152 

153 raise PythonRuntimeException(f"bitsizeof: Value '{value}' is out of range for '{varint_name}'!") 

154 

155VARINT16_MAX_VALUES = [ 

156 (1 << (6)) - 1, 

157 (1 << (6 + 8)) - 1 

158] 

159 

160VARINT32_MAX_VALUES = [ 

161 (1 << (6)) - 1, 

162 (1 << (6 + 7)) - 1, 

163 (1 << (6 + 7 + 7)) - 1, 

164 (1 << (6 + 7 + 7 + 8)) - 1, 

165] 

166 

167VARINT64_MAX_VALUES = [ 

168 (1 << (6)) - 1, 

169 (1 << (6 + 7)) - 1, 

170 (1 << (6 + 7 + 7)) - 1, 

171 (1 << (6 + 7 + 7 + 7)) - 1, 

172 (1 << (6 + 7 + 7 + 7 + 7)) - 1, 

173 (1 << (6 + 7 + 7 + 7 + 7 + 7)) - 1, 

174 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, 

175 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1, 

176] 

177 

178VARINT_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 + 7)) - 1, 

187 (1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1 

188] 

189 

190VARUINT16_MAX_VALUES = [ 

191 (1 << (7)) - 1, 

192 (1 << (7 + 8)) - 1 

193] 

194 

195VARUINT32_MAX_VALUES = [ 

196 (1 << (7)) - 1, 

197 (1 << (7 + 7)) - 1, 

198 (1 << (7 + 7 + 7)) - 1, 

199 (1 << (7 + 7 + 7 + 8)) - 1 

200] 

201 

202VARUINT64_MAX_VALUES = [ 

203 (1 << (7)) - 1, 

204 (1 << (7 + 7)) - 1, 

205 (1 << (7 + 7 + 7)) - 1, 

206 (1 << (7 + 7 + 7 + 7)) - 1, 

207 (1 << (7 + 7 + 7 + 7 + 7)) - 1, 

208 (1 << (7 + 7 + 7 + 7 + 7 + 7)) - 1, 

209 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, 

210 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1 

211] 

212 

213VARUINT_MAX_VALUES = [ 

214 (1 << (7)) - 1, 

215 (1 << (7 + 7)) - 1, 

216 (1 << (7 + 7 + 7)) - 1, 

217 (1 << (7 + 7 + 7 + 7)) - 1, 

218 (1 << (7 + 7 + 7 + 7 + 7)) - 1, 

219 (1 << (7 + 7 + 7 + 7 + 7 + 7)) - 1, 

220 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, 

221 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1, 

222 (1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1, 

223] 

224 

225VARSIZE_MAX_VALUES = [ 

226 (1 << (7)) - 1, 

227 (1 << (7 + 7)) - 1, 

228 (1 << (7 + 7 + 7)) - 1, 

229 (1 << (7 + 7 + 7 + 7)) - 1, 

230 (1 << (2 + 7 + 7 + 7 + 8)) - 1, 

231]