archmage 0.9.21

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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//! Tests for the incant! macro.
//!
//! Tests cover:
//! - Entry point mode (summons tokens)
//! - Passthrough mode (uses existing token)
//! - Scalar fallback
//! - Cross-architecture compilation

// incant! generates _v3/_v4 for x86_64, _neon for aarch64, _wasm128 for wasm32.
// On i686 (target_arch = "x86") none of these exist.
#![cfg(any(
    target_arch = "x86_64",
    target_arch = "aarch64",
    target_arch = "wasm32"
))]

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

// =============================================================================
// Test helper functions with required suffixes
// =============================================================================

// Scalar implementation - always available
fn sum_scalar(_token: ScalarToken, data: &[f32]) -> f32 {
    data.iter().sum()
}

// x86 implementations
#[cfg(target_arch = "x86_64")]
fn sum_v3(_token: archmage::X64V3Token, data: &[f32]) -> f32 {
    // In real code, use SIMD. For tests, just verify dispatch works.
    data.iter().sum::<f32>() * 1.0 // Multiply by 1.0 to distinguish from scalar
}

#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
fn sum_v4(_token: archmage::X64V4Token, data: &[f32]) -> f32 {
    data.iter().sum::<f32>() * 1.0
}

// ARM implementation
#[cfg(target_arch = "aarch64")]
fn sum_neon(_token: archmage::NeonToken, data: &[f32]) -> f32 {
    data.iter().sum::<f32>() * 1.0
}

// WASM implementation
#[cfg(target_arch = "wasm32")]
fn sum_wasm128(_token: archmage::Wasm128Token, data: &[f32]) -> f32 {
    data.iter().sum::<f32>() * 1.0
}

// =============================================================================
// V1/V2 tier variants — test lower-tier dispatch
// =============================================================================

// V1 (SSE2 baseline — always available on x86_64)
#[cfg(target_arch = "x86_64")]
fn add_one_v1(_token: archmage::X64V1Token, data: &[f32]) -> Vec<f32> {
    data.iter().map(|x| x + 1.0).collect()
}

// V2 (SSE4.2 — available on almost all x86_64 CPUs since 2008)
#[cfg(target_arch = "x86_64")]
fn add_one_v2(_token: archmage::X64V2Token, data: &[f32]) -> Vec<f32> {
    data.iter().map(|x| x + 1.0).collect()
}

fn add_one_scalar(_token: ScalarToken, data: &[f32]) -> Vec<f32> {
    data.iter().map(|x| x + 1.0).collect()
}

// =============================================================================
// Entry point mode tests
// =============================================================================

mod entry_point_tests {
    use super::*;
    use archmage::incant;

    /// Public API using incant! for dispatch
    pub fn sum_api(data: &[f32]) -> f32 {
        incant!(sum(data))
    }

    #[test]
    fn entry_point_dispatches() {
        let data = [1.0f32, 2.0, 3.0, 4.0];
        let result = sum_api(&data);
        assert_eq!(result, 10.0);
    }

    #[test]
    fn entry_point_with_empty_data() {
        let data: [f32; 0] = [];
        let result = sum_api(&data);
        assert_eq!(result, 0.0);
    }

    #[test]
    fn entry_point_with_large_data() {
        let data: Vec<f32> = (0..1000).map(|i| i as f32).collect();
        let result = sum_api(&data);
        let expected: f32 = (0..1000).map(|i| i as f32).sum();
        assert_eq!(result, expected);
    }
}

// =============================================================================
// V1/V2 dispatch tests — verify lower tiers work with incant!
// =============================================================================

mod v1_v2_dispatch_tests {
    use super::*;
    use archmage::incant;

    /// incant! with explicit [v1, scalar] dispatches to _v1 on x86_64.
    /// V1 is the x86_64 baseline (SSE2) — always available.
    pub fn add_one_v1_api(data: &[f32]) -> Vec<f32> {
        incant!(add_one(data), [v1, scalar])
    }

    #[test]
    fn v1_dispatch_works() {
        let data = [1.0f32, 2.0, 3.0];
        let result = add_one_v1_api(&data);
        assert_eq!(result, vec![2.0, 3.0, 4.0]);
    }

    /// incant! with explicit [v2, v1, scalar] tries V2 first, falls back to V1.
    pub fn add_one_v2_api(data: &[f32]) -> Vec<f32> {
        incant!(add_one(data), [v2, v1, scalar])
    }

    #[test]
    fn v2_dispatch_works() {
        let data = [10.0f32, 20.0];
        let result = add_one_v2_api(&data);
        assert_eq!(result, vec![11.0, 21.0]);
    }

    /// Verify V1 is always reached on x86_64 (it's baseline).
    #[cfg(target_arch = "x86_64")]
    #[test]
    fn v1_always_available_on_x86_64() {
        assert!(
            archmage::X64V1Token::summon().is_some(),
            "V1 is x86_64 baseline — must always be Some"
        );
    }

    /// Verify V2 dispatch still falls through correctly when
    /// only scalar is available (non-x86_64).
    #[cfg(not(target_arch = "x86_64"))]
    #[test]
    fn v1_v2_fall_through_to_scalar_on_non_x86() {
        let data = [5.0f32];
        let result = add_one_v1_api(&data);
        assert_eq!(result, vec![6.0]);
        let result = add_one_v2_api(&data);
        assert_eq!(result, vec![6.0]);
    }
}

// =============================================================================
// Passthrough mode tests
// =============================================================================

mod passthrough_tests {
    use super::*;
    use archmage::incant;

    /// Inner function called via passthrough
    fn inner_sum<T: IntoConcreteToken>(token: T, data: &[f32]) -> f32 {
        incant!(sum(data) with token)
    }

    #[test]
    fn passthrough_with_scalar_token() {
        let token = ScalarToken;
        let data = [1.0f32, 2.0, 3.0, 4.0];
        let result = inner_sum(token, &data);
        assert_eq!(result, 10.0);
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn passthrough_with_x64v3_token() {
        if let Some(token) = archmage::X64V3Token::summon() {
            let data = [1.0f32, 2.0, 3.0, 4.0];
            let result = inner_sum(token, &data);
            assert_eq!(result, 10.0);
        }
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    #[test]
    fn passthrough_with_x64v4_token() {
        if let Some(token) = archmage::X64V4Token::summon() {
            let data = [1.0f32, 2.0, 3.0, 4.0];
            let result = inner_sum(token, &data);
            assert_eq!(result, 10.0);
        }
    }
}

// =============================================================================
// Scalar fallback tests
// =============================================================================

mod scalar_fallback_tests {
    use super::*;
    use archmage::incant;

    // All variants must exist for incant! to work.
    // The scalar variant is the fallback when no SIMD is available.
    fn double_scalar(_token: ScalarToken, x: i32) -> i32 {
        x * 2
    }

    #[cfg(target_arch = "x86_64")]
    fn double_v3(_token: archmage::X64V3Token, x: i32) -> i32 {
        x * 2
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    fn double_v4(_token: archmage::X64V4Token, x: i32) -> i32 {
        x * 2
    }

    #[cfg(target_arch = "aarch64")]
    fn double_neon(_token: archmage::NeonToken, x: i32) -> i32 {
        x * 2
    }

    #[cfg(target_arch = "wasm32")]
    fn double_wasm128(_token: archmage::Wasm128Token, x: i32) -> i32 {
        x * 2
    }

    pub fn double_api(x: i32) -> i32 {
        incant!(double(x))
    }

    #[test]
    fn works_with_all_variants() {
        let result = double_api(21);
        assert_eq!(result, 42);
    }

    #[test]
    fn scalar_token_always_available() {
        // ScalarToken itself is always available
        assert!(ScalarToken::summon().is_some());
        assert_eq!(ScalarToken::compiled_with(), Some(true));
    }
}

// =============================================================================
// Multiple arguments tests
// =============================================================================

mod multi_arg_tests {
    use super::*;
    use archmage::incant;

    fn dot_scalar(_token: ScalarToken, a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    #[cfg(target_arch = "x86_64")]
    fn dot_v3(_token: archmage::X64V3Token, a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    fn dot_v4(_token: archmage::X64V4Token, a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    #[cfg(target_arch = "aarch64")]
    fn dot_neon(_token: archmage::NeonToken, a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    #[cfg(target_arch = "wasm32")]
    fn dot_wasm128(_token: archmage::Wasm128Token, a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

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

    #[test]
    fn multiple_arguments() {
        let a = [1.0f32, 2.0, 3.0, 4.0];
        let b = [4.0f32, 3.0, 2.0, 1.0];
        let result = dot_api(&a, &b);
        // 1*4 + 2*3 + 3*2 + 4*1 = 4 + 6 + 6 + 4 = 20
        assert_eq!(result, 20.0);
    }
}

// =============================================================================
// Return type tests
// =============================================================================

mod return_type_tests {
    use super::*;
    use archmage::incant;

    fn make_array_scalar(_token: ScalarToken, val: f32) -> [f32; 4] {
        [val, val, val, val]
    }

    #[cfg(target_arch = "x86_64")]
    fn make_array_v3(_token: archmage::X64V3Token, val: f32) -> [f32; 4] {
        [val, val, val, val]
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    fn make_array_v4(_token: archmage::X64V4Token, val: f32) -> [f32; 4] {
        [val, val, val, val]
    }

    #[cfg(target_arch = "aarch64")]
    fn make_array_neon(_token: archmage::NeonToken, val: f32) -> [f32; 4] {
        [val, val, val, val]
    }

    #[cfg(target_arch = "wasm32")]
    fn make_array_wasm128(_token: archmage::Wasm128Token, val: f32) -> [f32; 4] {
        [val, val, val, val]
    }

    pub fn make_array_api(val: f32) -> [f32; 4] {
        incant!(make_array(val))
    }

    #[test]
    fn returns_array() {
        let result = make_array_api(3.125);
        assert_eq!(result, [3.125, 3.125, 3.125, 3.125]);
    }
}

// =============================================================================
// simd_route! alias tests
// =============================================================================

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

    fn add_scalar(_token: ScalarToken, a: i32, b: i32) -> i32 {
        a + b
    }

    #[cfg(target_arch = "x86_64")]
    fn add_v3(_token: archmage::X64V3Token, a: i32, b: i32) -> i32 {
        a + b
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    fn add_v4(_token: archmage::X64V4Token, a: i32, b: i32) -> i32 {
        a + b
    }

    #[cfg(target_arch = "aarch64")]
    fn add_neon(_token: archmage::NeonToken, a: i32, b: i32) -> i32 {
        a + b
    }

    #[cfg(target_arch = "wasm32")]
    fn add_wasm128(_token: archmage::Wasm128Token, a: i32, b: i32) -> i32 {
        a + b
    }

    pub fn add_api(a: i32, b: i32) -> i32 {
        simd_route!(add(a, b))
    }

    #[test]
    fn simd_route_alias_works() {
        let result = add_api(20, 22);
        assert_eq!(result, 42);
    }
}

// =============================================================================
// IntoConcreteToken direct usage tests
// =============================================================================

// =============================================================================
// Module path tests (incant! with module::func syntax)
// =============================================================================

mod simd_impls {
    use archmage::ScalarToken;

    pub fn triple_scalar(_token: ScalarToken, x: i32) -> i32 {
        x * 3
    }

    #[cfg(target_arch = "x86_64")]
    pub fn triple_v3(_token: archmage::X64V3Token, x: i32) -> i32 {
        x * 3
    }

    #[cfg(all(target_arch = "x86_64", feature = "avx512"))]
    pub fn triple_v4(_token: archmage::X64V4Token, x: i32) -> i32 {
        x * 3
    }

    #[cfg(target_arch = "aarch64")]
    pub fn triple_neon(_token: archmage::NeonToken, x: i32) -> i32 {
        x * 3
    }

    #[cfg(target_arch = "wasm32")]
    pub fn triple_wasm128(_token: archmage::Wasm128Token, x: i32) -> i32 {
        x * 3
    }
}

mod module_path_tests {
    use archmage::incant;

    pub fn triple_api(x: i32) -> i32 {
        incant!(super::simd_impls::triple(x))
    }

    #[test]
    fn module_path_entry_point() {
        let result = triple_api(7);
        assert_eq!(result, 21);
    }

    #[test]
    fn module_path_passthrough() {
        use archmage::{IntoConcreteToken, ScalarToken};
        fn inner<T: IntoConcreteToken>(token: T, x: i32) -> i32 {
            incant!(super::simd_impls::triple(x) with token)
        }
        let result = inner(ScalarToken, 14);
        assert_eq!(result, 42);
    }
}

// =============================================================================
// IntoConcreteToken direct usage tests
// =============================================================================

mod into_concrete_token_tests {
    use super::*;

    /// Manual dispatch using IntoConcreteToken (what incant! generates)
    fn manual_dispatch<T: IntoConcreteToken>(token: T, data: &[f32]) -> f32 {
        #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
        {
            #[cfg(feature = "avx512")]
            if let Some(t) = token.as_x64v4() {
                return super::sum_v4(t, data);
            }
            if let Some(t) = token.as_x64v3() {
                return super::sum_v3(t, data);
            }
        }

        #[cfg(target_arch = "aarch64")]
        if let Some(t) = token.as_neon() {
            return super::sum_neon(t, data);
        }

        #[cfg(target_arch = "wasm32")]
        if let Some(t) = token.as_wasm128() {
            return super::sum_wasm128(t, data);
        }

        if let Some(t) = token.as_scalar() {
            return super::sum_scalar(t, data);
        }

        unreachable!()
    }

    #[test]
    fn manual_dispatch_scalar() {
        let data = [1.0f32, 2.0, 3.0, 4.0];
        let result = manual_dispatch(ScalarToken, &data);
        assert_eq!(result, 10.0);
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn manual_dispatch_x64v3() {
        if let Some(token) = archmage::X64V3Token::summon() {
            let data = [1.0f32, 2.0, 3.0, 4.0];
            let result = manual_dispatch(token, &data);
            assert_eq!(result, 10.0);
        }
    }
}

// =============================================================================
// `default` tier — tokenless fallback
// =============================================================================

mod default_tier_tests {
    use archmage::prelude::*;

    // -- Entry-point mode with default --

    fn entry_default(x: f32) -> f32 {
        x * 99.0
    }

    #[arcane]
    fn entry_v3(_: X64V3Token, x: f32) -> f32 {
        x * 30.0
    }

    fn entry_dispatch(x: f32) -> f32 {
        incant!(entry(x), [v3, default])
    }

    #[test]
    fn entry_point_default_dispatches() {
        let result = entry_dispatch(1.0);
        #[cfg(target_arch = "x86_64")]
        if X64V3Token::summon().is_some() {
            assert_eq!(result, 30.0);
        } else {
            assert_eq!(result, 99.0);
        }
        #[cfg(not(target_arch = "x86_64"))]
        assert_eq!(result, 99.0);
    }

    // -- Entry-point mode, default only (no other tiers) --

    fn default_only_default(x: f32) -> f32 {
        x + 42.0
    }

    fn default_only_dispatch(x: f32) -> f32 {
        incant!(default_only(x), [default])
    }

    #[test]
    fn default_only_works() {
        assert_eq!(default_only_dispatch(1.0), 43.0);
    }

    // -- Entry-point with multiple args --

    fn multi_arg_default(a: f32, b: f32, c: f32) -> f32 {
        a + b + c
    }

    #[arcane]
    fn multi_arg_v3(_: X64V3Token, a: f32, b: f32, c: f32) -> f32 {
        (a + b + c) * 100.0
    }

    fn multi_arg_dispatch(a: f32, b: f32, c: f32) -> f32 {
        incant!(multi_arg(a, b, c), [v3, default])
    }

    #[test]
    fn default_with_multiple_args() {
        let result = multi_arg_dispatch(1.0, 2.0, 3.0);
        #[cfg(target_arch = "x86_64")]
        if X64V3Token::summon().is_some() {
            assert_eq!(result, 600.0);
        } else {
            assert_eq!(result, 6.0);
        }
        #[cfg(not(target_arch = "x86_64"))]
        assert_eq!(result, 6.0);
    }

    // -- Passthrough mode with default --

    fn passthrough_default(x: f32) -> f32 {
        x * 77.0
    }

    #[arcane]
    fn passthrough_v3(_: X64V3Token, x: f32) -> f32 {
        x * 33.0
    }

    fn passthrough_dispatch<T: IntoConcreteToken>(token: T, x: f32) -> f32 {
        incant!(passthrough(x) with token, [v3, default])
    }

    #[test]
    fn passthrough_default_with_scalar() {
        let result = passthrough_dispatch(ScalarToken, 1.0);
        // ScalarToken doesn't match v3, falls through to default (tokenless)
        assert_eq!(result, 77.0);
    }

    #[cfg(target_arch = "x86_64")]
    #[test]
    fn passthrough_default_with_v3() {
        if let Some(token) = X64V3Token::summon() {
            let result = passthrough_dispatch(token, 1.0);
            assert_eq!(result, 33.0);
        }
    }

    // -- Verify default is called without token (tokenless) --

    // This function takes NO token. If incant! tried to pass one, it wouldn't compile.
    fn no_token_default(x: f32) -> f32 {
        x
    }

    fn no_token_dispatch(x: f32) -> f32 {
        incant!(no_token(x), [default])
    }

    #[test]
    fn default_is_truly_tokenless() {
        assert_eq!(no_token_dispatch(42.0), 42.0);
    }
}