Magetypes
SIMD vector types with natural Rust operators
For a step-by-step introduction from an ordinary function to vectors, start with the Path of Attunement.
magetypes provides SIMD vector types with ordinary Rust operators. You write a kernel once, and it compiles for AVX2, AVX-512, NEON, WASM SIMD128 and scalar.
This kernel multiplies a buffer by a gain, using AVX2, NEON or WASM SIMD where available:
#![forbid(unsafe_code)]
use archmage::prelude::*;
#[attune(_*_t, define(f32x8))]
fn gain_impl(token: Token, plane: &mut [f32], gain: f32) {
let factor = f32x8::splat_t(token, gain);
let (chunks, tail) = f32x8::partition_slice_mut_t(token, plane);
for chunk in chunks {
(f32x8::load_t(token, chunk) * factor).store(chunk);
}
for value in tail {
*value *= gain;
}
}
pub fn apply_gain(plane: &mut [f32], gain: f32) {
attuned!(gain_impl(plane, gain));
}
let mut data = [2.0; 11];
apply_gain(&mut data, 0.5);
assert_eq!(data, [1.0; 11]);
#[attune(_*_t, define(f32x8))] compiles gain_impl once per default portable
tier (v3, neon, wasm128, scalar), and attuned! runs the best one the
CPU supports. Call it around your loop, as here, not inside it. When callers do
not need separate _t entry points, #[attune(dispatch, define(f32x8))] with
let token = Token::from_context(); combines both into a single function. The
kernel is adapted from zenfilters:
Reusable generic kernels links the
production source.
Continue with the production patterns
- Type and const generics: pixel types, mode specialization, and turbofish dispatch.
- Reusable backend helpers: the complete zenblend SrcOver chain.
- Memory, bounds, and tails: array references and row/strip contracts.
- Direct intrinsic helpers: a feature-enabled entry and matched helper.
- ISA quirks and fixups: the numerical contract and its costs.
- Production coverage: which patterns the zen crates actually exercise.
Archmage provides tokens, feature contexts, and dispatch. Magetypes provides
vectors such as f32x4<T> and f32x8<T>. Generics and define(...) compose with
#[attune]; neither is a substitute for a feature-enabled entry. Inspect
optimized code and benchmark the complete loop before claiming zero overhead.