From: Joachim Wesner <joachim.wesner@frankfurt.netsurf.de>
Subject: Re: sin() on alpha.
Date: 1998/05/24 [thread overview]
Message-ID: <3567DC0A.35A72265@frankfurt.netsurf.de> (raw)
In-Reply-To: 3567893D.F3C0B746@dirac.phys.uwm.edu
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
next prev parent reply other threads:[~1998-05-24 4:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Christopher Browne
1998-05-23 4:00 ` Michal Jaegermann
1998-05-23 4:00 ` Linus Torvalds
1998-05-23 4:00 ` Andi Kleen
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 Dong-Woon Park
1998-05-23 4:00 ` Bruce Allen
1998-05-24 4:00 ` Joachim Wesner [this message]
1998-05-24 4:00 ` Joachim Wesner
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox