Skip to content

simdAdd ​

simdAdd soma os elementos correspondentes de dois arrays do mesmo tamanho e retorna um novo array em Result<T[], SimdError>.

AssinaturasimdAdd<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);
    }
}

Se os dois argumentos forem arrays fixos com comprimentos diferentes, o compilador informa E0308. Se os tamanhos só forem conhecidos em runtime, simdAdd retorna Err com o código ERR_SIMD_LENGTH_MISMATCH. Se não for possível alocar o array de resultado, retorna ERR_SIMD_OUT_OF_MEMORY. Essas falhas recuperáveis não provocam panic. Todas as lanes inteiras usam wrap por padrão. Por exemplo, 120 + 120 como i8 resulta em -16, e 255 + 1 como u8 resulta em 0, não 256. Ainda é obrigatório que literais inteiros caibam no tipo declarado para os elementos. Os tipos aceitos são i8, i16, i32, i64, u8, u16, u32, u64, f32 e f64; i128, u128, f128, isize, usize e decimal não são aceitos. O compilador escolhe as larguras vetoriais e trata os elementos restantes com segurança; a API não promete um ganho de velocidade fixo entre alvos.

Arrays de tamanho fixo também funcionam como argumentos diretos. O tamanho é conhecido em compilação, e a operação SIMD lê seus elementos sem alocar antes um array dinâmico de entrada.

Erros ​

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);
               ^^^^^^^