Skip to main content

Module math

Module math 

Source
Expand description

Vectorized transcendentals at audio-grade precision.

Each *_block op mirrors the shape of crate::ops: takes a mutable output slice and a read-only input slice, writes element-wise. Lengths are taken to min(out, src). Scalar fallback is identical math; the SIMD path uses wide’s sleef-derived approximations.

Why these and not a full math library: every truce plugin converts dB to linear (gain, mix, send, peak) and most waveshapers want a fast tanh. The rest (exp2, log2) come along for free since they’re the building blocks of the first two. We do not ship the wider set (sin, cos, erf, lgamma); plugins that need those pull wide directly or use simdeez / sleef.

§Error bounds

  • db_to_linear_block, linear_to_db_block: < 0.1 dB across the audio range [-120, +24] dB. Round-trip through linear→dB→linear stays within 1 part in 10^6 (verified by the fuzz test).
  • exp2_block, log2_block: < 1 ULP for inputs in [-126, +127] (exp2) and [2^-126, 2^127] (log2). NaN / negative log2 inputs return NaN.
  • tanh_block: exp-identity form via wide::exp, < 5e-6 absolute error vs f32::tanh across [-10, +10]. Inputs outside that range clamp first; at |x| = 10, true tanh is already within 5e-9 of ±1.

Functions§

db_to_linear_block
out[i] = 10^(src[i] / 20). The dB → linear conversion every gain knob needs.
db_to_linear_block_scalar
Scalar fallback for db_to_linear_block. Kept pub so Criterion benches can hand the same inputs to the scalar and vector paths.
exp2_block
out[i] = 2^src[i]. The building block for exp and db_to_linear.
exp2_block_scalar
Scalar fallback for exp2_block.
linear_to_db_block
out[i] = 20 * log10(src[i]). Linear → dB. Returns -f32::INFINITY for zero input (matches f32::log10).
linear_to_db_block_scalar
Scalar fallback for linear_to_db_block.
log2_block
out[i] = log2(src[i]). Building block for log10 and linear_to_db.
log2_block_scalar
Scalar fallback for log2_block.
tanh_block
out[i] = tanh(src[i]). For soft-clipping waveshapers and any other DSP that wants a bounded sigmoid.
tanh_block_scalar
Scalar fallback for tanh_block. Uses libm’s tanh (full precision) rather than the Padé approximant; the SIMD path’s approximation is the cost of vector throughput, and the scalar path doesn’t pay that cost.