51 #ifdef _USE_GENERIC_TRIGONOMETRIC_ 59 #define SIN_LUT_SIZEBITS 12 // maximum error for sin/cos for SIN_LUT_SIZEBITS=12: 0.000383 60 #define SIN_LUT_SIZE (1<<SIN_LUT_SIZEBITS) 61 #define SIN_LUT_BITS 28 63 #define DOUBLE_PI 3.1415926535897932384626433832795 64 #define DOUBLE_PI_OVER_2 (DOUBLE_PI/2.0) 65 #define DOUBLE_3_PI_OVER_2 (3.0*DOUBLE_PI/2.0) 66 #define DOUBLE_2_PI (2.0*DOUBLE_PI) 67 #define DOUBLE_2_OVER_PI (2.0/DOUBLE_PI) 69 #define FIXED28_PI FIXED_Float_To_Fixed_n(DOUBLE_PI, 28) 70 #define FIXED28_PI_OVER_2 FIXED_Float_To_Fixed_n(DOUBLE_PI_OVER_2, 28) 71 #define FIXED28_3_PI_OVER_2 FIXED_Float_To_Fixed_n(DOUBLE_3_PI_OVER_2, 28) 72 #define FIXED28_2_PI FIXED_Float_To_Fixed_n(DOUBLE_2_PI, 28) 73 #define FIXED28_2_OVER_PI FIXED_Float_To_Fixed_n(DOUBLE_2_OVER_PI, 28) 76 static I32 *sinLUT_28 = 0;
80 createSinLUT(
int nSize,
int nBits)
82 I32* lut =
new I32[nSize];
84 for(
int i=0; i<nSize; i++)
86 double phi = i*DOUBLE_PI_OVER_2/nSize;
87 double sinPhi = sin(phi);
98 Fixed28_SinCos(I32 phi, I32 &sin, I32 &cos)
101 bool negative =
false;
111 if(phi<FIXED28_PI_OVER_2)
113 else if(phi<FIXED28_PI)
118 else if(phi<FIXED28_3_PI_OVER_2)
126 phi=FIXED28_2_PI-phi;
130 FIXED_MUL2(phi, FIXED28_2_OVER_PI, i, SIN_LUT_BITS);
131 i>>=(SIN_LUT_BITS-SIN_LUT_SIZEBITS);
133 assert(i<SIN_LUT_SIZE);
136 cos = sinLUT_28[SIN_LUT_SIZE-1-i];
160 checkPrecisionSinCos()
162 double maxErrorSin=0.0, maxErrorCos=0.0;
164 for(
double phi=-5.0; phi<5.0; phi+=0.0001)
167 I32 fixedSin,fixedCos;
169 Fixed28_SinCos(fixedPhi, fixedSin,fixedCos);
171 double doubleSin=sin(phi),doubleCos=cos(phi);
175 double diffSin = fabs(doubleSin-doubleSinF),
176 diffCos = fabs(doubleCos-doubleCosF);
178 if(diffSin>maxErrorSin)
181 if(diffCos>maxErrorCos)
185 printf(
"Maximum error for sin() and cos(): %f %f\n", (
float)maxErrorSin, (
float)maxErrorCos);
193 sinLUT_28 = createSinLUT(SIN_LUT_SIZE, SIN_LUT_BITS);
205 #endif //_USE_GENERIC_TRIGONOMETRIC_
#define FIXED_Fixed_n_To_Float(x, n)
#define FIXED_Float_To_Fixed_n(x, n)