ref

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ref is the portable Go reference implementation of every kernel.

It defines the semantics. Every generated backend is differential-tested against it, and it is also the live fallback: the dispatcher runs these functions on architectures with no backend, in builds made with the purego tag, and below the per-kernel element threshold where crossing into assembly costs more than it saves.

It is therefore not throwaway code. The loops are written for bounds-check elimination because they run in the small-n hot path.

The numerical contract these functions define is documented on package kernel. The parts that are easy to get wrong:

  • Floating-point reductions use exactly kernel.SumLanes accumulators and kernel.CombineTree, so every vector width reproduces them bit for bit.
  • Dot multiplies and adds with separate roundings; it does not fuse.
  • Minimum, Maximum, Min and Max implement IEEE-754-2019 minimum/maximum: NaN propagates, and +0 compares greater than -0.
  • Integer Abs and Neg wrap, so Abs(MinInt32) is MinInt32, matching PABSD.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AbsFloat

func AbsFloat[T Float](dst, a []T)

func AbsInt

func AbsInt[T Integer](dst, a []T)

func Acos

func Acos[T float](dst, a []T)

func Acosh

func Acosh[T float](dst, a []T)

func Add

func Add[T Number](dst, a, b []T)

func Add3

func Add3[T Number](dst, a, b, c []T)

func Add4

func Add4[T Number](dst, a, b, c, d []T)

func AddScalar

func AddScalar[T Number](dst, a []T, s T)

func AddScaled

func AddScaled[T Number](dst, a, b []T, s T)

func ArgMaxFloat

func ArgMaxFloat[T Float](a []T) int

func ArgMaxInt

func ArgMaxInt[T Integer](a []T) int

func ArgMinFloat

func ArgMinFloat[T Float](a []T) int

func ArgMinInt

func ArgMinInt[T Integer](a []T) int

func Asin

func Asin[T float](dst, a []T)

func Asinh

func Asinh[T float](dst, a []T)

func Atan

func Atan[T float](dst, a []T)

func Atan2

func Atan2[T float](dst, a, b []T)

func Atanh

func Atanh[T float](dst, a []T)

func B64Decode

func B64Decode(dst, src []byte) int

func B64Encode

func B64Encode(dst, src []byte) int

func BF16ToF32

func BF16ToF32(dst []float32, a []uint16)

func BitAnd

func BitAnd(dst, a, b []byte)

func BitAndNot

func BitAndNot(dst, a, b []byte)

func BitNot

func BitNot(dst, a []byte)

func BitOr

func BitOr(dst, a, b []byte)

func BitPackU32

func BitPackU32(dst, a []uint32, bits int32)

func BitUnpackU32

func BitUnpackU32(dst, a []uint32, bits int32)

func BitXor

func BitXor(dst, a, b []byte)

func ByteSwap

func ByteSwap[T Integer](dst, a []T)

func CAbs64

func CAbs64(dst []float32, a []complex64)

func CAbs128

func CAbs128(dst []float64, a []complex128)

func CAdd

func CAdd[C complexT](dst, a, b []C)

func CConj64

func CConj64(dst, a []complex64)

func CConj128

func CConj128(dst, a []complex128)

func CDiv64

func CDiv64(dst, a, b []complex64)

func CDiv128

func CDiv128(dst, a, b []complex128)

func CDot64

func CDot64(a, b []complex64) complex64

func CDot128

func CDot128(a, b []complex128) complex128

func CDotConj64

func CDotConj64(a, b []complex64) complex64

func CDotConj128

func CDotConj128(a, b []complex128) complex128

func CFromParts64

func CFromParts64(dst []complex64, re, im []float32)

func CFromParts128

func CFromParts128(dst []complex128, re, im []float64)

func CImag64

func CImag64(dst []float32, a []complex64)

func CImag128

func CImag128(dst []float64, a []complex128)

func CMul64

func CMul64(dst, a, b []complex64)

func CMul128

func CMul128(dst, a, b []complex128)

func CNeg

func CNeg[C complexT](dst, a []C)

func CReal64

func CReal64(dst []float32, a []complex64)

func CReal128

func CReal128(dst []float64, a []complex128)

func CScale64

func CScale64(dst, a []complex64, s float32)

func CScale128

func CScale128(dst, a []complex128, s float64)

func CSub

func CSub[C complexT](dst, a, b []C)

func CSum64

func CSum64(a []complex64) complex64

func CSum128

func CSum128(a []complex128) complex128

func Cbrt

func Cbrt[T float](dst, a []T)

func Ceil

func Ceil[T Float](dst, a []T)

func ClampFloat

func ClampFloat[T Float](dst, a []T, lo, hi T)

func ClampInt

func ClampInt[T Integer](dst, a []T, lo, hi T)

func CommonPrefix

func CommonPrefix(a, b []byte) int

func CompareBytes

func CompareBytes(a, b []byte) int

func Compress

func Compress[T any](dst, src []T, keep []bool) int

func CompressFloat32

func CompressFloat32(dst, src []float32, keep []bool) int

func CompressFloat64

func CompressFloat64(dst, src []float64, keep []bool) int

func CompressInt32

func CompressInt32(dst, src []int32, keep []bool) int

func CompressInt64

func CompressInt64(dst, src []int64, keep []bool) int

func Convolve

func Convolve[T Number](dst, sig, ker []T)

func Correlate

func Correlate[T Number](dst, sig, ker []T)

func Cos

func Cos[T float](dst, a []T)

func Cosh

func Cosh[T float](dst, a []T)

func CountAny

func CountAny(b, chars []byte) int

func CountByte

func CountByte(b []byte, c byte) int

func CountSeq

func CountSeq(haystack, needle []byte) int

func CumMaxFloat

func CumMaxFloat[T Float](dst, a []T)

func CumMaxInt

func CumMaxInt[T Integer](dst, a []T)

func CumMinFloat

func CumMinFloat[T Float](dst, a []T)

func CumMinInt

func CumMinInt[T Integer](dst, a []T)

func CumProdInt

func CumProdInt[T Integer](dst, a []T)

CumProdInt is the EXACT integer product scan. Two's-complement multiplication is associative, so the blocked grouping is bit-identical to the serial loop for every input including ones that overflow — verified over four million deliberately overflowing values. It therefore needs no Fast prefix, and this is the reference the int32 kernel falls back to.

It is written as the blocked scan rather than as the obvious serial loop so that reference and kernel have the same shape, and a change to one is visibly a change to the other.

func DequantizeI8

func DequantizeI8(dst []float32, a []int8, scale float32, zeroPoint int32)

func DequantizePerChannelI8

func DequantizePerChannelI8(dst []float32, a []int8, scale []float32, zeroPoint []int32, channels, inner int)

func DequantizePerChannelU8

func DequantizePerChannelU8(dst []float32, a []uint8, scale []float32, zeroPoint []int32, channels, inner int)

func DequantizeU8

func DequantizeU8(dst []float32, a []uint8, scale float32, zeroPoint int32)

func Diff

func Diff[T Number](dst, a []T)

func DifferenceInt

func DifferenceInt[T Integer](dst, a, b []T) int

func Div

func Div[T Float](dst, a, b []T)

func DivScalar

func DivScalar[T Number](dst, a []T, s T)

func DotFloat

func DotFloat[T Float](a, b []T) T

func DotInt

func DotInt[T Integer](a, b []T) T

func EqualBytes

func EqualBytes(a, b []byte) bool

func EqualFoldASCII

func EqualFoldASCII(a, b []byte) bool

func EqualMask

func EqualMask[T number](dst []bool, a, b []T)

func EqualScalarMask

func EqualScalarMask[T number](dst []bool, a []T, v T)

func Erf

func Erf[T float](dst, a []T)

func Erfc

func Erfc[T float](dst, a []T)

func Exp

func Exp[T float](dst, a []T)

func Exp2

func Exp2[T float](dst, a []T)

func Expand

func Expand[T any](dst, src []T, keep []bool) int

func Expm1

func Expm1[T float](dst, a []T)

func F8E4M3ToF32

func F8E4M3ToF32(dst []float32, a []byte)

func F8E5M2ToF32

func F8E5M2ToF32(dst []float32, a []byte)

func F16ToF32

func F16ToF32(dst []float32, a []uint16)

func F32ToBF16

func F32ToBF16(dst []uint16, a []float32)

func F32ToF8E4M3

func F32ToF8E4M3(dst []byte, a []float32)

func F32ToF8E5M2

func F32ToF8E5M2(dst []byte, a []float32)

func F32ToF16

func F32ToF16(dst []uint16, a []float32)

func FastCumProdFloat

func FastCumProdFloat[T Float](dst, a []T)

func FastCumSumFloat

func FastCumSumFloat[T Float](dst, a []T)

FastCumSum and FastCumProd are the exported references the generated backends fall back to and the differential tests compare against.

FastCumSumFloat is the blocked scan for float32 and the SERIAL loop for float64, and that asymmetry is measured rather than arbitrary. Eight doubles fill one AVX-512 register, so the shift steps become cross-lane permutes, and against a serial chain of four-cycle adds the blocked form measured 0.91x — slower. float32 has sixteen lanes to hide the same latency and wins. Since no kernel is generated for float64, this branch is what every tier runs there, so they still agree with each other.

func Fill

func Fill[T Number](dst []T, v T)

func FillBytes

func FillBytes(dst []byte, v byte)

func FillFastFallbacks

func FillFastFallbacks(s *kernel.Set)

FillFastFallbacks points any Fast slot with no generated kernel at the accurate one, for both float groups of a backend.

It runs after a backend is fully assembled, which is the only moment the two cases can be distinguished, and it is what lets a caller use FastExp on every architecture without asking whether that architecture has it. A target where the Fast tier did not measure faster simply computes a more accurate answer, which an upper bound on error permits.

func Floor

func Floor[T Float](dst, a []T)

func FormatInts

func FormatInts(dst []byte, vals []int64, sep byte) int

FormatInts is the reference formatter: exact-fit, so it succeeds wherever success is possible, which is what makes it the safe fallback for short destinations.

func Gather

func Gather[T number](dst, src []T, idx []int32)

func GemmPackB

func GemmPackB[T Number](bp, b []T, k, n int)

func GemvFloat

func GemvFloat[T float](dst, a, x []T, m, k int)

func GemvInt

func GemvInt[T integer](dst, a, x []T, m, k int)

func Grayscale

func Grayscale(dst, r, g, b []byte)

func GreaterEqualMask

func GreaterEqualMask[T number](dst []bool, a, b []T)

func GreaterEqualScalarMask

func GreaterEqualScalarMask[T number](dst []bool, a []T, v T)

func GreaterMask

func GreaterMask[T number](dst []bool, a, b []T)

func GreaterScalarMask

func GreaterScalarMask[T number](dst []bool, a []T, v T)

func Hamming

func Hamming(a, b []byte) int

func HammingWords

func HammingWords(a, b []uint64) int

func HexDecode

func HexDecode(dst, src []byte) (int, bool)

HexDecode is exported for the generated dispatch tables, which name the reference by its exported identifier. The two results are what kept this portable before the generator could return a pair.

func HexEncode

func HexEncode(dst, src []byte) int

func Hypot

func Hypot[T float](dst, a, b []T)

func Index

func Index(haystack, needle []byte) int

func IndexAll

func IndexAll(dst []int32, b []byte, c byte) int

func IndexAny

func IndexAny(b, chars []byte) int

func IndexByte

func IndexByte(b []byte, c byte) int

func IndexNonASCII

func IndexNonASCII(b []byte) int

func IndexNonASCII16

func IndexNonASCII16(b []uint16) int

func IndexNotAny

func IndexNotAny(b, chars []byte) int

func IntersectInt

func IntersectInt[T Integer](dst, a, b []T) int

func IsASCII

func IsASCII(b []byte) bool

func L1DiffFloat

func L1DiffFloat[T Float](a, b []T) T

func L1DiffInt

func L1DiffInt[T Integer](a, b []T) T

func L1NormFloat

func L1NormFloat[T Float](a []T) T

func L1NormInt

func L1NormInt[T Integer](a []T) T

func LastIndex

func LastIndex(haystack, needle []byte) int

func LastIndexByte

func LastIndexByte(b []byte, c byte) int

func LastIndexNotAny

func LastIndexNotAny(b, chars []byte) int

func LayerNorm

func LayerNorm[T float](dst, a, gamma, beta []T, shift, denom T)

func LeadingZeros

func LeadingZeros[T Integer](dst, a []T)

func Lerp

func Lerp[T Number](dst, a, b []T, t T)

func LessEqualMask

func LessEqualMask[T number](dst []bool, a, b []T)

func LessEqualScalarMask

func LessEqualScalarMask[T number](dst []bool, a []T, v T)

func LessMask

func LessMask[T number](dst []bool, a, b []T)

func LessScalarMask

func LessScalarMask[T number](dst []bool, a []T, v T)

func Log

func Log[T float](dst, a []T)

func Log1p

func Log1p[T float](dst, a []T)

func Log2

func Log2[T float](dst, a []T)

func Log10

func Log10[T float](dst, a []T)

func LowerBoundFloat

func LowerBoundFloat[T Float](dst []int32, a, q []T)

func LowerBoundInt

func LowerBoundInt[T Integer](dst []int32, a, q []T)

func MaskAll

func MaskAll(m []bool) bool

func MaskAnd

func MaskAnd(dst, a, b []bool)

func MaskAny

func MaskAny(m []bool) bool

func MaskCount

func MaskCount(m []bool) int

func MaskNot

func MaskNot(dst, a []bool)

func MaskOr

func MaskOr(dst, a, b []bool)

func MaskXor

func MaskXor(dst, a, b []bool)

func MatMul

func MatMul[T number](dst, a, b []T, m, k, n int)

func MatMulPk

func MatMulPk[T Number](dst, a, bp []T, m, k, n int)

func MaxReduceFloat

func MaxReduceFloat[T Float](a []T) T

func MaxReduceInt

func MaxReduceInt[T Integer](a []T) T

func MaximumFloat

func MaximumFloat[T Float](dst, a, b []T)

func MaximumInt

func MaximumInt[T Integer](dst, a, b []T)

func MinMaxFloat

func MinMaxFloat[T Float](a []T) (T, T)

func MinMaxInt

func MinMaxInt[T Integer](a []T) (T, T)

func MinReduceFloat

func MinReduceFloat[T Float](a []T) T

func MinReduceInt

func MinReduceInt[T Integer](a []T) T

func MinimumFloat

func MinimumFloat[T Float](dst, a, b []T)

func MinimumInt

func MinimumInt[T Integer](dst, a, b []T)

func MovingAverage

func MovingAverage[T Number](dst, a []T, width int)

func Mul

func Mul[T Number](dst, a, b []T)

func Mul3

func Mul3[T Number](dst, a, b, c []T)

func Mul4

func Mul4[T Number](dst, a, b, c, d []T)

func NarrowU16U8

func NarrowU16U8(dst []byte, s []uint16)

func NarrowU32U8

func NarrowU32U8(dst []byte, s []uint32)

func NegFloat

func NegFloat[T Float](dst, a []T)

func NegInt

func NegInt[T Integer](dst, a []T)

func NormFloat

func NormFloat[T Float](a []T) T

func NotEqualMask

func NotEqualMask[T number](dst []bool, a, b []T)

func NotEqualScalarMask

func NotEqualScalarMask[T number](dst []bool, a []T, v T)

func OnesCount

func OnesCount[T Integer](dst, a []T)

func ParseInts

func ParseInts(dst []int64, src []byte, idx []int32) (int, bool)

ParseInts is the reference for the integer field parser. Fields are src[start:idx[k]] with start one past the previous separator, which is the shape IndexAll produces.

func ParseUints

func ParseUints(dst []uint64, src []byte, idx []int32) (int, bool)

ParseUints is ParseInts over the full uint64 range and with no sign.

A leading '+' is rejected rather than skipped, matching strconv.ParseUint with bitSize 64, which accepts no sign at all.

func Partition

func Partition[T Number](dst, src []T, pivot T) int

Partition is the exported entry point the generated guards call.

func PolyEval

func PolyEval[T Number](dst, x, coeffs []T)

func PopCount

func PopCount(b []byte) int

func Pow

func Pow[T float](dst, a, b []T)

func ProdInt

func ProdInt[T Integer](a []T) T

func QMatMulI8

func QMatMulI8(dst []int32, a, b []int8, m, k, n int)

func QuantizeI8

func QuantizeI8(dst []int8, a []float32, scale float32, zeroPoint int32)

func QuantizePerChannelI8

func QuantizePerChannelI8(dst []int8, a []float32, scale []float32, zeroPoint []int32, channels, inner int)

func QuantizePerChannelU8

func QuantizePerChannelU8(dst []uint8, a []float32, scale []float32, zeroPoint []int32, channels, inner int)

func QuantizeU8

func QuantizeU8(dst []uint8, a []float32, scale float32, zeroPoint int32)

func RGBToUV

func RGBToUV(u, v, r, g, b []byte)

func Ramp

func Ramp[T Number](dst []T, start, step T)

func RandomF32

func RandomF32(dst []float32, seed uint64)

func RandomF64

func RandomF64(dst []float64, seed uint64)

RandomF64 takes the top 53 bits and scales by 2^-53, the construction that cannot produce 1.0 — the largest result is 1 - 2^-53.

func RandomU64

func RandomU64(dst []uint64, seed uint64)

func RankOneFloat added in v1.2.0

func RankOneFloat[T Float](a, x, y []T, alpha T, m, n int)

RankOneFloat is a[i*n+j] += alpha*x[i]*y[j].

The row scale is hoisted exactly as the kernel hoists it. Writing alpha*x[i]*y[j] in the inner loop would associate the multiplications differently and disagree with the kernel in the last place, which the differential suite would then report as a kernel bug.

func Reciprocal

func Reciprocal[T Float](dst, a []T)

func ReplaceByte

func ReplaceByte(dst, b []byte, old, with byte)

func RequantizeI8

func RequantizeI8(dst []int8, a []int32, scale float32, zeroPoint int32)

func Reverse

func Reverse[T Number](dst, a []T)

func ReverseBits

func ReverseBits[T Integer](dst, a []T)

func RollingMaxFloat

func RollingMaxFloat[T Float](dst, a []T, window int)

func RollingMaxInt

func RollingMaxInt[T Integer](dst, a []T, window int)

func RollingMinFloat

func RollingMinFloat[T Float](dst, a []T, window int)

func RollingMinInt

func RollingMinInt[T Integer](dst, a []T, window int)

func RotateFloat added in v1.2.0

func RotateFloat[T Float](x, y []T, c, s T)

RotateFloat applies a Givens rotation to a pair of vectors, using the original x[i] in both assignments.

func Rotl

func Rotl[T Integer](dst, a []T, s uint64)

func Rotr

func Rotr[T Integer](dst, a []T, s uint64)

func Round

func Round[T Float](dst, a []T)

func RoundToEven

func RoundToEven[T Float](dst, a []T)

func RunStartsI32

func RunStartsI32(dst []bool, a []int32)

func RunStartsI64

func RunStartsI64(dst []bool, a []int64)

func RunStartsU8

func RunStartsU8(dst []bool, a []byte)

func SatAdd

func SatAdd[T Saturating](dst, a, b []T)

func SatSub

func SatSub[T Saturating](dst, a, b []T)

func Scale

func Scale[T Number](dst, a []T, s T)

func Scatter

func Scatter[T number](dst []T, idx []int32, src []T)

func Select

func Select[T number](dst []T, mask []bool, yes, no []T)

func Set

func Set() kernel.Set

Set returns the reference backend: every kernel, portable Go.

func ShiftDiv

func ShiftDiv[T float](dst, a []T, shift, denom T)

func Shl

func Shl[T Integer](dst, a []T, s uint64)

func Shr

func Shr[T Integer](dst, a []T, s uint64)

func Sigmoid

func Sigmoid[T float](dst, a []T)

func Sin

func Sin[T float](dst, a []T)

func Sinh

func Sinh[T float](dst, a []T)

func SortOrdered

func SortOrdered[T ordered](a []T)

SortOrdered sorts in place with the standard library's pdqsort.

It is here as the reference and as the small-case path, and on integers it is also the shipped implementation: see the note on the exported Sort.

func SparseDotFloat

func SparseDotFloat[T Float](v []T, idx []int32, x []T) T

func Sqrt

func Sqrt[T Float](dst, a []T)

func Sub

func Sub[T Number](dst, a, b []T)

func SubScalar

func SubScalar[T Number](dst, a []T, s T)

func SumFloat

func SumFloat[T Float](a []T) T

func SumInt

func SumInt[T Integer](a []T) T

func SumLanesFloat

func SumLanesFloat[T float](dst, a []T)

func SumSqDevFloat

func SumSqDevFloat[T Float](a []T, c T) T

func SumSqDevInt

func SumSqDevInt[T Integer](a []T, c T) T

func SumSqDiffFloat

func SumSqDiffFloat[T Float](a, b []T) T

func SumSqDiffInt

func SumSqDiffInt[T Integer](a, b []T) T

func SumSquaresFloat

func SumSquaresFloat[T Float](a []T) T

func SumSquaresInt

func SumSquaresInt[T Integer](a []T) T

func SwapFloat added in v1.2.0

func SwapFloat[T Float](x, y []T)

SwapFloat exchanges two vectors.

func SwapInt added in v1.2.0

func SwapInt[T Integer](x, y []T)

SwapInt is SwapFloat for the integer types.

func Tan

func Tan[T float](dst, a []T)

func Tanh

func Tanh[T float](dst, a []T)

func Tile

func Tile[T number](dst, pattern []T)

func ToLowerASCII

func ToLowerASCII(dst, b []byte)

func ToUpperASCII

func ToUpperASCII(dst, b []byte)

func TrailingZeros

func TrailingZeros[T Integer](dst, a []T)

func Transpose

func Transpose[T Number](dst, a []T, m, n int)

func Trunc

func Trunc[T Float](dst, a []T)

func ValidUTF8

func ValidUTF8(b []byte) bool

func VarintLenU32

func VarintLenU32(dst []int32, a []uint32)

func VarintLenU64

func VarintLenU64(dst []int32, a []uint64)

func VarintSizeU32

func VarintSizeU32(a []uint32) int

func VarintSizeU64

func VarintSizeU64(a []uint64) int

func WidenU8U16

func WidenU8U16(dst []uint16, s []byte)

func WidenU8U32

func WidenU8U32(dst []uint32, s []byte)

func ZigzagDecodeI8

func ZigzagDecodeI8(dst []int8, a []byte)

func ZigzagDecodeI16

func ZigzagDecodeI16(dst []int16, a []uint16)

func ZigzagDecodeI32

func ZigzagDecodeI32(dst []int32, a []uint32)

func ZigzagDecodeI64

func ZigzagDecodeI64(dst []int64, a []uint64)

func ZigzagEncodeI8

func ZigzagEncodeI8(dst []byte, a []int8)

func ZigzagEncodeI16

func ZigzagEncodeI16(dst []uint16, a []int16)

func ZigzagEncodeI32

func ZigzagEncodeI32(dst []uint32, a []int32)

func ZigzagEncodeI64

func ZigzagEncodeI64(dst []uint64, a []int64)

Types

type Float

type Float interface{ ~float32 | ~float64 }

Float is the element types with a fixed-tree reduction.

type Integer

type Integer = integer

Integer is the integer half of it, for the operations that are not the same on floats: integer minimum is not IEEE minimum, and integer Abs wraps where float Abs clears a sign bit.

type Number

type Number interface {
	~float32 | ~float64 |
		~int8 | ~int16 | ~int32 | ~int64 |
		~uint8 | ~uint16 | ~uint32 | ~uint64
}

Number is any element type the kernels handle.

type Saturating

type Saturating = satInteger

Saturating is the integer types that have saturating add and subtract. The 64-bit ones are absent for the reason kernels.saturating gives: there is nothing wider to detect the overflow in.

Jump to

Keyboard shortcuts

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