opusmath

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: BSD-3-Clause Imports: 1 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 opusmath holds the gopus floating-point math kernels shared by the CELT, SILK and Opus glue layers. Every function here is a deliberate, narrow mirror of a specific libopus 1.6.1 routine or macro from the default (non-FIXED_POINT) build, kept in one place so call sites stay bit-exact with the reference decoder/encoder.

Width discipline is the whole point of the package:

  • The CELT FLOAT_APPROX approximations (celt_log2, celt_exp2) are reproduced as float32 polynomial evaluations with the exact libopus coefficients and the same bit-twiddling on the IEEE-754 exponent. Doing the arithmetic in float64 would change the rounding and break parity, so these stay in float32 and route their fused multiply-add through fma32 (see fma32_*.go).
  • The thin SILK/Opus FLP wrappers (in silk.go) wrap libm functions that C evaluates in double and narrows once on assignment to a float. They keep the computation in float64 and convert the single final result, matching that one rounding step. Operands that libopus deliberately keeps in C double use the CReal alias.
  • The PCM quantizers (in pcm.go) reproduce libopus' float-to-int rounding and saturation exactly, including round-half-to-even and the rail clamps.

The package is import-safe from both the default and the gopus_fixed_point builds; it contains no build-tagged variants of its own except the fma32 helper, which is split per architecture only to control FMA contraction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcosF32

func AcosF32(x float32) float32

AcosF32 mirrors C acos() narrowed to float.

func AtanF32

func AtanF32(x float32) float32

AtanF32 mirrors C atan() narrowed to float, used by the CELT stereo-angle estimator.

func CELTCosNormF32

func CELTCosNormF32(x float32) float32

CELTCosNormF32 matches libopus celt/mathops.h celt_cos_norm() in the floating-point build: (float)cos((.5f*PI)*x). CELT exp_rotation() in celt/vq.c relies on that C double cos() before narrowing back to float, so the half-pi product is formed and evaluated in double here too.

func CeltExp2

func CeltExp2(x float32) float32

CeltExp2 mirrors libopus celt_exp2() on the FLOAT_APPROX path (celt/mathops.h). It splits x into integer and fractional parts, evaluates a degree-5 polynomial in the fraction via fma32 (so the fused multiply-adds match the reference), then adds the integer part straight into the IEEE-754 exponent field and masks off the sign bit. Inputs below -50 underflow to 0, exactly as libopus short-circuits. The float32 width and the fma32 fusion are load-bearing for bit-exactness.

func CeltLog2

func CeltLog2(x float32) float32

CeltLog2 mirrors libopus celt_log2() on the FLOAT_APPROX path (celt/mathops.h). It decomposes x into an IEEE-754 exponent and mantissa, selects one of eight piecewise ranges from the mantissa's top bits, applies a degree-4 minimax polynomial to the range-normalised mantissa, and recombines with the per-range offset. All arithmetic is float32 with the exact libopus coefficients; widening to float64 would change the rounding and lose parity.

func CeltSinNormArg

func CeltSinNormArg(x float32) float32

CeltSinNormArg reproduces the argument reduction inside libopus celt_sin() (celt/mathops.h): it forms (pi/2)*x - 1 with the half-pi product evaluated in double (as the C code does) before narrowing to float, so the reduced argument fed to the sine polynomial is bit-exact.

func CosF32

func CosF32(x float32) float32

CosF32 mirrors C cos() narrowed to float.

func Exp2DivIntF32

func Exp2DivIntF32(x float32, denom int) float32

Exp2DivIntF32 returns (2**x)/denom as a float, matching the libopus FLP expression exp2(x)/N. The division is carried out in double (denom widened to float64) before the single narrowing to float, so the quotient rounds exactly as the C code does.

func Exp2F32

func Exp2F32(x float32) float32

Exp2F32 mirrors C exp2() narrowed to float (2**x), rounded to float exactly once, as in libopus.

func ExpF32

func ExpF32(x float32) float32

ExpF32 mirrors the C exp() used in the SILK FLP paths (e**x). libopus computes it in double and narrows once on assignment to a float; doing the math in float64 and converting the single final result reproduces that rounding.

func Float32ToInt16

func Float32ToInt16(x float32) int16

Float32ToInt16 converts a normalised float sample in roughly [-1, 1) to signed 16-bit PCM, matching libopus FLOAT2INT16() (celt/float_cast.h): scale by 32768 then round-half-to-even and saturate to the int16 rails. The scale is applied in float32 first (as the C macro does) before the rounding/clamp in Float32ToInt16Raw, so the result is bit-exact with the reference.

func Float32ToInt16OSCEOutputScale

func Float32ToInt16OSCEOutputScale(x float32) int16

Float32ToInt16OSCEOutputScale mirrors libopus OSCE SCALE_OUTPUT quantization. Unlike the generic PCM helper it clamps the negative rail to -32767 (a symmetric +/-32767 range), matching the OSCE output scaling in the DNN post-filters; the scale-by-32768 and round-half-to-even are otherwise the same as Float32ToInt16.

func Float32ToInt16Raw

func Float32ToInt16Raw(y float32) int16

Float32ToInt16Raw rounds and saturates an already-scaled value (i.e. one in 16-bit code units, not normalised [-1, 1)) to int16. It is the inner half of FLOAT2INT16 and of silk_float2short(): clamp to [-32768, 32767] then round-half-to-even. The full negative rail (-32768) is allowed here, unlike the OSCE variant. Comparisons are done in float32 to match the C order of operations.

func Float32ToInt24

func Float32ToInt24(x float32) int32

Float32ToInt24 converts a float32 PCM sample to a signed 24-bit integer stored in int32, matching libopus RES2INT24 for float builds:

RES2INT24(a) = float2int(32768.f * 256.f * (a))  (celt/arch.h)

Full scale is ±8388608 (= 2^23). No explicit saturation is applied; libopus does not soft-clip before this conversion in the 24-bit path.

func FloorCRealToInt32

func FloorCRealToInt32(x CReal) int32

FloorCRealToInt32 truncates a C-double value toward negative infinity to int32.

func FloorF32ToInt32

func FloorF32ToInt32(x float32) int32

FloorF32ToInt32 truncates toward negative infinity, the C (opus_int32)floor(x) idiom used across the SILK FLP code. The floor is taken in double before narrowing so the result is bit-exact with libopus.

func FloorHalfPlusF32ToInt32

func FloorHalfPlusF32ToInt32(x float32) int32

FloorHalfPlusF32ToInt32 returns floor(x + 0.5) as int32, the plain round-half-up form (toward +inf) libopus uses where ties on negatives are not special-cased; contrast with RoundF32HalfAwayFromZeroToInt32. The bias and floor are taken in double before narrowing.

func ISqrt32

func ISqrt32(x uint32) uint32

ISqrt32 returns floor(sqrt(x)), matching libopus isqrt32() (celt/mathops.c). It seeds the result from a double sqrt and then corrects up or down with exact 64-bit integer multiplies so the floor is exact even where the double sqrt rounds the wrong side of an integer boundary. The 64-bit (uint64) products avoid overflow for x near 2**32, which the correction loops require.

func Log2F32

func Log2F32(x float32) float32

Log2F32 mirrors C log2() narrowed to float.

func Log10F32

func Log10F32(x float32) float32

Log10F32 mirrors C log10() narrowed to float.

func LogF32

func LogF32(x float32) float32

LogF32 mirrors C log() (natural log) narrowed to float.

func PCMSoftClip

func PCMSoftClip(x []float32, n, channels int, declipMem []float32)

PCMSoftClip applies libopus opus_pcm_soft_clip() (src/opus.c) in place to n interleaved frames of the given channel count. It expects samples in roughly [-1, 1] and smoothly attenuates excursions past +/-1 (hard-limiting only beyond +/-2) instead of clipping, carrying the per-channel adaptation coefficient across calls in declipMem (one entry per channel, the C float declip_mem[]).

The whole routine runs in float32 to match the reference: the quadratic soft-knee (v + a*v*v), the peak search, and the coefficient update a = (maxval-1)/(maxval*maxval) plus the 2.4e-7 nudge are all single precision, as is the boundary-continuity ramp. If every sample is already within [-1, 1] and all declipMem entries are zero, it returns without modifying x, exactly as libopus short-circuits.

func Pow10F32

func Pow10F32(x float32) float32

Pow10F32 mirrors C pow(10, x) narrowed to float, the 10**x form libopus uses for decibel conversions in the FLP gain code.

func RoundF32HalfAwayFromZeroToInt32

func RoundF32HalfAwayFromZeroToInt32(x float32) int32

RoundF32HalfAwayFromZeroToInt32 rounds x to the nearest integer with ties going away from zero. It matches libopus silk_float2int() (silk/float/SigProc_FLP.h), i.e. (opus_int32)floor(x + 0.5) for non-negative x and the symmetric ceil(x - 0.5) for negatives. The +/-0.5 bias and the floor/ceil run in double so the rounding boundary is exact before the int32 truncation.

func RoundToEvenF32ToInt32

func RoundToEvenF32ToInt32(x float32) int32

RoundToEvenF32ToInt32 rounds to the nearest integer with ties to even (banker's rounding), matching the C rint()/nearbyint() default rounding mode libopus assumes where round-to-nearest-even is intended.

func SilkLog2F32

func SilkLog2F32(x float32) float32

SilkLog2F32 matches silk/float/SigProc_FLP.h silk_log2(): log2(x) computed as 3.32192809488736 * log10(x). libopus uses exactly this log10-based identity (not a native log2), and the constant 1/log10(2) is the literal from the header, so reproducing the same expression is required for bit-exactness.

func SinF32

func SinF32(x float32) float32

SinF32 mirrors C sin() narrowed to float.

func SqrtF32

func SqrtF32(x float32) float32

SqrtF32 mirrors C sqrtf()/sqrt() narrowed to float.

Types

type CReal

type CReal = float64

CReal is the C double type as used by the libopus floating-point (FLP) build. It is an alias (not a defined type) so values flow to and from float64 helpers without conversions, exactly as C double does.

libopus' FLP code is mostly single precision, but a handful of accumulators are intentionally declared double even there to keep enough precision for bit-exactness against the reference. Marking those operands CReal documents that the double width is deliberate and stops them from being narrowed to float32. Examples include silk_energy_FLP(), silk_inner_product_FLP_c(), silk_burg_modified_FLP(), silk_schur_FLP(), warped_autocorrelation_FLP() and corrMatrix_FLP() (all in silk/float/).

func ExpCReal

func ExpCReal(x CReal) CReal

ExpCReal is the double-precision e**x for the FLP helpers that keep their accumulator in C double (see CReal); no narrowing to float occurs.

func Log10CReal

func Log10CReal(x CReal) CReal

Log10CReal is the double-precision log10 for the C-double FLP helpers.

func LogCReal

func LogCReal(x CReal) CReal

LogCReal is the double-precision natural log for the C-double FLP helpers.

func PowCReal

func PowCReal(x, y CReal) CReal

PowCReal is the double-precision x**y for the C-double FLP helpers.

func SqrtCReal

func SqrtCReal(x CReal) CReal

SqrtCReal is the double-precision sqrt for the C-double FLP helpers.

Jump to

Keyboard shortcuts

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