-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsg_striped_sse41_128_16.c
292 lines (261 loc) · 9.66 KB
/
sg_striped_sse41_128_16.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* @file
*
* @author [email protected]
*
* Copyright (c) 2015 Battelle Memorial Institute.
*/
// #include "config.h"
#include <stdint.h>
#include <stdlib.h>
#if defined(_MSC_VER)
#include <intrin.h>
#else
#include <emmintrin.h>
#include <smmintrin.h>
#endif
#include "parasail.h"
#include "parasail/memory.h"
#include "parasail/internal_sse.h"
static inline int16_t _mm_hmax_epi16_rpl(__m128i a) {
a = _mm_max_epi16(a, _mm_srli_si128(a, 8));
a = _mm_max_epi16(a, _mm_srli_si128(a, 4));
a = _mm_max_epi16(a, _mm_srli_si128(a, 2));
return _mm_extract_epi16(a, 0);
}
#ifdef PARASAIL_TABLE
static inline void arr_store_si128(
int *array,
__m128i vH,
int32_t t,
int32_t seglen,
int32_t d,
int32_t dlen)
{
array[(0*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 0);
array[(1*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 1);
array[(2*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 2);
array[(3*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 3);
array[(4*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 4);
array[(5*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 5);
array[(6*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 6);
array[(7*seglen+t)*dlen + d] = (int16_t)_mm_extract_epi16(vH, 7);
}
#endif
#ifdef PARASAIL_ROWCOL
static inline void arr_store_col(
int *col,
__m128i vH,
int32_t t,
int32_t seglen)
{
col[0*seglen+t] = (int16_t)_mm_extract_epi16(vH, 0);
col[1*seglen+t] = (int16_t)_mm_extract_epi16(vH, 1);
col[2*seglen+t] = (int16_t)_mm_extract_epi16(vH, 2);
col[3*seglen+t] = (int16_t)_mm_extract_epi16(vH, 3);
col[4*seglen+t] = (int16_t)_mm_extract_epi16(vH, 4);
col[5*seglen+t] = (int16_t)_mm_extract_epi16(vH, 5);
col[6*seglen+t] = (int16_t)_mm_extract_epi16(vH, 6);
col[7*seglen+t] = (int16_t)_mm_extract_epi16(vH, 7);
}
#endif
#ifdef PARASAIL_TABLE
#define FNAME parasail_sg_table_striped_sse41_128_16
#define PNAME parasail_sg_table_striped_profile_sse41_128_16
#else
#ifdef PARASAIL_ROWCOL
#define FNAME parasail_sg_rowcol_striped_sse41_128_16
#define PNAME parasail_sg_rowcol_striped_profile_sse41_128_16
#else
#define FNAME parasail_sg_striped_sse41_128_16
#define PNAME parasail_sg_striped_profile_sse41_128_16
#endif
#endif
parasail_result_t* FNAME(
const char * const restrict s1, const int s1Len,
const char * const restrict s2, const int s2Len,
const int open, const int gap, const parasail_matrix_t *matrix)
{
parasail_profile_t *profile = parasail_profile_create_sse_128_16(s1, s1Len, matrix);
parasail_result_t *result = PNAME(profile, s2, s2Len, open, gap);
parasail_profile_free(profile);
return result;
}
parasail_result_t* PNAME(
const parasail_profile_t * const restrict profile,
const char * const restrict s2, const int s2Len,
const int open, const int gap)
{
int32_t i = 0;
int32_t j = 0;
int32_t k = 0;
int32_t end_query = 0;
int32_t end_ref = 0;
const int s1Len = profile->s1Len;
const parasail_matrix_t *matrix = profile->matrix;
const int32_t segWidth = 8; /* number of values in vector unit */
const int32_t segLen = (s1Len + segWidth - 1) / segWidth;
const int32_t offset = (s1Len - 1) % segLen;
const int32_t position = (segWidth - 1) - (s1Len - 1) / segLen;
__m128i* const restrict vProfile = (__m128i*)profile->profile16.score;
__m128i* restrict pvHStore = parasail_memalign___m128i(16, segLen);
__m128i* restrict pvHLoad = parasail_memalign___m128i(16, segLen);
__m128i* const restrict pvE = parasail_memalign___m128i(16, segLen);
const __m128i vGapO = _mm_set1_epi16(open);
const __m128i vGapE = _mm_set1_epi16(gap);
const int16_t NEG_LIMIT = (-open < matrix->min ?
INT16_MIN + open : INT16_MIN - matrix->min) + 1;
const int16_t POS_LIMIT = INT16_MAX - matrix->max - 1;
int16_t score = NEG_LIMIT;
__m128i vNegLimit = _mm_set1_epi16(NEG_LIMIT);
__m128i vPosLimit = _mm_set1_epi16(POS_LIMIT);
__m128i vSaturationCheckMin = vPosLimit;
__m128i vSaturationCheckMax = vNegLimit;
__m128i vMaxH = vNegLimit;
__m128i vPosMask = _mm_cmpeq_epi16(_mm_set1_epi16(position),
_mm_set_epi16(0,1,2,3,4,5,6,7));
#ifdef PARASAIL_TABLE
parasail_result_t *result = parasail_result_new_table1(segLen*segWidth, s2Len);
#else
#ifdef PARASAIL_ROWCOL
parasail_result_t *result = parasail_result_new_rowcol1(segLen*segWidth, s2Len);
#else
parasail_result_t *result = parasail_result_new();
#endif
#endif
/* initialize H and E */
parasail_memset___m128i(pvHStore, _mm_set1_epi16(0), segLen);
parasail_memset___m128i(pvE, _mm_set1_epi16(-open), segLen);
/* outer loop over database sequence */
for (j=0; j<s2Len; ++j) {
__m128i vE;
/* Initialize F value to -inf. Any errors to vH values will be
* corrected in the Lazy_F loop. */
__m128i vF = vNegLimit;
/* load final segment of pvHStore and shift left by 2 bytes */
__m128i vH = _mm_slli_si128(pvHStore[segLen - 1], 2);
/* Correct part of the vProfile */
const __m128i* vP = vProfile + matrix->mapper[(unsigned char)s2[j]] * segLen;
/* Swap the 2 H buffers. */
__m128i* pv = pvHLoad;
pvHLoad = pvHStore;
pvHStore = pv;
/* inner loop to process the query sequence */
for (i=0; i<segLen; ++i) {
vH = _mm_add_epi16(vH, _mm_load_si128(vP + i));
vE = _mm_load_si128(pvE + i);
/* Get max from vH, vE and vF. */
vH = _mm_max_epi16(vH, vE);
vH = _mm_max_epi16(vH, vF);
/* Save vH values. */
_mm_store_si128(pvHStore + i, vH);
vSaturationCheckMin = _mm_min_epi16(vSaturationCheckMin, vH);
vSaturationCheckMax = _mm_max_epi16(vSaturationCheckMax, vH);
#ifdef PARASAIL_TABLE
arr_store_si128(result->score_table, vH, i, segLen, j, s2Len);
#endif
/* Update vE value. */
vH = _mm_sub_epi16(vH, vGapO);
vE = _mm_sub_epi16(vE, vGapE);
vE = _mm_max_epi16(vE, vH);
_mm_store_si128(pvE + i, vE);
/* Update vF value. */
vF = _mm_sub_epi16(vF, vGapE);
vF = _mm_max_epi16(vF, vH);
/* Load the next vH. */
vH = _mm_load_si128(pvHLoad + i);
}
/* Lazy_F loop: has been revised to disallow adjecent insertion and
* then deletion, so don't update E(i, i), learn from SWPS3 */
for (k=0; k<segWidth; ++k) {
vF = _mm_slli_si128(vF, 2);
vF = _mm_insert_epi16(vF, -open, 0);
for (i=0; i<segLen; ++i) {
vH = _mm_load_si128(pvHStore + i);
vH = _mm_max_epi16(vH,vF);
_mm_store_si128(pvHStore + i, vH);
vSaturationCheckMin = _mm_min_epi16(vSaturationCheckMin, vH);
vSaturationCheckMax = _mm_max_epi16(vSaturationCheckMax, vH);
#ifdef PARASAIL_TABLE
arr_store_si128(result->score_table, vH, i, segLen, j, s2Len);
#endif
vH = _mm_sub_epi16(vH, vGapO);
vF = _mm_sub_epi16(vF, vGapE);
if (! _mm_movemask_epi8(_mm_cmpgt_epi16(vF, vH))) goto end;
/*vF = _mm_max_epi16(vF, vH);*/
}
}
end:
{
/* extract vector containing last value from the column */
__m128i vCompare;
vH = _mm_load_si128(pvHStore + offset);
vCompare = _mm_and_si128(vPosMask, _mm_cmpgt_epi16(vH, vMaxH));
vMaxH = _mm_max_epi16(vH, vMaxH);
if (_mm_movemask_epi8(vCompare)) {
end_ref = j;
end_query = s1Len - 1;
}
#ifdef PARASAIL_ROWCOL
for (k=0; k<position; ++k) {
vH = _mm_slli_si128(vH, 2);
}
result->score_row[j] = (int16_t) _mm_extract_epi16 (vH, 7);
#endif
}
}
/* max last value from all columns */
{
for (k=0; k<position; ++k) {
vMaxH = _mm_slli_si128(vMaxH, 2);
}
score = (int16_t) _mm_extract_epi16(vMaxH, 7);
}
/* max of last column */
{
int16_t score_last;
vMaxH = vNegLimit;
for (i=0; i<segLen; ++i) {
__m128i vH = _mm_load_si128(pvHStore + i);
vMaxH = _mm_max_epi16(vH, vMaxH);
#ifdef PARASAIL_ROWCOL
arr_store_col(result->score_col, vH, i, segLen);
#endif
}
/* max in vec */
score_last = _mm_hmax_epi16_rpl(vMaxH);
if (score_last > score || (score_last == score && end_ref == s2Len - 1)) {
score = score_last;
end_ref = s2Len - 1;
end_query = s1Len;
/* Trace the alignment ending position on read. */
{
int16_t *t = (int16_t*)pvHStore;
int32_t column_len = segLen * segWidth;
for (i = 0; i<column_len; ++i, ++t) {
if (*t == score) {
int32_t temp = i / segWidth + i % segWidth * segLen;
if (temp < end_query) {
end_query = temp;
}
}
}
}
}
}
if (_mm_movemask_epi8(_mm_or_si128(
_mm_cmplt_epi16(vSaturationCheckMin, vNegLimit),
_mm_cmpgt_epi16(vSaturationCheckMax, vPosLimit)))) {
result->saturated = 1;
score = 0;
end_query = 0;
end_ref = 0;
}
result->score = score;
result->end_query = end_query;
result->end_ref = end_ref;
parasail_free(pvE);
parasail_free(pvHLoad);
parasail_free(pvHStore);
return result;
}