Debian Codegraph @

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