simdAdd
simdAdd suma los elementos correspondientes de dos arrays del mismo tamaño y devuelve un array nuevo dentro de Result<T[], SimdError>.
simdAdd<T>(left: T[], right: T[]): Result<T[], SimdError>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:
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:
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:
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);
}
}Si ambos argumentos son arrays fijos con longitudes diferentes, el compilador informa E0308. Si los tamaños solo se conocen en runtime, simdAdd devuelve Err con el código ERR_SIMD_LENGTH_MISMATCH. Si no se puede asignar el array de resultado, devuelve ERR_SIMD_OUT_OF_MEMORY. Estos errores recuperables no provocan panic. Todas las lanes enteras usan wrap por defecto. Por ejemplo, 120 + 120 como i8 da -16, y 255 + 1 como u8 da 0, no 256. Los literales enteros deben caber en el tipo declarado para los elementos. Los tipos aceptados son i8, i16, i32, i64, u8, u16, u32, u64, f32 y f64; i128, u128, f128, isize, usize y decimal no están aceptados. El compilador elige los anchos vectoriales y maneja con seguridad los elementos restantes; la API no promete una aceleración fija entre objetivos.
Los arrays de tamaño fijo también funcionan como argumentos directos. Su longitud se conoce en compilación y la operación SIMD lee sus elementos sin asignar antes un array dinámico de entrada.
Errores
error
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); }
}ERR_SIMD_LENGTH_MISMATCH simdAdd requires arrays of equal lengtherror
import { simdAdd } from "tyrus:simd";
const left: i32[] = [1, 2];
const right: i64[] = [3, 4];
const result = simdAdd(left, right); error[E0308]: type mismatch for argument 2 of `simdAdd`: expected `i32[]`, found `i64[]`
at /src/main.tyr:5:16
const result = simdAdd(left, right);
^^^^^^^