source: trunk/lib/sse_simd.h @ 58

Last change on this file since 58 was 58, checked in by cameron, 11 years ago

simd_splat_16/32

File size: 20.0 KB
Line 
1/*  Idealized SIMD Operations with SSE versions
2    Copyright (C) 2006, 2007, 2008, Robert D. Cameron
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters Inc.
5       under the Academic Free License version 3.0.
6*/
7#ifndef SSE_SIMD_H
8#define SSE_SIMD_H
9
10/*------------------------------------------------------------*/
11#include <stdint.h>
12#include <limits.h>
13#ifndef LONG_BIT
14#define LONG_BIT (8* sizeof(unsigned long))
15#endif
16#include <emmintrin.h>
17#ifdef USE_LDDQU
18#include <pmmintrin.h>
19#endif
20typedef __m128i SIMD_type;
21/*------------------------------------------------------------*/
22/* I. SIMD bitwise logical operations */
23
24#define simd_or(b1, b2) _mm_or_si128(b1, b2)
25#define simd_and(b1, b2) _mm_and_si128(b1, b2)
26#define simd_xor(b1, b2) _mm_xor_si128(b1, b2)
27#define simd_andc(b1, b2) _mm_andnot_si128(b2, b1)
28#define simd_if(cond, then_val, else_val) \
29  simd_or(simd_and(then_val, cond), simd_andc(else_val, cond))
30#define simd_not(b) (~b)
31#define simd_nor(a,b) (~simd_or(a,b))
32
33
34/*  Specific constants. */
35#define simd_himask_2 _mm_set1_epi8(0xAA)
36#define simd_himask_4 _mm_set1_epi8(0xCC)
37#define simd_himask_8 _mm_set1_epi8(0xF0)
38/* Little-endian */
39#define simd_himask_16 _mm_set1_epi16(0xFF00)
40#define simd_himask_32 _mm_set1_epi32(0xFFFF0000)
41#define simd_himask_64 _mm_set_epi32(-1,0,-1,0)
42#define simd_himask_128 _mm_set_epi32(-1,-1,0,0)
43
44/* Idealized operations with direct implementation by built-in
45   operations for various target architectures. */
46
47#define simd_add_8(a, b) _mm_add_epi8(a, b)
48#define simd_add_16(a, b) _mm_add_epi16(a, b)
49#define simd_add_32(a, b) _mm_add_epi32(a, b)
50#define simd_add_64(a, b) _mm_add_epi64(a, b)
51#define simd_sub_8(a, b) _mm_sub_epi8(a, b)
52#define simd_sub_16(a, b) _mm_sub_epi16(a, b)
53#define simd_sub_32(a, b) _mm_sub_epi32(a, b)
54#define simd_sub_64(a, b) _mm_sub_epi64(a, b)
55#define simd_mult_16(a, b) _mm_mullo_epi16(a, b)
56#define simd_slli_16(r, shft) _mm_slli_epi16(r, shft)
57#define simd_srli_16(r, shft) _mm_srli_epi16(r, shft)
58#define simd_srai_16(r, shft) _mm_srai_epi16(r, shft)
59#define simd_slli_32(r, shft) _mm_slli_epi32(r, shft)
60#define simd_srli_32(r, shft) _mm_srli_epi32(r, shft)
61#define simd_srai_32(r, shft) _mm_srai_epi32(r, shft)
62#define simd_slli_64(r, shft) _mm_slli_epi64(r, shft)
63#define simd_srli_64(r, shft) _mm_srli_epi64(r, shft)
64#define simd_sll_64(r, shft_reg) _mm_sll_epi64(r, shft_reg)
65#define simd_srl_64(r, shft_reg) _mm_srl_epi64(r, shft_reg)
66#define simd_pack_16(a, b) \
67  _mm_packus_epi16(simd_andc(b, simd_himask_16), simd_andc(a, simd_himask_16))
68#define simd_mergeh_8(a, b) _mm_unpackhi_epi8(b, a)
69#define simd_mergeh_16(a, b) _mm_unpackhi_epi16(b, a)
70#define simd_mergeh_32(a, b) _mm_unpackhi_epi32(b, a)
71#define simd_mergeh_64(a, b) _mm_unpackhi_epi64(b, a)
72#define simd_mergel_8(a, b) _mm_unpacklo_epi8(b, a)
73#define simd_mergel_16(a, b) _mm_unpacklo_epi16(b, a)
74#define simd_mergel_32(a, b) _mm_unpacklo_epi32(b, a)
75#define simd_mergel_64(a, b) _mm_unpacklo_epi64(b, a)
76#define simd_eq_8(a, b) _mm_cmpeq_epi8(a, b)
77#define simd_eq_16(a, b) _mm_cmpeq_epi16(a, b)
78#define simd_eq_32(a, b) _mm_cmpeq_epi32(a, b)
79
80#define simd_max_8(a, b) _mm_max_epu8(a, b)
81
82#define simd_slli_128(r, shft) \
83  ((shft) % 8 == 0 ? _mm_slli_si128(r, (shft)/8) : \
84   (shft) >= 64 ? simd_slli_64(_mm_slli_si128(r, 8), (shft) - 64) : \
85   simd_or(simd_slli_64(r, shft), _mm_slli_si128(simd_srli_64(r, 64-(shft)), 8)))
86
87#define simd_srli_128(r, shft) \
88  ((shft) % 8 == 0 ? _mm_srli_si128(r, (shft)/8) : \
89   (shft) >= 64 ? simd_srli_64(_mm_srli_si128(r, 8), (shft) - 64) : \
90   simd_or(simd_srli_64(r, shft), _mm_srli_si128(simd_slli_64(r, 64-(shft)), 8)))
91
92#define simd_sll_128(r, shft) \
93   simd_or(simd_sll_64(r, shft), \
94           simd_or(_mm_slli_si128(simd_sll_64(r, simd_sub_32(shft, sisd_from_int(64))), 8), \
95                   _mm_slli_si128(simd_srl_64(r, simd_sub_32(sisd_from_int(64), shft)), 8)))
96
97#define simd_srl_128(r, shft) \
98   simd_or(simd_srl_64(r, shft), \
99           simd_or(_mm_srli_si128(simd_srl_64(r, simd_sub_32(shft, sisd_from_int(64))), 8), \
100                   _mm_srli_si128(simd_sll_64(r, simd_sub_32(sisd_from_int(64), shft)), 8)))
101
102#define sisd_sll(r, shft) simd_sll_128(r, shft)
103#define sisd_srl(r, shft) simd_srl_128(r, shft)
104#define sisd_slli(r, shft) simd_slli_128(r, shft)
105#define sisd_srli(r, shft) simd_srli_128(r, shft)
106#define sisd_add(a, b) simd_add_128(a, b)
107#define sisd_sub(a, b) simd_sub_128(a, b)
108
109#define sisd_store_aligned(r, addr) _mm_store_si128(addr, r)
110#define sisd_store_unaligned(r, addr) _mm_storeu_si128(addr, r)
111#define sisd_load_aligned(addr) _mm_load_si128(addr)
112#ifndef USE_LDDQU
113#define sisd_load_unaligned(addr) _mm_loadu_si128(addr)
114#endif
115#ifdef USE_LDDQU
116#define sisd_load_unaligned(addr) _mm_lddqu_si128(addr)
117#endif
118
119
120
121#define simd_const_32(n) _mm_set1_epi32(n)
122#define simd_const_16(n) _mm_set1_epi16(n)
123#define simd_const_8(n) _mm_set1_epi8(n)
124#define simd_const_4(n) _mm_set1_epi8((n)<<4|(n))
125#define simd_const_2(n) simd_const_4(n<<2|n)
126#define simd_const_1(n) \
127  (n==0 ? simd_const_8(0): simd_const_8(-1))
128
129#define simd_pack_16_ll(a, b) simd_pack_16(a, b)
130#define simd_pack_16_hh(a, b) \
131  simd_pack_16(simd_srli_16(a, 8), simd_srli_16(b, 8))
132
133
134static inline
135SIMD_type simd_add_2(SIMD_type a, SIMD_type b)
136{
137         SIMD_type c1 = simd_xor(a,b);
138         SIMD_type borrow = simd_and(a,b);
139         SIMD_type c2 = simd_xor(c1,(sisd_slli(borrow,1)));
140         return simd_if(simd_himask_2,c2,c1);
141}
142#define simd_add_4(a, b)\
143        simd_if(simd_himask_8, simd_add_8(simd_and(a,simd_himask_8),simd_and(b,simd_himask_8))\
144        ,simd_add_8(simd_andc(a,simd_himask_8),simd_andc(b,simd_himask_8)))
145
146#define simd_srli_2(r, sh)\
147         simd_and(simd_srli_32(r,sh),simd_const_2(3>>sh))
148
149#define simd_srli_4(r, sh)\
150         simd_and(simd_srli_32(r,sh),simd_const_4(15>>sh))
151#define simd_srli_8(r, sh)\
152         simd_and(simd_srli_32(r,sh),simd_const_8(255>>sh))
153
154#define simd_slli_2(r, sh)\
155         simd_and(simd_slli_32(r,sh),simd_const_2((3<<sh)&3))
156
157#define simd_slli_4(r, sh)\
158         simd_and(simd_slli_32(r,sh),simd_const_4((15<<sh)&15))
159#define simd_slli_8(r, sh)\
160         simd_and(simd_slli_32(r,sh),simd_const_8((255<<sh) &255))
161
162
163
164
165#define simd_mergeh_4(a,b)\
166        simd_mergeh_8(simd_if(simd_himask_8,a,simd_srli_8(b,4)),\
167        simd_if(simd_himask_8,simd_slli_8(a,4),b))
168#define simd_mergel_4(a,b)\
169        simd_mergel_8(simd_if(simd_himask_8,a,simd_srli_8(b,4)),\
170        simd_if(simd_himask_8,simd_slli_8(a,4),b))
171#define simd_mergeh_2(a,b)\
172        simd_mergeh_4(simd_if(simd_himask_4,a,simd_srli_4(b,2)),\
173        simd_if(simd_himask_4,simd_slli_4(a,2),b))
174#define simd_mergel_2(a,b)\
175        simd_mergel_4(simd_if(simd_himask_4,a,simd_srli_4(b,2)),\
176        simd_if(simd_himask_4,simd_slli_4(a,2),b))
177#define simd_mergeh_1(a,b)\
178        simd_mergeh_2(simd_if(simd_himask_2,a,simd_srli_2(b,1)),\
179        simd_if(simd_himask_2,simd_slli_2(a,1),b))
180#define simd_mergel_1(a,b)\
181        simd_mergel_2(simd_if(simd_himask_2,a,simd_srli_2(b,1)),\
182        simd_if(simd_himask_2,simd_slli_2(a,1),b))
183
184#define sisd_to_int(x) _mm_cvtsi128_si32(x)
185
186#define sisd_from_int(n) _mm_cvtsi32_si128(n)
187
188char mask_x55 [16] __attribute__ ((aligned(16))) = 
189    {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
190char mask_x33 [16] __attribute__ ((aligned(16))) = 
191    {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33};
192char mask_x0F [16] __attribute__ ((aligned(16))) = 
193    {0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F};
194
195static inline int simd_all_true_8(SIMD_type v) {
196  return _mm_movemask_epi8(v) == 0xFFFF;
197}
198
199static inline int simd_any_true_8(SIMD_type v) {
200  return _mm_movemask_epi8(v) != 0;
201}
202
203static inline int simd_any_sign_bit_8(SIMD_type v) {
204  return _mm_movemask_epi8(v) != 0;
205}
206
207#define simd_all_eq_8(v1, v2) simd_all_true_8(_mm_cmpeq_epi8(v1, v2))
208#define simd_all_le_8(v1, v2) \
209  simd_all_eq_8(simd_max_8(v1, v2), v2)
210
211#define simd_all_signed_gt_8(v1, v2) simd_all_true_8(_mm_cmpgt_epi8(v1, v2))
212
213static inline int bitblock_has_bit(SIMD_type v) {
214  return !simd_all_true_8(simd_eq_8(v, simd_const_8(0)));
215}
216
217
218
219#define bitblock_test_bit(blk, n) \
220   sisd_to_int(sisd_srli(sisd_slli(blk, ((BLOCKSIZE-1)-(n))), BLOCKSIZE-1))
221
222#define simd_pack_2(a,b)\
223        simd_pack_4(simd_if(simd_himask_2,sisd_srli(a,1),a),\
224        simd_if(simd_himask_2,sisd_srli(b,1),b))
225#define simd_pack_4(a,b)\
226        simd_pack_8(simd_if(simd_himask_4,sisd_srli(a,2),a),\
227        simd_if(simd_himask_4,sisd_srli(b,2),b))
228#define simd_pack_8(a,b)\
229        simd_pack_16(simd_if(simd_himask_8,sisd_srli(a,4),a),\
230        simd_if(simd_himask_8,sisd_srli(b,4),b))
231
232#ifndef simd_add_2_xx
233#define simd_add_2_xx(v1, v2) simd_add_2(v1, v2)
234#endif
235
236#ifndef simd_add_2_xl
237#define simd_add_2_xl(v1, v2) simd_add_2(v1, simd_andc(v2, simd_himask_2))
238#endif
239
240#ifndef simd_add_2_xh
241#define simd_add_2_xh(v1, v2) simd_add_2(v1, simd_srli_2(v2, 1))
242#endif
243
244#ifndef simd_add_2_lx
245#define simd_add_2_lx(v1, v2) simd_add_2(simd_andc(v1, simd_himask_2), v2)
246#endif
247
248#ifndef simd_add_2_ll
249#define simd_add_2_ll(v1, v2) simd_add_8(simd_andc(v1, simd_himask_2), simd_andc(v2, simd_himask_2))
250#endif
251
252#ifndef simd_add_2_lh
253#define simd_add_2_lh(v1, v2) simd_add_8(simd_andc(v1, simd_himask_2), simd_srli_2(v2, 1))
254#endif
255
256#ifndef simd_add_2_hx
257#define simd_add_2_hx(v1, v2) simd_add_2(simd_srli_2(v1, 1), v2)
258#endif
259
260#ifndef simd_add_2_hl
261#define simd_add_2_hl(v1, v2) simd_add_8(simd_srli_2(v1, 1), simd_andc(v2, simd_himask_2))
262#endif
263
264#ifndef simd_add_2_hh
265#define simd_add_2_hh(v1, v2) simd_add_8(simd_srli_2(v1, 1), simd_srli_2(v2, 1))
266#endif
267
268#ifndef simd_add_4_xx
269#define simd_add_4_xx(v1, v2) simd_add_4(v1, v2)
270#endif
271
272#ifndef simd_add_4_xl
273#define simd_add_4_xl(v1, v2) simd_add_4(v1, simd_andc(v2, simd_himask_4))
274#endif
275
276#ifndef simd_add_4_xh
277#define simd_add_4_xh(v1, v2) simd_add_4(v1, simd_srli_4(v2, 2))
278#endif
279
280#ifndef simd_add_4_lx
281#define simd_add_4_lx(v1, v2) simd_add_4(simd_andc(v1, simd_himask_4), v2)
282#endif
283
284#ifndef simd_add_4_ll
285#define simd_add_4_ll(v1, v2) simd_add_8(simd_andc(v1, simd_himask_4), simd_andc(v2, simd_himask_4))
286#endif
287
288#ifndef simd_add_4_lh
289#define simd_add_4_lh(v1, v2) simd_add_8(simd_andc(v1, simd_himask_4), simd_srli_4(v2, 2))
290#endif
291
292#ifndef simd_add_4_hx
293#define simd_add_4_hx(v1, v2) simd_add_4(simd_srli_4(v1, 2), v2)
294#endif
295
296#ifndef simd_add_4_hl
297#define simd_add_4_hl(v1, v2) simd_add_8(simd_srli_4(v1, 2), simd_andc(v2, simd_himask_4))
298#endif
299
300#ifndef simd_add_4_hh
301#define simd_add_4_hh(v1, v2) simd_add_8(simd_srli_4(v1, 2), simd_srli_4(v2, 2))
302#endif
303
304#ifndef simd_add_8_xx
305#define simd_add_8_xx(v1, v2) simd_add_8(v1, v2)
306#endif
307
308#ifndef simd_add_8_xl
309#define simd_add_8_xl(v1, v2) simd_add_8(v1, simd_andc(v2, simd_himask_8))
310#endif
311
312#ifndef simd_add_8_xh
313#define simd_add_8_xh(v1, v2) simd_add_8(v1, simd_srli_8(v2, 4))
314#endif
315
316#ifndef simd_add_8_lx
317#define simd_add_8_lx(v1, v2) simd_add_8(simd_andc(v1, simd_himask_8), v2)
318#endif
319
320#ifndef simd_add_8_ll
321#define simd_add_8_ll(v1, v2) simd_add_8(simd_andc(v1, simd_himask_8), simd_andc(v2, simd_himask_8))
322#endif
323
324#ifndef simd_add_8_lh
325#define simd_add_8_lh(v1, v2) simd_add_8(simd_andc(v1, simd_himask_8), simd_srli_8(v2, 4))
326#endif
327
328#ifndef simd_add_8_hx
329#define simd_add_8_hx(v1, v2) simd_add_8(simd_srli_8(v1, 4), v2)
330#endif
331
332#ifndef simd_add_8_hl
333#define simd_add_8_hl(v1, v2) simd_add_8(simd_srli_8(v1, 4), simd_andc(v2, simd_himask_8))
334#endif
335
336#ifndef simd_add_8_hh
337#define simd_add_8_hh(v1, v2) simd_add_8(simd_srli_8(v1, 4), simd_srli_8(v2, 4))
338#endif
339
340#ifndef simd_add_16_xx
341#define simd_add_16_xx(v1, v2) simd_add_16(v1, v2)
342#endif
343
344#ifndef simd_add_16_xl
345#define simd_add_16_xl(v1, v2) simd_add_16(v1, simd_andc(v2, simd_himask_16))
346#endif
347
348#ifndef simd_add_16_xh
349#define simd_add_16_xh(v1, v2) simd_add_16(v1, simd_srli_16(v2, 8))
350#endif
351
352#ifndef simd_add_16_lx
353#define simd_add_16_lx(v1, v2) simd_add_16(simd_andc(v1, simd_himask_16), v2)
354#endif
355
356#ifndef simd_add_16_ll
357#define simd_add_16_ll(v1, v2) simd_add_16(simd_andc(v1, simd_himask_16), simd_andc(v2, simd_himask_16))
358#endif
359
360#ifndef simd_add_16_lh
361#define simd_add_16_lh(v1, v2) simd_add_16(simd_andc(v1, simd_himask_16), simd_srli_16(v2, 8))
362#endif
363
364#ifndef simd_add_16_hx
365#define simd_add_16_hx(v1, v2) simd_add_16(simd_srli_16(v1, 8), v2)
366#endif
367
368#ifndef simd_add_16_hl
369#define simd_add_16_hl(v1, v2) simd_add_16(simd_srli_16(v1, 8), simd_andc(v2, simd_himask_16))
370#endif
371
372#ifndef simd_add_16_hh
373#define simd_add_16_hh(v1, v2) simd_add_16(simd_srli_16(v1, 8), simd_srli_16(v2, 8))
374#endif
375
376#ifndef simd_add_32_xx
377#define simd_add_32_xx(v1, v2) simd_add_32(v1, v2)
378#endif
379
380#ifndef simd_add_32_xl
381#define simd_add_32_xl(v1, v2) simd_add_32(v1, simd_andc(v2, simd_himask_32))
382#endif
383
384#ifndef simd_add_32_xh
385#define simd_add_32_xh(v1, v2) simd_add_32(v1, simd_srli_32(v2, 16))
386#endif
387
388#ifndef simd_add_32_lx
389#define simd_add_32_lx(v1, v2) simd_add_32(simd_andc(v1, simd_himask_32), v2)
390#endif
391
392#ifndef simd_add_32_ll
393#define simd_add_32_ll(v1, v2) simd_add_32(simd_andc(v1, simd_himask_32), simd_andc(v2, simd_himask_32))
394#endif
395
396#ifndef simd_add_32_lh
397#define simd_add_32_lh(v1, v2) simd_add_32(simd_andc(v1, simd_himask_32), simd_srli_32(v2, 16))
398#endif
399
400#ifndef simd_add_32_hx
401#define simd_add_32_hx(v1, v2) simd_add_32(simd_srli_32(v1, 16), v2)
402#endif
403
404#ifndef simd_add_32_hl
405#define simd_add_32_hl(v1, v2) simd_add_32(simd_srli_32(v1, 16), simd_andc(v2, simd_himask_32))
406#endif
407
408#ifndef simd_add_32_hh
409#define simd_add_32_hh(v1, v2) simd_add_32(simd_srli_32(v1, 16), simd_srli_32(v2, 16))
410#endif
411
412#ifndef simd_add_64_xx
413#define simd_add_64_xx(v1, v2) simd_add_64(v1, v2)
414#endif
415
416#ifndef simd_add_64_xl
417#define simd_add_64_xl(v1, v2) simd_add_64(v1, simd_andc(v2, simd_himask_64))
418#endif
419
420#ifndef simd_add_64_xh
421#define simd_add_64_xh(v1, v2) simd_add_64(v1, simd_srli_64(v2, 32))
422#endif
423
424#ifndef simd_add_64_lx
425#define simd_add_64_lx(v1, v2) simd_add_64(simd_andc(v1, simd_himask_64), v2)
426#endif
427
428#ifndef simd_add_64_ll
429#define simd_add_64_ll(v1, v2) simd_add_64(simd_andc(v1, simd_himask_64), simd_andc(v2, simd_himask_64))
430#endif
431
432#ifndef simd_add_64_lh
433#define simd_add_64_lh(v1, v2) simd_add_64(simd_andc(v1, simd_himask_64), simd_srli_64(v2, 32))
434#endif
435
436#ifndef simd_add_64_hx
437#define simd_add_64_hx(v1, v2) simd_add_64(simd_srli_64(v1, 32), v2)
438#endif
439
440#ifndef simd_add_64_hl
441#define simd_add_64_hl(v1, v2) simd_add_64(simd_srli_64(v1, 32), simd_andc(v2, simd_himask_64))
442#endif
443
444#ifndef simd_add_64_hh
445#define simd_add_64_hh(v1, v2) simd_add_64(simd_srli_64(v1, 32), simd_srli_64(v2, 32))
446#endif
447
448#ifndef simd_add_128_xx
449#define simd_add_128_xx(v1, v2) simd_add_128(v1, v2)
450#endif
451
452#ifndef simd_add_128_xl
453#define simd_add_128_xl(v1, v2) simd_add_128(v1, simd_andc(v2, simd_himask_128))
454#endif
455
456#ifndef simd_add_128_xh
457#define simd_add_128_xh(v1, v2) simd_add_128(v1, simd_srli_128(v2, 64))
458#endif
459
460#ifndef simd_add_128_lx
461#define simd_add_128_lx(v1, v2) simd_add_128(simd_andc(v1, simd_himask_128), v2)
462#endif
463
464#ifndef simd_add_128_ll
465#define simd_add_128_ll(v1, v2) simd_add_128(simd_andc(v1, simd_himask_128), simd_andc(v2, simd_himask_128))
466#endif
467
468#ifndef simd_add_128_lh
469#define simd_add_128_lh(v1, v2) simd_add_128(simd_andc(v1, simd_himask_128), simd_srli_128(v2, 64))
470#endif
471
472#ifndef simd_add_128_hx
473#define simd_add_128_hx(v1, v2) simd_add_128(simd_srli_128(v1, 64), v2)
474#endif
475
476#ifndef simd_add_128_hl
477#define simd_add_128_hl(v1, v2) simd_add_128(simd_srli_128(v1, 64), simd_andc(v2, simd_himask_128))
478#endif
479
480#ifndef simd_add_128_hh
481#define simd_add_128_hh(v1, v2) simd_add_128(simd_srli_128(v1, 64), simd_srli_128(v2, 64))
482#endif
483
484#ifndef simd_pack_2_xx
485#define simd_pack_2_xx(v1, v2) simd_pack_2(v1, v2)
486#endif
487
488#ifndef simd_pack_2_xl
489#define simd_pack_2_xl(v1, v2) simd_pack_2(v1, v2)
490#endif
491
492#ifndef simd_pack_2_xh
493#define simd_pack_2_xh(v1, v2) simd_pack_2(v1, simd_srli_16(v2, 1))
494#endif
495
496#ifndef simd_pack_2_lx
497#define simd_pack_2_lx(v1, v2) simd_pack_2(v1, v2)
498#endif
499
500#ifndef simd_pack_2_ll
501#define simd_pack_2_ll(v1, v2) simd_pack_2(v1, v2)
502#endif
503
504#ifndef simd_pack_2_lh
505#define simd_pack_2_lh(v1, v2) simd_pack_2(v1, simd_srli_16(v2, 1))
506#endif
507
508#ifndef simd_pack_2_hx
509#define simd_pack_2_hx(v1, v2) simd_pack_2(simd_srli_16(v1, 1), v2)
510#endif
511
512#ifndef simd_pack_2_hl
513#define simd_pack_2_hl(v1, v2) simd_pack_2(simd_srli_16(v1, 1), v2)
514#endif
515
516#ifndef simd_pack_2_hh
517#define simd_pack_2_hh(v1, v2) simd_pack_2(simd_srli_16(v1, 1), simd_srli_16(v2, 1))
518#endif
519
520#ifndef simd_pack_4_xx
521#define simd_pack_4_xx(v1, v2) simd_pack_4(v1, v2)
522#endif
523
524#ifndef simd_pack_4_xl
525#define simd_pack_4_xl(v1, v2) simd_pack_4(v1, v2)
526#endif
527
528#ifndef simd_pack_4_xh
529#define simd_pack_4_xh(v1, v2) simd_pack_4(v1, simd_srli_16(v2, 2))
530#endif
531
532#ifndef simd_pack_4_lx
533#define simd_pack_4_lx(v1, v2) simd_pack_4(v1, v2)
534#endif
535
536#ifndef simd_pack_4_ll
537#define simd_pack_4_ll(v1, v2) simd_pack_4(v1, v2)
538#endif
539
540#ifndef simd_pack_4_lh
541#define simd_pack_4_lh(v1, v2) simd_pack_4(v1, simd_srli_16(v2, 2))
542#endif
543
544#ifndef simd_pack_4_hx
545#define simd_pack_4_hx(v1, v2) simd_pack_4(simd_srli_16(v1, 2), v2)
546#endif
547
548#ifndef simd_pack_4_hl
549#define simd_pack_4_hl(v1, v2) simd_pack_4(simd_srli_16(v1, 2), v2)
550#endif
551
552#ifndef simd_pack_4_hh
553#define simd_pack_4_hh(v1, v2) simd_pack_4(simd_srli_16(v1, 2), simd_srli_16(v2, 2))
554#endif
555
556#ifndef simd_pack_8_xx
557#define simd_pack_8_xx(v1, v2) simd_pack_8(v1, v2)
558#endif
559
560#ifndef simd_pack_8_xl
561#define simd_pack_8_xl(v1, v2) simd_pack_8(v1, v2)
562#endif
563
564#ifndef simd_pack_8_xh
565#define simd_pack_8_xh(v1, v2) simd_pack_8(v1, simd_srli_16(v2, 4))
566#endif
567
568#ifndef simd_pack_8_lx
569#define simd_pack_8_lx(v1, v2) simd_pack_8(v1, v2)
570#endif
571
572#ifndef simd_pack_8_ll
573#define simd_pack_8_ll(v1, v2) simd_pack_8(v1, v2)
574#endif
575
576#ifndef simd_pack_8_lh
577#define simd_pack_8_lh(v1, v2) simd_pack_8(v1, simd_srli_16(v2, 4))
578#endif
579
580#ifndef simd_pack_8_hx
581#define simd_pack_8_hx(v1, v2) simd_pack_8(simd_srli_16(v1, 4), v2)
582#endif
583
584#ifndef simd_pack_8_hl
585#define simd_pack_8_hl(v1, v2) simd_pack_8(simd_srli_16(v1, 4), v2)
586#endif
587
588#ifndef simd_pack_8_hh
589#define simd_pack_8_hh(v1, v2) simd_pack_8(simd_srli_16(v1, 4), simd_srli_16(v2, 4))
590#endif
591
592#ifndef simd_pack_16_xx
593#define simd_pack_16_xx(v1, v2) simd_pack_16(v1, v2)
594#endif
595
596#ifndef simd_pack_16_xl
597#define simd_pack_16_xl(v1, v2) simd_pack_16(v1, v2)
598#endif
599
600#ifndef simd_pack_16_xh
601#define simd_pack_16_xh(v1, v2) simd_pack_16(v1, simd_srli_16(v2, 8))
602#endif
603
604#ifndef simd_pack_16_lx
605#define simd_pack_16_lx(v1, v2) simd_pack_16(v1, v2)
606#endif
607
608#ifndef simd_pack_16_ll
609#define simd_pack_16_ll(v1, v2) simd_pack_16(v1, v2)
610#endif
611
612#ifndef simd_pack_16_lh
613#define simd_pack_16_lh(v1, v2) simd_pack_16(v1, simd_srli_16(v2, 8))
614#endif
615
616#ifndef simd_pack_16_hx
617#define simd_pack_16_hx(v1, v2) simd_pack_16(simd_srli_16(v1, 8), v2)
618#endif
619
620#ifndef simd_pack_16_hl
621#define simd_pack_16_hl(v1, v2) simd_pack_16(simd_srli_16(v1, 8), v2)
622#endif
623
624#ifndef simd_pack_16_hh
625#define simd_pack_16_hh(v1, v2) simd_pack_16(simd_srli_16(v1, 8), simd_srli_16(v2, 8))
626#endif
627
628
629// Splat the first 16-bit int into all positions.
630static inline SIMD_type simd_splat_16(SIMD_type x) {
631  SIMD_type t = _mm_shufflelo_epi16(x,0);
632  return _mm_shuffle_epi32(t,0);
633}
634
635// Splat the first 32-bit int into all positions.
636static inline SIMD_type simd_splat_32(SIMD_type x) {
637  return _mm_shuffle_epi32(x,0);
638}
639
640
641
642
643void print_bit_block(char * var_name, SIMD_type v) {
644  union {SIMD_type vec; unsigned char elems[8];} x;
645  x.vec = v;
646  unsigned char c, bit_reversed;
647  int i;
648  printf("%20s = ", var_name);
649  for (i = 0; i < sizeof(SIMD_type); i++) {
650    c = x.elems[i];
651     printf("%02X ", c); 
652  }
653  printf("\n");
654}
655
656static inline int bitblock_bit_count(SIMD_type v) {
657  int bit_count = 0;
658  SIMD_type cts_2 = simd_add_2_lh(v, v);
659  SIMD_type cts_4 = simd_add_4_lh(cts_2, cts_2);
660  SIMD_type cts_8 = simd_add_8_lh(cts_4, cts_4);
661  SIMD_type cts_64 = _mm_sad_epu8(cts_8, simd_const_8(0));
662  /* SIMD_type cts_128 = simd_add_128_lh(cts_64, cts_64) */;
663  SIMD_type cts_128 = simd_add_64(cts_64, sisd_srli(cts_64,64));
664  return (int) sisd_to_int(cts_128);
665}
666
667
668static inline int count_forward_zeroes(SIMD_type bits) {
669  union {SIMD_type vec; unsigned long elems[sizeof(SIMD_type)/LONG_BIT];} v;
670  v.vec = bits;
671  if (v.elems[0] != 0) return __builtin_ctzl(v.elems[0]);
672  else if (v.elems[1] != 0) return LONG_BIT + __builtin_ctzl(v.elems[1]);
673#if LONG_BIT < 64
674  else if (v.elems[2] != 0) return 2*LONG_BIT + __builtin_ctzl(v.elems[2]);
675  else if (v.elems[3] != 0) return 3*LONG_BIT + __builtin_ctzl(v.elems[3]);
676#endif
677  else return 8*sizeof(SIMD_type);
678}
679
680#endif
Note: See TracBrowser for help on using the repository browser.