Skip to main content

PyPI Version Python Build Documentation Coverage Status python-versions semantic-versioning

PRs welcome Issues

pyfxp

Bit-true fixed-point arithmetic for Python, at compiled speed.

Model the datapath of your FPGA, ASIC or DSP firmware in Python. Pick the word length, the rounding and the overflow behavior, and get exactly the numbers the hardware will produce: on plain floats and NumPy arrays, from Python or inside Numba @njit loops.

Documentation · Getting started · API reference · Comparison · Changelog · llms.txt

Installation

pip install pyfxp

A fixed-point FIR filter, end to end

import numpy as np
from numba import njit
from pyfxp import Q, fxp, to_hex
from pyfxp.constants import HALF_EVEN, SAT

x_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT)   # ADC samples:   sQ1.15
h_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT)   # filter taps:   sQ1.15
acc    = x_spec.mul(h_spec).acc(32)         # accumulator:   sQ7.30, can't overflow
y_spec = Q(1, 15, rnd=HALF_EVEN, ovf=SAT)   # output register

@njit                                       # pyfxp runs inside compiled loops
def fir(x, h, acc, y_spec):
    y = np.zeros_like(x)
    for n in range(h.size - 1, x.size):
        s = 0.0
        for k in range(h.size):
            s = fxp(s + x[n - k] * h[k], acc)
        y[n] = fxp(s, y_spec)
    return y

h = fxp(np.hanning(32) / np.hanning(32).sum(), h_spec)
x = fxp(0.9 * np.sin(np.linspace(0, 20, 10_000)), x_spec)
y = fir(x, h, acc, y_spec)

to_hex(y, y_spec)                           # expected values for your RTL testbench

Why pyfxp

  • Bit-true. Nine rounding modes, wrap / saturate / error on overflow, formats up to 64 bits. Every mode is property-tested against an exact rational-arithmetic reference.
  • Fast where it counts. About 5 ns per sample inside @njit loops, and as fast as the fastest library on NumPy arrays. The only Python fixed-point library that works inside Numba.
  • Plain floats and NumPy. No custom number type: results are floats and float64 arrays, so SciPy, Matplotlib and the model you already have keep working.
  • Hardware-ready. to_int, to_hex and to_bin give the stored integers and two's-complement patterns for $readmemh test vectors, exact at every width.
  • Bit growth, computed. a.mul(b), a.add(b) and a.acc(n) return the smallest format that holds the exact result, so intermediate values never overflow by accident.
  • Readable formats. Q(3, 13, True, HALF_EVEN, SAT) prints as sQ3.13(HALF_EVEN, SAT), and swapping the rounding and overflow arguments is an error, not a silent bug.

Performance

Quantizing to sQ8.12 with floor rounding and saturation, compared with the other Python fixed-point libraries (log scale, lower is better):

Bar chart. Array of one million values, time per value: pyfxp 5 ns, numfi 5 ns, APyTypes 32 ns, fxpmath 396 ns, spfpm 4.2 µs, fixedpoint 9.3 µs. One value at a time: pyfxp inside @njit 5 ns, APyTypes 387 ns, pyfxp from Python 1 µs, spfpm 3.8 µs, fixedpoint 9.2 µs, numfi 19 µs, fxpmath 85 µs.

On arrays, pyfxp and numfi are the fastest, within run-to-run noise of each other. The difference that matters for DSP models is per-sample code: inside an @njit loop pyfxp takes about 5 ns per value, while the other libraries can only be called from Python, at 0.4 µs or more per call.

pyfxp numfi APyTypes fxpmath fixedpoint spfpm
Result is a plain float / NumPy array ✔ — — — — —
Vectorized over NumPy arrays ✔ ✔ ✔ ✔ — —
Quantize inside Numba @njit code ✔ — — — — —
Formats wider than 64 bits — ✔ ✔ ✔ ✔ ✔

See the full comparison for the method, the numbers and the details.

Quick Start

pyfxp offers two ways to convert numbers to a fixed-point format:

  • fxpt → “typed” (parameter-based) Pass the format directly as function parameters.
  • fxp → “spec-based” (explicit FxpSpec) Build a reusable spec with Q(...), then pass that spec.

Use the fxpt function when the format is simple or used in a single place:

from pyfxp import fxpt
from pyfxp.constants import SAT, HALF_EVEN  # overflow & rounding modes

y = fxpt(
    x=3.14159,
    qi=3,            # integer bits (including sign when signed=True)
    qf=13,           # fractional bits
    signed=True,     # signed or unsigned format
    ovf=SAT,         # overflow behavior: WRAP | SAT | ERROR
    rnd=HALF_EVEN,   # rounding: TRUNC, CEIL, TO_ZERO, AWAY, HALF_UP, ...
)

Use fxp when you want a reusable, readable definition. The fixed-point format is defined using the Q(...) function once, and can be reused for different fxp calls:

import numpy as np
from pyfxp import fxp, Q
from pyfxp.constants import SAT, HALF_EVEN

# Define the format once
Q3_13 = Q(qi=3, qf=13, signed=True, ovf=SAT, rnd=HALF_EVEN)

# Reuse it across your codebase
y1 = fxp(3.14159, Q3_13)
y2 = fxp(np.array([0.1, 0.2, 0.3]), Q3_13)

Q(...) returns an FxpSpec instance which is basically a named tuple containing the specs used by fxp.

When specifying the fixed-point specs using either fxpt or Q, all the following rounding and overflow modes are supported:

Rounding modes:

Constant Description
TRUNC Bit truncation (rounds toward negative infinity)
CEIL Round toward positive infinity
TO_ZERO Round toward zero
AWAY Round away from zero
HALF_UP Round to nearest; ties toward positive infinity
HALF_DOWN Round to nearest; ties toward negative infinity
HALF_EVEN Round to nearest; ties to even
HALF_ZERO Round to nearest; ties toward zero
HALF_AWAY Round to nearest; ties away from zero

Overflow modes:

Constant Description
WRAP Wrap around on overflow (modular arithmetic)
SAT Saturate at the maximum/minimum representable value
ERROR Raise an exception on overflow

The constants above are defined in the pyfxp.constants module. They are used by both fxpt and fxp to specify fixed-point arithmetic behavior.

Metadata

Release files for pyfxp 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyfxp 1.0.0
File Size Uploaded Provenance Actions
pyfxp-1.0.0.tar.gz 72.5 kB Without provenance Details for pyfxp-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyfxp 1.0.0
File Interpreter ABI Platform Actions
pyfxp-1.0.0-py3-none-any.whl Python 3 none any Details for pyfxp-1.0.0-py3-none-any.whl

Total release size: 94.1 kB

Release files / pyfxp-1.0.0.tar.gz

Download URL pyfxp-1.0.0.tar.gz
Size 72.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5299dedd888776d29730c80739f40090637c586565cd17531e59f1c77193eca8
BLAKE2b-256 checksum
How to use checksums
ed0c9390374e0696ce75b41147197eaee5fd0d79b4680008c678fc65b1f3cc5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.7 CPython/3.15.2 Linux/7.0.0-1019-azure

Release files / pyfxp-1.0.0-py3-none-any.whl

Download URL pyfxp-1.0.0-py3-none-any.whl
Size 21.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a382b08d16c768172f32bb0359a092e3ae7f0cd72119ed19ea31387e8cf517f
BLAKE2b-256 checksum
How to use checksums
b4991bc59aead8fdb6db9bcbf6b03ed966313f34210b73155d2aadcc14ac5e9c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.7 CPython/3.15.2 Linux/7.0.0-1019-azure

Security for pyfxp 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page