# [libre-riscv-dev] [isa-dev] Re: FP transcendentals (trigonometry, root/exp/log) proposal

Jacob Lifshay programmerjake at gmail.com
Tue Aug 13 04:00:42 BST 2019

```On Mon, Aug 12, 2019 at 10:52 AM 'MitchAlsup' via RISC-V ISA Dev
<isa-dev at groups.riscv.org> wrote:
> double ATAN2( double y, double x )
>
> {   // IEEE 754-2008 quality ATAN2
>
>     // deal with NANs
>
>     if( ISNAN( x )             ) return x;
>
>     if( ISNAN( y )             ) return y;
>
>     // deal with infinities
>
>     if( x == +∞    && |y|== +∞  ) return copysign(  π/4, y );
>
>     if( x == +∞                 ) return copysign(  0.0, y );
>
>     if( x == -∞    && |y|== +∞  ) return copysign( 3π/4, y );
>
>     if( x == -∞                 ) return copysign(    π, y );
>
>     if(               |y|== +∞  ) return copysign(  π/2, y );
>
>     // deal with signed zeros
>
>     if( x == 0.0  &&  y != 0.0 ) return copysign(  π/2, y );
>
>     if( x >=+0.0  &&  y == 0.0 ) return copysign(  0.0, y );
>
>     if( x <=-0.0  &&  y == 0.0 ) return copysign(    π, y );
>
>     // calculate ATAN2 textbook style
>
>     if( x  > 0.0               ) return     ATAN( |y / x| );
>
>     if( x  < 0.0               ) return π - ATAN( |y / x| );
>
> }
>
>
> Yet the proposed encoding makes ATAN2 the primitive and has ATAN invent a constant and then call/use ATAN2.
>
> When one considers an implementation of ATAN, one must consider several ranges of evaluation::
>
>      x Î [  -∞, -1.0]:: ATAN( x ) = -π/2 + ATAN( 1/x );
>
>      x Î (-1.0, +1.0]:: ATAN( x ) =      + ATAN(   x );
>
>      x Î [ 1.0,   +∞]:: ATAN( x ) = +π/2 - ATAN( 1/x );

Notice how the ATAN implementation has a division before the
polynomial (or whatever implements the function in (-1.0, +1.0] ),
that division can be merged with the ATAN2 division to end up with
ATAN2 taking the same time as ATAN (assuming all the special cases can
be handled in parallel to the division). For the case of ATAN2(1.0, x)
with -1.0 < x <= +1.0, the division can be skipped if the pipeline is
designed to be able to forward operations to a later than normal
stage. So, in the case that reciprocal is the same speed as division
(which it is in our current division pipeline) ATAN2 is not more
expensive than ATAN except for the special cases which don't take up
that many gates to decode them.

Jacob Lifshay

```