Documentation
¶
Overview ¶
Package simd implements portable and vector-size-agnostic SIMD types, and functions and methods for working with these types. SIMD types are either implemented in hardware (for example, arm64 "Neon" or amd64 "AVX/AVX2/AVX512") using the corresponding types in the simd/archsimd package, or emulated in pure Go. In all cases, the vector length is at least 128 bits, and within a given program execution, all vectors have the same length.
SIMD Types ¶
There is a simd type corresponding to each primitive numeric type, except for complex64 and complex128. Each of these is the type name, capitalized, with an "s" suffix, for example Int8s, Uint16s, or Float64s.
There are also simd "mask" types that abstract the mask registers present in some architectures, otherwise these will be implemented as bit masks.
Obtaining SIMD values ¶
The zero value of a SIMD vector type is valid and represents a zero vector.
For each SIMD type, "Load<Types>(s []<type>) <Types>" loads a full verctor of the type from a long-enough slice.
For slices that are not long enough, "Load<Types>Part(s []<type>) (<Types>, int)" will load as many elements as are available from the slice, fill the remainder with zero, and also return the number that were loaded.
For each SIMD type, "Broadcast<Types>(x type) <Types>" returns a vector whose elements are all initialized to x.
Examples:
Operations ¶
SIMD types provide methods for unary (Float32s.Abs, Int16s.Not), binary (Float64s.Add, Uint32s.GreaterEqual), and ternary operations (Int64s.IfElse, Float32s.MulAdd). Relational operations produce masks.
SIMD types also support conversions between types, both those that are mostly value-preserving (Int32s.ConvertToFloat32, Float32s.ConvertToInt32) and those that change types without altering the underlying vector bit pattern.
Signed integer types convert to mask types (Int16s.ToMask), but this is a comparison against zero, not a simple bitwise conversion. Mask types convert to signed integers in an operation (Mask32s.ToInt32s) that may be a zero-cost bitwise conversion, or not, depending on the underlying hardware.
Storing ¶
SIMD vector types have two methods, one for storing the entire vector into a slice "Store([]<type>)"" and a second for storing part of a vector into a slice "StorePart([]<type>) int". StorePart returns the number of elements actually stored.
String conversion ¶
Vectors and masks provide a String method for conversion to strings.
Conversion to and from simd/archsimd types. ¶
Each SIMD vector type has a "ToArch() any" method that returns the type supported by the current hardware as an "any". Code using these methods must be build-tagged to the relevant architecture(s) and type-assert the returned value to the appropriate type.
The simd package also includes generic functions for converting an architecture-dependent simd/archsimd value (e.g. archsimd.Float32x4) into the corresponding simd type. This function will panic if the correspondence is incorrect.
For an example of converting between simd and arch/simd types, see the test file sum_amd64_test.go.
Index ¶
- func Emulated() bool
- func HasHardwareCarrylessMultiply() bool
- func VectorBitSize() int
- type Float32s
- func (x Float32s) Abs() Float32s
- func (x Float32s) Add(y Float32s) Float32s
- func (x Float32s) ConvertToInt32() Int32s
- func (x Float32s) Div(y Float32s) Float32s
- func (x Float32s) Equal(y Float32s) Mask32s
- func (x Float32s) Greater(y Float32s) Mask32s
- func (x Float32s) GreaterEqual(y Float32s) Mask32s
- func (x Float32s) IfElse(mask Mask32s, y Float32s) Float32s
- func (x Float32s) Len() int
- func (x Float32s) Less(y Float32s) Mask32s
- func (x Float32s) LessEqual(y Float32s) Mask32s
- func (x Float32s) Masked(mask Mask32s) Float32s
- func (x Float32s) Max(y Float32s) Float32s
- func (x Float32s) Min(y Float32s) Float32s
- func (x Float32s) Mul(y Float32s) Float32s
- func (x Float32s) MulAdd(y Float32s, z Float32s) Float32s
- func (x Float32s) Neg() Float32s
- func (x Float32s) NotEqual(y Float32s) Mask32s
- func (x Float32s) Sqrt() Float32s
- func (x Float32s) Store(s []float32)
- func (x Float32s) StorePart(s []float32) int
- func (x Float32s) String() string
- func (x Float32s) Sub(y Float32s) Float32s
- func (x Float32s) ToArch() any
- func (x Float32s) ToBits() Uint32s
- type Float64s
- func (x Float64s) Abs() Float64s
- func (x Float64s) Add(y Float64s) Float64s
- func (x Float64s) Div(y Float64s) Float64s
- func (x Float64s) Equal(y Float64s) Mask64s
- func (x Float64s) Greater(y Float64s) Mask64s
- func (x Float64s) GreaterEqual(y Float64s) Mask64s
- func (x Float64s) IfElse(mask Mask64s, y Float64s) Float64s
- func (x Float64s) Len() int
- func (x Float64s) Less(y Float64s) Mask64s
- func (x Float64s) LessEqual(y Float64s) Mask64s
- func (x Float64s) Masked(mask Mask64s) Float64s
- func (x Float64s) Max(y Float64s) Float64s
- func (x Float64s) Min(y Float64s) Float64s
- func (x Float64s) Mul(y Float64s) Float64s
- func (x Float64s) MulAdd(y Float64s, z Float64s) Float64s
- func (x Float64s) Neg() Float64s
- func (x Float64s) NotEqual(y Float64s) Mask64s
- func (x Float64s) Sqrt() Float64s
- func (x Float64s) Store(s []float64)
- func (x Float64s) StorePart(s []float64) int
- func (x Float64s) String() string
- func (x Float64s) Sub(y Float64s) Float64s
- func (x Float64s) ToArch() any
- func (x Float64s) ToBits() Uint64s
- type Int8s
- func (x Int8s) Abs() Int8s
- func (x Int8s) Add(y Int8s) Int8s
- func (x Int8s) AddSaturated(y Int8s) Int8s
- func (x Int8s) And(y Int8s) Int8s
- func (x Int8s) AndNot(y Int8s) Int8s
- func (x Int8s) ConvertToUint8() Uint8s
- func (x Int8s) Equal(y Int8s) Mask8s
- func (x Int8s) Greater(y Int8s) Mask8s
- func (x Int8s) GreaterEqual(y Int8s) Mask8s
- func (x Int8s) IfElse(mask Mask8s, y Int8s) Int8s
- func (x Int8s) Len() int
- func (x Int8s) Less(y Int8s) Mask8s
- func (x Int8s) LessEqual(y Int8s) Mask8s
- func (x Int8s) Masked(mask Mask8s) Int8s
- func (x Int8s) Max(y Int8s) Int8s
- func (x Int8s) Min(y Int8s) Int8s
- func (x Int8s) Mul(y Int8s) Int8s
- func (x Int8s) Neg() Int8s
- func (x Int8s) Not() Int8s
- func (x Int8s) NotEqual(y Int8s) Mask8s
- func (x Int8s) Or(y Int8s) Int8s
- func (x Int8s) Store(s []int8)
- func (x Int8s) StorePart(s []int8) int
- func (x Int8s) String() string
- func (x Int8s) Sub(y Int8s) Int8s
- func (x Int8s) SubSaturated(y Int8s) Int8s
- func (x Int8s) ToArch() any
- func (x Int8s) ToBits() Uint8s
- func (x Int8s) ToMask() (to Mask8s)
- func (x Int8s) Xor(y Int8s) Int8s
- type Int16s
- func (x Int16s) Abs() Int16s
- func (x Int16s) Add(y Int16s) Int16s
- func (x Int16s) AddSaturated(y Int16s) Int16s
- func (x Int16s) And(y Int16s) Int16s
- func (x Int16s) AndNot(y Int16s) Int16s
- func (x Int16s) ConvertToUint16() Uint16s
- func (x Int16s) Equal(y Int16s) Mask16s
- func (x Int16s) Greater(y Int16s) Mask16s
- func (x Int16s) GreaterEqual(y Int16s) Mask16s
- func (x Int16s) IfElse(mask Mask16s, y Int16s) Int16s
- func (x Int16s) Len() int
- func (x Int16s) Less(y Int16s) Mask16s
- func (x Int16s) LessEqual(y Int16s) Mask16s
- func (x Int16s) Masked(mask Mask16s) Int16s
- func (x Int16s) Max(y Int16s) Int16s
- func (x Int16s) Min(y Int16s) Int16s
- func (x Int16s) Mul(y Int16s) Int16s
- func (x Int16s) Neg() Int16s
- func (x Int16s) Not() Int16s
- func (x Int16s) NotEqual(y Int16s) Mask16s
- func (x Int16s) Or(y Int16s) Int16s
- func (x Int16s) RotateAllLeft(dist uint64) Int16s
- func (x Int16s) RotateAllRight(dist uint64) Int16s
- func (x Int16s) ShiftAllLeft(shift uint64) Int16s
- func (x Int16s) ShiftAllRight(shift uint64) Int16s
- func (x Int16s) Store(s []int16)
- func (x Int16s) StorePart(s []int16) int
- func (x Int16s) String() string
- func (x Int16s) Sub(y Int16s) Int16s
- func (x Int16s) SubSaturated(y Int16s) Int16s
- func (x Int16s) ToArch() any
- func (x Int16s) ToBits() Uint16s
- func (x Int16s) ToMask() (to Mask16s)
- func (x Int16s) Xor(y Int16s) Int16s
- type Int32s
- func (x Int32s) Abs() Int32s
- func (x Int32s) Add(y Int32s) Int32s
- func (x Int32s) And(y Int32s) Int32s
- func (x Int32s) AndNot(y Int32s) Int32s
- func (x Int32s) ConvertToFloat32() Float32s
- func (x Int32s) ConvertToUint32() Uint32s
- func (x Int32s) Equal(y Int32s) Mask32s
- func (x Int32s) Greater(y Int32s) Mask32s
- func (x Int32s) GreaterEqual(y Int32s) Mask32s
- func (x Int32s) IfElse(mask Mask32s, y Int32s) Int32s
- func (x Int32s) Len() int
- func (x Int32s) Less(y Int32s) Mask32s
- func (x Int32s) LessEqual(y Int32s) Mask32s
- func (x Int32s) Masked(mask Mask32s) Int32s
- func (x Int32s) Max(y Int32s) Int32s
- func (x Int32s) Min(y Int32s) Int32s
- func (x Int32s) Mul(y Int32s) Int32s
- func (x Int32s) Neg() Int32s
- func (x Int32s) Not() Int32s
- func (x Int32s) NotEqual(y Int32s) Mask32s
- func (x Int32s) Or(y Int32s) Int32s
- func (x Int32s) RotateAllLeft(dist uint64) Int32s
- func (x Int32s) RotateAllRight(dist uint64) Int32s
- func (x Int32s) ShiftAllLeft(shift uint64) Int32s
- func (x Int32s) ShiftAllRight(shift uint64) Int32s
- func (x Int32s) Store(s []int32)
- func (x Int32s) StorePart(s []int32) int
- func (x Int32s) String() string
- func (x Int32s) Sub(y Int32s) Int32s
- func (x Int32s) ToArch() any
- func (x Int32s) ToBits() Uint32s
- func (x Int32s) ToMask() (to Mask32s)
- func (x Int32s) Xor(y Int32s) Int32s
- type Int64s
- func (x Int64s) Add(y Int64s) Int64s
- func (x Int64s) And(y Int64s) Int64s
- func (x Int64s) AndNot(y Int64s) Int64s
- func (x Int64s) ConvertToUint64() Uint64s
- func (x Int64s) Equal(y Int64s) Mask64s
- func (x Int64s) Greater(y Int64s) Mask64s
- func (x Int64s) GreaterEqual(y Int64s) Mask64s
- func (x Int64s) IfElse(mask Mask64s, y Int64s) Int64s
- func (x Int64s) Len() int
- func (x Int64s) Less(y Int64s) Mask64s
- func (x Int64s) LessEqual(y Int64s) Mask64s
- func (x Int64s) Masked(mask Mask64s) Int64s
- func (x Int64s) Neg() Int64s
- func (x Int64s) Not() Int64s
- func (x Int64s) NotEqual(y Int64s) Mask64s
- func (x Int64s) Or(y Int64s) Int64s
- func (x Int64s) RotateAllLeft(dist uint64) Int64s
- func (x Int64s) RotateAllRight(dist uint64) Int64s
- func (x Int64s) ShiftAllLeft(shift uint64) Int64s
- func (x Int64s) Store(s []int64)
- func (x Int64s) StorePart(s []int64) int
- func (x Int64s) String() string
- func (x Int64s) Sub(y Int64s) Int64s
- func (x Int64s) ToArch() any
- func (x Int64s) ToBits() Uint64s
- func (x Int64s) ToMask() (to Mask64s)
- func (x Int64s) Xor(y Int64s) Int64s
- type Mask8s
- type Mask16s
- type Mask32s
- type Mask64s
- type Uint8s
- func (x Uint8s) Add(y Uint8s) Uint8s
- func (x Uint8s) AddSaturated(y Uint8s) Uint8s
- func (x Uint8s) And(y Uint8s) Uint8s
- func (x Uint8s) AndNot(y Uint8s) Uint8s
- func (x Uint8s) Average(y Uint8s) Uint8s
- func (x Uint8s) BitsToInt8() Int8s
- func (x Uint8s) ConvertToInt8() Int8s
- func (x Uint8s) Equal(y Uint8s) Mask8s
- func (x Uint8s) IfElse(mask Mask8s, y Uint8s) Uint8s
- func (x Uint8s) Len() int
- func (x Uint8s) Masked(mask Mask8s) Uint8s
- func (x Uint8s) Max(y Uint8s) Uint8s
- func (x Uint8s) Min(y Uint8s) Uint8s
- func (x Uint8s) Mul(y Uint8s) Uint8s
- func (x Uint8s) Not() Uint8s
- func (x Uint8s) NotEqual(y Uint8s) Mask8s
- func (x Uint8s) Or(y Uint8s) Uint8s
- func (x Uint8s) ReshapeToUint16s() Uint16s
- func (x Uint8s) ReshapeToUint32s() Uint32s
- func (x Uint8s) ReshapeToUint64s() Uint64s
- func (x Uint8s) Store(s []uint8)
- func (x Uint8s) StorePart(s []uint8) int
- func (x Uint8s) String() string
- func (x Uint8s) Sub(y Uint8s) Uint8s
- func (x Uint8s) SubSaturated(y Uint8s) Uint8s
- func (x Uint8s) ToArch() any
- func (x Uint8s) Xor(y Uint8s) Uint8s
- type Uint16s
- func (x Uint16s) Add(y Uint16s) Uint16s
- func (x Uint16s) AddSaturated(y Uint16s) Uint16s
- func (x Uint16s) And(y Uint16s) Uint16s
- func (x Uint16s) AndNot(y Uint16s) Uint16s
- func (x Uint16s) Average(y Uint16s) Uint16s
- func (x Uint16s) BitsToInt16() Int16s
- func (x Uint16s) ConvertToInt16() Int16s
- func (x Uint16s) Equal(y Uint16s) Mask16s
- func (x Uint16s) Greater(y Uint16s) Mask16s
- func (x Uint16s) GreaterEqual(y Uint16s) Mask16s
- func (x Uint16s) IfElse(mask Mask16s, y Uint16s) Uint16s
- func (x Uint16s) Len() int
- func (x Uint16s) Less(y Uint16s) Mask16s
- func (x Uint16s) LessEqual(y Uint16s) Mask16s
- func (x Uint16s) Masked(mask Mask16s) Uint16s
- func (x Uint16s) Max(y Uint16s) Uint16s
- func (x Uint16s) Min(y Uint16s) Uint16s
- func (x Uint16s) Mul(y Uint16s) Uint16s
- func (x Uint16s) Not() Uint16s
- func (x Uint16s) NotEqual(y Uint16s) Mask16s
- func (x Uint16s) Or(y Uint16s) Uint16s
- func (x Uint16s) ReshapeToUint8s() Uint8s
- func (x Uint16s) ReshapeToUint32s() Uint32s
- func (x Uint16s) ReshapeToUint64s() Uint64s
- func (x Uint16s) RotateAllLeft(dist uint64) Uint16s
- func (x Uint16s) RotateAllRight(dist uint64) Uint16s
- func (x Uint16s) ShiftAllLeft(shift uint64) Uint16s
- func (x Uint16s) ShiftAllRight(shift uint64) Uint16s
- func (x Uint16s) Store(s []uint16)
- func (x Uint16s) StorePart(s []uint16) int
- func (x Uint16s) String() string
- func (x Uint16s) Sub(y Uint16s) Uint16s
- func (x Uint16s) SubSaturated(y Uint16s) Uint16s
- func (x Uint16s) ToArch() any
- func (x Uint16s) Xor(y Uint16s) Uint16s
- type Uint32s
- func (x Uint32s) Add(y Uint32s) Uint32s
- func (x Uint32s) And(y Uint32s) Uint32s
- func (x Uint32s) AndNot(y Uint32s) Uint32s
- func (x Uint32s) BitsToFloat32() Float32s
- func (x Uint32s) BitsToInt32() Int32s
- func (x Uint32s) ConvertToInt32() Int32s
- func (x Uint32s) Equal(y Uint32s) Mask32s
- func (x Uint32s) Greater(y Uint32s) Mask32s
- func (x Uint32s) GreaterEqual(y Uint32s) Mask32s
- func (x Uint32s) IfElse(mask Mask32s, y Uint32s) Uint32s
- func (x Uint32s) Len() int
- func (x Uint32s) Less(y Uint32s) Mask32s
- func (x Uint32s) LessEqual(y Uint32s) Mask32s
- func (x Uint32s) Masked(mask Mask32s) Uint32s
- func (x Uint32s) Max(y Uint32s) Uint32s
- func (x Uint32s) Min(y Uint32s) Uint32s
- func (x Uint32s) Mul(y Uint32s) Uint32s
- func (x Uint32s) Not() Uint32s
- func (x Uint32s) NotEqual(y Uint32s) Mask32s
- func (x Uint32s) Or(y Uint32s) Uint32s
- func (x Uint32s) ReshapeToUint8s() Uint8s
- func (x Uint32s) ReshapeToUint16s() Uint16s
- func (x Uint32s) ReshapeToUint64s() Uint64s
- func (x Uint32s) RotateAllLeft(dist uint64) Uint32s
- func (x Uint32s) RotateAllRight(dist uint64) Uint32s
- func (x Uint32s) ShiftAllLeft(shift uint64) Uint32s
- func (x Uint32s) ShiftAllRight(shift uint64) Uint32s
- func (x Uint32s) Store(s []uint32)
- func (x Uint32s) StorePart(s []uint32) int
- func (x Uint32s) String() string
- func (x Uint32s) Sub(y Uint32s) Uint32s
- func (x Uint32s) ToArch() any
- func (x Uint32s) Xor(y Uint32s) Uint32s
- type Uint64s
- func (x Uint64s) Add(y Uint64s) Uint64s
- func (x Uint64s) And(y Uint64s) Uint64s
- func (x Uint64s) AndNot(y Uint64s) Uint64s
- func (x Uint64s) BitsToFloat64() Float64s
- func (x Uint64s) BitsToInt64() Int64s
- func (x Uint64s) CarrylessMultiplyEven(y Uint64s) Uint64s
- func (x Uint64s) CarrylessMultiplyOdd(y Uint64s) Uint64s
- func (x Uint64s) ConvertToInt64() Int64s
- func (x Uint64s) Equal(y Uint64s) Mask64s
- func (x Uint64s) Greater(y Uint64s) Mask64s
- func (x Uint64s) GreaterEqual(y Uint64s) Mask64s
- func (x Uint64s) IfElse(mask Mask64s, y Uint64s) Uint64s
- func (x Uint64s) Len() int
- func (x Uint64s) Less(y Uint64s) Mask64s
- func (x Uint64s) LessEqual(y Uint64s) Mask64s
- func (x Uint64s) Masked(mask Mask64s) Uint64s
- func (x Uint64s) Not() Uint64s
- func (x Uint64s) NotEqual(y Uint64s) Mask64s
- func (x Uint64s) Or(y Uint64s) Uint64s
- func (x Uint64s) ReshapeToUint8s() Uint8s
- func (x Uint64s) ReshapeToUint16s() Uint16s
- func (x Uint64s) ReshapeToUint32s() Uint32s
- func (x Uint64s) RotateAllLeft(dist uint64) Uint64s
- func (x Uint64s) RotateAllRight(dist uint64) Uint64s
- func (x Uint64s) ShiftAllLeft(shift uint64) Uint64s
- func (x Uint64s) ShiftAllRight(shift uint64) Uint64s
- func (x Uint64s) Store(s []uint64)
- func (x Uint64s) StorePart(s []uint64) int
- func (x Uint64s) String() string
- func (x Uint64s) Sub(y Uint64s) Uint64s
- func (x Uint64s) ToArch() any
- func (x Uint64s) Xor(y Uint64s) Uint64s
- Bugs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Emulated ¶
func Emulated() bool
Emulated returns whether simd operations are emulated or running on actual vector hardware.
func HasHardwareCarrylessMultiply ¶
func HasHardwareCarrylessMultiply() bool
HasHardwareCarrylessMultiply returns whether this platform as a hardware-implemented version of carryless multiply. With default GODEBUG=simd settings, if this is false, it is emulated and merely slow, but with non-default settings this can indicate the possibility of a missing instruction that will fail ("SIGILL") if it is executed.
func VectorBitSize ¶
func VectorBitSize() int
VectorBitSize returns the bit length of the longest vector available on the current hardware. It can be artificially reduced by setting GODEBUG=simd=<smaller size> environment variable before running a program.
Types ¶
type Float32s ¶
type Float32s struct {
// contains filtered or unexported fields
}
Float32s represents a vector of 32-bit floating-point numbers.
func BroadcastFloat32s ¶
BroadcastFloat32 fills the elements of a slice with its argument value.
func Float32sFromArch ¶
func Float32sFromArch[T archSimdFloat32s](x T) Float32s
func LoadFloat32s ¶
LoadFloat32 loads a slice of float32 into an Float32s vector.
func LoadFloat32sPart ¶
LoadFloat32Part loads a partial slice of float32 into an Float32s vector, returning the vector and the number of elements loaded.
func (Float32s) ConvertToInt32 ¶
ConvertToInt32 converts the vector elements to int32.
func (Float32s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Float32s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Float32s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.