Skip to main content

Mojo struct

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:
  • 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, 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__(inout 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].

__gt__

__gt__(self, rhs: Self) -> SIMD[bool, size]

Compares two SIMD vectors using greater-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].

__ge__

__ge__(self, rhs: Self) -> SIMD[bool, size]

Compares two SIMD vectors using greater-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].

__contains__

__contains__(self, value: SIMD[type, 1]) -> Bool

Whether the vector contains the value.

Args:

  • value (SIMD[type, 1]): The value.

Returns:

Whether the vector contains the value.

__add__

__add__(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, 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, 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, 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, rhs: Self) -> Self

Returns the division of self and rhs rounded down to the nearest integer.

Constraints:

The element type of the SIMD vector must be numeric.

Args:

  • rhs (Self): The value to divide with.

Returns:

floor(self / rhs) value.

__mod__

__mod__(self, rhs: Self) -> Self

Returns 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, exp: Int) -> Self

Computes the vector raised to the power of the input integer value.

Args:

  • exp (Int): The exponent value.

Returns:

A SIMD vector where each element is raised to the power of the specified exponent value.

__pow__(self, exp: Self) -> Self

Computes the vector raised elementwise to the right hand side power.

Args:

  • exp (Self): The exponent value.

Returns:

A SIMD vector where each element is raised to the power of the specified exponent value.

__lshift__

__lshift__(self, rhs: Self) -> Self

Returns 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, rhs: Self) -> Self

Returns 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, rhs: Self) -> Self

Returns 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, rhs: Self) -> Self

Returns 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, rhs: Self) -> Self

Returns self ^ rhs.

Constraints:

The element type of the SIMD vector must be bool or integral.

Args:

  • rhs (Self): The RHS value.

Returns:

self ^ rhs.

__radd__

__radd__(self, value: Self) -> Self

Returns value + self.

Args:

  • value (Self): The other value.

Returns:

value + self.

__rsub__

__rsub__(self, value: Self) -> Self

Returns value - self.

Args:

  • value (Self): The other value.

Returns:

value - self.

__rmul__

__rmul__(self, value: Self) -> Self

Returns value * self.

Args:

  • value (Self): The other value.

Returns:

value * self.

__rtruediv__

__rtruediv__(self, value: Self) -> Self

Returns value / self.

Args:

  • value (Self): The other value.

Returns:

value / self.

__rfloordiv__

__rfloordiv__(self, rhs: Self) -> Self

Returns the division of rhs and self rounded down to the nearest integer.

Constraints:

The element type of the SIMD vector must be numeric.

Args:

  • rhs (Self): The value to divide by self.

Returns:

floor(rhs / self) value.

__rmod__

__rmod__(self, value: Self) -> Self

Returns value mod self.

Args:

  • value (Self): The other value.

Returns:

value mod self.

__rpow__

__rpow__(self, base: Self) -> Self

Returns base ** self.

Args:

  • base (Self): The base value.

Returns:

base ** self.

__rlshift__

__rlshift__(self, value: Self) -> Self

Returns 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, value: Self) -> Self

Returns 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, value: Self) -> Self

Returns 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, value: Self) -> Self

Returns 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, value: Self) -> Self

Returns value ^ self.

Constraints:

The element type of the SIMD vector must be bool or integral.

Args:

  • value (Self): The other value.

Returns:

value ^ self.

__iadd__

__iadd__(inout self, rhs: Self)

Performs in-place addition.

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__(inout self, rhs: Self)

Performs in-place subtraction.

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__(inout self, rhs: Self)

Performs in-place multiplication.

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__(inout self, rhs: Self)

In-place true divide operator.

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__(inout self, rhs: Self)

In-place flood div operator.

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__(inout self, rhs: Self)

In-place mod operator.

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__(inout self, rhs: Int)

In-place pow operator.

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__(inout self, rhs: Self)

Computes 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__(inout self, rhs: Self)

Computes 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__(inout self, rhs: Self)

Computes 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__(inout self, rhs: Self)

Computes 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.

__ior__

__ior__(inout self, rhs: Self)

Computes 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.

__len__

__len__(self) -> Int

Gets the length of the SIMD vector.

Returns:

The length of the SIMD vector.

__int__

__int__(self) -> Int

Casts to the value to an Int. If there is a fractional component, then the fractional part is truncated.

Constraints:

The size of the SIMD vector must be 1.

Returns:

The value as an integer.

__mlir_index__

__mlir_index__(self) -> index

Convert to index.

Returns:

The corresponding __mlir_type.index value.

__float__

__float__(self) -> SIMD[float64, 1]

Casts the value to a float.

Constraints:

The size of the SIMD vector must be 1.

Returns:

The value as a float.

__str__

__str__(self) -> String

Get the SIMD as a string.

Returns:

A string representation.

__repr__

__repr__(self) -> String

Get the representation of the SIMD value e.g. "SIMD[DType.int8, 2](1, 2)".

Returns:

The representation of the SIMD value.

__floor__

__floor__(self) -> Self

Performs elementwise floor on the elements of a SIMD vector.

Returns:

The elementwise floor of this SIMD vector.

__ceil__

__ceil__(self) -> Self

Performs elementwise ceiling on the elements of a SIMD vector.

Returns:

The elementwise ceiling of this SIMD vector.

__trunc__

__trunc__(self) -> Self

Performs elementwise truncation on the elements of a SIMD vector.

Returns:

The elementwise truncated values of this SIMD vector.

__abs__

__abs__(self) -> Self

Defines the absolute value operation.

Returns:

The absolute value of this SIMD vector.

__round__

__round__(self) -> Self

Performs elementwise rounding on the elements of a SIMD vector.

This rounding goes to the nearest integer with ties away from zero.

Returns:

The elementwise rounded value of this SIMD vector.

__round__(self, ndigits: Int) -> Self

Performs elementwise rounding on the elements of a SIMD vector.

This rounding goes to the nearest integer with ties away from zero.

Args:

  • ndigits (Int): The number of digits to round to.

Returns:

The elementwise rounded value of this SIMD vector.

__hash__

__hash__(self) -> UInt

Hash the value using builtin hash.

Returns:

A 64-bit hash value. This value is not suitable for cryptographic uses. Its intended usage is for data structures. See the hash builtin documentation for more details.

__hash__[H: _Hasher](self, inout hasher: H)

Updates hasher with this SIMD value.

Parameters:

  • H (_Hasher): The hasher type.

Args:

  • hasher (H): The hasher instance.

__ceildiv__

__ceildiv__(self, denominator: Self) -> Self

Return the rounded-up result of dividing self by denominator.

Args:

  • denominator (Self): The denominator.

Returns:

The ceiling of dividing numerator by denominator.

cast

cast[target: DType](self) -> SIMD[$0, 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.

write_to

write_to[W: Writer](self, inout writer: W)

Formats this SIMD value to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.

to_bits

to_bits[int_dtype: DType = _integral_type_of[::DType]()](self) -> SIMD[$0, size]

Bitcasts the SIMD vector to an integer SIMD vector.

Parameters:

  • int_dtype (DType): The integer type to cast to.

Returns:

An integer representation of the floating-point value.

clamp

clamp(self, lower_bound: Self, upper_bound: Self) -> Self

Clamps the values in a SIMD vector to be in a certain range.

Clamp cuts values in the input SIMD vector off at the upper bound and lower bound values. For example, SIMD vector [0, 1, 2, 3] clamped to a lower bound of 1 and an upper bound of 2 would return [1, 1, 2, 2].

Args:

  • lower_bound (Self): Minimum of the range to clamp to.
  • upper_bound (Self): Maximum of the range to clamp to.

Returns:

A new SIMD vector containing x clamped to be within lower_bound and upper_bound.

roundeven

roundeven(self) -> Self

Performs elementwise banker's rounding on the elements of a SIMD vector.

This rounding goes to the nearest integer with ties toward the nearest even integer.

Returns:

The elementwise banker's rounding of this SIMD vector.

fma

fma(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].

shuffle

shuffle[*mask: Int](self) -> Self

Shuffles (also called blend) the values of the current vector with the other value using the specified mask (permutation). The mask values must be within 2 * len(self).

Parameters:

  • *mask (Int): The permutation to use in the shuffle.

Returns:

A new vector with the same length as the mask where the value at position i is (self)[permutation[i]].

shuffle[*mask: Int](self, other: Self) -> Self

Shuffles (also called blend) the values of the current vector with the other value using the specified mask (permutation). The mask values must be within 2 * len(self).

Parameters:

  • *mask (Int): The permutation to use in the shuffle.

Args:

  • other (Self): The other vector to shuffle with.

Returns:

A new vector with the same length as the mask where the value at position i is (self + other)[permutation[i]].

shuffle[: Bool, : Int, //, mask: IndexList[size, element_bitwidth=$1, unsigned=$0]](self) -> Self

Shuffles (also called blend) the values of the current vector with the other value using the specified mask (permutation). The mask values must be within 2 * len(self).

Parameters:

  • mask (IndexList[size, element_bitwidth=$1, unsigned=$0]): The permutation to use in the shuffle.

Returns:

A new vector with the same length as the mask where the value at position i is (self)[permutation[i]].

shuffle[: Bool, : Int, //, mask: IndexList[size, element_bitwidth=$1, unsigned=$0]](self, other: Self) -> Self

Shuffles (also called blend) the values of the current vector with the other value using the specified mask (permutation). The mask values must be within 2 * len(self).

Parameters:

  • mask (IndexList[size, element_bitwidth=$1, unsigned=$0]): The permutation to use in the shuffle.

Args:

  • other (Self): The other vector to shuffle with.

Returns:

A new vector with the same length as the mask where the value at position i is (self + other)[permutation[i]].

slice

slice[output_width: Int, /, *, offset: Int = 0](self) -> SIMD[type, $0]

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.
  • offset (Int): The given offset for the slice.

Returns:

A new vector whose elements map to self[offset:offset+output_width].

insert

insert[*, offset: Int = 0](self, value: SIMD[type, size]) -> Self

Returns a new vector where the elements between offset and offset + input_width have been replaced with the elements in value.

Parameters:

  • offset (Int): The offset to insert at.

Args:

  • value (SIMD[type, size]): The value to be inserted.

Returns:

A new vector whose elements at self[offset:offset+input_width] contain the values of value.

join

join(self, other: Self) -> SIMD[type, 2.__mul__(size)]

Concatenates the two vectors together.

Args:

  • other (Self): The other SIMD vector.

Returns:

A new vector self_0, self_1, ..., self_n, other_0, ..., other_n.

interleave

interleave(self, other: Self) -> SIMD[type, 2.__mul__(size)]

Constructs a vector by interleaving two input vectors.

Args:

  • other (Self): The other SIMD vector.

Returns:

A new vector self_0, other_0, ..., self_n, other_n.

split

split(self) -> Tuple[SIMD[type, size.__floordiv__(2)], SIMD[type, size.__floordiv__(2)]]

Splits the SIMD vector into 2 subvectors.

Returns:

A new vector self_0:N/2, self_N/2:N.

deinterleave

deinterleave(self) -> Tuple[SIMD[type, size.__floordiv__(2)], SIMD[type, size.__floordiv__(2)]]

Constructs two vectors by deinterleaving the even and odd lanes of the vector.

Constraints:

The vector size must be greater than 1.

Returns:

Two vectors the first of the form self_0, self_2, ..., self_{n-2} and the other being self_1, self_3, ..., self_{n-1}.

reduce

reduce[func: fn[DType, Int](SIMD[$0, $1], SIMD[$0, $1]) capturing -> SIMD[$0, $1], size_out: Int = 1](self) -> SIMD[type, $1]

Reduces the vector using a provided reduce operator.

Constraints:

size_out must not exceed width of the vector.

Parameters:

  • func (fn[DType, Int](SIMD[$0, $1], SIMD[$0, $1]) capturing -> SIMD[$0, $1]): The reduce function to apply to elements in this SIMD.
  • size_out (Int): The width of the reduction.

Returns:

A new scalar which is the reduction of all vector elements.

reduce_max

reduce_max[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the max operator.

Constraints:

size_out must not exceed width of the vector. The element type of the vector must be integer or FP.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The maximum element of the vector.

reduce_min

reduce_min[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the min operator.

Constraints:

size_out must not exceed width of the vector. The element type of the vector must be integer or FP.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The minimum element of the vector.

reduce_add

reduce_add[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the add operator.

Constraints:

size_out must not exceed width of the vector.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The sum of all vector elements.

reduce_mul

reduce_mul[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the mul operator.

Constraints:

size_out must not exceed width of the vector. The element type of the vector must be integer or FP.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The product of all vector elements.

reduce_and

reduce_and[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the bitwise & operator.

Constraints:

size_out must not exceed width of the vector. The element type of the vector must be integer or boolean.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The reduced vector.

reduce_or

reduce_or[size_out: Int = 1](self) -> SIMD[type, $0]

Reduces the vector using the bitwise | operator.

Constraints:

size_out must not exceed width of the vector. The element type of the vector must be integer or boolean.

Parameters:

  • size_out (Int): The width of the reduction.

Returns:

The reduced vector.

reduce_bit_count

reduce_bit_count(self) -> Int

Returns the total number of bits set in the SIMD vector.

Constraints:

Must be either an integral or a boolean type.

Returns:

Count of set bits across all elements of the vector.

select

select[result_type: DType](self, true_case: SIMD[result_type, size], false_case: SIMD[result_type, size]) -> SIMD[$0, size]

Selects the values of the true_case or the false_case based on the current boolean values of the SIMD vector.

Constraints:

The element type of the vector must be boolean.

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] for i, elem in enumerate(self)].

rotate_left

rotate_left[shift: Int](self) -> Self

Shifts the elements of a SIMD vector to the left by shift elements (with wrap-around).

Constraints:

-size <= shift < size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the left (with wrap-around).

Returns:

The SIMD vector rotated to the left by shift elements (with wrap-around).

rotate_right

rotate_right[shift: Int](self) -> Self

Shifts the elements of a SIMD vector to the right by shift elements (with wrap-around).

Constraints:

-size < shift <= size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the right (with wrap-around).

Returns:

The SIMD vector rotated to the right by shift elements (with wrap-around).

shift_left

shift_left[shift: Int](self) -> Self

Shifts the elements of a SIMD vector to the left by shift elements (no wrap-around, fill with zero).

Constraints:

0 <= shift <= size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the left (no wrap-around, fill with zero).

Returns:

The SIMD vector rotated to the left by shift elements (no wrap-around, fill with zero).

shift_right

shift_right[shift: Int](self) -> Self

Shifts the elements of a SIMD vector to the right by shift elements (no wrap-around, fill with zero).

Constraints:

0 <= shift <= size

Parameters:

  • shift (Int): The number of positions by which to rotate the elements of SIMD vector to the right (no wrap-around, fill with zero).

Returns:

The SIMD vector rotated to the right by shift elements (no wrap-around, fill with zero).

reversed

reversed(self) -> Self

Reverses the SIMD vector by indexes.

Examples:

print(SIMD[DType.uint8, 4](1, 2, 3, 4).reversed()) # [4, 3, 2, 1]
print(SIMD[DType.uint8, 4](1, 2, 3, 4).reversed()) # [4, 3, 2, 1]

.

Returns:

The by index reversed vector.