Registry / type-stubs / hwtypes

hwtypes

JSON →
library1.4.7pypypi✓ verified 89d ago

hwtypes is a Python library providing implementations of fixed-size hardware types such as Bit, BitVector, UInt, and SInt. These types are designed to mimic hardware semantics, including explicit bit-widths and modular arithmetic, based on SMT-LIB2 specifications. The current version is 1.4.7, with an active but less frequent release cadence focused on stability and core functionality.

pip install hwtypes
INSTALL
IMPORT
SIG · HWTYPES
H
hwtypes
type-stubspythonv1.4.7
Install
4.0s avg
Import
619ms
Disk
74MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.4.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.910 runs
build_error
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 4.0s · import 0.619s · 76MB
74MB installed
● package 74MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Bit
✓ from hwtypes import Bit
BitVector
✓ from hwtypes import BitVector
UInt
✓ from hwtypes import UInt
SInt
✓ from hwtypes import SInt

Demonstrates the basic usage of Bit, BitVector, UInt, and SInt types, including their initialization and simple arithmetic operations, highlighting the bit-width specification and distinct arithmetic semantics.

from hwtypes import Bit, BitVector, UInt, SInt # Bit a = Bit(0) b = Bit(1) c = a & b # c = Bit(0) # BitVector (8-bit) x = BitVector[8](0xAF) y = BitVector[8](0xBA) z = x + y # z = BitVector[8](0x69) (modular arithmetic) # UInt (4-bit unsigned integer) u = UInt[4](5) v = UInt[4](3) w = u + v # w = UInt[4](8) # SInt (4-bit signed integer) s = SInt[4](-2) t = SInt[4](-3) u_sint = s + t # u_sint = SInt[4](-5) print(f"Bit c: {c}") print(f"BitVector z: {z}") print(f"UInt w: {w}") print(f"SInt u_sint: {u_sint}")
Debug
Known issues
gotchaArithmetic operations on `BitVector` types perform modular (truncating) arithmetic and do not inherently distinguish between signed and unsigned interpretations. For defined signed or unsigned semantics, explicitly use `SInt` or `UInt`.
fix
Use `SInt[N](value)` or `UInt[N](value)` for operations requiring specific signed/unsigned behavior. Be aware of `BitVector`'s modular arithmetic for raw bit manipulation.
affects: all
gotchaAll `BitVector`, `UInt`, and `SInt` types require an explicit bit-width specification during instantiation (e.g., `BitVector[8]`). Operations between types of different bit-widths are typically disallowed and require explicit casting.
fix
Always specify the bit-width `[N]` when defining hardware types. For operations with mixed widths, use casting methods like `.as_type(NewType)` or `.resize(N)`.
affects: all
gotcha`hwtypes` objects are immutable. Operations like addition or bitwise logic return new `hwtypes` instances rather than modifying the original object in place. This mirrors hardware behavior.
fix
Assign the result of any operation to a new variable (e.g., `c = a & b`). Do not expect in-place modification.
affects: all
Errors
Common errors & fixes
TypeError: Can't infer bit_width
Attempting to create a `BitVector`, `UInt`, or `SInt` instance without explicitly specifying its bit-width.
fix
Specify the bit-width using bracket notation: `BitVector[8](value)` or `UInt[16](value)`.
TypeError: Unsupported operand type(s) for +: 'BitVector[8]' and 'BitVector[16]'
Performing an operation between `hwtypes` objects that have different bit-widths.
fix
Ensure all operands have the same bit-width, or explicitly cast one to match the other's width, e.g., `bv8 + bv16.as_type(BitVector[8])`.
TypeError: BitVector() takes no arguments
Trying to instantiate a hardware type directly like a function call without the bit-width specification.
fix
Use the indexed type constructor with the desired bit-width, e.g., `BitVector[N](value)` instead of `BitVector(value)`.
Upgrade
Version history
1.4.7latest on PyPI · released Oct 14, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
38 hits · last 30 days
node
32
OpenAI (training)
1
Resources
hwtypes — pip install hwtypes · libregistry