Mojo struct
SIMD
@register_passable(trivial)
struct SIMD[type: DType, size: Int]
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
:MAX = SIMD(max_or_inf[::DType]())
: Gets the maximum value for the SIMD value, potentially +inf.MIN = SIMD(min_or_neg_inf[::DType]())
: Gets the minimum value for the SIMD value, potentially -inf.MAX_FINITE = SIMD(max_finite[::DType]())
: Returns the maximum finite value of SIMD value.MIN_FINITE = SIMD(min_finite[::DType]())
: Returns the minimum (lowest) finite value of SIMD value.
Fields
- value (
simd<#lit.struct.extract<:@stdlib::@builtin::@int::@Int size, "value">, #lit.struct.extract<:@stdlib::@builtin::@dtype::@DType type, "value">>
): The underlying storage for the vector.
Implemented traits
Absable
,
AnyType
,
Boolable
,
BoolableCollectionElement
,
CeilDivable
,
Ceilable
,
CollectionElement
,
Copyable
,
Floatable
,
Floorable
,
Hashable
,
IntLike
,
Intable
,
Movable
,
Powable
,
Representable
,
Roundable
,
Sized
,
Stringable
,
Truncable
,
UnknownDestructibility
,
Writable
,
_HashableWithHasher
Methods
__init__
__init__(out self)
Default initializer of the SIMD vector.
By default the SIMD vectors are initialized to all zeros.
__init__(out self, value: UInt)
Initializes the SIMD vector with an unsigned integer.
The unsigned integer value is splatted across all the elements of the SIMD vector.
Args:
- value (
UInt
): The input value.
__init__(out self, value: Int)
Initializes the SIMD vector with a signed integer.
The signed integer value is splatted across all the elements of the SIMD vector.
Args:
- value (
Int
): The input value.
__init__(out self, value: IntLiteral)
Initializes the SIMD vector with an integer.
The integer value is splatted across all the elements of the SIMD vector.
Args:
- value (
IntLiteral
): The input value.
__init__(out self: SIMD[bool, size], value: Bool, /)
Initializes 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.
__init__(out self, value: simd<#lit.struct.extract<:_stdlib::_builtin::_int::_Int size, "value">, #lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>)
Initializes the SIMD vector with the underlying mlir value.
Args:
- value (
simd<#lit.struct.extract<:_stdlib::_builtin::_int::_Int size, "value">, #lit.struct.extract<:_stdlib::_builtin::_dtype::_DType type, "value">>
): The input value.
__init__(out self, value: SIMD[type, 1], /)
Constructs a SIMD vector by splatting a scalar value.
The input value is splatted across all elements of the SIMD vector.
Args:
- value (
SIMD[type, 1]
): The value to splat to the elements of the vector.
__init__(out self, *elems: SIMD[type, 1])
Constructs a SIMD vector via a variadic list of elements.
The input values are assigned to the corresponding elements of the SIMD vector.
Constraints:
The number of input values is equal to size of the SIMD vector.
Args:
- *elems (
SIMD[type, 1]
): The variadic list of elements from which the SIMD vector is constructed.
__init__(out self, value: FloatLiteral)
Initializes the SIMD vector with a float.
The value is splatted across all the elements of the SIMD vector.
Args:
- value (
FloatLiteral
): The input value.
__init__[int_type: DType, //](out self, *, from_bits: SIMD[int_type, size])
Initializes the SIMD vector from the bits of an integral SIMD vector.
Parameters:
- int_type (
DType
): The integral type of the input SIMD vector.
Args:
- from_bits (
SIMD[int_type, size]
): The SIMD vector to copy the bits from.
__bool__
__bool__(self) -> Bool
Converts the SIMD scalar into a boolean value.
Constraints:
The size of the SIMD vector must be 1.
Returns:
True if the SIMD scalar is non-zero and False otherwise.
__getitem__
__getitem__(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__(mut 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.
__neg__
__neg__(self) -> Self
Defines the unary -
operation.
Returns:
The negation of this SIMD vector.
__pos__
__pos__(self) -> Self
Defines the unary +
operation.
Returns:
This SIMD vector.
__invert__
__invert__(self) -> Self
Returns ~self
.
Constraints:
The element type of the SIMD vector must be boolean or integral.
Returns:
The ~self
value.
__lt__
__lt__(self, rhs: Self) -> SIMD[bool, size]
Compares two SIMD vectors using less-than 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, rhs: Self) -> SIMD[bool, size]
Compares two SIMD vectors using less-than-or-equal 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, rhs: Self) -> SIMD[bool, size]
Compares two SIMD vectors using equal-to 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, rhs: Self) -> SIMD[bool, size]
Compares two SIMD vectors using not-equal 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]
.