Debian Codegraph @

src/sg_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 #define SG_SUFFIX _diag_neon_128_32
19 #include "sg_helper.h"
20
21
22
23 #ifdef PARASAIL_TABLE
24 static inline void arr_store_si128(
25 int *array,
26 simde__m128i vWH,
27 int32_t i,
28 int32_t s1Len,
29 int32_t j,
30 int32_t s2Len)
31 {
32 if (0 <= i+0 && i+0 < s1Len && 0 <= j-0 && j-0 < s2Len) {
33 array[1LL*(i+0)*s2Len + (j-0)] = (int32_t)simde_mm_extract_epi32(vWH, 3);
34 }
35 if (0 <= i+1 && i+1 < s1Len && 0 <= j-1 && j-1 < s2Len) {
36 array[1LL*(i+1)*s2Len + (j-1)] = (int32_t)simde_mm_extract_epi32(vWH, 2);
37 }
38 if (0 <= i+2 && i+2 < s1Len && 0 <= j-2 && j-2 < s2Len) {
39 array[1LL*(i+2)*s2Len + (j-2)] = (int32_t)simde_mm_extract_epi32(vWH, 1);
40 }
41 if (0 <= i+3 && i+3 < s1Len && 0 <= j-3 && j-3 < s2Len) {
42 array[1LL*(i+3)*s2Len + (j-3)] = (int32_t)simde_mm_extract_epi32(vWH, 0);
43 }
44 }
45 #endif
46
47 #ifdef PARASAIL_ROWCOL
48 static inline void arr_store_rowcol(
49 int *row,
50 int *col,
51 simde__m128i vWH,
52 int32_t i,
53 int32_t s1Len,
54 int32_t j,
55 int32_t s2Len)
56 {
57 if (i+0 == s1Len-1 && 0 <= j-0 && j-0 < s2Len) {
58 row[j-0] = (int32_t)simde_mm_extract_epi32(vWH, 3);
59 }
60 if (j-0 == s2Len-1 && 0 <= i+0 && i+0 < s1Len) {
61 col[(i+0)] = (int32_t)simde_mm_extract_epi32(vWH, 3);
62 }
63 if (i+1 == s1Len-1 && 0 <= j-1 && j-1 < s2Len) {
64 row[j-1] = (int32_t)simde_mm_extract_epi32(vWH, 2);
65 }
66 if (j-1 == s2Len-1 && 0 <= i+1 && i+1 < s1Len) {
67 col[(i+1)] = (int32_t)simde_mm_extract_epi32(vWH, 2);
68 }
69 if (i+2 == s1Len-1 && 0 <= j-2 && j-2 < s2Len) {
70 row[j-2] = (int32_t)simde_mm_extract_epi32(vWH, 1);
71 }
72 if (j-2 == s2Len-1 && 0 <= i+2 && i+2 < s1Len) {
73 col[(i+2)] = (int32_t)simde_mm_extract_epi32(vWH, 1);
74 }
75 if (i+3 == s1Len-1 && 0 <= j-3 && j-3 < s2Len) {
76 row[j-3] = (int32_t)simde_mm_extract_epi32(vWH, 0);
77 }
78 if (j-3 == s2Len-1 && 0 <= i+3 && i+3 < s1Len) {
79 col[(i+3)] = (int32_t)simde_mm_extract_epi32(vWH, 0);
80 }
81 }
82 #endif
83
84 #ifdef PARASAIL_TABLE
85 #define FNAME parasail_sg_flags_table_diag_neon_128_32
86 #else
87 #ifdef PARASAIL_ROWCOL
88 #define FNAME parasail_sg_flags_rowcol_diag_neon_128_32
89 #else
90 #define FNAME parasail_sg_flags_diag_neon_128_32
91 #endif
92 #endif
93
94 parasail_result_t* FNAME(
95 const char * const restrict _s1, const int _s1Len,
96 const char * const restrict _s2, const int s2Len,
97 const int open, const int gap, const parasail_matrix_t *matrix,
98 int s1_beg, int s1_end, int s2_beg, int s2_end)
99 {
100 /* declare local variables */
101 int32_t N = 0;
102 int32_t PAD = 0;
103 int32_t PAD2 = 0;
104 int32_t s1Len = 0;
105 int32_t s1Len_PAD = 0;
106 int32_t s2Len_PAD = 0;
107 int32_t * restrict s1 = NULL;
108 int32_t * restrict s2B = NULL;
109 int32_t * restrict _H_pr = NULL;
110 int32_t * restrict _F_pr = NULL;
111 int32_t * restrict s2 = NULL;
112 int32_t * restrict H_pr = NULL;
113 int32_t * restrict F_pr = NULL;
114 parasail_result_t *result = NULL;
115 int32_t i = 0;
116 int32_t j = 0;
117 int32_t end_query = 0;
118 int32_t end_ref = 0;
119 int32_t NEG_LIMIT = 0;
120 int32_t POS_LIMIT = 0;
121 int32_t score = 0;
122 simde__m128i vNegLimit;
123 simde__m128i vPosLimit;
124 simde__m128i vSaturationCheckMin;
125 simde__m128i vSaturationCheckMax;
126 simde__m128i vNegInf;
127 simde__m128i vOpen;
128 simde__m128i vGap;
129 simde__m128i vOne;
130 simde__m128i vN;
131 simde__m128i vGapN;
132 simde__m128i vNegOne;
133 simde__m128i vI;
134 simde__m128i vJreset;
135 simde__m128i vMaxHRow;
136 simde__m128i vMaxHCol;
137 simde__m128i vLastVal;
138 simde__m128i vEndI;
139 simde__m128i vEndJ;
140 simde__m128i vILimit;
141 simde__m128i vILimit1;
142 simde__m128i vJLimit;
143 simde__m128i vJLimit1;
144 simde__m128i vIBoundary;
145
146 /* validate inputs */
147 PARASAIL_CHECK_NULL(_s2);
148 PARASAIL_CHECK_GT0(s2Len);
149 PARASAIL_CHECK_GE0(open);
150 PARASAIL_CHECK_GE0(gap);
151 PARASAIL_CHECK_NULL(matrix);
152 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
153 PARASAIL_CHECK_NULL(_s1);
154 PARASAIL_CHECK_GT0(_s1Len);
155 }
156
157 /* initialize stack variables */
158 N = 4; /* number of values in vector */
159 PAD = N-1;
160 PAD2 = PAD*2;
161 s1Len = matrix->type == PARASAIL_MATRIX_TYPE_SQUARE ? _s1Len : matrix->length;
162 s1Len_PAD = s1Len+PAD;
163 s2Len_PAD = s2Len+PAD;
164 i = 0;
165 j = 0;
166 end_query = 0;
167 end_ref = 0;
168 NEG_LIMIT = (-open < matrix->min ? INT32_MIN + open : INT32_MIN - matrix->min) + 1;
169 POS_LIMIT = INT32_MAX - matrix->max - 1;
170 score = NEG_LIMIT;
171 vNegLimit = simde_mm_set1_epi32(NEG_LIMIT);
172 vPosLimit = simde_mm_set1_epi32(POS_LIMIT);
173 vSaturationCheckMin = vPosLimit;
174 vSaturationCheckMax = vNegLimit;
175 vNegInf = simde_mm_set1_epi32(NEG_LIMIT);
176 vOpen = simde_mm_set1_epi32(open);
177 vGap = simde_mm_set1_epi32(gap);
178 vOne = simde_mm_set1_epi32(1);
179 vN = simde_mm_set1_epi32(N);
180 vGapN = s1_beg ? simde_mm_set1_epi32(0) : simde_mm_set1_epi32(gap*N);
181 vNegOne = simde_mm_set1_epi32(-1);
182 vI = simde_mm_set_epi32(0,1,2,3);
183 vJreset = simde_mm_set_epi32(0,-1,-2,-3);
184 vMaxHRow = vNegInf;
185 vMaxHCol = vNegInf;
186 vLastVal = vNegInf;
187 vEndI = vNegInf;
188 vEndJ = vNegInf;
189 vILimit = simde_mm_set1_epi32(s1Len);
190 vILimit1 = simde_mm_sub_epi32(vILimit, vOne);
191 vJLimit = simde_mm_set1_epi32(s2Len);
192 vJLimit1 = simde_mm_sub_epi32(vJLimit, vOne);
193 vIBoundary = s1_beg ? simde_mm_set1_epi32(0) : simde_mm_set_epi32(
194 -open-0*gap,
195 -open-1*gap,
196 -open-2*gap,
197 -open-3*gap);
198
199 /* initialize result */
200 #ifdef PARASAIL_TABLE
201 result = parasail_result_new_table1(s1Len, s2Len);
202 #else
203 #ifdef PARASAIL_ROWCOL
204 result = parasail_result_new_rowcol1(s1Len, s2Len);
205 #else
206 result = parasail_result_new();
207 #endif
208 #endif
209 if (!result) return NULL;
210
211 /* set known flags */
212 result->flag |= PARASAIL_FLAG_SG | PARASAIL_FLAG_DIAG
213 | PARASAIL_FLAG_BITS_32 | PARASAIL_FLAG_LANES_4;
214 result->flag |= s1_beg ? PARASAIL_FLAG_SG_S1_BEG : 0;
215 result->flag |= s1_end ? PARASAIL_FLAG_SG_S1_END : 0;
216 result->flag |= s2_beg ? PARASAIL_FLAG_SG_S2_BEG : 0;
217 result->flag |= s2_end ? PARASAIL_FLAG_SG_S2_END : 0;
218 #ifdef PARASAIL_TABLE
219 result->flag |= PARASAIL_FLAG_TABLE;
220 #endif
221 #ifdef PARASAIL_ROWCOL
222 result->flag |= PARASAIL_FLAG_ROWCOL;
223 #endif
224
225 /* initialize heap variables */
226 s2B= parasail_memalign_int32_t(16, s2Len+PAD2);
227 _H_pr = parasail_memalign_int32_t(16, s2Len+PAD2);
228 _F_pr = parasail_memalign_int32_t(16, s2Len+PAD2);
229 s2 = s2B+PAD; /* will allow later for negative indices */
230 H_pr = _H_pr+PAD;
231 F_pr = _F_pr+PAD;
232
233 /* validate heap variables */
234 if (!s2B) return NULL;
235 if (!_H_pr) return NULL;
236 if (!_F_pr) return NULL;
237
238 /* convert _s1 from char to int in range 0-23 */
239 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
240 s1 = parasail_memalign_int32_t(16, s1Len+PAD);
241 if (!s1) return NULL;
242 for (i=0; i<s1Len; ++i) {
243 s1[i] = matrix->mapper[(unsigned char)_s1[i]];
244 }
245 /* pad back of s1 with dummy values */
246 for (i=s1Len; i<s1Len_PAD; ++i) {
247 s1[i] = 0; /* point to first matrix row because we don't care */
248 }
249 }
250
251 /* convert _s2 from char to int in range 0-23 */
252 for (j=0; j<s2Len; ++j) {
253 s2[j] = matrix->mapper[(unsigned char)_s2[j]];
254 }
255 /* pad front of s2 with dummy values */
256 for (j=-PAD; j<0; ++j) {
257 s2[j] = 0; /* point to first matrix row because we don't care */
258 }
259 /* pad back of s2 with dummy values */
260 for (j=s2Len; j<s2Len_PAD; ++j) {
261 s2[j] = 0; /* point to first matrix row because we don't care */
262 }
263
264 /* set initial values for stored row */
265 if (s2_beg) {
266 for (j=0; j<s2Len; ++j) {
267 H_pr[j] = 0;
268 F_pr[j] = NEG_LIMIT;
269 }
270 }
271 else {
272 for (j=0; j<s2Len; ++j) {
273 H_pr[j] = -open - j*gap;
274 F_pr[j] = NEG_LIMIT;
275 }
276 }
277 /* pad front of stored row values */
278 for (j=-PAD; j<0; ++j) {
279 H_pr[j] = NEG_LIMIT;
280 F_pr[j] = NEG_LIMIT;
281 }
282 /* pad back of stored row values */
283 for (j=s2Len; j<s2Len+PAD; ++j) {
284 H_pr[j] = NEG_LIMIT;
285 F_pr[j] = NEG_LIMIT;
286 }
287 H_pr[-1] = 0; /* upper left corner */
288
289 /* iterate over query sequence */
290 for (i=0; i<s1Len; i+=N) {
291 simde__m128i vNH = vNegInf;
292 simde__m128i vWH = vNegInf;
293 simde__m128i vE = vNegInf;
294 simde__m128i vF = vNegInf;
295 simde__m128i vJ = vJreset;
296 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))];
297 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))];
298 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))];
299 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))];
300 simde__m128i vIltLimit = simde_mm_cmplt_epi32(vI, vILimit);
301 simde__m128i vIeqLimit1 = simde_mm_cmpeq_epi32(vI, vILimit1);
302 vNH = simde_mm_srli_si128(vNH, 4);
303 vNH = simde_mm_insert_epi32(vNH, H_pr[-1], 3);
304 vWH = simde_mm_srli_si128(vWH, 4);
305 vWH = simde_mm_insert_epi32(vWH, s1_beg ? 0 : (-open - i*gap), 3);
306 H_pr[-1] = -open - (i+N)*gap;
307 /* iterate over database sequence */
308 for (j=0; j<s2Len+PAD; ++j) {
309 simde__m128i vMat;
310 simde__m128i vNWH = vNH;
311 vNH = simde_mm_srli_si128(vWH, 4);
312 vNH = simde_mm_insert_epi32(vNH, H_pr[j], 3);
313 vF = simde_mm_srli_si128(vF, 4);
314 vF = simde_mm_insert_epi32(vF, F_pr[j], 3);
315 vF = simde_mm_max_epi32(
316 simde_mm_sub_epi32(vNH, vOpen),
317 simde_mm_sub_epi32(vF, vGap));
318 vE = simde_mm_max_epi32(
319 simde_mm_sub_epi32(vWH, vOpen),
320 simde_mm_sub_epi32(vE, vGap));
321 vMat = simde_mm_set_epi32(
322 matrow0[s2[j-0]],
323 matrow1[s2[j-1]],
324 matrow2[s2[j-2]],
325 matrow3[s2[j-3]]
326 );
327 vNWH = simde_mm_add_epi32(vNWH, vMat);
328 vWH = simde_mm_max_epi32(vNWH, vE);
329 vWH = simde_mm_max_epi32(vWH, vF);
330 /* as minor diagonal vector passes across the j=-1 boundary,
331 * assign the appropriate boundary conditions */
332 {
333 simde__m128i cond = simde_mm_cmpeq_epi32(vJ,vNegOne);
334 vWH = simde_mm_blendv_epi8(vWH, vIBoundary, cond);
335 vF = simde_mm_blendv_epi8(vF, vNegInf, cond);
336 vE = simde_mm_blendv_epi8(vE, vNegInf, cond);
337 }
338 /* cannot start checking sat until after J clears boundary */
339 if (j > PAD) {
340 vSaturationCheckMin = simde_mm_min_epi32(vSaturationCheckMin, vWH);
341 vSaturationCheckMax = simde_mm_max_epi32(vSaturationCheckMax, vWH);
342 }
343 #ifdef PARASAIL_TABLE
344 arr_store_si128(result->tables->score_table, vWH, i, s1Len, j, s2Len);
345 #endif
346 #ifdef PARASAIL_ROWCOL
347 arr_store_rowcol(result->rowcols->score_row, result->rowcols->score_col, vWH, i, s1Len, j, s2Len);
348 #endif
349 H_pr[j-3] = (int32_t)simde_mm_extract_epi32(vWH,0);
350 F_pr[j-3] = (int32_t)simde_mm_extract_epi32(vF,0);
351 /* as minor diagonal vector passes across the i or j limit
352 * boundary, extract the last value of the column or row */
353 {
354 simde__m128i vJeqLimit1 = simde_mm_cmpeq_epi32(vJ, vJLimit1);
355 simde__m128i vJgtNegOne = simde_mm_cmpgt_epi32(vJ, vNegOne);
356 simde__m128i vJltLimit = simde_mm_cmplt_epi32(vJ, vJLimit);
357 simde__m128i cond_j = simde_mm_and_si128(vIltLimit, vJeqLimit1);
358 simde__m128i cond_i = simde_mm_and_si128(vIeqLimit1,
359 simde_mm_and_si128(vJgtNegOne, vJltLimit));
360 simde__m128i cond_max_row = simde_mm_cmpgt_epi32(vWH, vMaxHRow);
361 simde__m128i cond_max_col = simde_mm_cmpgt_epi32(vWH, vMaxHCol);
362 simde__m128i cond_last_val = simde_mm_and_si128(vIeqLimit1, vJeqLimit1);
363 simde__m128i cond_all_row = simde_mm_and_si128(cond_max_row, cond_i);
364 simde__m128i cond_all_col = simde_mm_and_si128(cond_max_col, cond_j);
365 vMaxHRow = simde_mm_blendv_epi8(vMaxHRow, vWH, cond_all_row);
366 vMaxHCol = simde_mm_blendv_epi8(vMaxHCol, vWH, cond_all_col);
367 vLastVal = simde_mm_blendv_epi8(vLastVal, vWH, cond_last_val);
368 vEndI = simde_mm_blendv_epi8(vEndI, vI, cond_all_col);
369 vEndJ = simde_mm_blendv_epi8(vEndJ, vJ, cond_all_row);
370 }
371 vJ = simde_mm_add_epi32(vJ, vOne);
372 }
373 vI = simde_mm_add_epi32(vI, vN);
374 vIBoundary = simde_mm_sub_epi32(vIBoundary, vGapN);
375 }
376
377 /* alignment ending position */
378 {
379 int32_t max_row = NEG_LIMIT;
380 int32_t max_col = NEG_LIMIT;
381 int32_t last_val = NEG_LIMIT;
382 int32_t *s = (int32_t*)&vMaxHRow;
383 int32_t *t = (int32_t*)&vMaxHCol;
384 int32_t *u = (int32_t*)&vLastVal;
385 int32_t *i = (int32_t*)&vEndI;
386 int32_t *j = (int32_t*)&vEndJ;
387 int32_t k;
388 for (k=0; k<N; ++k, ++s, ++t, ++u, ++i, ++j) {
389 if (*t > max_col || (*t == max_col && *i < end_query)) {
390 max_col = *t;
391 end_query = *i;
392 }
393 if (*s > max_row) {
394 max_row = *s;
395 end_ref = *j;
396 }
397 if (*u > last_val) {
398 last_val = *u;
399 }
400 }
401 if (s1_end && s2_end) {
402 if (max_col > max_row || (max_col == max_row && end_ref == s2Len-1)) {
403 score = max_col;
404 end_ref = s2Len-1;
405 }
406 else {
407 score = max_row;
408 end_query = s1Len-1;
409 }
410 }
411 else if (s1_end) {
412 score = max_col;
413 end_ref = s2Len-1;
414 }
415 else if (s2_end) {
416 score = max_row;
417 end_query = s1Len-1;
418 }
419 else {
420 score = last_val;
421 end_query = s1Len-1;
422 end_ref = s2Len-1;
423 }
424 }
425
426 if (simde_mm_movemask_epi8(simde_mm_or_si128(
427 simde_mm_cmplt_epi32(vSaturationCheckMin, vNegLimit),
428 simde_mm_cmpgt_epi32(vSaturationCheckMax, vPosLimit)))) {
429 result->flag |= PARASAIL_FLAG_SATURATED;
430 score = 0;
431 end_query = 0;
432 end_ref = 0;
433 }
434
435 result->score = score;
436 result->end_query = end_query;
437 result->end_ref = end_ref;
438
439 parasail_free(_F_pr);
440 parasail_free(_H_pr);
441 parasail_free(s2B);
442 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
443 parasail_free(s1);
444 }
445
446 return result;
447 }
448
449 SG_IMPL_ALL
450
451