archmage
Safely invoke your intrinsic power, using the tokens granted to you by the CPU. Cast primitive magics faster than any mage alive.
archmage provides capability tokens that prove CPU feature availability at runtime, making raw SIMD intrinsics safe to call via the #[arcane] macro.
Quick Start
use ;
use avx; // safe load/store (enabled by default)
use *;
How It Works
The Problem
Raw SIMD intrinsics have two safety concerns:
- Feature availability: Calling
_mm256_add_pson a CPU without AVX is undefined behavior - Memory safety:
_mm256_loadu_ps(ptr)dereferences a raw pointer
Rust 1.85+ made value-based intrinsics safe inside #[target_feature] functions, but calling those functions is still unsafe because the compiler can't verify the CPU supports the features.
The Solution: Tokens + #[arcane]
archmage solves this with two components:
1. Capability Tokens - Zero-sized proof types created after runtime CPU detection:
use ;
// summon() checks CPUID and returns Some only if features are available
// (check is elided if compiled with -C target-cpu=native or similar)
if let Some = summon
2. The #[arcane] Macro - Transforms your function to enable #[target_feature]:
The macro generates:
Why is this safe?
inner()has#[target_feature(enable = "avx2")], so Rust allows intrinsics withoutunsafe- Calling
inner()is unsafe, but we know it's valid because:- The function requires a token parameter
- Tokens can only be created via
summon()which checks CPU features - Therefore, if you have a token, the CPU supports the features
Generic Token Bounds
Functions accept any token that provides the required capabilities:
use ;
use avx;
use *;
// Accept any token with AVX2 (Avx2Token, Desktop64, Server64, etc.)
// Require multiple features with inline bounds
// Where clause syntax
The trait hierarchy means broader tokens satisfy narrower bounds:
Desktop64implementsHasAvx2,HasFma,HasSse42, etc.Server64implements everythingDesktop64does, plusHasAvx512f, etc.
Choosing a Token
Start with Desktop64 - it's the sweet spot for modern x86-64:
| Token | Features | Hardware Coverage |
|---|---|---|
Desktop64 |
AVX2 + FMA + BMI2 | Intel Haswell 2013+, AMD Zen 1 2017+ (~95% of x86-64) |
Server64 |
+ AVX-512 | Intel Skylake-X 2017+, AMD Zen 4 2022+ |
X64V2Token |
SSE4.2 + POPCNT | Intel Nehalem 2008+, AMD Bulldozer 2011+ |
For specific features:
| Token | Use Case |
|---|---|
Avx2Token |
Need AVX2 but not FMA |
Avx2FmaToken |
AVX2 + FMA (most floating-point SIMD) |
FmaToken |
FMA only |
Sse2Token |
Baseline x86-64 (always available) |
ARM tokens:
| Token | Features | Hardware |
|---|---|---|
NeonToken |
NEON | All AArch64 (baseline, including Apple M-series) |
SveToken |
SVE | Graviton 3, A64FX |
Sve2Token |
SVE2 | ARMv9: Graviton 4, Cortex-X2+ |
Safe Memory Operations
With the safe_unaligned_simd feature, load/store uses references instead of raw pointers:
use ;
use avx;
if let Some = summon
The mem module wrappers accept impl HasAvx, impl HasSse2, etc., so any compatible token works.
When to Use archmage
archmage is for when you need specific instructions that autovectorization won't produce:
- Complex shuffles and permutes
- Exact FMA sequences for numerical precision
- DCT butterflies and signal processing
- Gather/scatter operations
- Bit manipulation (BMI1/BMI2)
For portable SIMD without manual intrinsics, use the wide crate instead.
| Approach | When to Use |
|---|---|
| wide | Portable code, let the compiler choose instructions |
| archmage | Need specific instructions, complex algorithms |
Feature Flags
[]
= "0.1"
| Feature | Description |
|---|---|
std (default) |
Enable std library support |
macros (default) |
Enable #[arcane] macro (alias: #[simd_fn]) |
safe_unaligned_simd (default) |
Safe load/store via references (exposed as mem module) |
Unstable features (API may change):
| Feature | Description |
|---|---|
__composite |
Higher-level ops (transpose, dot product) |
__wide |
Integration with the wide crate |
Limitations
Self receivers not supported in #[arcane]:
// This won't work - inner functions can't have `self`
// Instead, take self as a regular parameter or use free functions
License
MIT OR Apache-2.0
AI-Generated Code Notice
Developed with Claude (Anthropic). Not all code manually reviewed. Review critical paths before production use.