| 1 | /* The SSV filter implementation; SSE version. |
| 2 | * |
| 3 | * Contents: |
| 4 | * 1. Introduction |
| 5 | * 2. p7_SSVFilter() implementation |
| 6 | * |
| 7 | * Bjarne Knudsen, CLC Bio |
| 8 | */ |
| 9 | |
| 10 | /***************************************************************** |
| 11 | * 1. Introduction |
| 12 | *****************************************************************/ |
| 13 | |
| 14 | /* Here is a description of the major ideas going into this |
| 15 | * implementation of the SSV filter. |
| 16 | * |
| 17 | * |
| 18 | * REMOVING THE J STATE |
| 19 | * ==================== |
| 20 | * |
| 21 | * The original MSV filter allows use of the J state to chain together |
| 22 | * multiple matches in different diagonals. Thus, a full match can |
| 23 | * consist of diagonal match followed by the J state and then another |
| 24 | * diagonal match later in the sequence. Going through the J state |
| 25 | * has a certain price so for the full match to contain two different |
| 26 | * diagonal matches connected by the J state, each of the individual |
| 27 | * diagonal matches must score higher than the cost of going through |
| 28 | * the J state. |
| 29 | * |
| 30 | * It turns out that even the best match in a model-sequence |
| 31 | * comparison rarely scores higher than the cost of going through the |
| 32 | * J state. This is the basis of the idea used here, which is to |
| 33 | * completely ignore the J state. To avoid this leading to false |
| 34 | * negatives, we check the resulting maximum score against the cost of |
| 35 | * the going through the J state. In the rare cases where the J state |
| 36 | * may in fact have been used, we return eslNORESULT. This indicates |
| 37 | * to the original J state that it should recalculate the score. |
| 38 | * |
| 39 | * Since removing the J state allows significant improvements in |
| 40 | * speed, the extra overhead of having to go through the original MSV |
| 41 | * filter in about 1% of the cases is not a problem. |
| 42 | * |
| 43 | * Note that for the score to actually be different, we need two |
| 44 | * diagonals to have a high scoring match, but we cannot easily check |
| 45 | * for that. Thus, oftentimes the re-calculated score in the original |
| 46 | * MSV filter will be the same as without the J state. |
| 47 | * |
| 48 | * The code governing the use of the J state in the original filter is: |
| 49 | * |
| 50 | * xEv = _mm_subs_epu8(xEv, tecv); |
| 51 | * xJv = _mm_max_epu8(xJv,xEv); |
| 52 | * xBv = _mm_max_epu8(basev, xJv); |
| 53 | * |
| 54 | * So for an xE value to be high enough to affect xJ, the following |
| 55 | * inequality must be true: |
| 56 | * |
| 57 | * xJ = xE - om->tec_b > om->base_b |
| 58 | * |
| 59 | * We defer this check until the final maximal xE value has been |
| 60 | * calculated. If the above holds true, we return eslNORESULT. |
| 61 | * |
| 62 | * Since the J state is removed, the xBv vector is constant, so we can |
| 63 | * set it once an for all to a vector where all entries are: |
| 64 | * |
| 65 | * om->base_b - om->tjb_b - om->tbm_b |
| 66 | * |
| 67 | * But see the following section for why this is changed for other |
| 68 | * reasons. |
| 69 | * |
| 70 | * |
| 71 | * INTERNAL LOOP ADJUSTMENT AND IMPLICATIONS |
| 72 | * ========================================= |
| 73 | * |
| 74 | * The following assumes that we have already gotten rid of the J |
| 75 | * state. |
| 76 | * |
| 77 | * Here is an analysis of what is going on in the central loop. The |
| 78 | * original code is: |
| 79 | * |
| 80 | * 1: sv = _mm_max_epu8(sv, xBv); |
| 81 | * 2: sv = _mm_adds_epu8(sv, biasv); |
| 82 | * 3: sv = _mm_subs_epu8(sv, *rsc); rsc++; |
| 83 | * 4: xEv = _mm_max_epu8(xEv, sv); |
| 84 | * |
| 85 | * Here is a line by line description: |
| 86 | * |
| 87 | * 1: If sv is below xBv, it is set to xBv. xBv is the begin score, |
| 88 | * which is om->base_b - om->tjb_b - om->tbm_b. |
| 89 | * |
| 90 | * 2: The bias (om->bias_b) is added. This is done since we are |
| 91 | * using unsigned numbers and the score can be both positive and |
| 92 | * negative. The bias is the negative of the highest value the |
| 93 | * real match scores may have. |
| 94 | * |
| 95 | * 3: The match score (and bias) is subtracted. The subtracted score |
| 96 | * must be positive since we using are unsigned bytes, thus the |
| 97 | * score subtracted here is the one adjusted for bias. We also |
| 98 | * progress to the next match score (rsc++). |
| 99 | * |
| 100 | * 4: The global maximum is updated. |
| 101 | * |
| 102 | * When the everything has been traversed, xEv is checked for a number |
| 103 | * of conditions. First, the maximum value is extracted to xE, though. |
| 104 | * |
| 105 | * if xE is greater than or equal to 255 - om->bias_b, there may have |
| 106 | * been an overflow, and the result is reported as infinite. |
| 107 | * |
| 108 | * Since we ignored the J state, we have to check whether it could |
| 109 | * potentially have been used, possibly resulting in a higher |
| 110 | * score. This is the case if (xE - om->tec_b) > om->base_b. The left |
| 111 | * side of the check is the highest score that xJ could have |
| 112 | * attained. In the original MSV filter this score would only have |
| 113 | * affected the begin scores if this xJ value exceeded |
| 114 | * om->base_b. This explains the check. |
| 115 | * |
| 116 | * Now, we optimize this internal loop by using two ideas: |
| 117 | * |
| 118 | * A: Get rid of line 1 by using saturation. This can be done |
| 119 | * because xBv is a constant vector after getting rid of the J |
| 120 | * state. |
| 121 | * |
| 122 | * B: Combine lines 2 and 3 by using a single signed subtraction |
| 123 | * instead of an unsigned addition followed by an unsigned |
| 124 | * subtraction. |
| 125 | * |
| 126 | * It is a challenge that SSE2 does not have a signed byte max |
| 127 | * operation, yet we need to subtract a signed byte in idea B. First |
| 128 | * the new code, then the explanation: |
| 129 | * |
| 130 | * sv = _mm_subs_epi8(sv, *rsc); rsc++; |
| 131 | * xEv = _mm_max_epu8(xEv, sv); |
| 132 | * |
| 133 | * The last line is unchanged, i.e. the overall max is still done as |
| 134 | * an unsigned maximum. The subtraction is saturated to satisfy idea A |
| 135 | * and it is signed to satisfy idea B. |
| 136 | * |
| 137 | * To make the saturation work in the lower end of the scale, the |
| 138 | * begin scores have to equal signed -128 which is the same as |
| 139 | * unsigned 128, or a bit value of 10000000. Thus, we basically shift |
| 140 | * the calculation with a (signed) value of -(om->base_b - om->tjb_b - |
| 141 | * om->tbm_b + 128), which takes the original begin value to -128. |
| 142 | * |
| 143 | * Since we are using an unsigned maximum, the signed saturation at |
| 144 | * +127 will not work. Thus, if the score gets high enough, we are |
| 145 | * going to pass from signed negative values to non-negative values |
| 146 | * without any saturation kicking in. In the unsigned domain this |
| 147 | * basically constitutes an overflow from 255 to 0. This means that we |
| 148 | * may miss a high score of it crosses this boundary. |
| 149 | * |
| 150 | * The highest positive effect that the subtraction can have is to add |
| 151 | * om->bias_b, since this is the highest real match score. So only |
| 152 | * scores strictly higher than 255 - om->bias_b in the unsigned domain |
| 153 | * may cause an overflow. In the signed domain this corresponds to -1 |
| 154 | * - om->bias_b. |
| 155 | * |
| 156 | * When the calculation is all done, we may check xE against this |
| 157 | * boundary to determine if an overflow might have occurred. The other |
| 158 | * thing to consider is the check for whether the J state may have |
| 159 | * been used. This happens when: |
| 160 | * |
| 161 | * (xE + (om->base_b - om->tjb_b - om->tbm_b - 128) - om->tec_b) |
| 162 | * > om->base_b. |
| 163 | * |
| 164 | * <=> xE > om->tjb_b + om->tbm_b + om->tec_b + 128 |
| 165 | * |
| 166 | * Thus, we have these two checks: |
| 167 | * |
| 168 | * xE >= 255 - om->bias_b (possible overflow) |
| 169 | * |
| 170 | * xE > om->tjb_b + om->tbm_b + om->tec_b - 128 (possible J state) |
| 171 | * |
| 172 | * To avoid having to call too many false positives, we do not want |
| 173 | * the overflow to occur before the J state becomes possible. This |
| 174 | * mean that we want: |
| 175 | * |
| 176 | * (Overflow => J state) |
| 177 | * |
| 178 | * <=> 255 - om->bias_b > om->tjb_b + om->tbm_b + om->tec_b + 128 |
| 179 | * |
| 180 | * <=> om->tjb_b + om->tbm_b + om->tec_b + om->bias_b < 127 |
| 181 | * |
| 182 | * The worst case bias is 19, om->tec_B is 3 for a sequence length of |
| 183 | * L and a model length of M, we have: |
| 184 | * |
| 185 | * om->tjb_b = 3 * logf(3 / (L + 3)) |
| 186 | * om->tbm_b = 3 * logf(2 / (M * (M + 1))) |
| 187 | * |
| 188 | * So if the sequence length is L = 1,000,000, the longest possible |
| 189 | * model where the above holds true is M = 482. If the model size is M |
| 190 | * = 2,295 (the largest in Pfam 23.0), the longest sequence length |
| 191 | * where the condition is true is L = 43,786. So, the condition is not |
| 192 | * always true, but typically, it is. And, importantly, it can be |
| 193 | * checked. |
| 194 | * |
| 195 | * A final thing to consider is what to do on an overflow. Since we |
| 196 | * shifted the baseline for the calculation, the question is if an |
| 197 | * overflow is necessarily going to happen in the original MSV |
| 198 | * filter. This is true when our baseline as no higher than the |
| 199 | * original MSV filter baseline. Thus, when the following holds we |
| 200 | * know that an overflow will occur for the original filter: |
| 201 | * |
| 202 | * om->base_b - om->tjb_b - om->tbm_b >= 128 |
| 203 | * |
| 204 | * If it does not hold, we are not sure what the true result is and we |
| 205 | * have to indicate that in the return value. |
| 206 | * |
| 207 | * Since we perform a single signed subtraction instead of an unsigned |
| 208 | * addition followed by in unsigned subtraction, a new set of match |
| 209 | * scores have been introduced in the P7_OPROFILE structure. These are |
| 210 | * called sb where the originals are rb. |
| 211 | * |
| 212 | * |
| 213 | * EXPLANATION OF THE CODE |
| 214 | * ======================= |
| 215 | * |
| 216 | * The basic idea is to traverse the sequence while analyzing only |
| 217 | * enough diagonals that they may residue in registers rather than |
| 218 | * memory. This may require several traversals of the sequence, but |
| 219 | * this is still worth it due to reduced memory access. |
| 220 | * |
| 221 | * So we have a basic calculation concept where we fill out some |
| 222 | * number of adjacent striped diagonal vectors throughout the whole |
| 223 | * sequence. Consider a simple case where we have two registers, A and |
| 224 | * B and they each have only two fields instead of 16. In one sweep of |
| 225 | * a sequence we calculate the following matrix cells: |
| 226 | * |
| 227 | * | BA BA BA BA BA BA BA |
| 228 | * | BA BA BA BA BA BA BA |
| 229 | * |BA BA BA BA BA BA BA |
| 230 | * H |A BA BA BA BA BA BA B |
| 231 | * M | BA BA BA BA BA BA BA |
| 232 | * M | BA BA BA BA BA BA BA |
| 233 | * |BA BA BA BA BA BA BA |
| 234 | * |A BA BA BA BA BA BA B |
| 235 | * ---------------------------- |
| 236 | * Sequence |
| 237 | * |
| 238 | * When the top entry in one of the vectors hits the top, the vector |
| 239 | * must be left shifted to be ready for the next column. This first |
| 240 | * happens to the last vector (B), then in the following round to the |
| 241 | * first vector (A). |
| 242 | * |
| 243 | * This means that the sweep contains two different phases: one where |
| 244 | * vectors are being moved without shifting and then a phase where the |
| 245 | * vectors are being shifted one by one until the have all been |
| 246 | * shifted. If we have Q sets of 16 diagonal and we have w registers |
| 247 | * in use, the first phase takes Q - w rounds and the second phase |
| 248 | * takes w rounds and we are back where we started. This is done until |
| 249 | * the sequence ends. |
| 250 | * |
| 251 | * After having done this, we do another sweep, where we calculate the |
| 252 | * remaining cells: |
| 253 | * |
| 254 | * |BA BA BA BA BA BA BA |
| 255 | * |A BA BA BA BA BA BA B |
| 256 | * | BA BA BA BA BA BA BA |
| 257 | * H | BA BA BA BA BA BA BA |
| 258 | * M |BA BA BA BA BA BA BA |
| 259 | * M |A BA BA BA BA BA BA B |
| 260 | * | BA BA BA BA BA BA BA |
| 261 | * | BA BA BA BA BA BA BA |
| 262 | * ---------------------------- |
| 263 | * Sequence |
| 264 | * |
| 265 | * This sweep is identical to the first, except there is an offset to |
| 266 | * the starting point. We call this offset q which is 2 in this case |
| 267 | * and 0 above). |
| 268 | * |
| 269 | * Apart from the model, sequence, etc., the core calculation has two |
| 270 | * parameters: w and q. If we have three registers and Q = 8, we do |
| 271 | * three sweeps: |
| 272 | * |
| 273 | * sweep 1: q = 0, w = 3 |
| 274 | * sweep 2: q = 3, w = 3 |
| 275 | * sweep 3: q = 6, w = 2 |
| 276 | * |
| 277 | * This covers all diagonals and we are done. |
| 278 | * |
| 279 | * To make the compiler use registers as much as possible, we have to |
| 280 | * be quite specific about what is going on, so we have to make a |
| 281 | * function for each value of w. Since 64 bit machines have 16 xmm |
| 282 | * registers, we need quite a few of these functions. It is also |
| 283 | * possible that some of the diagonals actually end up in memory while |
| 284 | * retaining high performance since a few scattered memory accesses |
| 285 | * are not going to slow things down. |
| 286 | * |
| 287 | * To make the code maintainable, we cannot write out all these |
| 288 | * functions. Instead the are defined via macros. So a function |
| 289 | * definition may look like this: |
| 290 | * |
| 291 | * __m128i calc_band_6(ESL_DSQ *dsq, int L, P7_OPROFILE *om, int q, __m128i beginv, __m128i xEv) |
| 292 | * { |
| 293 | * CALC(RESET_6, STEP_BANDS_6, CONVERT_6, 6) |
| 294 | * } |
| 295 | * |
| 296 | * The parameters are the sequence, its length, the model, the q |
| 297 | * value, a begin vector and the max vector. The return value is the |
| 298 | * updated max vector. The whole body of the function is defined as a |
| 299 | * macro with parameters that are themselves expanded macros (apart |
| 300 | * from the last parameter). |
| 301 | * |
| 302 | * The RESET macro defines and resets the xmm variables in the |
| 303 | * function. It is defined recursively: |
| 304 | * |
| 305 | * #define RESET_1() |
| 306 | * register __m128i sv00 = beginv; |
| 307 | * |
| 308 | * #define RESET_2() |
| 309 | * RESET_1() |
| 310 | * register __m128i sv01 = beginv; |
| 311 | * |
| 312 | * #define RESET_3() |
| 313 | * RESET_2() |
| 314 | * register __m128i sv02 = beginv; |
| 315 | * |
| 316 | * So the variables holding the scores for the diagonals are called |
| 317 | * sv00, sv01, etc. |
| 318 | * |
| 319 | * The next macro is STEP_BANDS, which moves the diagonals. Again, |
| 320 | * this is a recursively defined macro: |
| 321 | * |
| 322 | * #define STEP_BANDS_1() |
| 323 | * STEP_SINGLE(sv00) |
| 324 | * |
| 325 | * #define STEP_BANDS_2() |
| 326 | * STEP_BANDS_1() |
| 327 | * STEP_SINGLE(sv01) |
| 328 | * |
| 329 | * #define STEP_BANDS_3() |
| 330 | * STEP_BANDS_2() |
| 331 | * STEP_SINGLE(sv02) |
| 332 | * |
| 333 | * So we end up using STEP_SINGLE on each vector. This is where the |
| 334 | * central calculation is done as described above: |
| 335 | * |
| 336 | * #define STEP_SINGLE(sv) |
| 337 | * sv = _mm_subs_epi8(sv, *rsc); rsc++; |
| 338 | * xEv = _mm_max_epu8(xEv, sv); |
| 339 | * |
| 340 | * The CONVERT macro handles the second phase mentioned above where |
| 341 | * the vectors have to be shifted. This is yet another recursive |
| 342 | * macro: |
| 343 | * |
| 344 | * #define CONVERT_1(step, LENGTH_CHECK, label) |
| 345 | * CONVERT_STEP(step, LENGTH_CHECK, label, sv00, Q - 1) |
| 346 | * |
| 347 | * #define CONVERT_2(step, LENGTH_CHECK, label) |
| 348 | * CONVERT_STEP(step, LENGTH_CHECK, label, sv01, Q - 2) |
| 349 | * CONVERT_1(step, LENGTH_CHECK, label) |
| 350 | * |
| 351 | * #define CONVERT_3(step, LENGTH_CHECK, label) |
| 352 | * CONVERT_STEP(step, LENGTH_CHECK, label, sv02, Q - 3) |
| 353 | * CONVERT_2(step, LENGTH_CHECK, label) |
| 354 | * |
| 355 | * Here, CONVERT_STEP ends up being called on each vector in reverse |
| 356 | * order. It does the following: |
| 357 | * |
| 358 | * #define CONVERT_STEP(step, LENGTH_CHECK, label, sv, pos) |
| 359 | * length_check(label) |
| 360 | * rsc = om->sbv[dsq[i]] + pos; |
| 361 | * step() |
| 362 | * sv = _mm_slli_si128(sv, 1); |
| 363 | * sv = _mm_or_si128(sv, beginv); |
| 364 | * i++; |
| 365 | * |
| 366 | * First a check is made. This is sometimes used to check whether the |
| 367 | * sequence is done. Then the match score pointer is set. After this, |
| 368 | * STEP_BANDS is called using the step parameter of this |
| 369 | * macro. Finally one vector is shifted and or'ed with the begin |
| 370 | * vector of (128, 128, ... ). This ensures that the zero that was |
| 371 | * shifted in is converted to the needed base line of 128. Other |
| 372 | * entries are not significantly affected by this since either their |
| 373 | * most significant bit is already set or we already had an overflow |
| 374 | * and it does not matter. |
| 375 | * |
| 376 | * Notice that the CONVERT macro ends up stepping the diagonals w |
| 377 | * times, so it handles the whole of phase two. Note also that the |
| 378 | * macro may let rsc overflow since it does not reset rsc after a |
| 379 | * shift operation. This is handled by extending the match score array |
| 380 | * in the P7_OPROFILE by MAX_BANDS - 1 = 17 as defined by the p7O_EXTRA_SB |
| 381 | * constant in that file. |
| 382 | * |
| 383 | * The only macro remaining is the CALC macro which just contains the |
| 384 | * overall function for going through the various phases. Due to the |
| 385 | * starting offset (q), the first Q - q sequence positions have to be |
| 386 | * handled separately. After this follows a number of blocks of length |
| 387 | * Q where we can be efficient and not do a check of whether the |
| 388 | * sequence stops (the NO_CHECK macro indicates this). Finally, at the |
| 389 | * end of the sequence we have to be careful and stop at the right |
| 390 | * time, again using LENGTH_CHECK. |
| 391 | * |
| 392 | * Even though the code is only around 500 lines, it expands to a |
| 393 | * fairly large file when the macros are parsed. For example, |
| 394 | * _mm_subs_epi8() is called 6,840 times even though it is only |
| 395 | * present once in this file. The object file is still not |
| 396 | * ridiculously large. |
| 397 | * |
| 398 | * To better see what is going on, run the preprocessor on this file: |
| 399 | * |
| 400 | * gcc -E ssvfilter.c | sed 's/[;:]/&\n/g' | less |
| 401 | * |
| 402 | * Ignore the warnings and go look for the calc_band_2 function. |
| 403 | * |
| 404 | */ |
| 405 | |
| 406 | #include <p7_config.h> |
| 407 | |
| 408 | #include <math.h> |
| 409 | |
| 410 | #include <xmmintrin.h> /* SSE */ |
| 411 | #include <emmintrin.h> /* SSE2 */ |
| 412 | |
| 413 | #include "easel.h" |
| 414 | #include "esl_sse.h" |
| 415 | |
| 416 | #include "hmmer.h" |
| 417 | #include "impl_sse.h" |
| 418 | |
| 419 | /* Note that some ifdefs below has to be changed if these values are |
| 420 | changed. These values are chosen based on some simple speed |
| 421 | tests. Apparently, two registers are generally used for something |
| 422 | else, leaving 14 registers on 64 bit versions and 6 registers on 32 |
| 423 | bit versions. */ |
| 424 | #ifdef __x86_64__ /* 64 bit version */ |
| 425 | #define MAX_BANDS 14 |
| 426 | #else |
| 427 | #define MAX_BANDS 6 |
| 428 | #endif |
| 429 | |
| 430 | |
| 431 | #define STEP_SINGLE(sv) \ |
| 432 | sv = _mm_subs_epi8(sv, *rsc); rsc++; \ |
| 433 | xEv = _mm_max_epu8(xEv, sv); |
| 434 | |
| 435 | |
| 436 | #define LENGTH_CHECK(label) \ |
| 437 | if (i >= L) goto label; |
| 438 | |
| 439 | |
| 440 | #define NO_CHECK(label) |
| 441 | |
| 442 | |
| 443 | #define STEP_BANDS_1() \ |
| 444 | STEP_SINGLE(sv00) |
| 445 | |
| 446 | #define STEP_BANDS_2() \ |
| 447 | STEP_BANDS_1() \ |
| 448 | STEP_SINGLE(sv01) |
| 449 | |
| 450 | #define STEP_BANDS_3() \ |
| 451 | STEP_BANDS_2() \ |
| 452 | STEP_SINGLE(sv02) |
| 453 | |
| 454 | #define STEP_BANDS_4() \ |
| 455 | STEP_BANDS_3() \ |
| 456 | STEP_SINGLE(sv03) |
| 457 | |
| 458 | #define STEP_BANDS_5() \ |
| 459 | STEP_BANDS_4() \ |
| 460 | STEP_SINGLE(sv04) |
| 461 | |
| 462 | #define STEP_BANDS_6() \ |
| 463 | STEP_BANDS_5() \ |
| 464 | STEP_SINGLE(sv05) |
| 465 | |
| 466 | #define STEP_BANDS_7() \ |
| 467 | STEP_BANDS_6() \ |
| 468 | STEP_SINGLE(sv06) |
| 469 | |
| 470 | #define STEP_BANDS_8() \ |
| 471 | STEP_BANDS_7() \ |
| 472 | STEP_SINGLE(sv07) |
| 473 | |
| 474 | #define STEP_BANDS_9() \ |
| 475 | STEP_BANDS_8() \ |
| 476 | STEP_SINGLE(sv08) |
| 477 | |
| 478 | #define STEP_BANDS_10() \ |
| 479 | STEP_BANDS_9() \ |
| 480 | STEP_SINGLE(sv09) |
| 481 | |
| 482 | #define STEP_BANDS_11() \ |
| 483 | STEP_BANDS_10() \ |
| 484 | STEP_SINGLE(sv10) |
| 485 | |
| 486 | #define STEP_BANDS_12() \ |
| 487 | STEP_BANDS_11() \ |
| 488 | STEP_SINGLE(sv11) |
| 489 | |
| 490 | #define STEP_BANDS_13() \ |
| 491 | STEP_BANDS_12() \ |
| 492 | STEP_SINGLE(sv12) |
| 493 | |
| 494 | #define STEP_BANDS_14() \ |
| 495 | STEP_BANDS_13() \ |
| 496 | STEP_SINGLE(sv13) |
| 497 | |
| 498 | #define STEP_BANDS_15() \ |
| 499 | STEP_BANDS_14() \ |
| 500 | STEP_SINGLE(sv14) |
| 501 | |
| 502 | #define STEP_BANDS_16() \ |
| 503 | STEP_BANDS_15() \ |
| 504 | STEP_SINGLE(sv15) |
| 505 | |
| 506 | #define STEP_BANDS_17() \ |
| 507 | STEP_BANDS_16() \ |
| 508 | STEP_SINGLE(sv16) |
| 509 | |
| 510 | #define STEP_BANDS_18() \ |
| 511 | STEP_BANDS_17() \ |
| 512 | STEP_SINGLE(sv17) |
| 513 | |
| 514 | |
| 515 | #define CONVERT_STEP(step, length_check, label, sv, pos) \ |
| 516 | length_check(label) \ |
| 517 | rsc = om->sbv[dsq[i]] + pos; \ |
| 518 | step() \ |
| 519 | sv = _mm_slli_si128(sv, 1); \ |
| 520 | sv = _mm_or_si128(sv, beginv); \ |
| 521 | i++; |
| 522 | |
| 523 | |
| 524 | #define CONVERT_1(step, LENGTH_CHECK, label) \ |
| 525 | CONVERT_STEP(step, LENGTH_CHECK, label, sv00, Q - 1) |
| 526 | |
| 527 | #define CONVERT_2(step, LENGTH_CHECK, label) \ |
| 528 | CONVERT_STEP(step, LENGTH_CHECK, label, sv01, Q - 2) \ |
| 529 | CONVERT_1(step, LENGTH_CHECK, label) |
| 530 | |
| 531 | #define CONVERT_3(step, LENGTH_CHECK, label) \ |
| 532 | CONVERT_STEP(step, LENGTH_CHECK, label, sv02, Q - 3) \ |
| 533 | CONVERT_2(step, LENGTH_CHECK, label) |
| 534 | |
| 535 | #define CONVERT_4(step, LENGTH_CHECK, label) \ |
| 536 | CONVERT_STEP(step, LENGTH_CHECK, label, sv03, Q - 4) \ |
| 537 | CONVERT_3(step, LENGTH_CHECK, label) |
| 538 | |
| 539 | #define CONVERT_5(step, LENGTH_CHECK, label) \ |
| 540 | CONVERT_STEP(step, LENGTH_CHECK, label, sv04, Q - 5) \ |
| 541 | CONVERT_4(step, LENGTH_CHECK, label) |
| 542 | |
| 543 | #define CONVERT_6(step, LENGTH_CHECK, label) \ |
| 544 | CONVERT_STEP(step, LENGTH_CHECK, label, sv05, Q - 6) \ |
| 545 | CONVERT_5(step, LENGTH_CHECK, label) |
| 546 | |
| 547 | #define CONVERT_7(step, LENGTH_CHECK, label) \ |
| 548 | CONVERT_STEP(step, LENGTH_CHECK, label, sv06, Q - 7) \ |
| 549 | CONVERT_6(step, LENGTH_CHECK, label) |
| 550 | |
| 551 | #define CONVERT_8(step, LENGTH_CHECK, label) \ |
| 552 | CONVERT_STEP(step, LENGTH_CHECK, label, sv07, Q - 8) \ |
| 553 | CONVERT_7(step, LENGTH_CHECK, label) |
| 554 | |
| 555 | #define CONVERT_9(step, LENGTH_CHECK, label) \ |
| 556 | CONVERT_STEP(step, LENGTH_CHECK, label, sv08, Q - 9) \ |
| 557 | CONVERT_8(step, LENGTH_CHECK, label) |
| 558 | |
| 559 | #define CONVERT_10(step, LENGTH_CHECK, label) \ |
| 560 | CONVERT_STEP(step, LENGTH_CHECK, label, sv09, Q - 10) \ |
| 561 | CONVERT_9(step, LENGTH_CHECK, label) |
| 562 | |
| 563 | #define CONVERT_11(step, LENGTH_CHECK, label) \ |
| 564 | CONVERT_STEP(step, LENGTH_CHECK, label, sv10, Q - 11) \ |
| 565 | CONVERT_10(step, LENGTH_CHECK, label) |
| 566 | |
| 567 | #define CONVERT_12(step, LENGTH_CHECK, label) \ |
| 568 | CONVERT_STEP(step, LENGTH_CHECK, label, sv11, Q - 12) \ |
| 569 | CONVERT_11(step, LENGTH_CHECK, label) |
| 570 | |
| 571 | #define CONVERT_13(step, LENGTH_CHECK, label) \ |
| 572 | CONVERT_STEP(step, LENGTH_CHECK, label, sv12, Q - 13) \ |
| 573 | CONVERT_12(step, LENGTH_CHECK, label) |
| 574 | |
| 575 | #define CONVERT_14(step, LENGTH_CHECK, label) \ |
| 576 | CONVERT_STEP(step, LENGTH_CHECK, label, sv13, Q - 14) \ |
| 577 | CONVERT_13(step, LENGTH_CHECK, label) |
| 578 | |
| 579 | #define CONVERT_15(step, LENGTH_CHECK, label) \ |
| 580 | CONVERT_STEP(step, LENGTH_CHECK, label, sv14, Q - 15) \ |
| 581 | CONVERT_14(step, LENGTH_CHECK, label) |
| 582 | |
| 583 | #define CONVERT_16(step, LENGTH_CHECK, label) \ |
| 584 | CONVERT_STEP(step, LENGTH_CHECK, label, sv15, Q - 16) \ |
| 585 | CONVERT_15(step, LENGTH_CHECK, label) |
| 586 | |
| 587 | #define CONVERT_17(step, LENGTH_CHECK, label) \ |
| 588 | CONVERT_STEP(step, LENGTH_CHECK, label, sv16, Q - 17) \ |
| 589 | CONVERT_16(step, LENGTH_CHECK, label) |
| 590 | |
| 591 | #define CONVERT_18(step, LENGTH_CHECK, label) \ |
| 592 | CONVERT_STEP(step, LENGTH_CHECK, label, sv17, Q - 18) \ |
| 593 | CONVERT_17(step, LENGTH_CHECK, label) |
| 594 | |
| 595 | |
| 596 | #define RESET_1() \ |
| 597 | register __m128i sv00 = beginv; |
| 598 | |
| 599 | #define RESET_2() \ |
| 600 | RESET_1() \ |
| 601 | register __m128i sv01 = beginv; |
| 602 | |
| 603 | #define RESET_3() \ |
| 604 | RESET_2() \ |
| 605 | register __m128i sv02 = beginv; |
| 606 | |
| 607 | #define RESET_4() \ |
| 608 | RESET_3() \ |
| 609 | register __m128i sv03 = beginv; |
| 610 | |
| 611 | #define RESET_5() \ |
| 612 | RESET_4() \ |
| 613 | register __m128i sv04 = beginv; |
| 614 | |
| 615 | #define RESET_6() \ |
| 616 | RESET_5() \ |
| 617 | register __m128i sv05 = beginv; |
| 618 | |
| 619 | #define RESET_7() \ |
| 620 | RESET_6() \ |
| 621 | register __m128i sv06 = beginv; |
| 622 | |
| 623 | #define RESET_8() \ |
| 624 | RESET_7() \ |
| 625 | register __m128i sv07 = beginv; |
| 626 | |
| 627 | #define RESET_9() \ |
| 628 | RESET_8() \ |
| 629 | register __m128i sv08 = beginv; |
| 630 | |
| 631 | #define RESET_10() \ |
| 632 | RESET_9() \ |
| 633 | register __m128i sv09 = beginv; |
| 634 | |
| 635 | #define RESET_11() \ |
| 636 | RESET_10() \ |
| 637 | register __m128i sv10 = beginv; |
| 638 | |
| 639 | #define RESET_12() \ |
| 640 | RESET_11() \ |
| 641 | register __m128i sv11 = beginv; |
| 642 | |
| 643 | #define RESET_13() \ |
| 644 | RESET_12() \ |
| 645 | register __m128i sv12 = beginv; |
| 646 | |
| 647 | #define RESET_14() \ |
| 648 | RESET_13() \ |
| 649 | register __m128i sv13 = beginv; |
| 650 | |
| 651 | #define RESET_15() \ |
| 652 | RESET_14() \ |
| 653 | register __m128i sv14 = beginv; |
| 654 | |
| 655 | #define RESET_16() \ |
| 656 | RESET_15() \ |
| 657 | register __m128i sv15 = beginv; |
| 658 | |
| 659 | #define RESET_17() \ |
| 660 | RESET_16() \ |
| 661 | register __m128i sv16 = beginv; |
| 662 | |
| 663 | #define RESET_18() \ |
| 664 | RESET_17() \ |
| 665 | register __m128i sv17 = beginv; |
| 666 | |
| 667 | |
| 668 | #define CALC(reset, step, convert, width) \ |
| 669 | int i; \ |
| 670 | int i2; \ |
| 671 | int Q = p7O_NQB(om->M); \ |
| 672 | __m128i *rsc; \ |
| 673 | \ |
| 674 | int w = width; \ |
| 675 | \ |
| 676 | dsq++; \ |
| 677 | \ |
| 678 | reset() \ |
| 679 | \ |
| 680 | for (i = 0; i < L && i < Q - q - w; i++) \ |
| 681 | { \ |
| 682 | rsc = om->sbv[dsq[i]] + i + q; \ |
| 683 | step() \ |
| 684 | } \ |
| 685 | \ |
| 686 | i = Q - q - w; \ |
| 687 | convert(step, LENGTH_CHECK, done1) \ |
| 688 | done1: \ |
| 689 | \ |
| 690 | for (i2 = Q - q; i2 < L - Q; i2 += Q) \ |
| 691 | { \ |
| 692 | for (i = 0; i < Q - w; i++) \ |
| 693 | { \ |
| 694 | rsc = om->sbv[dsq[i2 + i]] + i; \ |
| 695 | step() \ |
| 696 | } \ |
| 697 | \ |
| 698 | i += i2; \ |
| 699 | convert(step, NO_CHECK, ) \ |
| 700 | } \ |
| 701 | \ |
| 702 | for (i = 0; i2 + i < L && i < Q - w; i++) \ |
| 703 | { \ |
| 704 | rsc = om->sbv[dsq[i2 + i]] + i; \ |
| 705 | step() \ |
| 706 | } \ |
| 707 | \ |
| 708 | i+=i2; \ |
| 709 | convert(step, LENGTH_CHECK, done2) \ |
| 710 | done2: \ |
| 711 | \ |
| 712 | return xEv; |
| 713 | |
| 714 | |
| 715 | __m128i |
| 716 | calc_band_1(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 717 | { |
| 718 | CALC(RESET_1, STEP_BANDS_1, CONVERT_1, 1) |
| 719 | } |
| 720 | |
| 721 | __m128i |
| 722 | calc_band_2(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 723 | { |
| 724 | CALC(RESET_2, STEP_BANDS_2, CONVERT_2, 2) |
| 725 | } |
| 726 | |
| 727 | __m128i |
| 728 | calc_band_3(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 729 | { |
| 730 | CALC(RESET_3, STEP_BANDS_3, CONVERT_3, 3) |
| 731 | } |
| 732 | |
| 733 | __m128i |
| 734 | calc_band_4(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 735 | { |
| 736 | CALC(RESET_4, STEP_BANDS_4, CONVERT_4, 4) |
| 737 | } |
| 738 | |
| 739 | __m128i |
| 740 | calc_band_5(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 741 | { |
| 742 | CALC(RESET_5, STEP_BANDS_5, CONVERT_5, 5) |
| 743 | } |
| 744 | |
| 745 | __m128i |
| 746 | calc_band_6(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 747 | { |
| 748 | CALC(RESET_6, STEP_BANDS_6, CONVERT_6, 6) |
| 749 | } |
| 750 | |
| 751 | #if MAX_BANDS > 6 /* Only include needed functions to limit object file size */ |
| 752 | __m128i |
| 753 | calc_band_7(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 754 | { |
| 755 | CALC(RESET_7, STEP_BANDS_7, CONVERT_7, 7) |
| 756 | } |
| 757 | |
| 758 | __m128i |
| 759 | calc_band_8(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 760 | { |
| 761 | CALC(RESET_8, STEP_BANDS_8, CONVERT_8, 8) |
| 762 | } |
| 763 | |
| 764 | __m128i |
| 765 | calc_band_9(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 766 | { |
| 767 | CALC(RESET_9, STEP_BANDS_9, CONVERT_9, 9) |
| 768 | } |
| 769 | |
| 770 | __m128i |
| 771 | calc_band_10(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 772 | { |
| 773 | CALC(RESET_10, STEP_BANDS_10, CONVERT_10, 10) |
| 774 | } |
| 775 | |
| 776 | __m128i |
| 777 | calc_band_11(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 778 | { |
| 779 | CALC(RESET_11, STEP_BANDS_11, CONVERT_11, 11) |
| 780 | } |
| 781 | |
| 782 | __m128i |
| 783 | calc_band_12(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 784 | { |
| 785 | CALC(RESET_12, STEP_BANDS_12, CONVERT_12, 12) |
| 786 | } |
| 787 | |
| 788 | __m128i |
| 789 | calc_band_13(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 790 | { |
| 791 | CALC(RESET_13, STEP_BANDS_13, CONVERT_13, 13) |
| 792 | } |
| 793 | |
| 794 | __m128i |
| 795 | calc_band_14(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 796 | { |
| 797 | CALC(RESET_14, STEP_BANDS_14, CONVERT_14, 14) |
| 798 | } |
| 799 | #endif /* MAX_BANDS > 6 */ |
| 800 | #if MAX_BANDS > 14 |
| 801 | __m128i |
| 802 | calc_band_15(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 803 | { |
| 804 | CALC(RESET_15, STEP_BANDS_15, CONVERT_15, 15) |
| 805 | } |
| 806 | |
| 807 | __m128i |
| 808 | calc_band_16(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 809 | { |
| 810 | CALC(RESET_16, STEP_BANDS_16, CONVERT_16, 16) |
| 811 | } |
| 812 | |
| 813 | __m128i |
| 814 | calc_band_17(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 815 | { |
| 816 | CALC(RESET_17, STEP_BANDS_17, CONVERT_17, 17) |
| 817 | } |
| 818 | |
| 819 | __m128i |
| 820 | calc_band_18(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, int q, __m128i beginv, register __m128i xEv) |
| 821 | { |
| 822 | CALC(RESET_18, STEP_BANDS_18, CONVERT_18, 18) |
| 823 | } |
| 824 | #endif /* MAX_BANDS > 14 */ |
| 825 | |
| 826 | |
| 827 | /***************************************************************** |
| 828 | * 2. p7_SSVFilter() implementation |
| 829 | *****************************************************************/ |
| 830 | |
| 831 | uint8_t |
| 832 | get_xE(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om) |
| 833 | { |
| 834 | __m128i xEv; /* E state: keeps max for Mk->E as we go */ |
| 835 | __m128i beginv; /* begin scores */ |
| 836 | |
| 837 | int q; /* counter over vectors 0..nq-1 */ |
| 838 | int Q = p7O_NQB(om->M); /* segment length: # of vectors */ |
| 839 | |
| 840 | int bands; /* the number of bands (rounds) to use */ |
| 841 | |
| 842 | int last_q = 0; /* for saving the last q value to find band width */ |
| 843 | int i; /* counter for bands */ |
| 844 | |
| 845 | /* function pointers for the various number of vectors to use */ |
| 846 | __m128i (*fs[MAX_BANDS + 1]) (const ESL_DSQ *, int, const P7_OPROFILE *, int, register __m128i, __m128i) |
| 847 | = {NULL |
| 848 | , calc_band_1, calc_band_2, calc_band_3, calc_band_4, calc_band_5, calc_band_6 |
| 849 | #if MAX_BANDS > 6 |
| 850 | , calc_band_7, calc_band_8, calc_band_9, calc_band_10, calc_band_11, calc_band_12, calc_band_13, calc_band_14 |
| 851 | #endif |
| 852 | #if MAX_BANDS > 14 |
| 853 | , calc_band_15, calc_band_16, calc_band_17, calc_band_18 |
| 854 | #endif |
| 855 | }; |
| 856 | |
| 857 | beginv = _mm_set1_epi8(-128); |
| 858 | xEv = beginv; |
| 859 | |
| 860 | /* Use the highest number of bands but no more than MAX_BANDS */ |
| 861 | bands = (Q + MAX_BANDS - 1) / MAX_BANDS; |
| 862 | |
| 863 | for (i = 0; i < bands; i++) { |
| 864 | q = (Q * (i + 1)) / bands; |
| 865 | |
| 866 | xEv = fs[q-last_q](dsq, L, om, last_q, beginv, xEv); |
| 867 | |
| 868 | last_q = q; |
| 869 | } |
| 870 | |
| 871 | return esl_sse_hmax_epu8(xEv); |
| 872 | } |
| 873 | |
| 874 | |
| 875 | int |
| 876 | p7_SSVFilter(const ESL_DSQ *dsq, int L, const P7_OPROFILE *om, float *ret_sc) |
| 877 | { |
| 878 | /* Use 16 bit values to avoid overflow due to moved baseline */ |
| 879 | uint16_t xE; |
| 880 | uint16_t xJ; |
| 881 | |
| 882 | if (om->tjb_b + om->tbm_b + om->tec_b + om->bias_b >= 127) { |
| 883 | /* the optimizations are not guaranteed to work under these |
| 884 | conditions (see comments at start of file) */ |
| 885 | return eslENORESULT; |
| 886 | } |
| 887 | |
| 888 | xE = get_xE(dsq, L, om); |
| 889 | |
| 890 | if (xE >= 255 - om->bias_b) |
| 891 | { |
| 892 | /* We have an overflow. */ |
| 893 | *ret_sc = eslINFINITY; |
| 894 | if (om->base_b - om->tjb_b - om->tbm_b < 128) |
| 895 | { |
| 896 | /* The original MSV filter may not overflow, so we are not sure our result is correct */ |
| 897 | return eslENORESULT; |
| 898 | } |
| 899 | |
| 900 | /* We know that the overflow will also occur in the original MSV filter */ |
| 901 | return eslERANGE; |
| 902 | } |
| 903 | |
| 904 | xE += om->base_b - om->tjb_b - om->tbm_b; |
| 905 | xE -= 128; |
| 906 | |
| 907 | if (xE >= 255 - om->bias_b) |
| 908 | { |
| 909 | /* We know that the result will overflow in the original MSV filter */ |
| 910 | *ret_sc = eslINFINITY; |
| 911 | return eslERANGE; |
| 912 | } |
| 913 | |
| 914 | xJ = xE - om->tec_b; |
| 915 | |
| 916 | if (xJ > om->base_b) return eslENORESULT; /* The J state could have been used, so doubt about score */ |
| 917 | |
| 918 | /* finally C->T, and add our missing precision on the NN,CC,JJ back */ |
| 919 | *ret_sc = ((float) (xJ - om->tjb_b) - (float) om->base_b); |
| 920 | *ret_sc /= om->scale_b; |
| 921 | *ret_sc -= 3.0; /* that's ~ L \log \frac{L}{L+3}, for our NN,CC,JJ */ |
| 922 | |
| 923 | return eslOK; |
| 924 | } |
| 925 | |
| 926 |