Skip to main content

hyper_u256/
simd.rs

1use crate::U256;
2
3/// Paire de `U256` traités ensemble pour amortir l'overhead d'appel de
4/// fonction lors d'opérations par lots (batch processing).
5///
6/// ⚠️ Note d'implémentation : ce type ne contient **aucune** instruction
7/// SIMD explicite (pas d'intrinsics `core::arch`, pas de `core::simd`).
8/// Les deux `U256` sont traités par deux appels scalaires séquentiels.
9/// Le compilateur peut, selon la cible et les flags (`-C target-cpu=native`),
10/// choisir d'autovectoriser ce code — mais ce n'est ni garanti ni
11/// contractuel. Ce type existe pour donner une API groupée pratique en
12/// traitement par lots, pas pour promettre une exécution parallèle.
13/// Les intrinsics SIMD explicites nécessiteraient du code `unsafe`, exclu
14/// ici par `#![deny(unsafe_code)]` au niveau du crate.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(C, align(64))]
17pub struct U256x2 {
18    pub low: U256,
19    pub high: U256,
20}
21
22impl U256x2 {
23    /// Construit une paire à partir de deux `U256`.
24    #[inline(always)]
25    pub const fn new(low: U256, high: U256) -> Self {
26        Self { low, high }
27    }
28
29    /// Additionne deux paires `U256`, élément par élément
30    /// (`self.low + rhs.low`, `self.high + rhs.high`), en `wrapping`.
31    #[inline(always)]
32    pub fn add_pair(&self, rhs: &Self) -> Self {
33        Self {
34            low: self.low.wrapping_add(&rhs.low),
35            high: self.high.wrapping_add(&rhs.high),
36        }
37    }
38
39    /// Multiplie deux paires `U256`, élément par élément, en `wrapping`.
40    #[inline(always)]
41    pub fn mul_pair(&self, rhs: &Self) -> Self {
42        Self {
43            low: self.low.wrapping_mul(&rhs.low),
44            high: self.high.wrapping_mul(&rhs.high),
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_new_and_fields() {
55        let a = U256::from_limbs([1, 2, 3, 4]);
56        let b = U256::from_limbs([5, 6, 7, 8]);
57        let pair = U256x2::new(a, b);
58        assert_eq!(pair.low, a);
59        assert_eq!(pair.high, b);
60    }
61
62    #[test]
63    fn test_add_pair_is_elementwise() {
64        let pair_a = U256x2::new(U256::from_limbs([1, 0, 0, 0]), U256::from_limbs([10, 0, 0, 0]));
65        let pair_b = U256x2::new(U256::from_limbs([2, 0, 0, 0]), U256::from_limbs([20, 0, 0, 0]));
66        let res = pair_a.add_pair(&pair_b);
67        assert_eq!(res.low, U256::from_limbs([3, 0, 0, 0]));
68        assert_eq!(res.high, U256::from_limbs([30, 0, 0, 0]));
69    }
70
71    #[test]
72    fn test_add_pair_carry_is_independent_per_lane() {
73        // Le carry de `low` ne doit jamais contaminer `high`.
74        let pair_a = U256x2::new(U256::from_limbs([u64::MAX, 0, 0, 0]), U256::from_limbs([1, 0, 0, 0]));
75        let pair_b = U256x2::new(U256::from_limbs([1, 0, 0, 0]), U256::from_limbs([1, 0, 0, 0]));
76        let res = pair_a.add_pair(&pair_b);
77        assert_eq!(res.low, U256::from_limbs([0, 1, 0, 0]));
78        assert_eq!(res.high, U256::from_limbs([2, 0, 0, 0]));
79    }
80
81    #[test]
82    fn test_mul_pair_is_elementwise() {
83        let pair_a = U256x2::new(U256::from_limbs([10, 0, 0, 0]), U256::from_limbs([3, 0, 0, 0]));
84        let pair_b = U256x2::new(U256::from_limbs([5, 0, 0, 0]), U256::from_limbs([4, 0, 0, 0]));
85        let res = pair_a.mul_pair(&pair_b);
86        assert_eq!(res.low, U256::from_limbs([50, 0, 0, 0]));
87        assert_eq!(res.high, U256::from_limbs([12, 0, 0, 0]));
88    }
89
90    #[test]
91    fn test_equality_derive() {
92        let a = U256x2::new(U256::ZERO, U256::ONE);
93        let b = U256x2::new(U256::ZERO, U256::ONE);
94        let c = U256x2::new(U256::ONE, U256::ZERO);
95        assert_eq!(a, b);
96        assert_ne!(a, c);
97    }
98}