rscrypto 0.8.0

Pure Rust Cryptography: RSA, Ed25519, X25519, SHA-2/3, BLAKE2/3, AES-GCM/GCM-SIV, X/ChaCha20-Poly1305, Argon2, HMAC/HKDF, CRC. no_std, WASM, hardware acceleration.
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// x86_64 Detection

#[cfg(target_arch = "x86_64")]
fn detect_x86_64() -> Detected {
  // Start with compile-time detected features (includes SSE2 baseline)
  let caps_static = caps_static();

  // Runtime detection extracts features + vendor/family/model in batch
  #[cfg(feature = "std")]
  let (runtime_caps, is_amd, family, model, amx_permission) = {
    let batch = cpuid_batch_x86_64();
    (
      batch.caps,
      batch.is_amd,
      batch.family,
      batch.model,
      batch.amx_permission,
    )
  };

  #[cfg(feature = "std")]
  let mut caps = caps_static.union(runtime_caps);
  #[cfg(not(feature = "std"))]
  let mut caps = caps_static;

  #[cfg(feature = "std")]
  {
    caps = gate_x86_amx_permission(caps, amx_permission);
  }

  #[cfg(all(
    not(feature = "std"),
    any(target_os = "linux", target_os = "android")
  ))]
  {
    caps = gate_x86_amx_permission(caps, false);
  }

  // Hybrid Intel AVX-512 Safety: Clear AVX-512 caps on hybrid CPUs
  // On hybrid Intel CPUs (Alder Lake, Raptor Lake, etc.), the P-cores have
  // AVX-512 but E-cores don't. If a thread migrates to an E-core while
  // executing AVX-512 code, it will SIGILL. The only safe approach is to
  // disable AVX-512 entirely unless the user explicitly overrides.
  #[cfg(feature = "std")]
  {
    use crate::platform::caps::x86;

    if is_intel_hybrid(is_amd, family, model) && !hybrid_avx512_override() {
      // Clear all AVX-512 related capabilities to prevent kernel selection
      // from choosing AVX-512/VPCLMUL paths that could SIGILL on E-cores.
      caps = caps
        .difference(x86::AVX512F)
        .difference(x86::AVX512DQ)
        .difference(x86::AVX512IFMA)
        .difference(x86::AVX512CD)
        .difference(x86::AVX512BW)
        .difference(x86::AVX512VL)
        .difference(x86::AVX512VBMI)
        .difference(x86::AVX512VBMI2)
        .difference(x86::AVX512VNNI)
        .difference(x86::AVX512BITALG)
        .difference(x86::AVX512VPOPCNTDQ)
        .difference(x86::AVX512BF16)
        .difference(x86::AVX512FP16)
        .difference(x86::AVX512VP2INTERSECT)
        .difference(x86::AVX10_1)
        .difference(x86::AVX10_2);
    }
  }

  Detected {
    caps,
    arch: Arch::X86_64,
  }
}

#[cfg(target_arch = "x86_64")]
const X86_ALL_AMX: Caps = crate::platform::caps::x86::AMX_TILE
  .union(crate::platform::caps::x86::AMX_BF16)
  .union(crate::platform::caps::x86::AMX_INT8)
  .union(crate::platform::caps::x86::AMX_FP16)
  .union(crate::platform::caps::x86::AMX_COMPLEX);

#[cfg(target_arch = "x86_64")]
#[inline]
const fn gate_x86_amx_permission(caps: Caps, permitted: bool) -> Caps {
  if permitted {
    caps
  } else {
    caps.difference(X86_ALL_AMX)
  }
}

#[cfg(target_arch = "x86")]
fn detect_x86() -> Detected {
  // Start with compile-time detected features
  let mut caps = caps_static();

  #[cfg(feature = "std")]
  {
    use crate::platform::caps::x86;

    // SSE2 is not guaranteed on 32-bit x86, detect at runtime
    if std::arch::is_x86_feature_detected!("sse2") {
      caps |= x86::SSE2;
    }
    caps |= runtime_x86_32();
  }

  Detected {
    caps,
    arch: Arch::X86,
  }
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
#[inline]
// MSRV: CPUID is unsafe on Rust 1.91 but safe on the pinned nightly.
#[allow(unsafe_code, unused_unsafe)]
fn cpuid_leaf(leaf: u32) -> core::arch::x86_64::CpuidResult {
  // SAFETY: CPUID leaf read is safe here because:
  // 1. This function is compiled only for x86_64 targets.
  // 2. CPUID is a non-privileged CPU-identification instruction.
  // 3. The intrinsic returns register values and does not access Rust memory.
  unsafe { core::arch::x86_64::__cpuid(leaf) }
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
#[inline]
// MSRV: CPUID is unsafe on Rust 1.91 but safe on the pinned nightly.
#[allow(unsafe_code, unused_unsafe)]
fn cpuid_leaf_count(leaf: u32, subleaf: u32) -> core::arch::x86_64::CpuidResult {
  // SAFETY: CPUID leaf/subleaf read is safe here because:
  // 1. This function is compiled only for x86_64 targets.
  // 2. CPUID is a non-privileged CPU-identification instruction.
  // 3. The intrinsic returns register values and does not access Rust memory.
  unsafe { core::arch::x86_64::__cpuid_count(leaf, subleaf) }
}

#[cfg(all(target_arch = "x86", feature = "std"))]
#[inline]
// MSRV: CPUID is unsafe on Rust 1.91 but safe on the pinned nightly.
#[allow(unsafe_code, unused_unsafe)]
fn cpuid_leaf(leaf: u32) -> core::arch::x86::CpuidResult {
  // SAFETY: CPUID leaf read is safe here because:
  // 1. This function is compiled only for x86 targets.
  // 2. CPUID is a non-privileged CPU-identification instruction.
  // 3. The intrinsic returns register values and does not access Rust memory.
  unsafe { core::arch::x86::__cpuid(leaf) }
}

/// Batch CPUID result containing all extracted information.
///
/// This struct consolidates all CPUID-derived data to avoid redundant calls.
/// A single call to `cpuid_batch_x86_64()` extracts:
/// - Feature capabilities (Caps)
/// - Vendor identification (Intel/AMD/Unknown)
/// - CPU family and model for microarchitecture selection
#[cfg(all(target_arch = "x86_64", feature = "std"))]
struct CpuidBatch {
  caps: Caps,
  is_amd: bool,
  family: u32,
  model: u32,
  amx_permission: bool,
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
#[derive(Clone, Copy, Default)]
struct CpuidRegisters {
  eax: u32,
  ebx: u32,
  ecx: u32,
  edx: u32,
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
impl From<core::arch::x86_64::CpuidResult> for CpuidRegisters {
  fn from(result: core::arch::x86_64::CpuidResult) -> Self {
    Self {
      eax: result.eax,
      ebx: result.ebx,
      ecx: result.ecx,
      edx: result.edx,
    }
  }
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
#[derive(Clone, Copy, Default)]
struct CpuidSnapshot {
  leaf0: CpuidRegisters,
  leaf1: CpuidRegisters,
  leaf7_0: CpuidRegisters,
  leaf7_1: CpuidRegisters,
  leaf24_0: CpuidRegisters,
  extended_leaf0: CpuidRegisters,
  extended_leaf1: CpuidRegisters,
  xcr0: u64,
  amx_permission: bool,
}

/// Batch CPUID extraction - extracts all features and CPU info in minimal CPUID calls.
///
/// Makes 5-7 CPUID calls (depending on CPU capabilities):
/// - Leaf 0: vendor string
/// - Leaf 1: processor info + basic features
/// - Leaf 7.0: extended features
/// - Leaf 7.1: more extended features
/// - Leaf 0x24: AVX10 version (if leaf 7 reports AVX10 and max leaf permits it)
/// - Leaf 0x80000001: AMD-specific features
///
/// **Critical**: This function properly gates AVX/AVX-512 features by checking
/// OSXSAVE and XGETBV(XCR0) to ensure the OS will save/restore extended registers.
/// Without this check, using AVX/AVX-512 instructions could cause SIGILL.
///
/// # Safety
/// Uses XGETBV, which requires `unsafe` and is only called when OSXSAVE is set.
#[cfg(all(target_arch = "x86_64", feature = "std"))]
#[allow(unsafe_code)]
fn cpuid_batch_x86_64() -> CpuidBatch {
  use core::arch::x86_64::_xgetbv;

  let leaf0 = CpuidRegisters::from(cpuid_leaf(0));
  let leaf1 = if leaf0.eax >= 1 {
    CpuidRegisters::from(cpuid_leaf(1))
  } else {
    CpuidRegisters::default()
  };
  let leaf7_0 = if leaf0.eax >= 7 {
    CpuidRegisters::from(cpuid_leaf_count(7, 0))
  } else {
    CpuidRegisters::default()
  };
  let leaf7_1 = if leaf0.eax >= 7 && leaf7_0.eax >= 1 {
    CpuidRegisters::from(cpuid_leaf_count(7, 1))
  } else {
    CpuidRegisters::default()
  };
  let leaf24_0 = if leaf0.eax >= 0x24 && leaf7_1.edx & (1 << 19) != 0 {
    CpuidRegisters::from(cpuid_leaf_count(0x24, 0))
  } else {
    CpuidRegisters::default()
  };
  let extended_leaf0 = CpuidRegisters::from(cpuid_leaf(0x8000_0000));
  let extended_leaf1 = if extended_leaf0.eax >= 0x8000_0001 {
    CpuidRegisters::from(cpuid_leaf(0x8000_0001))
  } else {
    CpuidRegisters::default()
  };
  let xcr0 = if leaf1.ecx & (1 << 27) != 0 {
    // SAFETY: leaf 1 reported OSXSAVE, so XGETBV is enabled; index zero reads
    // the architectural extended-state mask without accessing Rust memory.
    unsafe { _xgetbv(0) }
  } else {
    0
  };

  decode_cpuid_x86_64(CpuidSnapshot {
    leaf0,
    leaf1,
    leaf7_0,
    leaf7_1,
    leaf24_0,
    extended_leaf0,
    extended_leaf1,
    xcr0,
    amx_permission: amx_xstate_permission_x86_64(),
  })
}

#[cfg(all(
  target_arch = "x86_64",
  feature = "std",
  any(target_os = "linux", target_os = "android")
))]
#[allow(unsafe_code)]
fn amx_xstate_permission_x86_64() -> bool {
  const SYS_ARCH_PRCTL: isize = 158;
  const ARCH_GET_XCOMP_PERM: usize = 0x1022;
  const XCOMP_TILE_MASK: u64 = (1 << 17) | (1 << 18);

  let mut permissions = 0u64;
  let mut result = SYS_ARCH_PRCTL;

  // SAFETY: Linux x86_64 syscall ABI invocation because:
  // 1. syscall 158 is arch_prctl on the x86_64 Linux ABI.
  // 2. ARCH_GET_XCOMP_PERM writes one u64 through the valid exclusive pointer
  //    in RSI and does not retain it.
  // 3. syscall clobbers RAX, RCX, and R11; all are declared, and no Rust
  //    reference is live across an undeclared register or stack mutation.
  unsafe {
    core::arch::asm!(
      "syscall",
      inlateout("rax") result,
      in("rdi") ARCH_GET_XCOMP_PERM,
      in("rsi") &mut permissions,
      lateout("rcx") _,
      lateout("r11") _,
      options(nostack),
    );
  }

  result == 0 && permissions & XCOMP_TILE_MASK == XCOMP_TILE_MASK
}

#[cfg(all(
  target_arch = "x86_64",
  feature = "std",
  not(any(target_os = "linux", target_os = "android"))
))]
const fn amx_xstate_permission_x86_64() -> bool {
  true
}

#[cfg(all(target_arch = "x86_64", feature = "std"))]
fn decode_cpuid_x86_64(snapshot: CpuidSnapshot) -> CpuidBatch {
  use crate::platform::caps::x86;

  const XCR0_AVX_MASK: u64 = 0x6;
  const XCR0_AVX512_MASK: u64 = 0xE0;
  const XCR0_AMX_MASK: u64 = (1 << 17) | (1 << 18);
  const XCR0_APX_MASK: u64 = 1 << 19;

  let mut caps = Caps::NONE;
  let cpuid0 = snapshot.leaf0;
  let cpuid1 = if cpuid0.eax >= 1 {
    snapshot.leaf1
  } else {
    CpuidRegisters::default()
  };
  let cpuid7 = if cpuid0.eax >= 7 {
    snapshot.leaf7_0
  } else {
    CpuidRegisters::default()
  };
  let cpuid7_1 = if cpuid0.eax >= 7 && cpuid7.eax >= 1 {
    snapshot.leaf7_1
  } else {
    CpuidRegisters::default()
  };
  let cpuid24 = if cpuid0.eax >= 0x24 {
    snapshot.leaf24_0
  } else {
    CpuidRegisters::default()
  };
  let cpuid_ext = if snapshot.extended_leaf0.eax >= 0x8000_0001 {
    snapshot.extended_leaf1
  } else {
    CpuidRegisters::default()
  };

  // "GenuineIntel" has EBX/EDX/ECX = "Genu" / "ineI" / "ntel".
  let is_intel = cpuid0.ebx == 0x756e_6547 && cpuid0.edx == 0x4965_6e69 && cpuid0.ecx == 0x6c65_746e;
  // "AuthenticAMD" has ebx = 0x68747541 ("Auth")
  let is_amd = cpuid0.ebx == 0x6874_7541;

  // Extract extended family (bits 27:20) + base family (bits 11:8)
  let base_family = (cpuid1.eax >> 8) & 0xF;
  let ext_family = (cpuid1.eax >> 20) & 0xFF;
  let family = base_family + ext_family;

  // Extract model (bits 7:4 + extended model bits 19:16 for family 6/15)
  let base_model = (cpuid1.eax >> 4) & 0xF;
  let ext_model = (cpuid1.eax >> 16) & 0xF;
  let model = if base_family == 6 || base_family == 15 {
    base_model + (ext_model << 4)
  } else {
    base_model
  };

  // OS Support Detection via OSXSAVE + XGETBV
  // CRITICAL: CPUID reports what the CPU supports, not what the OS allows.
  // We must check OSXSAVE (indicates OS uses XSAVE) and read XCR0 to verify
  // the OS will actually save/restore AVX/AVX-512 registers. Without this,
  // using AVX instructions on an OS that doesn't save YMM/ZMM state causes SIGILL.

  // OSXSAVE (bit 27): OS has set CR4.OSXSAVE and supports XSAVE/XGETBV
  let osxsave = cpuid1.ecx & (1 << 27) != 0;

  // Determine OS support for AVX and AVX-512 register state
  let os_avx = osxsave && (snapshot.xcr0 & XCR0_AVX_MASK) == XCR0_AVX_MASK;
  let os_avx512 = os_avx && (snapshot.xcr0 & XCR0_AVX512_MASK) == XCR0_AVX512_MASK;
  let os_amx =
    osxsave && (snapshot.xcr0 & XCR0_AMX_MASK) == XCR0_AMX_MASK && snapshot.amx_permission;
  let os_apx = osxsave && (snapshot.xcr0 & XCR0_APX_MASK) == XCR0_APX_MASK;

  let has_avx = cpuid1.ecx & (1 << 28) != 0;
  let has_avx2 = cpuid7.ebx & (1 << 5) != 0;
  let has_avx512f = cpuid7.ebx & (1 << 16) != 0;
  let has_avx512bw = cpuid7.ebx & (1 << 30) != 0;
  let has_fma = cpuid1.ecx & (1 << 12) != 0;
  let has_f16c = cpuid1.ecx & (1 << 29) != 0;
  let has_aes = cpuid1.ecx & (1 << 25) != 0;
  let has_pclmul = cpuid1.ecx & (1 << 1) != 0;
  let rust_avx = os_avx && has_avx;
  let rust_avx512 = os_avx512 && rust_avx && has_avx512f && has_fma && has_f16c;

  // ECX features (leaf 1) - SSE/basic features (no OS gating needed)
  if cpuid1.ecx & (1 << 0) != 0 {
    caps |= x86::SSE3;
  }
  if cpuid1.ecx & (1 << 9) != 0 {
    caps |= x86::SSSE3;
  }
  if cpuid1.ecx & (1 << 19) != 0 {
    caps |= x86::SSE41;
  }
  if cpuid1.ecx & (1 << 20) != 0 {
    caps |= x86::SSE42;
  }
  if cpuid1.ecx & (1 << 23) != 0 {
    caps |= x86::POPCNT;
  }
  if has_aes {
    caps |= x86::AESNI;
  }
  if has_pclmul {
    caps |= x86::PCLMULQDQ;
  }
  if cpuid1.ecx & (1 << 30) != 0 {
    caps |= x86::RDRAND;
  }

  // AVX-class features (require OS AVX support via XCR0)
  if rust_avx {
    caps |= x86::AVX;
    if has_fma {
      caps |= x86::FMA;
    }
    if has_f16c {
      caps |= x86::F16C;
    }
  }

  // EBX features (leaf 7) - non-AVX features (no OS gating needed)
  if cpuid7.ebx & (1 << 3) != 0 {
    caps |= x86::BMI1;
  }
  if cpuid7.ebx & (1 << 8) != 0 {
    caps |= x86::BMI2;
  }
  if cpuid7.ebx & (1 << 19) != 0 {
    caps |= x86::ADX;
  }
  if cpuid7.ebx & (1 << 29) != 0 {
    caps |= x86::SHA;
  }

  // AVX2 (requires OS AVX support for YMM registers)
  if rust_avx && has_avx2 {
    caps |= x86::AVX2;
  }

  // Rust AVX-512 target features also imply FMA and F16C.
  if rust_avx512 {
    caps |= x86::AVX512F;
    if cpuid7.ebx & (1 << 17) != 0 {
      caps |= x86::AVX512DQ;
    }
    if cpuid7.ebx & (1 << 21) != 0 {
      caps |= x86::AVX512IFMA;
    }
    if cpuid7.ebx & (1 << 28) != 0 {
      caps |= x86::AVX512CD;
    }
    if cpuid7.ebx & (1 << 30) != 0 {
      caps |= x86::AVX512BW;
    }
    if cpuid7.ebx & (1 << 31) != 0 {
      caps |= x86::AVX512VL;
    }

    // ECX AVX-512 features (leaf 7)
    if cpuid7.ecx & (1 << 1) != 0 {
      caps |= x86::AVX512VBMI;
    }
    if cpuid7.ecx & (1 << 6) != 0 {
      caps |= x86::AVX512VBMI2;
    }
    if cpuid7.ecx & (1 << 11) != 0 {
      caps |= x86::AVX512VNNI;
    }
    if cpuid7.ecx & (1 << 12) != 0 {
      caps |= x86::AVX512BITALG;
    }
    if cpuid7.ecx & (1 << 14) != 0 {
      caps |= x86::AVX512VPOPCNTDQ;
    }
    if cpuid7.edx & (1 << 8) != 0 {
      caps |= x86::AVX512VP2INTERSECT;
    }
    if has_avx512bw && cpuid7.edx & (1 << 23) != 0 {
      caps |= x86::AVX512FP16;
    }
    if has_avx512bw && cpuid7_1.eax & (1 << 5) != 0 {
      caps |= x86::AVX512BF16;
    }

    if cpuid7_1.edx & (1 << 19) != 0 {
      let avx10_version = cpuid24.ebx & 0xFF;
      if avx10_version >= 1 {
        caps |= x86::AVX10_1;
      }
      // Rust's AVX10.2 feature also implies AVX-VNNI, AVX-VNNI-INT8, and
      // AVX-VNNI-INT16. Caps does not model that prerequisite set, so runtime
      // detection deliberately under-reports AVX10_2.
    }
  }

  if cpuid7.ecx & (1 << 8) != 0 {
    caps |= x86::GFNI;
  }
  if rust_avx && has_avx2 && has_aes && cpuid7.ecx & (1 << 9) != 0 {
    caps |= x86::VAES;
  }
  if rust_avx && has_pclmul && cpuid7.ecx & (1 << 10) != 0 {
    caps |= x86::VPCLMULQDQ;
  }

  // EBX/ECX/EDX features (leaf 7)
  if cpuid7.ebx & (1 << 18) != 0 {
    caps |= x86::RDSEED;
  }
  if cpuid7.ecx & (1 << 27) != 0 {
    caps |= x86::MOVDIRI;
  }
  if cpuid7.ecx & (1 << 28) != 0 {
    caps |= x86::MOVDIR64B;
  }
  if cpuid7.edx & (1 << 14) != 0 {
    caps |= x86::SERIALIZE;
  }
  if os_amx && cpuid7.edx & (1 << 24) != 0 {
    caps |= x86::AMX_TILE;
  }
  if os_amx && cpuid7.edx & (1 << 22) != 0 {
    caps |= x86::AMX_BF16;
  }
  if os_amx && cpuid7.edx & (1 << 25) != 0 {
    caps |= x86::AMX_INT8;
  }

  // EAX features (leaf 7, subleaf 1)
  // SHA512 doesn't require AVX-512 (uses XMM registers).
  if cpuid7_1.eax & (1 << 0) != 0 {
    caps |= x86::SHA512;
  }

  // AMX extensions require both architectural tile-state components.
  if os_amx && cpuid7_1.eax & (1 << 21) != 0 {
    caps |= x86::AMX_FP16;
  }
  if os_amx && cpuid7_1.edx & (1 << 8) != 0 {
    caps |= x86::AMX_COMPLEX;
  }

  if os_apx && cpuid7_1.edx & (1 << 21) != 0 {
    caps |= x86::APX;
  }

  // Extended CPUID (leaf 0x80000001) features
  if cpuid_ext.ecx & (1 << 5) != 0 {
    caps |= x86::LZCNT;
  }
  if cpuid_ext.ecx & (1 << 6) != 0 {
    caps |= x86::SSE4A;
  }

  // ─── Vendor Identification ───
  // Store vendor as a capability bit for vendor-aware dispatch (e.g., AMD Zen 5
  // prefers AVX2 over AVX-512VL for SHA-512 compression).
  if is_amd {
    caps |= x86::AMD;
    // Zen 5+ (family ≥ 0x1A) has a 6-wide dispatch pipeline where standard
    // SHA-512 round structure outscales the deferred-Σ0 variant.
    if family >= 0x1A {
      caps |= x86::AMD_ZEN5;
    }
  }
  if is_intel_sapphire_rapids(is_intel, family, model) {
    caps |= x86::INTEL_SAPPHIRE_RAPIDS;
  }

  CpuidBatch {
    caps,
    is_amd,
    family,
    model,
    amx_permission: snapshot.amx_permission,
  }
}

/// Runtime x86 (32-bit) feature detection using CPUID.
///
/// # Safety
/// Uses CPUID instruction which requires unsafe, but is always safe to call on x86.
#[cfg(all(target_arch = "x86", feature = "std"))]
#[allow(unsafe_code)]
fn runtime_x86_32() -> Caps {
  use crate::platform::caps::x86;

  let mut caps = Caps::NONE;

  // CPUID leaf 1: processor info and feature bits
  let cpuid1 = cpuid_leaf(1);

  // ECX features (leaf 1)
  if cpuid1.ecx & (1 << 0) != 0 {
    caps |= x86::SSE3;
  }
  if cpuid1.ecx & (1 << 9) != 0 {
    caps |= x86::SSSE3;
  }
  if cpuid1.ecx & (1 << 19) != 0 {
    caps |= x86::SSE41;
  }
  if cpuid1.ecx & (1 << 20) != 0 {
    caps |= x86::SSE42;
  }
  if cpuid1.ecx & (1 << 1) != 0 {
    caps |= x86::PCLMULQDQ;
  }
  if cpuid1.ecx & (1 << 25) != 0 {
    caps |= x86::AESNI;
  }

  caps
}

/// Check if user has explicitly enabled AVX-512 on hybrid Intel CPUs.
///
/// On Alder Lake and newer hybrid Intel CPUs, AVX-512 is disabled by default
/// because E-cores don't support it. Power users who have disabled E-cores
/// in BIOS or are using early unfused chips can set this environment variable
/// to force AVX-512 usage.
///
/// # Environment Variable
///
/// `RSCRYPTO_FORCE_AVX512=1` enables AVX-512 on hybrid Intel CPUs.
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "std"))]
fn hybrid_avx512_override() -> bool {
  let value = std::env::var("RSCRYPTO_FORCE_AVX512").ok();
  parse_hybrid_avx512_override(value.as_deref())
}

#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "std"))]
fn parse_hybrid_avx512_override(value: Option<&str>) -> bool {
  matches!(value, Some("1")) || value.is_some_and(|value| value.eq_ignore_ascii_case("true"))
}

#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "std"))]
fn is_intel_sapphire_rapids(is_intel: bool, family: u32, model: u32) -> bool {
  is_intel && family == 6 && model == 0x8F
}

/// Detect Intel hybrid CPU (Alder Lake family and newer).
///
/// Returns true if this is an Intel hybrid CPU (P+E cores) where AVX-512
/// is problematic. These CPUs have family 6, model 0x97 (ADL-S), 0x9A (ADL-P),
/// 0xB7 (RPL-S), 0xBA (RPL-P), etc.
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), feature = "std"))]
fn is_intel_hybrid(is_amd: bool, family: u32, model: u32) -> bool {
  if is_amd {
    return false;
  }

  // Intel uses extended family + base family for family >= 15
  // Family 6 is used for all modern Intel client/server CPUs
  if family != 6 {
    return false;
  }

  // Hybrid CPU models (Alder Lake, Raptor Lake, Meteor Lake, etc.)
  // These have E-cores that don't support AVX-512
  matches!(
    model,
    0x97  // Alder Lake-S (desktop)
    | 0x9A  // Alder Lake-P/H/U (mobile)
    | 0x9C  // Alder Lake-N (low power)
    | 0xB7  // Raptor Lake-S (desktop)
    | 0xBA  // Raptor Lake-P/H (mobile)
    | 0xBF  // Raptor Lake-S refresh
    | 0xAA  // Meteor Lake-H
    | 0xAC  // Meteor Lake-U
    | 0xBD  // Lunar Lake
    | 0xC5  // Arrow Lake-S
    | 0xC6 // Arrow Lake-H
  )
}