SIMD.
These members work on every lane of a vector at once. simd<T, N> holds N lanes of the integer or float type T, and mask<N> holds N lanes that are each set or clear. The silk/simd/simd modules wrap them for the 128-bit and 256-bit shapes, one module per shape such as silk.simd.u32x4 and silk.simd.mask4. Instructions that exist on one architecture only are on the Architecture page.
Rules shared by every SIMD member#
Every member takes the vector or mask type as its first type argument, written out:
Intrinsic.simdSplat<simd<u32, 4>>(10). In a signature,Vis that vector type,Lits lane type,Nits lane count andMa mask type.A vector is 16 to 512 bits wide, with a power-of-two lane count; a mask has 2 to 64 lanes.
simd<i32, 3>andsimd<bool, 4>are SEM0239.A member used with a lane type it does not take is SEM0239 at the call, with the rule in the message:
Intrinsic.simdShiftLeft takes a SIMD vector with integer lanes, but simd<f32, 4> is not one.Every member is safe except
simdPointerLoadandsimdPointerStore. Lane indices and slice ranges are checked and trap when out of range.The operators also work on vectors:
a + bon twosimd<u32, 4>adds lane-wise and traps on overflow in any lane.Shapes wider than the target's registers are split into several instructions, and a shape with no module, such as
simd<u8, 64>orsimd<u32, 2>, is reached only through these members.
Intrinsic.simdSplat#
fn simdSplat<V>(lane: L) -> VReturns the vector V with every lane set to lane. L is V's lane type.
pub fn main() -> i32 {
let tens = Intrinsic.simdSplat<simd<u32, 4>>(10)
if Intrinsic.simdExtract<simd<u32, 4>>(tens, 3) != 10 {
return 1
}
return 0
}Intrinsic.simdExtract#
fn simdExtract<V>(vector: V, index: usize) -> LReturns lane index of vector.
An index past the last lane traps; it never reads outside the vector. A constant index compiles to one lane move.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
if Intrinsic.simdExtract<simd<u32, 4>>(values, 2) != 3 {
return 1
}
return 0
}Intrinsic.simdInsert#
fn simdInsert<V>(vector: V, index: usize, lane: L) -> VReturns vector with lane index replaced by lane. The vector is a value, so the original is unchanged.
An index past the last lane traps.
pub fn main() -> i32 {
let values = Intrinsic.simdInsert<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), 0, 9)
if Intrinsic.simdExtract<simd<u32, 4>>(values, 0) != 9 {
return 1
}
return 0
}Intrinsic.simdFromArray#
fn simdFromArray<V>(lanes: [L; N]) -> VReturns the vector whose lane i is element i of the array. N must be V's lane count.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
if Intrinsic.simdExtract<simd<u32, 4>>(values, 3) != 4 {
return 1
}
return 0
}Intrinsic.simdToArray#
fn simdToArray<V>(vector: V) -> [L; N]Returns the array whose element i is lane i of the vector.
pub fn main() -> i32 {
let lanes = Intrinsic.simdToArray<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]))
if lanes[0] + lanes[3] != 5 {
return 1
}
return 0
}Intrinsic.simdLanesEqual#
fn simdLanesEqual<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] == right[i]. Lane-wise ==; a NaN lane is never equal.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([1, 0, 3, 0])
let mask = Intrinsic.simdLanesEqual<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b0101 {
return 1
}
return 0
}Intrinsic.simdLanesNotEqual#
fn simdLanesNotEqual<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] != right[i]. Lane-wise !=; a NaN lane always differs.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([1, 0, 3, 0])
let mask = Intrinsic.simdLanesNotEqual<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b1010 {
return 1
}
return 0
}Intrinsic.simdLanesLess#
fn simdLanesLess<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] < right[i]. Lane-wise <, under the lane type's signedness or IEEE 754 order.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([2, 2, 2, 2])
let mask = Intrinsic.simdLanesLess<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b0001 {
return 1
}
return 0
}Intrinsic.simdLanesLessOrEqual#
fn simdLanesLessOrEqual<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] <= right[i]. Lane-wise <=.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([2, 2, 2, 2])
let mask = Intrinsic.simdLanesLessOrEqual<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b0011 {
return 1
}
return 0
}Intrinsic.simdLanesGreater#
fn simdLanesGreater<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] > right[i]. Lane-wise >.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([2, 2, 2, 2])
let mask = Intrinsic.simdLanesGreater<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b1100 {
return 1
}
return 0
}Intrinsic.simdLanesGreaterOrEqual#
fn simdLanesGreaterOrEqual<V>(left: V, right: V) -> mask<N>Compares left and right lane by lane and returns a mask whose lane i is set when left[i] >= right[i]. Lane-wise >=.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let other = Intrinsic.simdFromArray<simd<u32, 4>>([2, 2, 2, 2])
let mask = Intrinsic.simdLanesGreaterOrEqual<simd<u32, 4>>(values, other)
if Intrinsic.simdMaskToBits<mask<4>>(mask) != 0b1110 {
return 1
}
return 0
}Intrinsic.simdSelect#
fn simdSelect<V>(mask: mask<N>, chosen: V, other: V) -> VReturns lane i of chosen where mask lane i is set, and lane i of other where it is clear. It is a branch-free if per lane.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let small = Intrinsic.simdLanesLess<simd<u32, 4>>(values, Intrinsic.simdSplat<simd<u32, 4>>(3))
// Lanes below 3 become 0.
let clipped = Intrinsic.simdSelect<simd<u32, 4>>(small, Intrinsic.simdSplat<simd<u32, 4>>(0), values)
if Intrinsic.simdExtract<simd<u32, 4>>(clipped, 1) != 0 || Intrinsic.simdExtract<simd<u32, 4>>(clipped, 2) != 3 {
return 1
}
return 0
}Intrinsic.simdWrappingAdd#
fn simdWrappingAdd<V>(left: V, right: V) -> VReturns the lane-wise integer sum modulo 2 to the power of the lane width. It never traps. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u8, 4>>([255, 1, 2, 3])
let result = Intrinsic.simdWrappingAdd<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(1))
if Intrinsic.simdExtract<simd<u8, 4>>(result, 0) != 0 {
return 1
}
return 0
}Intrinsic.simdWrappingSubtract#
fn simdWrappingSubtract<V>(left: V, right: V) -> VReturns the lane-wise integer difference modulo 2 to the power of the lane width. It never traps. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u8, 4>>([0, 1, 2, 3])
let result = Intrinsic.simdWrappingSubtract<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(1))
if Intrinsic.simdExtract<simd<u8, 4>>(result, 0) != 255 {
return 1
}
return 0
}Intrinsic.simdWrappingMultiply#
fn simdWrappingMultiply<V>(left: V, right: V) -> VReturns the lane-wise low half of the integer product. It never traps. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u8, 4>>([16, 1, 2, 3])
let result = Intrinsic.simdWrappingMultiply<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(17))
if Intrinsic.simdExtract<simd<u8, 4>>(result, 0) != 16 {
return 1
}
return 0
}Intrinsic.simdSaturatingAdd#
fn simdSaturatingAdd<V>(left: V, right: V) -> VReturns the lane-wise integer sum clamped to the lane type's range. It never traps. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u8, 4>>([250, 1, 2, 3])
let result = Intrinsic.simdSaturatingAdd<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(10))
if Intrinsic.simdExtract<simd<u8, 4>>(result, 0) != 255 {
return 1
}
return 0
}Intrinsic.simdSaturatingSubtract#
fn simdSaturatingSubtract<V>(left: V, right: V) -> VReturns the lane-wise integer difference clamped to the lane type's range. It never traps. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u8, 4>>([3, 1, 2, 3])
let result = Intrinsic.simdSaturatingSubtract<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(5))
if Intrinsic.simdExtract<simd<u8, 4>>(result, 0) != 0 {
return 1
}
return 0
}Intrinsic.simdShiftLeft#
fn simdShiftLeft<V>(left: V, right: V) -> VShifts each lane of left left by the matching lane of right, filling with zeros. Integer lanes only.
A count of the lane width or more traps.
pub fn main() -> i32 {
let shifted = Intrinsic.simdShiftLeft<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdFromArray<simd<u32, 4>>([0, 1, 2, 3]))
if Intrinsic.simdExtract<simd<u32, 4>>(shifted, 3) != 32 {
return 1
}
return 0
}Intrinsic.simdShiftRight#
fn simdShiftRight<V>(left: V, right: V) -> VShifts each lane of left right by the matching lane of right: logical for unsigned lanes, arithmetic for signed lanes. Integer lanes only.
A count of the lane width or more traps.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<i32, 4>>([-8, 8, 16, 32])
let shifted = Intrinsic.simdShiftRight<simd<i32, 4>>(values, Intrinsic.simdSplat<simd<i32, 4>>(2))
if Intrinsic.simdExtract<simd<i32, 4>>(shifted, 0) != -2 {
return 1
}
return 0
}Intrinsic.simdRotateLeft#
fn simdRotateLeft<V>(left: V, right: V) -> VRotates each lane of left left by the matching lane of right, modulo the lane width. Integer lanes only; it never traps.
pub fn main() -> i32 {
let values = Intrinsic.simdSplat<simd<u8, 4>>(0x81)
let rotated = Intrinsic.simdRotateLeft<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(1))
if Intrinsic.simdExtract<simd<u8, 4>>(rotated, 0) != 0x03 {
return 1
}
return 0
}Intrinsic.simdRotateRight#
fn simdRotateRight<V>(left: V, right: V) -> VRotates each lane of left right by the matching lane of right, modulo the lane width. Integer lanes only.
pub fn main() -> i32 {
let values = Intrinsic.simdSplat<simd<u8, 4>>(0x81)
let rotated = Intrinsic.simdRotateRight<simd<u8, 4>>(values, Intrinsic.simdSplat<simd<u8, 4>>(1))
if Intrinsic.simdExtract<simd<u8, 4>>(rotated, 0) != 0xc0 {
return 1
}
return 0
}Intrinsic.simdMinimum#
fn simdMinimum<V>(left: V, right: V) -> VReturns the lane-wise lesser value. Integer lanes compare under their signedness. A float lane follows the scalar min in silk/num/f32, except that the payload of a NaN result is unspecified.
pub fn main() -> i32 {
let low = Intrinsic.simdMinimum<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdExtract<simd<u32, 4>>(low, 3) != 2 || Intrinsic.simdExtract<simd<u32, 4>>(low, 0) != 1 {
return 1
}
return 0
}Intrinsic.simdMaximum#
fn simdMaximum<V>(left: V, right: V) -> VReturns the lane-wise greater value, under the same rules as simdMinimum.
pub fn main() -> i32 {
let high = Intrinsic.simdMaximum<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdExtract<simd<u32, 4>>(high, 0) != 2 {
return 1
}
return 0
}Intrinsic.simdSqrt#
fn simdSqrt<V>(vector: V) -> VReturns the correctly rounded square root of each lane. Float lanes only.
pub fn main() -> i32 {
let roots = Intrinsic.simdSqrt<simd<f32, 4>>(Intrinsic.simdFromArray<simd<f32, 4>>([1.0, 4.0, 9.0, 16.0]))
if Intrinsic.simdExtract<simd<f32, 4>>(roots, 3) != 4.0 {
return 1
}
return 0
}Intrinsic.simdMulAdd#
fn simdMulAdd<V>(left: V, right: V, addend: V) -> VReturns left * right + addend for each lane, fused: rounded once, as fma. Float lanes only.
pub fn main() -> i32 {
let two = Intrinsic.simdSplat<simd<f32, 4>>(2.0)
let result = Intrinsic.simdMulAdd<simd<f32, 4>>(Intrinsic.simdFromArray<simd<f32, 4>>([1.0, 4.0, 9.0, 16.0]), two, two)
if Intrinsic.simdExtract<simd<f32, 4>>(result, 0) != 4.0 {
return 1
}
return 0
}Intrinsic.simdReduceAdd#
fn simdReduceAdd<V>(vector: V) -> LReturns the sum of every lane.
Integer lanes: a sum that leaves the lane type traps with "arithmetic overflow"; use
simdReduceWrappingAddto wrap.Float lanes: the lanes are added in order from lane 0, so the result is reproducible.
simdReduceAddUnorderedlets the target pick a faster order.
pub fn main() -> i32 {
if Intrinsic.simdReduceAdd<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 10 {
return 1
}
return 0
}Intrinsic.simdReduceWrappingAdd#
fn simdReduceWrappingAdd<V>(vector: V) -> LReturns the sum of every integer lane modulo 2 to the power of the lane width. It never traps.
pub fn main() -> i32 {
let values = Intrinsic.simdSplat<simd<u8, 4>>(100)
if Intrinsic.simdReduceWrappingAdd<simd<u8, 4>>(values) != 144 {
return 1
}
return 0
}Intrinsic.simdReduceAddUnordered#
fn simdReduceAddUnordered<V>(vector: V) -> LReturns the sum of every float lane, added in an order the target chooses. Rounding may differ from simdReduceAdd, and from one target to another.
pub fn main() -> i32 {
if Intrinsic.simdReduceAddUnordered<simd<f32, 4>>(Intrinsic.simdFromArray<simd<f32, 4>>([1.0, 4.0, 9.0, 16.0])) != 30.0 {
return 1
}
return 0
}Intrinsic.simdReduceMinimum#
fn simdReduceMinimum<V>(vector: V) -> LReturns the least lane, under the rules of simdMinimum.
pub fn main() -> i32 {
if Intrinsic.simdReduceMinimum<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 1 {
return 1
}
return 0
}Intrinsic.simdReduceMaximum#
fn simdReduceMaximum<V>(vector: V) -> LReturns the greatest lane, under the rules of simdMaximum.
pub fn main() -> i32 {
if Intrinsic.simdReduceMaximum<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 4 {
return 1
}
return 0
}Intrinsic.simdReduceBitAnd#
fn simdReduceBitAnd<V>(vector: V) -> LReturns the bitwise AND of every integer lane.
pub fn main() -> i32 {
if Intrinsic.simdReduceBitAnd<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 0 {
return 1
}
return 0
}Intrinsic.simdReduceBitOr#
fn simdReduceBitOr<V>(vector: V) -> LReturns the bitwise OR of every integer lane.
pub fn main() -> i32 {
if Intrinsic.simdReduceBitOr<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 7 {
return 1
}
return 0
}Intrinsic.simdReduceBitXor#
fn simdReduceBitXor<V>(vector: V) -> LReturns the bitwise exclusive OR of every integer lane.
pub fn main() -> i32 {
if Intrinsic.simdReduceBitXor<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])) != 4 {
return 1
}
return 0
}Intrinsic.simdMaskAny#
fn simdMaskAny<M>(mask: M) -> boolReturns whether any lane of the mask is set.
pub fn main() -> i32 {
let large = Intrinsic.simdLanesGreater<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if !Intrinsic.simdMaskAny<mask<4>>(large) {
return 1
}
return 0
}Intrinsic.simdMaskAll#
fn simdMaskAll<M>(mask: M) -> boolReturns whether every lane of the mask is set.
pub fn main() -> i32 {
let large = Intrinsic.simdLanesGreater<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdMaskAll<mask<4>>(large) {
return 1
}
return 0
}Intrinsic.simdMaskCount#
fn simdMaskCount<M>(mask: M) -> usizeReturns the number of set lanes.
pub fn main() -> i32 {
let large = Intrinsic.simdLanesGreater<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdMaskCount<mask<4>>(large) != 2 {
return 1
}
return 0
}Intrinsic.simdMaskFirstSet#
fn simdMaskFirstSet<M>(mask: M) -> usizeReturns the index of the lowest set lane, or the lane count when no lane is set. A byte search uses it to find the first match.
pub fn main() -> i32 {
let large = Intrinsic.simdLanesGreater<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdMaskFirstSet<mask<4>>(large) != 2 {
return 1
}
return 0
}Intrinsic.simdMaskToBits#
fn simdMaskToBits<M>(mask: M) -> u64Returns the mask as an integer: lane i is bit i, and the bits above the last lane are zero.
pub fn main() -> i32 {
let large = Intrinsic.simdLanesGreater<simd<u32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]), Intrinsic.simdSplat<simd<u32, 4>>(2))
if Intrinsic.simdMaskToBits<mask<4>>(large) != 0b1100 {
return 1
}
return 0
}Intrinsic.simdMaskFromBits#
fn simdMaskFromBits<M>(bits: u64) -> MReturns the mask whose lane i is set when bit i of bits is set. Bits past the last lane are ignored.
pub fn main() -> i32 {
let mask = Intrinsic.simdMaskFromBits<mask<4>>(0xf1)
if Intrinsic.simdMaskCount<mask<4>>(mask) != 1 {
return 1
}
return 0
}Intrinsic.simdLoad#
fn simdLoad<V>(values: &[L], offset: usize) -> VReturns the vector of values[offset] to values[offset + N - 1], where N is V's lane count. The slice may have any alignment.
A range past the end of the slice traps.
pub fn main() -> i32 {
let array: [u32; 6] = [1, 2, 3, 4, 5, 6]
let values: &[u32] = &array
let middle = Intrinsic.simdLoad<simd<u32, 4>>(values, 2)
if Intrinsic.simdExtract<simd<u32, 4>>(middle, 0) != 3 {
return 1
}
return 0
}Intrinsic.simdStore#
fn simdStore<V>(values: &mut [L], offset: usize, vector: V) -> ()Writes the lanes of vector to values[offset] through values[offset + N - 1].
A range past the end of the slice traps before anything is written.
pub fn main() -> i32 {
let mut array: [u32; 6] = [0; 6]
let values: &mut [u32] = &mut array
Intrinsic.simdStore<simd<u32, 4>>(move values, 2, Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]))
if array[5] != 4 || array[1] != 0 {
return 1
}
return 0
}Intrinsic.simdPointerLoad#
unsafe fn simdPointerLoad<V>(pointer: [*]const L) -> VReturns the vector of N consecutive elements starting at pointer, without a bounds check.
Unsafe. The caller proves that
Nelements frompointerare live and initialized. The pointer needs only the element's alignment, not the vector's.
pub fn main() -> i32 {
let array: [u32; 4] = [5, 6, 7, 8]
let values: &[u32] = &array
let pointer = unsafe Intrinsic.pointerRequalify<?[*]const u32, [*]const u32>(Intrinsic.pointerFromSlice<u32>(values))
let vector = unsafe Intrinsic.simdPointerLoad<simd<u32, 4>>(pointer)
if Intrinsic.simdReduceAdd<simd<u32, 4>>(vector) != 26 {
return 1
}
return 0
}Intrinsic.simdPointerStore#
unsafe fn simdPointerStore<V>(pointer: [*]mut L, vector: V) -> ()Writes the lanes of vector to N consecutive elements starting at pointer, without a bounds check.
Unsafe. The caller proves that
Nelements frompointerare live, writable and not used by anything else. The pointer needs only the element's alignment.
pub fn main() -> i32 {
let mut array: [u32; 4] = [0; 4]
let values: &mut [u32] = &mut array
let pointer = unsafe Intrinsic.pointerRequalify<?[*]mut u32, [*]mut u32>(Intrinsic.pointerFromMutSlice<u32>(move values))
unsafe Intrinsic.simdPointerStore<simd<u32, 4>>(pointer, Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]))
if array[3] != 4 {
return 1
}
return 0
}Intrinsic.simdConvert#
fn simdConvert<From, To>(vector: From) -> ToConverts each lane to the lane type of To, as the scalar To conversion does: integers keep their exact value, floats to integers truncate toward zero. From and To have the same lane count.
A lane that does not fit the target type traps, as the scalar conversion does.
Different lane counts are SEM0239.
pub fn main() -> i32 {
let floats = Intrinsic.simdConvert<simd<u32, 4>, simd<f32, 4>>(Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4]))
if Intrinsic.simdExtract<simd<f32, 4>>(floats, 3) != 4.0 {
return 1
}
return 0
}Intrinsic.simdBitcast#
fn simdBitcast<From, To>(vector: From) -> ToReturns a vector with the same bits as another vector type of the same total width, such as simd<u32, 4> as simd<u8, 16>. Lane 0 is at the lowest address, so the byte order within a lane is the target's.
Different widths are SEM0239. Every bit pattern is a valid integer or float lane, so it is safe.
pub fn main() -> i32 {
let bytes = Intrinsic.simdBitcast<simd<u32, 4>, simd<u8, 16>>(Intrinsic.simdSplat<simd<u32, 4>>(0x01020304))
static if Intrinsic.targetEndianness() == "little" {
if Intrinsic.simdExtract<simd<u8, 16>>(bytes, 0) != 4 {
return 1
}
}
return 0
}Intrinsic.simdShuffle#
fn simdShuffle<V, R>(left: V, right: V, static indices: [usize; M]) -> RReturns a vector of M lanes where lane i is lane indices[i] of left followed by right: indices below N pick from left, and N to 2N - 1 from right. R has V's lane type and M lanes, so a shuffle can also narrow or widen.
The indices are static, a literal or a value known during specialization (SEM0176 otherwise), and each is below
2N(SEM0239 otherwise). A changed lane type is SEM0239.
pub fn main() -> i32 {
let values = Intrinsic.simdFromArray<simd<u32, 4>>([1, 2, 3, 4])
let reversed = Intrinsic.simdShuffle<simd<u32, 4>, simd<u32, 4>>(values, values, [3, 2, 1, 0])
let pair = Intrinsic.simdShuffle<simd<u32, 4>, simd<u32, 2>>(values, Intrinsic.simdSplat<simd<u32, 4>>(9), [0, 4])
if Intrinsic.simdExtract<simd<u32, 4>>(reversed, 0) != 4 || Intrinsic.simdExtract<simd<u32, 2>>(pair, 1) != 9 {
return 1
}
return 0
}