Debian Codegraph @

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