Debian Codegraph @

src/sw_diag_neon_128_16.c

raw symbols SCIP
1 /**
2 * @file
3 *
4 * @author [email protected]
5 *
6 * Copyright (c) 2015 Battelle Memorial Institute.
7 */
8 #include "config.h"
9
10 #include <stdlib.h>
11
12
13
14 #include "parasail.h"
15 #include "parasail/memory.h"
16 #include "parasail/internal_neon.h"
17
18
19
20 #ifdef PARASAIL_TABLE
21 static inline void arr_store_si128(
22 int *array,
23 simde__m128i vWH,
24 int32_t i,
25 int32_t s1Len,
26 int32_t j,
27 int32_t s2Len)
28 {
29 if (0 <= i+0 && i+0 < s1Len && 0 <= j-0 && j-0 < s2Len) {
30 array[1LL*(i+0)*s2Len + (j-0)] = (int16_t)simde_mm_extract_epi16(vWH, 7);
31 }
32 if (0 <= i+1 && i+1 < s1Len && 0 <= j-1 && j-1 < s2Len) {
33 array[1LL*(i+1)*s2Len + (j-1)] = (int16_t)simde_mm_extract_epi16(vWH, 6);
34 }
35 if (0 <= i+2 && i+2 < s1Len && 0 <= j-2 && j-2 < s2Len) {
36 array[1LL*(i+2)*s2Len + (j-2)] = (int16_t)simde_mm_extract_epi16(vWH, 5);
37 }
38 if (0 <= i+3 && i+3 < s1Len && 0 <= j-3 && j-3 < s2Len) {
39 array[1LL*(i+3)*s2Len + (j-3)] = (int16_t)simde_mm_extract_epi16(vWH, 4);
40 }
41 if (0 <= i+4 && i+4 < s1Len && 0 <= j-4 && j-4 < s2Len) {
42 array[1LL*(i+4)*s2Len + (j-4)] = (int16_t)simde_mm_extract_epi16(vWH, 3);
43 }
44 if (0 <= i+5 && i+5 < s1Len && 0 <= j-5 && j-5 < s2Len) {
45 array[1LL*(i+5)*s2Len + (j-5)] = (int16_t)simde_mm_extract_epi16(vWH, 2);
46 }
47 if (0 <= i+6 && i+6 < s1Len && 0 <= j-6 && j-6 < s2Len) {
48 array[1LL*(i+6)*s2Len + (j-6)] = (int16_t)simde_mm_extract_epi16(vWH, 1);
49 }
50 if (0 <= i+7 && i+7 < s1Len && 0 <= j-7 && j-7 < s2Len) {
51 array[1LL*(i+7)*s2Len + (j-7)] = (int16_t)simde_mm_extract_epi16(vWH, 0);
52 }
53 }
54 #endif
55
56 #ifdef PARASAIL_ROWCOL
57 static inline void arr_store_rowcol(
58 int *row,
59 int *col,
60 simde__m128i vWH,
61 int32_t i,
62 int32_t s1Len,
63 int32_t j,
64 int32_t s2Len)
65 {
66 if (i+0 == s1Len-1 && 0 <= j-0 && j-0 < s2Len) {
67 row[j-0] = (int16_t)simde_mm_extract_epi16(vWH, 7);
68 }
69 if (j-0 == s2Len-1 && 0 <= i+0 && i+0 < s1Len) {
70 col[(i+0)] = (int16_t)simde_mm_extract_epi16(vWH, 7);
71 }
72 if (i+1 == s1Len-1 && 0 <= j-1 && j-1 < s2Len) {
73 row[j-1] = (int16_t)simde_mm_extract_epi16(vWH, 6);
74 }
75 if (j-1 == s2Len-1 && 0 <= i+1 && i+1 < s1Len) {
76 col[(i+1)] = (int16_t)simde_mm_extract_epi16(vWH, 6);
77 }
78 if (i+2 == s1Len-1 && 0 <= j-2 && j-2 < s2Len) {
79 row[j-2] = (int16_t)simde_mm_extract_epi16(vWH, 5);
80 }
81 if (j-2 == s2Len-1 && 0 <= i+2 && i+2 < s1Len) {
82 col[(i+2)] = (int16_t)simde_mm_extract_epi16(vWH, 5);
83 }
84 if (i+3 == s1Len-1 && 0 <= j-3 && j-3 < s2Len) {
85 row[j-3] = (int16_t)simde_mm_extract_epi16(vWH, 4);
86 }
87 if (j-3 == s2Len-1 && 0 <= i+3 && i+3 < s1Len) {
88 col[(i+3)] = (int16_t)simde_mm_extract_epi16(vWH, 4);
89 }
90 if (i+4 == s1Len-1 && 0 <= j-4 && j-4 < s2Len) {
91 row[j-4] = (int16_t)simde_mm_extract_epi16(vWH, 3);
92 }
93 if (j-4 == s2Len-1 && 0 <= i+4 && i+4 < s1Len) {
94 col[(i+4)] = (int16_t)simde_mm_extract_epi16(vWH, 3);
95 }
96 if (i+5 == s1Len-1 && 0 <= j-5 && j-5 < s2Len) {
97 row[j-5] = (int16_t)simde_mm_extract_epi16(vWH, 2);
98 }
99 if (j-5 == s2Len-1 && 0 <= i+5 && i+5 < s1Len) {
100 col[(i+5)] = (int16_t)simde_mm_extract_epi16(vWH, 2);
101 }
102 if (i+6 == s1Len-1 && 0 <= j-6 && j-6 < s2Len) {
103 row[j-6] = (int16_t)simde_mm_extract_epi16(vWH, 1);
104 }
105 if (j-6 == s2Len-1 && 0 <= i+6 && i+6 < s1Len) {
106 col[(i+6)] = (int16_t)simde_mm_extract_epi16(vWH, 1);
107 }
108 if (i+7 == s1Len-1 && 0 <= j-7 && j-7 < s2Len) {
109 row[j-7] = (int16_t)simde_mm_extract_epi16(vWH, 0);
110 }
111 if (j-7 == s2Len-1 && 0 <= i+7 && i+7 < s1Len) {
112 col[(i+7)] = (int16_t)simde_mm_extract_epi16(vWH, 0);
113 }
114 }
115 #endif
116
117 #ifdef PARASAIL_TABLE
118 #define FNAME parasail_sw_table_diag_neon_128_16
119 #else
120 #ifdef PARASAIL_ROWCOL
121 #define FNAME parasail_sw_rowcol_diag_neon_128_16
122 #else
123 #define FNAME parasail_sw_diag_neon_128_16
124 #endif
125 #endif
126
127 parasail_result_t* FNAME(
128 const char * const restrict _s1, const int _s1Len,
129 const char * const restrict _s2, const int s2Len,
130 const int open, const int gap, const parasail_matrix_t *matrix)
131 {
132 /* declare local variables */
133 int32_t N = 0;
134 int32_t PAD = 0;
135 int32_t PAD2 = 0;
136 int32_t s1Len = 0;
137 int32_t s1Len_PAD = 0;
138 int32_t s2Len_PAD = 0;
139 int16_t * restrict s1 = NULL;
140 int16_t * restrict s2B = NULL;
141 int16_t * restrict _H_pr = NULL;
142 int16_t * restrict _F_pr = NULL;
143 int16_t * restrict s2 = NULL;
144 int16_t * restrict H_pr = NULL;
145 int16_t * restrict F_pr = NULL;
146 parasail_result_t *result = NULL;
147 int32_t i = 0;
148 int32_t j = 0;
149 int32_t end_query = 0;
150 int32_t end_ref = 0;
151 int16_t NEG_LIMIT = 0;
152 int16_t POS_LIMIT = 0;
153 int16_t score = 0;
154 simde__m128i vNegLimit;
155 simde__m128i vPosLimit;
156 simde__m128i vSaturationCheckMin;
157 simde__m128i vSaturationCheckMax;
158 simde__m128i vNegInf;
159 simde__m128i vNegInf0;
160 simde__m128i vOpen;
161 simde__m128i vGap;
162 simde__m128i vZero;
163 simde__m128i vOne;
164 simde__m128i vN;
165 simde__m128i vNegOne;
166 simde__m128i vI;
167 simde__m128i vJreset;
168 simde__m128i vMaxH;
169 simde__m128i vEndI;
170 simde__m128i vEndJ;
171 simde__m128i vILimit;
172 simde__m128i vJLimit;
173
174 /* validate inputs */
175 PARASAIL_CHECK_NULL(_s2);
176 PARASAIL_CHECK_GT0(s2Len);
177 PARASAIL_CHECK_GE0(open);
178 PARASAIL_CHECK_GE0(gap);
179 PARASAIL_CHECK_NULL(matrix);
180 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
181 PARASAIL_CHECK_NULL(_s1);
182 PARASAIL_CHECK_GT0(_s1Len);
183 }
184
185 /* initialize stack variables */
186 N = 8; /* number of values in vector */
187 PAD = N-1;
188 PAD2 = PAD*2;
189 s1Len = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? _s1Len : matrix->length;
190 s1Len_PAD = s1Len+PAD;
191 s2Len_PAD = s2Len+PAD;
192 i = 0;
193 j = 0;
194 end_query = 0;
195 end_ref = 0;
196 NEG_LIMIT = (-open < matrix->min ? INT16_MIN + open : INT16_MIN - matrix->min) + 1;
197 POS_LIMIT = INT16_MAX - matrix->max - 1;
198 score = NEG_LIMIT;
199 vNegLimit = simde_mm_set1_epi16(NEG_LIMIT);
200 vPosLimit = simde_mm_set1_epi16(POS_LIMIT);
201 vSaturationCheckMin = vPosLimit;
202 vSaturationCheckMax = vNegLimit;
203 vNegInf = simde_mm_set1_epi16(NEG_LIMIT);
204 vNegInf0 = simde_mm_srli_si128(vNegInf, 2); /* shift in a 0 */
205 vOpen = simde_mm_set1_epi16(open);
206 vGap = simde_mm_set1_epi16(gap);
207 vZero = simde_mm_set1_epi16(0);
208 vOne = simde_mm_set1_epi16(1);
209 vN = simde_mm_set1_epi16(N);
210 vNegOne = simde_mm_set1_epi16(-1);
211 vI = simde_mm_set_epi16(0,1,2,3,4,5,6,7);
212 vJreset = simde_mm_set_epi16(0,-1,-2,-3,-4,-5,-6,-7);
213 vMaxH = vNegInf;
214 vEndI = vNegInf;
215 vEndJ = vNegInf;
216 vILimit = simde_mm_set1_epi16(s1Len);
217 vJLimit = simde_mm_set1_epi16(s2Len);
218
219 /* initialize result */
220 #ifdef PARASAIL_TABLE
221 result = parasail_result_new_table1(s1Len, s2Len);
222 #else
223 #ifdef PARASAIL_ROWCOL
224 result = parasail_result_new_rowcol1(s1Len, s2Len);
225 #else
226 result = parasail_result_new();
227 #endif
228 #endif
229 if (!result) return NULL;
230
231 /* set known flags */
232 result->flag |= PARASAIL_FLAG_SW | PARASAIL_FLAG_DIAG
233 | PARASAIL_FLAG_BITS_16 | PARASAIL_FLAG_LANES_8;
234 #ifdef PARASAIL_TABLE
235 result->flag |= PARASAIL_FLAG_TABLE;
236 #endif
237 #ifdef PARASAIL_ROWCOL
238 result->flag |= PARASAIL_FLAG_ROWCOL;
239 #endif
240
241 /* initialize heap variables */
242 s2B= parasail_memalign_int16_t(16, s2Len+PAD2);
243 _H_pr = parasail_memalign_int16_t(16, s2Len+PAD2);
244 _F_pr = parasail_memalign_int16_t(16, s2Len+PAD2);
245 s2 = s2B+PAD; /* will allow later for negative indices */
246 H_pr = _H_pr+PAD;
247 F_pr = _F_pr+PAD;
248
249 /* validate heap variables */
250 if (!s2B) return NULL;
251 if (!_H_pr) return NULL;
252 if (!_F_pr) return NULL;
253
254 /* convert _s1 from char to int in range 0-23 */
255 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
256 s1 = parasail_memalign_int16_t(16, s1Len+PAD);
257 if (!s1) return NULL;
258 for (i=0; i<s1Len; ++i) {
259 s1[i] = matrix->mapper[(unsigned char)_s1[i]];
260 }
261 /* pad back of s1 with dummy values */
262 for (i=s1Len; i<s1Len_PAD; ++i) {
263 s1[i] = 0; /* point to first matrix row because we don't care */
264 }
265 }
266
267 /* convert _s2 from char to int in range 0-23 */
268 for (j=0; j<s2Len; ++j) {
269 s2[j] = matrix->mapper[(unsigned char)_s2[j]];
270 }
271 /* pad front of s2 with dummy values */
272 for (j=-PAD; j<0; ++j) {
273 s2[j] = 0; /* point to first matrix row because we don't care */
274 }
275 /* pad back of s2 with dummy values */
276 for (j=s2Len; j<s2Len_PAD; ++j) {
277 s2[j] = 0; /* point to first matrix row because we don't care */
278 }
279
280 /* set initial values for stored row */
281 for (j=0; j<s2Len; ++j) {
282 H_pr[j] = 0;
283 F_pr[j] = NEG_LIMIT;
284 }
285 /* pad front of stored row values */
286 for (j=-PAD; j<0; ++j) {
287 H_pr[j] = NEG_LIMIT;
288 F_pr[j] = NEG_LIMIT;
289 }
290 /* pad back of stored row values */
291 for (j=s2Len; j<s2Len+PAD; ++j) {
292 H_pr[j] = NEG_LIMIT;
293 F_pr[j] = NEG_LIMIT;
294 }
295
296 /* iterate over query sequence */
297 for (i=0; i<s1Len; i+=N) {
298 simde__m128i vNH = vNegInf0;
299 simde__m128i vWH = vNegInf0;
300 simde__m128i vE = vNegInf;
301 simde__m128i vF = vNegInf;
302 simde__m128i vJ = vJreset;
303 const int * const restrict matrow0 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+0] : ((i+0 >= s1Len) ? s1Len-1 : i+0))];
304 const int * const restrict matrow1 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+1] : ((i+1 >= s1Len) ? s1Len-1 : i+1))];
305 const int * const restrict matrow2 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+2] : ((i+2 >= s1Len) ? s1Len-1 : i+2))];
306 const int * const restrict matrow3 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+3] : ((i+3 >= s1Len) ? s1Len-1 : i+3))];
307 const int * const restrict matrow4 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+4] : ((i+4 >= s1Len) ? s1Len-1 : i+4))];
308 const int * const restrict matrow5 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+5] : ((i+5 >= s1Len) ? s1Len-1 : i+5))];
309 const int * const restrict matrow6 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+6] : ((i+6 >= s1Len) ? s1Len-1 : i+6))];
310 const int * const restrict matrow7 = &matrix->matrix[matrix->size * ((matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) ? s1[i+7] : ((i+7 >= s1Len) ? s1Len-1 : i+7))];
311 simde__m128i vIltLimit = simde_mm_cmplt_epi16(vI, vILimit);
312 /* iterate over database sequence */
313 for (j=0; j<s2Len+PAD; ++j) {
314 simde__m128i vMat;
315 simde__m128i vNWH = vNH;
316 vNH = simde_mm_srli_si128(vWH, 2);
317 vNH = simde_mm_insert_epi16(vNH, H_pr[j], 7);
318 vF = simde_mm_srli_si128(vF, 2);
319 vF = simde_mm_insert_epi16(vF, F_pr[j], 7);
320 vF = simde_mm_max_epi16(
321 simde_mm_subs_epi16(vNH, vOpen),
322 simde_mm_subs_epi16(vF, vGap));
323 vE = simde_mm_max_epi16(
324 simde_mm_subs_epi16(vWH, vOpen),
325 simde_mm_subs_epi16(vE, vGap));
326 vMat = simde_mm_set_epi16(
327 matrow0[s2[j-0]],
328 matrow1[s2[j-1]],
329 matrow2[s2[j-2]],
330 matrow3[s2[j-3]],
331 matrow4[s2[j-4]],
332 matrow5[s2[j-5]],
333 matrow6[s2[j-6]],
334 matrow7[s2[j-7]]
335 );
336 vNWH = simde_mm_adds_epi16(vNWH, vMat);
337 vWH = simde_mm_max_epi16(vNWH, vE);
338 vWH = simde_mm_max_epi16(vWH, vF);
339 vWH = simde_mm_max_epi16(vWH, vZero);
340 /* as minor diagonal vector passes across the j=-1 boundary,
341 * assign the appropriate boundary conditions */
342 {
343 simde__m128i cond = simde_mm_cmpeq_epi16(vJ,vNegOne);
344 vWH = simde_mm_andnot_si128(cond, vWH);
345 vF = simde_mm_blendv_epi8(vF, vNegInf, cond);
346 vE = simde_mm_blendv_epi8(vE, vNegInf, cond);
347 }
348 /* cannot start checking sat until after J clears boundary */
349 if (j > PAD) {
350 vSaturationCheckMin = simde_mm_min_epi16(vSaturationCheckMin, vWH);
351 vSaturationCheckMax = simde_mm_max_epi16(vSaturationCheckMax, vWH);
352 }
353 #ifdef PARASAIL_TABLE
354 arr_store_si128(result->tables->score_table, vWH, i, s1Len, j, s2Len);
355 #endif
356 #ifdef PARASAIL_ROWCOL
357 arr_store_rowcol(result->rowcols->score_row, result->rowcols->score_col, vWH, i, s1Len, j, s2Len);
358 #endif
359 H_pr[j-7] = (int16_t)simde_mm_extract_epi16(vWH,0);
360 F_pr[j-7] = (int16_t)simde_mm_extract_epi16(vF,0);
361 /* as minor diagonal vector passes across table, extract
362 * max values within the i,j bounds */
363 {
364 simde__m128i cond_valid_J = simde_mm_and_si128(
365 simde_mm_cmpgt_epi16(vJ, vNegOne),
366 simde_mm_cmplt_epi16(vJ, vJLimit));
367 simde__m128i cond_valid_IJ = simde_mm_and_si128(cond_valid_J, vIltLimit);
368 simde__m128i cond_eq = simde_mm_cmpeq_epi16(vWH, vMaxH);
369 simde__m128i cond_max = simde_mm_cmpgt_epi16(vWH, vMaxH);
370 simde__m128i cond_all = simde_mm_and_si128(cond_max, cond_valid_IJ);
371 simde__m128i cond_Jlt = simde_mm_cmplt_epi16(vJ, vEndJ);
372 vMaxH = simde_mm_blendv_epi8(vMaxH, vWH, cond_all);
373 vEndI = simde_mm_blendv_epi8(vEndI, vI, cond_all);
374 vEndJ = simde_mm_blendv_epi8(vEndJ, vJ, cond_all);
375 cond_all = simde_mm_and_si128(cond_Jlt, cond_eq);
376 cond_all = simde_mm_and_si128(cond_all, cond_valid_IJ);
377 vEndI = simde_mm_blendv_epi8(vEndI, vI, cond_all);
378 vEndJ = simde_mm_blendv_epi8(vEndJ, vJ, cond_all);
379 }
380 vJ = simde_mm_adds_epi16(vJ, vOne);
381 }
382 vI = simde_mm_adds_epi16(vI, vN);
383 }
384
385 /* alignment ending position */
386 {
387 int16_t *t = (int16_t*)&vMaxH;
388 int16_t *i = (int16_t*)&vEndI;
389 int16_t *j = (int16_t*)&vEndJ;
390 int32_t k;
391 for (k=0; k<N; ++k, ++t, ++i, ++j) {
392 if (*t > score) {
393 score = *t;
394 end_query = *i;
395 end_ref = *j;
396 }
397 else if (*t == score) {
398 if (*j < end_ref) {
399 end_query = *i;
400 end_ref = *j;
401 }
402 else if (*j == end_ref && *i < end_query) {
403 end_query = *i;
404 end_ref = *j;
405 }
406 }
407 }
408 }
409
410 if (simde_mm_movemask_epi8(simde_mm_or_si128(
411 simde_mm_cmplt_epi16(vSaturationCheckMin, vNegLimit),
412 simde_mm_cmpgt_epi16(vSaturationCheckMax, vPosLimit)))) {
413 result->flag |= PARASAIL_FLAG_SATURATED;
414 score = 0;
415 end_query = 0;
416 end_ref = 0;
417 }
418
419 result->score = score;
420 result->end_query = end_query;
421 result->end_ref = end_ref;
422
423 parasail_free(_F_pr);
424 parasail_free(_H_pr);
425 parasail_free(s2B);
426 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
427 parasail_free(s1);
428 }
429
430 return result;
431 }
432
433