Debian Codegraph @

src/nw_striped_neon_128_16.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 <stdint.h>
11 #include <stdlib.h>
12
13
14
15 #include "parasail.h"
16 #include "parasail/memory.h"
17 #include "parasail/internal_neon.h"
18
19
20
21 #ifdef PARASAIL_TABLE
22 static inline void arr_store_si128(
23 int *array,
24 simde__m128i vH,
25 int32_t t,
26 int32_t seglen,
27 int32_t d,
28 int32_t dlen)
29 {
30 array[1LL*(0*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 0);
31 array[1LL*(1*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 1);
32 array[1LL*(2*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 2);
33 array[1LL*(3*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 3);
34 array[1LL*(4*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 4);
35 array[1LL*(5*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 5);
36 array[1LL*(6*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 6);
37 array[1LL*(7*seglen+t)*dlen + d] = (int16_t)simde_mm_extract_epi16(vH, 7);
38 }
39 #endif
40
41 #ifdef PARASAIL_ROWCOL
42 static inline void arr_store_col(
43 int *col,
44 simde__m128i vH,
45 int32_t t,
46 int32_t seglen)
47 {
48 col[0*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 0);
49 col[1*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 1);
50 col[2*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 2);
51 col[3*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 3);
52 col[4*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 4);
53 col[5*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 5);
54 col[6*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 6);
55 col[7*seglen+t] = (int16_t)simde_mm_extract_epi16(vH, 7);
56 }
57 #endif
58
59 #ifdef PARASAIL_TABLE
60 #define FNAME parasail_nw_table_striped_neon_128_16
61 #define PNAME parasail_nw_table_striped_profile_neon_128_16
62 #else
63 #ifdef PARASAIL_ROWCOL
64 #define FNAME parasail_nw_rowcol_striped_neon_128_16
65 #define PNAME parasail_nw_rowcol_striped_profile_neon_128_16
66 #else
67 #define FNAME parasail_nw_striped_neon_128_16
68 #define PNAME parasail_nw_striped_profile_neon_128_16
69 #endif
70 #endif
71
72 parasail_result_t* FNAME(
73 const char * const restrict s1, const int s1Len,
74 const char * const restrict s2, const int s2Len,
75 const int open, const int gap, const parasail_matrix_t *matrix)
76 {
77 /* declare local variables */
78 parasail_profile_t *profile = NULL;
79 parasail_result_t *result = NULL;
80
81 /* validate inputs */
82 PARASAIL_CHECK_NULL(s2);
83 PARASAIL_CHECK_GT0(s2Len);
84 PARASAIL_CHECK_GE0(open);
85 PARASAIL_CHECK_GE0(gap);
86 PARASAIL_CHECK_NULL(matrix);
87 if (matrix->type == PARASAIL_MATRIX_TYPE_SQUARE) {
88 PARASAIL_CHECK_NULL(s1);
89 PARASAIL_CHECK_GT0(s1Len);
90 }
91
92 /* initialize local variables */
93 profile = parasail_profile_create_neon_128_16(s1, s1Len, matrix);
94 if (!profile) return NULL;
95 result = PNAME(profile, s2, s2Len, open, gap);
96
97 parasail_profile_free(profile);
98
99 return result;
100 }
101
102 parasail_result_t* PNAME(
103 const parasail_profile_t * const restrict profile,
104 const char * const restrict s2, const int s2Len,
105 const int open, const int gap)
106 {
107 /* declare local variables */
108 int32_t i = 0;
109 int32_t j = 0;
110 int32_t k = 0;
111 int s1Len = 0;
112 int32_t end_query = 0;
113 int32_t end_ref = 0;
114 const parasail_matrix_t *matrix = NULL;
115 int32_t segWidth = 0;
116 int32_t segLen = 0;
117 int32_t offset = 0;
118 int32_t position = 0;
119 simde__m128i* restrict vProfile = NULL;
120 simde__m128i* restrict pvHStore = NULL;
121 simde__m128i* restrict pvHLoad = NULL;
122 simde__m128i* restrict pvE = NULL;
123 int16_t* restrict boundary = NULL;
124 simde__m128i vGapO;
125 simde__m128i vGapE;
126 int16_t NEG_LIMIT = 0;
127 int16_t POS_LIMIT = 0;
128 int16_t score = 0;
129 simde__m128i vNegLimit;
130 simde__m128i vPosLimit;
131 simde__m128i vSaturationCheckMin;
132 simde__m128i vSaturationCheckMax;
133 parasail_result_t *result = NULL;
134
135 /* validate inputs */
136 PARASAIL_CHECK_NULL(profile);
137 PARASAIL_CHECK_NULL(profile->profile16.score);
138 PARASAIL_CHECK_NULL(profile->matrix);
139 PARASAIL_CHECK_GT0(profile->s1Len);
140 PARASAIL_CHECK_NULL(s2);
141 PARASAIL_CHECK_GT0(s2Len);
142 PARASAIL_CHECK_GE0(open);
143 PARASAIL_CHECK_GE0(gap);
144
145 /* initialize stack variables */
146 i = 0;
147 j = 0;
148 k = 0;
149 s1Len = profile->s1Len;
150 end_query = s1Len-1;
151 end_ref = s2Len-1;
152 matrix = profile->matrix;
153 segWidth = 8; /* number of values in vector unit */
154 segLen = (s1Len + segWidth - 1) / segWidth;
155 offset = (s1Len - 1) % segLen;
156 position = (segWidth - 1) - (s1Len - 1) / segLen;
157 vProfile = (simde__m128i*)profile->profile16.score;
158 vGapO = simde_mm_set1_epi16(open);
159 vGapE = simde_mm_set1_epi16(gap);
160 NEG_LIMIT = (-open < matrix->min ? INT16_MIN + open : INT16_MIN - matrix->min) + 1;
161 POS_LIMIT = INT16_MAX - matrix->max - 1;
162 score = NEG_LIMIT;
163 vNegLimit = simde_mm_set1_epi16(NEG_LIMIT);
164 vPosLimit = simde_mm_set1_epi16(POS_LIMIT);
165 vSaturationCheckMin = vPosLimit;
166 vSaturationCheckMax = vNegLimit;
167
168 /* initialize result */
169 #ifdef PARASAIL_TABLE
170 result = parasail_result_new_table1(segLen*segWidth, s2Len);
171 #else
172 #ifdef PARASAIL_ROWCOL
173 result = parasail_result_new_rowcol1(segLen*segWidth, s2Len);
174 #else
175 result = parasail_result_new();
176 #endif
177 #endif
178 if (!result) return NULL;
179
180 /* set known flags */
181 result->flag |= PARASAIL_FLAG_NW | PARASAIL_FLAG_STRIPED
182 | PARASAIL_FLAG_BITS_16 | PARASAIL_FLAG_LANES_8;
183 #ifdef PARASAIL_TABLE
184 result->flag |= PARASAIL_FLAG_TABLE;
185 #endif
186 #ifdef PARASAIL_ROWCOL
187 result->flag |= PARASAIL_FLAG_ROWCOL;
188 #endif
189
190 /* initialize heap variables */
191 pvHStore = parasail_memalign_simde__m128i(16, segLen);
192 pvHLoad = parasail_memalign_simde__m128i(16, segLen);
193 pvE = parasail_memalign_simde__m128i(16, segLen);
194 boundary = parasail_memalign_int16_t(16, s2Len+1);
195
196 /* validate heap variables */
197 if (!pvHStore) return NULL;
198 if (!pvHLoad) return NULL;
199 if (!pvE) return NULL;
200 if (!boundary) return NULL;
201
202 /* initialize H and E */
203 {
204 int32_t index = 0;
205 for (i=0; i<segLen; ++i) {
206 int32_t segNum = 0;
207 simde__m128i h;
208 simde__m128i e;
209 for (segNum=0; segNum<segWidth; ++segNum) {
210 int64_t tmp = -open-gap*(segNum*segLen+i);
211 h.i16[segNum] = tmp < INT16_MIN ? INT16_MIN : tmp;
212 tmp = tmp - open;
213 e.i16[segNum] = tmp < INT16_MIN ? INT16_MIN : tmp;
214 }
215 simde_mm_store_si128(&pvHStore[index], h);
216 simde_mm_store_si128(&pvE[index], e);
217 ++index;
218 }
219 }
220
221 /* initialize uppder boundary */
222 {
223 boundary[0] = 0;
224 for (i=1; i<=s2Len; ++i) {
225 int64_t tmp = -open-gap*(i-1);
226 boundary[i] = tmp < INT16_MIN ? INT16_MIN : tmp;
227 }
228 }
229
230 /* outer loop over database sequence */
231 for (j=0; j<s2Len; ++j) {
232 simde__m128i vE;
233 /* Initialize F value to -inf. Any errors to vH values will be
234 * corrected in the Lazy_F loop. */
235 simde__m128i vF = vNegLimit;
236
237 /* load final segment of pvHStore and shift left by 2 bytes */
238 simde__m128i vH = simde_mm_slli_si128(pvHStore[segLen - 1], 2);
239
240 /* Correct part of the vProfile */
241 const simde__m128i* vP = vProfile + matrix->mapper[(unsigned char)s2[j]] * segLen;
242
243 /* Swap the 2 H buffers. */
244 simde__m128i* pv = pvHLoad;
245 pvHLoad = pvHStore;
246 pvHStore = pv;
247
248 /* insert upper boundary condition */
249 vH = simde_mm_insert_epi16(vH, boundary[j], 0);
250
251 /* inner loop to process the query sequence */
252 for (i=0; i<segLen; ++i) {
253 vH = simde_mm_adds_epi16(vH, simde_mm_load_si128(vP + i));
254 vE = simde_mm_load_si128(pvE + i);
255
256 /* Get max from vH, vE and vF. */
257 vH = simde_mm_max_epi16(vH, vE);
258 vH = simde_mm_max_epi16(vH, vF);
259 /* Save vH values. */
260 simde_mm_store_si128(pvHStore + i, vH);
261 vSaturationCheckMax = simde_mm_max_epi16(vSaturationCheckMax, vH);
262 vSaturationCheckMin = simde_mm_min_epi16(vSaturationCheckMin, vH);
263 vSaturationCheckMin = simde_mm_min_epi16(vSaturationCheckMin, vE);
264 vSaturationCheckMin = simde_mm_min_epi16(vSaturationCheckMin, vF);
265 #ifdef PARASAIL_TABLE
266 arr_store_si128(result->tables->score_table, vH, i, segLen, j, s2Len);
267 #endif
268
269 /* Update vE value. */
270 vH = simde_mm_subs_epi16(vH, vGapO);
271 vE = simde_mm_subs_epi16(vE, vGapE);
272 vE = simde_mm_max_epi16(vE, vH);
273 simde_mm_store_si128(pvE + i, vE);
274
275 /* Update vF value. */
276 vF = simde_mm_subs_epi16(vF, vGapE);
277 vF = simde_mm_max_epi16(vF, vH);
278
279 /* Load the next vH. */
280 vH = simde_mm_load_si128(pvHLoad + i);
281 }
282
283 /* Lazy_F loop: has been revised to disallow adjecent insertion and
284 * then deletion, so don't update E(i, i), learn from SWPS3 */
285 for (k=0; k<segWidth; ++k) {
286 int64_t tmp = boundary[j+1]-open;
287 int16_t tmp2 = tmp < INT16_MIN ? INT16_MIN : tmp;
288 vF = simde_mm_slli_si128(vF, 2);
289 vF = simde_mm_insert_epi16(vF, tmp2, 0);
290 for (i=0; i<segLen; ++i) {
291 vH = simde_mm_load_si128(pvHStore + i);
292 vH = simde_mm_max_epi16(vH,vF);
293 simde_mm_store_si128(pvHStore + i, vH);
294 vSaturationCheckMin = simde_mm_min_epi16(vSaturationCheckMin, vH);
295 vSaturationCheckMax = simde_mm_max_epi16(vSaturationCheckMax, vH);
296 #ifdef PARASAIL_TABLE
297 arr_store_si128(result->tables->score_table, vH, i, segLen, j, s2Len);
298 #endif
299 vH = simde_mm_subs_epi16(vH, vGapO);
300 vF = simde_mm_subs_epi16(vF, vGapE);
301 if (! simde_mm_movemask_epi8(simde_mm_cmpgt_epi16(vF, vH))) goto end;
302 /*vF = simde_mm_max_epi16(vF, vH);*/
303 }
304 }
305 end:
306 {
307 }
308
309 #ifdef PARASAIL_ROWCOL
310 /* extract last value from the column */
311 {
312 vH = simde_mm_load_si128(pvHStore + offset);
313 for (k=0; k<position; ++k) {
314 vH = simde_mm_slli_si128(vH, 2);
315 }
316 result->rowcols->score_row[j] = (int16_t) simde_mm_extract_epi16 (vH, 7);
317 }
318 #endif
319 }
320
321 #ifdef PARASAIL_ROWCOL
322 for (i=0; i<segLen; ++i) {
323 simde__m128i vH = simde_mm_load_si128(pvHStore+i);
324 arr_store_col(result->rowcols->score_col, vH, i, segLen);
325 }
326 #endif
327
328 /* extract last value from the last column */
329 {
330 simde__m128i vH = simde_mm_load_si128(pvHStore + offset);
331 for (k=0; k<position; ++k) {
332 vH = simde_mm_slli_si128 (vH, 2);
333 }
334 score = (int16_t) simde_mm_extract_epi16 (vH, 7);
335 }
336
337 if (simde_mm_movemask_epi8(simde_mm_or_si128(
338 simde_mm_cmplt_epi16(vSaturationCheckMin, vNegLimit),
339 simde_mm_cmpgt_epi16(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(boundary);
351 parasail_free(pvE);
352 parasail_free(pvHLoad);
353 parasail_free(pvHStore);
354
355 return result;
356 }
357