elem-simd-math 0.1.3

Vectorized elementwise math kernels for f32/f64 over stable-Rust SIMD (port of SLEEF via sleef-rs, on fearless_simd).
Documentation
#!/usr/bin/env python3
"""Generates the committed high-precision reference tables for `i0e` and `i1e`.

MPFR has no modified Bessel function of the first kind, so the oracle is mpmath:

    i0e(x) = exp(-|x|) * besseli(0, x)
    i1e(x) = exp(-|x|) * besseli(1, x)         (odd in x)

Run `python3 gen_bessel_reference.py` in this directory (needs only `mpmath`). Output is
deterministic: the sample points come from `random.Random` with a fixed seed, which Python
guarantees to be stable across versions, and the values are decimal strings with 40 significant
digits, computed at 60 digits of working precision and re-checked at 90.

Generated with: Python 3.12.3, mpmath 1.4.1.

Columns: `function,x_bits,value`. `x_bits` is the IEEE-754 binary64 pattern of the argument in hex
(for the f32 file, an exactly representable f32 widened to f64); `value` is the true result.
"""
import random
import struct
import sys

import mpmath as mp

SEED = 20261202
DIGITS = 40


def bits(x: float) -> str:
    return "%016x" % struct.unpack("<Q", struct.pack("<d", x))[0]


def f32(x: float) -> float:
    return struct.unpack("<f", struct.pack("<f", x))[0]


def truth(fn: str, x: float, dps: int) -> "mp.mpf":
    with mp.workdps(dps):
        ax = abs(mp.mpf(x))
        order = 0 if fn == "i0e" else 1
        v = mp.besseli(order, ax) * mp.exp(-ax)
        if order == 1 and x < 0:
            v = -v
        return v


def points(rng: random.Random, limit: float, narrow) -> list:
    pts = {0.0, 5e-324 if limit > 1e100 else 1.4e-45, 1e-300 if limit > 1e100 else 1e-30}
    # [0, 1], dense near 0 where i0e -> 1 and i1e ~ x/2.
    pts.update(rng.uniform(0, 1) for _ in range(400))
    pts.update(10 ** rng.uniform(-12, 0) for _ in range(300))
    # [1, 8]: the Chebyshev interval of the A series, and its boundary.
    pts.update(rng.uniform(1, 8) for _ in range(500))
    pts.update(rng.uniform(7.9, 8.1) for _ in range(100))
    pts.update({8.0, 7.999999999999999, 8.000000000000002})
    # (8, inf): the B series, log-spaced up to the largest finite argument.
    pts.update(rng.uniform(8, 64) for _ in range(300))
    pts.update(10 ** rng.uniform(1, mp.log10(limit)) for _ in range(500))
    pts.update({limit, limit / 2, 700.0, 709.0, 710.0, 713.0, 714.0, 745.0, 1e4, 1e8})
    out = set()
    for p in pts:
        p = narrow(p)
        if p <= limit:
            out.add(p)
            out.add(-p)
    return sorted(out)


def main() -> None:
    rng = random.Random(SEED)
    for name, limit, narrow in (
        ("bessel_ref_f64.csv", 1.7976931348623157e308, float),
        ("bessel_ref_f32.csv", 3.4028234663852886e38, f32),
    ):
        xs = points(rng, limit, narrow)
        rows = []
        for fn in ("i0e", "i1e"):
            for x in xs:
                v = truth(fn, x, 60)
                if abs(x) in (0.0,) or len(rows) % 53 == 0:
                    v2 = truth(fn, x, 90)
                    assert v == 0 and v2 == 0 or abs(v - v2) <= abs(v2) * mp.mpf(10) ** -45, (fn, x)
                rows.append("%s,%s,%s" % (fn, bits(x), mp.nstr(v, DIGITS, min_fixed=1, max_fixed=0)))
        with open(name, "w") as f:
            f.write("function,x_bits,value\n")
            f.write("\n".join(rows) + "\n")
        print(name, len(rows), "rows", file=sys.stderr)


if __name__ == "__main__":
    main()