pyMathBitPrecise package

Submodules

pyMathBitPrecise.array3t module

class pyMathBitPrecise.array3t.Array3t(element_t, size: int, name: str | None = None)

Bases: object

bit_length() int
from_py(val: List[value] | Dict[int, value] | None, vld_mask: int | None = None) Array3val

Construct value from pythonic value :note: str value has to start with base specifier (0b, 0h)

and is much slower than the value specified by ‘val’ and ‘vld_mask’. Does support x.

class pyMathBitPrecise.array3t.Array3val(t: Array3t, val: Dict[int, object], vld_mask: int)

Bases: object

Value of Array3t.

Note:

use Array3t.from_py if you want to check the the type of val

Variables:

vld_mask – if 0 the value is entirely invalid else some item may be valid

pyMathBitPrecise.bit_utils module

exception pyMathBitPrecise.bit_utils.ValidityError

Bases: ValueError

Value is not fully defined and thus can not be used

pyMathBitPrecise.bit_utils.align(val: int, lowerBitCntToAlign: int) int

Cut off lower bits to align a int value.

pyMathBitPrecise.bit_utils.align_with_known_width(val, width: int, lowerBitCntToAlign: int)

Does same as align() just with the known width of val

pyMathBitPrecise.bit_utils.apply_set_and_clear(val: int, set_flag: int, clear_flag: int)
Parameters:
  • val – an input value of the flag(s)

  • set_flag – a mask of bits to set to 1

  • clear_flag – a mask of bits to set to 0

Note:

set has higher priority

Returns:

new value of the flag

pyMathBitPrecise.bit_utils.apply_write_with_mask(current_data: Bits3val, new_data: Bits3val, write_mask: Bits3val) Bits3val
Returns:

an updated value current_data which has bytes defined by write_mask updated from new_data

pyMathBitPrecise.bit_utils.bit_field(_from: int, to: int) int

Generate int which has bits ‘_from’ to ‘to’ set to ‘1’

Note:

_from 0 to 1 -> ‘1’

pyMathBitPrecise.bit_utils.bit_list_reversed_bits_in_bytes(bitList: list[Literal[0, 1]], extend=None)

Byte reflection (0x0f -> 0xf0)

pyMathBitPrecise.bit_utils.bit_list_reversed_endianity(bitList: list[Literal[0, 1]], extend=True)
pyMathBitPrecise.bit_utils.bit_list_to_bytes(bitList: list[Literal[0, 1]]) bytes
pyMathBitPrecise.bit_utils.bit_list_to_int(bitList: list[Literal[0, 1]])

In input list LSB first, in result little endian ([0, 1] -> 0b10)

pyMathBitPrecise.bit_utils.bit_mask_to_byte_mask_int(m: int, width: int, byte_width: int = 8) int

Compresses all bit in byte to 1 bit to convert from bit mask to byte mask

pyMathBitPrecise.bit_utils.bit_set_to(val: int, bitNo: int, bitVal: int) int

Set specified bit in int to a specified value

pyMathBitPrecise.bit_utils.byte_list_to_be_int(_bytes: list[Literal[0, 1, 2, 3, 4, 5, 6, 7]])

In input list LSB first, in result little endian ([1, 0] -> 0x0001)

pyMathBitPrecise.bit_utils.byte_mask_to_bit_mask(m: Bits3Val, byte_width: int = 8) Bits3Val

Replicate each bit byte_width times

pyMathBitPrecise.bit_utils.byte_mask_to_bit_mask_int(m: int, width: int, byte_width: int = 8) int

Expands each bit byte_width times to convert from byte mask to bit mask

pyMathBitPrecise.bit_utils.bytes_to_bit_list_lower_bit_first(bytes_: bytes) list[Literal[0, 1]]

b’’ to [1, 0, 0, 0, 0, 0, 0, 0]

pyMathBitPrecise.bit_utils.bytes_to_bit_list_upper_bit_first(bytes_: bytes) list[Literal[0, 1]]

b’’ to [0, 0, 0, 0, 0, 0, 0, 1]

pyMathBitPrecise.bit_utils.clean_bit(val: int, bitNo: int) int

Set a specified bit to ‘0’

pyMathBitPrecise.bit_utils.clear_least_significant_1(x: int) int

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits 010110 -> 010100

Note:

can be used for 2**n test

pyMathBitPrecise.bit_utils.clear_trailing_1s(x: int) int

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits 10100111 -> 10100000

Note:

can be used for 2**n – 1 test

pyMathBitPrecise.bit_utils.ctlo(val: int, width: int) int

Count leading ones

pyMathBitPrecise.bit_utils.ctlz(val: int, width: int) int

Count leading zeros

pyMathBitPrecise.bit_utils.ctpop(val: int, width: int)

count number of 1 in val (population count)

pyMathBitPrecise.bit_utils.ctto(val: int)

Count trailing ones

pyMathBitPrecise.bit_utils.cttz(val: int, width: int)

Count trailing zeros

pyMathBitPrecise.bit_utils.extend_to_size(collection: Sequence, items: int, pad=0)
pyMathBitPrecise.bit_utils.extend_to_width_multiple_of_8(v: Bits3val) Bits3val

make width of signal modulo 8 equal to 0

pyMathBitPrecise.bit_utils.get_bit(val: int, bitNo: int) int

Get bit from int

pyMathBitPrecise.bit_utils.get_bit_range(val: int, bitsStart: int, bitsLen: int) int

Get sequence of bits from an int value

pyMathBitPrecise.bit_utils.get_single_0_at_position_of_least_significant_1(x: int, width: int)

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits

10101000 -> 11110111

pyMathBitPrecise.bit_utils.get_single_1_at_position_of_least_significant_0(x: int)

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits

10100111 -> 00001000

pyMathBitPrecise.bit_utils.int_list_to_int(il: list[int], item_width: int)

[0x0201, 0x0403] -> 0x04030201

pyMathBitPrecise.bit_utils.int_list_to_int_list(v: list[int], in_item_width: int, out_item_width: int, padvalue: int | None = None)

[0x0100, 0x0302, ] (width=16) <-> [0x00, 0x01, 0x02, 0x03] (width=8)

pyMathBitPrecise.bit_utils.int_to_int_list(v: int, item_width: int, number_of_items: int)

opposite of int_list_to_int()

pyMathBitPrecise.bit_utils.is_power_of_2(v: Bits3val | int)
pyMathBitPrecise.bit_utils.iter_bits(val: int, length: int) Generator[Literal[0, 1], None, None]

Iterate bits in int. LSB first.

pyMathBitPrecise.bit_utils.iter_bits_sequences(val: int, length: int) Generator[tuple[Literal[0, 1], int], None, None]

Iter tuples (bitVal, number of same bits), lsb first

pyMathBitPrecise.bit_utils.mask(bits: int) int

Generate mask of specified size (sequence of ‘1’)

pyMathBitPrecise.bit_utils.mask_bytes(val: int, byte_mask: int, mask_bit_length: int) int

Use each bit in byte_mask as a mask for each byte in val.

Note:

Useful for masking of value for HW interfaces where mask is represented by a vector of bits where each bit is mask for byte in data vector.

pyMathBitPrecise.bit_utils.next_power_of_2(v: Bits3val | int, width: int | None = None)
pyMathBitPrecise.bit_utils.normalize_slice(s: slice, obj_width: int) tuple[int, int]
pyMathBitPrecise.bit_utils.reverse_bits(val: int, width: int)

Reverse bits in integer value of specified width

pyMathBitPrecise.bit_utils.reverse_byte_order(val: Bits3val)

Reverse byteorder (littleendian/bigendian) of signal or value

pyMathBitPrecise.bit_utils.reverse_byte_order_int(val: int, width: int)
pyMathBitPrecise.bit_utils.rotate_left(v: int, width: int, shAmount: int)
pyMathBitPrecise.bit_utils.rotate_right(v: int, width: int, shAmount: int)
pyMathBitPrecise.bit_utils.round_up_to_multiple_of(v: int, divider: int)

Round up the v to be the multiple of divider

pyMathBitPrecise.bit_utils.round_up_to_power_of_2(x: int)
pyMathBitPrecise.bit_utils.separate_least_significant_1(x: int)

1100 -> 0100

pyMathBitPrecise.bit_utils.set_bit(val: int, bitNo: int) int

Set a specified bit to ‘1’

pyMathBitPrecise.bit_utils.set_bit_range(val: int, bitStart: int, bitsLen: int, newBits: int) int

Set specified range of bits in int to a specified value

pyMathBitPrecise.bit_utils.set_least_significant_0(x: int) int

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits

101001 -> 101011

pyMathBitPrecise.bit_utils.set_trailing_0s(x: int) int

Hacker’s Delight: 2nd Edition, 2-1 Manipulating Rightmost Bits

10101000 -> 10101111

pyMathBitPrecise.bit_utils.to_signed(val: int, width: int) int

Convert unsigned int to negative int which has same bits set (emulate sign overflow).

Note:

bits in value are not changed, just python int object has signed flag set properly. And number is in expected range.

pyMathBitPrecise.bit_utils.to_unsigned(val, width) int
pyMathBitPrecise.bit_utils.toggle_bit(val: int, bitNo: int) int

Toggle specified bit in int

pyMathBitPrecise.bits3t module

class pyMathBitPrecise.bits3t.Bits3t(bit_length: int, signed: bool | None = False, name: str | None = None, force_vector=False)

Bases: object

Meta type for integer of specified size where each bit can be ‘1’, ‘0’ or ‘X’ for undefined value.

Variables:
  • ~.bit_length – number representation of value of this type

  • ~.signed – flag which tells if this type is signed or not

  • ~._all_mask – cached value of mask for all bits

  • ~.name – name for annotation

  • ~.force_vector

    use always hdl vector type (for example std_logic_vector(0 downto 0)

    instead of std_logic in VHDL, wire[1] instead of wire)

all_mask() int
Returns:

mask for bites of this type ( 0b111 for Bits(3) )

bit_length() int
Returns:

number of bits required for representation of value of this type

from_py(val: int | bytes | str | Enum, vld_mask: int | None = None) Bits3val

Construct value from pythonic value :note: str value has to start with base specifier (0b, 0h)

and is much slower than the value specified by ‘val’ and ‘vld_mask’. Does support x.

get_domain_range()

equvalent of (get_min_value(), get_max_value()) :note: as an independent function from performace reasons

get_max_value()
get_min_value()
class pyMathBitPrecise.bits3t.Bits3val(t: Bits3t, val: int, vld_mask: int)

Bases: object

Class for value of Bits3t type

Variables:
  • ~._dtype – reference on type of this value

  • ~.val – always unsigned representation int value

  • ~.vld_mask – always unsigned value of the mask, if bit in mask is ‘0’ the corresponding bit in val is invalid

Note:

the reason why the unsigned is always used is that the signed variant would require cast to unsigned on every bitwise operation

to_py() int | None
pyMathBitPrecise.bits3t.bitsArithOp__val(self: Bits3val, other: Bits3val | int, evalFn: Callable[[int, int], int]) Bits3val

Apply arithmetic operator

pyMathBitPrecise.bits3t.bitsBitOp__ashr(self: Bits3val, shAmount: Bits3val | int) Bits3val

arithmetic shift right (shifts in MSB)

pyMathBitPrecise.bits3t.bitsBitOp__lshr(self: Bits3val, shAmount: Bits3val | int) Bits3val

logical shift right (shifts in 0)

pyMathBitPrecise.bits3t.bitsBitOp__rol(self: Bits3val, shAmount: Bits3val | int)

rotate left by specified amount

pyMathBitPrecise.bits3t.bitsBitOp__ror(self: Bits3val, shAmount: Bits3val | int)

rotate right by specified amount

pyMathBitPrecise.bits3t.bitsBitOp__val(self: Bits3val, other: Bits3val | int, evalFn, getVldFn) Bits3val

Apply bitwise operator

pyMathBitPrecise.bits3t.bitsCmp__val(self: Bits3val, other: Bits3val | int, evalFn: Callable[[int, int], bool]) Bits3val

Apply comparative operator

pyMathBitPrecise.bits3t.bitsCmp__val_EQ(self: Bits3val, other: Bits3val | int) Bits3val

Apply == operator

pyMathBitPrecise.bits3t.bitsCmp__val_NE(self: Bits3val, other: Bits3val | int) Bits3val

Apply != operator

pyMathBitPrecise.bits3t_vld_masks module

pyMathBitPrecise.bits3t_vld_masks.vld_mask_for_and(a, b) int
pyMathBitPrecise.bits3t_vld_masks.vld_mask_for_or(a, b) int
pyMathBitPrecise.bits3t_vld_masks.vld_mask_for_xor(a, b) int

pyMathBitPrecise.enum3t module

class pyMathBitPrecise.enum3t.Enum3t

Bases: object

Note:

use as Python enum.Enum, the value is always ignored and name is used as a value

from_py(val: None, vld_mask: int | None = None) Enum3val
Attention:

Used only in initialization, use enum class properties if you want to get a value

class pyMathBitPrecise.enum3t.Enum3tMeta(cls, bases, classdict)

Bases: type

class pyMathBitPrecise.enum3t.Enum3val(t, val, vld_mask)

Bases: object

pyMathBitPrecise.enum3t.define_Enum3t(name: str, values: List[str])

Define Enum3t subclass from enum names

pyMathBitPrecise.floatt module

class pyMathBitPrecise.floatt.Floatt(exponent_w, mantisa_w, name=None)

Bases: object

IEEE 754 like float type with configurable sizes fo exponent and mantisa.

all_mask()
Returns:

mask for bites of this type ( 0b111 for Bits(3) )

bit_length()
Returns:

number of bits required for representation of value of this type

from_py(val: int | str | float, vld_mask: int | None = None) FloattVal

Construct value from pythonic value

class pyMathBitPrecise.floatt.FloattVal(t: Floatt, val: Tuple[int, int, int], vld_mask: int)

Bases: object

Class for value of Bits3t type

Variables:
  • ~._dtype – reference on type of this value

  • ~.val – always unsigned representation int value

  • ~.vld_mask – always unsigned value of the mask, if bit in mask is ‘0’ the corresponding bit in val is invalid

to_py() int
pyMathBitPrecise.floatt.FloattVal__arith_op(self, other, op)
pyMathBitPrecise.floatt.FloattVal__cmp_op(self, other, op)

pyMathBitPrecise.utils module

pyMathBitPrecise.utils.grouper(n, iterable, padvalue=None)

grouper(3, ‘abcdefg’, ‘x’) –> (‘a’,’b’,’c’), (‘d’,’e’,’f’), (‘g’,’x’,’x’)