Magetypes

SIMD vector types with natural Rust operators

Taking the path of attunement? The short beta guide starts with #[attune(dispatch)], composes helpers, and adds vectors without requiring the spellcraft attributes first. This section keeps the existing spellcraft APIs and production examples.

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::*;

#[magetypes(define(f32x8), v3, neon, wasm128, scalar)]
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) {
    incant!(gain_impl(plane, gain), [v3, neon, wasm128, scalar])
}

let mut data = [2.0; 11];
apply_gain(&mut data, 0.5);
assert_eq!(data, [1.0; 11]);

#[magetypes] compiles gain_impl once per tier in its list, and incant! runs the best one the CPU supports. Call it around your loop, as here, not inside it. The kernel is adapted from zenfilters: Reusable generic kernels links the production source.

Continue with the production patterns

  1. Type and const generics: pixel types, mode specialization, and turbofish dispatch.
  2. Reusable backend helpers: the complete zenblend SrcOver chain.
  3. Memory, bounds, and tails: array references and row/strip contracts.
  4. Direct intrinsic helpers: a feature-enabled entry and matched helper.
  5. ISA quirks and fixups: the numerical contract and its costs.
  6. 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 #[magetypes]; neither is a substitute for a feature-enabled entry. Inspect optimized code and benchmark the complete loop before claiming zero overhead.

Installation · Intrinsics browser · API reference