38 #ifndef included_vector_sse2_h 39 #define included_vector_sse2_h 42 #include <x86intrin.h> 46 {
return (u8x16) _mm_unpackhi_epi8 ((__m128i) a, (__m128i) b); }
49 {
return (u8x16) _mm_unpacklo_epi8 ((__m128i) a, (__m128i) b); }
52 {
return (u16x8) _mm_unpackhi_epi16 ((__m128i) a, (__m128i) b); }
55 {
return (u16x8) _mm_unpacklo_epi16 ((__m128i) a, (__m128i) b); }
58 {
return (u32x4) _mm_unpackhi_epi32 ((__m128i) a, (__m128i) b); }
61 {
return (u32x4) _mm_unpacklo_epi32 ((__m128i) a, (__m128i) b); }
64 {
return (u64x2) _mm_unpackhi_epi64 ((__m128i) a, (__m128i) b); }
67 {
return (u64x2) _mm_unpacklo_epi64 ((__m128i) a, (__m128i) b); }
71 {
return (u8x8) _m_punpckhbw ((__m64) a, (__m64) b); }
74 {
return (u8x8) _m_punpcklbw ((__m64) a, (__m64) b); }
77 {
return (u16x4) _m_punpckhwd ((__m64) a, (__m64) b); }
80 {
return (u16x4) _m_punpcklwd ((__m64) a, (__m64) b); }
83 {
return (u32x2) _m_punpckhdq ((__m64) a, (__m64) b); }
86 {
return (u32x2) _m_punpckldq ((__m64) a, (__m64) b); }
90 {
return (u8x16) _mm_packus_epi16 ((__m128i) lo, (__m128i) hi); }
93 {
return (i8x16) _mm_packs_epi16 ((__m128i) lo, (__m128i) hi); }
96 {
return (u16x8) _mm_packs_epi32 ((__m128i) lo, (__m128i) hi); }
100 {
return (u8x8) _m_packuswb ((__m64) lo, (__m64) hi); }
103 {
return (i8x8) _m_packsswb ((__m64) lo, (__m64) hi); }
106 {
return (u16x4) _m_packssdw ((__m64) lo, (__m64) hi); }
109 {
return (i16x4) _m_packssdw ((__m64) lo, (__m64) hi); }
160 #define i64x2_splat u64x2_splat 161 #define i32x4_splat u32x4_splat 162 #define i16x8_splat u16x8_splat 163 #define i8x16_splat u8x16_splat 164 #define i32x2_splat u32x2_splat 165 #define i16x4_splat u16x4_splat 166 #define i8x8_splat u8x8_splat 170 {
return (u64x2) _mm_loadl_pi ((__m128) x, (__m64 *) a); }
173 {
return (u64x2) _mm_loadh_pi ((__m128) x, (__m64 *) a); }
176 { _mm_storel_pi ((__m64 *) a, (__m128) x); }
179 { _mm_storeh_pi ((__m64 *) a, (__m128) x); }
185 always_inline void t##_store_unaligned (t x, t * a) \ 186 { _mm_storeu_si128 ((__m128i *) a, (__m128i) x); } \ 187 always_inline t t##_load_unaligned (t * a) \ 188 { return (t) _mm_loadu_si128 ((__m128i *) a); } 201 #define _signed_binop(n,m,f,g) \ 203 always_inline u##n##x##m \ 204 u##n##x##m##_##f (u##n##x##m x, u##n##x##m y) \ 205 { return (u##n##x##m) _mm_##g##n ((__m128i) x, (__m128i) y); } \ 208 always_inline i##n##x##m \ 209 i##n##x##m##_##f (i##n##x##m x, i##n##x##m y) \ 210 { return (i##n##x##m) _mm_##g##n ((__m128i) x, (__m128i) y); } 213 _signed_binop (8, 16,
add, add_epi)
214 _signed_binop (16, 8,
add, add_epi)
215 _signed_binop (32, 4, add, add_epi)
216 _signed_binop (64, 2, add, add_epi)
217 _signed_binop (8, 16,
sub, sub_epi)
218 _signed_binop (16, 8,
sub, sub_epi)
219 _signed_binop (32, 4, sub, sub_epi)
220 _signed_binop (64, 2, sub, sub_epi)
231 {
return (
i16x8) _mm_mullo_epi16 ((__m128i) x, (__m128i) y); }
234 {
return (u16x8) _mm_mullo_epi16 ((__m128i) x, (__m128i) y); }
237 {
return (
i16x8) _mm_mulhi_epu16 ((__m128i) x, (__m128i) y); }
240 {
return (u16x8) _mm_mulhi_epu16 ((__m128i) x, (__m128i) y); }
244 #define _(p,a,b,c,f) \ 245 always_inline p##a##x##b p##a##x##b##_ishift_##c (p##a##x##b x, int i) \ 246 { return (p##a##x##b) _mm_##f##i_epi##a ((__m128i) x, i); } \ 248 always_inline p##a##x##b p##a##x##b##_shift_##c (p##a##x##b x, p##a##x##b y) \ 249 { return (p##a##x##b) _mm_##f##_epi##a ((__m128i) x, (__m128i) y); } 251 _ (u, 16, 8,
left, sll)
253 _ (u, 64, 2,
left, sll)
255 _ (u, 32, 4,
right, srl)
256 _ (u, 64, 2,
right, srl)
257 _ (
i, 16, 8,
left, sll)
258 _ (
i, 32, 4,
left, sll)
259 _ (
i, 64, 2,
left, sll)
267 {
return (u16x4) _m_psllw ((__m64) x, (__m64) i); };
270 {
return (u32x2) _m_pslld ((__m64) x, (__m64) i); };
273 {
return (u16x4) _m_psrlw ((__m64) x, (__m64) i); };
276 {
return (u32x2) _m_psrld ((__m64) x, (__m64) i); };
279 {
return (i16x4) _m_psllw ((__m64) x, (__m64) i); };
282 {
return (i32x2) _m_pslld ((__m64) x, (__m64) i); };
285 {
return (i16x4) _m_psraw ((__m64) x, (__m64) i); };
288 {
return (i32x2) _m_psrad ((__m64) x, (__m64) i); };
290 #define u8x16_word_shift_left(a,n) (u8x16) _mm_slli_si128((__m128i) a, n) 291 #define u8x16_word_shift_right(a,n) (u8x16) _mm_srli_si128((__m128i) a, n) 293 #define i8x16_word_shift_left(a,n) \ 294 ((i8x16) u8x16_word_shift_left((u8x16) (a), (n))) 295 #define i8x16_word_shift_right(a,n) \ 296 ((i8x16) u8x16_word_shift_right((u8x16) (a), (n))) 298 #define u16x8_word_shift_left(a,n) \ 299 ((u16x8) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u16))) 300 #define i16x8_word_shift_left(a,n) \ 301 ((u16x8) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u16))) 302 #define u16x8_word_shift_right(a,n) \ 303 ((u16x8) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u16))) 304 #define i16x8_word_shift_right(a,n) \ 305 ((i16x8) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u16))) 307 #define u32x4_word_shift_left(a,n) \ 308 ((u32x4) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u32))) 309 #define i32x4_word_shift_left(a,n) \ 310 ((u32x4) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u32))) 311 #define u32x4_word_shift_right(a,n) \ 312 ((u32x4) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u32))) 313 #define i32x4_word_shift_right(a,n) \ 314 ((i32x4) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u32))) 316 #define u64x2_word_shift_left(a,n) \ 317 ((u64x2) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u64))) 318 #define i64x2_word_shift_left(a,n) \ 319 ((u64x2) u8x16_word_shift_left((u8x16) (a), (n) * sizeof (u64))) 320 #define u64x2_word_shift_right(a,n) \ 321 ((u64x2) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u64))) 322 #define i64x2_word_shift_right(a,n) \ 323 ((i64x2) u8x16_word_shift_right((u8x16) (a), (n) * sizeof (u64))) 326 #define _(t,n,lr1,lr2) \ 327 always_inline t##x##n \ 328 t##x##n##_irotate_##lr1 (t##x##n w, int i) \ 330 ASSERT (i >= 0 && i <= BITS (t)); \ 331 return (t##x##n##_ishift_##lr1 (w, i) \ 332 | t##x##n##_ishift_##lr2 (w, BITS (t) - i)); \ 335 always_inline t##x##n \ 336 t##x##n##_rotate_##lr1 (t##x##n w, t##x##n i) \ 338 t##x##n j = t##x##n##_splat (BITS (t)); \ 339 return (t##x##n##_shift_##lr1 (w, i) \ 340 | t##x##n##_shift_##lr2 (w, j - i)); \ 353 #define _(t,n,lr1,lr2) \ 354 always_inline t##x##n \ 355 t##x##n##_word_rotate2_##lr1 (t##x##n w0, t##x##n w1, int i) \ 357 int m = sizeof (t##x##n) / sizeof (t); \ 358 ASSERT (i >= 0 && i < m); \ 359 return (t##x##n##_word_shift_##lr1 (w0, i) \ 360 | t##x##n##_word_shift_##lr2 (w1, m - i)); \ 363 always_inline t##x##n \ 364 t##x##n##_word_rotate_##lr1 (t##x##n w0, int i) \ 365 { return t##x##n##_word_rotate2_##lr1 (w0, w0, i); } 381 {
return (u8x16) _mm_cmpeq_epi8 ((__m128i) x, (__m128i) y); }
384 {
return (i8x16) _mm_cmpeq_epi8 ((__m128i) x, (__m128i) y); }
387 {
return (u16x8) _mm_cmpeq_epi16 ((__m128i) x, (__m128i) y); }
390 {
return (
i16x8) _mm_cmpeq_epi16 ((__m128i) x, (__m128i) y); }
393 {
return (u32x4) _mm_cmpeq_epi32 ((__m128i) x, (__m128i) y); }
396 {
return (
i32x4) _mm_cmpeq_epi32 ((__m128i) x, (__m128i) y); }
400 {
return (u8x16) _mm_cmpgt_epi8 ((__m128i) x, (__m128i) y); }
404 {
return (u16x8) _mm_cmpgt_epi16 ((__m128i) x, (__m128i) y); }
408 {
return (u32x4) _mm_cmpgt_epi32 ((__m128i) x, (__m128i) y); }
428 #define u32x4_select(A,MASK) \ 432 asm volatile ("pshufd %[mask], %[x], %[y]" \ 434 : [x] "x" (_x), [mask] "i" (MASK)); \ 438 #define u32x4_splat_word(x,i) \ 439 u32x4_select ((x), (((i) << (2*0)) \ 449 asm volatile (
"movd %[x], %[result]" 450 : [result]
"=r" (result)
459 asm volatile (
"movd %[x], %[result]" 460 : [result]
"=x" (result)
475 {
return _mm_movemask_epi8 ((__m128i) x); }
505 {
return (u8x16) _mm_max_epu8 ((__m128i) x, (__m128i) y); }
513 return _mm_extract_epi16 ((__m128i) x, 0) & 0xff;
517 {
return (u8x16) _mm_min_epu8 ((__m128i) x, (__m128i) y); }
525 return _mm_extract_epi16 ((__m128i) x, 0) & 0xff;
529 {
return (
i16x8) _mm_max_epi16 ((__m128i) x, (__m128i) y); }
536 return _mm_extract_epi16 ((__m128i) x, 0);
540 {
return (
i16x8) _mm_min_epi16 ((__m128i) x, (__m128i) y); }
547 return _mm_extract_epi16 ((__m128i) x, 0);
always_inline i32x4 i32x4_set0(i32 x)
always_inline u64x2 u64x2_interleave_hi(u64x2 a, u64x2 b)
always_inline u8x16 u8x16_is_equal(u8x16 x, u8x16 y)
#define i16x8_word_shift_right(a, n)
always_inline u32 u32x4_zero_byte_mask(u32x4 x)
sll srl srl sll sra u16x4 i
#define u8x16_word_shift_right(a, n)
always_inline u8x16 u8x16_min(u8x16 x, u8x16 y)
always_inline u8x16 i8x16_is_greater(i8x16 x, i8x16 y)
always_inline u32x4 u32x4_is_equal(u32x4 x, u32x4 y)
always_inline i32x2 i32x2_shift_left(i32x2 x, i32x2 i)
always_inline u16x8 u16x8_splat(u16 a)
always_inline u32x2 u32x2_shift_left(u32x2 x, u32x2 i)
always_inline u16x8 u16x8_is_zero(u16x8 x)
always_inline u32x2 u32x2_interleave_lo(u32x2 a, u32x2 b)
add_epi add_epi sub_epi sub_epi adds_epu subs_epu i16x8 y
u8 u32x4_compare_word_mask_table[256]
add_epi add_epi sub_epi sub_epi add_saturate
always_inline u32 u8x16_max_scalar(u8x16 x)
always_inline u8x16 u8x16_splat(u8 a)
always_inline u32x4 u32x4_splat(u32 a)
always_inline i16x8 i16x8_min(i16x8 x, i16x8 y)
always_inline u16x8 u16x8_interleave_hi(u16x8 a, u16x8 b)
always_inline void u64x2_write_hi(u64x2 x, u64 *a)
always_inline u8x8 u8x8_interleave_lo(u8x8 a, u8x8 b)
always_inline u16x8 u16x8_interleave_lo(u16x8 a, u16x8 b)
always_inline u16x4 u16x4_interleave_hi(u16x4 a, u16x4 b)
always_inline u8x16 u8x16_interleave_hi(u8x16 a, u8x16 b)
always_inline u32x4 u32x4_set0(u32 x)
always_inline u8x8 u8x8_splat(u8 a)
always_inline u16x8 u16x8_mul_lo(u16x8 x, u16x8 y)
always_inline u8x16 u8x16_max(u8x16 x, u8x16 y)
always_inline u32x2 u32x2_splat(u32 a)
always_inline u32 u8x16_zero_byte_mask(u8x16 x)
always_inline i16x8 i16x8_mul_hi(i16x8 x, i16x8 y)
always_inline void u64x2_write_lo(u64x2 x, u64 *a)
always_inline u32x4 i32x4_is_greater(i32x4 x, i32x4 y)
always_inline u8 u8x16_min_scalar(u8x16 x)
always_inline u8x16 u8x16_is_zero(u8x16 x)
always_inline u32x4 u32x4_interleave_hi(u32x4 a, u32x4 b)
always_inline u16x4 u16x4_shift_right(u16x4 x, u16x4 i)
always_inline u16x8 u16x8_mul_hi(u16x8 x, u16x8 y)
always_inline u64x2 u64x2_splat(u64 a)
always_inline u8x16 u16x8_pack(u16x8 lo, u16x8 hi)
always_inline u16x4 u32x2_pack(u32x2 lo, u32x2 hi)
always_inline u64x2 u64x2_read_hi(u64x2 x, u64 *a)
add_epi add_epi sub_epi sub_epi adds_epu sub_saturate
always_inline u16x4 u16x4_splat(u16 a)
always_inline u16x8 u16x8_is_equal(u16x8 x, u16x8 y)
always_inline u32 u32x4_compare_word_mask(u32x4 x)
always_inline u8x8 u8x8_interleave_hi(u8x8 a, u8x8 b)
always_inline i8x16 i8x16_is_equal(i8x16 x, i8x16 y)
always_inline i8x8 i16x4_pack(i16x4 lo, i16x4 hi)
always_inline u64x2 u64x2_interleave_lo(u64x2 a, u64x2 b)
always_inline i16 i16x8_min_scalar(i16x8 x)
always_inline u16x8 i16x8_is_greater(i16x8 x, i16x8 y)
always_inline u64x2 u64x2_read_lo(u64x2 x, u64 *a)
always_inline u32x4 u32x4_is_zero(u32x4 x)
always_inline i32x2 i32x2_shift_right(i32x2 x, i32x2 i)
always_inline u32 u32x4_get0(u32x4 x)
always_inline i16x8 i16x8_is_equal(i16x8 x, i16x8 y)
always_inline u32 u16x8_zero_byte_mask(u16x8 x)
always_inline u8x8 u16x4_pack(u16x4 lo, u16x4 hi)
always_inline i16x4 i32x2_pack(i32x2 lo, i32x2 hi)
always_inline i16x4 i16x4_shift_left(i16x4 x, i16x4 i)
always_inline u32x4 u32x4_interleave_lo(u32x4 a, u32x4 b)
always_inline i8x16 i16x8_pack(i16x8 lo, i16x8 hi)
always_inline u8x16 u8x16_interleave_lo(u8x16 a, u8x16 b)
always_inline u32 u8x16_compare_byte_mask(u8x16 x)
always_inline u32x2 u32x2_interleave_hi(u32x2 a, u32x2 b)
always_inline u16x8 u32x4_pack(u32x4 lo, u32x4 hi)
always_inline i32x4 i32x4_is_equal(i32x4 x, i32x4 y)
always_inline i16x4 i16x4_shift_right(i16x4 x, i16x4 i)
always_inline u32x2 u32x2_shift_right(u32x2 x, u32x2 i)
always_inline i32 i32x4_get0(i32x4 x)
always_inline u16x4 u16x4_interleave_lo(u16x4 a, u16x4 b)
always_inline i16x8 i16x8_max(i16x8 x, i16x8 y)
always_inline i16 i16x8_max_scalar(i16x8 x)