Nic30 / pyMathBitPrecise

Python library for math with values on specified number of bits

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyMathBitPrecise

CircleCI Coverage Status PyPI version Documentation Status Python version

This library contains data types of fixed bit size and utils for bit manipulations. It also contains types with support for tri state values etc. (Python equivalents of VHDL std_logic_vector, Verilog wire/reg.)

This may be useful for tools which are simulating hardware or software which needs numbers of exact size.

Example

from pyMathBitPrecise.bits3t import Bits3t

#3t means that bits can have values 1,0,x
uint512_t = Bits3t(512, signed=False)

a = uint512_t.from_py(1)

# indexing on bits
# [note] == is not overloaded, because it would make the values unhashable
#        because of support of partially valid values which can not be compared
assert a[0]._eq(1)
assert a[0]._dtype.bit_length() == 1
assert a[1]._eq(0)
assert a[8:]._eq(1)
assert a[8:]._dtype.bit_length() == 8

# arithmetic
b = a + 1
assert b._eq(2)
assert b._dtype == uint512_t

# bitwise operations
c = a >> 8
assert c._eq(0)
assert c._dtype == uint512_t

# casting
d = int(a)
assert d == 1 and isinstance(d, int)

uint8_t = Bits3t(8, signed=False)
e = a.cast(uint8_t)
assert e._dtype == uint8_t

Similar projects

  • hwtypes - Python implementations of fixed size hardware types
  • Bitwise
  • bsim - C++, arbitrary length bit vectors

About

Python library for math with values on specified number of bits

License:MIT License


Languages

Language:Python 100.0%