compute

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package compute provides portable SIMD primitives for the dataset engines.

This package is a thin, zero-overhead wrapper around go-highway (hwy). It re-exports the complete hwy core API so that engine code never imports hwy directly — allowing the SIMD backend to be swapped transparently.

Architecture dispatch is automatic at runtime:

AMD64: AVX-512 → AVX2 → scalar
ARM64: SVE → NEON → scalar
Other: pure Go scalar fallback

All functions are generic over hwy.Lanes (float32, float64, int32, int64).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompressStore

func CompressStore[T Lanes](v Vec[T], mask Mask[T], dst []T) int

CompressStore compresses elements based on a mask and stores them directly to a slice. It returns the number of elements successfully stored.

func ConstValue

func ConstValue[T Lanes](val float32) T

ConstValue converts a float32 constant to type T.

func GetLane

func GetLane[T Lanes](v Vec[T], lane int) T

GetLane extracts the value from the specified lane of a vector.

func HasSIMD

func HasSIMD() bool

HasSIMD returns true if hardware SIMD acceleration is active. Returns false in scalar fallback mode (no GOEXPERIMENT=simd or HWY_NO_SIMD set).

func MaskStore

func MaskStore[T Lanes](mask Mask[T], v Vec[T], data []T)

MaskStore writes only the lanes where mask is true.

func NumLanes

func NumLanes[T Lanes]() int

NumLanes returns the number of lanes in a SIMD vector for type T on the current hardware (e.g. 8 for float64 on AVX-512).

func ReduceMax

func ReduceMax[T Lanes](v Vec[T]) T

ReduceMax computes the maximum lane value in the vector.

func ReduceMin

func ReduceMin[T Lanes](v Vec[T]) T

ReduceMin computes the minimum lane value in the vector.

func ReduceSum

func ReduceSum[T Lanes](v Vec[T]) T

ReduceSum computes the sum of all lanes in the vector.

func SIMDName

func SIMDName() string

SIMDName returns the active SIMD target name (e.g. "avx512", "avx2", "neon", "scalar").

func SliceAbs

func SliceAbs[T Lanes](dst, src []T)

SliceAbs computes dst[i] = |src[i]|.

func SliceAdd

func SliceAdd[T Lanes](dst, a, b []T)

SliceAdd computes dst[i] = a[i] + b[i].

func SliceAddScalar

func SliceAddScalar[T Lanes](dst, src []T, val T)

SliceAddScalar computes dst[i] = src[i] + val.

func SliceDiv

func SliceDiv[T Floats](dst, a, b []T)

SliceDiv computes dst[i] = a[i] / b[i] (floats only).

func SliceMax

func SliceMax[T Lanes](data []T) T

SliceMax computes the maximum element in a slice.

func SliceMin

func SliceMin[T Lanes](data []T) T

SliceMin computes the minimum element in a slice.

func SliceMinMax

func SliceMinMax[T Lanes](data []T) (lo, hi T)

SliceMinMax computes both min and max of a slice in a single pass.

func SliceMul

func SliceMul[T Lanes](dst, a, b []T)

SliceMul computes dst[i] = a[i] * b[i].

func SliceMulScalar

func SliceMulScalar[T Lanes](dst, src []T, val T)

SliceMulScalar computes dst[i] = src[i] * val.

func SliceNeg

func SliceNeg[T Lanes](dst, src []T)

SliceNeg computes dst[i] = -src[i].

func SlicePow

func SlicePow(dst, src []float64, exp float64)

SlicePow computes dst[i] = src[i]^exp (float64 only).

func SliceSqrt

func SliceSqrt(dst, src []float64)

SliceSqrt computes dst[i] = √src[i] (float64 only).

func SliceSub

func SliceSub[T Lanes](dst, a, b []T)

SliceSub computes dst[i] = a[i] - b[i].

func SliceSum

func SliceSum[T Lanes](data []T) T

SliceSum computes the sum of all elements in a slice.

func Store

func Store[T Lanes](v Vec[T], data []T)

Store writes a vector to data (no bounds check).

func StoreSlice

func StoreSlice[T Lanes](v Vec[T], data []T)

StoreSlice safely writes a vector to data, handling short slices.

Types

type Floats

type Floats = hwy.Floats

Floats is the constraint for floating-point SIMD types.

type Integers

type Integers = hwy.Integers

Integers is a re-export of hwy.Integers for use by callers.

type Lanes

type Lanes = hwy.Lanes

Lanes is the constraint for types supported by SIMD vectors.

type Mask

type Mask[T hwy.Lanes] = hwy.Mask[T]

Mask is a re-export of hwy.Mask.

func Equal

func Equal[T Lanes](a, b Vec[T]) Mask[T]

Equal returns a mask where a[i] == b[i].

func GreaterEqual

func GreaterEqual[T Lanes](a, b Vec[T]) Mask[T]

GreaterEqual returns a mask where a[i] >= b[i].

func GreaterThan

func GreaterThan[T Lanes](a, b Vec[T]) Mask[T]

GreaterThan returns a mask where a[i] > b[i].

func IsFinite

func IsFinite[T Floats](v Vec[T]) Mask[T]

IsFinite returns a mask where v[i] is finite (not NaN and not ±Inf).

func IsInf

func IsInf[T Floats](v Vec[T], sign int) Mask[T]

IsInf returns a mask where v[i] is ±Inf. sign: 0 = either, >0 = +Inf only, <0 = -Inf only.

func IsNaN

func IsNaN[T Floats](v Vec[T]) Mask[T]

IsNaN returns a mask where v[i] is NaN.

func LessEqual

func LessEqual[T Lanes](a, b Vec[T]) Mask[T]

LessEqual returns a mask where a[i] <= b[i].

func LessThan

func LessThan[T Lanes](a, b Vec[T]) Mask[T]

LessThan returns a mask where a[i] < b[i].

func NotEqual

func NotEqual[T Lanes](a, b Vec[T]) Mask[T]

NotEqual returns a mask where a[i] != b[i].

type Vec

type Vec[T hwy.Lanes] = hwy.Vec[T]

Vec is a re-export of hwy.Vec so callers never import hwy directly.

func Abs

func Abs[T Lanes](v Vec[T]) Vec[T]

Abs returns element-wise |v|.

func Add

func Add[T Lanes](a, b Vec[T]) Vec[T]

Add returns element-wise a + b.

func And

func And[T Lanes](a, b Vec[T]) Vec[T]

And returns element-wise a & b.

func AndNot

func AndNot[T Lanes](a, b Vec[T]) Vec[T]

AndNot returns element-wise (^a) & b.

func AsFloat32

func AsFloat32(v Vec[int32]) Vec[float32]

AsFloat32 reinterprets an int32 vector as float32.

func AsFloat64

func AsFloat64(v Vec[int64]) Vec[float64]

AsFloat64 reinterprets an int64 vector as float64.

func AsInt32

func AsInt32(v Vec[float32]) Vec[int32]

AsInt32 reinterprets a float32 vector as int32.

func AsInt64

func AsInt64(v Vec[float64]) Vec[int64]

AsInt64 reinterprets a float64 vector as int64.

func Broadcast

func Broadcast[T Lanes](v Vec[T], lane int) Vec[T]

Broadcast copies lane n to all other lanes.

func Const

func Const[T Lanes](val float32) Vec[T]

Const returns a vector with all lanes set to the given float32 constant. Convenient for float literals: compute.Const[float64](0.5).

func Div

func Div[T Floats](a, b Vec[T]) Vec[T]

Div returns element-wise a / b (floats only).

func FMA

func FMA[T Floats](a, b, c Vec[T]) Vec[T]

FMA returns fused multiply-add: a*b + c with single rounding (floats only).

func IfThenElse

func IfThenElse[T Lanes](mask Mask[T], a, b Vec[T]) Vec[T]

IfThenElse returns a[i] where mask is true, b[i] otherwise.

func IfThenElseZero

func IfThenElseZero[T Lanes](mask Mask[T], v Vec[T]) Vec[T]

IfThenElseZero returns v[i] where mask is true, 0 otherwise.

func IfThenZeroElse

func IfThenZeroElse[T Lanes](mask Mask[T], v Vec[T]) Vec[T]

IfThenZeroElse returns 0 where mask is true, v[i] otherwise.

func Iota

func Iota[T Lanes]() Vec[T]

Iota returns a vector with lanes set to [0, 1, 2, ...].

func Load

func Load[T Lanes](data []T) Vec[T]

Load loads a vector from data (no bounds check — caller must guarantee len ≥ NumLanes).

func Load4

func Load4[T Lanes](data []T) (a, b, c, d Vec[T])

Load4 loads four interleaved vectors from data (AoS → SoA deinterleave).

func LoadSlice

func LoadSlice[T Lanes](data []T) Vec[T]

LoadSlice safely loads a vector from data, handling short slices.

func MaskLoad

func MaskLoad[T Lanes](mask Mask[T], data []T) Vec[T]

MaskLoad loads a vector from data, zeroing lanes where mask is false.

func Max

func Max[T Lanes](a, b Vec[T]) Vec[T]

Max returns element-wise max(a, b).

func Min

func Min[T Lanes](a, b Vec[T]) Vec[T]

Min returns element-wise min(a, b).

func Mul

func Mul[T Lanes](a, b Vec[T]) Vec[T]

Mul returns element-wise a * b.

func MulAdd

func MulAdd[T Floats](a, b, c Vec[T]) Vec[T]

MulAdd returns a*b + c (floats only). Alias for FMA.

func Neg

func Neg[T Lanes](v Vec[T]) Vec[T]

Neg returns element-wise -v.

func Not

func Not[T Lanes](v Vec[T]) Vec[T]

Not returns element-wise ^v (bitwise complement).

func Or

func Or[T Lanes](a, b Vec[T]) Vec[T]

Or returns element-wise a | b.

func Pow

func Pow[T Floats](base, exp Vec[T]) Vec[T]

Pow returns element-wise base^exp.

func RSqrt

func RSqrt[T Floats](v Vec[T]) Vec[T]

RSqrt returns element-wise approximate 1/√v.

func RSqrtNewtonRaphson

func RSqrtNewtonRaphson[T Floats](v Vec[T]) Vec[T]

RSqrtNewtonRaphson returns element-wise 1/√v refined with Newton-Raphson.

func RSqrtPrecise

func RSqrtPrecise[T Floats](v Vec[T]) Vec[T]

RSqrtPrecise returns element-wise 1/√v with full precision.

func Reverse

func Reverse[T Lanes](v Vec[T]) Vec[T]

Reverse reverses the order of all lanes in a vector.

func Reverse2

func Reverse2[T Lanes](v Vec[T]) Vec[T]

Reverse2 reverses pairs of adjacent lanes.

func Reverse4

func Reverse4[T Lanes](v Vec[T]) Vec[T]

Reverse4 reverses groups of 4 adjacent lanes.

func Reverse8

func Reverse8[T Lanes](v Vec[T]) Vec[T]

Reverse8 reverses groups of 8 adjacent lanes.

func RoundToEven

func RoundToEven[T Floats](v Vec[T]) Vec[T]

RoundToEven returns element-wise banker's rounding (round half to even).

func Set

func Set[T Lanes](val T) Vec[T]

Set broadcasts a scalar value to all lanes of a vector.

func ShiftLeft

func ShiftLeft[T Integers](v Vec[T], bits int) Vec[T]

ShiftLeft returns element-wise v << bits (integers only).

func ShiftRight

func ShiftRight[T Integers](v Vec[T], bits int) Vec[T]

ShiftRight returns element-wise v >> bits (integers only). Arithmetic for signed, logical for unsigned.

func SignBit

func SignBit[T Lanes]() Vec[T]

SignBit returns a vector with the sign bit set in every lane. For floats: -0.0. For signed ints: minimum value. For unsigned: high bit.

func Sqrt

func Sqrt[T Floats](v Vec[T]) Vec[T]

Sqrt returns element-wise √v.

func Sub

func Sub[T Lanes](a, b Vec[T]) Vec[T]

Sub returns element-wise a - b.

func Xor

func Xor[T Lanes](a, b Vec[T]) Vec[T]

Xor returns element-wise a ^ b.

func Zero

func Zero[T Lanes]() Vec[T]

Zero returns a vector with all lanes set to zero.

func ZeroIfNegative

func ZeroIfNegative[T Lanes](v Vec[T]) Vec[T]

ZeroIfNegative returns 0 where v[i] < 0, v[i] otherwise (ReLU).

Jump to

Keyboard shortcuts

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