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
//! `i0e`, `i1e` and `xlogy` over native-width `f64` vectors.
//!
//! `i0e` and `i1e` are ported from SciPy's [xsf] (BSD-3-Clause) commit
//! `39019a470a208221d55a587f0baa329c3d1054c5`, files `include/xsf/cephes/i0.h`,
//! `include/xsf/cephes/i1.h` and `include/xsf/cephes/chbevl.h` (Cephes, Stephen L. Moshier).
//! `xlogy` follows `include/xsf/log.h` and takes its logarithm from [`u10::log`]. The
//! coefficient tables are in [`crate::bessel`], which also holds the scalar reference.
//!
//! [xsf]: https://github.com/scipy/xsf
//!
//! # Accuracy
//!
//! These are not SLEEF tiers. The bounds are what the kernels reach against a high-precision
//! oracle (MPFR for `xlogy`, mpmath for `i0e`/`i1e`; see `tests/special.rs` and
//! `tests/data/`), asserted there with a margin:
//!
//! | function | bound | reference |
//! |---|---|---|
//! | [`i0e`] | 6 ULP (5.2 measured) | mpmath `besseli(0, x) * exp(-x)` |
//! | [`i1e`] | 8 ULP (6.9 measured) | mpmath `besseli(1, x) * exp(-x)` |
//! | [`xlogy`] | 1.5 ULP | exact `x * ln(y)` (MPFR) |
//!
//! The Chebyshev series are those of Cephes, whose own tables report `5.4e-16` (`i0e`) and
//! `2.0e-15` (`i1e`) peak relative error; the bound is a property of the series, not of the
//! vectorization.
//!
//! # Determinism
//!
//! All three are bit-identical on every SIMD level (`mul_add_precise` rounds once everywhere).
//! `i0e` and `i1e` are additionally bit-identical to the scalar reference in
//! [`crate::bessel`]; `xlogy` is not, because the scalar reference calls the platform `ln`.

use super::*;

/// `exp(-|x|) I0(x)`, exponentially scaled modified Bessel function of order zero.
///
/// The error bound of the returned value is `6 ULP`. Defined and finite for every `x`; it
/// never overflows, which is what the scaled form is for (`i0e(1e300) = 3.99e-151`, while
/// `I0(1e300)` is not representable). `i0e(+-inf) = 0`, `i0e(NaN) = NaN`.
#[inline(always)]
pub fn i0e<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
    let ax = x.abs();
    let small = chbevl(
        simd,
        mul_sub(simd, ax, splat(simd, 0.5), splat(simd, 2.)),
        &crate::bessel::I0_A,
    );
    let large = chbevl(simd, scaled_arg(simd, ax), &crate::bessel::I0_B) / ax.sqrt();
    ax.simd_le(splat(simd, 8.)).select(small, large)
}

/// `exp(-|x|) I1(x)`, exponentially scaled modified Bessel function of order one.
///
/// The error bound of the returned value is `8 ULP`. Odd in `x`, finite for every `x`,
/// `i1e(+-inf) = +-0`, `i1e(NaN) = NaN`.
#[inline(always)]
pub fn i1e<S: Simd>(simd: S, x: F64s<S>) -> F64s<S> {
    let ax = x.abs();
    let small = chbevl(
        simd,
        mul_sub(simd, ax, splat(simd, 0.5), splat(simd, 2.)),
        &crate::bessel::I1_A,
    ) * ax;
    let large = chbevl(simd, scaled_arg(simd, ax), &crate::bessel::I1_B) / ax.sqrt();
    ax.simd_le(splat(simd, 8.)).select(small, large).copysign(x)
}

/// `x * ln(y)`, defined as `0` when `x == 0` and `y` is not NaN.
///
/// The error bound of the returned value is `1.5 ULP`: the 1.0 ULP logarithm and one
/// multiplication. `xlogy(0, 0) = xlogy(0, inf) = 0`, `xlogy(0, NaN) = NaN`,
/// `xlogy(x > 0, 0) = -inf`, `xlogy(x < 0, 0) = +inf`, `xlogy(x, y < 0) = NaN`.
#[inline(always)]
pub fn xlogy<S: Simd>(simd: S, x: F64s<S>, y: F64s<S>) -> F64s<S> {
    let product = x * u10::log(simd, y);
    let zero = splat(simd, 0.);
    let defined_zero = x.simd_eq(zero) & y.simd_eq(y);
    defined_zero.select(zero, product)
}

/// `32 / x - 2`, the Chebyshev argument of the `[8, inf)` series.
#[inline(always)]
fn scaled_arg<S: Simd>(simd: S, ax: F64s<S>) -> F64s<S> {
    splat(simd, 32.) / ax - splat(simd, 2.)
}

/// Clenshaw recurrence of Cephes `chbevl`, the same operation order as
/// [`crate::bessel::chbevl`].
#[inline(always)]
fn chbevl<S: Simd>(simd: S, x: F64s<S>, coeffs: &[f64]) -> F64s<S> {
    let mut b0 = splat(simd, coeffs[0]);
    let mut b1 = splat(simd, 0.);
    let mut b2 = splat(simd, 0.);
    for &c in &coeffs[1..] {
        b2 = b1;
        b1 = b0;
        b0 = mul_sub(simd, x, b1, b2) + splat(simd, c);
    }
    splat(simd, 0.5) * (b0 - b2)
}