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.
func (Float32s) MulAdd ¶
MulAdd returns x * y + z element-wise.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Float32s on 512-bit vector has 16 elements.
v1 := simd.LoadFloat32s([]float32{
1.5, 2.5, 3.5, 4.5,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
})
v2 := simd.LoadFloat32s([]float32{
2.0, 2.0, 2.0, 2.0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
})
v3 := simd.LoadFloat32s([]float32{
1.0, 2.0, 3.0, 4.0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
})
// Perform element-wise v1 * v2 + v3.
res := v1.MulAdd(v2, v3)
out := make([]float32, res.Len())
res.Store(out)
// Print the first 4 elements.
fmt.Println(out[:4])
}
Output: [4 7 10 13]
func (Float32s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
type Float64s ¶
type Float64s struct {
// contains filtered or unexported fields
}
Float64s represents a vector of 64-bit floating-point numbers.
func BroadcastFloat64s ¶
BroadcastFloat64 fills the elements of a slice with its argument value.
func Float64sFromArch ¶
func Float64sFromArch[T archSimdFloat64s](x T) Float64s
func LoadFloat64s ¶
LoadFloat64 loads a slice of float64 into an Float64s vector.
func LoadFloat64sPart ¶
LoadFloat64Part loads a partial slice of float64 into an Float64s vector, returning the vector and the number of elements loaded.
func (Float64s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Float64s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Float64s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Float64s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
type Int8s ¶
type Int8s struct {
// contains filtered or unexported fields
}
Int8s represents a vector of 8-bit signed integers.
func BroadcastInt8s ¶
BroadcastInt8 fills the elements of a slice with its argument value.
func Int8sFromArch ¶
func Int8sFromArch[T archSimdInt8s](x T) Int8s
func LoadInt8sPart ¶
LoadInt8Part loads a partial slice of int8 into an Int8s vector, returning the vector and the number of elements loaded.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Slice smaller than the full vector length.
s := []int8{1, 2, 3, 4, 5}
// Load partial slice.
v, n := simd.LoadInt8sPart(s)
fmt.Printf("Loaded %d elements\n", n)
// Store only the loaded elements.
out := make([]int8, n)
v.StorePart(out)
fmt.Println(out)
}
Output: Loaded 5 elements [1 2 3 4 5]
func (Int8s) Add ¶
Add returns the element-wise sum of x and y.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Initialize slice of 64 int8s (max vector size under AVX512).
in1 := []int8{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
in2 := []int8{
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 100, 10, 10, 10, 16,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}
// Load slices into vectors.
v1 := simd.LoadInt8s(in1)
v2 := simd.LoadInt8s(in2)
// Add the vectors.
sum := v1.Add(v2)
// Store the result back to a slice.
out := make([]int8, sum.Len())
sum.Store(out)
// Print the first 16 elements (minimum vector width across architectures).
fmt.Println(out[:16])
}
Output: [11 22 33 44 55 66 77 88 99 110 121 112 23 24 25 32]
func (Int8s) AddSaturated ¶
AddSaturated returns the element-wise saturated sum of x and y.
func (Int8s) ConvertToUint8 ¶
ConvertToUint8 converts the vector elements to uint8.
func (Int8s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Int8s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Int8s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Load vectors of 64 elements.
v1 := simd.LoadInt8s([]int8{
1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
})
var v2 simd.Int8s // zero value
// Create a mask where elements in v1 are greater than zero.
mask := v1.Greater(v2)
// Keep elements of v1 where the mask is true, zero out elsewhere.
res := v1.Masked(mask)
out := make([]int8, res.Len())
res.Store(out)
// Print the first 16 elements.
fmt.Println(out[:16])
}
Output: [1 0 3 0 5 0 7 0 9 0 11 0 13 0 15 0]
func (Int8s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
func (Int8s) SubSaturated ¶
SubSaturated returns the element-wise saturated difference of x and y.
type Int16s ¶
type Int16s struct {
// contains filtered or unexported fields
}
Int16s represents a vector of 16-bit signed integers.
func BroadcastInt16s ¶
BroadcastInt16 fills the elements of a slice with its argument value.
func Int16sFromArch ¶
func Int16sFromArch[T archSimdInt16s](x T) Int16s
func LoadInt16s ¶
LoadInt16 loads a slice of int16 into an Int16s vector.
func LoadInt16sPart ¶
LoadInt16Part loads a partial slice of int16 into an Int16s vector, returning the vector and the number of elements loaded.
func (Int16s) AddSaturated ¶
AddSaturated returns the element-wise saturated sum of x and y.
func (Int16s) ConvertToUint16 ¶
ConvertToUint16 converts the vector elements to uint16.
func (Int16s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Int16s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Int16s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Int16s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Int16s on 512-bit vector has 32 elements.
in := []int16{
0x00f0, 0x1234, 0, 0, 0, 0, 0, 0x7000,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}
v := simd.LoadInt16s(in)
// Rotate all elements left by 4 bits.
res := v.RotateAllLeft(4)
out := make([]int16, res.Len())
res.Store(out)
fmt.Printf("%#04x\n", out[:8])
}
Output: [0x0f00 0x2341 0x0000 0x0000 0x0000 0x0000 0x0000 0x0007]
func (Int16s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Int16s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
Example ¶
package main
import (
"fmt"
"simd"
)
func main() {
// Int16s on 512-bit vector has 32 elements.
in := []int16{
1, 2, 4, 8, 16, 32, 64, 128,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
}
v := simd.LoadInt16s(in)
// Shift all elements left by 2 bits.
res := v.ShiftAllLeft(2)
out := make([]int16, res.Len())
res.Store(out)
// Print the first 8 elements.
fmt.Println(out[:8])
}
Output: [4 8 16 32 64 128 256 512]
func (Int16s) ShiftAllRight ¶
ShiftAllRight shifts all elements right by y bits.
func (Int16s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
func (Int16s) SubSaturated ¶
SubSaturated returns the element-wise saturated difference of x and y.
type Int32s ¶
type Int32s struct {
// contains filtered or unexported fields
}
Int32s represents a vector of 32-bit signed integers.
func BroadcastInt32s ¶
BroadcastInt32 fills the elements of a slice with its argument value.
func Int32sFromArch ¶
func Int32sFromArch[T archSimdInt32s](x T) Int32s
func LoadInt32s ¶
LoadInt32 loads a slice of int32 into an Int32s vector.
func LoadInt32sPart ¶
LoadInt32Part loads a partial slice of int32 into an Int32s vector, returning the vector and the number of elements loaded.
func (Int32s) ConvertToFloat32 ¶
ConvertToFloat32 converts the vector elements to float32.
func (Int32s) ConvertToUint32 ¶
ConvertToUint32 converts the vector elements to uint32.
func (Int32s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Int32s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Int32s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Int32s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
func (Int32s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Int32s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
func (Int32s) ShiftAllRight ¶
ShiftAllRight shifts all elements right by y bits.
func (Int32s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
type Int64s ¶
type Int64s struct {
// contains filtered or unexported fields
}
Int64s represents a vector of 64-bit signed integers.
func BroadcastInt64s ¶
BroadcastInt64 fills the elements of a slice with its argument value.
func Int64sFromArch ¶
func Int64sFromArch[T archSimdInt64s](x T) Int64s
func LoadInt64s ¶
LoadInt64 loads a slice of int64 into an Int64s vector.
func LoadInt64sPart ¶
LoadInt64Part loads a partial slice of int64 into an Int64s vector, returning the vector and the number of elements loaded.
func (Int64s) ConvertToUint64 ¶
ConvertToUint64 converts the vector elements to uint64.
func (Int64s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Int64s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Int64s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Int64s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
func (Int64s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Int64s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
func (Int64s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
type Mask8s ¶
type Mask8s struct {
// contains filtered or unexported fields
}
Mask8s represents a boolean mask for Int8s/Uint8s vectors.
func Mask8sFromArch ¶
func Mask8sFromArch[T archSimdMask8s](x T) Mask8s
type Mask16s ¶
type Mask16s struct {
// contains filtered or unexported fields
}
Mask16s represents a boolean mask for Int16s/Uint16s vectors.
func Mask16sFromArch ¶
func Mask16sFromArch[T archSimdMask16s](x T) Mask16s
type Mask32s ¶
type Mask32s struct {
// contains filtered or unexported fields
}
Mask32s represents a boolean mask for Int32s/Uint32s vectors.
func Mask32sFromArch ¶
func Mask32sFromArch[T archSimdMask32s](x T) Mask32s
type Mask64s ¶
type Mask64s struct {
// contains filtered or unexported fields
}
Mask64s represents a boolean mask for Int64s/Uint64s vectors.
func Mask64sFromArch ¶
func Mask64sFromArch[T archSimdMask64s](x T) Mask64s
type Uint8s ¶
type Uint8s struct {
// contains filtered or unexported fields
}
Uint8s represents a vector of 8-bit unsigned integers.
func BroadcastUint8s ¶
BroadcastUint8 fills the elements of a slice with its argument value.
func LoadUint8s ¶
LoadUint8 loads a slice of uint8 into an Uint8s vector.
func LoadUint8sPart ¶
LoadUint8Part loads a partial slice of uint8 into an Uint8s vector, returning the vector and the number of elements loaded.
func Uint8sFromArch ¶
func Uint8sFromArch[T archSimdUint8s](x T) Uint8s
func (Uint8s) AddSaturated ¶
AddSaturated returns the element-wise saturated sum of x and y.
func (Uint8s) BitsToInt8 ¶
BitsToInt8 reinterprets the vector bits as an Int8s vector.
func (Uint8s) ConvertToInt8 ¶
ConvertToInt8 converts the vector elements to int8.
func (Uint8s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Uint8s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Uint8s) ReshapeToUint16s ¶
ReshapeToUint16s reinterprets the vector bits as a Uint16s vector.
func (Uint8s) ReshapeToUint32s ¶
ReshapeToUint32s reinterprets the vector bits as a Uint32s vector.
func (Uint8s) ReshapeToUint64s ¶
ReshapeToUint64s reinterprets the vector bits as a Uint64s vector.
func (Uint8s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
func (Uint8s) SubSaturated ¶
SubSaturated returns the element-wise saturated difference of x and y.
type Uint16s ¶
type Uint16s struct {
// contains filtered or unexported fields
}
Uint16s represents a vector of 16-bit unsigned integers.
func BroadcastUint16s ¶
BroadcastUint16 fills the elements of a slice with its argument value.
func LoadUint16s ¶
LoadUint16 loads a slice of uint16 into an Uint16s vector.
func LoadUint16sPart ¶
LoadUint16Part loads a partial slice of uint16 into an Uint16s vector, returning the vector and the number of elements loaded.
func Uint16sFromArch ¶
func Uint16sFromArch[T archSimdUint16s](x T) Uint16s
func (Uint16s) AddSaturated ¶
AddSaturated returns the element-wise saturated sum of x and y.
func (Uint16s) BitsToInt16 ¶
BitsToInt16 reinterprets the vector bits as an Int16s vector.
func (Uint16s) ConvertToInt16 ¶
ConvertToInt16 converts the vector elements to int16.
func (Uint16s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Uint16s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Uint16s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Uint16s) ReshapeToUint8s ¶
ReshapeToUint8s reinterprets the vector bits as a Uint8s vector.
func (Uint16s) ReshapeToUint32s ¶
ReshapeToUint32s reinterprets the vector bits as a Uint32s vector.
func (Uint16s) ReshapeToUint64s ¶
ReshapeToUint64s reinterprets the vector bits as a Uint64s vector.
func (Uint16s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
func (Uint16s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Uint16s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
func (Uint16s) ShiftAllRight ¶
ShiftAllRight shifts all elements right by y bits.
func (Uint16s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
func (Uint16s) SubSaturated ¶
SubSaturated returns the element-wise saturated difference of x and y.
type Uint32s ¶
type Uint32s struct {
// contains filtered or unexported fields
}
Uint32s represents a vector of 32-bit unsigned integers.
func BroadcastUint32s ¶
BroadcastUint32 fills the elements of a slice with its argument value.
func LoadUint32s ¶
LoadUint32 loads a slice of uint32 into an Uint32s vector.
func LoadUint32sPart ¶
LoadUint32Part loads a partial slice of uint32 into an Uint32s vector, returning the vector and the number of elements loaded.
func Uint32sFromArch ¶
func Uint32sFromArch[T archSimdUint32s](x T) Uint32s
func (Uint32s) BitsToFloat32 ¶
BitsToFloat32 reinterprets the vector bits as a Float32s vector.
func (Uint32s) BitsToInt32 ¶
BitsToInt32 reinterprets the vector bits as an Int32s vector.
func (Uint32s) ConvertToInt32 ¶
ConvertToInt32 converts the vector elements to int32.
func (Uint32s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Uint32s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Uint32s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Uint32s) ReshapeToUint8s ¶
ReshapeToUint8s reinterprets the vector bits as a Uint8s vector.
func (Uint32s) ReshapeToUint16s ¶
ReshapeToUint16s reinterprets the vector bits as a Uint16s vector.
func (Uint32s) ReshapeToUint64s ¶
ReshapeToUint64s reinterprets the vector bits as a Uint64s vector.
func (Uint32s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
func (Uint32s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Uint32s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
func (Uint32s) ShiftAllRight ¶
ShiftAllRight shifts all elements right by y bits.
func (Uint32s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
type Uint64s ¶
type Uint64s struct {
// contains filtered or unexported fields
}
Uint64s represents a vector of 64-bit unsigned integers.
func BroadcastUint64s ¶
BroadcastUint64 fills the elements of a slice with its argument value.
func LoadUint64s ¶
LoadUint64 loads a slice of uint64 into an Uint64s vector.
func LoadUint64sPart ¶
LoadUint64Part loads a partial slice of uint64 into an Uint64s vector, returning the vector and the number of elements loaded.
func Uint64sFromArch ¶
func Uint64sFromArch[T archSimdUint64s](x T) Uint64s
func (Uint64s) BitsToFloat64 ¶
BitsToFloat64 reinterprets the vector bits as a Float64s vector.
func (Uint64s) BitsToInt64 ¶
BitsToInt64 reinterprets the vector bits as an Int64s vector.
func (Uint64s) CarrylessMultiplyEven ¶
CarrylessMultiplyOdd computes the carryless multiplications of selected even indexed elements of x and y. Each product is 128 bits wide and fills the corresponding even-odd pairs in the result.
A carryless multiplication uses bitwise XOR instead of add-with-carry, for example (in base two):
11 * 11 = 11 * (10 ^ 1) = (11 * 10) ^ (11 * 1) = 110 ^ 11 = 101
This also models multiplication of polynomials with coefficients from GF(2) -- 11 * 11 models (x+1)*(x+1) = x**2 + (1^1)x + 1 = x**2 + 0x + 1 = x**2 + 1 modeled by 101. (Note that "+" adds polynomial terms, but coefficients "add" with XOR.)"
func (Uint64s) CarrylessMultiplyOdd ¶
CarrylessMultiplyOdd computes the carryless multiplications of selected odd indexed elements of x and y. Each product is 128 bits wide and fills the corresponding even-odd pairs in the result.
A carryless multiplication uses bitwise XOR instead of add-with-carry, for example (in base two):
11 * 11 = 11 * (10 ^ 1) = (11 * 10) ^ (11 * 1) = 110 ^ 11 = 101
This also models multiplication of polynomials with coefficients from GF(2) -- 11 * 11 models (x+1)*(x+1) = x**2 + (1^1)x + 1 = x**2 + 0x + 1 = x**2 + 1 modeled by 101. (Note that "+" adds polynomial terms, but coefficients "add" with XOR.)"
func (Uint64s) ConvertToInt64 ¶
ConvertToInt64 converts the vector elements to int64.
func (Uint64s) GreaterEqual ¶
GreaterEqual returns a mask indicating where x is greater than or equal to y.
func (Uint64s) IfElse ¶
IfElse returns a new vector with elements from x where mask is true, and y where mask is false.
func (Uint64s) Masked ¶
Masked returns a new vector with elements from x where mask is true, and zero elsewhere.
func (Uint64s) ReshapeToUint8s ¶
ReshapeToUint8s reinterprets the vector bits as a Uint8s vector.
func (Uint64s) ReshapeToUint16s ¶
ReshapeToUint16s reinterprets the vector bits as a Uint16s vector.
func (Uint64s) ReshapeToUint32s ¶
ReshapeToUint32s reinterprets the vector bits as a Uint32s vector.
func (Uint64s) RotateAllLeft ¶
RotatesAllLeft rotates all elements left by y bits.
func (Uint64s) RotateAllRight ¶
RotatesAllRight rotates all elements right by y bits.
func (Uint64s) ShiftAllLeft ¶
ShiftAllLeft shifts all elements left by y bits.
func (Uint64s) ShiftAllRight ¶
ShiftAllRight shifts all elements right by y bits.
func (Uint64s) StorePart ¶
StorePart stores a partial vector into the slice s and returns the number actually stored.
Notes ¶
Bugs ¶
Calls won't work, and there may be other bugs.
SIMD-dependent var initializers don't work.
Modified names may appear in stack traces and debugging.