Getting started¶
Installation¶
pip install pyfxp
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, ...
)
Note
ARM-style Q-format
In ARM-style Q-format notation (used by pyfxp), the sign bit is included in the integer bit count qi.
Refer to Q (number format) for more information.
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 |
|---|---|
|
Bit truncation (rounds toward negative infinity) |
|
Round toward positive infinity |
|
Round toward zero |
|
Round away from zero |
|
Round to nearest; ties toward positive infinity |
|
Round to nearest; ties toward negative infinity |
|
Round to nearest; ties to even |
|
Round to nearest; ties toward zero |
|
Round to nearest; ties away from zero |
Overflow modes:
Constant |
Description |
|---|---|
|
Wrap around on overflow (modular arithmetic) |
|
Saturate at the maximum/minimum representable value |
|
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.
Tip
Usage example
The modes are IntEnum members: they print by name (<Rounding.HALF_EVEN: 6>) but are plain integers everywhere else,
so the numeric codes (rnd=6) keep working and the modes can be used inside @njit code.
Q(...) converts integer codes to members, and passing a Rounding member as ovf (or the reverse) raises a TypeError.
from pyfxp.constants import SAT, HALF_EVEN
fxpt(1.25, qi=3, qf=13, ovf=SAT, rnd=HALF_EVEN)
Call pyfxp on arrays or inside @njit code¶
Each call to fxp or fxpt from Python costs about 1 µs, almost all of it call overhead, so a
Python loop that quantizes one sample per call runs at about 1 µs per sample. Do the same work in
one of these two ways instead, at 5–15 ns per sample:
On a whole array, when the samples don’t depend on each other:
import numpy as np from pyfxp import fxp, Q Q3_13 = Q(3, 13) x = np.random.default_rng(0).uniform(-1, 1, 1_000_000) y = fxp(x, Q3_13) # one call: ~5 ms # y = [fxp(v, Q3_13) for v in x] # one call per sample: ~1 s
Inside an
@njitfunction, when each sample depends on the previous ones (filters with feedback, accumulators, control loops):from numba import njit ACC = Q(8, 13) @njit def integrate(x, spec): y = np.empty_like(x) acc = 0.0 for i in range(x.size): acc = fxp(acc + x[i], spec) # compiled: ~15 ns per sample y[i] = acc return y y = integrate(fxp(x, Q3_13), ACC)
The @njit loop measures ~15 ns per sample rather than ~5 because each step waits for the previous
result. Pass the spec into the compiled function as an argument, as above, or define it as a global
constant.