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
/*!
Simd standard types
*/

use crate::Vector2;

pub use packed_simd::{f32x2, f64x2, i16x2, i32x2, i64x2, i8x2, u16x2, u32x2, u64x2, u8x2};

macro_rules! int_mod {
    ($T:ident, $V:ident) => {
        /// Standard geometric types for a scalar type
        pub mod $T {
            /// A standard 2D vector type
            pub type Vec2 = super::$V;
            /// A standard rectangle type
            pub type Rect = [super::$V; 2];
        }
    };
}

int_mod!(u8, u8x2);
int_mod!(u16, u16x2);
int_mod!(u32, u32x2);
int_mod!(u64, u64x2);
int_mod!(i8, i8x2);
int_mod!(i16, i16x2);
int_mod!(i32, i32x2);
int_mod!(i64, i64x2);

macro_rules! float_mod {
    ($T:ident, $V:ident) => {
        /// Standard geometric types for a scalar type
        pub mod $T {
            /// A standard 2D vector type
            pub type Vec2 = super::$V;
            /// A standard rectangle type
            pub type Rect = [super::$V; 2];
            /// A standard circle type
            pub type Circ = (super::$V, $T);
            /// A standard transform type
            pub type Trans = [$T; 6];
        }
    };
}

float_mod!(f32, f32x2);
float_mod!(f64, f64x2);

macro_rules! impl_simd_vector2 {
    ($Vector:ty, $Scalar:ty) => {
        impl Vector2 for $Vector {
            type Scalar = $Scalar;
            fn new(x: Self::Scalar, y: Self::Scalar) -> Self {
                <$Vector>::new(x, y)
            }
            fn x(&self) -> Self::Scalar {
                unsafe { self.extract_unchecked(0) }
            }
            fn y(&self) -> Self::Scalar {
                unsafe { self.extract_unchecked(1) }
            }
            fn with_x(self, x: $Scalar) -> Self {
                unsafe { self.replace_unchecked(0, x) }
            }
            fn with_y(self, y: $Scalar) -> Self {
                unsafe { self.replace_unchecked(1, y) }
            }
            fn square(s: Self::Scalar) -> Self {
                Self::splat(s)
            }
            fn add(self, other: Self) -> Self {
                self + other
            }
            fn sub(self, other: Self) -> Self {
                self - other
            }
            fn mul2(self, other: Self) -> Self {
                self * other
            }
            fn div2(self, other: Self) -> Self {
                self * other
            }
            fn add_assign(&mut self, other: Self) {
                *self += other;
            }
            fn sub_assign(&mut self, other: Self) {
                *self -= other;
            }
            fn mul2_assign(&mut self, other: Self) {
                *self *= other;
            }
            fn div2_assign(&mut self, other: Self) {
                *self /= other;
            }
        }
    };
}

impl_simd_vector2!(u8x2, u8);
impl_simd_vector2!(u16x2, u16);
impl_simd_vector2!(u32x2, u32);
impl_simd_vector2!(u64x2, u64);

impl_simd_vector2!(i8x2, i8);
impl_simd_vector2!(i16x2, i16);
impl_simd_vector2!(i32x2, i32);
impl_simd_vector2!(i64x2, i64);

impl_simd_vector2!(f32x2, f32);
impl_simd_vector2!(f64x2, f64);

#[cfg(test)]
#[test]
fn simd() {
    use crate::{FloatingVector2, Transform};
    let a = f32x2::new(1.0, 2.0);
    let b = f32x2::new(3.0, 5.0);
    let c = a.add(b);
    assert_eq!(f32x2::new(4.0, 7.0), c);
    let c = a.transform(f32::Trans::identity().translate(b));
    assert_eq!(f32x2::new(4.0, 7.0), c);
}