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
use super::super::*;
use bitcast;

macro_rules! bitcast {
    ($func: ident($a: ident, $b: ident)) => {
        bitcast($func(bitcast($a), bitcast($b)))
    }
}

extern "platform-intrinsic" {
    fn x86_mm_abs_epi8(x: i8x16) -> i8x16;
    fn x86_mm_abs_epi16(x: i16x8) -> i16x8;
    fn x86_mm_abs_epi32(x: i32x4) -> i32x4;
    fn x86_mm_hadd_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_hadd_epi32(x: i32x4, y: i32x4) -> i32x4;
    fn x86_mm_hadds_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_hsub_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_hsub_epi32(x: i32x4, y: i32x4) -> i32x4;
    fn x86_mm_hsubs_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_maddubs_epi16(x: u8x16, y: i8x16) -> i16x8;
    fn x86_mm_mulhrs_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_shuffle_epi8(x: i8x16, y: i8x16) -> i8x16;
    fn x86_mm_sign_epi8(x: i8x16, y: i8x16) -> i8x16;
    fn x86_mm_sign_epi16(x: i16x8, y: i16x8) -> i16x8;
    fn x86_mm_sign_epi32(x: i32x4, y: i32x4) -> i32x4;
}

// 32 bit integers

pub trait Ssse3I32x4 {
    fn abs(self) -> Self;
    fn hadd(self, other: Self) -> Self;
    fn hsub(self, other: Self) -> Self;
    fn sign(self, other: Self) -> Self;
}
impl Ssse3I32x4 for i32x4 {
    #[inline]
    fn abs(self) -> Self {
        unsafe { x86_mm_abs_epi32(self) }
    }

    #[inline]
    fn hadd(self, other: Self) -> Self {
        unsafe { x86_mm_hadd_epi32(self, other) }
    }
    #[inline]
    fn hsub(self, other: Self) -> Self {
        unsafe { x86_mm_hsub_epi32(self, other) }
    }

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

pub trait Ssse3U32x4 {
    fn hadd(self, other: Self) -> Self;
    fn hsub(self, other: Self) -> Self;
}
impl Ssse3U32x4 for u32x4 {
    #[inline]
    fn hadd(self, other: Self) -> Self {
        unsafe { bitcast!(x86_mm_hadd_epi32(self, other)) }
    }
    #[inline]
    fn hsub(self, other: Self) -> Self {
        unsafe { bitcast!(x86_mm_hsub_epi32(self, other)) }
    }
}

// 16 bit integers

pub trait Ssse3I16x8 {
    fn abs(self) -> Self;
    fn hadd(self, other: Self) -> Self;
    fn hadds(self, other: Self) -> Self;
    fn hsub(self, other: Self) -> Self;
    fn hsubs(self, other: Self) -> Self;
    fn sign(self, other: Self) -> Self;
    fn mulhrs(self, other: Self) -> Self;
}
impl Ssse3I16x8 for i16x8 {
    #[inline]
    fn abs(self) -> Self {
        unsafe { x86_mm_abs_epi16(self) }
    }

    #[inline]
    fn hadd(self, other: Self) -> Self {
        unsafe { x86_mm_hadd_epi16(self, other) }
    }
    #[inline]
    fn hadds(self, other: Self) -> Self {
        unsafe { x86_mm_hadds_epi16(self, other) }
    }
    #[inline]
    fn hsub(self, other: Self) -> Self {
        unsafe { x86_mm_hsub_epi16(self, other) }
    }
    #[inline]
    fn hsubs(self, other: Self) -> Self {
        unsafe { x86_mm_hsubs_epi16(self, other) }
    }

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

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

pub trait Ssse3U16x8 {
    fn hadd(self, other: Self) -> Self;
    fn hsub(self, other: Self) -> Self;
}
impl Ssse3U16x8 for u16x8 {
    #[inline]
    fn hadd(self, other: Self) -> Self {
        unsafe { bitcast!(x86_mm_hadd_epi16(self, other)) }
    }
    #[inline]
    fn hsub(self, other: Self) -> Self {
        unsafe { bitcast!(x86_mm_hsub_epi16(self, other)) }
    }
}


// 8 bit integers

pub trait Ssse3U8x16 {
    fn shuffle_bytes(self, indices: Self) -> Self;
    fn maddubs(self, other: i8x16) -> i16x8;
}

impl Ssse3U8x16 for u8x16 {
    #[inline]
    fn shuffle_bytes(self, indices: Self) -> Self {
        unsafe {bitcast!(x86_mm_shuffle_epi8(self, indices))}
    }

    fn maddubs(self, other: i8x16) -> i16x8 {
        unsafe { x86_mm_maddubs_epi16(self, other) }
    }
}

pub trait Ssse3I8x16 {
    fn abs(self) -> Self;
    fn shuffle_bytes(self, indices: Self) -> Self;
    fn sign(self, other: Self) -> Self;
}
impl Ssse3I8x16 for i8x16 {
    #[inline]
    fn abs(self) -> Self {
        unsafe {x86_mm_abs_epi8(self)}
    }
    #[inline]
    fn shuffle_bytes(self, indices: Self) -> Self {
        unsafe {
            x86_mm_shuffle_epi8(self, indices)
        }
    }

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