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

17 statements  

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

1""" 

2The module provides help methods for bit fields calculation. 

3""" 

4 

5from zserio.exception import PythonRuntimeException 

6 

7def bitfield_lowerbound(length: int) -> int: 

8 """ 

9 Gets the lower bound of a unsigned bitfield type with given length. 

10 

11 :param length: Length of the unsigned bitfield in bits. 

12 :returns: The lowest value the unsigned bitfield can hold. 

13 :raises PythonRuntimeException: If unsigned bitfield with wrong length has been specified. 

14 """ 

15 

16 _check_bitfield_length(length) 

17 return 0 

18 

19def bitfield_upperbound(length: int) -> int: 

20 

21 """ 

22 Gets the upper bound of a unsigned bitfield type with given length. 

23 

24 :param length: Length of the unsigned bitfield in bits. 

25 :returns: The largest value the unsigned bitfield can hold. 

26 :raises PythonRuntimeException: If unsigned bitfield with wrong length has been specified. 

27 """ 

28 

29 _check_bitfield_length(length) 

30 return (1 << length) - 1 

31 

32def signed_bitfield_lowerbound(length: int) -> int: 

33 """ 

34 Gets the lower bound of a signed bitfield type with given length. 

35 

36 :param length: Length of the signed bitfield in bits. 

37 :returns: The lowest value the signed bitfield can hold. 

38 :raises PythonRuntimeException: If signed bitfield with wrong length has been specified. 

39 """ 

40 

41 _check_bitfield_length(length) 

42 return -(1 << (length - 1)) 

43 

44def signed_bitfield_upperbound(length: int) -> int: 

45 """ 

46 Gets the upper bound of a signed bitfield type with given length. 

47 

48 :param length: Length of the signed bitfield in bits. 

49 :returns: The largest value the signed bitfield can hold. 

50 :raises PythonRuntimeException: If signed bitfield with wrong length has been specified. 

51 """ 

52 

53 _check_bitfield_length(length) 

54 return (1 << (length - 1)) - 1 

55 

56def _check_bitfield_length(length: int) -> None: 

57 if length <= 0 or length > MAX_BITFIELD_BITS: 

58 raise PythonRuntimeException(f"bitfield: Asking for bound of bitfield with invalid length '{length}'!") 

59 

60MAX_BITFIELD_BITS = 64