sin(cos) algorithm

32 views
Skip to first unread message

Domagoj Šarić

unread,
Jun 2, 2011, 7:58:06 AM6/2/11
to nt2...@googlegroups.com
Is it a secret which algorithm is employed for the sin(cos) functions? :)
I'm asking because when I stepped through the sincos code I noticed
the angle reduction code and it seemed very complex to me (I thought a
modulo should be enough). Supposing I was maybe thin at math and/or
the instruction scheduling details I first asked at #nt2 but Mathias
and Joel said it might be better to "ask the math guy" on the list ;)


--
"What Huxley teaches is that in the age of advanced technology, spiritual
devastation is more likely to come from an enemy with a smiling face than
from one whose countenance exudes suspicion and hate."
Neil Postman

jtl

unread,
Jun 2, 2011, 8:16:49 AM6/2/11
to nt2...@googlegroups.com
Le 02/06/2011 13:58, Domagoj Šarić a écrit :
> Is it a secret which algorithm is employed for the sin(cos) functions? :)
> I'm asking because when I stepped through the sincos code I noticed
> the angle reduction code and it seemed very complex to me (I thought a
> modulo should be enough). Supposing I was maybe thin at math and/or
> the instruction scheduling details I first asked at #nt2 but Mathias
> and Joel said it might be better to "ask the math guy" on the list ;)
>
>
No secret,
If there is no errors in what we wrote, we use several reduction
algorithms of increasing
complexities according to the magnitude of the parameter involved.
On simd vectors the used algorithm is the less complex to fit all the
vector arguments. (max of magnitudes over the vector)
All the way I thought the pure modulo was not quite sufficient with the
case limits
we used, but perhaps you are right and I could try and intermediate
beween pi/4 and ~100*pi
for which simple modulo could rise the right answer.
Thanks for the implicit suggestion.

J.T. Lapresté

Domagoj Saric

unread,
Jun 6, 2011, 9:38:19 AM6/6/11
to nt2...@googlegroups.com
On 2.6.2011. 14:16, jtl wrote:
> No secret,
> If there is no errors in what we wrote, we use several reduction algorithms of
> increasing
> complexities according to the magnitude of the parameter involved.
> On simd vectors the used algorithm is the less complex to fit all the
> vector arguments. (max of magnitudes over the vector)
> All the way I thought the pure modulo was not quite sufficient with the case limits
> we used, but perhaps you are right and I could try and intermediate beween pi/4
> and ~100*pi
> for which simple modulo could rise the right answer.

Is the nt2::sincos algorithm (including its angle reduction) some "well
known"/"named" algorithm (whose pseudo-code/description can be found somewhere
online)? I'm asking because I'm for example still puzzled as to why the modulo
approach would not be good enough for all angles (and instead of having you
explaining it here I can then just google it ;)


> Thanks for the implicit suggestion.

My pleasure ;)

jtl

unread,
Jun 6, 2011, 1:15:30 PM6/6/11
to nt2...@googlegroups.com
Le 06/06/2011 15:38, Domagoj Saric a écrit :
> On 2.6.2011. 14:16, jtl wrote:
>> No secret,
>> If there is no errors in what we wrote, we use several reduction
>> algorithms of
>> increasing
>> complexities according to the magnitude of the parameter involved.
>> On simd vectors the used algorithm is the less complex to fit all the
>> vector arguments. (max of magnitudes over the vector)
>> All the way I thought the pure modulo was not quite sufficient with
>> the case limits
>> we used, but perhaps you are right and I could try and intermediate
>> beween pi/4
>> and ~100*pi
>> for which simple modulo could rise the right answer.
>
> Is the nt2::sincos algorithm (including its angle reduction) some
> "well known"/"named" algorithm (whose pseudo-code/description can be
> found somewhere online)? I'm asking because I'm for example still
> puzzled as to why the modulo approach would not be good enough for all
> angles (and instead of having you explaining it here I can then just
> google it ;)
>
>
>> Thanks for the implicit suggestion.
>
> My pleasure ;)
>
>
I tried "the implicit suggestion" and in fact, my experiment showed it
never works with 0.5 ulp.
Even if x is in [0 pi/2], substracting pi/2 (floating value) to get
something between [-pi/4, pi/4]
does not ever lead (as well in floats as in double) to a sufficient
precision
to get a 0.5 ulp correct value of (for instance) of the sin(x).
The only advantage I found and I will nearly commit is that in the
interval [0pi/4pi/2], we can get
a slight simplification of the modulo approach

The reduction used are those that can be found in cephes library or
fdlibm classical libraries
(provided with nt2). They are not new, rather old in fact, but I do not
know the articles of reference.

The simplest algorithm, valid on a small range use the writing of pi/2
as the sum of 3
smaller and smaller terms which improve the accuracy in cancellations cases
and are harmless elsewhere.

Of course, straight computations using double (for float) or extended
for (double)
can lead in some cases to sufficently accurate results, but they are not
very intesting
because slow in simd
and not available everywhere in scalar.


J.T. Lapresté

mau...@blueskystudios.com

unread,
May 8, 2015, 6:23:01 PM5/8/15
to nt2...@googlegroups.com, jtlap...@gmail.com
Hi,

I was doing some testing on ISPC and I happen to notice some performance problem with the angle reduction too.

When using values from random () I get poor performance compared to c++ (clang), but values in the range 0-PI work fine.

I can give a reproducer if you are interested.

Thanks.

Joel FALCOU

unread,
May 13, 2015, 5:06:29 AM5/13/15
to nt2...@googlegroups.com, jtlap...@gmail.com


On 09/05/2015 00:23, mau...@blueskystudios.com wrote:
> Hi,
>
> I was doing some testing on ISPC and I happen to notice some performance
> problem with the angle reduction too.
>
> When using values from random () I get poor performance compared to c++
> (clang), but values in the range 0-PI work fine.

Sounds normal actually.

> I can give a reproducer if you are interested.

Yes please.

Regards
Reply all
Reply to author
Forward
0 new messages