#undef __STRICT_ANSI__ #include #include #include #include #include #include #ifdef __ALTIVEC__ #include #endif #include "vp.h" #undef FILT_DEBUG #define DO_TESTS #if defined(DO_MAIN) && !defined(DO_TESTS) #define DO_TESTS #endif #define VELMS ((VECTSZ)/sizeof(float)) /* 4 */ typedef union { vFloat v; float e[VELMS]; } vFloatU; #ifdef __ALTIVEC__ /* NOTE: the Newton-Raphson refinement step doesn't handle * 'infinity' / division by zero correctly! */ static __inline__ vFloat vec_div(vFloat y, vFloat x, vFloat const_1) { register vFloat xr = vec_re(x); xr = vec_madd( vec_nmsub( xr, x, const_1 ), xr, xr ); return y*xr; } #endif /* Rational approximation on -1..1 * 1 + a v^2 + b v^4 * atan(v) ~ v ------------------------- * 1 + c v^2 + d v^4 + e v^6 * * can be restated as * * v ( 1 - v^2 ( e v^4 + (d-b) v^2 + (c-a) ) * ------------- */ #define FABS(f,s) (vFloat)(((vUns)(f)) & ~(s)) #define VFCONST(x) ((vFloat){x,x,x,x}) #ifdef DO_TESTS #if 0 typedef uint32_t VpTimeStamp; #define TSUNIT "CPU clicks" #ifdef __PPC__ static __inline__ void rdtimer(VpTimeStamp *pts) { #ifdef __rtems__ __asm__ __volatile__("mfspr %0, 268":"=r"(*pts)); #else __asm__ __volatile__("mftb %0":"=r"(*pts)); #endif } #elif defined(__i386__) || defined(__x86_64__) static __inline__ void rdtimer(VpTimeStamp *pts) { uint64_t rval; __asm__ __volatile__("rdtsc":"=A"(rval)); *pts = (uint32_t)rval; } #else #define rdtimer() 0 #endif static __inline__ uint32_t usdiff(VpTimeStamp *now, VpTimeStamp *then) { return *now-*then; } #else typedef struct timespec VpTimeStamp; #define TSUNIT "us" static __inline__ void rdtimer(VpTimeStamp *pts) { clock_gettime(CLOCK_REALTIME, pts); } static __inline__ uint32_t usdiff(struct timespec *now, struct timespec *then) { int32_t usdiff = (now->tv_nsec - then->tv_nsec)/1000; uint32_t sdiff = now->tv_sec - then->tv_sec; if ( usdiff < 0 ) { sdiff--; usdiff += 1000000; } return sdiff * 1000000 + usdiff; } #endif static void prtsdiff(VpTimeStamp *now, VpTimeStamp *then, FILE *f) { if ( !f ) f = stdout; fprintf(f,"Execution took %"PRIu32" %s\n", usdiff(now, then), TSUNIT); } #endif #ifdef __ALTIVEC__ /* Switch off C9x/Java mode to avoid altivec-assist * exceptions when operations return denormalized * numbers (mpc7457). We're not terribly concerned * about accuracy here... */ void altivec_nonC9xmode() { /* Altivec initialization */ vector unsigned short vscr = {0,0,0,0,0,0,1,0}; vscr = vec_or(vscr, vec_mfvscr()); /* enable non-java mode to avoid 'almost-zero' exceptions; * we're not terribly concerned about accuracy here */ vec_mtvscr(vscr); } #define IINC 16 #define VEC_ADDR(idx,vec) ( ((uintptr_t)(vec)) + (idx)) static __inline__ int vec_any_outofbnd(vFloat x,vFloat bound,vUns sgnmsk) { return vec_any_out(x,bound); } #define vec_ctf0(a) vec_ctf(a,0) #define vec_splat0(x) vec_splat(x,0) #define vec_splat1(x) vec_splat(x,1) #define vec_splat2(x) vec_splat(x,2) #define vec_splat3(x) vec_splat(x,3) #define vec_any_eqh(a,b) vec_any_eq(a,b) static __inline__ vShort vec_swap8(vShort x) { return vec_sld(x,x,8); } #else /* __ALTIVEC__ */ void altivec_nonC9xmode() { } #define IINC 1 #define VEC_ADDR(idx,vec) ( (uintptr_t) ((vec)+(idx))) #define vec_ld(idx,arr) arr[idx] #define vec_st(v,i,a) do { a[i]=(v); } while (0) #define vec_div(y,x,c1) (y)/(x) #define vec_sel(a,b,c) ((vFloat)(((vUns)(b) & (vUns)(c)) | ((vUns)(a) & ~(vUns)(c)))) #ifdef __SSE__ #define vec_cmplt(a,b) ((vUns)__builtin_ia32_cmpltps(a,b)) #define vec_cmpeq(a,b) ((vUns)__builtin_ia32_cmpeqps(a,b)) static __inline__ int vec_any_outofbnd(vFloat x,vFloat bound,vUns sgnmsk) { x = FABS(x, sgnmsk); return __builtin_ia32_movmskps((vFloat)__builtin_ia32_cmpnleps(x,bound)); } static __inline__ vFloat vec_srd4_ia32(vFloat a, vFloat b) { /* copy 1st elements of b into a (shufps cannot move * across 2-int element boundary). * * [ a3, a2, a1, a0], [b3, b2, b1, b0] * \ \ / / * \ \ / / * \ \ / / * \ \ / / * -> [ a3, a2, b1, b0 ] */ a = __builtin_ia32_shufps(b,a,0xe4 /* 3210 */); /* * build [ b2, b1, b0, a3 ] * / / \ / * / / / \ * / / / \ * / / / \ * [b3, b2, b1, b0], [ a3, a2, b1, b0 ] */ return __builtin_ia32_shufps(a,b, 0x93 /* 2103*/); } static __inline__ vFloat vec_splat0(register vFloat v) { return __builtin_ia32_shufps(v,v,0x00); } static __inline__ vFloat vec_splat1(register vFloat v) { return __builtin_ia32_shufps(v,v,0x55); } static __inline__ vFloat vec_splat2(register vFloat v) { return __builtin_ia32_shufps(v,v,0xaa); } static __inline__ vFloat vec_splat3(register vFloat v) { return __builtin_ia32_shufps(v,v,0xff); } static __inline__ vShort vec_swap8(vShort x) { return (vShort)__builtin_ia32_shufps((vFloat)x,(vFloat)x,0x4e); } #ifdef __SSE2__ #define vec_ctf0(i) __builtin_ia32_cvtdq2ps(i) /* SSE is little-endian; must use punpcklwd here */ #define vec_unpackh(s) __builtin_ia32_psradi128((vInt)__builtin_ia32_punpcklwd128((s),(s)),16) /* SSE is little-endian; must use punpckhwd here */ #define vec_unpackl(s) __builtin_ia32_psradi128((vInt)__builtin_ia32_punpckhwd128((s),(s)),16) #if 0 /* For some reason these operate on non 16-byte vectors ??? */ #define vec_mergeh(a,b) __builtin_ia32_punpckhps(a,b) #define vec_mergel(a,b) __builtin_ia32_punpcklps(a,b) #else /* SSE is little-endian; must use punpckldq128 here */ #define vec_mergeh(a,b) (vFloat)__builtin_ia32_punpckldq128((vInt)a,(vInt)b) /* SSE is little-endian; must use punpckhdq128 here */ #define vec_mergel(a,b) (vFloat)__builtin_ia32_punpckhdq128((vInt)a,(vInt)b) #endif #define vec_any_eqh(a,b) (__builtin_ia32_pmovmskb128((vChar)__builtin_ia32_pcmpeqw128(a,b)) ? 1 : 0) #else /* __SSE2__ */ #error "vec_ctf0, vec_unpackh, vec_unpackl, vec_mergeh, vec_mergel, vec_any_eqh not implemented -- use SSE2 or remove this message to use the non-vectorized versions" #endif /* __SSE2__ */ #else /* __SSE__ */ static __inline__ vUns vec_cmplt(vFloat a, vFloat b) { vUns res; uns32_a *rf = (uns32_a*)&res; float_a *af = (float_a*)&a; float_a *bf = (float_a*)&b; rf[0] = af[0] < bf[0] ? -1 : 0; rf[1] = af[1] < bf[1] ? -1 : 0; rf[2] = af[2] < bf[2] ? -1 : 0; rf[3] = af[3] < bf[3] ? -1 : 0; return res; } static __inline__ vUns vec_cmpeq(vFloat a, vFloat b) { vUns res; unsigned *rf = (unsigned*)&res; float *af = (float*)&a; float *bf = (float*)&b; rf[0] = af[0] == bf[0] ? -1 : 0; rf[1] = af[1] == bf[1] ? -1 : 0; rf[2] = af[2] == bf[2] ? -1 : 0; rf[3] = af[3] == bf[3] ? -1 : 0; return res; } static __inline__ int vec_any_outofbnd(vFloat x,vFloat bound,vUns sgnmsk) { float_a *xp = (float_a*)&x, *bp = (float_a*)&bound; x = FABS(x, sgnmsk); return xp[0] > bp[0] || xp[1] > bp[1] || xp[2] > bp[2] || xp[3] > bp[3]; } static __inline__ vFloat vec_perm(vFloat a, vFloat b, vByte c) { int i; vFloat res; uint8_t *pr = (uint8_t*)&res; uint8_t *pa = (uint8_t*)&a; uint8_t *pb = (uint8_t*)&b; uint8_t *pc = (uint8_t*)&c; for ( i=0; i0, y>0 : fi = atan(|y/x|) * x<0, y>0 : fi = PI - atan(|y/x|) * x>0, y<0 : fi = - atan(|y/x|) * x<0, y<0 : fi = -PI + atan(|y/x|) */ static __inline__ vFloat vec_atanf_1(vFloat y, vFloat x) { /* AltiVec has 32 registers; we can keep the constants around w/o need * for re-loading. * * NOTE: if the caller of this 'inline' code executes vec_atanf_1 in a loop, * the compiler hopefully moves loading the constants out of the loop * (I double-checked that gcc-4 does this). */ register const vFloat const_1 = VFCONST(1.); register const vFloat const_0 = VFCONST(0.); register const vFloat const_p1 = VFCONST(0.899376603633967162376852668 ); register const vFloat const_p2 = VFCONST(0.1379537432000611625119608928 ); register const vFloat const_q1 = VFCONST(1.232708631171391230842915467 ); register const vFloat const_q2 = VFCONST(0.348885004241092208440874528 ); register const vFloat const_q3 = VFCONST(0.01241596898229266147502864921); #if 1 /*sizeof(rr)/sizeof(float)/4 ); * Weird -- if the constant is defined as (1./0.) it is reloaded * every time this inline routine is 'called' from a loop; * using __builtin_inff() [or the hard-coded bit-pattern] works better. */ register const vFloat const_infty = VFCONST(__builtin_inff()); #else register const vFloat const_infty = (vFloat){ 0x7f800000, 0x7f800000, 0x7f800000, 0x7f800000 }; #endif register const vFloat const_mpiover2 = VFCONST(-1.5707963268); register const vUns sgnmsk = {1<<31, 1<<31, 1<<31, 1<<31 }; register vFloat v,v2,P,Q,p0; register vUns sgn,xly,xl0,xlys; /* If x < 0 rotate by -PI/2 (y>0) or +Pi/2 (y<0), respectively */ /* if ( x < 0 ) { * if ( y >= 0 ) { * t = x; * x = y * y = -t; * res = PI/2; * } else { * t = x; * x = -y; * y = t; * res = -Pi/2; * } * } * * which is equivalent to * * if ( x < 0 ) { * x = yabs; * y = sign(y) * xabs; // y = (y >=0) ? -x : x; -> y = (y>=0) ? xabs : - xabs; * res = sign(y)*PI/2; * } */ xl0 = (vUns)vec_cmplt(x, const_0); sgn = (vUns)y & sgnmsk; /* rotate towards right half plane */ { /* sign(y) ^ xabs == sign(y) ^ sign(x) ^ x */ v = (vFloat)(sgn | (vUns)FABS(x, sgnmsk)); /* note: sign(y) will be multiplied at the * very end when the result is ^ sgn */ p0 = (vFloat)((vUns)const_mpiover2 ^ sgnmsk); /* Now select the rotated or unrotated versions, * dependent on xl0 */ x = vec_sel(x, FABS(y,sgnmsk), xl0); y = vec_sel(y, v, xl0); p0 = vec_sel(const_0, p0, xl0); } /* At this point, x is guaranteed to be positive */ /* strip off sign */ y = FABS(y,sgnmsk); /* if x < y swap them */ xly = (vUns)vec_cmplt(x,y); v = vec_sel(y,x,xly); x = vec_sel(x,y,xly); /* We have the following cases: * * x < y: FALSE, sign(y/x): +1 -> + atan(|y/x|) * x < y: TRUE , sign(x/y): +1 -> Pi/2 - atan(|x/y|) * x < y: FALSE, sign(y/x): -1 -> - atan(|y/x|) * x < y: TRUE , sign(x/y): -1 -> - Pi/2 + atan(|x/y|) * * { |y/x| for |y| <= |x| } * If define v = { } * { |x/y| for |x| < |y| } * * and xly = truth value of |x| < |y| * * sgn = truth value of sign(x/y) == -1 * * then we can summarize in the following table: * * xly sgn result * F F atan(v) == + ( 0 + atan(v) ) * F T - atan(v) == - ( 0 + atan(v) ) * T F Pi/2 - atan(v) == - ( - Pi/2 + atan(v) ) * T T - Pi/2 + atan(v) == + ( - Pi/2 + atan(v) ) * * { 0 if |y| <= |x| } * Hence, if we define P0 = { } * { -Pi/2 if |x| < |y| } * * { -1 if sgn ^ xly == TRUE } * S = { } * { +1 if sgn ^ xly == FALSE } * * then we obtain * * atan(y/x) = S * ( P0 + atan(v) ) */ /* calculate S == sgn ^ xly */ xlys = xly & sgnmsk; sgn = sgn ^ xlys; /* since sign of xly is factored in (final result is ^sgn(y)^xly) * we must flip the sign of p0 by xly so that the 1st contribution * to p0 (caused by x < 0) ends up as Pi/2 ^ sign(y) in the * final result... */ p0 = (vFloat)((vUns)p0 ^ xlys); /* select p0: -Pi/2 if |x|<|y|, 0. otherwise */ p0 += vec_sel(const_0,const_mpiover2,xly); /* Rational approximation */ v2 = vec_sel(vec_div(v,x,const_1), const_0, vec_cmpeq(x,const_infty)); /* Special hack; cheat at singularity atan2(0.,0.) and let it return 0.; * this is what glibc/libm and scilab are returning, too. Dunno if it's * POSIX/standard behavior. */ v = vec_sel(v2, const_0, vec_cmpeq(v, const_0) & vec_cmpeq(x, const_0)); v2 = v*v; Q = const_1 + v2 * (const_q1 + v2 * (const_q2 + v2 * const_q3)); P = const_1 + v2 * (const_p1 + v2 * const_p2); P = vec_div(P,Q,const_1); /* Add -Pi/2 if necessary */ P = p0 + v*P; /* 'multiply' sign back */ P = (vFloat)((vUns)P ^ sgn); return P; } void vec_atanf(vFloat *pr, vFloat *py, vFloat *px, int dim) __attribute__((noinline)); void vec_atanf(vFloat *pr, vFloat *py, vFloat *px, int dim) { register vFloat x,y; dim *= IINC; while ( dim > 0 ) { dim -= IINC; x = vec_ld(dim,px); y = vec_ld(dim,py); vec_st(vec_atanf_1(y,x),dim,pr); } } /* compute cumulative phase response of an array of complex values */ void vec_phase(vFloat *pr, vFloat *pim, vFloat *pre, int dim) { register vFloat zro = VFCONST(0.); register vFloat one = VFCONST(1.); register vFloat re_n_1 = one; register vFloat im_n_1 = zro; register vFloat re_n; register vFloat im_n; register vFloat num; register vFloat den; int i; vpVect d_phi; double phase = 0.; #ifdef __SSE__ #define VEC_SRD4(a,b) vec_srd4_ia32(a,b) #else register vByte const_srd4 = { 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b }; #define VEC_SRD4(a,b) vec_perm(a,b,const_srd4) #endif dim *= IINC; for ( i = 0; i < dim; i+=IINC ) { re_n = vec_ld(i,pre); im_n = vec_ld(i,pim); /* compute atan of difference: * atan(phi(n) - phi(n-1)) * * tg(phi(n)) - tg(phi(n-1)) * = atan{ --------------------------- } * 1 + tg(phi(n)) tg(phi(n-1)) * * im(n) re(n-1) - im(n-1) re(n) * = atan{ ----------------------------- } * re(n) re(n-1) + im(n) im(n-1) * */ /* This breaks down in the singular points re=im=zero. * We set re=1. in this case, thus 'defining' the phase * in these points to be zero (a case of zero/zero = zero ;-) */ re_n = vec_sel(re_n, one, vec_cmpeq(re_n,zro) & vec_cmpeq(im_n,zro)); /* compute 'shifted' sequence z(n-1) */ re_n_1 = VEC_SRD4(re_n_1, re_n); im_n_1 = VEC_SRD4(im_n_1, im_n); num = im_n*re_n_1 - im_n_1*re_n; den = re_n*re_n_1 + im_n*im_n_1; d_phi.vf = vec_atanf_1( num, den ); /* save this sample for next iteration */ re_n_1 = re_n; im_n_1 = im_n; /* compute the cumulative phase; this cannot be * done efficiently in the vector engine */ phase += d_phi.af[0]; ((float_a*)VEC_ADDR(i,pr))[0] = phase; phase += d_phi.af[1]; ((float_a*)VEC_ADDR(i,pr))[1] = phase; phase += d_phi.af[2]; ((float_a*)VEC_ADDR(i,pr))[2] = phase; phase += d_phi.af[3]; ((float_a*)VEC_ADDR(i,pr))[3] = phase; } } /* Return: number of rescaling operations performed */ int vec_tadj(double *pdel_phi, double *pdel_T, double *pRMS, vFloat *pHref_re, vFloat *pHref_im, vFloat *pH_re, vFloat *pH_im, int dim) { register vFloat one = VFCONST(1.); register vFloat zro = VFCONST(0.); register vFloat re_n_1 = one; register vFloat im_n_1 = zro; register vFloat Href_re, Href_im, H_re, H_im, W; register vFloat m11 = im_n_1; register vFloat m12 = im_n_1; register vFloat m22 = im_n_1; register vFloat y1 = im_n_1; register vFloat y2 = im_n_1; register vFloat f = (vFloat){0.,1.,2.,3.}; register vFloat finc= (vFloat){4.,4.,4.,4.}; register vFloat re_n; register vFloat im_n; register vFloat num; register vFloat den; register vFloat tmpv; register vFloat energy = zro; int rescaled = 0; #ifndef NO_SCALING register vFloat scl = re_n_1; /* Define bounds for scaling; we assume |H|*|Href| can always be represented * by single-precision floats. Computing powers of that for the weighting function * may represent a problem. * NOTE: the bounds have to be adapted to the powers we take to compute the * weight; for now we use W = (|H|*|Href|)^4. Hence, if we want that to * fit into an exponent of 10^38 then the bound on |H||Href| is 38/4 ~9. * We want some margin so we pick 10E6. * * A smaller bound results in more frequent rescaling than a larger one. */ register const vFloat bnd = VFCONST(1.0E+3); register const vFloat one_over_bnd = VFCONST(1.0E-3); register const vUns sgnmsk = {1<<31, 1<<31, 1<<31, 1<<31 }; #endif int i; vFloatU d_phi; vFloatU tmp; double phase = 0.; double Y1, Y2, M11, M12, M22, det; double nsmpl = dim*sizeof(vFloat)/sizeof(float); #ifdef __SSE__ #define VEC_SRD4(a,b) vec_srd4_ia32(a,b) #else register vByte const_srd4 = { 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b }; #define VEC_SRD4(a,b) vec_perm(a,b,const_srd4) #endif dim *= IINC; dim -= IINC; for ( i = 0; i <= dim; i+=IINC ) { Href_re = vec_ld(i, pHref_re); Href_im = vec_ld(i, pHref_im); H_re = vec_ld(i, pH_re ); H_im = vec_ld(i, pH_im ); /* Href * conj(H) */ re_n = (Href_re*H_re + Href_im*H_im)*scl; im_n = (Href_im*H_re - Href_re*H_im)*scl; #ifndef NO_SCALING if ( vec_any_outofbnd(re_n, bnd, sgnmsk) || vec_any_outofbnd(im_n, bnd, sgnmsk) ) { /* Need to rescale */ re_n *= one_over_bnd; im_n *= one_over_bnd; re_n_1 *= one_over_bnd; im_n_1 *= one_over_bnd; tmpv = one_over_bnd * one_over_bnd; /* tmp1 now 1/bnd^2 */ energy *= tmpv; tmpv *= tmpv; /* tmp1 now 1/bnd^4 */ y1 *= tmpv; y2 *= tmpv; m11 *= tmpv; m12 *= tmpv; m22 *= tmpv; /* use new scale */ scl *= one_over_bnd; rescaled++; } #endif /* compute weight; a power of the modulus of |Href||H| */ W = re_n*re_n + im_n*im_n; /* 'integrate' signal energy */ energy += W; /* NOTE: if the power is changed from (|Href||H|)^4 then * the bounds and power in the scaling algorithm mus * be adapted! */ W = W*W; /* should we do one more ? */ /* 1st and last element (assume fft has even number of elements) * are never taken. (const phase shift * * phase(jw) = j sign(w) * PHI * * is zero at w==0 because the phase is odd) */ if ( 0 == i ) { tmp.v = W; tmp.e[0] = (float)0.; W = tmp.v; } else if ( dim == i ) { tmp.v = W; tmp.e[3] = (float)0.; W = tmp.v; } /* compute atan of difference: * atan(phi(n) - phi(n-1)) * * tg(phi(n)) - tg(phi(n-1)) * = atan{ --------------------------- } * 1 + tg(phi(n)) tg(phi(n-1)) * * im(n) re(n-1) - im(n-1) re(n) * = atan{ ----------------------------- } * re(n) re(n-1) + im(n) im(n-1) * */ /* This breaks down in the singular points re=im=zero. * We set re=1. in this case, thus 'defining' the phase * in these points to be zero (a case of zero/zero = zero ;-) */ re_n = vec_sel(re_n, one, vec_cmpeq(re_n,zro) & vec_cmpeq(im_n,zro)); /* compute 'shifted' sequence z(n-1) */ re_n_1 = VEC_SRD4(re_n_1, re_n); im_n_1 = VEC_SRD4(im_n_1, im_n); num = im_n*re_n_1 - im_n_1*re_n; den = re_n*re_n_1 + im_n*im_n_1; d_phi.v = vec_atanf_1( num, den ); /* save this sample for next iteration */ re_n_1 = re_n; im_n_1 = im_n; /* compute the cumulative phase; this cannot be * done efficiently in the vector engine */ phase += d_phi.e[0]; d_phi.e[0] = phase; phase += d_phi.e[1]; d_phi.e[1] = phase; phase += d_phi.e[2]; d_phi.e[2] = phase; phase += d_phi.e[3]; d_phi.e[3] = phase; #ifdef DEBUG printf("%10.7g %10.7g %10.7g %10.7g\n",d_phi.e[0],d_phi.e[1],d_phi.e[2],d_phi.e[3]); #endif /* Hopefully, the compiler optimizes redundant multiplications * by remembering intermediate steps... */ y1 += W*d_phi.v; y2 += W*d_phi.v*f; m11 += W; m12 += W*f; m22 += W*f*f; f += finc; } /* Final horizontal sum */ tmp.v = y1; Y1 = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; tmp.v = y2; Y2 = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; tmp.v = m11; M11 = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; tmp.v = m12; M12 = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; tmp.v = m22; M22 = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; tmp.v = energy; *pRMS = tmp.e[0] + tmp.e[1] + tmp.e[2] + tmp.e[3]; /* undo the scaling and normalize; */ tmp.v = scl; *pRMS = sqrt(*pRMS/nsmpl)/(double)tmp.e[0]; #if defined(DEBUG) printf("%10.7g %10.7g %10.7g ; %10.7g %10.7g\n",M11,M12,M22,Y1,Y2); #endif det = (M11*M22 - M12*M12); if ( 0. == det ) { /* pathological case; e.g., because we have a CW signal so * we only have one free parameter. * set del_T == 0 and calculate del_phi only. */ *pdel_T = 0.; *pdel_phi = Y1/M11; /* Note: if M11 is zero, too, then so must be Y1 and * we have a true NaN situation (no signal). */ return rescaled; } #if defined(DEBUG) printf("%15.10g %15.10g %15.10g\n", M11, M12, M22); printf("%15.10g %15.10g %15.10g\n", det, M11*M22, M12*M12); #endif det = 1./det; *pdel_phi = ( M22*Y1 - M12*Y2)*det; *pdel_T = (-M12*Y1 + M11*Y2)*det; return rescaled; } #ifdef DO_TESTS unsigned tadjtst(unsigned nloops, int nelms, unsigned scale) { double phi, dT, rms; float rr[nelms+4] __attribute__((aligned(VECTSZ))); float ri[nelms+4] __attribute__((aligned(VECTSZ))); float hr[nelms+4] __attribute__((aligned(VECTSZ))); float hi[nelms+4] __attribute__((aligned(VECTSZ))); float scl; unsigned then = 0, now = 0; int i, res = 0 /* silence compiler warning */; if ( nelms % 4 ) { printf("Need nelms multiple of 4"); return 0; } if (nloops<1) nloops=1; scl = (float)scale/(float)(1<<31); for (i=0; i 0 ) { dim -= IINC; x = vec_ld(dim,px); y = vec_ld(dim,py); /* strip off sign */ sgn = (((vUns)x) ^ ((vUns)y)); x = FABS(x,sgnmsk); y = FABS(y,sgnmsk); /* if x < y swap them */ xly = vec_cmplt(x,y); v = vec_sel(y,x,xly); x = vec_sel(x,y,xly); /* We have the following cases: * * x < y: FALSE, sign(y/x): +1 -> + atan(|y/x|) * x < y: TRUE , sign(x/y): +1 -> Pi/2 - atan(|x/y|) * x < y: FALSE, sign(y/x): -1 -> - atan(|y/x|) * x < y: TRUE , sign(x/y): -1 -> - Pi/2 + atan(|x/y|) * * { |y/x| for |y| <= |x| } * If define v = { } * { |x/y| for |x| < |y| } * * and xly = truth value of |x| < |y| * * sgn = truth value of sign(x/y) == -1 * * then we can summarize in the following table: * * xly sgn result * F F atan(v) == + ( 0 + atan(v) ) * F T - atan(v) == - ( 0 + atan(v) ) * T F Pi/2 - atan(v) == - ( - Pi/2 + atan(v) ) * T T - Pi/2 + atan(v) == + ( - Pi/2 + atan(v) ) * * { 0 if |y| <= |x| } * Hence, if we define P0 = { } * { -Pi/2 if |x| < |y| } * * { -1 if sgn ^ xly == TRUE } * S = { } * { +1 if sgn ^ xly == FALSE } * * then we obtain * * atan(y/x) = S * ( P0 + atan(v) ) */ /* calculate S == sgn ^ xly */ sgn = sgn ^ xly; /* select p0: -Pi/2 if |x|<|y|, 0. otherwise */ p0 = vec_sel(const_0,const_mpiover2,xly); /* Rational approximation */ v = vec_sel(vec_div(v,x,const_1), const_0, vec_cmpeq(x,const_infty)); v2 = v*v; Q = const_1 + v2 * (const_q1 + v2 * (const_q2 + v2 * const_q3)); P = const_1 + v2 * (const_p1 + v2 * const_p2); P = vec_div(P,Q,const_1); /* Add -Pi/2 if necessary */ P = p0 + v*P; /* 'multiply' sign back */ P = (vFloat)((vUns)P ^ (sgn & sgnmsk)); vec_st(P,dim,pr); } } #endif /* ***************************************************************** * NOTE: This is a special-purpose routine designed to accommodate * the data format of the SLAC VME Digitizer. ***************************************************************** * * Convert an array in col-major layout from int16_t -> float; * * Also, reverse the ordering of channels (the labels on the * digitizer front-panel don't match the memory layout). * * Since we load 1 short vector and produce 2 float vectors * 'nsamples' must be an even number. * * short s_vec[nsamples][4]; * float f_vec[nsamples][4]; * * This routine does the following: * * ovfl0 = ovfl1 = ovfl2 = ovfl3 = 0; * for (i=0; i 0 ) { idx -= 2*IINC; sdim -= IINC; s = vec_ld(sdim,sp); /* Reverse order of the channels */ #if !defined(__SSE__) s = (vShort)vec_perm((vFloat)s, (vFloat)s, s32107654); #elif defined(__SSE2__) s = __builtin_ia32_pshuflw(s, 0x1b); s = __builtin_ia32_pshufhw(s, 0x1b); #else #error "Need SSE2" #endif ovfl |= (s & ovfl_msk); tmp = vec_ctf0(vec_unpackh(s)); sum1 = sum1 + tmp; sum2 = sum2 + tmp*tmp; vec_st(tmp, idx, fp); tmp = vec_ctf0(vec_unpackl(s)); sum1 = sum1 + tmp; sum2 = sum2 + tmp*tmp; vec_st(tmp, idx, fp1); } /* OR the upper and lower halves */ res.v = ovfl | vec_swap8(ovfl); vsum2.v = sum2; vsum1.v = sum1*sum1; for (rval=0, i=0; i<4; i++) { if ( res.s[i] ) rval |= (1< 1.E-5 ) { printf("Amplitude mismatch (channel %i): ref %g, got %g\n", j, ampr[j], ampl[j]); } } return now - then; } #endif #define TRIGSTEP(c,s,a,b,t) \ do { \ t = a*c+b*s; \ s-= a*s-b*c; \ c-= t; \ } while (0) /* Compute * * Sum{ conj(Href[n])*H[n]*exp(j n del_T) } * * and return the real/imaginary parts of the * result in *p_re, *p_im. * * NOTE: 'dim' is the number of 4-element vectors in the arrays, * i.e., the number of floats / 4 */ void vec_filt(double *p_re, double *p_im, double del_T, vFloat *pHref_re, vFloat *pHref_im, vFloat *pH_re, vFloat *pH_im, int dim) { register vFloat r1,i1,r2,i2,r,i; register double re,im; register double c,s,a,b,t; register vFloat cv,sv,av,bv,tv; register int j,idx; vFloatU vr,vi; if ( dim <= 0 ) return; /* gcc generates slightly better code if we step down through * an array (probably makes no real difference in run-time though). * Initializing the trigonometric iteration is easier if we * step upwards so we do that in this routine... */ re = im = 0.; /* Fill a vector with exp(j{0,1,2,3}*del_T) */ b = sin(del_T); a = sin(del_T/2.0); a *= 2.0*a; /* NOTE: if we would ever use large arrays then * we would have to occasionally 're-seed' * the trig. recursion in the loop below * to tame round-off errors. * To that end we'd have to execute the * code in braces (initialize c/s to * cos(n*del_T)/sin(n*del_T) instead of * 1.0/0.0, of course). */ { /* NOTE: if c/s are initialized to anything else that 1.0/0.0 * then computing sin(4*del_T/2) and sin(4*del_T) cannot * use the intermediate results from these three steps * but must start over! */ vr.e[0] = c = 1.0; vi.e[0] = s = 0.0; TRIGSTEP(c,s,a,b,t); vr.e[1] = c; vi.e[1] = s; TRIGSTEP(c,s,a,b,t); vr.e[2] = c; vi.e[2] = s; TRIGSTEP(c,s,a,b,t); vr.e[3] = c; vi.e[3] = s; /* load into vector register */ cv = vr.v; sv = vi.v; } /* do one more iteration and we obtain sin(del_T*4) */ TRIGSTEP(c,s,a,b,t); vi.e[0] = s; /* sin(del_T*4/2) is still in vi.e[2] */ vr.e[0] = vi.e[2]*vi.e[2]*2.0; /* splat twiddle factors a,b into registers */ av = vec_splat0(vr.v); bv = vec_splat0(vi.v); j = idx = 0; goto entry; for ( ; j= 0 ) { t = t0; for ( i=0; i= 0 ) rval |= 0x01 << map[k]; } } else { /* testing 'map' is not necessary - orng == 0 */ if ( vec_any_lsbset( orng ) /* && map[j] >= 0 */ ) rval |= 0x01 << map[j]; /* OK even if map[j] < 0 -- should then be all zeroes */ res[j] = vec_hadd4f(s1, s2, s1t, s2t); } p += nvec; } if ( !layout_colmajor ) { VEC_TRANSP4F(s1,s2,s1t,s2t,res[0],res[1],res[2],res[3]); } /* Correct for offsets * _ 2 2 _ _ 2 2 2 * sum( (x - x) ) = sum( x ) - 2 sum(x) x + x sum(1) = sum( x ) - sum(x) / N * * * _ 2 2 _ _ 2 * sum( n (x - x) ) = sum( n x ) - 2 sum( n x) x + x sum(n) * * sum(n=0..N-1) = (N-1)*(N)/2 = (N*N-N)/2 { N even } * sum(n=0..N-1) = (N-1)(N-1)/2 + (N-1)/2 = (N*(N-1))/2 { N odd } * * -> sum(n=0..N-1)/N = (N-1)/2; * */ /* First element of 't' contains nsamples (for both row- and column-major mode). * CAVEAT: t is incorrect if all map[k] == -1 ! */ N = vec_splat0(t); m1 = vec_div(s1,N,const_1); s2 -= m1*s1; s2t = (s2t - (s1t * m1 + s1t * m1)) + m1*s1*(N-const_1)*const_p5 /*.5*/; /* TODO: subtract noise pedestal */ /* normalize and store result */ s2t_u.vf = vec_div(s2t,s2,const_1); s2_u.vf = s2; for ( i=0; i<4; i++ ) { if ( map[i] < 0 ) continue; pacc[map[i]] = s2_u.af[i]; ptim[map[i]] = s2t_u.af[i]; } return rval; } #ifdef DO_TESTS union conv { vFloat vf; vShort vs; float af[4]; short as[8]; }; static void pvec(union conv *p, unsigned prec) { int i; printf("["); for ( i=0; i<4; i++ ) { printf("%.*x%s", prec, (unsigned)p->af[i], i<3 ? ", ":"]"); } } static void pmat(union conv *p, unsigned prec) { int i; for ( i=0; i<4; i++ ) { pvec(p+i, prec); printf("\n"); } } int vechoriztst(void) { union conv a, b, c, d, dst[4], src[4]; int i,j,err; int rval = 0; for (i=0; i<4; i++) { a.af[i] = (float)(0xa0+i); b.af[i] = (float)(0xb0+i); for ( j=0; j<4; j++ ) { src[i].af[j] = (float)(0xa0 + (i<<4) + j); } } c.vf = vec_mergeh(a.vf, b.vf); printf("**** Testing vec_mergeh **********************\n"); printf("vec_mergeh([a0, a1, a2, a3], [b0, b1, b2, b3]): "); pvec(&c,2); printf("\n"); err = 1; for ( i=0; i<4/2; i++ ) { if ( (unsigned)c.af[2*i ] != (unsigned)a.af[i] || (unsigned)c.af[2*i+1] != (unsigned)b.af[i] ) { fprintf(stderr,"vec_mergeh test FAILURE @[%u]\n",i); rval += err; err = 0; } } c.vf = vec_mergel(a.vf, b.vf); printf("**** Testing vec_mergel **********************\n"); printf("vec_mergel([a0, a1, a2, a3], [b0, b1, b2, b3]): "); pvec(&c,2); printf("\n"); err = 1; for ( i=0; i<4/2; i++ ) { if ( (unsigned)c.af[2*i ] != (unsigned)a.af[i+4/2] || (unsigned)c.af[2*i+1] != (unsigned)b.af[i+4/2] ) { fprintf(stderr,"vec_mergel test FAILURE @[%u]\n",i); rval += err; err = 0; } } printf("**** Testing vec_transp4f ********************\n"); printf("vec_transp4f:\n"); vec_transp4f(&dst[0].vf,&src[0].vf); printf("Src:\n"); pmat(src,2); printf("Dst:\n"); pmat(dst,2); err = 1; for ( i=0; i<4; i++ ) { for (j=0; j<4; j++) { if ( dst[i].af[j] != src[j].af[i] ) { fprintf(stderr,"vec_transp4f test FAILURE @[%i,%i]\n",i,j); rval += err; err = 0; } } } for ( i=0; i<4; i++ ) { for (j=0; j<4; j++) { src[i].af[j] = (float)((1<<(4*i)) + j); } } printf("**** Testing vec_hadd4f **********************\n"); printf("vec_hadd4f:\n"); c.vf = vec_hadd4f(src[0].vf, src[1].vf, src[2].vf, src[3].vf); printf("Src:\n"); pmat(src,4); printf("Horizontal Sum:\n"); pvec(&c,4); printf("\n"); err = 1; for ( i=0; i<4; i++ ) { unsigned s = 0; for (j=0; j<4; j++) { s += (unsigned)src[i].af[j]; } if ( s != (unsigned)c.af[i] ) { fprintf(stderr,"vec_hadd4f FAILURE @[%u]\n",i); rval += err; err = 0; } } printf("**** Testing vec_any_eqh *********************\n"); err = 1; for ( i=0; i>> Test FAILURE (%u tests failed)\n", rval); else fprintf(stderr,">>> Test PASSED\n"); return rval; } static void energy_gen_wav(short *buf, int inc, int nsamples, short a, short off, short toff, int ovfl) { int i,j; float t; short v; for ( j=i=0; i sum(n=0..N-1)/N = (N-1)/2; * */ N = (float)nsamples; m1 = s1/N; s2 -= m1*s1; s2t = (s2t - 2 * s1t * m1) + m1*s1*(N-1.)/2.; /* TODO: subtract noise pedestal */ /* normalize */ s2t /= s2; /* Signal under-range if we have less than 64 * noise * contribution. If we have noise of ~2.5 bits then the * integrated energy is 2^5 = 32 * N */ if ( s2 < (nsamples<<11) ) { clipped |= (1 << j); } s2 = sqrtf(s2); pacc[j] = s2; ptim[j] = s2t; } return clipped; } static int eng_cmp(float *eres, float *tres) { int i; /* Expected values (calculated with scilab, script tste.sce) */ float eres_cmp[4] = { 17798.524, 35594.486, 0., 53159.635 }; float tres_cmp[4] = { 30.320759, 50.235121, -1./0., 89.680674 }; for ( i=0; i<4; i++ ) { if ( 0. == eres_cmp[i] ) { if ( fabs(eres[i]) > 1.E-4 ) { fprintf(stderr,"TEST FAILED: (Zero amplitude mismatch @%i)\n", i); return 1; } if ( fabs(1. - eres[i]/eres_cmp[i]) > 1.E-4 ) { fprintf(stderr,"TEST FAILED: (Amplitude mismatch @%i)\n", i); fprintf(stderr,"Expected %10.5e Got %10.5e\n",eres_cmp[i], eres[i]); return 1; } if ( fabs(1. - tres[i]/tres_cmp[i]) > 1.E-4 ) { fprintf(stderr,"TEST FAILED: (Arrival time mismatch @%i)\n", i); fprintf(stderr,"Expected %10.5e Got %10.5e\n",eres_cmp[i], eres[i]); return 1; } } } return 0; } int tst_energy(void) { vpVect *buf; void *mem; int stat; int rval = 0; VpTimeStamp now,then; int def_map[4]={0,1,2,3}; float eres[4], tres[4]; mem = malloc( 128*4*2+VECTSZ ); buf = (vpVect*)((((uintptr_t)mem)+(VECTSZ-1)) & ~(VECTSZ-1)); printf("Testing energy: ROW-MAJOR (scalar)\n"); energy_gen_wav(buf[0*128/8].as,1,128, 8000,4000, 20,-1); energy_gen_wav(buf[1*128/8].as,1,128,16000,-1600, 40, 10); energy_gen_wav(buf[2*128/8].as,1,128, 0,-1000, 0,-1); energy_gen_wav(buf[3*128/8].as,1,128,24000,800, 80,-1); rdtimer( &then ); stat = detect_energy_s(eres, tres, buf[0].as, 128, 0); rdtimer( &now ); prtsdiff(&now, &then, stdout); if ( stat != 0x24 ) { fprintf(stderr,"TEST FAILED: Over/Underflow MISMATCH (got 0x%02x -- expected 0x24)\n", stat); rval ++; } printf("Amplitude: %10.5e %10.5e %10.5e %10.5e\n", eres[0], eres[1], eres[2], eres[3]); printf("TOA : %10.5e %10.5e %10.5e %10.5e\n", tres[0], tres[1], tres[2], tres[3]); if ( eng_cmp(eres,tres) ) { rval++; } printf("Testing energy: ROW-MAJOR (vectorized)\n"); rdtimer( &then ); stat = vec_detect_energy_4(eres, tres, &buf[0].vs, def_map, 128, 0); rdtimer( &now ); prtsdiff(&now, &then, stdout); if ( stat != 0x24 ) { fprintf(stderr,"TEST FAILED: Over/Underflow MISMATCH (got 0x%02x -- expected 0x24)\n", stat); rval ++; } printf("Amplitude: %10.5e %10.5e %10.5e %10.5e\n", eres[0], eres[1], eres[2], eres[3]); printf("TOA : %10.5e %10.5e %10.5e %10.5e\n", tres[0], tres[1], tres[2], tres[3]); if ( eng_cmp(eres,tres) ) { rval++; } printf("Testing energy: COL-MAJOR (scalar)\n"); energy_gen_wav(&buf[0*128/8].as[0], 4, 128, 8000, 4000, 20, -1); energy_gen_wav(&buf[0*128/8].as[1], 4, 128, 16000, -1600, 40, 10); energy_gen_wav(&buf[0*128/8].as[2], 4, 128, 0, -1000, 0, -1); energy_gen_wav(&buf[0*128/8].as[3], 4, 128, 24000, 800, 80, -1); rdtimer( &then ); stat = detect_energy_s(eres, tres, buf[0].as, 128, 1); rdtimer( &now ); prtsdiff(&now, &then, stdout); if ( stat != 0x24 ) { fprintf(stderr,"TEST FAILED: Over/Underflow MISMATCH (got 0x%02x -- expected 0x24)\n", stat); rval ++; } printf("Amplitude: %10.5e %10.5e %10.5e %10.5e\n", eres[0], eres[1], eres[2], eres[3]); printf("TOA : %10.5e %10.5e %10.5e %10.5e\n", tres[0], tres[1], tres[2], tres[3]); if ( eng_cmp(eres,tres) ) { rval++; } printf("Testing energy: COL-MAJOR (vectorized)\n"); rdtimer( &then ); stat = vec_detect_energy_4(eres, tres, &buf[0].vs, def_map, 128, 1); rdtimer( &now ); prtsdiff(&now, &then, stdout); if ( stat != 0x24 ) { fprintf(stderr,"TEST FAILED: Over/Underflow MISMATCH (got 0x%02x -- expected 0x24)\n", stat); rval ++; } printf("Amplitude: %10.5e %10.5e %10.5e %10.5e\n", eres[0], eres[1], eres[2], eres[3]); printf("TOA : %10.5e %10.5e %10.5e %10.5e\n", tres[0], tres[1], tres[2], tres[3]); if ( eng_cmp(eres,tres) ) { rval++; } free(mem); return rval; } #endif #ifdef DO_MAIN #include #include #include static void usage(const char *nm) { fprintf(stderr,"usage: %s [-s] [-f lag] [-c] [-H] [-E] [-R nrand] [-T amp] [-t scale] [-n loops] \n",nm); } int main(int argc, char *argv[]) { volatile vFloat vx[2], vy[2], vr[2]; float x; float y; float_a *pf = (float_a*)&vr[0]; double dparm; int nloops = 1; int ch,i; clock_t then, now; unsigned uparm = 1; while ( (ch = getopt(argc, argv, "f:ct:n:rsT:R:lHE")) >= 0 ) { switch (ch) { case 'n': if ( 1 != sscanf(optarg,"%i",&nloops) ) { usage(argv[0]); return 1; } break; case 'c': tst_cvtf(1,1,4); return 0; case 'H': return vechoriztst(); case 'E': i = tst_energy(); printf("Test %s\n", i?"FAILED":"PASSED"); return ; case 'f': #ifdef FILT_DEBUG if ( 1 != sscanf(optarg,"%lg",&dparm) ) { usage(argv[0]); return 1; } filttst(1,1,4,dparm); #else printf("Must compile with FILT_DEBUG defined\n"); return 1; #endif return 0; case 'R': srand48(33); if ( 1 != sscanf(optarg,"%u",&uparm) ) { usage(argv[0]); return 1; } for (i=0; i 1 ) { int i; then = clock(); for ( i=0; i