archmage 0.9.23

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
//! Tests that verify feature detection is consistent with CPU capabilities.
//!
//! These tests run actual SIMD operations to verify that when a token is available,
//! the corresponding instructions actually work. If feature detection lies about
//! capabilities, these tests will crash (SIGILL).
//!
//! Cross-platform: tests for x86_64, aarch64, and wasm32.
//!
//! The `x64_tokens_are_x86_64_only` and `compiled_with_reflects_architecture`
//! tests verify that X64* tokens are stubs on non-x86_64 targets (including
//! 32-bit x86). Run on i686 to verify:
//!   cross test --target i686-unknown-linux-gnu --test feature_consistency

use archmage::SimdToken;
use archmage::*;

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

    /// SSE2 is baseline on x86_64 and always works.
    #[test]
    fn sse2_instructions_work_baseline() {
        // SSE2 is baseline on x86_64, no token needed
        unsafe {
            use core::arch::x86_64::*;
            let a = _mm_setzero_ps();
            let b = _mm_set1_ps(1.0);
            let c = _mm_add_ps(a, b);
            std::hint::black_box(c);
        }
    }

    /// If X64V2Token is available, SSE4.2 instructions must work.
    #[test]
    fn x64v2_instructions_work_when_token_available() {
        if let Some(_token) = X64V2Token::summon() {
            unsafe {
                use core::arch::x86_64::*;
                let result = _mm_crc32_u8(0, 0);
                std::hint::black_box(result);
                let v = _mm_set1_epi32(42);
                let extracted = _mm_extract_epi32::<0>(v);
                std::hint::black_box(extracted);
            }
        }
    }

    /// If X64V3Token is available, AVX2 and FMA instructions must work.
    #[test]
    fn x64v3_instructions_work_when_token_available() {
        if let Some(_token) = X64V3Token::summon() {
            unsafe {
                use core::arch::x86_64::*;
                let a = _mm256_setzero_ps();
                let b = _mm256_set1_ps(1.0);
                let c = _mm256_add_ps(a, b);
                std::hint::black_box(c);
                let d = _mm256_setzero_si256();
                let e = _mm256_set1_epi32(1);
                let f = _mm256_add_epi32(d, e);
                std::hint::black_box(f);
                let g = _mm256_set1_ps(1.0);
                let h = _mm256_set1_ps(2.0);
                let i = _mm256_set1_ps(3.0);
                let j = _mm256_fmadd_ps(g, h, i);
                std::hint::black_box(j);
            }
        }
    }

    /// If X64CryptoToken is available, PCLMULQDQ and AES-NI must work.
    #[test]
    fn x64_crypto_instructions_work_when_token_available() {
        if let Some(_token) = X64CryptoToken::summon() {
            unsafe {
                use core::arch::x86_64::*;
                let a = _mm_set_epi64x(0x0123456789ABCDEFi64, 0x1111111111111111i64);
                let clmul = _mm_clmulepi64_si128::<0x00>(a, a);
                std::hint::black_box(clmul);
                let key = _mm_setzero_si128();
                let enc = _mm_aesenc_si128(a, key);
                std::hint::black_box(enc);
            }
        }
    }

    /// If X64V3CryptoToken is available, VPCLMULQDQ and VAES must work.
    #[test]
    fn x64v3_crypto_instructions_work_when_token_available() {
        if let Some(_token) = X64V3CryptoToken::summon() {
            unsafe {
                use core::arch::x86_64::*;
                let a = _mm256_set1_epi64x(0x0123456789ABCDEFi64);
                let clmul = _mm256_clmulepi64_epi128::<0x00>(a, a);
                std::hint::black_box(clmul);
                let key = _mm256_setzero_si256();
                let enc = _mm256_aesenc_epi128(a, key);
                std::hint::black_box(enc);
            }
        }
    }

    /// If X64V4Token is available, AVX-512 instructions must work.
    #[cfg(feature = "avx512")]
    #[test]
    fn x64v4_instructions_work_when_token_available() {
        if let Some(_token) = X64V4Token::summon() {
            unsafe {
                use core::arch::x86_64::*;
                let a = _mm512_setzero_ps();
                let b = _mm512_set1_ps(1.0);
                let c = _mm512_add_ps(a, b);
                std::hint::black_box(c);
            }
        }
    }

    /// Verify token hierarchy: v4 implies v3 implies v2.
    #[test]
    fn token_hierarchy_is_correct() {
        if X64V3Token::summon().is_some() {
            assert!(
                X64V2Token::summon().is_some(),
                "v3 implies v2 should be available"
            );
            assert!(
                Avx2FmaToken::summon().is_some(),
                "v3 implies Avx2FmaToken (same type)"
            );
        }

        #[cfg(feature = "avx512")]
        if X64V4Token::summon().is_some() {
            assert!(
                X64V3Token::summon().is_some(),
                "v4 implies v3 should be available"
            );
            assert!(
                X64V2Token::summon().is_some(),
                "v4 implies v2 should be available"
            );
        }

        // Crypto hierarchy
        if X64CryptoToken::summon().is_some() {
            assert!(
                X64V2Token::summon().is_some(),
                "Crypto implies v2 should be available"
            );
        }
        if X64V3CryptoToken::summon().is_some() {
            assert!(
                X64V3Token::summon().is_some(),
                "V3Crypto implies v3 should be available"
            );
            assert!(
                X64CryptoToken::summon().is_some(),
                "V3Crypto implies Crypto should be available"
            );
        }
    }

    #[test]
    fn avx2fma_is_x64v3() {
        let v3 = X64V3Token::summon();
        let avx2fma = Avx2FmaToken::summon();
        assert_eq!(v3.is_some(), avx2fma.is_some());
        assert_eq!(Avx2FmaToken::NAME, X64V3Token::NAME);
    }

    #[test]
    fn print_detected_features() {
        println!("Feature detection results (x86_64):");
        println!("  SSE2:         always (baseline on x86_64)");
        println!("  x86-64-v2:    {}", X64V2Token::summon().is_some());
        println!("  x86 Crypto:   {}", X64CryptoToken::summon().is_some());
        println!(
            "  x86-64-v3:    {} (Desktop64/Avx2FmaToken)",
            X64V3Token::summon().is_some()
        );
        println!(
            "  x86-64-v3 Crypto: {}",
            X64V3CryptoToken::summon().is_some()
        );
        #[cfg(feature = "avx512")]
        {
            println!(
                "  x86-64-v4:    {} (Avx512Token/Server64)",
                X64V4Token::summon().is_some()
            );
            println!("  AVX-512 Modern: {}", X64V4xToken::summon().is_some());
            println!("  AVX-512 FP16:   {}", Avx512Fp16Token::summon().is_some());
        }
    }
}

// ============================================================================
// AArch64 Tests
// ============================================================================
#[cfg(target_arch = "aarch64")]
mod aarch64_tests {
    use super::*;

    /// NEON is baseline on aarch64 — NeonToken::summon() should always succeed.
    #[test]
    fn neon_always_available() {
        assert!(
            NeonToken::summon().is_some(),
            "NEON should always be available on aarch64"
        );
    }

    /// If NeonToken is available, basic NEON operations must work.
    #[test]
    fn neon_instructions_work() {
        if let Some(_token) = NeonToken::summon() {
            unsafe {
                use std::arch::aarch64::*;
                let a = vdupq_n_f32(1.0);
                let b = vdupq_n_f32(2.0);
                let c = vaddq_f32(a, b);
                std::hint::black_box(c);
                let d = vdupq_n_s32(1);
                let e = vdupq_n_s32(2);
                let f = vaddq_s32(d, e);
                std::hint::black_box(f);
            }
        }
    }

    /// If Arm64V2Token is available, RDM and DotProd must work.
    #[test]
    fn arm64v2_instructions_work_when_token_available() {
        if let Some(_token) = Arm64V2Token::summon() {
            unsafe {
                use std::arch::aarch64::*;
                // RDM: saturating rounding doubling multiply accumulate
                let a = vdup_n_s16(100);
                let b = vdup_n_s16(200);
                let acc = vdup_n_s16(0);
                let rdm = vqrdmlah_s16(acc, a, b);
                std::hint::black_box(rdm);

                // DotProd: vdot_s32 is nightly-only (stdarch_neon_dotprod)
                // Verified: compiles on nightly but unstable on stable Rust
            }
        }
    }

    /// If NeonAesToken is available, AES round operations must work.
    #[test]
    fn neon_aes_instructions_work_when_token_available() {
        if let Some(_token) = NeonAesToken::summon() {
            unsafe {
                use std::arch::aarch64::*;
                let state = vdupq_n_u8(0x42);
                let key = vdupq_n_u8(0x2b);
                let enc = vaeseq_u8(state, key);
                std::hint::black_box(enc);
                // p64 multiply
                let result = vmull_p64(0x0123456789ABCDEF, 0xFEDCBA9876543210);
                std::hint::black_box(result);
            }
        }
    }

    /// If NeonSha3Token is available, SHA-3 instructions must work.
    #[test]
    fn neon_sha3_instructions_work_when_token_available() {
        if let Some(_token) = NeonSha3Token::summon() {
            unsafe {
                use std::arch::aarch64::*;
                let a = vdupq_n_u8(0x0F);
                let b = vdupq_n_u8(0x33);
                let c = vdupq_n_u8(0x55);
                let bcax = vbcaxq_u8(a, b, c);
                std::hint::black_box(bcax);
                let eor3 = veor3q_u8(a, b, c);
                std::hint::black_box(eor3);
            }
        }
    }

    /// If NeonCrcToken is available, CRC32 instructions must work.
    #[test]
    fn neon_crc_instructions_work_when_token_available() {
        if let Some(_token) = NeonCrcToken::summon() {
            unsafe {
                use std::arch::aarch64::*;
                let crc = __crc32b(0xFFFFFFFF, 0x42);
                std::hint::black_box(crc);
                let crc_c = __crc32cb(0xFFFFFFFF, 0x42);
                std::hint::black_box(crc_c);
            }
        }
    }

    /// Verify token hierarchy: V3 implies V2 implies Neon.
    #[test]
    fn token_hierarchy_is_correct() {
        if Arm64V3Token::summon().is_some() {
            assert!(Arm64V2Token::summon().is_some(), "Arm64V3 implies Arm64V2");
            assert!(NeonToken::summon().is_some(), "Arm64V3 implies Neon");
            assert!(
                NeonSha3Token::summon().is_some(),
                "Arm64V3 implies NeonSha3"
            );
        }

        if Arm64V2Token::summon().is_some() {
            assert!(NeonToken::summon().is_some(), "Arm64V2 implies Neon");
            assert!(NeonAesToken::summon().is_some(), "Arm64V2 implies NeonAes");
            assert!(NeonCrcToken::summon().is_some(), "Arm64V2 implies NeonCrc");
        }
    }

    #[test]
    fn print_detected_features() {
        println!("Feature detection results (aarch64):");
        println!("  NEON:        {}", NeonToken::summon().is_some());
        println!("  NeonAes:     {}", NeonAesToken::summon().is_some());
        println!("  NeonSha3:    {}", NeonSha3Token::summon().is_some());
        println!("  NeonCrc:     {}", NeonCrcToken::summon().is_some());
        println!("  Arm64-v2:    {}", Arm64V2Token::summon().is_some());
        println!("  Arm64-v3:    {}", Arm64V3Token::summon().is_some());
    }
}

// ============================================================================
// WASM Tests
// ============================================================================
#[cfg(target_arch = "wasm32")]
mod wasm32_tests {
    use super::*;

    /// Wasm128Token should be available when compiled with simd128 target feature.
    #[test]
    fn wasm128_available_with_simd128() {
        // When compiled with RUSTFLAGS="-C target-feature=+simd128",
        // Wasm128Token::summon() should return Some.
        #[cfg(target_feature = "simd128")]
        {
            assert!(
                Wasm128Token::summon().is_some(),
                "Wasm128Token should be available when compiled with +simd128"
            );
        }
        #[cfg(not(target_feature = "simd128"))]
        {
            println!("simd128 not enabled - Wasm128Token not available");
        }
    }

    /// If Wasm128Token is available, basic SIMD128 operations must work.
    #[cfg(target_feature = "simd128")]
    #[test]
    fn wasm128_instructions_work() {
        if let Some(_token) = Wasm128Token::summon() {
            use core::arch::wasm32::*;
            let a = f32x4_splat(1.0);
            let b = f32x4_splat(2.0);
            let c = f32x4_add(a, b);
            std::hint::black_box(c);

            let d = i32x4_splat(1);
            let e = i32x4_splat(2);
            let f = i32x4_add(d, e);
            std::hint::black_box(f);
        }
    }

    /// Wasm128RelaxedToken should be available when compiled with both
    /// simd128 and relaxed-simd target features.
    #[test]
    fn wasm128_relaxed_available_with_relaxed_simd() {
        #[cfg(all(target_feature = "simd128", target_feature = "relaxed-simd"))]
        {
            assert!(
                Wasm128RelaxedToken::summon().is_some(),
                "Wasm128RelaxedToken should be available with +simd128,+relaxed-simd"
            );
            // Relaxed implies base wasm128
            assert!(
                Wasm128Token::summon().is_some(),
                "Wasm128Token should be available when relaxed-simd is enabled"
            );
        }
        #[cfg(not(all(target_feature = "simd128", target_feature = "relaxed-simd")))]
        {
            println!("relaxed-simd not enabled - Wasm128RelaxedToken not available");
        }
    }

    #[test]
    fn print_detected_features() {
        println!("Feature detection results (wasm32):");
        println!("  Wasm128:          {}", Wasm128Token::summon().is_some());
        println!(
            "  Wasm128Relaxed:   {}",
            Wasm128RelaxedToken::summon().is_some()
        );
    }
}

// ============================================================================
// Cross-Platform Tests (always run)
// ============================================================================

/// ScalarToken should always be available on every platform.
#[test]
fn scalar_token_always_available() {
    assert!(
        ScalarToken::summon().is_some(),
        "ScalarToken should always be available"
    );
}

/// Verify that tokens on unsupported architectures return None.
#[test]
fn wrong_arch_tokens_return_none() {
    #[cfg(not(target_arch = "x86_64"))]
    {
        assert!(
            X64V1Token::summon().is_none(),
            "x86_64 tokens should be None on non-x86_64"
        );
        assert!(
            X64V2Token::summon().is_none(),
            "x86_64 tokens should be None on non-x86_64"
        );
        assert!(X64V3Token::summon().is_none());
    }
    #[cfg(not(target_arch = "aarch64"))]
    {
        assert!(
            NeonToken::summon().is_none(),
            "ARM tokens should be None on non-ARM"
        );
        assert!(Arm64V2Token::summon().is_none());
        assert!(Arm64V3Token::summon().is_none());
    }
    #[cfg(not(target_arch = "wasm32"))]
    {
        assert!(
            Wasm128Token::summon().is_none(),
            "WASM tokens should be None on non-WASM"
        );
        assert!(
            Wasm128RelaxedToken::summon().is_none(),
            "WASM relaxed tokens should be None on non-WASM"
        );
    }
}

/// X64 tokens are x86_64-only. On 32-bit x86, they must return None.
/// This test verifies the cfg gating — on x86_64 it checks tokens work,
/// on any other arch (including 32-bit x86) it checks they return None.
#[test]
fn x64_tokens_are_x86_64_only() {
    #[cfg(target_arch = "x86_64")]
    {
        // On x86_64, V1 is baseline — must always be Some
        assert!(
            X64V1Token::summon().is_some(),
            "X64V1Token should always be Some on x86_64"
        );
    }
    #[cfg(not(target_arch = "x86_64"))]
    {
        // On ALL other architectures (including 32-bit x86), must be None.
        // This catches the bug where arch="x86" in the registry would give
        // 32-bit x86 real implementations instead of stubs.
        assert!(
            X64V1Token::summon().is_none(),
            "X64V1Token must be None on non-x86_64 (including 32-bit x86)"
        );
        assert!(
            X64V2Token::summon().is_none(),
            "X64V2Token must be None on non-x86_64"
        );
        assert!(
            X64V3Token::summon().is_none(),
            "X64V3Token must be None on non-x86_64"
        );
        assert!(
            X64CryptoToken::summon().is_none(),
            "X64CryptoToken must be None on non-x86_64"
        );
        assert!(
            X64V3CryptoToken::summon().is_none(),
            "X64V3CryptoToken must be None on non-x86_64"
        );
    }
}

/// Verify compiled_with() returns correct values per architecture.
/// On x86_64, V1 features are baseline so compiled_with() = Some(true).
/// On other architectures, compiled_with() must indicate unavailability.
#[test]
fn compiled_with_reflects_architecture() {
    #[cfg(target_arch = "x86_64")]
    {
        // SSE2 is the x86_64 ABI baseline — always compiled in
        #[cfg(not(feature = "testable_dispatch"))]
        assert_eq!(
            X64V1Token::compiled_with(),
            Some(true),
            "X64V1Token::compiled_with() should be Some(true) on x86_64"
        );
    }
    #[cfg(not(target_arch = "x86_64"))]
    {
        // On non-x86_64 (including 32-bit x86), compiled_with should NOT
        // return Some(true) — these tokens don't belong to this architecture.
        assert_ne!(
            X64V1Token::compiled_with(),
            Some(true),
            "X64V1Token::compiled_with() must not be Some(true) on non-x86_64"
        );
    }
}

/// The #[arcane] macro cfg-gates to x86_64 only. Verify the macro registry
/// agrees with the token module routing — both must target x86_64, not x86.
#[test]
fn macro_registry_targets_x86_64_not_x86() {
    // This is a compile-time check: if the macro registry returned "x86"
    // instead of "x86_64", #[arcane] would emit cfg(target_arch = "x86")
    // which would generate real implementations on 32-bit x86.
    //
    // The test verifies the invariant indirectly: on x86_64 the token
    // exists and works, on everything else it's a stub returning None.
    // A cross-compilation test on i686 would fail if the routing were wrong.
    #[cfg(target_arch = "x86_64")]
    assert!(X64V3Token::summon().is_some() || X64V3Token::summon().is_none());
    #[cfg(not(target_arch = "x86_64"))]
    assert!(X64V3Token::summon().is_none());
}