include/vecmat/array.h file

Scalar loops on contiguous vm_float_t buffers.

These rules apply to every function in this file. n <= 0 reads and writes nothing; null pointers are then allowed. For n > 0 the pointers must be non-null and address n elements. An output may exactly alias an input; a partial overlap is caller error. No allocation.

Exception: vm_array_silu returns false on a null pointer or n <= 0 and does not write.

Functions

void vm_array_add(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)
res[i] = a[i] + b[i].
void vm_array_sub(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)
res[i] = a[i] - b[i].
void vm_array_mul(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)
res[i] = a[i] * b[i].
void vm_array_div(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)
res[i] = a[i] / b[i].
void vm_array_fma(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, const vm_float_t* c, int n)
res[i] = fma(a[i], b[i], c[i]).
void vm_axpy(vm_float_t* y, vm_float_t alpha, const vm_float_t* x, int n)
y[i] += alpha * x[i].
auto vm_nrm2(const vm_float_t* x, int n) -> vm_float_t
Euclidean norm of x, scaled so a finite result does not overflow early.
auto vm_asum(const vm_float_t* x, int n) -> vm_float_t
Sum of absolute values, Neumaier compensated.
void vm_rsqrt_array(vm_float_t* res, const vm_float_t* x, int n)
res[i] = 1 / sqrt(x[i]).
auto vm_array_silu(vm_float_t* y, const vm_float_t* x, int n) -> bool
y[i] = x[i] * sigmoid(x[i]).

Function documentation

void vm_array_add(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)

res[i] = a[i] + b[i].

Parameters
res Output; length n. May exactly alias a or b.
a Left input; length n.
b Right input; length n.
n

One rounding. Finite results are within 1 ULP of the correctly rounded sum.

void vm_array_sub(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)

res[i] = a[i] - b[i].

Parameters
res Output; length n. May exactly alias a or b.
a Left input; length n.
b Right input; length n.
n

One rounding. Finite results are within 1 ULP of the correctly rounded difference.

void vm_array_mul(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)

res[i] = a[i] * b[i].

Parameters
res Output; length n. May exactly alias a or b.
a Left input; length n.
b Right input; length n.
n

One rounding. Finite results are within 1 ULP of the correctly rounded product.

void vm_array_div(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, int n)

res[i] = a[i] / b[i].

Parameters
res Output; length n. May exactly alias a or b.
a Dividend; length n.
b Divisor; length n.
n

One rounding. Finite results are within 1 ULP of the correctly rounded quotient. Division by zero follows IEEE (finite nonzero → signed inf, 0/0 → NaN).

void vm_array_fma(vm_float_t* res, const vm_float_t* a, const vm_float_t* b, const vm_float_t* c, int n)

res[i] = fma(a[i], b[i], c[i]).

Parameters
res Output; length n. May exactly alias a, b, or c.
a Factor; length n.
b Factor; length n.
c Addend; length n.
n

One rounding, through VECMAT_FMA (fma / fmaf). This is not a license to contract other expressions. Finite results are within 1 ULP of the correctly rounded fused operation.

void vm_axpy(vm_float_t* y, vm_float_t alpha, const vm_float_t* x, int n)

y[i] += alpha * x[i].

Parameters
y Input and output; length n.
alpha Scale applied to x.
x
n

A multiply, then an add. Not a fused multiply-add. x may exactly alias y. For finite inputs the forward error is at most 8 eps × (|y_old| + |alpha * x|).

vm_float_t vm_nrm2(const vm_float_t* x, int n)

Euclidean norm of x, scaled so a finite result does not overflow early.

Returns sqrt(sum x[i]^2), or +0 when n <= 0.

n <= 0 or a null x returns +0 and does not read. A NaN in x returns a NaN. An infinity returns +inf. No allocation.

vm_float_t vm_asum(const vm_float_t* x, int n)

Sum of absolute values, Neumaier compensated.

Returns Compensated sum |x[i]|, or +0 when n <= 0.

n <= 0 or a null x returns +0 and does not read. No allocation.

void vm_rsqrt_array(vm_float_t* res, const vm_float_t* x, int n)

res[i] = 1 / sqrt(x[i]).

Parameters
res Output; length n.
x
n

Strict reciprocal square root. The SIMD path uses hardware sqrt then div (not an estimate). +0 → +inf, −0 → −inf, +inf → +0, a negative nonzero (including −inf) → NaN, and a NaN stays a NaN. Positive finite normals whose reciprocal square root is finite are within 2 ULP of the correctly rounded value. res may exactly alias x.

bool vm_array_silu(vm_float_t* y, const vm_float_t* x, int n)

y[i] = x[i] * sigmoid(x[i]).

Parameters
y Output; length n. May exactly alias x. Unchanged on failure.
x
n
Returns false when a pointer or the count is invalid.

sigmoid(x) = 1 / (1 + exp(-x)), evaluated with VECMAT_EXP. The reciprocal is one rounding, then the multiply is another. y may exactly alias x. A partial overlap is caller error. No allocation.

A null pointer or n <= 0 returns false and does not write y.

On a finite input, the result differs from the same steps in IEEE-754 double by less than 16 * VECMAT_EPSILON * (1 + |x|) absolute. +inf stays +inf. -inf and a NaN yield a NaN.