1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240 |
- #ifdef cmVectOpsRICode_h
-
- VECT_OP_TYPE* VECT_OP_FUNC(Col)( VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn )
- {
- assert(ci<cn);
- return m + (ci*rn);
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Row)( VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn )
- {
- assert(ri<rn);
- return m + ri;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(ElePtr)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
- {
- assert(ri<rn && ci<cn);
- return m + (ci*rn) + ri;
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(Ele)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
- { return *VECT_OP_FUNC(ElePtr)(m,ri,ci,rn,cn); }
-
- void VECT_OP_FUNC(Set)( VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn, VECT_OP_TYPE v )
- { *(VECT_OP_FUNC(ElePtr)(m,ri,ci,rn,cn)) = v; }
-
- const VECT_OP_TYPE* VECT_OP_FUNC(CCol)( const VECT_OP_TYPE* m, unsigned ci, unsigned rn, unsigned cn )
- {
- assert(ci<cn);
- return m + (ci*rn);
- }
-
- const VECT_OP_TYPE* VECT_OP_FUNC(CRow)( const VECT_OP_TYPE* m, unsigned ri, unsigned rn, unsigned cn )
- {
- assert(ri<rn);
- return m + ri;
- }
-
- const VECT_OP_TYPE* VECT_OP_FUNC(CElePtr)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
- {
- assert(ri<rn && ci<cn);
- return m + (ci*rn) + ri;
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(CEle)( const VECT_OP_TYPE* m, unsigned ri, unsigned ci, unsigned rn, unsigned cn )
- { return *VECT_OP_FUNC(CElePtr)(m,ri,ci,rn,cn); }
-
-
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Fill)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE value )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
-
- if( value == 0 )
- memset(dbp,0,(dep-dbp)*sizeof(VECT_OP_TYPE));
- else
- {
- while( dbp < dep )
- *dbp++ = value;
- }
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Zero)( VECT_OP_TYPE* dbp, unsigned dn )
- {
- memset( dbp, 0, sizeof(VECT_OP_TYPE)*dn);
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Move)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* sp )
- {
- memmove(bp,sp,sizeof(VECT_OP_TYPE)*bn);
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Copy)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* sp )
- {
- memcpy(bp,sp,sizeof(VECT_OP_TYPE)*bn);
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyN)( VECT_OP_TYPE* bp, unsigned bn, unsigned d_stride, const VECT_OP_TYPE* sp, unsigned s_stride )
- {
- VECT_OP_TYPE* dbp = bp;
- const VECT_OP_TYPE* ep = bp + (bn*d_stride);
- for(; bp < ep; bp += d_stride, sp += s_stride )
- *bp = *sp;
-
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyU)( VECT_OP_TYPE* bp, unsigned bn, const unsigned* sp )
- {
- VECT_OP_TYPE* dbp = bp;
- const VECT_OP_TYPE* ep = bp + bn;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyI)( VECT_OP_TYPE* dbp, unsigned dn, const int* sp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dp < dep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyF)( VECT_OP_TYPE* dbp, unsigned dn, const float* sp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dp < dep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyD)( VECT_OP_TYPE* dbp, unsigned dn, const double* sp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dp < dep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyS)( VECT_OP_TYPE* dbp, unsigned dn, const cmSample_t* sp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dp < dep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyR)( VECT_OP_TYPE* dbp, unsigned dn, const cmReal_t* sp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dp < dep )
- *dp++ = (VECT_OP_TYPE)*sp++;
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(CopyStride)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sp, unsigned srcStride )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- for(; dp < dep; sp += srcStride )
- *dp++ = *sp;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Shrink)( VECT_OP_TYPE* s, unsigned sn, const VECT_OP_TYPE* t, unsigned tn )
- {
- assert( s <= t && t <= (s+sn) );
- assert( s <= (t+tn) && (t+tn) <= (s+sn));
- //VECT_OP_FUNC(Move)(s,sn - ((t - s) + tn),t+tn);
- VECT_OP_FUNC(Move)((VECT_OP_TYPE*)t,(sn - ((t+tn)-s)) + 1,t+tn);
- return s;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Expand)( VECT_OP_TYPE* s, unsigned sn, const VECT_OP_TYPE* t, unsigned tn )
- {
- assert( s <= t && t <= s+sn );
- unsigned i = t - s;
- s = cmMemResizeP(VECT_OP_TYPE,s,sn+tn);
- t = s + i;
- assert( t + tn + sn - i == s + sn + tn );
- VECT_OP_FUNC(Move)(((VECT_OP_TYPE*)t)+tn,sn-i,t);
- return s;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Replace)(VECT_OP_TYPE* s, unsigned* sn, const VECT_OP_TYPE* t, unsigned tn, const VECT_OP_TYPE* u, unsigned un )
- {
- // if s is empty and t[tn] is empty
- if( s == NULL && tn == 0 )
- {
- if( un == 0 )
- return s;
-
- s = cmMemAllocZ(VECT_OP_TYPE,un);
- VECT_OP_FUNC(Copy)(s,un,u);
-
- if( sn != NULL )
- *sn = un;
-
- return s;
- }
-
-
- assert( s!=NULL && t != NULL );
- assert( (u!=NULL && un>0) || (u==NULL && un==0) );
-
- if( (tn==0 && un==0) || (t==NULL && u==NULL))
- return s;
-
- // if the area to replace is greater than the area to insert ...
- if( tn > un )
- {
- VECT_OP_FUNC(Shrink)(s,*sn,t+un,tn-un); // ... then shrink the buffer
- *sn -= tn-un;
- }
- else
- // if the area to insert is greater than the area to replace ...
- if( un > tn )
- {
- unsigned offs = t - s;
- s = VECT_OP_FUNC(Expand)(s,*sn,t+tn,un-tn); // ... then expand the buffer
- t = s + offs;
- *sn += un-tn;
- }
-
- assert(t+un <= s+(*sn));
-
- if( u!=NULL )
- VECT_OP_FUNC(Copy)((VECT_OP_TYPE*)t,un,u);
-
- return s;
- }
-
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Rotate)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt )
- {
- VECT_OP_TYPE* dep = dbp + dn;
- int i = 0;
- unsigned k = 0;
- int n = dep - dbp;
- VECT_OP_TYPE t1 = dbp[i];
-
- for(k=0; k<n; ++k)
- {
- int j;
-
- j = (i + shiftCnt) % n;
-
- if( j<0 )
- j += n;
-
- VECT_OP_TYPE t2 = dbp[j];
-
- dbp[j] = t1;
- t1 = t2;
- i = j;
- }
-
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(RotateM)( VECT_OP_TYPE* dbp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sbp, int rShiftCnt, int cShiftCnt )
- {
- int j;
-
- while( rShiftCnt < 0 )
- rShiftCnt += drn;
-
- while( cShiftCnt < 0 )
- cShiftCnt += dcn;
-
- int m = rShiftCnt % drn;
- int n = cShiftCnt % dcn;
-
-
- for(j=0; j<dcn; ++j,++n)
- {
- if(n==dcn)
- n = 0;
-
- // cnt from dst position to end of column
- unsigned cn = drn - m;
-
- // copy from top of src col to bottom of dst column
- VECT_OP_FUNC(Copy)(dbp + (n*drn) + m, cn, sbp );
- sbp+=cn;
-
-
- if( cn < drn )
- {
- // copy from bottom of src col to top of dst column
- VECT_OP_FUNC(Copy)(dbp + (n*drn), drn-cn, sbp );
- sbp += drn-cn;
- }
-
- }
- return dbp;
-
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Shift)( VECT_OP_TYPE* dbp, unsigned dn, int shiftCnt, VECT_OP_TYPE fillValue )
- {
- VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* rp = dbp;
- unsigned n = dep - dbp;
-
- if( shiftCnt == 0 )
- return dbp;
-
- if( abs(shiftCnt) >= n )
- return VECT_OP_FUNC(Fill)(dbp,dn,fillValue);
-
- if( shiftCnt > 0 )
- {
- const VECT_OP_TYPE* sbp = dep - (shiftCnt+1);
- const VECT_OP_TYPE* sep = dbp;
- VECT_OP_TYPE* dp = dbp + (n-1);
-
- while( sbp >= sep )
- *dp-- = *sbp--;
-
- while(dbp <= dp )
- *dbp++ = fillValue;
-
- }
- else
- {
- const VECT_OP_TYPE* sbp = dbp + abs(shiftCnt);
- while( sbp < dep )
- *dbp++ = *sbp++;
-
- while(dbp<dep)
- *dbp++ = fillValue;
- }
-
- return rp;
-
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Flip)( VECT_OP_TYPE* dbp, unsigned dn)
- {
- VECT_OP_TYPE* p0 = dbp;
- VECT_OP_TYPE* p1 = dbp + dn - 1;
-
- while( p0 < p1 )
- {
- VECT_OP_TYPE t = *p0;
- *p0++ = *p1;
- *p1-- = t;
- }
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
- {
- const VECT_OP_TYPE* ep = bp + n;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ -= v;
- return bp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVV)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v )
- {
- const VECT_OP_TYPE* ep = bp + n;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ -= *v++;
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVVS)( VECT_OP_TYPE* bp, unsigned n, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* ep = bp + n;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ = *v++ - s;
- return bp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
- {
- const VECT_OP_TYPE* ep = dp + (dn*dnn);
- VECT_OP_TYPE* dbp = dp;
- for(; dp < ep; dp+=dnn, v+=n )
- *dp -= *v;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sb0p++ - *sb1p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(SubVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = s0 - *sb1p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(AddVS)( VECT_OP_TYPE* bp, unsigned n, VECT_OP_TYPE v )
- {
- const VECT_OP_TYPE* ep = bp + n;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ += v;
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(AddVV)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v )
- {
- const VECT_OP_TYPE* ep = bp + bn;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ += *v++;
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(AddVVS)( VECT_OP_TYPE* bp, unsigned bn, const VECT_OP_TYPE* v, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* ep = bp + bn;
- VECT_OP_TYPE* dp = bp;
- while( dp < ep )
- *dp++ = *v++ + s;
- return bp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(AddVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
- {
- const VECT_OP_TYPE* ep = dp + (dn*dnn);
- VECT_OP_TYPE* dbp = dp;
- for(; dp < ep; v+=n, dp+=dnn )
- *dp += *v;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(AddVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sb0p++ + *sb1p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sb0p++ * *sb1p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ *= *sbp++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
- {
- const VECT_OP_TYPE* ep = dp + (dn*dnn);
- VECT_OP_TYPE* dbp = dp;
- for(; dp < ep; v+=n, dp+=dnn )
- *dp *= *v;
- return dbp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ *= s;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sbp++ * s;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultVaVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ += *sbp++ * s;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MultSumVVS)(VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sbp, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ += *sbp++ * s;
- return dp;
- }
-
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVVS)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, VECT_OP_TYPE s1 )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sb0p++ / s1;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ /= *sb0p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVVV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = *sb0p++ / *sb1p++;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVVNN)(VECT_OP_TYPE* dp, unsigned dn, unsigned dnn, const VECT_OP_TYPE* v, unsigned n )
- {
- const VECT_OP_TYPE* ep = dp + (dn*dnn);
- VECT_OP_TYPE* dbp = dp;
- for(; dp < ep; v+=n, dp+=dnn )
- *dp /= *v;
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVS)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE s )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ /= s;
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVSV)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE s0, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- while( dbp < dep )
- *dbp++ = s0 / *sb1p++;
- return dp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- for(; dbp < dep; ++sb0p )
- if( *sb0p == 0 )
- *dbp++ = 0;
- else
- *dbp++ /= *sb0p;
-
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivVVVZ)( VECT_OP_TYPE* dbp, unsigned dn, const VECT_OP_TYPE* sb0p, const VECT_OP_TYPE* sb1p )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- VECT_OP_TYPE* dp = dbp;
- for(; dbp < dep; ++sb0p,++sb1p )
- if( *sb1p == 0 )
- *dbp++ = 0;
- else
- *dbp++ = *sb0p / *sb1p;
-
- return dp;
- }
-
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(DivMS)( VECT_OP_TYPE* dp, unsigned drn, unsigned dcn, const VECT_OP_TYPE* sp )
- {
- unsigned i;
- for(i=0; i<dcn; ++i)
- VECT_OP_FUNC(DivVS)( dp + i*drn, drn, sp[i] );
- return dp;
- }
-
-
- VECT_OP_TYPE VECT_OP_FUNC(Sum)( const VECT_OP_TYPE* bp, unsigned n )
- {
- const VECT_OP_TYPE* ep = bp + n;
- VECT_OP_TYPE s = 0;
- while( bp < ep )
- s += *bp++;
-
- return s;
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(SumN)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
- {
- const VECT_OP_TYPE* ep = bp + (n*stride);
- VECT_OP_TYPE s = 0;
- for(; bp < ep; bp += stride )
- s += *bp;
-
- return s;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(SumM)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
- {
- unsigned i;
- for(i=0; i<scn; ++i)
- dp[i] = VECT_OP_FUNC(Sum)(sp + (i*srn), srn );
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(SumMN)(const VECT_OP_TYPE* sp, unsigned srn, unsigned scn, VECT_OP_TYPE* dp )
- {
- unsigned i;
- for(i=0; i<srn; ++i)
- dp[i] = VECT_OP_FUNC(SumN)(sp + i, scn, srn );
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Abs)( VECT_OP_TYPE* dbp, unsigned dn )
- {
- unsigned i;
- for(i=0; i<dn; ++i)
- if( dbp[i]<0 )
- dbp[i] = -dbp[i];
-
- return dbp;
- }
-
- // mi is a target value - it holds the number of elements in ap[an] which must be be less than the median value.
- // If the initial array contains an even number of values then the median value is formed by averaging the two center values.
- // In this case *evenFlPtr is set and used to indicate that the center-upper value must be found during undwinding.
- VECT_OP_TYPE VECT_OP_FUNC(MedianSearch)( unsigned mi, const VECT_OP_TYPE* ap, unsigned an, bool* evenFlPtr )
- {
- VECT_OP_TYPE x = ap[0]; // pick a random value as a potential median value
-
- VECT_OP_TYPE a1[ an ]; // values below x
- VECT_OP_TYPE a3[ an ]; // values above x
- unsigned a1n = 0;
- unsigned a2n = 0; // values equal to x
- unsigned a3n = 0;
-
-
- const VECT_OP_TYPE* abp = ap;
- const VECT_OP_TYPE* aep = abp + an;
-
-
- for(; abp < aep; ++abp )
- {
- if( *abp < x )
- a1[a1n++] = *abp;
- else
- {
- if( *abp > x )
- a3[a3n++] = *abp;
- else
- ++a2n;
- }
- }
-
- //printf("%i : %i %i %i\n",mi,a1n,a2n,a3n);
-
- // there are more values below x (mi remains the target split point)
- if( a1n > mi )
- {
- x = VECT_OP_FUNC(MedianSearch)(mi,a1,a1n,evenFlPtr);
- }
- else
- {
- // the target was located
- if( a1n+a2n >= mi )
- {
-
- // if a1n alone matches mi then the max value in a1[] holds the median value otherwise x is the median
- if(a1n>=1 && a1n==mi)
- {
- VECT_OP_TYPE mv = VECT_OP_FUNC(Max)(a1,a1n,1);
- x = *evenFlPtr ? (mv+x)/2 : mv;
- *evenFlPtr = false;
- }
-
- // if the evenFl is set then the closest value above the median (x) must be located
- if( *evenFlPtr )
- {
- // if the next greater value is in a2[]
- if( a2n > 1 && (a1n+a2n) > mi )
- *evenFlPtr = false;
- else
- // if the next greater value is in a3[]
- if( a3n > 1 )
- {
- x = (x + VECT_OP_FUNC(Min)(a3,a3n,1))/2;
- *evenFlPtr = false;
- }
- }
-
- // no need for unwind processing - all the possibilities at this level have been exhausted
- return x;
- }
- else
- {
- // There are more values above x - the median must therefore be in a3[].
- // Reset mi cmcounting for the fact that we know that there are
- // a1n+a2n values below the lowest value in a3.
- x = VECT_OP_FUNC(MedianSearch)(mi - (a1n+a2n), a3, a3n, evenFlPtr );
- }
- }
-
- if( *evenFlPtr )
- {
-
- // find the first value greater than x
- while( ap < aep && *ap <= x )
- ++ap;
-
- if( ap < aep )
- {
-
- VECT_OP_TYPE v = *ap++;
-
- // find the nearest value greater than x
- for(; ap < aep; ++ap )
- if( *ap > x && ((*ap - x) < (v-x)))
- v = *ap;
-
-
- x = (v + x)/2;
- *evenFlPtr = false;
- }
- }
- return x;
- }
-
-
- VECT_OP_TYPE VECT_OP_FUNC(Median)( const VECT_OP_TYPE* bp, unsigned n )
- {
- bool evenFl = cmIsEvenU(n);
- unsigned medIdx = evenFl ? n/2 : (n+1)/2;
- return VECT_OP_FUNC(MedianSearch)( medIdx, bp, n, &evenFl );
- }
-
-
- unsigned VECT_OP_FUNC(MinIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
- {
- const VECT_OP_TYPE* ep = bp + (n*stride);
- if( bp >= ep )
- return cmInvalidIdx;
-
- const VECT_OP_TYPE* p = bp;
- const VECT_OP_TYPE* mp = bp;
-
- bp+=stride;
-
- for(; bp < ep; bp+=stride )
- if( *bp < *mp )
- mp = bp;
-
- return (mp - p)/stride;
- }
-
- unsigned VECT_OP_FUNC(MaxIndex)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
- {
- const VECT_OP_TYPE* ep = bp + (n*stride);
-
- if( bp >= ep )
- return cmInvalidIdx;
-
- const VECT_OP_TYPE* p = bp;
- const VECT_OP_TYPE* mp = bp;
-
- bp+=stride;
-
- for(; bp < ep; bp+=stride )
- if( *bp > *mp )
- mp = bp;
-
- return (mp - p)/stride;
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(Min)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
- {
- unsigned i;
-
- if((i = VECT_OP_FUNC(MinIndex)(bp,n,stride)) == cmInvalidIdx )
- {
- assert(0);
- return 0;
- }
- return bp[i*stride];
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(Max)( const VECT_OP_TYPE* bp, unsigned n, unsigned stride )
- {
- unsigned i;
-
-
- if((i = VECT_OP_FUNC(MaxIndex)(bp,n,stride)) == cmInvalidIdx )
- {
- assert(0);
- return 0;
- }
- return bp[i*stride];
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MinVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
- {
- unsigned i;
- for(i=0; i<dn; ++i)
- if( sp[i] < dp[i] )
- dp[i] = sp[i];
- return dp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(MaxVV)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp )
- {
- unsigned i;
- for(i=0; i<dn; ++i)
- if( sp[i] > dp[i] )
- dp[i] = sp[i];
-
- return dp;
- }
-
- unsigned* VECT_OP_FUNC(MinIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
- {
- unsigned i = 0;
- for(i=0; i<scn; ++i)
- dp[i] = VECT_OP_FUNC(MinIndex)(sp + (i*srn), srn, 1 );
- return dp;
- }
-
- unsigned* VECT_OP_FUNC(MaxIndexM)( unsigned* dp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
- {
- unsigned i = 0;
- for(i=0; i<scn; ++i)
- dp[i] = VECT_OP_FUNC(MaxIndex)(sp + (i*srn), srn, 1 );
- return dp;
- }
-
- bool VECT_OP_FUNC(IsEqual)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn )
- {
- const VECT_OP_TYPE* ep = s0p + sn;
- for(; s0p < ep; ++s0p,++s1p )
- if( *s0p != *s1p )
- return false;
- return true;
- }
-
- bool VECT_OP_FUNC(IsClose)( const VECT_OP_TYPE* s0p, const VECT_OP_TYPE* s1p, unsigned sn, double pct )
- {
- const VECT_OP_TYPE* ep = s0p + sn;
- for(; s0p < ep; ++s0p,++s1p )
- {
- double d = abs(*s1p - *s0p);
- double s = cmMin(*s1p,*s0p);
- if( d*100.0/s > pct )
- return false;
- }
- return true;
- }
-
-
- VECT_OP_TYPE VECT_OP_FUNC(Mode)( const VECT_OP_TYPE* sp, unsigned sn )
- {
- unsigned n[sn];
- VECT_OP_TYPE v[sn];
- unsigned i,j,k = 0;
- unsigned n0 = 0; // idx of most freq occurring ele
- unsigned n1 = -1; // idx of 2nd most freq occurring ele
-
- for(i=0; i<sn; ++i)
- {
- // find sp[i] in v[]
- for(j=0; j<k; ++j)
- if( sp[i] == v[j] )
- {
- ++n[j];
- break;
- }
-
- // sp[i] was not found in v[]
- if( k == j )
- {
- v[j] = sp[i];
- n[j] = 1;
- ++k;
- }
-
- // n[j] holds frq of sp[i]
-
- // do nothing if j is already most freq
- if( j != n0 )
- {
- // if j is new most freq
- if( n[j] > n[n0] )
- {
- n1 = n0;
- n0 = j;
- }
- else
- // if j is 2nd most freq
- if( (n1==-1) || (n[j] > n[n1]) )
- n1 = j;
- }
-
- // if diff between two most freq is greater than remaining ele's
- if( (n1!=-1) && (n[n0]-n[n1]) >= (sn-i) )
- break;
-
- }
-
-
- // if there are no ele's with same count
- if( n[n0] > n[n1] )
- return v[n0];
-
- // break tie between ele's with same count be returning min value
- // (this is the same as Matlab tie break criteria)
- j = 0;
- for(i=1; i<k; ++i)
- if( (n[i] > n[j]) || (n[i] == n[j] && v[i] < v[j]) )
- j=i;
-
- return v[j];
- }
-
- unsigned VECT_OP_FUNC(Find)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
- {
- const VECT_OP_TYPE* sbp = sp;
- const VECT_OP_TYPE* ep = sp + sn;
- while( sp<ep )
- if( *sp++ == key )
- break;
-
- if( sp==ep )
- return cmInvalidIdx;
-
- return (sp-1) - sbp;
- }
-
- unsigned VECT_OP_FUNC(Count)( const VECT_OP_TYPE* sp, unsigned sn, VECT_OP_TYPE key )
- {
- unsigned cnt = 0;
- const VECT_OP_TYPE* ep = sp + sn;
- while( sp<ep )
- if( *sp++ == key )
- ++cnt;
-
- return cnt;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(ReplaceLte)( VECT_OP_TYPE* dp, unsigned dn, const VECT_OP_TYPE* sp, VECT_OP_TYPE lteKeyVal, VECT_OP_TYPE replaceVal )
- {
- VECT_OP_TYPE* rp = dp;
- const VECT_OP_TYPE* ep = dp + dn;
-
- for(; dp < ep; ++sp )
- *dp++ = *sp <= lteKeyVal ? replaceVal : *sp;
-
-
-
- return rp;
- }
-
- VECT_OP_TYPE* VECT_OP_FUNC(Diag)( VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
- {
- unsigned i,j;
- for(i=0,j=0; i<n && j<n; ++i,++j)
- dbp[ (i*n) + j ] = sbp[i];
-
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(DiagZ)(VECT_OP_TYPE* dbp, unsigned n, const VECT_OP_TYPE* sbp )
- {
- VECT_OP_FUNC(Fill)(dbp,n*n,0);
- return VECT_OP_FUNC(Diag)(dbp,n,sbp);
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Identity)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
- {
- unsigned i,j;
- for(i=0,j=0; i<cn && j<rn; ++i,++j)
- dbp[ (i*rn) + j ] = 1;
-
- return dbp;
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(IdentityZ)( VECT_OP_TYPE* dbp, unsigned rn, unsigned cn )
- {
- VECT_OP_FUNC(Fill)(dbp,rn*cn,0);
- return VECT_OP_FUNC(Identity)(dbp,rn,cn);
- }
-
-
- VECT_OP_TYPE* VECT_OP_FUNC(Transpose)( VECT_OP_TYPE* dbp, const VECT_OP_TYPE* sp, unsigned srn, unsigned scn )
- {
- VECT_OP_TYPE* dp = dbp;
- const VECT_OP_TYPE* dep = dbp + (srn*scn);
-
- while( dbp < dep )
- {
- const VECT_OP_TYPE* sbp = sp++;
- const VECT_OP_TYPE* sep = sbp + (srn*scn);
-
- for(; sbp < sep; sbp+=srn )
- *dbp++ = *sbp;
- }
-
- return dp;
- }
-
- VECT_OP_TYPE VECT_OP_FUNC(Seq)( VECT_OP_TYPE* dbp, unsigned dn, VECT_OP_TYPE beg, VECT_OP_TYPE incr )
- {
- const VECT_OP_TYPE* dep = dbp + dn;
- unsigned i = 0;
- for(; dbp<dep; ++i)
- *dbp++ = beg + (incr*i);
- return beg + (incr*i);
- }
-
-
- void VECT_OP_FUNC(FnThresh)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride, VECT_OP_TYPE (*fnPtr)(const VECT_OP_TYPE*, unsigned) )
- {
- int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
- int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
- int i,j;
-
- i0 = -i0;
-
- if( fnPtr == NULL )
- fnPtr = &(VECT_OP_FUNC(Median));
-
- for(i=0; i<xN; ++i,++i0,++i1)
- {
- j = (i*yStride);
- if( i0 < 0 )
- if( i1 >= xN )
- yV[j] = (*fnPtr)(xV,xN);
- else
- yV[j] = (*fnPtr)(xV,i1+1);
- else if( i1 >= xN )
- yV[j] = (*fnPtr)(xV+i0,xN-i0);
- else
- yV[j] = (*fnPtr)(xV+i0,wndN);
- }
- }
-
-
- void VECT_OP_FUNC(MedianFilt)( const VECT_OP_TYPE* xV, unsigned xN, unsigned wndN, VECT_OP_TYPE* yV, unsigned yStride )
- {
- int i0 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2;
- int i1 = cmIsOddU(wndN) ? (wndN-1)/2 : wndN/2 - 1;
- int i,j;
- VECT_OP_TYPE tV[ wndN ];
-
- i0 = -i0;
-
- VECT_OP_FUNC(Fill)(tV,wndN,0);
-
- for(i=0; i<xN; ++i,++i0,++i1)
- {
-
- j = (i*yStride);
-
- // note that the position of the zero padding in tV[]
- // does not matter because the median calcluation does
- // not make any assumptions about the order of the argument
- // vector.
-
- if( i0 < 0 )
- {
- VECT_OP_FUNC(Copy)(tV,wndN+i0,xV);
- VECT_OP_FUNC(Fill)(tV+wndN+i0,labs(i0),0);
- //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
-
- yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
- continue;
- }
-
-
-
- if( i1 >= xN )
- {
- VECT_OP_FUNC(Copy)(tV,wndN-(i1-xN+1),xV+i0);
- VECT_OP_FUNC(Fill)(tV+wndN-(i1-xN+1),i1-xN+1,0);
- //VECT_OP_FUNC(Print)(NULL,1,wndN,tV,-1,-1);
-
- yV[j] = VECT_OP_FUNC(Median)(tV,wndN);
- continue;
- }
-
- //VECT_OP_FUNC(Print)(NULL,1,wndN,xV+i0,-1,-1);
- yV[j] = VECT_OP_FUNC(Median)(xV+i0,wndN);
-
- }
- }
-
- unsigned* VECT_OP_FUNC(LevEditDistAllocMtx)(unsigned maxN)
- {
- maxN += 1;
-
- unsigned* m = cmMemAllocZ(unsigned,maxN*maxN);
- unsigned* p = m;
- unsigned i;
-
- // initialize the comparison matrix with the default costs in the
- // first row and column
- // (Note that this matrix is not oriented in column major order like most 'cm' matrices.)
- for(i=0; i<maxN; ++i)
- {
- p[i] = i; // 0th row
- p[ i * maxN ] = i; // 0th col
- }
-
- return m;
- }
-
- double VECT_OP_FUNC(LevEditDist)(unsigned mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, unsigned maxN )
- {
- mtxMaxN += 1;
-
- assert( n0 < mtxMaxN && n1 < mtxMaxN );
-
- int v = 0;
- unsigned i;
- // Note that m[maxN,maxN] is not oriented in column major order like most 'cm' matrices.
-
- for(i=1; i<n0+1; ++i)
- {
- unsigned ii = i * mtxMaxN; // current row
- unsigned i_1 = ii - mtxMaxN; // previous row
- unsigned j;
- for( j=1; j<n1+1; ++j)
- {
- int cost = s0[i-1] == s1[j-1] ? 0 : 1;
-
- //m[i][j] = min( m[i-1][j] + 1, min( m[i][j-1] + 1, m[i-1][j-1] + cost ) );
-
- m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
- }
- }
- return (double) v / maxN;
- }
-
-
- double VECT_OP_FUNC(LevEditDistWithCostThresh)( int mtxMaxN, unsigned* m, const VECT_OP_TYPE* s0, int n0, const VECT_OP_TYPE* s1, int n1, double maxCost, unsigned maxN )
- {
- mtxMaxN += 1;
-
- int v = 0;
-
- maxCost = cmMin(1.0,cmMax(0.0,maxCost));
-
- int iMaxCost = ceil( maxCost * maxN );
-
- assert( iMaxCost > 0 && maxCost > 0 );
-
- // If the two strings are different lengths and the min possible distance is
- // greater than the threshold then return the threshold as the cost.
- // (Note: For strings of different length the min possible distance is the
- // difference in length between the two strings).
- if( abs(n0-n1) > iMaxCost )
- return maxCost;
-
- int i;
- // for each row in the matrix ...
- for(i=1; i<n0+1; ++i)
- {
- int ii = i * mtxMaxN; // current row
- int i_1 = ii - mtxMaxN; // previous row
-
- // Limit the row to (2*iMaxCost)+1 diagnal strip.
- // This strip is based on the idea that the best case can be precomputed for
- // all matrix elements in advance - where the best case for position i,j is:
- // abs(i-j). This can be justified based on the idea that the least possible
- // distance between two strings of length i and j is abs(i-1). The minimum least
- // possible distance is therefore found on the matrix diagnal and grows as the
- // distance from the diagnal increases.
-
- int ji = cmMax( 1, i - iMaxCost );
- int jn = cmMin(iMaxCost + i, n1) + 1;
- int j;
-
- // fill in (max cost + 1) as the value in the column before the starting column
- // (it will be referred to during the first computation in this row)
- if( ji >= 2 )
- m[ ii + (ji-1) ] = iMaxCost + 1;
-
- // for each column in the diagnal stripe - beginning with the leftmost column.
- for( j=ji; j<jn; ++j)
- {
- int cost = s0[i-1] == s1[j-1] ? 0 : 1;
-
- m[ ii + j ] = v = cmMin( m[ i_1 + j] + 1, cmMin( m[ ii + j - 1] + 1, m[ i_1 + j - 1 ] + cost ) );
- }
-
- // fill in (max cost + 1) in the column following the last column
- // (it will be referred to during computation of the following row)
- if( j < n1+1 )
- m[ii + j] = iMaxCost + 1;
- }
-
- assert( v >= 0 );
-
-
- return cmMin( maxCost , (double) v / maxN);
- }
-
- #endif
|