Debian Codegraph @

src/nw_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_nw_table_diag_neon_128_64
65 #else
66 #ifdef PARASAIL_ROWCOL
67 #define FNAME parasail_nw_rowcol_diag_neon_128_64
68 #else
69 #define FNAME parasail_nw_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 vOpen;
106 simde__m128i vGap;
107 simde__m128i vOne;
108 simde__m128i vN;
109 simde__m128i vGapN;
110 simde__m128i vNegOne;
111 simde__m128i vI;
112 simde__m128i vJreset;
113 simde__m128i vMax;
114 simde__m128i vILimit;
115 simde__m128i vILimit1;
116 simde__m128i vJLimit;
117 simde__m128i vJLimit1;
118 simde__m128i vIBoundary;
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 = s1Len-1;
141 end_ref = s2Len-1;
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 vOpen = simde_mm_set1_epi64x(open);
151 vGap = simde_mm_set1_epi64x(gap);
152 vOne = simde_mm_set1_epi64x(1);
153 vN = simde_mm_set1_epi64x(N);
154 vGapN = simde_mm_set1_epi64x(gap*N);
155 vNegOne = simde_mm_set1_epi64x(-1);
156 vI = simde_mm_set_epi64x(0,1);
157 vJreset = simde_mm_set_epi64x(0,-1);
158 vMax = vNegInf;
159 vILimit = simde_mm_set1_epi64x(s1Len);
160 vILimit1 = simde_mm_sub_epi64(vILimit, vOne);
161 vJLimit = simde_mm_set1_epi64x(s2Len);
162 vJLimit1 = simde_mm_sub_epi64(vJLimit, vOne);
163 vIBoundary = simde_mm_set_epi64x(
164 -open-0*gap,
165 -open-1*gap);
166
167 /* initialize result */
168 #ifdef PARASAIL_TABLE
169 result = parasail_result_new_table1(s1Len, s2Len);
170 #else
171 #ifdef PARASAIL_ROWCOL
172 result = parasail_result_new_rowcol1(s1Len, s2Len);
173 #else
174 result = parasail_result_new();
175 #endif
176 #endif
177 if (!result) return NULL;
178
179 /* set known flags */
180 result->flag |= PARASAIL_FLAG_NW | PARASAIL_FLAG_DIAG
181 | PARASAIL_FLAG_BITS_64 | PARASAIL_FLAG_LANES_2;
182 #ifdef PARASAIL_TABLE
183 result->flag |= PARASAIL_FLAG_TABLE;
184 #endif
185 #ifdef PARASAIL_ROWCOL
186 result->flag |= PARASAIL_FLAG_ROWCOL;
187 #endif
188
189 /* initialize heap variables */
190 s2B= parasail_memalign_int64_t(16, s2Len+PAD2);
191 _H_pr = parasail_memalign_int64_t(16, s2Len+PAD2);
192 _F_pr = parasail_memalign_int64_t(16, s2Len+PAD2);
193 s2 = s2B+PAD; /* will allow later for negative indices */
194 H_pr = _H_pr+PAD;
195 F_pr = _F_pr+PAD;
196
197 /* validate heap variables */
198 if (!s2B) return NULL;
199 if (!_H_pr) return NULL;
200 if (!_F_pr) return NULL;
201
202 /* convert _s1 from char to int in range 0-23 */
203 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
204 s1 = parasail_memalign_int64_t(16, s1Len+PAD);
205 if (!s1) return NULL;
206 for (i=0; i<s1Len; ++i) {
207 s1[i] = matrix->mapper[(unsigned char)_s1[i]];
208 }
209 /* pad back of s1 with dummy values */
210 for (i=s1Len; i<s1Len_PAD; ++i) {
211 s1[i] = 0; /* point to first matrix row because we don't care */
212 }
213 }
214
215 /* convert _s2 from char to int in range 0-23 */
216 for (j=0; j<s2Len; ++j) {
217 s2[j] = matrix->mapper[(unsigned char)_s2[j]];
218 }
219 /* pad front of s2 with dummy values */
220 for (j=-PAD; j<0; ++j) {
221 s2[j] = 0; /* point to first matrix row because we don't care */
222 }
223 /* pad back of s2 with dummy values */
224 for (j=s2Len; j<s2Len_PAD; ++j) {
225 s2[j] = 0; /* point to first matrix row because we don't care */
226 }
227
228 /* set initial values for stored row */
229 for (j=0; j<s2Len; ++j) {
230 H_pr[j] = -open - j*gap;
231 F_pr[j] = NEG_LIMIT;
232 }
233 /* pad front of stored row values */
234 for (j=-PAD; j<0; ++j) {
235 H_pr[j] = NEG_LIMIT;
236 F_pr[j] = NEG_LIMIT;
237 }
238 /* pad back of stored row values */
239 for (j=s2Len; j<s2Len+PAD; ++j) {
240 H_pr[j] = NEG_LIMIT;
241 F_pr[j] = NEG_LIMIT;
242 }
243 H_pr[-1] = 0; /* upper left corner */
244
245 /* iterate over query sequence */
246 for (i=0; i<s1Len; i+=N) {
247 simde__m128i vNH = vNegInf;
248 simde__m128i vWH = vNegInf;
249 simde__m128i vE = vNegInf;
250 simde__m128i vF = vNegInf;
251 simde__m128i vJ = vJreset;
252 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))];
253 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))];
254 vNH = simde_mm_srli_si128(vNH, 8);
255 vNH = simde_mm_insert_epi64(vNH, H_pr[-1], 1);
256 vWH = simde_mm_srli_si128(vWH, 8);
257 vWH = simde_mm_insert_epi64(vWH, -open - i*gap, 1);
258 H_pr[-1] = -open - (i+N)*gap;
259 /* iterate over database sequence */
260 for (j=0; j<s2Len+PAD; ++j) {
261 simde__m128i vMat;
262 simde__m128i vNWH = vNH;
263 vNH = simde_mm_srli_si128(vWH, 8);
264 vNH = simde_mm_insert_epi64(vNH, H_pr[j], 1);
265 vF = simde_mm_srli_si128(vF, 8);
266 vF = simde_mm_insert_epi64(vF, F_pr[j], 1);
267 vF = simde_mm_max_epi64(
268 simde_mm_sub_epi64(vNH, vOpen),
269 simde_mm_sub_epi64(vF, vGap));
270 vE = simde_mm_max_epi64(
271 simde_mm_sub_epi64(vWH, vOpen),
272 simde_mm_sub_epi64(vE, vGap));
273 vMat = simde_mm_set_epi64x(
274 matrow0[s2[j-0]],
275 matrow1[s2[j-1]]
276 );
277 vNWH = simde_mm_add_epi64(vNWH, vMat);
278 vWH = simde_mm_max_epi64(vNWH, vE);
279 vWH = simde_mm_max_epi64(vWH, vF);
280 /* as minor diagonal vector passes across the j=-1 boundary,
281 * assign the appropriate boundary conditions */
282 {
283 simde__m128i cond = simde_mm_cmpeq_epi64(vJ,vNegOne);
284 vWH = simde_mm_blendv_epi8(vWH, vIBoundary, cond);
285 vF = simde_mm_blendv_epi8(vF, vNegInf, cond);
286 vE = simde_mm_blendv_epi8(vE, vNegInf, cond);
287 }
288 /* cannot start checking sat until after J clears boundary */
289 if (j > PAD) {
290 vSaturationCheckMin = simde_mm_min_epi64(vSaturationCheckMin, vWH);
291 vSaturationCheckMax = simde_mm_max_epi64(vSaturationCheckMax, vWH);
292 }
293 #ifdef PARASAIL_TABLE
294 arr_store_si128(result->tables->score_table, vWH, i, s1Len, j, s2Len);
295 #endif
296 #ifdef PARASAIL_ROWCOL
297 arr_store_rowcol(result->rowcols->score_row, result->rowcols->score_col, vWH, i, s1Len, j, s2Len);
298 #endif
299 H_pr[j-1] = (int64_t)simde_mm_extract_epi64(vWH,0);
300 F_pr[j-1] = (int64_t)simde_mm_extract_epi64(vF,0);
301 /* as minor diagonal vector passes across table, extract
302 last table value at the i,j bound */
303 {
304 simde__m128i cond_valid_I = simde_mm_cmpeq_epi64(vI, vILimit1);
305 simde__m128i cond_valid_J = simde_mm_cmpeq_epi64(vJ, vJLimit1);
306 simde__m128i cond_all = simde_mm_and_si128(cond_valid_I, cond_valid_J);
307 vMax = simde_mm_blendv_epi8(vMax, vWH, cond_all);
308 }
309 vJ = simde_mm_add_epi64(vJ, vOne);
310 }
311 vI = simde_mm_add_epi64(vI, vN);
312 vIBoundary = simde_mm_sub_epi64(vIBoundary, vGapN);
313 }
314
315 /* max in vMax */
316 for (i=0; i<N; ++i) {
317 int64_t value;
318 value = (int64_t) simde_mm_extract_epi64(vMax, 1);
319 if (value > score) {
320 score = value;
321 }
322 vMax = simde_mm_slli_si128(vMax, 8);
323 }
324
325 if (simde_mm_movemask_epi8(simde_mm_or_si128(
326 simde_mm_cmplt_epi64(vSaturationCheckMin, vNegLimit),
327 simde_mm_cmpgt_epi64(vSaturationCheckMax, vPosLimit)))) {
328 result->flag |= PARASAIL_FLAG_SATURATED;
329 score = 0;
330 end_query = 0;
331 end_ref = 0;
332 }
333
334 result->score = score;
335 result->end_query = end_query;
336 result->end_ref = end_ref;
337
338 parasail_free(_F_pr);
339 parasail_free(_H_pr);
340 parasail_free(s2B);
341 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
342 parasail_free(s1);
343 }
344
345 return result;
346 }
347
348