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
use super::super::*;
use {bitcast, simd_cast, f32x2};

pub use sixty_four::{f64x2, i64x2, u64x2, bool64ix2, bool64fx2};

//pub use super::{u64x2, i64x2, f64x2, bool64ix2, bool64fx2};

// strictly speaking, these are SSE instructions, not SSE2.
extern "platform-intrinsic" {
    fn x86_mm_movemask_ps(x: f32x4) -> i32;
    fn x86_mm_max_ps(x: f32x4, y: f32x4) -> f32x4;
    fn x86_mm_min_ps(x: f32x4, y: f32x4) -> f32x4;
    fn x86_mm_rsqrt_ps(x: f32x4) -> f32x4;
    fn x86_mm_rcp_ps(x: f32x4) -> f32x4;
    fn x86_mm_sqrt_ps(x: f32x4) -> f32x4;
}

extern "platform-intrinsic" {
    fn x86_mm_adds_epi8(x: i8x16, y: i8x16) -> i8x16;
    fn x86_mm_adds_epu8(x: u8x16, y: u8x16) -> u8x16;
    fn x86_mm_adds_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_adds_epu16(x: u16x8, y: u16x8) -> u16x8;
    fn x86_mm_avg_epu8(x: u8x16, y: u8x16) -> u8x16;
    fn x86_mm_avg_epu16(x: u16x8, y: u16x8) -> u16x8;
    fn x86_mm_madd_epi16(x: i16x8, y: i16x8) -> i32x4;
    fn x86_mm_max_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_max_epu8(x: u8x16, y: u8x16) -> u8x16;
    fn x86_mm_max_pd(x: f64x2, y: f64x2) -> f64x2;
    fn x86_mm_min_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_min_epu8(x: u8x16, y: u8x16) -> u8x16;
    fn x86_mm_min_pd(x: f64x2, y: f64x2) -> f64x2;
    fn x86_mm_movemask_pd(x: f64x2) -> i32;
    fn x86_mm_movemask_epi8(x: i8x16) -> i32;
    fn x86_mm_mul_epu32(x: u32x4, y: u32x4) -> u64x2;
    fn x86_mm_mulhi_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_mulhi_epu16(x: u16x8, y: u16x8) -> u16x8;
    fn x86_mm_packs_epi16(x: i16x8, y: i16x8) -> i8x16;
    fn x86_mm_packs_epi32(x: i32x4, y: i32x4) -> i16x8;
    fn x86_mm_packus_epi16(x: i16x8, y: i16x8) -> u8x16;
    fn x86_mm_sad_epu8(x: u8x16, y: u8x16) -> u64x2;
    fn x86_mm_sqrt_pd(x: f64x2) -> f64x2;
    fn x86_mm_subs_epi8(x: i8x16, y: i8x16) -> i8x16;
    fn x86_mm_subs_epu8(x: u8x16, y: u8x16) -> u8x16;
    fn x86_mm_subs_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_subs_epu16(x: u16x8, y: u16x8) -> u16x8;
}

#[doc(hidden)]
pub mod common {
    use super::super::super::*;
    use std::mem;

    #[inline]
    pub fn f32x4_sqrt(x: f32x4) -> f32x4 {
        unsafe {super::x86_mm_sqrt_ps(x)}
    }
    #[inline]
    pub fn f32x4_approx_rsqrt(x: f32x4) -> f32x4 {
        unsafe {super::x86_mm_rsqrt_ps(x)}
    }
    #[inline]
    pub fn f32x4_approx_reciprocal(x: f32x4) -> f32x4 {
        unsafe {super::x86_mm_rcp_ps(x)}
    }
    #[inline]
    pub fn f32x4_max(x: f32x4, y: f32x4) -> f32x4 {
        unsafe {super::x86_mm_max_ps(x, y)}
    }
    #[inline]
    pub fn f32x4_min(x: f32x4, y: f32x4) -> f32x4 {
        unsafe {super::x86_mm_min_ps(x, y)}
    }

    macro_rules! bools {
        ($($ty: ty, $all: ident, $any: ident, $movemask: ident, $width: expr;)*) => {
            $(
                #[inline]
                pub fn $all(x: $ty) -> bool {
                    unsafe {
                        super::$movemask(mem::transmute(x)) == (1 << $width) - 1
                    }
                }
                #[inline]
                pub fn $any(x: $ty) -> bool {
                    unsafe {
                        super::$movemask(mem::transmute(x)) != 0
                    }
                }
                )*
        }
    }

    bools! {
        bool32fx4, bool32fx4_all, bool32fx4_any, x86_mm_movemask_ps, 4;
        bool8ix16, bool8ix16_all, bool8ix16_any, x86_mm_movemask_epi8, 16;
        bool16ix8, bool16ix8_all, bool16ix8_any, x86_mm_movemask_epi8, 16;
        bool32ix4, bool32ix4_all, bool32ix4_any, x86_mm_movemask_epi8, 16;
    }
}

// 32 bit floats

pub trait Sse2F32x4 {
    fn to_f64(self) -> f64x2;
    fn move_mask(self) -> u32;
}
impl Sse2F32x4 for f32x4 {
    #[inline]
    fn to_f64(self) -> f64x2 {
        unsafe {
            simd_cast(f32x2(self.0, self.1))
        }
    }
    fn move_mask(self) -> u32 {
        unsafe {x86_mm_movemask_ps(self) as u32}
    }
}
pub trait Sse2Bool32fx4 {
    fn move_mask(self) -> u32;
}
impl Sse2Bool32fx4 for bool32fx4 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_ps(bitcast(self)) as u32}
    }
}

// 64 bit floats

pub trait Sse2F64x2 {
    fn move_mask(self) -> u32;
    fn sqrt(self) -> Self;
    fn max(self, other: Self) -> Self;
    fn min(self, other: Self) -> Self;
}
impl Sse2F64x2 for f64x2 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_pd(bitcast(self)) as u32}
    }

    #[inline]
    fn sqrt(self) -> Self {
        unsafe { x86_mm_sqrt_pd(self) }
    }

    #[inline]
    fn max(self, other: Self) -> Self {
        unsafe { x86_mm_max_pd(self, other) }
    }
    #[inline]
    fn min(self, other: Self) -> Self {
        unsafe { x86_mm_min_pd(self, other) }
    }
}

pub trait Sse2Bool64fx2 {
    fn move_mask(self) -> u32;
}
impl Sse2Bool64fx2 for bool64fx2 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_pd(bitcast(self)) as u32}
    }
}

// 64 bit ints

pub trait Sse2U64x2 {}
impl Sse2U64x2 for u64x2 {}

pub trait Sse2I64x2 {}
impl Sse2I64x2 for i64x2 {}

pub trait Sse2Bool64ix2 {}
impl Sse2Bool64ix2 for bool64ix2 {}

// 32 bit ints

pub trait Sse2U32x4 {
    fn low_mul(self, other: Self) -> u64x2;
}
impl Sse2U32x4 for u32x4 {
    #[inline]
    fn low_mul(self, other: Self) -> u64x2 {
        unsafe { x86_mm_mul_epu32(self, other) }
    }
}

pub trait Sse2I32x4 {
    fn packs(self, other: Self) -> i16x8;
}
impl Sse2I32x4 for i32x4 {
    #[inline]
    fn packs(self, other: Self) -> i16x8 {
        unsafe { x86_mm_packs_epi32(self, other) }
    }
}

pub trait Sse2Bool32ix4 {}
impl Sse2Bool32ix4 for bool32ix4 {}

// 16 bit ints

pub trait Sse2U16x8 {
    fn adds(self, other: Self) -> Self;
    fn subs(self, other: Self) -> Self;
    fn avg(self, other: Self) -> Self;
    fn mulhi(self, other: Self) -> Self;
}
impl Sse2U16x8 for u16x8 {
    #[inline]
    fn adds(self, other: Self) -> Self {
        unsafe { x86_mm_adds_epu16(self, other) }
    }
    #[inline]
    fn subs(self, other: Self) -> Self {
        unsafe { x86_mm_subs_epu16(self, other) }
    }

    #[inline]
    fn avg(self, other: Self) -> Self {
        unsafe { x86_mm_avg_epu16(self, other) }
    }

    #[inline]
    fn mulhi(self, other: Self) -> Self {
        unsafe { x86_mm_mulhi_epu16(self, other) }
    }
}

pub trait Sse2I16x8 {
    fn adds(self, other: Self) -> Self;
    fn subs(self, other: Self) -> Self;
    fn madd(self, other: Self) -> i32x4;
    fn max(self, other: Self) -> Self;
    fn min(self, other: Self) -> Self;
    fn mulhi(self, other: Self) -> Self;
    fn packs(self, other: Self) -> i8x16;
    fn packus(self, other: Self) -> u8x16;
}
impl Sse2I16x8 for i16x8 {
    #[inline]
    fn adds(self, other: Self) -> Self {
        unsafe { x86_mm_adds_epi16(self, other) }
    }
    #[inline]
    fn subs(self, other: Self) -> Self {
        unsafe { x86_mm_subs_epi16(self, other) }
    }

    #[inline]
    fn madd(self, other: Self) -> i32x4 {
        unsafe { x86_mm_madd_epi16(self, other) }
    }

    #[inline]
    fn max(self, other: Self) -> Self {
        unsafe { x86_mm_max_epi16(self, other) }
    }
    #[inline]
    fn min(self, other: Self) -> Self {
        unsafe { x86_mm_min_epi16(self, other) }
    }

    #[inline]
    fn mulhi(self, other: Self) -> Self {
        unsafe { x86_mm_mulhi_epi16(self, other) }
    }

    #[inline]
    fn packs(self, other: Self) -> i8x16 {
        unsafe { x86_mm_packs_epi16(self, other) }
    }
    #[inline]
    fn packus(self, other: Self) -> u8x16 {
        unsafe { x86_mm_packus_epi16(self, other) }
    }
}

pub trait Sse2Bool16ix8 {}
impl Sse2Bool16ix8 for bool16ix8 {}

// 8 bit ints

pub trait Sse2U8x16 {
    fn move_mask(self) -> u32;
    fn adds(self, other: Self) -> Self;
    fn subs(self, other: Self) -> Self;
    fn avg(self, other: Self) -> Self;
    fn max(self, other: Self) -> Self;
    fn min(self, other: Self) -> Self;
    fn sad(self, other: Self) -> u64x2;
}
impl Sse2U8x16 for u8x16 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_epi8(bitcast(self)) as u32}
    }

    #[inline]
    fn adds(self, other: Self) -> Self {
        unsafe { x86_mm_adds_epu8(self, other) }
    }
    #[inline]
    fn subs(self, other: Self) -> Self {
        unsafe { x86_mm_subs_epu8(self, other) }
    }

    #[inline]
    fn avg(self, other: Self) -> Self {
        unsafe { x86_mm_avg_epu8(self, other) }
    }

    #[inline]
    fn max(self, other: Self) -> Self {
        unsafe { x86_mm_max_epu8(self, other) }
    }
    #[inline]
    fn min(self, other: Self) -> Self {
        unsafe { x86_mm_min_epu8(self, other) }
    }

    #[inline]
    fn sad(self, other: Self) -> u64x2 {
        unsafe { x86_mm_sad_epu8(self, other) }
    }
}

pub trait Sse2I8x16 {
    fn move_mask(self) -> u32;
    fn adds(self, other: Self) -> Self;
    fn subs(self, other: Self) -> Self;
}
impl Sse2I8x16 for i8x16 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_epi8(bitcast(self)) as u32}
    }

    #[inline]
    fn adds(self, other: Self) -> Self {
        unsafe { x86_mm_adds_epi8(self, other) }
    }
    #[inline]
    fn subs(self, other: Self) -> Self {
        unsafe { x86_mm_subs_epi8(self, other) }
    }
}

pub trait Sse2Bool8ix16 {
    fn move_mask(self) -> u32;
}
impl Sse2Bool8ix16 for bool8ix16 {
    #[inline]
    fn move_mask(self) -> u32 {
        unsafe { x86_mm_movemask_epi8(bitcast(self)) as u32}
    }
}