Skip to main content

crfix: correctly rounded fixed-point functions for Python

crfix.sigmoid(x, fin, fout, bits): for every integer x, the exact sigmoid of x·2^−fin rounded once to an integer with fout fractional bits, in the rounding mode you ask for, then saturated to bits. The same goes for exp, exp2, log, log2, tanh, sqrt, rsqrt, the reciprocal, division, SiLU and GELU (the exact one, v·Φ(v)). A container is 8, 16 or 32 bits, or "u8" and "u16", unsigned (TFLite's uint8). Every result is the one an exact computation followed by one rounding would give, so a lookup table or a fixed-point kernel built from it has no error beyond its format.

import numpy as np, crfix
x = np.arange(-128, 128, dtype=np.int8)
y = crfix.sigmoid(x, fin=4, fout=7, bits=8)              # sigmoid(x / 16) as round(sigmoid * 128), int8
t = crfix.table("tanh", fin=5, fout=7, bits=8)            # an int8 kernel's lookup table: t[x + 128]
q = crfix.div(a, 8, b, 8, fout=12, bits=16, mode="down")  # (a / 256) / (b / 256), rounded down

Layer normalization over the last axis, exact, with γ, β and ε in fixed point:

y = crfix.layernorm(x, fin=3, fout=5, eps=1, fe=10, gamma=g, fg=7, beta=b, fb=4, bits=8)

TFLite's affine formats, where a value is scale·(q − zero_point), for any scales:

inp, out = crfix.Affine(0.0478, 3), crfix.Affine(1 / 256, -128)
t = crfix.table_affine("sigmoid", inp, out)               # a LOGISTIC kernel's table, every entry correctly rounded
p = crfix.softmax(logits, c=beta * 0.0478, out=out)       # the exact softmax of c·logits, rounded into out

A lookup table as C source, CSV or a list, from the command line (--f32 rounds decimal scales to float32 first, as a TFLite model stores them; hex floats such as 0x1p-8 are taken exactly):

python -m crfix table tanh --fin 5 --fout 7 --bits 8 --format c
python -m crfix table-affine sigmoid --in 0.0478,3 --out 0x1p-8,-128 --f32 --format c --name logistic
  • Modes: "nearest" (ties to even), "up", "down" and "zero". In an affine format, "zero" rounds the value itself toward 0, before the zero point is added.
  • Domain edges: log of a value ≤ 0 gives the most negative result. sqrt of a negative value gives 0. rsqrt of a value ≤ 0, and the reciprocal of 0, give the largest. a/0 goes by a's sign, and 0/0 gives 0.
  • Integer arithmetic only: the library has no floating point, and gives the same bits on x86-64, ARM64, 32-bit ARM and a Cortex-M0.
  • How it is proven: every 16-bit input, in every format, mode and container, is checked against MPFR. Results that the fast 64-bit enclosures cannot decide go to interval arithmetic that is correct by construction. This package's own tests use a second, independent reference: Python's decimal and exact fractions.

pip install crfix brings a wheel with the library, libcrfix.so.0 (or libcrfix.0.dylib on macOS); CRFIX_LIBRARY names another. The methods and their checks are in crnumerics' fix/README.md.

Metadata

Release files for crfix 0.9.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 crfix 0.9.0
File Size Uploaded
crfix-0.9.0.tar.gz 16.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for crfix 0.9.0
File Interpreter ABI Platform
crfix-0.9.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details
crfix-0.9.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl Python 3 none Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details
crfix-0.9.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details

Total release size: 140.9 kB

Release files / crfix-0.9.0.tar.gz

Download URL crfix-0.9.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
18caa2622dcb6ea0fbd59af7c4f683c9ab6ad05fa95bf0d9eae39c11ab75591a
BLAKE2b-256 checksum
How to use checksums
3105ced3dc47a1ef69ad314c73637e4a9d0ed24c81e52e58b5e91e7668a87b15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / crfix-0.9.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL crfix-0.9.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 42.4 kB
Tags Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
35a425fb7be457d2b4609d5c1a964d659ee44604a61c59692124ea10b535206d
BLAKE2b-256 checksum
How to use checksums
2cb03c134e20bc57b7175645609d05ad29b9a8a98bad18884c8e065efe1e1858
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / crfix-0.9.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL crfix-0.9.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 42.4 kB
Tags Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
e4ef70a6d1c0de74f0615a090c37a1adbfd0f57b6e47f988447addb684a4205a
BLAKE2b-256 checksum
How to use checksums
925e8cc2d97dbe540ea3cb51bdab61936772fa03e5ad3e140831da1ed8477886
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / crfix-0.9.0-py3-none-macosx_11_0_arm64.whl

Download URL crfix-0.9.0-py3-none-macosx_11_0_arm64.whl
Size 39.7 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3d26a398b370b256e47b4f66ebc3ecbce409049cd690852c433082d96dda1ef5
BLAKE2b-256 checksum
How to use checksums
a20f4087f20d80c88289f5488b390d3c0c6f05fc016fedf2b143f1052f5698cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.9.0 This release

4 release files

0.8.0

4 release files

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