Debian Codegraph @

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