archmage 0.9.17

Safely invoke your intrinsic power, using the tokens granted to you by the CPU. Cast primitive magics faster than any mage alive.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// incant!/magetypes dispatch generates _v3 etc. for x86_64, not for i686
#![cfg(any(
    target_arch = "x86_64",
    target_arch = "aarch64",
    target_arch = "wasm32"
))]

use archmage::{IntoConcreteToken, ScalarToken, SimdToken, incant, magetypes};

#[magetypes]
pub fn dot(token: Token, a: &[f32], b: &[f32]) -> f32 {
    let _ = token;
    a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
}

pub fn dot_product(a: &[f32], b: &[f32]) -> f32 {
    incant!(dot(a, b))
}

#[magetypes]
pub fn normalize(token: Token, data: &mut [f32]) {
    let _ = token;
    let sum: f32 = data.iter().map(|x| x * x).sum();
    if sum > 0.0 {
        let inv_norm = 1.0 / sum.sqrt();
        for x in data.iter_mut() {
            *x *= inv_norm;
        }
    }
}

pub fn normalize_api(data: &mut [f32]) {
    incant!(normalize(data))
}

#[magetypes]
pub fn scale(token: Token, data: &mut [f32], factor: f32) {
    let _ = token;
    for x in data.iter_mut() {
        *x *= factor;
    }
}

fn scale_dispatch<T: IntoConcreteToken>(token: T, data: &mut [f32], factor: f32) {
    incant!(scale(data, factor) with token)
}

#[magetypes]
pub fn min_max(token: Token, data: &[f32]) -> (f32, f32) {
    let _ = token;
    if data.is_empty() {
        return (f32::NAN, f32::NAN);
    }
    let mut min = data[0];
    let mut max = data[0];
    for &x in &data[1..] {
        if x < min {
            min = x;
        }
        if x > max {
            max = x;
        }
    }
    (min, max)
}

pub fn min_max_api(data: &[f32]) -> (f32, f32) {
    incant!(min_max(data))
}

#[magetypes]
pub fn square(token: Token, data: &mut [f32]) {
    let _ = token;
    for x in data.iter_mut() {
        *x *= *x;
    }
}

#[magetypes]
pub fn add_const(token: Token, data: &mut [f32], val: f32) {
    let _ = token;
    for x in data.iter_mut() {
        *x += val;
    }
}

pub fn square_plus_one(data: &mut [f32]) {
    incant!(square(data));
    incant!(add_const(data, 1.0));
}

#[magetypes]
pub fn identity(token: Token, x: f32) -> f32 {
    let _ = token;
    x
}

pub fn identity_api(x: f32) -> f32 {
    incant!(identity(x))
}

// =============================================================================
// Tests
// =============================================================================

#[test]
fn dot_product_basic() {
    let a = [1.0, 2.0, 3.0, 4.0];
    let b = [4.0, 3.0, 2.0, 1.0];
    assert_eq!(dot_product(&a, &b), 20.0);
}

#[test]
fn normalize_unit_vector() {
    let mut data = [3.0f32, 4.0];
    normalize_api(&mut data);
    assert!((data[0] - 0.6).abs() < 1e-6);
    assert!((data[1] - 0.8).abs() < 1e-6);
}

#[test]
fn generic_dispatch_scalar() {
    let mut data = [1.0, 2.0, 3.0];
    scale_dispatch(ScalarToken, &mut data, 10.0);
    assert_eq!(data, [10.0, 20.0, 30.0]);
}

#[test]
fn chained_dispatch() {
    let mut data = [2.0, 3.0, 4.0];
    square_plus_one(&mut data);
    assert_eq!(data, [5.0, 10.0, 17.0]);
}

#[test]
fn min_max_basic() {
    let (min, max) = min_max_api(&[3.0, 1.0, 4.0, 1.0, 5.0, 9.0]);
    assert_eq!(min, 1.0);
    assert_eq!(max, 9.0);
}

#[test]
fn nan_passthrough() {
    assert!(identity_api(f32::NAN).is_nan());
}

mod alias_test {
    use super::*;
    use archmage::simd_route;

    pub fn dot_via_alias(a: &[f32], b: &[f32]) -> f32 {
        simd_route!(dot(a, b))
    }

    #[test]
    fn simd_route_identical_to_incant() {
        let a = [1.0, 2.0, 3.0];
        let b = [4.0, 5.0, 6.0];
        assert_eq!(dot_product(&a, &b), dot_via_alias(&a, &b));
    }
}

// =============================================================================
// Edge cases
// =============================================================================

#[test]
fn dot_product_empty() {
    assert_eq!(dot_product(&[], &[]), 0.0);
}

#[test]
fn dot_product_single() {
    assert_eq!(dot_product(&[5.0], &[3.0]), 15.0);
}

#[test]
fn dot_product_large() {
    let n = 1024;
    let a: Vec<f32> = (0..n).map(|i| i as f32).collect();
    let b: Vec<f32> = (0..n).map(|i| (n - i) as f32).collect();
    let expected: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    assert_eq!(dot_product(&a, &b), expected);
}

#[test]
fn normalize_zeros() {
    let mut data = [0.0f32, 0.0, 0.0];
    normalize_api(&mut data);
    assert_eq!(data, [0.0, 0.0, 0.0]);
}

#[test]
fn min_max_single() {
    let (min, max) = min_max_api(&[42.0]);
    assert_eq!(min, 42.0);
    assert_eq!(max, 42.0);
}

#[test]
fn infinity_passthrough() {
    assert_eq!(identity_api(f32::INFINITY), f32::INFINITY);
    assert_eq!(identity_api(f32::NEG_INFINITY), f32::NEG_INFINITY);
}

#[test]
fn negative_zero() {
    let result = identity_api(-0.0f32);
    assert!(result.is_sign_negative());
    assert_eq!(result, 0.0);
}

// =============================================================================
// Platform-specific dispatch verification
// =============================================================================

#[cfg(target_arch = "x86_64")]
mod x86_dispatch {
    use super::*;

    #[test]
    fn v3_dispatch() {
        if let Some(token) = archmage::X64V3Token::summon() {
            let mut data = [1.0, 2.0, 3.0];
            scale_dispatch(token, &mut data, 2.0);
            assert_eq!(data, [2.0, 4.0, 6.0]);
        }
    }

    #[cfg(feature = "avx512")]
    #[test]
    fn v4_dispatch() {
        if let Some(token) = archmage::X64V4Token::summon() {
            let mut data = [1.0, 2.0, 3.0];
            scale_dispatch(token, &mut data, 3.0);
            assert_eq!(data, [3.0, 6.0, 9.0]);
        }
    }
}

// =============================================================================
// ScalarToken fundamentals
// =============================================================================

#[test]
fn scalar_token_always_available() {
    assert_eq!(ScalarToken::compiled_with(), Some(true));
    assert!(ScalarToken::summon().is_some());
    assert_eq!(ScalarToken::NAME, "Scalar");
}

#[test]
fn scalar_token_into_concrete() {
    let token = ScalarToken;
    assert!(token.as_scalar().is_some());
    assert!(token.as_x64v2().is_none());
    assert!(token.as_x64v3().is_none());
    assert!(token.as_neon().is_none());
    assert!(token.as_neon_aes().is_none());
    assert!(token.as_neon_sha3().is_none());
    assert!(token.as_neon_crc().is_none());
    assert!(token.as_wasm128().is_none());
}

#[cfg(target_arch = "x86_64")]
#[test]
fn x64v3_into_concrete() {
    if let Some(token) = archmage::X64V3Token::summon() {
        assert!(token.as_x64v3().is_some());
        assert!(token.as_scalar().is_none());
        assert!(token.as_x64v2().is_none());
        assert!(token.as_neon().is_none());
        assert!(token.as_neon_aes().is_none());
        assert!(token.as_neon_sha3().is_none());
        assert!(token.as_neon_crc().is_none());
        assert!(token.as_wasm128().is_none());
    }
}

// =============================================================================
// IntoConcreteToken — all tokens match only themselves
// =============================================================================

#[test]
fn x64v2_into_concrete() {
    let token = archmage::X64V2Token::summon();
    if let Some(t) = token {
        assert!(t.as_x64v2().is_some());
        assert!(t.as_x64v3().is_none());
        assert!(t.as_scalar().is_none());
        assert!(t.as_neon().is_none());
        assert!(t.as_neon_aes().is_none());
        assert!(t.as_neon_sha3().is_none());
        assert!(t.as_neon_crc().is_none());
        assert!(t.as_wasm128().is_none());
    }
}

#[test]
fn neon_into_concrete() {
    let token = archmage::NeonToken::summon();
    if let Some(t) = token {
        assert!(t.as_neon().is_some());
        assert!(t.as_neon_aes().is_none());
        assert!(t.as_neon_sha3().is_none());
        assert!(t.as_neon_crc().is_none());
        assert!(t.as_scalar().is_none());
        assert!(t.as_x64v2().is_none());
        assert!(t.as_x64v3().is_none());
        assert!(t.as_wasm128().is_none());
    }
}

#[test]
fn neon_aes_into_concrete() {
    let token = archmage::NeonAesToken::summon();
    if let Some(t) = token {
        assert!(t.as_neon_aes().is_some());
        assert!(t.as_neon().is_none());
        assert!(t.as_neon_sha3().is_none());
        assert!(t.as_neon_crc().is_none());
        assert!(t.as_scalar().is_none());
        assert!(t.as_x64v2().is_none());
        assert!(t.as_x64v3().is_none());
        assert!(t.as_wasm128().is_none());
    }
}

#[test]
fn neon_sha3_into_concrete() {
    let token = archmage::NeonSha3Token::summon();
    if let Some(t) = token {
        assert!(t.as_neon_sha3().is_some());
        assert!(t.as_neon().is_none());
        assert!(t.as_neon_aes().is_none());
        assert!(t.as_neon_crc().is_none());
        assert!(t.as_scalar().is_none());
    }
}

#[test]
fn neon_crc_into_concrete() {
    let token = archmage::NeonCrcToken::summon();
    if let Some(t) = token {
        assert!(t.as_neon_crc().is_some());
        assert!(t.as_neon().is_none());
        assert!(t.as_neon_aes().is_none());
        assert!(t.as_neon_sha3().is_none());
        assert!(t.as_scalar().is_none());
    }
}

#[test]
fn wasm128_into_concrete() {
    let token = archmage::Wasm128Token::summon();
    if let Some(t) = token {
        assert!(t.as_wasm128().is_some());
        assert!(t.as_scalar().is_none());
        assert!(t.as_neon().is_none());
        assert!(t.as_neon_aes().is_none());
        assert!(t.as_neon_sha3().is_none());
        assert!(t.as_neon_crc().is_none());
        assert!(t.as_x64v2().is_none());
        assert!(t.as_x64v3().is_none());
    }
}

#[cfg(feature = "avx512")]
mod avx512_into_concrete {
    use archmage::{IntoConcreteToken, SimdToken};

    #[test]
    fn x64v4_into_concrete() {
        if let Some(t) = archmage::X64V4Token::summon() {
            assert!(t.as_x64v4().is_some());
            assert!(t.as_x64v4x().is_none());
            assert!(t.as_avx512_fp16().is_none());
            assert!(t.as_x64v2().is_none());
            assert!(t.as_x64v3().is_none());
            assert!(t.as_scalar().is_none());
            assert!(t.as_neon().is_none());
        }
    }

    #[test]
    fn x64v4x_into_concrete() {
        if let Some(t) = archmage::X64V4xToken::summon() {
            assert!(t.as_x64v4x().is_some());
            assert!(t.as_x64v4().is_none());
            assert!(t.as_avx512_fp16().is_none());
            assert!(t.as_x64v2().is_none());
            assert!(t.as_x64v3().is_none());
            assert!(t.as_scalar().is_none());
        }
    }

    #[test]
    fn avx512_fp16_into_concrete() {
        if let Some(t) = archmage::Avx512Fp16Token::summon() {
            assert!(t.as_avx512_fp16().is_some());
            assert!(t.as_x64v4x().is_none());
            assert!(t.as_x64v4().is_none());
            assert!(t.as_x64v2().is_none());
            assert!(t.as_x64v3().is_none());
            assert!(t.as_scalar().is_none());
        }
    }
}

// =============================================================================
// Generic dispatch with sub-tier tokens
// =============================================================================

/// Verify that IntoConcreteToken enables manual dispatch for sub-tier tokens
fn dispatch_with_sub_tiers<T: IntoConcreteToken>(token: T) -> &'static str {
    if token.as_neon_aes().is_some() {
        return "neon_aes";
    }
    if token.as_neon_sha3().is_some() {
        return "neon_sha3";
    }
    if token.as_neon_crc().is_some() {
        return "neon_crc";
    }
    if token.as_neon().is_some() {
        return "neon";
    }
    if token.as_scalar().is_some() {
        return "scalar";
    }
    "unknown"
}

#[test]
fn sub_tier_dispatch_scalar() {
    assert_eq!(dispatch_with_sub_tiers(ScalarToken), "scalar");
}

#[cfg(target_arch = "aarch64")]
#[test]
fn sub_tier_dispatch_neon() {
    if let Some(token) = archmage::NeonToken::summon() {
        assert_eq!(dispatch_with_sub_tiers(token), "neon");
    }
    if let Some(token) = archmage::NeonAesToken::summon() {
        assert_eq!(dispatch_with_sub_tiers(token), "neon_aes");
    }
}