38 #include <sys/types.h>
40 #define ALIGN16(x) (((x)+15)&(-16))
43 template<
typename T>
static inline T min(
T a,
T b ){
return a<b?a:b; }
44 template<
typename T>
static inline T max(
T a,
T b ){
return a>b?a:b; }
57 template<>
struct MaxValue<float> {
static const float value = FLT_MAX; };
58 template<>
struct MaxValue<double> {
static const double value = DBL_MAX; };
61 template<>
struct MinValue<float> {
static const float value = FLT_MIN; };
62 template<>
struct MinValue<double> {
static const double value = DBL_MIN; };
64 template<
typename T,
typename V>
struct Profile {
83 const int nSeg =
sizeof(V)/
sizeof(
T);
84 const int segLen =
ALIGN16(len)/nSeg;
88 profile->
rD = (V*)malloc(
sizeof(V)*segLen);
89 profile->
loadOpt = (V*)malloc(
sizeof(V)*segLen);
90 profile->
storeOpt = (V*)malloc(
sizeof(V)*segLen);
110 template<
typename V>
static inline V
vec_addx(V a, V b)
112 return vec_adds(a,b);
127 template<
typename V>
static inline V
vec_subx(V a, V b)
129 return vec_subs(a,b);
153 const char* db,
int dbLen,
179 vZero = vec_splat( vZero, 0 );
181 vGoal = vec_splat( vGoal, 0 );
182 V vDelFixed = {(
T)options->
gapOpen};
183 vDelFixed = vec_splat( vDelFixed, 0 );
184 V vDelInc = {(
T)options->
gapExt};
185 vDelInc = vec_splat( vDelInc, 0 );
186 V vBias = {(
T)profile->
bias};
187 vBias = vec_splat( vBias, 0 );
190 const int nSeg =
sizeof(V)/
sizeof(
T);
191 const int segLen =
ALIGN16(profile->
len)/nSeg;
196 for(
int i=0;
LIKELY(i<segLen); i++)
201 for(
int i=0;
LIKELY(i<dbLen); i++ ){
205 V vStoreOpt = vec_sld(vZero, profile->
storeOpt[segLen-1],
sizeof(V)-
sizeof(
T));
209 V * currentProfile = profile->
profile + db[i]*segLen;
212 for(
int ii=0; ii<nSeg; ++ii) {
213 for(
int jj=0; jj<segLen; ++jj) {
214 if(ii*segLen+jj < profile->len)
215 printf(
"\t%d",(
int)((
T*)currentProfile)[ii+jj*nSeg]);
229 for(
int j=0;
LIKELY(j<segLen); j++ ){
231 V vRD = profile->
rD[j];
236 vRD = vec_max(vRD,vZero);
238 vRD = vec_max(vTmp,vRD);
239 profile->
rD[j] = vRD;
242 vStoreOpt =
vec_addx(currentProfile[j],vStoreOpt);
244 vStoreOpt =
vec_subx(vStoreOpt,vBias);
247 vMaxScore = vec_max( vMaxScore, vStoreOpt );
249 vTmp = vec_max( vCD, vRD );
251 vStoreOpt = vec_max( vStoreOpt, vTmp );
257 vStoreOpt =
vec_addx(vStoreOpt,vDelFixed);
261 vStoreOpt = vec_max(vStoreOpt, vZero);
262 vRD = vec_max( vStoreOpt, vRD );
263 vCD = vec_max( vStoreOpt, vCD );
266 profile->
rD[j] = vRD;
269 vStoreOpt = profile->
loadOpt[j];
279 for(
T* tmp = (
T*)&vMaxScore; tmp<(
T*)(&vMaxScore+1); tmp++ )
286 V vStoreOptx = profile->
storeOpt[0];
287 vStoreOptx =
vec_addx(vStoreOptx,vDelFixed - vDelInc);
289 vStoreOptx = vec_max( vStoreOptx, vZero );
290 V vCDx = vec_sld(vZero, vCD,
sizeof(V)-
sizeof(
T));
292 if(vec_all_le(vCDx,vStoreOptx) == 0) {
293 for(
int j=0;
LIKELY(j<nSeg); ++j) {
295 vCD = vec_sld(vZero, vCD,
sizeof(V)-
sizeof(
T));
297 for(
int k=0;
LIKELY(k<segLen-1); ++k) {
300 vStoreOpt = vec_max( vStoreOpt, vCD );
305 vStoreOpt =
vec_addx( vStoreOpt, vDelFixed);
307 vCD = vec_max( vCD, vZero );
308 vStoreOpt = vec_max( vStoreOpt, vZero );
312 if(
UNLIKELY(vec_all_le(vCD,vStoreOpt)))
318 vStoreOpt = profile->
storeOpt[segLen-1];
319 vStoreOpt = vec_max( vStoreOpt, vCD );
320 profile->
storeOpt[segLen-1] = vStoreOpt;
324 vStoreOpt =
vec_addx( vStoreOpt, vDelFixed);
326 vCD = vec_max( vCD, vZero );
327 vStoreOpt = vec_max( vStoreOpt, vZero );
330 if(
UNLIKELY(vec_all_le(vCD,vStoreOpt)))
340 printf(
"%c\t",db[i]);
341 for(
int ii=0; ii<nSeg; ++ii) {
342 for(
int jj=0; jj<segLen; ++jj) {
343 if(ii*segLen+jj < profile->len)
344 printf(
"%d\t",(
int)(((
T*)profile->
storeOpt)[ii+jj*nSeg]-zero));
366 const char* db,
int dbLen,
391 vZero = vec_splat( vZero, 0 );
393 vGoal = vec_splat( vGoal, 0 );
394 V vDelFixed = {(
T)options->
gapOpen};
395 vDelFixed = vec_splat( vDelFixed, 0 );
396 V vDelInc = {(
T)options->
gapExt};
397 vDelInc = vec_splat( vDelInc, 0 );
398 V vBias = {(
T)profile->
bias};
399 vBias = vec_splat( vBias, 0 );
402 const int nSeg =
sizeof(V)/
sizeof(
T);
403 const int segLen = (
ALIGN16(profile->
len)/nSeg + 1) & ~1;
404 const int subSegLen = segLen / 2;
406 V vMaxScore1 = vZero, vMaxScore2 = vZero;
410 for(
int i=0;
LIKELY(i<segLen); i++)
415 for(
int i=0;
LIKELY(i<dbLen); i++ ){
416 V vCD1 = vZero, vCD2 = vZero;
419 V vStoreOpt1 = vec_sld(vZero, profile->
storeOpt[segLen-1],
sizeof(V)-
sizeof(
T));
420 V vStoreOpt2 = profile->
storeOpt[subSegLen-1];
424 V * currentProfile = profile->
profile + db[i]*segLen;
428 for(
int ii=0; ii<nSeg; ++ii) {
429 for(
int jj=0; jj<segLen; ++jj) {
430 if(ii*segLen+jj < profile->len)
431 printf(
"\t%d",(
int)((
T*)currentProfile)[ii+jj*nSeg]);
445 for(
int j=0;
LIKELY(j<subSegLen); j++ ){
447 V vRD1 = profile->
rD[j ];
448 V vRD2 = profile->
rD[j+subSegLen];
449 V vTmp1 = profile->
loadOpt[j ];
450 V vTmp2 = profile->
loadOpt[j+subSegLen];
456 vRD1 = vec_max(vRD1,vZero);
457 vRD2 = vec_max(vRD2,vZero);
459 vRD1 = vec_max(vTmp1,vRD1);
460 vRD2 = vec_max(vTmp2,vRD2);
461 profile->
rD[j ] = vRD1;
462 profile->
rD[j+subSegLen] = vRD2;
465 vStoreOpt1 =
vec_addx(currentProfile[j ],vStoreOpt1);
466 vStoreOpt2 =
vec_addx(currentProfile[j+subSegLen],vStoreOpt2);
468 vStoreOpt1 =
vec_subx(vStoreOpt1,vBias);
469 vStoreOpt2 =
vec_subx(vStoreOpt2,vBias);
473 vMaxScore1 = vec_max( vMaxScore1, vStoreOpt1 );
474 vMaxScore2 = vec_max( vMaxScore2, vStoreOpt2 );
477 vTmp1 = vec_max( vCD1, vRD1 );
478 vTmp2 = vec_max( vCD2, vRD2 );
481 vStoreOpt1 = vec_max( vStoreOpt1, vTmp1 );
482 vStoreOpt2 = vec_max( vStoreOpt2, vTmp2 );
486 profile->
storeOpt[j+subSegLen] = vStoreOpt2;
489 vStoreOpt1 =
vec_addx(vStoreOpt1,vDelFixed);
490 vStoreOpt2 =
vec_addx(vStoreOpt2,vDelFixed);
496 vStoreOpt1 = vec_max(vStoreOpt1, vZero);
497 vStoreOpt2 = vec_max(vStoreOpt2, vZero);
499 vRD1 = vec_max( vStoreOpt1, vRD1 );
500 vRD2 = vec_max( vStoreOpt2, vRD2 );
501 vCD1 = vec_max( vStoreOpt1, vCD1 );
502 vCD2 = vec_max( vStoreOpt2, vCD2 );
505 profile->
rD[j ] = vRD1;
506 profile->
rD[j+subSegLen] = vRD2;
509 vStoreOpt1 = profile->
loadOpt[j ];
510 vStoreOpt2 = profile->
loadOpt[j+subSegLen];
520 V vMaxScore = vec_max( vMaxScore1, vMaxScore2 );
521 for(
T* tmp = (
T*)&vMaxScore; tmp<(
T*)(&vMaxScore+1); tmp++ )
528 V vStoreOptx1 = profile->
storeOpt[0 ];
529 V vStoreOptx2 = profile->
storeOpt[subSegLen];
530 vStoreOptx1 =
vec_addx(vStoreOptx1,vDelFixed - vDelInc);
531 vStoreOptx2 =
vec_addx(vStoreOptx2,vDelFixed - vDelInc);
533 vStoreOptx1 = vec_max( vStoreOptx1, vZero );
534 vStoreOptx2 = vec_max( vStoreOptx2, vZero );
536 V vCDx1 = vec_sld(vZero, vCD2,
sizeof(V)-
sizeof(
T));
539 if(
UNLIKELY((vec_all_le(vCDx1,vStoreOptx1) == 0) || (vec_all_le(vCDx2,vStoreOptx2) == 0))) {
540 for(
int j=0;
LIKELY(j<nSeg+1); ++j) {
544 vCD1 = vec_sld(vZero, vRotate,
sizeof(V)-
sizeof(
T));
546 for(
int k=0;
LIKELY(k<subSegLen-1); ++k) {
549 vStoreOpt2 = profile->
storeOpt[k + subSegLen];
550 vStoreOpt1 = vec_max( vStoreOpt1, vCD1 );
551 vStoreOpt2 = vec_max( vStoreOpt2, vCD2 );
553 profile->
storeOpt[k + subSegLen] = vStoreOpt2;
558 vStoreOpt1 =
vec_addx( vStoreOpt1, vDelFixed);
559 vStoreOpt2 =
vec_addx( vStoreOpt2, vDelFixed);
561 vCD1 = vec_max( vCD1, vZero );
562 vCD2 = vec_max( vCD2, vZero );
563 vStoreOpt1 = vec_max( vStoreOpt1, vZero );
564 vStoreOpt2 = vec_max( vStoreOpt2, vZero );
568 if(
UNLIKELY(vec_all_le(vCD1,vStoreOpt1) != 0 && vec_all_le(vCD2,vStoreOpt2) != 0))
574 vStoreOpt1 = profile->
storeOpt[subSegLen - 1];
575 vStoreOpt2 = profile->
storeOpt[segLen - 1];
576 vStoreOpt1 = vec_max( vStoreOpt1, vCD1 );
577 vStoreOpt2 = vec_max( vStoreOpt2, vCD2 );
578 profile->
storeOpt[subSegLen - 1] = vStoreOpt1;
579 profile->
storeOpt[segLen - 1] = vStoreOpt2;
584 vStoreOpt1 =
vec_addx( vStoreOpt1, vDelFixed);
585 vStoreOpt2 =
vec_addx( vStoreOpt2, vDelFixed);
587 vCD1 = vec_max( vCD1, vZero );
588 vCD2 = vec_max( vCD2, vZero );
589 vStoreOpt1 = vec_max( vStoreOpt1, vZero );
590 vStoreOpt2 = vec_max( vStoreOpt2, vZero );
593 if(
UNLIKELY(vec_all_le(vCD1,vStoreOpt1) != 0 && vec_all_le(vCD2,vStoreOpt2) != 0))
602 printf(
"%c\t",db[i]);
603 for(
int ii=0; ii<nSeg; ++ii) {
604 for(
int jj=0; jj<segLen; ++jj) {
605 if(ii*segLen+jj < profile->len)
606 printf(
"%d\t",(
int)(((
T*)profile->
storeOpt)[ii+jj*nSeg]-zero));
628 template<
typename T,
typename V,
typename X >
630 const int alignedLen =
ALIGN16(queryLen);
631 const int nSeg =
sizeof(V)/
sizeof(
T);
632 const int segLen = alignedLen/nSeg;
637 T *currentProfile = ((
T*)profile->
profile)+i*alignedLen;
638 for(
int j=0; j<segLen; j++ ){
639 T *tmp = currentProfile + j*nSeg;
640 for(
int k=0; k<nSeg; k++ )
641 if( j + k*segLen < queryLen )
642 tmp[k] = (
T)simi[ query[j + k*segLen ] * MATRIX_DIM + i ];
668 template<
typename T,
typename V >
686 currentScore = dynProgrLocal<T,V> ( db, dbLen, profile, options );
688 currentScore = dynProgrLocal2<T,V> ( db, dbLen,
profile, options );
690 T currentScore = dynProgrLocal<T,V> ( db, dbLen,
profile, options );
692 if( maxScore < currentScore)
693 maxScore = currentScore;
700 return (
double)(maxScore-zero);
776 return swps3_createProfileAltivec<int8_t, vector int8_t>(query, queryLen, matrix);
791 return swps3_createProfileAltivec<int16_t, vector int16_t>(query, queryLen, matrix);
806 return swps3_createProfileAltivec<float, vector float>(query, queryLen, matrix);