Skip to content

simdAdd ​

simdAdd adds corresponding elements from two equal-length arrays and returns a new array inside Result<T[], SimdError>.

SignaturesimdAdd<T>(left: T[], right: T[]): Result<T[], SimdError>
tyrus
import { simdAdd, SimdError } from "tyrus:simd";

const left: f32[] = [1.0, 2.0, 3.0, 4.0, 5.0];
const right: f32[] = [10.0, 20.0, 30.0, 40.0, 50.0];
const result: Result<f32[], SimdError> = simdAdd(left, right);

match (result) {
    Ok(values) => {
        print(values); // [11.0, 22.0, 33.0, 44.0, 55.0]
    }
    Err(error) => {
        printError(error.code, error.message);
    }
}

Fixed-size arrays can be passed directly as well. Their length is known at compile time, and no dynamic input array is allocated:

tyrus
const fixedLeft: [i32; 5] = [1, 2, 3, 4, 5];
const fixedRight: [i32; 5] = [10, 20, 30, 40, 50];
const fixedResult: Result<i32[], SimdError> = simdAdd(fixedLeft, fixedRight);

match (fixedResult) {
    Ok(values) => {
        print(values); // [11, 22, 33, 44, 55]
    }
    Err(error) => {
        printError(error.code, error.message);
    }
}

The same operation accepts integer arrays. Signed and unsigned integer addition wraps at the element width by default, while the declared element type is still checked for literal range at compile time:

tyrus
const unsignedLeft: u8[] = [255; 5];
const unsignedRight: u8[] = [1; 5];
const unsignedResult: Result<u8[], SimdError> = simdAdd(unsignedLeft, unsignedRight);

match (unsignedResult) {
    Ok(values) => {
        print(values); // [0, 0, 0, 0, 0]
    }
    Err(error) => {
        printError(error.code, error.message);
    }
}

Signed integer values wrap too. Here 120 + 120 is reduced to the i8 range and produces -16 without a runtime overflow error:

tyrus
import { simdAdd, SimdError } from "tyrus:simd";

const signedLeft: i8[] = [120; 5];
const signedRight: i8[] = [120; 5];
const signedResult: Result<i8[], SimdError> = simdAdd(signedLeft, signedRight);

match (signedResult) {
    Ok(values) => {
        print(values); // [-16, -16, -16, -16, -16]
    }
    Err(error) => {
        printError(error.code, error.message);
    }
}

If both inputs are fixed-size arrays with different lengths, the compiler reports E0308. If the lengths are only known at runtime, simdAdd returns Err with code ERR_SIMD_LENGTH_MISMATCH. If the result array cannot be allocated, it returns ERR_SIMD_OUT_OF_MEMORY. These recoverable failures do not panic. All integer lanes use wrapping arithmetic by default. For example, 120 + 120 as i8 returns -16, and 255 + 1 as u8 returns 0, not 256. Integer literals must still fit their declared element type at compile time. Supported element types are i8, i16, i32, i64, u8, u16, u32, u64, f32, and f64; i128, u128, f128, isize, usize, and decimal are not accepted. The compiler selects vector widths and handles remaining elements safely; the API does not promise a fixed speedup across targets.

Fixed-size arrays work as direct arguments too. Their length is known at compile time, and the SIMD operation reads their elements without first allocating a dynamic input array.

Errors ​

error

tyrus
import { simdAdd, SimdError } from "tyrus:simd";

const left: i32[] = [1, 2];
const right: i32[] = [3, 4, 5];
const result: Result<i32[], SimdError> = simdAdd(left, right);

match (result) {
    Ok(_) => {}
    Err(error) => { printError(error.code, error.message); }
}
text
ERR_SIMD_LENGTH_MISMATCH simdAdd requires arrays of equal length

error

tyrus
import { simdAdd } from "tyrus:simd";

const left: i32[] = [1, 2];
const right: i64[] = [3, 4];
const result = simdAdd(left, right); 
text
error[E0308]: type mismatch for argument 2 of `simdAdd`: expected `i32[]`, found `i64[]`
at /src/main.tyr:5:16
const result = simdAdd(left, right);
               ^^^^^^^