* Re: sin() on alpha.
1998-05-22 4:00 ` Bruce Allen
@ 1998-05-22 4:00 ` Greg Lindahl
1998-05-23 4:00 ` Dong-Woon Park
1998-05-23 4:00 ` Christopher Browne
2 siblings, 0 replies; 13+ messages in thread
From: Greg Lindahl @ 1998-05-22 4:00 UTC (permalink / raw)
Bruce Allen <ballen@dirac.phys.uwm.edu> writes:
> If you don't need to do range
> reduction of x to [0,pi/2] this will be significantly faster than most
> libraries.
Most libraries don't take that much time to figure out that they don't
need to reduce the arguments. Test it and see, or look at the posting
on another thread which gave some sample times.
-- g
^ permalink raw reply [flat|nested] 13+ messages in thread
* sin() on alpha.
@ 1998-05-22 4:00 Dong-Woon Park
1998-05-22 4:00 ` Bruce Allen
0 siblings, 1 reply; 13+ messages in thread
From: Dong-Woon Park @ 1998-05-22 4:00 UTC (permalink / raw)
[-- Attachment #1: Type: text/plain, Size: 308 bytes --]
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 ?
--
PGP public key: http://ghost1.snu.ac.kr/~dwpark/dwpark.asc
[-- Attachment #2: Type: application/pgp-signature, Size: 346 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-22 4:00 sin() on alpha Dong-Woon Park
@ 1998-05-22 4:00 ` Bruce Allen
1998-05-22 4:00 ` Greg Lindahl
` (2 more replies)
0 siblings, 3 replies; 13+ messages in thread
From: Bruce Allen @ 1998-05-22 4:00 UTC (permalink / raw)
> 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
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-22 4:00 ` Bruce Allen
1998-05-22 4:00 ` Greg Lindahl
1998-05-23 4:00 ` Dong-Woon Park
@ 1998-05-23 4:00 ` Christopher Browne
1998-05-23 4:00 ` Linus Torvalds
1998-05-23 4:00 ` Michal Jaegermann
2 siblings, 2 replies; 13+ messages in thread
From: Christopher Browne @ 1998-05-23 4:00 UTC (permalink / raw)
On Fri, 22 May 1998 02:29:35 -0500, Bruce Allen
<ballen@dirac.phys.uwm.edu> wrote:
>> 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.
'Tis been a while since I've calculated trig values; are there any
processes where you start with an estimate, and then iterate to refine
it?
In that case, you would start with an interpolation table (probably
linear) involving some "small-but-useful" set of values to get the
initial estimate, and then iterate to improve precision.
A 1024 entry linear interpolation table is going to get one *fairly*
close; I don't recall offhand what identities would be used to do
"function point iteration," but no doubt there's something useful. A
higher-order polynomial interpolation table would surely give somewhat
better results, at somewhat higher cost, to be sure.
If the best such methods provide slow convergence, feel free to tell me
I'm making a stupid suggestion...
--
"Linux: the operating system with a CLUE... Command Line User
Environment". (seen in a posting in comp.software.testing)
cbbrowne@hex.net - <http://www.hex.net/~cbbrowne/lsf.html>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-22 4:00 ` Bruce Allen
1998-05-22 4:00 ` Greg Lindahl
@ 1998-05-23 4:00 ` Dong-Woon Park
1998-05-23 4:00 ` Bruce Allen
1998-05-24 4:00 ` Joachim Wesner
1998-05-23 4:00 ` Christopher Browne
2 siblings, 2 replies; 13+ messages in thread
From: Dong-Woon Park @ 1998-05-23 4:00 UTC (permalink / raw)
[-- Attachment #1: Type: text/plain, Size: 945 bytes --]
Thanks for replies.
Why polynomials are faster ? I don't know at all about the way
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
>
--
PGP public key: http://ghost1.snu.ac.kr/~dwpark/dwpark.asc
[-- Attachment #2: Type: application/pgp-signature, Size: 346 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Christopher Browne
@ 1998-05-23 4:00 ` Linus Torvalds
1998-05-23 4:00 ` Greg Lindahl
1998-05-23 4:00 ` sin() on alpha Andi Kleen
1998-05-23 4:00 ` Michal Jaegermann
1 sibling, 2 replies; 13+ messages in thread
From: Linus Torvalds @ 1998-05-23 4:00 UTC (permalink / raw)
In article <6k59a0$1cq$1@blue.hex.net>,
Christopher Browne <cbbrowne@hex.net> wrote:
>
>'Tis been a while since I've calculated trig values; are there any
>processes where you start with an estimate, and then iterate to refine
>it?
>
>In that case, you would start with an interpolation table (probably
>linear) involving some "small-but-useful" set of values to get the
>initial estimate, and then iterate to improve precision.
>
>A 1024 entry linear interpolation table is going to get one *fairly*
>close; I don't recall offhand what identities would be used to do
>"function point iteration," but no doubt there's something useful. A
>higher-order polynomial interpolation table would surely give somewhat
>better results, at somewhat higher cost, to be sure.
Be careful.
This is what DEC does with their integer divide routine, and it performs
pretty well for benchmarks where the data cache is completely quiescent
apart from the divide.
However, with a cold data cache (or a benchmark that wants to use the
data cache for other things) the DEC routines were actually slower than
doing the divide the "stupid" way which has very nice cache behaviour.
Remember: if you can make a small and simple routine that avoids one
cache miss, that one cache miss is often more costly than the cost of
the whole routine. This is less true of more complex functions like
sin() and cos(), but it's still something that is easily overlooked,
because when you write the routines you tend to test performance by
having something like
for (i = 0; i < 100000000; i++) {
sum += sin(x); x += 0.00001;
}
or similar where the cache will be warm all the time.
So the traditional argument goes "well, if it isn't in the cache, then
we don't care about performance, because it obviously isn't being called
often enough to matter".
Which is very shortsighted. Nobody cares about the performance of most
library routines on their own. People care about the performance of
programs that _use_ the routines, and if the routines need to be in the
cache to be fast, then that implies that something _else_ cannot be in
the cache.
So do take cache behaviour into account. One great thing about modern
CPU's is that they all tend to have cycle counters, which means that you
can actually benchmark how fast a routine is even with a cold cache,
because you can actually measure the performance of the very first
invocation rather than the performance of doing a million of the things
in a loop.
Don't be overly scared about loops. The one great thing about loops is
that they perform _very_ well icache-wise, and as a result it can be
faster to do an extra iteration or two if it cuts down on your misses.
A cache miss can easily cost you several hundred instruction slots
wasted.
Linus
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Linus Torvalds
1998-05-23 4:00 ` Greg Lindahl
@ 1998-05-23 4:00 ` Andi Kleen
1 sibling, 0 replies; 13+ messages in thread
From: Andi Kleen @ 1998-05-23 4:00 UTC (permalink / raw)
torvalds@transmeta.com (Linus Torvalds) writes:
>
> Remember: if you can make a small and simple routine that avoids one
> cache miss, that one cache miss is often more costly than the cost of
> the whole routine. This is less true of more complex functions like
> sin() and cos(), but it's still something that is easily overlooked,
> because when you write the routines you tend to test performance by
> having something like
>
> for (i = 0; i < 100000000; i++) {
> sum += sin(x); x += 0.00001;
> }
>
> or similar where the cache will be warm all the time.
If you want a good example for this sin look at glibc's source in
stdio-common/_itoa.c
It really shows how to _not_ do it.
-Andi
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Christopher Browne
1998-05-23 4:00 ` Linus Torvalds
@ 1998-05-23 4:00 ` Michal Jaegermann
1 sibling, 0 replies; 13+ messages in thread
From: Michal Jaegermann @ 1998-05-23 4:00 UTC (permalink / raw)
Christopher Browne (cbbrowne@news.hex.net) wrote:
: On Fri, 22 May 1998 02:29:35 -0500, Bruce Allen
: >
: >NO! Use min/max (Chebyshev) polynomials to evaulate sin(x).
: In that case, you would start with an interpolation table (probably
: linear) involving some "small-but-useful" set of values to get the
: initial estimate, and then iterate to improve precision.
Tables are really not so hot as they will be killing your caches
when it matters most. See an article published by David Mosberger
in issues 42 and 43 of Linux Journal for a lot of details why.
A good polynomial approximation can be surprisigly good - especially
when you will think carefuly about your argument ranges.
Michal
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Linus Torvalds
@ 1998-05-23 4:00 ` Greg Lindahl
1998-05-24 4:00 ` sin() on alpha / Cray libm Joachim Wesner
1998-05-23 4:00 ` sin() on alpha Andi Kleen
1 sibling, 1 reply; 13+ messages in thread
From: Greg Lindahl @ 1998-05-23 4:00 UTC (permalink / raw)
torvalds@transmeta.com (Linus Torvalds) writes:
> Remember: if you can make a small and simple routine that avoids one
> cache miss, that one cache miss is often more costly than the cost of
> the whole routine. This is less true of more complex functions like
> sin() and cos(), but it's still something that is easily overlooked,
This is very true. I would suspect that the Cray "vector" sin() cos()
and other library functions might have completely different
implementations for this reason; when you know you are computing with
a warm cache, you can use different algorithms... so if your compiler
is already doing vector analysis, you can get a big win.
-- g
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Dong-Woon Park
@ 1998-05-23 4:00 ` Bruce Allen
1998-05-24 4:00 ` Joachim Wesner
1998-05-24 4:00 ` Joachim Wesner
1 sibling, 1 reply; 13+ messages in thread
From: Bruce Allen @ 1998-05-23 4:00 UTC (permalink / raw)
[-- Attachment #1: Type: text/plain, Size: 12970 bytes --]
Dong-Woon Park wrote:
> Why polynomials are faster ? I don't know at all about the way
> 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
For 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.
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
[-- Attachment #2: Type: text/html, Size: 15004 bytes --]
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Dong-Woon Park
1998-05-23 4:00 ` Bruce Allen
@ 1998-05-24 4:00 ` Joachim Wesner
1 sibling, 0 replies; 13+ messages in thread
From: Joachim Wesner @ 1998-05-24 4:00 UTC (permalink / raw)
Dong-Woon Park wrote:
>
> Thanks for replies.
>
>
> Why polynomials are faster ? I don't know at all about the way
> 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 ?
>
the actual libm uses polynomials, however
1) approximating over a certain range is only one part of the task,
you need also do range reduction for larger arguments and this can
be a nontrivial task for example for the trig-functions. It is *NOT*
good enough to subtract a multiple of a double precision pi value
to get into the range, this will give you a totally wrong result
for say sin(1e6). Somebody would probably say, this is pointless,
because the relative precision in the large argument is so
much lower, however, the usual philosophy here is to take the
big number
as "exact" and to demand that the routine should give the same result
for that large number as an infinite precision calculation (within
the few lowest bits). Usually that means you need to simulate kind
of a quad floating precision calculation here.
2) the libm range reduction is pretty complex (way larger in sourcecode
than any of the approximation routines therein), I think a bit
overdone in precision and it seems that gcc does a bad job in
compiling it.
3) Likewise the approximation polymials in libm are a bit to much on the
save side regarding (theoretical) precision, that is mostly lost in
the rest of the calculation, that is only double precision, for
speed.
4) Still, my measurements clearly show that for large arguments, running
time in libm is mostly governed by range reduction and not
approximation.
5) goto in japan rewrote part of the sine and cos routines in
assembler and optimized the approximation with respect to the
pipelined nature
of the alpha, however, it seems he still uses the c-Version of the
too slow original range reduction, and looses again for large
arguments.
I'm working on a different set of routines that uses a somewhat simpler
approximation that sacrifices some accuracy for speed and it seems I
have a more effective range reduction. However, in the recent days I
have been porting very interesting routines from Cray they use on their
Multiple-Alpha machine (somebody pointed those out here) that have
virtually constant running times for all arguments. The copyright status
of those is not yet totally clear, these could be pretty interesting for
us. See my other post.
Joachim
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha / Cray libm
1998-05-23 4:00 ` Greg Lindahl
@ 1998-05-24 4:00 ` Joachim Wesner
0 siblings, 0 replies; 13+ messages in thread
From: Joachim Wesner @ 1998-05-24 4:00 UTC (permalink / raw)
Greg Lindahl wrote:
>
> torvalds@transmeta.com (Linus Torvalds) writes:
>
> > Remember: if you can make a small and simple routine that avoids one
> > cache miss, that one cache miss is often more costly than the cost of
> > the whole routine. This is less true of more complex functions like
> > sin() and cos(), but it's still something that is easily overlooked,
>
> This is very true. I would suspect that the Cray "vector" sin() cos()
> and other library functions might have completely different
> implementations for this reason; when you know you are computing with
> a warm cache, you can use different algorithms... so if your compiler
> is already doing vector analysis, you can get a big win.
Actually, their fast *scalar* routines (did not yet look at the vector
routines) seem to use a pretty good balance between a table-based
approach and an polynomial approximation, some functions use tables with
up to 100 double prec constants, so some cache space will be needed :-((
I do not yet understand the exact algorithm, because all the constant
are coded als big integer numbers, originally in a fortran block data
subroutine !!! However, I got the stuff running in a prelim. form on
linux/axp. The latest results are:
My measured execution time on a LX 533 (1 tick ~ 2 ns) (all routines
were tested with random arguments from 0 to x)
0..x 0..0.3 0..10 0..300
actual libm:
sin 0.18 us 0.45 us 0.51 us
cos 0.20 us 0.46 us 0.52 us
tan 0.30 us 0.63 us 0.69 us
log 0.45 us 0.45 us 0.45 us
log10 0.62 us 0.61 us 0.61 us
sqrt 0.19 us 0.19 us 0.19 us
New converted CRAY routines:
sin 0.20 us 0.20 us 0.20 us
cos 0.22 us 0.22 us 0.22 us
sin+cos
(simultaneous!) 0.24 us 0.24 us 0.24 us
tan
(derived) 0.27 us 0.27 us 0.27 us
log 0.16 us 0.16 us 0.16 us
log10 0.19 us 0.19 us 0.19 us
exp 0.20 us 0.20 us 0.20 us
sqrt 0.16 us 0.15 us 0.15 us
These results are for a "hot" cache in a tight loop, the random #
generation and the looping/calling overhad removed !
All those sacrifice a bit of accuracy, often the last bit and sometimes
the second last bit will be wrong, as oposed to libm that often gets the
last bit (rounded) correctly. There's still a fast cubic root and some
reduced precision (~ 32 bits) routine. I will add a good (derived) pow
soon (hey, it's not always as easy as exp(log(x)*y)). HOWEVER, there are
no inverse trig and no hyperbolic functions in the cray lib.
Some cleaning up work will still have to follow before it would be ready
for general use (if copyright permits), as it uses temporary storage in
a *static* data segment (just where also the *constants* where stored,
this is a 100% typical good old fortran approach), so it could not be
interrupted and shared in the actual form, but those changes seem pretty
trivial.
Joachim
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: sin() on alpha.
1998-05-23 4:00 ` Bruce Allen
@ 1998-05-24 4:00 ` Joachim Wesner
0 siblings, 0 replies; 13+ messages in thread
From: Joachim Wesner @ 1998-05-24 4:00 UTC (permalink / raw)
Bruce Allen wrote:
>
> Dong-Woon Park wrote:
>
> Why polynomials are faster ? I don't know at all about the
> way
> 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 ?
>
> For 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.
> As I said, the key is if you don't need to do range reduction.
>
However, if you can decide pretty fast if range reduction is needed or
not, you can nearly have the best of both worlds. Usual this will
require to go to assembler in effectively implementing it. I mostly
agree with your basic conclusion, actually I wrote a full set of of libm
routines using alternative chebyshev polynomials and, as it seems, a
pretty effective range reduction, for the i860 several years ago.
However, I cannot use most of the coding ideas as is, as the stuff was
also give to a company. On the other hand, Alpha is different enough in
characteristics from the i860 so that some further modifications could
lead to an even better result. See below.
>
> 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.
> 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
The problem with that kind of code, "Horner's scheme", is that it is of
full recursive nature, you can't do the add, before you have the result
of the mult and cannot do the mult before the result of the add, because
all depends on each other. So the pipelined nature of the FPU will not
be helpful and you need to wait the full 4 cycles before calculation can
continue. Supercomputer people would say that such a construct is not
"vectorizable". This was, in principle, even an issue on the i860, that
had explicit "pipelined" floating adds and mults, so you could simulate
a "vector machine", however, as you needed to prescribe any single step
in the pipeline stages, like in a long instruction word microprogram, it
was a hassle to use, (the high level compilers usually could not
generate such a code), so one used the "automatic" "scalar" float
instructions instead, where, however, much of the pipelining nature was
lost and the horner scheme would run (nearly) as good as any other
scheme, it's calculation time mostly depending on the overall number of
ops. On the Alpha instead, that fully exploits the internal pipelining
automatically, if you only feed the operands that way that no
dependencies and hence stalls will appear, it is much more interesting
to find another scheme, that, even when using a bit higher # of
operations, runs faster in the end.
Kazushige Goto <goto@statabo.rim.or.jp> rewritten sin()and cos()
routines seem to acchieve that pretty effectively and if one would
remove the arguments reduction, that is still too slow in the actual
version, they should be faster in a limited interval as anything else
one could compile in C using a hornerlike scheme, even when using the
DEC compiler.
Joachim
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~1998-05-24 4:00 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-05-22 4:00 sin() on alpha Dong-Woon Park
1998-05-22 4:00 ` Bruce Allen
1998-05-22 4:00 ` Greg Lindahl
1998-05-23 4:00 ` Dong-Woon Park
1998-05-23 4:00 ` Bruce Allen
1998-05-24 4:00 ` Joachim Wesner
1998-05-24 4:00 ` Joachim Wesner
1998-05-23 4:00 ` Christopher Browne
1998-05-23 4:00 ` Linus Torvalds
1998-05-23 4:00 ` Greg Lindahl
1998-05-24 4:00 ` sin() on alpha / Cray libm Joachim Wesner
1998-05-23 4:00 ` sin() on alpha Andi Kleen
1998-05-23 4:00 ` Michal Jaegermann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox