fmath

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: LGPL-2.1 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const MaxFloat32 = math.MaxFloat32

MaxFloat32 mirrors C FLT_MAX.

Variables

This section is empty.

Functions

func Absf

func Absf(x float32) float32

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

func Atan32(x float32) float32

Atan32 is float32 arctangent, replacing C atanf. Computed in float64 and rounded once to float32.

func BesselI0

func BesselI0(x float64) float64

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

func Cbrt32(x float32) float32

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 Ceil32

func Ceil32(x float32) float32

Ceil32 is float32 ceiling, replacing C ceilf.

func Clipf

func Clipf(v, lo, hi float32) float32

Clipf clamps v to the inclusive range [lo, hi] for float32 operands.

func Clipi

func Clipi(v, lo, hi int) int

Clipi clamps v to the inclusive range [lo, hi] for int operands.

func Cos

func Cos(x float64) float64

Cos is float64 cosine with an architecture-deterministic implementation (see portable_sincos.go), replacing math.Cos in the encoder.

func Exp

func Exp(x float64) float64

Exp is a float64 pass-through for init-time code (psy ATH curve).

func Exp2

func Exp2(x float64) float64

Exp2 is a float64 pass-through for init-time code (psy min_snr).

func Exp10

func Exp10(x float64) float64

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

func Exp32(x float32) float32

Exp32 is float32 natural exponential, replacing C expf. Computed in float64 and rounded once to float32.

func Exp232

func Exp232(x float32) float32

Exp232 is float32 base-2 exponential, replacing C exp2f. Computed in float64 and rounded once to float32.

func Inf32

func Inf32() float32

Inf32 is the positive float32 infinity.

func Log32

func Log32(x float32) float32

Log32 is float32 natural logarithm, replacing C logf. Computed in float64 and rounded once to float32.

func Log232

func Log232(x float32) float32

Log232 is float32 base-2 logarithm, replacing C log2f. Computed in float64 and rounded once to float32.

func NaN32

func NaN32() float32

NaN32 is a float32 quiet NaN, replacing C NAN.

func Pow

func Pow(x, y float64) float64

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

func Pow32(x, y float32) float32

Pow32 is float32 power, replacing C powf. Computed in float64 and rounded once to float32.

func Round32

func Round32(x float32) float32

Round32 is float32 round-half-away-from-zero, replacing C roundf.

func Sin

func Sin(x float64) float64

Sin is float64 sine with an architecture-deterministic implementation (see portable_sincos.go), replacing math.Sin in the encoder.

func Sqrt32

func Sqrt32(x float32) float32

Sqrt32 is float32 square root.

func Sqrt64

func Sqrt64(x float64) float64

Sqrt64 is float64 square root, replacing C double sqrt where the C promotes float operands (the intensity stereo downmix scale).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL