Integer

Implements fixed width integer types: Uint - unsigned, Int - signed and Integer - sign agnostic type. These types correspond to HDL logic vector types.

Objects of these classes can also be instantiated and they provide some integer arithmetic capabilities.

class IntegerType

Bases: pygears.typing.base.EnumerableGenericMeta

Defines common methods for all Integer based classes.

__add__(other)

Returns the same type, but one bit wider to accomodate potential overflow.

>>> Uint[8] + Uint[8]
Uint[9]
__int__()

Calculates the bit width of the type.

>>> int(Tuple[Uint[1], Uint[2]])
3
__mul__(other)

Returns the same type, whose width is equal to the sum of operand widths.

>>> Uint[8] + Uint[8]
Uint[16]
__sub__(other)

Returns the signed Int type, but one bit wider to accomodate potential overflow.

>>> Uint[8] + Uint[8]
Int[9]
args

Returns a list of values supplied for each generic parameter.

>>> Tuple[Uint[1], Uint[2]].args
[Uint[1], Uint[2]]
base

Returns base generic class of the type.

>>> assert Uint[16].base == Uint
fields

Returns the names of the generic parameters.

>>> Tuple[Uint[1], Uint[2]].fields
('f0', 'f1')
>>> Tuple[{'u1': Uint[1], 'u2': Uint[2]}].fields
('u0', 'u1')
is_generic()

Return True if no values have been supplied for the generic parameters.

>>> Uint.is_generic()
True
>>> Uint['template'].is_generic()
False
is_specified

Return True if all generic parameters were supplied concrete values.

>>> Uint['template'].is_specified()
False
>>> Uint[16].is_specified()
True
items()

Generator that yields (key, element) pairs.

keys()

Returns a list of keys that can be used for indexing the type.

>>> Int[8].keys()
[0, 1, 2, 3, 4, 5, 6, 7]
templates

Returns a list of templated generic variables within the type. The type is searched recursively. Each template is reported only once.

>>> Tuple[Tuple['T1', 'T2'], 'T1'].templates
['T1', 'T2']
class Integer

Bases: int

Base type for both Int and Uint generic types. Corresponds to HDL logic vector types. For an example Integer[9] translates to logic [8:0].

__getitem__(index)

Returns the value of the indexth bit in the number representation.

>>> Integer[8](0b10101010)[5]
1
>>> Integer[8](0b10101010)[1::2]
Uint[4](15)
__int__()

Returns builtin integer type

>>> type(int(Integer[8](0)))
<class 'int'>
__len__()

Returns the number of bits used for the representation

>>> len(Integer[8](0))
8
classmethod decode(val)

Creates Integer object from any int-convertible object val.

>>> Integer[8].decode(0xffff)
Integer[8](255)
width

Returns the number of bits used for the representation

>>> Integer[8](0).width
8