Dong-Woon Park wrote:
Why polynomials are faster ? I don't know at all about the wayFor many situations, polynomials are faster. I was able to speed up code on other platforms, for example an intel paragon (I860 based) by a factor of >10 like this. Code fragments below. The I860 has some features in common with the alpha, for example add and multiply are cheap, but divide is expensive. It is so easy to try that for your situtation I would not even discuss it further, just try it and see. As I said, the key is if you don't need to do range reduction. In this case you can implement the formulas in Abramowitz and Stegun with about 7 multiplies and adds for single precision accuracy. This should be much faster than the library numbers that I have seen quoted.
co-processor or libm works. Doesn't libm use these schemes? or is
there any chance to lose some accuracy using polynomials instead of
sin() libm provides ?Bruce Allen <ballen@dirac.phys.uwm.edu> writes:
> > With a good co-processor of alpha, how much does it take to calcuate
> > sin() in comparison with simple + or * operations ? Should I suppress
> > the use of sin() to reduce computation as much as possible ? Should I
> > use Taylor expanded polynomials ?
>
> NO! Use min/max (Chebyshev) polynomials to evaulate sin(x). See Numerical
> Recipes in C, section 5.8 or to save time use Abramowitz and Stegun
> 4.3.96-103 for all standard trig functions. If you don't need to do range
> reduction of x to [0,pi/2] this will be significantly faster than most
> libraries.
>
> Bruce Allen
A number of readers have suggested a lookup table followed by iterations of Newton-Raphson to clean up the accuracy. This works very well for functions like x^(1/2) or x^(1/3) [square root and cube root] where the derivative of the function can be re-expressed in terms of the quantity being searched for. However Newton-Raphson can not be used to "clean up" the solution here with small numbers of cycles because the derivative of sin(x) is cos(x) which can not be cheaply computed -- at least, not easily.
Here are a couple of fragments from my own code [optimized for I860 but should work well on Alpha]. The sin(x) and cos(x) routines are NOT based on the Abramowitz and Stegun approximations -- I recommend that if you use this idea you should get the approximations from there and modify the code accordingly. The cuberoot function is based on a lookup table to scale the argument into the range 1->2, then two passes of Newton-Raphson. A good reference for Newton-Raphson is section 9.4 of Numerical Recipes in C. I DO NOT GUARANTEE ACCURACY however these were more than good enough for what I was doing. Compare the output of your program with these versus library routines before reaching the same conclusion yourself.
Good luck!
Bruce Allen
#ifdef INLINE_TRIGS
/* Chebyshev polynomials for expansion of sin/cos on -pi < x <
pi */
static const float cs0=1.000000e+00, cs2=-4.999999e-01, cs4=4.166650e-02,
cs6=-1.388787e-03, cs8=2.477176e-05, cs10=-2.709995e-07,
cs12=1.732713e-09;
static const float cs1=1.000000e+00, cs3=-1.666669e-01, cs5=8.333445e-03,
cs7=-1.984473e-04, cs9=2.760671e-06, cs11=-2.530742e-08,
cs13=1.544217e-10;
#endif
#ifdef INLINE_TRIG
phs2=phs*phs;
si=phs*(cs1+phs2*(cs3+phs2*(cs5+phs2*(cs7+phs2*(cs9+phs2*(cs11+phs2*cs13))))));
co=cs0+phs2*(cs2+phs2*(cs4+phs2*(cs6+phs2*(cs8+phs2*(cs10+phs2*cs12)))));
#else
si=sin(2.0*phs);
co=cos(2.0*phs);
#endif
#ifdef INLINE_CUBEROOT
static unsigned int psave=123456789;
unsigned int p;
static float scale1,scale2;
extern const float gcheb0,gcheb1,gcheb2,gcheb3,gcheb4,gcheb5;
extern const float scaling_cuberoot[];
float x,z;
#endif
#ifdef INLINE_CUBEROOT
/* HARDWIRED INLINE CUBE ROOT (see cuberoot()) */
/* y=cuberoot(ptrfrequency[i]); */
x=ptrfrequency[i];
p=(*(int *)&x) >> 23;
if (p!=psave) {
psave=p;
scale1=scaling_cuberoot[2*p];
scale2=scaling_cuberoot[2*p+1];
}
x*=scale1;
z=gcheb0+x*(gcheb1+x*(gcheb2+x*(gcheb3+x*(gcheb4+x*gcheb5))));
y=x*z;
y*=(4.0f-z*y*y)*scale2;
/* END OF HARDWIRED INLINE CUBE ROOT (see cuberoot()) */
#else
y=pow(ptrfrequency[i],1.0/3.0);
#endif
#ifdef INLINE_CUBEROOT
float cuberoot(float x) {
static unsigned int psave=4096;
static float scale1,scale2;
unsigned int p;
float y,z;
extern const float gcheb0,gcheb1,gcheb2,gcheb3,gcheb4,gcheb5;
extern const float scaling_cuberoot[];
/* the exponent of the float rep of x */
p=(*(int *)&x) >> 23;
/* if exponent hasn't changed, neither have scale factors */
if (p!=psave) {
psave=p;
scale1=scaling_cuberoot[2*p];
scale2=scaling_cuberoot[2*p+1];
}
/* rescale x to the range from 1.0->2.0 inclusive */
x*=scale1;
/* fifth-order fit to z=x^(-2/3) with uniform-error Chebyshevs
*/
z=gcheb0+x*(gcheb1+x*(gcheb2+x*(gcheb3+x*(gcheb4+x*gcheb5))));
/* y ~ x^(-2/3) x ~ x^(1/3) */
y=x*z;
/* Now do one pass of Newton-Raphson on f(z)=z^-3-x^2
so dz=-f/f' = -(z^-3 - x^2)/(-3 z^-4)
so z'=
z+dz = z(4 - z^3 x^2)/3 = z(4- y^2 z)/3
This pass
doubles # of decimal places of precision.
*/
y*=(4.0f-z*y*y)*scale2;
return y;
}
/* Coefficients of Chebyshev polynomial approx for x^(-2/3) on interval
1<x<2. Error about part in 10^4.
*/
const float
gcheb0=2.8567583422f,
gcheb1=-4.0127830607f,
gcheb2=3.4804896648f,
gcheb3=-1.7355624528f,
gcheb4=0.4620750592f,
gcheb5=-0.05099705856f;
/* Table to reduce domain of input for Chebychev polynomial approx.
First column is (1/2)^k. Second column 1/3 2^(-k/3)
*/
const float scaling_cuberoot[2*256] = {
1.701412e+38, 6.015553e-14,
8.507059e+37, 7.579123e-14,
4.253530e+37, 9.549096e-14,
2.126765e+37, 1.203111e-13,
1.063382e+37, 1.515825e-13,
5.316912e+36, 1.909819e-13,
2.658456e+36, 2.406221e-13,
1.329228e+36, 3.031649e-13,
6.646140e+35, 3.819638e-13,
3.323070e+35, 4.812443e-13,
1.661535e+35, 6.063298e-13,
8.307675e+34, 7.639277e-13,
4.153837e+34, 9.624886e-13,
2.076919e+34, 1.212660e-12,
1.038459e+34, 1.527855e-12,
5.192297e+33, 1.924977e-12,
2.596148e+33, 2.425319e-12,
1.298074e+33, 3.055711e-12,
6.490371e+32, 3.849954e-12,
3.245186e+32, 4.850639e-12,
1.622593e+32, 6.111421e-12,
8.112964e+31, 7.699908e-12,
4.056482e+31, 9.701277e-12,
2.028241e+31, 1.222284e-11,
1.014120e+31, 1.539982e-11,
5.070602e+30, 1.940255e-11,
2.535301e+30, 2.444569e-11,
1.267651e+30, 3.079963e-11,
6.338253e+29, 3.880511e-11,
3.169127e+29, 4.889137e-11,
1.584563e+29, 6.159927e-11,
7.922816e+28, 7.761022e-11,
3.961408e+28, 9.778274e-11,
1.980704e+28, 1.231985e-10,
9.903520e+27, 1.552204e-10,
4.951760e+27, 1.955655e-10,
2.475880e+27, 2.463971e-10,
1.237940e+27, 3.104409e-10,
6.189700e+26, 3.911310e-10,
3.094850e+26, 4.927941e-10,
1.547425e+26, 6.208817e-10,
7.737125e+25, 7.822619e-10,
3.868563e+25, 9.855883e-10,
1.934281e+25, 1.241763e-09,
9.671407e+24, 1.564524e-09,
4.835703e+24, 1.971177e-09,
2.417852e+24, 2.483527e-09,
1.208926e+24, 3.129048e-09,
6.044629e+23, 3.942353e-09,
3.022315e+23, 4.967054e-09,
1.511157e+23, 6.258095e-09,
7.555786e+22, 7.884706e-09,
3.777893e+22, 9.934108e-09,
1.888947e+22, 1.251619e-08,
9.444733e+21, 1.576941e-08,
4.722366e+21, 1.986822e-08,
2.361183e+21, 2.503238e-08,
1.180592e+21, 3.153882e-08,
5.902958e+20, 3.973643e-08,
2.951479e+20, 5.006476e-08,
1.475740e+20, 6.307765e-08,
7.378698e+19, 7.947286e-08,
3.689349e+19, 1.001295e-07,
1.844674e+19, 1.261553e-07,
9.223372e+18, 1.589457e-07,
4.611686e+18, 2.002591e-07,
2.305843e+18, 2.523106e-07,
1.152922e+18, 3.178914e-07,
5.764608e+17, 4.005181e-07,
2.882304e+17, 5.046212e-07,
1.441152e+17, 6.357829e-07,
7.205759e+16, 8.010362e-07,
3.602880e+16, 1.009242e-06,
1.801440e+16, 1.271566e-06,
9.007199e+15, 1.602072e-06,
4.503600e+15, 2.018485e-06,
2.251800e+15, 2.543132e-06,
1.125900e+15, 3.204145e-06,
5.629500e+14, 4.036970e-06,
2.814750e+14, 5.086263e-06,
1.407375e+14, 6.408290e-06,
7.036874e+13, 8.073939e-06,
3.518437e+13, 1.017253e-05,
1.759219e+13, 1.281658e-05,
8.796093e+12, 1.614788e-05,
4.398047e+12, 2.034505e-05,
2.199023e+12, 2.563316e-05,
1.099512e+12, 3.229576e-05,
5.497558e+11, 4.069011e-05,
2.748779e+11, 5.126632e-05,
1.374390e+11, 6.459151e-05,
6.871948e+10, 8.138021e-05,
3.435974e+10, 1.025326e-04,
1.717987e+10, 1.291830e-04,
8.589935e+09, 1.627604e-04,
4.294967e+09, 2.050653e-04,
2.147484e+09, 2.583661e-04,
1.073742e+09, 3.255208e-04,
5.368709e+08, 4.101305e-04,
2.684355e+08, 5.167321e-04,
1.342177e+08, 6.510417e-04,
6.710886e+07, 8.202611e-04,
3.355443e+07, 1.033464e-03,
1.677722e+07, 1.302083e-03,
8.388608e+06, 1.640522e-03,
4.194304e+06, 2.066928e-03,
2.097152e+06, 2.604167e-03,
1.048576e+06, 3.281044e-03,
5.242880e+05, 4.133857e-03,
2.621440e+05, 5.208333e-03,
1.310720e+05, 6.562089e-03,
6.553600e+04, 8.267714e-03,
3.276800e+04, 1.041667e-02,
1.638400e+04, 1.312418e-02,
8.192000e+03, 1.653543e-02,
4.096000e+03, 2.083333e-02,
2.048000e+03, 2.624835e-02,
1.024000e+03, 3.307085e-02,
5.120000e+02, 4.166667e-02,
2.560000e+02, 5.249671e-02,
1.280000e+02, 6.614171e-02,
6.400000e+01, 8.333334e-02,
3.200000e+01, 1.049934e-01,
1.600000e+01, 1.322834e-01,
8.000000e+00, 1.666667e-01,
4.000000e+00, 2.099868e-01,
2.000000e+00, 2.645668e-01,
1.000000e+00, 3.333333e-01,
5.000000e-01, 4.199737e-01,
2.500000e-01, 5.291337e-01,
1.250000e-01, 6.666667e-01,
6.250000e-02, 8.399473e-01,
3.125000e-02, 1.058267e+00,
1.562500e-02, 1.333333e+00,
7.812500e-03, 1.679895e+00,
3.906250e-03, 2.116535e+00,
1.953125e-03, 2.666667e+00,
9.765625e-04, 3.359789e+00,
4.882812e-04, 4.233069e+00,
2.441406e-04, 5.333333e+00,
1.220703e-04, 6.719579e+00,
6.103516e-05, 8.466139e+00,
3.051758e-05, 1.066667e+01,
1.525879e-05, 1.343916e+01,
7.629395e-06, 1.693228e+01,
3.814697e-06, 2.133333e+01,
1.907349e-06, 2.687831e+01,
9.536743e-07, 3.386456e+01,
4.768372e-07, 4.266667e+01,
2.384186e-07, 5.375663e+01,
1.192093e-07, 6.772911e+01,
5.960464e-08, 8.533334e+01,
2.980232e-08, 1.075133e+02,
1.490116e-08, 1.354582e+02,
7.450581e-09, 1.706667e+02,
3.725290e-09, 2.150265e+02,
1.862645e-09, 2.709164e+02,
9.313226e-10, 3.413333e+02,
4.656613e-10, 4.300530e+02,
2.328306e-10, 5.418329e+02,
1.164153e-10, 6.826667e+02,
5.820766e-11, 8.601061e+02,
2.910383e-11, 1.083666e+03,
1.455192e-11, 1.365333e+03,
7.275958e-12, 1.720212e+03,
3.637979e-12, 2.167332e+03,
1.818989e-12, 2.730667e+03,
9.094947e-13, 3.440424e+03,
4.547474e-13, 4.334663e+03,
2.273737e-13, 5.461333e+03,
1.136868e-13, 6.880849e+03,
5.684342e-14, 8.669326e+03,
2.842171e-14, 1.092267e+04,
1.421085e-14, 1.376170e+04,
7.105427e-15, 1.733865e+04,
3.552714e-15, 2.184533e+04,
1.776357e-15, 2.752339e+04,
8.881784e-16, 3.467730e+04,
4.440892e-16, 4.369067e+04,
2.220446e-16, 5.504679e+04,
1.110223e-16, 6.935461e+04,
5.551115e-17, 8.738134e+04,
2.775558e-17, 1.100936e+05,
1.387779e-17, 1.387092e+05,
6.938894e-18, 1.747627e+05,
3.469447e-18, 2.201872e+05,
1.734723e-18, 2.774184e+05,
8.673617e-19, 3.495253e+05,
4.336809e-19, 4.403743e+05,
2.168404e-19, 5.548369e+05,
1.084202e-19, 6.990507e+05,
5.421011e-20, 8.807486e+05,
2.710505e-20, 1.109674e+06,
1.355253e-20, 1.398101e+06,
6.776264e-21, 1.761497e+06,
3.388132e-21, 2.219348e+06,
1.694066e-21, 2.796203e+06,
8.470329e-22, 3.522994e+06,
4.235165e-22, 4.438695e+06,
2.117582e-22, 5.592406e+06,
1.058791e-22, 7.045989e+06,
5.293956e-23, 8.877390e+06,
2.646978e-23, 1.118481e+07,
1.323489e-23, 1.409198e+07,
6.617445e-24, 1.775478e+07,
3.308722e-24, 2.236962e+07,
1.654361e-24, 2.818396e+07,
8.271806e-25, 3.550956e+07,
4.135903e-25, 4.473924e+07,
2.067952e-25, 5.636791e+07,
1.033976e-25, 7.101912e+07,
5.169879e-26, 8.947849e+07,
2.584939e-26, 1.127358e+08,
1.292470e-26, 1.420382e+08,
6.462349e-27, 1.789570e+08,
3.231174e-27, 2.254716e+08,
1.615587e-27, 2.840765e+08,
8.077936e-28, 3.579140e+08,
4.038968e-28, 4.509433e+08,
2.019484e-28, 5.681530e+08,
1.009742e-28, 7.158279e+08,
5.048710e-29, 9.018866e+08,
2.524355e-29, 1.136306e+09,
1.262177e-29, 1.431656e+09,
6.310887e-30, 1.803773e+09,
3.155444e-30, 2.272612e+09,
1.577722e-30, 2.863312e+09,
7.888609e-31, 3.607546e+09,
3.944305e-31, 4.545224e+09,
1.972152e-31, 5.726623e+09,
9.860761e-32, 7.215093e+09,
4.930381e-32, 9.090447e+09,
2.465190e-32, 1.145325e+10,
1.232595e-32, 1.443019e+10,
6.162976e-33, 1.818089e+10,
3.081488e-33, 2.290649e+10,
1.540744e-33, 2.886037e+10,
7.703720e-34, 3.636179e+10,
3.851860e-34, 4.581299e+10,
1.925930e-34, 5.772074e+10,
9.629650e-35, 7.272358e+10,
4.814825e-35, 9.162597e+10,
2.407412e-35, 1.154415e+11,
1.203706e-35, 1.454472e+11,
6.018531e-36, 1.832519e+11,
3.009266e-36, 2.308830e+11,
1.504633e-36, 2.908943e+11,
7.523164e-37, 3.665039e+11,
3.761582e-37, 4.617659e+11,
1.880791e-37, 5.817886e+11,
9.403955e-38, 7.330078e+11,
4.701977e-38, 9.235319e+11,
2.350989e-38, 1.163577e+12,
1.175494e-38, 1.466016e+12,
5.877472e-39, 1.847064e+12,
2.938736e-39, 2.327155e+12};
#endif