SIMD
Module
Implements SIMD struct.
Aliases:
F16 = SIMD[f16, 1]
F32 = SIMD[f32, 1]
F64 = SIMD[f64, 1]
F8 = SIMD[f8, 1]
SI16 = SIMD[si16, 1]
SI32 = SIMD[si32, 1]
SI64 = SIMD[si64, 1]
SI8 = SIMD[si8, 1]
UI16 = SIMD[ui16, 1]
UI32 = SIMD[ui32, 1]
UI64 = SIMD[ui64, 1]
UI8 = SIMD[ui8, 1]
SIMD
Represents a small vector that is backed by a hardware vector element.
SIMD allows a single instruction to be executed across the multiple data elements of the vector.
Constraints:
The size of the SIMD vector to be positive and a power of 2.
Parameters:
- type (
DType
): The data type of SIMD vector elements. - size (
Int
): The size of the SIMD vector.
Aliases:
element_type = type
Fields:
value
The underlying storage for the vector.
Functions:
__init__
__init__() -> Self
Default initializer of the SIMD vector.
By default the SIMD vectors are initialized to all zeros.
Returns:
SIMD vector whose elements are 0.
__init__(value: Int) -> Self
Initialize the SIMD vector with an integral value.
The integer value is splatted across all the elements of the SIMD vector.
Args:
- value (
Int
): The input value.
Returns:
SIMD vector whose elements have the specified value.
__init__(value: Bool) -> Self
Initialize the SIMD vector with a bool value.
The bool value is splatted across all elements of the SIMD vector.
Args:
- value (
Bool
): The bool value.
Returns:
SIMD vector whose elements have the specified value.
__init__(value: simd<apply(:<>(!kgen.declref<_"$Int"::_Int> borrow) -> index _"$Int"::_Int::_"__as_mlir_index($Int::Int)", size), #lit.struct.extract<:_"$DType"::_DType type, "value">>) -> Self
Initialize the SIMD vector with the underlying mlir value.
Args:
- value (
simd<apply(:<>(!kgen.declref<_"$Int"::_Int> borrow) -> index _"$Int"::_Int::_"__as_mlir_index($Int::Int)", size), #lit.struct.extract<:_"$DType"::_DType type, "value">>
): The input value.
Returns:
SIMD vector using the specified value.
__init__(*elems: SIMD[type, 1]) -> Self
Construct a SIMD vector via a variadic list of elements.
If there is just one input value, then it is splatted to all elements of the SIMD vector. Otherwise, the input values are assigned to the corresponding elements of the SIMD vector.
Constraints:
The number of input values is 1 or equal to size of the SIMD vector.
Args:
- elems (
*SIMD[type, 1]
): The variadic list of elements from which the SIMD vector is constructed.
Returns:
The constructed SIMD vector.
__init__(value: FloatLiteral) -> Self
Initialize the SIMD vector with a FP64 value.
The input value is splatted across all the elements of the SIMD vector.
Constraints:
The SIMD element type must be of floating point type.
Args:
- value (
FloatLiteral
): The input value.
Returns:
A SIMD vector whose elements have the specified value.
__bool__
__bool__(self: Self) -> Bool
Converts the SIMD vector into a boolean scalar value.
Returns:
True if all the elements in the SIMD vector are non-zero and False otherwise.
__getitem__
__getitem__(self: Self, idx: Int) -> SIMD[type, 1]
Gets an element from the vector.
Args:
- idx (
Int
): The element index.
Returns:
The value at position idx
.
__setitem__
__setitem__(self: Self&, idx: Int, val: SIMD[type, 1])
Sets an element in the vector.
Args:
- idx (
Int
): The index to set. - val (
SIMD[type, 1]
): The value to set.
__setitem__(self: Self&, idx: Int, val: scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>)
Sets an element in the vector.
Args:
- idx (
Int
): The index to set. - val (
scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>
): The value to set.
__neg__
__neg__(self: Self) -> Self
The unary -
operation.
Returns:
The negation of this SIMD vector.
__pos__
__pos__(self: Self) -> Self
The unary +
operation.
Returns:
This SIMD vector.
__invert__
__invert__(self: Self) -> Self
Return ~self
.
Constraints:
The element type of the SIMD vector must be integral.
Returns:
The ~self
value.
__lt__
__lt__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using LT comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] < rhs[i]
.
__le__
__le__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using LE comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] <= rhs[i]
.
__eq__
__eq__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using EQ comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] == rhs[i]
.
__ne__
__ne__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using NE comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] != rhs[i]
.
__gt__
__gt__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using GT comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] > rhs[i]
.
__ge__
__ge__(self: Self, rhs: Self) -> SIMD[bool, size]
Compare two SIMD vectors using GE comparison.
Args:
- rhs (
Self
): The rhs of the operation.
Returns:
A new bool SIMD vector of the same size whose element at position i
is True or False depending on the expression self[i] >= rhs[i]
.
__add__
__add__(self: Self, rhs: Self) -> Self
Computes self + rhs
.
Args:
- rhs (
Self
): The rhs value.
Returns:
A new vector whose element at position i
is computed as self[i] + rhs[i]
.
__sub__
__sub__(self: Self, rhs: Self) -> Self
Computes self - rhs
.
Args:
- rhs (
Self
): The rhs value.
Returns:
A new vector whose element at position i
is computed as self[i] - rhs[i]
.
__mul__
__mul__(self: Self, rhs: Self) -> Self
Computes self * rhs
.
Args:
- rhs (
Self
): The rhs value.
Returns:
A new vector whose element at position i
is computed as self[i] * rhs[i]
.
__truediv__
__truediv__(self: Self, rhs: Self) -> Self
Computes self / rhs
.
Args:
- rhs (
Self
): The rhs value.
Returns:
A new vector whose element at position i
is computed as self[i] / rhs[i]
.
__floordiv__
__floordiv__(self: Self, rhs: Self) -> Self
Return the division of self and rhs rounded down to the nearest integer.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The value to divide on.
Returns:
floor(self / rhs)
value.
__mod__
__mod__(self: Self, rhs: Self) -> Self
Return the remainder of self divided by rhs.
Args:
- rhs (
Self
): The value to divide on.
Returns:
The remainder of dividing self by rhs.
__pow__
__pow__(self: Self, rhs: Int) -> Self
Computes the vector raised to the power of the input integer value.
Args:
- rhs (
Int
): The exponential value.
Returns:
A SIMD vector where each element is raised to the power of the specified exponential value.
__lshift__
__lshift__(self: Self, rhs: Self) -> Self
Return self << rhs
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
Returns:
self << rhs
.
__rshift__
__rshift__(self: Self, rhs: Self) -> Self
Return self >> rhs
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
Returns:
self >> rhs
.
__and__
__and__(self: Self, rhs: Self) -> Self
Return self & rhs
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- rhs (
Self
): The RHS value.
Returns:
self & rhs
.
__or__
__or__(self: Self, rhs: Self) -> Self
Return self | rhs
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- rhs (
Self
): The RHS value.
Returns:
self | rhs
.
__xor__
__xor__(self: Self, rhs: Self) -> Self
Return self ^ rhs
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
Returns:
self ^ rhs
.
__radd__
__radd__(self: Self, value: Self) -> Self
Return value + self
.
Args:
- value (
Self
): The other value.
Returns:
value + self
.
__rsub__
__rsub__(self: Self, value: Self) -> Self
Return value - self
.
Args:
- value (
Self
): The other value.
Returns:
value - self
.
__rmul__
__rmul__(self: Self, value: Self) -> Self
Return value * self
.
Args:
- value (
Self
): The other value.
Returns:
value * self
.
__rtruediv__
__rtruediv__(self: Self, value: Self) -> Self
Return value / self
.
Args:
- value (
Self
): The other value.
Returns:
value / self
.
__rfloordiv__
__rfloordiv__(self: Self, value: Int) -> Int
Return value // self
.
Args:
- value (
Int
): The other value.
Returns:
value // self
.
__rmod__
__rmod__(self: Self, value: Int) -> Int
Return value % self
.
Args:
- value (
Int
): The other value.
Returns:
value % self
.
__rlshift__
__rlshift__(self: Self, value: Self) -> Self
Return value << self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- value (
Self
): The other value.
Returns:
value << self
.
__rrshift__
__rrshift__(self: Self, value: Self) -> Self
Return value >> self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- value (
Self
): The other value.
Returns:
value >> self
.
__rand__
__rand__(self: Self, value: Self) -> Self
Return value & self
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- value (
Self
): The other value.
Returns:
value & self
.
__ror__
__ror__(self: Self, value: Self) -> Self
Return value | self
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- value (
Self
): The other value.
Returns:
value | self
.
__rxor__
__rxor__(self: Self, value: Self) -> Self
Return value ^ self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- value (
Self
): The other value.
Returns:
value ^ self
.
__iadd__
__iadd__(self: Self&, rhs: Self)
Inplace add operation.
The vector is mutated where each element at position i
is computed as self[i] + rhs[i]
.
Args:
- rhs (
Self
): The rhs of the addition operation.
__isub__
__isub__(self: Self&, rhs: Self)
Inplace sub operation.
The vector is mutated where each element at position i
is computed as self[i] - rhs[i]
.
Args:
- rhs (
Self
): The rhs of the operation.
__imul__
__imul__(self: Self&, rhs: Self)
Inplace mul operation.
The vector is mutated where each element at position i
is computed as self[i] * rhs[i]
.
Args:
- rhs (
Self
): The rhs of the operation.
__itruediv__
__itruediv__(self: Self&, rhs: Self)
Inplace true divide operation.
The vector is mutated where each element at position i
is computed as self[i] / rhs[i]
.
Args:
- rhs (
Self
): The rhs of the operation.
__ifloordiv__
__ifloordiv__(self: Self&, rhs: Self)
Inplace flood div operation.
The vector is mutated where each element at position i
is computed as self[i] // rhs[i]
.
Args:
- rhs (
Self
): The rhs of the operation.
__imod__
__imod__(self: Self&, rhs: Self)
Inplace mod operation.
The vector is mutated where each element at position i
is computed as self[i] % rhs[i]
.
Args:
- rhs (
Self
): The rhs of the operation.
__ipow__
__ipow__(self: Self&, rhs: Int)
Inplace pow operation.
The vector is mutated where each element at position i
is computed as pow(self[i], rhs)
.
Args:
- rhs (
Int
): The rhs of the operation.
__ilshift__
__ilshift__(self: Self&, rhs: Self)
Compute self << rhs
and save the result in self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
__irshift__
__irshift__(self: Self&, rhs: Self)
Compute self >> rhs
and save the result in self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
__iand__
__iand__(self: Self&, rhs: Self)
Compute self & rhs
and save the result in self
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- rhs (
Self
): The RHS value.
__ixor__
__ixor__(self: Self&, rhs: Self)
Compute self ^ rhs
and save the result in self
.
Constraints:
The element type of the SIMD vector must be integral.
Args:
- rhs (
Self
): The RHS value.
__ior__
__ior__(self: Self&, rhs: Self)
Compute self | rhs
and save the result in self
.
Constraints:
The element type of the SIMD vector must be bool or integral.
Args:
- rhs (
Self
): The RHS value.
cast
cast[target: DType](self: Self) -> SIMD[target, size]
Casts the elements of the SIMD vector to the target element type.
Parameters:
- target (
DType
): The target DType.
Returns:
A new SIMD vector whose elements have been casted to the target element type.
fma
fma(self: Self, multiplier: Self, accumulator: Self) -> Self
Performs a fused multiply-add operation, i.e. self*multiplier + accumulator
.
Args:
- multiplier (
Self
): The value to multiply. - accumulator (
Self
): The value to accumulate.
Returns:
A new vector whose element at position i
is computed as self[i]*multiplier[i] + accumulator[i]
.
max
max(self: Self, other: Self) -> Self
Computes the elementwise maximum between the two vectors.
Args:
- other (
Self
): The other SIMD vector.
Returns:
A new SIMD vector where each element at position i
is max(self[i], other[i])
.
min
min(self: Self, other: Self) -> Self
Computes the elementwise minimum between the two vectors.
Args:
- other (
Self
): The other SIMD vector.
Returns:
A new SIMD vector where each element at position i
is min(self[i], other[i])
.
reduce_add
reduce_add(self: Self) -> SIMD[type, 1]
Reduces the vector using the add
operator.
Returns:
The sum of all vector elements.
reduce_and
reduce_and(self: Self) -> Bool
Reduces the boolean vector using the and
operator.
Constraints:
The element type of the vector must be boolean.
Returns:
True if all element in the vector is True and False otherwise.
reduce_max
reduce_max(self: Self) -> SIMD[type, 1]
Reduces the vector using the max
operator.
Constraints:
The element type of the vector must be integer or FP.
Returns:
The maximum element of the vector.
reduce_min
reduce_min(self: Self) -> SIMD[type, 1]
Reduces the vector using the min
operator.
Constraints:
The element type of the vector must be integer or FP.
Returns:
The minimum element of the vector.
reduce_mul
reduce_mul(self: Self) -> SIMD[type, 1]
Reduces the vector using the mul
operator.
Constraints:
The element type of the vector must be integer or FP.
Returns:
The product of all vector elements.
reduce_or
reduce_or(self: Self) -> Bool
Reduces the boolean vector using the or
operator.
Constraints:
The element type of the vector must be boolean.
Returns:
True if any element in the vector is True and False otherwise.
select
select[result_type: DType](self: Self, true_case: SIMD[result_type, size], false_case: SIMD[result_type, size]) -> SIMD[result_type, size]
Selects the values of the true_case or the false_case based on the current boolean values of the SIMD vector.
Parameters:
- result_type (
DType
): The element type of the input and output SIMD vectors.
Args:
- true_case (
SIMD[result_type, size]
): The values selected if the positional value is True. - false_case (
SIMD[result_type, size]
): The values selected if the positional value is False.
Returns:
A new vector of the form [true_case[i] if elem else false_case[i] in enumerate(self)]
.
shuffle
shuffle[*mask: Int](self: Self, other: Self) -> Self
Shuffles (also called blend) the values of the current vector with the other
value using the specified mask (permutation).
Parameters:
- mask (
*Int
): The permutation to use in the shuffle.
Args:
- other (
Self
): The other vector to shuffle with.
Returns:
A new vector of length len
where the value at position i
is (self+other)[permutation[i]]
.
shuffle_list
shuffle_list[mask: VariadicList[Int]](self: Self, other: Self) -> Self
Shuffles (also called blend) the values of the current vector with the other
value using the specified mask (permutation).
Parameters:
- mask (
VariadicList[Int]
): The permutation to use in the shuffle.
Args:
- other (
Self
): The other vector to shuffle with.
Returns:
A new vector of length len
where the value at position i
is (self+other)[permutation[i]]
.
slice
slice[output_width: Int](self: Self, offset: Int) -> SIMD[type, output_width]
Returns a slice of the vector of the specified width with the given offset.
Constraints:
output_width + offset
must not exceed the size of this SIMD vector.
Parameters:
- output_width (
Int
): The output SIMD vector size.
Args:
- offset (
Int
): The given offset for the slice.
Returns:
A new vector whose elements map to self[offset:offset+output_width]
.
splat
splat(x: scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>) -> Self
Splats (also known as broadcasts) the element onto the vector.
Args:
- x (
scalar<#lit.struct.extract<:_"$DType"::_DType type, "value">>
): The input value.
Returns:
A new SIMD vector whose elements are the same as the input value.
splat(x: Bool) -> Self
Splats (also known as broadcasts) the element onto the vector.
Args:
- x (
Bool
): The input value.
Returns:
A new SIMD vector whose elements are the same as the input value.
splat(x: SIMD[type, 1]) -> Self
Splats (also known as broadcasts) the element onto the vector.
Args:
- x (
SIMD[type, 1]
): The input scalar value.
Returns:
A new SIMD vector whose elements are the same as the input value.
to_int
to_int(self: Self) -> Int
Casts to Int.
Constraints:
The size of the SIMD vector must be 1. The element type of the SIMD vector must be integral.
Returns:
The value of the single integer element in the SIMD vector.