#ifndef BPM_VECTOR_OPERATIONS_H #define BPM_VECTOR_OPERATIONS_H #include #include #define VECTSZ 16 #define VECTALIGN 16 #ifdef __cplusplus extern "C" { #endif typedef float __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vFloat; /* altivec doesn't like uint32_t */ typedef unsigned __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vUns; typedef int __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vInt; typedef int16_t __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vShort; typedef uint8_t __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vByte; typedef char __attribute__((vector_size(VECTSZ), aligned(VECTSZ))) vChar; typedef union vpVect { vFloat vf; float af[4]; vUns vu; unsigned au[4]; vInt vi; int ai[4]; vShort vs; int16_t as[4]; vByte vb; uint8_t ab[4]; vChar vc; char ac[4]; } vpVect; typedef float __attribute__((may_alias)) float_a; typedef uint32_t __attribute__((may_alias)) uns32_a; typedef int32_t __attribute__((may_alias)) int32_a; typedef int16_t __attribute__((may_alias)) int16_a; #define VFCONST(x) ((vFloat){x,x,x,x}) /* Vectorized 'atanf'. Computes the atan * * pr[i] = atan(py[i]/px[i]) * * (between -Pi/2 .. +Pi/2 only; this NOT atan2) * of 'dim' vectors, i.e., dim*4 floats. * * NOTES: - 'dim' gives number of vectors, not floats * - all arrays must be properlly aligned * - py[i] and px[i] must not both be zero nor both * be 'infinity' nor must either be 'nan' or * the result will be 'nan'. * - the 'result' array 'pr' may be one of the * 'py' or 'px' arrays. * * x>0, 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|) */ void vec_atanf(vFloat *pr, vFloat *py, vFloat *px, int dim); /* Compute cumulative phase response of an array of complex * values. * pr : pointer to result array of vector floats * pre: pointer to input array of vector floats (real part) * pim: pointer to output array of vector floats (imag. part) * dim: number of vectors, i.e., arrays are 4*'dim' floats. */ void vec_phase(vFloat *pr, vFloat *pim, vFloat *pre, int dim); /* Compute time lag between two signals. * * INPUT: * pHref_re, pHref_im: fourier transform of reference signal. * pH_re, pH_im : fourier transform of second signal. * dim : number of vectors, i.e., arrays are 4*'dim' floats. * * OUTPUT: * *pdel_phi : phase difference of signals. * *pdel_T : time lag. * *pRMS : filtered signal RMS sqrt(sum( (|Href| |H|)^2 ) / nsamples) * * NOTE: Input arrays are only HALF of the spectrum (positive frequencies). * E.g., if a real signal of 256 samples undergoes a complex FFT then * 256 complex values would be the result. Assuming they are stored * in the order {fo, f1, f2, ... f127, f128, f-127, ... f-2, f-1 } * then only the first half of this array would be supplied to vec_tadj() * which is easy to do just by cutting the dimension in half: * dim = 256/2/4 = 32 vectors in this example. * A FFT package (such as fftw) which computes the 'half-complex' * transform of a real signal immediately provides the correct * 'half-complex' arrays (in split-storage mode). * * RETURNS: number of rescaling operations performed (for * debugging purposes). * * NOTE: result is undefined if |Href|*|H| === 0 everywhere. */ 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); /* ***************************************************************** * 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 * in col-major layout. * * 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