Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/src/zserio/bitposition.py: 100%
13 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-12-13 15:12 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-12-13 15:12 +0000
1"""
2The module provides help methods for bit position calculation.
3"""
5from zserio.exception import PythonRuntimeException
7def alignto(alignment_value: int, bitposition: int) -> int:
8 """
9 Aligns the bit size to the given alignment value.
11 :param alignment_value: Value to align.
12 :param bitposition: Current bit position where to apply alignment.
13 :returns: Aligned bit position.
14 """
16 if bitposition <= 0 or alignment_value == 0:
17 return bitposition
19 return (((bitposition - 1) // alignment_value) + 1) * alignment_value
21def bits_to_bytes(numbits: int) -> int:
22 """
23 Converts number of bits to bytes.
25 :param numbits: The number of bits to convert.
26 :returns: Number of bytes
27 :raises PythonRuntimeException: If number of bits to convert is not divisible by 8.
28 """
30 if numbits % 8 != 0:
31 raise PythonRuntimeException(f"bitposition: '{numbits}' is not a multiple of 8!")
33 return numbits // 8
35def bytes_to_bits(num_bytes: int) -> int:
36 """
37 Converts number of bytes to bits.
39 :param num_bytes: The n number of bytes to convert.
40 :returns: Number of bits.
41 """
43 return num_bytes * 8
45def bitsize_to_bytesize(bitsize: int) -> int:
46 """
47 Converts number of bits to number of bytes.
49 :param bitsize: Size in bits to convert.
50 :returns: Size in bytes.
51 """
53 return (bitsize + 7) // 8