Debian Codegraph @

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