pub trait VectorBackend {
Show 36 methods
// Required methods
unsafe fn add(a: &[f32], b: &[f32], result: &mut [f32]);
unsafe fn sub(a: &[f32], b: &[f32], result: &mut [f32]);
unsafe fn mul(a: &[f32], b: &[f32], result: &mut [f32]);
unsafe fn div(a: &[f32], b: &[f32], result: &mut [f32]);
unsafe fn dot(a: &[f32], b: &[f32]) -> f32;
unsafe fn sum(a: &[f32]) -> f32;
unsafe fn max(a: &[f32]) -> f32;
unsafe fn min(a: &[f32]) -> f32;
unsafe fn argmax(a: &[f32]) -> usize;
unsafe fn argmin(a: &[f32]) -> usize;
unsafe fn sum_kahan(a: &[f32]) -> f32;
unsafe fn norm_l2(a: &[f32]) -> f32;
unsafe fn norm_l1(a: &[f32]) -> f32;
unsafe fn norm_linf(a: &[f32]) -> f32;
unsafe fn scale(a: &[f32], scalar: f32, result: &mut [f32]);
unsafe fn abs(a: &[f32], result: &mut [f32]);
unsafe fn clamp(a: &[f32], min_val: f32, max_val: f32, result: &mut [f32]);
unsafe fn lerp(a: &[f32], b: &[f32], t: f32, result: &mut [f32]);
unsafe fn fma(a: &[f32], b: &[f32], c: &[f32], result: &mut [f32]);
unsafe fn relu(a: &[f32], result: &mut [f32]);
unsafe fn exp(a: &[f32], result: &mut [f32]);
unsafe fn sigmoid(a: &[f32], result: &mut [f32]);
unsafe fn gelu(a: &[f32], result: &mut [f32]);
unsafe fn swish(a: &[f32], result: &mut [f32]);
unsafe fn tanh(a: &[f32], result: &mut [f32]);
unsafe fn sqrt(a: &[f32], result: &mut [f32]);
unsafe fn recip(a: &[f32], result: &mut [f32]);
unsafe fn ln(a: &[f32], result: &mut [f32]);
unsafe fn log2(a: &[f32], result: &mut [f32]);
unsafe fn log10(a: &[f32], result: &mut [f32]);
unsafe fn sin(a: &[f32], result: &mut [f32]);
unsafe fn cos(a: &[f32], result: &mut [f32]);
unsafe fn tan(a: &[f32], result: &mut [f32]);
unsafe fn floor(a: &[f32], result: &mut [f32]);
unsafe fn ceil(a: &[f32], result: &mut [f32]);
unsafe fn round(a: &[f32], result: &mut [f32]);
}Expand description
Backend trait defining common operations
All backend implementations must implement this trait to ensure consistent behavior across different SIMD instruction sets.
§Safety
Implementations may use unsafe SIMD intrinsics. Callers must ensure:
- Input slices are valid
- Result slice has sufficient capacity
- Slices
aandbhave the same length
Required Methods§
Sourceunsafe fn add(a: &[f32], b: &[f32], result: &mut [f32])
unsafe fn add(a: &[f32], b: &[f32], result: &mut [f32])
Element-wise addition: a[i] + b[i]
§Safety
aandbmust have the same lengthresultmust have length >=a.len()
Sourceunsafe fn sub(a: &[f32], b: &[f32], result: &mut [f32])
unsafe fn sub(a: &[f32], b: &[f32], result: &mut [f32])
Element-wise subtraction: a[i] - b[i]
§Safety
aandbmust have the same lengthresultmust have length >=a.len()
Sourceunsafe fn mul(a: &[f32], b: &[f32], result: &mut [f32])
unsafe fn mul(a: &[f32], b: &[f32], result: &mut [f32])
Element-wise multiplication: a[i] * b[i]
§Safety
aandbmust have the same lengthresultmust have length >=a.len()
Sourceunsafe fn div(a: &[f32], b: &[f32], result: &mut [f32])
unsafe fn div(a: &[f32], b: &[f32], result: &mut [f32])
Element-wise division: a[i] / b[i]
§Safety
aandbmust have the same lengthresultmust have length >=a.len()
Sourceunsafe fn argmax(a: &[f32]) -> usize
unsafe fn argmax(a: &[f32]) -> usize
Argmax: index of maximum value
Returns the index of the first occurrence of the maximum value.
§Safety
amust not be empty
Sourceunsafe fn argmin(a: &[f32]) -> usize
unsafe fn argmin(a: &[f32]) -> usize
Argmin: index of minimum value
Returns the index of the first occurrence of the minimum value.
§Safety
amust not be empty
Sourceunsafe fn sum_kahan(a: &[f32]) -> f32
unsafe fn sum_kahan(a: &[f32]) -> f32
Kahan summation: numerically stable sum(a[i])
Uses the Kahan summation algorithm to reduce floating-point rounding errors when summing many numbers. Tracks a running compensation for lost low-order bits.
§Safety
- Can handle empty slice (returns 0.0)
Sourceunsafe fn norm_l2(a: &[f32]) -> f32
unsafe fn norm_l2(a: &[f32]) -> f32
L2 norm (Euclidean norm): sqrt(sum(a[i]^2))
Computes the Euclidean length of the vector. This is equivalent to sqrt(dot(a, a)).
§Safety
- Can handle empty slice (returns 0.0)
Sourceunsafe fn norm_l1(a: &[f32]) -> f32
unsafe fn norm_l1(a: &[f32]) -> f32
L1 norm (Manhattan norm): sum(|a[i]|)
Computes the sum of absolute values of all elements. Used in machine learning (L1 regularization), distance metrics, and sparse modeling.
§Safety
- Can handle empty slice (returns 0.0)
Sourceunsafe fn norm_linf(a: &[f32]) -> f32
unsafe fn norm_linf(a: &[f32]) -> f32
L-infinity norm (maximum absolute value): max(|a[i]|)
Computes the maximum absolute value of all elements. Used in optimization (constraint checking), numerical analysis, and error bounds.
§Safety
- Can handle empty slice (returns 0.0)
Sourceunsafe fn scale(a: &[f32], scalar: f32, result: &mut [f32])
unsafe fn scale(a: &[f32], scalar: f32, result: &mut [f32])
Scalar multiplication: result[i] = a[i] * scalar
Multiplies all elements by a scalar value. Used in vector scaling, normalization, and linear transformations.
§Safety
resultmust have the same length asa- Can handle empty slice
Sourceunsafe fn abs(a: &[f32], result: &mut [f32])
unsafe fn abs(a: &[f32], result: &mut [f32])
Absolute value: result[i] = |a[i]|
Computes the absolute value of each element. Used in distance metrics (L1 norm), numerical stability, and signal processing.
§Safety
resultmust have the same length asa- Can handle empty slice
Sourceunsafe fn clamp(a: &[f32], min_val: f32, max_val: f32, result: &mut [f32])
unsafe fn clamp(a: &[f32], min_val: f32, max_val: f32, result: &mut [f32])
Clamp elements to range [min_val, max_val]: result[i] = max(min_val, min(a[i], max_val))
Constrains each element to the specified range. Used in neural networks (gradient clipping), graphics (color clamping), and signal processing.
§Safety
resultmust have the same length asa- Can handle empty slice
- Assumes min_val <= max_val (caller must validate)
Sourceunsafe fn lerp(a: &[f32], b: &[f32], t: f32, result: &mut [f32])
unsafe fn lerp(a: &[f32], b: &[f32], t: f32, result: &mut [f32])
Linear interpolation: result[i] = a[i] + t * (b[i] - a[i])
Computes element-wise linear interpolation between two vectors. When t=0, returns a; when t=1, returns b; values outside [0,1] extrapolate. Used in graphics, animation, neural networks, and signal processing.
§Safety
aandbmust have the same lengthresultmust have the same length asa- Can handle empty slices
Sourceunsafe fn fma(a: &[f32], b: &[f32], c: &[f32], result: &mut [f32])
unsafe fn fma(a: &[f32], b: &[f32], c: &[f32], result: &mut [f32])
Fused multiply-add: result[i] = a[i] * b[i] + c[i]
Computes element-wise fused multiply-add operation. On hardware with FMA support, this is a single instruction with better performance and numerical accuracy (no intermediate rounding). Used in neural networks, matrix multiplication, and scientific computing.
§Safety
a,b, andcmust all have the same lengthresultmust have the same length asa- Can handle empty slices
Sourceunsafe fn relu(a: &[f32], result: &mut [f32])
unsafe fn relu(a: &[f32], result: &mut [f32])
ReLU activation: result[i] = max(0, a[i])
Rectified Linear Unit - the most common activation function in neural networks. Sets negative values to zero, passes positive values unchanged.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn exp(a: &[f32], result: &mut [f32])
unsafe fn exp(a: &[f32], result: &mut [f32])
Exponential function: result[i] = exp(a[i])
Computes e^x for each element using range reduction for numerical accuracy. Foundation for sigmoid, softmax, GELU, and other activation functions.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn sigmoid(a: &[f32], result: &mut [f32])
unsafe fn sigmoid(a: &[f32], result: &mut [f32])
Sigmoid activation: result[i] = 1 / (1 + exp(-a[i]))
Logistic sigmoid function - maps inputs to (0, 1) range. Used in binary classification and as gating mechanism.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn gelu(a: &[f32], result: &mut [f32])
unsafe fn gelu(a: &[f32], result: &mut [f32])
GELU activation: result[i] = 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))
Gaussian Error Linear Unit - smooth non-monotonic activation. Used in BERT, GPT, and modern transformers.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn swish(a: &[f32], result: &mut [f32])
unsafe fn swish(a: &[f32], result: &mut [f32])
Swish activation: result[i] = x * sigmoid(x) = x / (1 + exp(-x))
Self-gated activation function (also called SiLU). Used in EfficientNet, MobileNetV3.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn tanh(a: &[f32], result: &mut [f32])
unsafe fn tanh(a: &[f32], result: &mut [f32])
Hyperbolic tangent activation: result[i] = tanh(a[i]) = (exp(2x) - 1) / (exp(2x) + 1)
Hyperbolic tangent - maps inputs to (-1, 1) range. Classic activation function from early neural networks. Used in RNNs, LSTMs, and as smooth alternative to ReLU.
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn sqrt(a: &[f32], result: &mut [f32])
unsafe fn sqrt(a: &[f32], result: &mut [f32])
Square root: result[i] = sqrt(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn recip(a: &[f32], result: &mut [f32])
unsafe fn recip(a: &[f32], result: &mut [f32])
Reciprocal: result[i] = 1 / a[i]
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn ln(a: &[f32], result: &mut [f32])
unsafe fn ln(a: &[f32], result: &mut [f32])
Natural logarithm: result[i] = ln(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn log2(a: &[f32], result: &mut [f32])
unsafe fn log2(a: &[f32], result: &mut [f32])
Base-2 logarithm: result[i] = log2(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn log10(a: &[f32], result: &mut [f32])
unsafe fn log10(a: &[f32], result: &mut [f32])
Base-10 logarithm: result[i] = log10(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn tan(a: &[f32], result: &mut [f32])
unsafe fn tan(a: &[f32], result: &mut [f32])
Tangent: result[i] = tan(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Sourceunsafe fn floor(a: &[f32], result: &mut [f32])
unsafe fn floor(a: &[f32], result: &mut [f32])
Floor: result[i] = floor(a[i])
§Safety
resultmust have the same length asa- Can handle empty slices
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.