Debian Codegraph @

hmmer/src/fm_sse.c

raw symbols SCIP
1 #include <p7_config.h>
2
3 #include <stdio.h>
4
5 #if defined eslENABLE_SSE
6 #include <xmmintrin.h> /* SSE */
7 #include <emmintrin.h> /* SSE2 */
8 #endif
9
10 #include "easel.h"
11 #include "esl_getopts.h"
12 #include "hmmer.h"
13
14 #if defined eslENABLE_SSE
15 int
16 fm_getbits_m128 (__m128i in, char *buf, int reverse)
17 {
18 byte_m128 new;
19 new.m128 = in;
20 int i,j;
21
22 for (i=0; i<16;i++) {
23
24 for (j=0; j<8; j++) {
25 if (reverse)
26 buf[9*i+j] = (new.bytes[i]>>j)&0x1 ? '1' : '0';
27 else
28 buf[9*i+(7-j)] = (new.bytes[i]>>j)&0x1 ? '1' : '0';
29 }
30 buf[9*i + 8] = ' ';
31 }
32 buf[143] = '\0';
33
34 return eslOK;
35 }
36
37 int
38 fm_print_m128 (__m128i in)
39 {
40 char str[144];
41 fm_getbits_m128(in, str, 0);
42 fprintf (stderr, "%s\n", str);
43 return eslOK;
44 }
45
46
47 int
48 fm_print_m128_rev (__m128i in)
49 {
50 char str[144];
51 fm_getbits_m128(in, str, 1);
52 fprintf (stderr, "%s\n", str);
53 return eslOK;
54 }
55 #endif //#if defined (eslENABLE_SSE)
56
57
58 /* Function: fm_initConfig()
59 * Purpose: Initialize vector masks used in SSE FMindex implementation
60 */
61 int
62 fm_configInit( FM_CFG *cfg, ESL_GETOPTS *go )
63 {
64 fm_initConfigGeneric(cfg, go);
65
66 #if defined (eslENABLE_SSE)
67 int i,j;
68 int trim_chunk_count;
69 int status;
70
71 cfg->fm_allones_v = _mm_set1_epi8(0xff);
72 cfg->fm_neg128_v = _mm_set1_epi8((int8_t) -128);
73 cfg->fm_zeros_v = _mm_set1_epi8((int8_t) 0x00); //00 00 00 00
74 cfg->fm_m0f = _mm_set1_epi8((int8_t) 0x0f); //00 00 11 11
75
76 if (cfg->meta->alph_type == fm_DNA) {
77 cfg->fm_m01 = _mm_set1_epi8((int8_t) 0x55); //01 01 01 01
78 cfg->fm_m11 = _mm_set1_epi8((int8_t) 0x03); //00 00 00 11
79 }
80 //set up an array of vectors, one for each character in the alphabet
81 cfg->fm_chars_v = NULL;
82 ESL_ALLOC (cfg->fm_chars_mem, cfg->meta->alph_size * sizeof(__m128) + 15 ); // +15 for manual 16-byte alignment, which matters for SIMD stuff
83 cfg->fm_chars_v = (__m128i *) (((unsigned long int)(cfg->fm_chars_mem) + 15) & (~0xf)); /* align vector memory on 16-byte boundaries */
84
85
86 for (i=0; i<cfg->meta->alph_size; i++) {
87 int8_t c = i;
88 if (cfg->meta->alph_type == fm_DNA) {
89 // need 4 copies per byte
90 c |= i<<2;
91 c |= i<<4;
92 c |= i<<6;
93 } //else, just leave it on the right-most bits
94
95 cfg->fm_chars_v[i] = _mm_set1_epi8(c);
96 }
97
98 /* this is a collection of masks used to clear off the left- or right- part
99 * of a register when we shouldn't be counting the whole thing
100 * Incrementally chew off the 1s in chunks of 2 (for DNA) or 4 (for DNA_full)
101 * from the right side, and stick each result into an element of a __m128 array
102 */
103 if (cfg->meta->alph_type == fm_DNA)
104 trim_chunk_count = 64; //2-bit steps
105 else // then cfg->meta->alph_type == fm_AMINO, we suppose
106 trim_chunk_count = 16; //8-bit steps
107
108 //chars_per_vector = 128/meta->charBits;
109 cfg->fm_masks_v = NULL;
110 cfg->fm_reverse_masks_v = NULL;
111 ESL_ALLOC (cfg->fm_masks_mem, (1+trim_chunk_count) *sizeof(__m128) + 15 ); // +15 for manual 16-byte alignment, which matters for SIMD stuff
112 cfg->fm_masks_v = (__m128i *) (((unsigned long int)(cfg->fm_masks_mem) + 15) & (~0xf)); /* align vector memory on 16-byte boundaries */
113
114 ESL_ALLOC (cfg->fm_reverse_masks_mem, (1+trim_chunk_count) *sizeof(__m128) + 15 ); // +15 for manual 16-byte alignment, which matters for SIMD stuff
115 cfg->fm_reverse_masks_v = (__m128i *) (((unsigned long int)(cfg->fm_reverse_masks_mem) + 15) & (~0xf)); /* align vector memory on 16-byte boundaries */
116
117 {
118 byte_m128 arr;
119 arr.m128 = cfg->fm_allones_v;
120
121 for (i=trim_chunk_count-1; i>0; i--) {
122 int byte_mask=0xff; //11 11 11 11
123 int byte_i = (i-1)/(trim_chunk_count/16);
124 if (cfg->meta->alph_type == fm_DNA) {
125 switch (i&0x03) {
126 case 1:
127 byte_mask = 0xc0; //11 00 00 00
128 break;
129 case 2:
130 byte_mask = 0xf0; //11 11 00 00
131 break;
132 case 3:
133 byte_mask = 0xfc; //11 11 11 00
134 break;
135 default:
136 break;
137 }
138 }
139
140 arr.bytes[byte_i] = byte_mask; //chew off the appropriate number of bits
141 for (j=byte_i+1; j<16; j++) {
142 arr.bytes[j] = 0x0;
143 }
144 cfg->fm_masks_v[i] = *(__m128i*)(&(arr.m128));
145 cfg->fm_reverse_masks_v[trim_chunk_count-i] = _mm_andnot_si128(cfg->fm_masks_v[i], cfg->fm_allones_v );
146
147 }
148 }
149 #endif // eslENABLE_SSE
150
151 /*
152 if (cfg->meta->alph_type == fm_DNA_full) {
153 cfg->fm_masks_v[16] = cfg->fm_allones_v;
154 cfg->fm_reverse_masks_v[16] = cfg->fm_allones_v;
155 }
156 */
157 return eslOK;
158
159 #if defined (eslENABLE_SSE)
160 ERROR:
161 if (cfg->fm_chars_mem) free(cfg->fm_chars_mem);
162 if (cfg->fm_masks_mem) free(cfg->fm_masks_mem);
163 if (cfg->fm_reverse_masks_mem) free(cfg->fm_reverse_masks_mem);
164
165 esl_fatal("Error allocating memory in initGlobals\n");
166 return eslFAIL;
167 #endif
168 }
169
170
171
172 /* Function: fm_getOccCount()
173 * Synopsis: Compute number of occurrences of c in BWT[1..pos]
174 *
175 * Purpose: Scan through the BWT to compute number of occurrence of c in BWT[0..pos],
176 * using SSE to scan 16 bytes-at-a-time.
177 *
178 * First, use checkpointed occurrence counts in the arrays occCnts_sb and occCnts_b.
179 * The checkpoint used is the one closest to pos, possibly requiring that counts be
180 * subtracted from the checkpointed total
181 *
182 * The counting method is SIMD, loading 16 bytes (32 or 64 chars, depending on
183 * alphabet) at a time into the vector co-processors, then counting occurrences. One
184 * constraint of this approach is that occCnts_b checkpoints must be spaced at least
185 * every 32 or 64 chars (16 bytes, in pressed format), and in multiples of 64/32, so
186 * that _mm_load_si128 calls appropriately meet 16-byte-alignment requirements. That's
187 * a reasonable expectation, as spacings of 256 or more seem to give the best speed,
188 * and certainly better space-utilization.
189 */
190 int
191 fm_getOccCount (const FM_DATA *fm, const FM_CFG *cfg, int pos, uint8_t c)
192 {
193 FM_METADATA *meta = cfg->meta;
194 int cnt = 0;
195 const int b_pos = (pos+1) / meta->freq_cnt_b ; //floor(pos/b_size) : the b count element preceding pos
196 const int cnt_mod_mask_b = meta->freq_cnt_b - 1; //used to compute the mod function
197 const int b_rel_pos = (pos+1) & cnt_mod_mask_b; // pos % b_size : how close is pos to the boundary corresponding to b_pos
198 int up_b = 2*b_rel_pos/meta->freq_cnt_b; //1 if pos is expected to be closer to the boundary of b_pos+1, 0 otherwise
199 int landmark = ((b_pos+up_b)*meta->freq_cnt_b) - 1 ;
200
201 if (landmark >= fm->N) { // special case: for a count in the final block, just count from the bottom
202 up_b = 0;
203 landmark = (b_pos*(meta->freq_cnt_b)) - 1 ;
204 }
205
206 #if defined (eslENABLE_SSE)
207 int i;
208 const uint16_t * occCnts_b = fm->occCnts_b;
209 const uint32_t * occCnts_sb = fm->occCnts_sb;
210 const int sb_pos = (pos+1) / meta->freq_cnt_sb; //floor(pos/sb_size) : the sb count element preceding pos
211
212
213 // get the cnt stored at the nearest checkpoint
214 cnt = FM_OCC_CNT(sb, sb_pos, c );
215
216 if (up_b)
217 cnt += FM_OCC_CNT(b, b_pos + 1, c ) ;
218 else if ( b_pos != sb_pos * (meta->freq_cnt_sb / meta->freq_cnt_b) )
219 cnt += FM_OCC_CNT(b, b_pos, c ) ;// b_pos has cumulative counts since the prior sb_pos - if sb_pos references the same count as b_pos, it'll doublecount
220
221 if ( landmark < fm->N || landmark == -1 ) {
222
223 const uint8_t * BWT = fm->BWT;
224
225
226 register __m128i c_v = *(cfg->fm_chars_v + c);
227 register __m128i BWT_v;
228 register __m128i tmp_v;
229 register __m128i tmp2_v;
230 register __m128i counts_v = cfg->fm_neg128_v; // set to -128, offset to allow each 8-bit int to hold up to 255.
231 // so effectively, can guarantee holding 128*16 = 2048.
232 // Since I count from left or right, whichever is closer, this means
233 // we can support an occ_b interval of up to 4096 with guarantee of
234 // correctness.
235 if (meta->alph_type == fm_DNA ) {
236
237 if (!up_b) { // count forward, adding
238 for (i=1+floor(landmark/4.0) ; i+15<( (pos+1)/4); i+=16) { // keep running until i begins a run that shouldn't all be counted
239 BWT_v = *(__m128i*)(BWT+i);
240 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
241 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v);
242 }
243
244 int remaining_cnt = pos + 1 - i*4 ;
245 if (remaining_cnt > 0) {
246 BWT_v = *(__m128i*)(BWT+i);
247 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
248 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
249 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v);
250 }
251
252 } else { // count backwards, subtracting
253 for (i=(landmark/4)-15 ; i>(pos/4); i-=16) {
254 BWT_v = *(__m128i*)(BWT+i);
255 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
256 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v);
257 }
258
259 int remaining_cnt = 64 - (pos + 1 - i*4);
260 if (remaining_cnt > 0) {
261 BWT_v = *(__m128i*)(BWT+i);
262 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
263 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
264 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v);
265 }
266 }
267 /*
268 } else if ( meta->alph_type == fm_DNA_full ) {
269
270 if (!up_b) { // count forward, adding
271 for (i=1+floor(landmark/2.0) ; i+15<( (pos+1)/2); i+=16) { // keep running until i begins a run that shouldn't all be counted
272 BWT_v = *(__m128i*)(BWT+i);
273 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
274 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v);
275 }
276 int remaining_cnt = pos + 1 - i*2 ;
277 if (remaining_cnt > 0) {
278 BWT_v = *(__m128i*)(BWT+i);
279 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
280 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + (remaining_cnt+1)/2)); // mask characters we don't want to count
281 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_masks_v + remaining_cnt/2));
282 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v);
283 }
284
285 } else { // count backwards, subtracting
286 for (i=(landmark/2)-15 ; i>(pos/2); i-=16) {
287 BWT_v = *(__m128i*)(BWT+i);
288 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
289 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v);
290 }
291
292 int remaining_cnt = 32 - (pos + 1 - i*2);
293 if (remaining_cnt > 0) {
294 BWT_v = *(__m128i*)(BWT+i);
295 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
296 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt/2)); // mask characters we don't want to count
297 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_reverse_masks_v + (remaining_cnt+1)/2));
298 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v);
299 }
300 }
301 */
302 } else { //amino
303
304 if (!up_b) { // count forward, adding
305 for (i=1+landmark ; i+15<(pos+1); i+=16) { // keep running until i begins a run that shouldn't all be counted
306 BWT_v = *(__m128i*)(BWT+i);
307 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v); // each byte is all 1s if matching, all zeros otherwise
308 counts_v = _mm_subs_epi8(counts_v, BWT_v); // adds 1 for each matching byte (subtracting negative 1)
309 }
310 int remaining_cnt = pos + 1 - i ;
311
312 if (remaining_cnt > 0) {
313 BWT_v = *(__m128i*)(BWT+i);
314 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v);
315 BWT_v = _mm_and_si128(BWT_v, *(cfg->fm_masks_v + remaining_cnt));// mask characters we don't want to count
316 counts_v = _mm_subs_epi8(counts_v, BWT_v);
317 }
318 } else { // count backwards, subtracting
319
320 for (i=landmark-15 ; i>pos; i-=16) {
321 BWT_v = *(__m128i*)(BWT+i);
322 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v); // each byte is all 1s if matching, all zeros otherwise
323 counts_v = _mm_subs_epi8(counts_v, BWT_v); // adds 1 for each matching byte (subtracting negative 1)
324 }
325 int remaining_cnt = 16 - (pos + 1 - i);
326 if (remaining_cnt > 0) {
327 BWT_v = *(__m128i*)(BWT+i);
328 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v);
329 BWT_v = _mm_and_si128(BWT_v, *(cfg->fm_reverse_masks_v + remaining_cnt));// mask characters we don't want to count
330 //tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_reverse_masks_v + (remaining_cnt+1)/2));
331 counts_v = _mm_subs_epi8(counts_v, BWT_v);
332 }
333 }
334 }
335
336 counts_v = _mm_xor_si128(counts_v, cfg->fm_neg128_v); //counts are stored in signed bytes, base -128. Move them to unsigned bytes
337 FM_GATHER_8BIT_COUNTS(counts_v,counts_v,counts_v);
338
339 cnt += ( up_b == 1 ? -1 : 1) * ( _mm_extract_epi16(counts_v, 0) );
340 }
341
342 if (c==0 && pos >= fm->term_loc) { // I overcounted 'A' by one, because '$' was replaced with an 'A'
343 cnt--;
344 }
345
346 #endif //#if defined (eslENABLE_SSE)
347
348
349 return cnt ;
350
351 }
352
353
354
355 /* Function: fm_getOccCountLT()
356 * Synopsis: Compute number of occurrences of characters with value <c in BWT[1..pos]
357 *
358 * Purpose: Scan through the BWT to compute number of occurrences of characters with value <c
359 * in BWT[0..pos], using SSE to scan 16 bytes-at-a-time.
360 *
361 * First, use checkpointed occurrence counts in the arrays occCnts_sb and occCnts_b.
362 * The checkpoint used is the one closest to pos, possibly requiring that counts be
363 * subtracted from the checkpointed total
364 *
365 * The counting method is SIMD, loading 16 bytes (32 or 64 chars, depending on
366 * alphabet) at a time into the vector co-processors, then counting occurrences. One
367 * constraint of this approach is that occCnts_b checkpoints must be spaced at least
368 * every 32 or 64 chars (16 bytes, in pressed format), and in multiples of 64/32, so
369 * that _mm_load_si128 calls appropriately meet 16-byte-alignment requirements. That's
370 * a reasonable expectation, as spacings of 256 or more seem to give the best speed,
371 * and certainly better space-utilization.
372 *
373 */
374 int
375 fm_getOccCountLT (const FM_DATA *fm, const FM_CFG *cfg, int pos, uint8_t c, uint32_t *cnteq, uint32_t *cntlt)
376 {
377 FM_METADATA *meta = cfg->meta;
378 int i;
379 const uint16_t * occCnts_b = fm->occCnts_b;
380 const uint32_t * occCnts_sb = fm->occCnts_sb;
381 const int b_pos = (pos+1) / meta->freq_cnt_b; //floor(pos/b_size) : the b count element preceding pos
382 const int sb_pos = (pos+1) / meta->freq_cnt_sb; //floor(pos/sb_size) : the sb count element preceding pos
383
384 const int b_rel_pos = (pos+1) % meta->freq_cnt_b; // how close is pos to the boundary corresponding to b_pos
385 int up_b = 2*b_rel_pos/meta->freq_cnt_b; //1 if pos is expected to be closer to the boundary of b_pos+1, 0 otherwise
386 int landmark = ((b_pos+up_b)*(meta->freq_cnt_b)) - 1 ;
387
388
389 if (landmark >= fm->N) { // special case: for a count in the final block, just count from the bottom
390 up_b = 0;
391 landmark = (b_pos*(meta->freq_cnt_b)) - 1 ;
392 }
393
394 // get the cnt stored at the nearest checkpoint
395 *cntlt = 0;
396 *cnteq = FM_OCC_CNT(sb, sb_pos, c );
397 for (i=0; i<c; i++)
398 *cntlt += FM_OCC_CNT(sb, sb_pos, i );
399
400 if (up_b) {
401 *cnteq += FM_OCC_CNT(b, b_pos + 1, c ) ;
402 for (i=0; i<c; i++)
403 *cntlt += FM_OCC_CNT(b, b_pos + 1, i ) ;
404 } else if ( b_pos != sb_pos * (meta->freq_cnt_sb / meta->freq_cnt_b)) {
405 *cnteq += FM_OCC_CNT(b, b_pos, c ) ;// b_pos has cumulative counts since the prior sb_pos - if sb_pos references the same count as b_pos, it'll doublecount
406 for (i=0; i<c; i++)
407 *cntlt += FM_OCC_CNT(b, b_pos, i ) ;
408 }
409
410 #if defined (eslENABLE_SSE)
411 int j;
412
413 if ( landmark < fm->N - 1 || landmark == -1 ) {
414
415 const uint8_t * BWT = fm->BWT;
416
417
418 register __m128i c_v = cfg->fm_zeros_v;
419 register __m128i BWT_v;
420 register __m128i tmp_v;
421 register __m128i tmp2_v;
422 register __m128i counts_v_lt = cfg->fm_neg128_v;
423 register __m128i counts_v_eq = cfg->fm_neg128_v; // set to -128, offset to allow each 8-bit int to hold up to 255.
424 // so effectively, can guarantee holding 128*16 = 2048.
425 // Since I count from left or right, whichever is closer, this means
426 // we can support an occ_b interval of up to 4096 with guarantee of
427 // correctness.
428 if (meta->alph_type == fm_DNA ) {
429
430 /* TODO: For 4-bit characters, it's easy to develop an alternative SSE function that will count
431 * instances <c in the same time as counting matches. I haven't yet identified a similar
432 * modification to the 2-bit counting. Instead, I just loop over the FM_MATCH_2BIT macro
433 * for each character j<c. The expected # of such iterations is (0+1+2+3)/4 = 1.5 ...
434 * since much of the run time is in loading data from memory/cache, I don't expect
435 * this to be a major problem for speed, but improving the less-than counting is still
436 * desirable.
437 */
438
439
440 if (!up_b) { // count forward, adding
441 for (i=1+floor(landmark/4.0) ; i+15<( (pos+1)/4); i+=16) { // keep running until i begins a run that shouldn't all be counted
442 BWT_v = *(__m128i*)(BWT+i);
443 for (j=0; j<c; j++) {
444 c_v = *(cfg->fm_chars_v + j);
445 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
446 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_lt);
447 }
448 c_v = *(cfg->fm_chars_v + c);
449 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
450 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_eq);
451
452 }
453
454 int remaining_cnt = pos + 1 - i*4 ;
455 if (remaining_cnt > 0) {
456 BWT_v = *(__m128i*)(BWT+i);
457 for (j=0; j<c; j++) {
458 c_v = *(cfg->fm_chars_v + j);
459 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
460 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
461 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_lt);
462 }
463 c_v = *(cfg->fm_chars_v + c);
464 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
465 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
466 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_eq);
467
468 }
469
470 } else { // count backwards, subtracting
471 for (i=(landmark/4)-15 ; i>(pos/4); i-=16) {
472 BWT_v = *(__m128i*)(BWT+i);
473 for (j=0; j<c; j++) {
474 c_v = *(cfg->fm_chars_v + j);
475 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
476 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_lt);
477 }
478 c_v = *(cfg->fm_chars_v + c);
479 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
480 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_eq);
481
482 }
483
484 int remaining_cnt = 64 - (pos + 1 - i*4);
485 if (remaining_cnt > 0) {
486 BWT_v = *(__m128i*)(BWT+i);
487 for (j=0; j<c; j++) {
488 c_v = *(cfg->fm_chars_v + j);
489 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
490 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
491 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_lt);
492 }
493 c_v = *(cfg->fm_chars_v + c);
494 FM_MATCH_2BIT(BWT_v, c_v, tmp_v, tmp2_v, tmp_v);
495 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt)); // leaves only the remaining_cnt chars in the array
496 FM_COUNT_2BIT(tmp_v, tmp2_v, counts_v_eq);
497 }
498 }
499 /*
500 } else if ( meta->alph_type == fm_DNA_full) {
501 c_v = *(cfg->fm_chars_v + c);
502
503 if (!up_b) { // count forward, adding
504 for (i=1+floor(landmark/2.0) ; i+15<( (pos+1)/2); i+=16) { // keep running until i begins a run that shouldn't all be counted
505 BWT_v = *(__m128i*)(BWT+i);
506 FM_LT_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
507 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_lt);
508 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
509 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_eq);
510
511 }
512 int remaining_cnt = pos + 1 - i*2 ;
513 if (remaining_cnt > 0) {
514 BWT_v = *(__m128i*)(BWT+i);
515 FM_LT_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
516 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + (remaining_cnt+1)/2)); // mask characters we don't want to count
517 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_masks_v + remaining_cnt/2));
518 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_lt);
519
520 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
521 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + (remaining_cnt+1)/2)); // mask characters we don't want to count
522 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_masks_v + remaining_cnt/2));
523 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_eq);
524
525 }
526
527 } else { // count backwards, subtracting
528 for (i=(landmark/2)-15 ; i>(pos/2); i-=16) {
529 BWT_v = *(__m128i*)(BWT+i);
530 FM_LT_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
531 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_lt);
532 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
533 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_eq);
534 }
535
536 int remaining_cnt = 32 - (pos + 1 - i*2);
537 if (remaining_cnt > 0) {
538 BWT_v = *(__m128i*)(BWT+i);
539 FM_LT_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
540 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt/2)); // mask characters we don't want to count
541 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_reverse_masks_v + (remaining_cnt+1)/2));
542 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_lt);
543
544 FM_MATCH_4BIT(BWT_v, c_v, tmp_v, tmp2_v);
545 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt/2)); // mask characters we don't want to count
546 tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_reverse_masks_v + (remaining_cnt+1)/2));
547 FM_COUNT_4BIT(tmp_v, tmp2_v, counts_v_eq);
548
549 }
550 }
551 */
552 } else { //amino
553 if (!up_b) { // count forward, adding
554 for (i=1+landmark ; i+15<(pos+1); i+=16) { // keep running until i begins a run that shouldn't all be counted
555 BWT_v = *(__m128i*)(BWT+i);
556 tmp_v = _mm_cmplt_epi8(BWT_v, c_v); // each byte is all 1s if leq, all zeros otherwise
557 counts_v_lt = _mm_subs_epi8(counts_v_lt, tmp_v); // adds 1 for each matching byte (subtracting negative 1)
558 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v); // each byte is all 1s if eq, all zeros otherwise
559 counts_v_eq = _mm_subs_epi8(counts_v_eq, BWT_v);
560 }
561 int remaining_cnt = pos + 1 - i ;
562 if (remaining_cnt > 0) {
563 BWT_v = *(__m128i*)(BWT+i);
564 tmp_v = _mm_cmplt_epi8(BWT_v, c_v); // each byte is all 1s if leq, all zeros otherwise
565 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_masks_v + remaining_cnt/4));
566 counts_v_lt = _mm_subs_epi8(counts_v_lt, tmp_v); // adds 1 for each matching byte (subtracting negative 1)
567
568 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v);
569 BWT_v = _mm_and_si128(BWT_v, *(cfg->fm_masks_v + remaining_cnt/4));// mask characters we don't want to count
570 counts_v_eq = _mm_subs_epi8(counts_v_eq, BWT_v);
571 }
572
573 } else { // count backwards, subtracting
574 for (i=landmark-15 ; i>pos; i-=16) {
575 BWT_v = *(__m128i*)(BWT+i);
576 tmp_v = _mm_cmplt_epi8(BWT_v, c_v); // each byte is all 1s if leq, all zeros otherwise
577 counts_v_lt = _mm_subs_epi8(counts_v_lt, tmp_v); // adds 1 for each matching byte (subtracting negative 1)
578 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v); // each byte is all 1s if eq, all zeros otherwise
579 counts_v_eq = _mm_subs_epi8(counts_v_eq, BWT_v);
580 }
581
582 int remaining_cnt = 16 - (pos + 1 - i);
583 if (remaining_cnt > 0) {
584 BWT_v = *(__m128i*)(BWT+i);
585 tmp_v = _mm_cmplt_epi8(BWT_v, c_v); // each byte is all 1s if leq, all zeros otherwise
586 tmp_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt/4));
587 counts_v_lt = _mm_subs_epi8(counts_v_lt, tmp_v); // adds 1 for each matching byte (subtracting negative 1)
588
589 BWT_v = _mm_cmpeq_epi8(BWT_v, c_v);
590 BWT_v = _mm_and_si128(tmp_v, *(cfg->fm_reverse_masks_v + remaining_cnt/4));// mask characters we don't want to count
591 //tmp2_v = _mm_and_si128(tmp2_v, *(cfg->fm_reverse_masks_v + (remaining_cnt+1)/2));
592 counts_v_eq = _mm_subs_epi8(counts_v_eq, BWT_v);
593 }
594 }
595
596 }
597
598 if (c>0) {
599 counts_v_lt = _mm_xor_si128(counts_v_lt, cfg->fm_neg128_v); //counts are stored in signed bytes, base -128. Move them to unsigned bytes
600 FM_GATHER_8BIT_COUNTS(counts_v_lt,counts_v_lt,counts_v_lt);
601 (*cntlt) += ( up_b == 1 ? -1 : 1) * ( _mm_extract_epi16(counts_v_lt, 0) );
602 }
603
604 counts_v_eq = _mm_xor_si128(counts_v_eq, cfg->fm_neg128_v);
605 FM_GATHER_8BIT_COUNTS(counts_v_eq,counts_v_eq,counts_v_eq);
606 (*cnteq) += ( up_b == 1 ? -1 : 1) * ( _mm_extract_epi16(counts_v_eq, 0) );
607 }
608
609
610 if ( pos >= fm->term_loc) {
611 if (c == 0) { // deal with the fact that '$' was replaced with an 'A'
612 (*cnteq)--; // I overcounted 'A' by one
613 (*cntlt) = 1; // '$' is lexicographically lower than 'A', but I didn't count it in the method above
614 }
615 }
616
617 #endif //#if defined (eslENABLE_SSE)
618
619 return eslOK ;
620
621 }
622