Documentation
¶
Overview ¶
Package fmath centralizes the math primitives used by encoder code paths (docs/go-design.md: only this package imports math in per-frame code). It also holds shared scalar helpers (Absf, Clipf, Clipi) that use only Go builtins and add no math import, so the package fence stays intact.
The transcendentals (Exp/Exp2/Log/Log2/Pow/Atan/Sin/Cos and their float32 wrappers) are backed by architecture-deterministic implementations vendored from the Go standard library in portable_*.go; see PROVENANCE.md ("Go standard library derivations") and issue #79. Sqrt/Ceil/Round stay on package math and are architecture-identical (Sqrt is a single correctly-rounded hardware instruction). Cbrt also stays on math; it does fuse into an FMA on arm64, but the encoder reaches it only through Cbrt32 to build the init-time Pow43 dequant table (internal/tables), and its sub-ULP arm64 effect is absorbed by that table's float32 entries. That absorption is not proven by this FMA guard; it is backstopped end to end by TestEncoderArchDeterminism, which CI runs on both arm64 and amd64.
Index ¶
- Constants
- func Absf(x float32) float32
- func Atan32(x float32) float32
- func BesselI0(x float64) float64
- func Cbrt32(x float32) float32
- func Ceil32(x float32) float32
- func Clipf(v, lo, hi float32) float32
- func Clipi(v, lo, hi int) int
- func Cos(x float64) float64
- func Exp(x float64) float64
- func Exp2(x float64) float64
- func Exp10(x float64) float64
- func Exp32(x float32) float32
- func Exp232(x float32) float32
- func Inf32() float32
- func Log32(x float32) float32
- func Log232(x float32) float32
- func NaN32() float32
- func Pow(x, y float64) float64
- func Pow32(x, y float32) float32
- func Round32(x float32) float32
- func Sin(x float64) float64
- func Sqrt32(x float32) float32
- func Sqrt64(x float64) float64
Constants ¶
const MaxFloat32 = math.MaxFloat32
MaxFloat32 mirrors C FLT_MAX.
Variables ¶
This section is empty.
Functions ¶
func Absf ¶
Absf is the float32 absolute value, expressed as max(x, -x) so it inlines with no math import. It propagates NaN: max(NaN, -NaN) is NaN, so Absf(NaN) is NaN. It also normalizes -0.0 to +0.0.
func Atan32 ¶
Atan32 is float32 arctangent, replacing C atanf. Computed in float64 and rounded once to float32.
func BesselI0 ¶
BesselI0 is the zeroth-order modified Bessel function of the first kind, power-series form. Replaces libavutil/mathematics.c:av_bessel_i0's minimax rational approximation @ d09d5afc3a. For the KBD window argument range (x <= 13) the two agree to about 1e-13, far below float32 window resolution, so generated windows match FFmpeg's at float32. The series terminates on term < 1e-17*sum, a comparison that is false for NaN and for Inf/Inf, so the guards below are load-bearing: without them a NaN or overflowing argument spins forever. I0 overflows float64 near x = 713, and I0 is even, so folding the sign and saturating above 700 is exact for every argument the caller can reach. KBD window generation passes x <= 13, so none of these paths are taken in practice.
BesselI0 feeds both the encoder's float32 KBD window (internal/window.KBD) and the decoder's int32 KBDFixed (internal/window/fixed.go). The float64 product below is barriered so the sum accumulation cannot contract to an FMADDD on arm64; the barrier is value-neutral on amd64 and its arm64 effect rounds away in both the float32 and the int32 quantization, so both tables stay byte-identical (encoder pinned by TestWindowTablesGolden, decoder by internal/dec's TestIMDCTDump).
func Cbrt32 ¶
Cbrt32 is float32 cube root, replacing C cbrtf. Computed in float64 and rounded once to float32; may differ from cbrtf by 1 ulp, which only feeds distortion costs, never emitted bits.
func Cos ¶
Cos is float64 cosine with an architecture-deterministic implementation (see portable_sincos.go), replacing math.Cos in the encoder.
func Exp10 ¶
Exp10 computes 10**x exactly like libavutil/ffmath.h:ff_exp10 @ d09d5afc3a: exp2(M_LOG2_10 * x). Used by the psy model init.
func Exp32 ¶
Exp32 is float32 natural exponential, replacing C expf. Computed in float64 and rounded once to float32.
func Exp232 ¶
Exp232 is float32 base-2 exponential, replacing C exp2f. Computed in float64 and rounded once to float32.
func Log32 ¶
Log32 is float32 natural logarithm, replacing C logf. Computed in float64 and rounded once to float32.
func Log232 ¶
Log232 is float32 base-2 logarithm, replacing C log2f. Computed in float64 and rounded once to float32.
func Pow ¶
Pow is a float64 pass-through for init-time code outside the depguard math fence (the psy ATH curve runs once per encoder init).
func Pow32 ¶
Pow32 is float32 power, replacing C powf. Computed in float64 and rounded once to float32.
Types ¶
This section is empty.