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
#![no_std]
#![forbid(unsafe_code)]

//! A crate to trick the optimizer into generating SIMD instructions.

use core::{fmt, mem, ops};

pub type Simd<TArray> = <TArray as internals::ToSimd>::Vector;

pub type I8x8 = Simd<[i8; 8]>;
pub type I8x16 = Simd<[i8; 16]>;
pub type I8x32 = Simd<[i8; 32]>;
pub type I8x64 = Simd<[i8; 64]>;

pub type I16x4 = Simd<[i16; 4]>;
pub type I16x8 = Simd<[i16; 8]>;
pub type I16x16 = Simd<[i16; 16]>;
pub type I16x32 = Simd<[i16; 32]>;

pub type I32x2 = Simd<[i32; 2]>;
pub type I32x4 = Simd<[i32; 4]>;
pub type I32x8 = Simd<[i32; 8]>;
pub type I32x16 = Simd<[i32; 16]>;

pub type I64x1 = Simd<[i64; 1]>;
pub type I64x2 = Simd<[i64; 2]>;
pub type I64x4 = Simd<[i64; 4]>;
pub type I64x8 = Simd<[i64; 8]>;

pub type U8x8 = Simd<[u8; 8]>;
pub type U8x16 = Simd<[u8; 16]>;
pub type U8x32 = Simd<[u8; 32]>;
pub type U8x64 = Simd<[u8; 64]>;

pub type U16x4 = Simd<[u16; 4]>;
pub type U16x8 = Simd<[u16; 8]>;
pub type U16x16 = Simd<[u16; 16]>;
pub type U16x32 = Simd<[u16; 32]>;

pub type U32x2 = Simd<[u32; 2]>;
pub type U32x4 = Simd<[u32; 4]>;
pub type U32x8 = Simd<[u32; 8]>;
pub type U32x16 = Simd<[u32; 16]>;

pub type U64x1 = Simd<[u64; 1]>;
pub type U64x2 = Simd<[u64; 2]>;
pub type U64x3 = Simd<[u64; 4]>;
pub type U64x4 = Simd<[u64; 8]>;

pub type F32x2 = Simd<[f32; 2]>;
pub type F32x4 = Simd<[f32; 4]>;
pub type F32x8 = Simd<[f32; 8]>;
pub type F32x16 = Simd<[f32; 16]>;

pub type F64x1 = Simd<[f64; 1]>;
pub type F64x2 = Simd<[f64; 2]>;
pub type F64x4 = Simd<[f64; 4]>;
pub type F64x8 = Simd<[f64; 8]>;

pub trait Vector {
    type Element;
    const LANES: usize;
}

mod internals {
    pub trait ToSimd {
        type Vector: super::Vector;
    }
}

macro_rules! impl_simd_type {
    ($name:ident $size_align:literal) => {
        impl_simd_type!(int $name $size_align =>
            i8 i16 i32 i64
            u8 u16 u32 u64
        );
        impl_simd_type!(float $name $size_align =>
            f32 f64
        );
    };
    ($k:ident $name:ident $size_align:literal => $($t:ident)+) => {$(
        impl_simd_type!($k $name $size_align => $t ($size_align / mem::size_of::<$t>()));
    )+};
    (int $name:ident $size_align:literal => $t:ident $n:expr) => {
        impl $name<[$t; $n]> {
            #[inline]
            fn map(self, f: impl Fn($t) -> $t) -> Self {
                Self(array_utils::map(self.0, f))
            }

            #[inline]
            pub fn wrapping_add(self, other: Self) -> Self {
                self.zip(other, <$t>::wrapping_add)
            }

            #[inline]
            pub fn saturating_add(self, other: Self) -> Self {
                self.zip(other, <$t>::saturating_add)
            }

            #[inline]
            pub fn wrapping_sub(self, other: Self) -> Self {
                self.zip(other, <$t>::wrapping_sub)
            }

            #[inline]
            pub fn saturating_sub(self, other: Self) -> Self {
                self.zip(other, <$t>::saturating_sub)
            }

            #[inline]
            pub fn count_ones(self) -> Self {
                self.map(|a| <$t>::count_ones(a) as $t)
            }

            #[inline]
            pub fn count_zeros(self) -> Self {
                self.map(|a| <$t>::count_zeros(a) as $t)
            }
        }
        impl_simd_type!(common $name $size_align => $t $n);
    };
    (float $name:ident $size_align:literal => $t:ident $n:expr) => {
        impl $name<[$t; $n]> {
            #[inline]
            pub fn add(self, other: Self) -> Self {
                self.zip(other, ops::Add::add)
            }

            #[inline]
            pub fn sub(self, other: Self) -> Self {
                self.zip(other, ops::Sub::sub)
            }
        }
        impl_simd_type!(common $name $size_align => $t $n);
    };
    (common $name:ident $size_align:literal => $t:ident $n:expr) => {
        impl Vector for $name<[$t; $n]> {
            type Element = $t;
            const LANES: usize = $n;
        }
        impl internals::ToSimd for [$t; $n] {
            type Vector = $name<[$t; $n]>;
        }
        impl $name<[$t; $n]> {
            #[inline]
            fn zip(self, other: Self, f: impl Fn($t, $t) -> $t) -> Self {
                Self(array_utils::zip(self.0, other.0, f))
            }

            #[inline]
            pub fn splat(value: $t) -> Self {
                Self([value; $n])
            }
        }

        impl Default for $name<[$t; $n]> {
            #[inline]
            fn default() -> Self { Self::splat(Default::default()) }
        }

        impl fmt::Debug for $name<[$t; $n]> {
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                <[$t] as fmt::Debug>::fmt(&**self, f)
            }
        }

        impl From<[$t; $n]> for $name<[$t; $n]> {
            #[inline]
            fn from(other: [$t; $n]) -> Self {
                Self(other)
            }
        }

        impl From<$name<[$t; $n]>> for [$t; $n] {
            #[inline]
            fn from(other: $name<[$t; $n]>) -> Self {
                other.0
            }
        }
    };
}

macro_rules! define_simd_types {
    ($($name:ident $size_align:literal;)+) => {$(
        #[repr(C, align($size_align))]
        #[derive(Copy, Clone)]
        pub struct $name<TArray>(TArray);
        impl_simd_type!($name $size_align);
        impl<TArray> ops::Deref for $name<TArray> {
            type Target = TArray;
            #[inline]
            fn deref(&self) -> &TArray {
                &self.0
            }
        }
        impl<TArray> ops::DerefMut for $name<TArray> {
            #[inline]
            fn deref_mut(&mut self) -> &mut TArray {
                &mut self.0
            }
        }
    )+};
}

define_simd_types!(
    Simd64 8;
    Simd128 16;
    Simd256 32;
    Simd512 64;
);

mod array_utils;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let a = I32x4::from([1, 2, 3, 4]);
        let b = I32x4::from([45, 56, 78, 89]);
        let c = b.wrapping_sub(a);
        assert_eq!(c[..], [44, 54, 75, 85]);
    }

    #[test]
    fn defaults() {
        I8x8::default();
        I8x16::default();
        I8x32::default();
        I8x64::default();
    }
}