Debian Codegraph @

easel/esl_sse.h

raw symbols SCIP
1 /* Vectorized routines for x86 Streaming SIMD Extensions (SSE).
2 *
3 * This header file, unusually, provides many complete function
4 * implementations so they can be inlined by the compiler.
5 *
6 * esl_sse supports both our SSE implementations and our SSE4
7 * implementations. In a plain "SSE" (i.e. SSE/SSE2, e.g. HMMER3),
8 * SSE4.1-dependent code is ifdef'ed out.
9 *
10 * Contents:
11 * 1. Function declarations for esl_sse.c
12 * 2. Inlined functions: horizontal max, min, sum
13 * 3. Inlined functions: left, right shift
14 * 4. Inlined functions: any_gt
15 * 5. Inlined functions: select
16 */
17 #ifndef eslSSE_INCLUDED
18 #define eslSSE_INCLUDED
19 #include <esl_config.h>
20 #if defined(eslENABLE_SSE) || defined(eslENABLE_SSE4)
21
22 #include "easel.h"
23
24 #include <stdio.h>
25 #include <x86intrin.h>
26
27
28 /*****************************************************************
29 * 1. Function declarations (from esl_sse.c)
30 *****************************************************************/
31 extern __m128 esl_sse_logf(__m128 x);
32 extern __m128 esl_sse_expf(__m128 x);
33 extern void esl_sse_dump_ps(FILE *fp, __m128 v);
34
35
36
37
38 /*****************************************************************
39 * 2. Inlined functions: horizontal max, min
40 *****************************************************************/
41
42 /* Function: esl_sse_hmax_epu8()
43 * Synopsis: Return max of 16 uint8_t elements in epu8 vector.
44 */
45 static inline uint8_t
46 esl_sse_hmax_epu8(__m128i a)
47 {
48 a = _mm_max_epu8(a, _mm_srli_si128(a, 8));
49 a = _mm_max_epu8(a, _mm_srli_si128(a, 4));
50 a = _mm_max_epu8(a, _mm_srli_si128(a, 2));
51 a = _mm_max_epu8(a, _mm_srli_si128(a, 1));
52 return (uint8_t) _mm_extract_epi16(a, 0); /* only low-order 8 bits set; so _epi16 or _epi8 equiv; _epi8 is SSE4.1 */
53 }
54
55 #ifdef eslENABLE_SSE4
56 /* Function: esl_sse_hmax_epi8()
57 * Synopsis: Return max of 16 int8_t elements in epi8 vector.
58 * (SSE4.1)
59 */
60 static inline int8_t
61 esl_sse_hmax_epi8(__m128i a)
62 {
63 a = _mm_max_epi8(a, _mm_shuffle_epi32 (a, _MM_SHUFFLE(2,3,0,1))); // _MM_SHUFFLE() args are reversed._MM_SHUFFLE(3,2,1,0) is a no-op, for example.
64 a = _mm_max_epi8(a, _mm_shuffle_epi32 (a, _MM_SHUFFLE(0,1,2,3)));
65 a = _mm_max_epi8(a, _mm_shufflelo_epi16(a, _MM_SHUFFLE(2,3,0,1)));
66 a = _mm_max_epi8(a, _mm_srli_epi16 (a, 8));
67 return (int8_t) _mm_cvtsi128_si32(a);
68 }
69 #endif
70
71 /* Function: esl_sse_hmax_epi16()
72 * Synopsis: Return max of 16 int16_t elements in epi16 vector.
73 */
74 static inline int16_t
75 esl_sse_hmax_epi16(__m128i a)
76 {
77 a = _mm_max_epi16(a, _mm_shuffle_epi32 (a, _MM_SHUFFLE(1,0,3,2)));
78 a = _mm_max_epi16(a, _mm_shufflelo_epi16(a, _MM_SHUFFLE(1,0,3,2)));
79 a = _mm_max_epi16(a, _mm_srli_epi32(a, 16));
80 return (int16_t) _mm_cvtsi128_si32(a);
81 }
82
83
84 /* Function: esl_sse_hmax_ps()
85 * Synopsis: Find the maximum of elements in a vector.
86 *
87 * Purpose: Find the maximum valued element in the four float elements
88 * in <a>, and return that maximum value in <*ret_max>.
89 *
90 * Xref: J3/90 for benchmarking of some alternative implementations.
91 */
92 static inline void
93 esl_sse_hmax_ps(__m128 a, float *ret_max)
94 {
95 a = _mm_max_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 3, 2, 1)));
96 a = _mm_max_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 0, 3, 2)));
97 _mm_store_ss(ret_max, a);
98 }
99
100 /* Function: esl_sse_hmin_ps()
101 * Synopsis: Find the minimum of elements in a vector.
102 *
103 * Purpose: Find the minimum valued element in the four float elements
104 * in <a> and return that minimum value in <*ret_min>.
105 */
106 static inline void
107 esl_sse_hmin_ps(__m128 a, float *ret_min)
108 {
109 a = _mm_min_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 3, 2, 1)));
110 a = _mm_min_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 0, 3, 2)));
111 _mm_store_ss(ret_min, a);
112 }
113
114 /* Function: esl_sse_hsum_ps()
115 * Synopsis: Takes the horizontal sum of elements in a vector.
116 *
117 * Purpose: Add the four float elements in vector <a>; return
118 * that sum in <*ret_sum>.
119 */
120 static inline void
121 esl_sse_hsum_ps(__m128 a, float *ret_sum)
122 {
123 a = _mm_add_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(0, 3, 2, 1)));
124 a = _mm_add_ps(a, _mm_shuffle_ps(a, a, _MM_SHUFFLE(1, 0, 3, 2)));
125 _mm_store_ss(ret_sum, a);
126 }
127
128
129
130
131 /*****************************************************************
132 * 3. Inlined functions: left, right shift
133 *****************************************************************/
134
135 /* Function: esl_sse_rightshift_int8()
136 * Synopsis: Shift int8 vector elements to the right, shifting -inf on
137 * Incept: SRE, Sun Jun 4 09:52:45 2017
138 *
139 * Purpose: Given an int8 vector <{ a0 .. a16}>, and a mask <{-inf,
140 * 0*15 }> with the desired value of -inf in slot 0
141 * (and zeros elsewhere), return <{ -inf, a0..a14 }>;
142 * i.e. shift the values in <a> to the right, while
143 * shifting $-\infty$ on.
144 *
145 * By our convention, "right" and "left" refer to memory
146 * order (low addresses on the left). On a little-endian
147 * (x86) architecture, this is a left shift in the hardware
148 * register.
149 *
150 * This can be used both for signed (epi8) and unsigned
151 * (epu8) int8 vectors.
152 *
153 * Xref: HMMER's simdvec.md: on our left/right convention.
154 */
155 static inline __m128i
156 esl_sse_rightshift_int8(__m128i a, __m128i neginfmask)
157 {
158 return _mm_or_si128(_mm_slli_si128(a, 1), neginfmask);
159 }
160
161
162 /* Function: esl_sse_rightshift_int16()
163 * Synopsis: Shift int16 vector elements to the right, shifting -inf on
164 * Incept: SRE, Sun Jun 4 10:12:24 2017 [Gary Jules, Mad World]
165 *
166 * Purpose: Same as <esl_sse_rightshift_int8()> but for int16.
167 */
168 static inline __m128i
169 esl_sse_rightshift_int16(__m128i a, __m128i neginfmask)
170 {
171 return _mm_or_si128( _mm_slli_si128(a, 2), neginfmask);
172 }
173
174 /* Function: esl_sse_rightshiftz_float()
175 * Synopsis: Shift float vector elements to the right, shifting zero on.
176 *
177 * Purpose: Same as <esl_sse_rightshift_int8()> but for floats,
178 * and the value that is shifted on is a zero.
179 */
180 static inline __m128
181 esl_sse_rightshiftz_float(__m128 a)
182 {
183 // Tricky. IEEE754 representation of zero is all 0 bits, so shift alone suffices.
184 return (__m128) _mm_slli_si128( (__m128i) a, 4);
185 }
186
187 /* Function: esl_sse_leftshiftz_float()
188 * Synopsis: Shift float vector elements to the left, shifting zero on.
189 *
190 * Purpose: Same as <esl_sse_rightshift_float()> but leftwise: <[ a0 a1 a2
191 * a3 ]> becomes <[ a1 a2 a3 0 ]>. Used in Backwards.
192 */
193 static inline __m128
194 esl_sse_leftshiftz_float(__m128 a)
195 {
196 // Same trick. IEEE754 representation of zero is all 0 bits.
197 return (__m128) _mm_srli_si128( (__m128i) a, 4);
198 }
199
200 /* Function: esl_sse_rightshift_ps()
201 * Synopsis: Shift vector elements to the right, shifting -inf on.
202 *
203 * Purpose: Returns a vector containing
204 * <{ b[0] a[0] a[1] a[2] }>:
205 * i.e. shift the values in <a> to the
206 * right, and load the first value of
207 * <b> into the first slot.
208 *
209 * Note: used in Infernal.
210 */
211 static inline __m128
212 esl_sse_rightshift_ps(__m128 a, __m128 b)
213 {
214 return _mm_move_ss(_mm_shuffle_ps(a, a, _MM_SHUFFLE(2, 1, 0, 0)), b);
215 }
216
217 /* Function: esl_sse_leftshift_ps()
218 * Synopsis: Shift vector elements to the left, shifting -inf on.
219 *
220 * Purpose: Returns a vector containing
221 * <{ a[1] a[2] a[3] b[0]}>:
222 * i.e. shift the values in <a> to the
223 * left and load the first value of
224 * <b> into the first slot.
225 *
226 * Note: used in Infernal.
227 */
228 static inline __m128
229 esl_sse_leftshift_ps(__m128 a, __m128 b)
230 {
231 register __m128 v = _mm_move_ss(a, b); /* now b[0] a[1] a[2] a[3] */
232 return _mm_shuffle_ps(v, v, _MM_SHUFFLE(0, 3, 2, 1)); /* now a[1] a[2] a[3] b[0] */
233 }
234
235
236
237 /*****************************************************************
238 * 3. Inlined functions: any_gt
239 *****************************************************************/
240
241 /* Function: esl_sse_any_gt_epu8()
242 * Synopsis: Returns TRUE if any a[z] > b[z].
243 *
244 * Purpose: Return TRUE if any <a[z] > b[z]> for <z=0..15>
245 * in two <epu8> vectors of unsigned chars.
246 *
247 * We need this incantation because SSE provides
248 * no <cmpgt_epu8> instruction.
249 *
250 * For equality tests, note that <cmpeq_epi8> works fine
251 * for unsigned ints though there is no <cmpeq_epu8>
252 * instruction either).
253 *
254 * See vec_any_gt
255 */
256 static inline int
257 esl_sse_any_gt_epu8(__m128i a, __m128i b)
258 {
259 __m128i mask = _mm_cmpeq_epi8(_mm_max_epu8(a,b), b); /* anywhere a>b, mask[z] = 0x0; elsewhere 0xff */
260 int maskbits = _mm_movemask_epi8(_mm_xor_si128(mask, _mm_cmpeq_epi8(mask, mask))); /* the xor incantation is a bitwise inversion */
261 return maskbits != 0;
262 }
263
264 /* Function: esl_sse_any_gt_epi16()
265 * Synopsis: Return >0 if any a[z] > b[z]
266 */
267 static inline int
268 esl_sse_any_gt_epi16(__m128i a, __m128i b)
269 {
270 return (_mm_movemask_epi8(_mm_cmpgt_epi16(a,b)) != 0);
271 }
272
273 /* Function: esl_sse_any_gt_ps()
274 * Synopsis: Returns TRUE if any a[z] > b[z]
275 *
276 * Xref: From Apple Altivec/SSE migration guide.
277 */
278 static inline int
279 esl_sse_any_gt_ps(__m128 a, __m128 b)
280 {
281 __m128 mask = _mm_cmpgt_ps(a,b);
282 int maskbits = _mm_movemask_ps( mask );
283 return maskbits != 0;
284 }
285
286
287
288 /*****************************************************************
289 * 5. Inlined functions: select
290 *****************************************************************/
291
292 /* Function: esl_sse_select_ps()
293 * Synopsis: SSE equivalent of <vec_sel()>
294 *
295 * Purpose: Vector select. Returns a vector <r[z] = a[z]> where <mask[z]>
296 * is all 0's; <r[z] = b[z]> where <mask[z]> is all 1's.
297 *
298 * Useful for avoiding conditional branches. For example,
299 * to implement \ccode{if (a > 0) a += a;}:
300 *
301 * \begin{cchunk}
302 * mask = _mm_cmpgt_ps(a, _mm_setzero_ps());
303 * twoa = _mm_add_ps(a, a);
304 * a = esl_sse_select_ps(a, twoa, mask);
305 * \end{cchunk}
306 *
307 * Notes: As recommended by the Altivec/SSE Migration Guide,
308 * Apple Computer, Inc.
309 */
310 static inline __m128
311 esl_sse_select_ps(__m128 a, __m128 b, __m128 mask)
312 {
313 #ifdef eslENABLE_SSE4
314 return _mm_blendv_ps(a, b, mask);
315 #else
316 b = _mm_and_ps(b, mask);
317 a = _mm_andnot_ps(mask, a);
318 return _mm_or_ps(a,b);
319 #endif
320 }
321
322
323 #endif /*eslENABLE_SSE || eslENABLE_SSE4 */
324 #endif /*eslSSE_INCLUDED*/
325