source: trunk/lib/sse_simd.h @ 42

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

lib_simd: refactored allocation, bitstream_scan

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