e_rem_pio2l.h revision 187128
140843Smsmith
240843Smsmith/* @(#)e_rem_pio2.c 1.4 95/01/18 */
340843Smsmith/*
440843Smsmith * ====================================================
594290Sdcs * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
651786Sdcs *
751786Sdcs * Developed at SunSoft, a Sun Microsystems, Inc. business.
840843Smsmith * Permission to use, copy, modify, and distribute this
940843Smsmith * software is freely granted, provided that this notice
1040843Smsmith * is preserved.
1140843Smsmith * ====================================================
1240843Smsmith *
1340843Smsmith * Optimized by Bruce D. Evans.
1440843Smsmith */
1540843Smsmith
1640843Smsmith#include <sys/cdefs.h>
1740843Smsmith__FBSDID("$FreeBSD: head/lib/msun/src/e_rem_pio2.c 187128 2009-01-13 05:13:20Z das $");
1840843Smsmith
1940843Smsmith/* __ieee754_rem_pio2(x,y)
2040843Smsmith *
2140843Smsmith * return the remainder of x rem pi/2 in y[0]+y[1]
2240843Smsmith * use __kernel_rem_pio2()
2340843Smsmith */
2440843Smsmith
2540843Smsmith#include <float.h>
2640843Smsmith
2740843Smsmith#include "math.h"
2840843Smsmith#include "math_private.h"
2940843Smsmith
3040843Smsmith/*
3140843Smsmith * invpio2:  53 bits of 2/pi
3240843Smsmith * pio2_1:   first  33 bit of pi/2
3376116Sdcs * pio2_1t:  pi/2 - pio2_1
3461584Sdcs * pio2_2:   second 33 bit of pi/2
3561584Sdcs * pio2_2t:  pi/2 - (pio2_1+pio2_2)
3676116Sdcs * pio2_3:   third  33 bit of pi/2
3761584Sdcs * pio2_3t:  pi/2 - (pio2_1+pio2_2+pio2_3)
3861584Sdcs */
3961584Sdcs
4061584Sdcsstatic const double
4161584Sdcszero =  0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
4261584Sdcstwo24 =  1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
4376116Sdcsinvpio2 =  6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */
4461584Sdcspio2_1  =  1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
4561584Sdcspio2_1t =  6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */
4661584Sdcspio2_2  =  6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */
4761584Sdcspio2_2t =  2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */
4876116Sdcspio2_3  =  2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */
4976116Sdcspio2_3t =  8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */
5076116Sdcs
5176116Sdcs#ifdef INLINE_REM_PIO2
5261584Sdcsextern __gnu89_inline
5340843Smsmith#endif
5440843Smsmithint
5540843Smsmith__ieee754_rem_pio2(double x, double *y)
5676116Sdcs{
5776116Sdcs	double z,w,t,r,fn;
5856718Sdcs	double tx[3],ty[2];
5956718Sdcs	int32_t e0,i,j,nx,n,ix,hx;
6040843Smsmith	u_int32_t low;
6194290Sdcs
6240843Smsmith	GET_HIGH_WORD(hx,x);		/* high word of x */
6394290Sdcs	ix = hx&0x7fffffff;
6494290Sdcs#if 0 /* Must be handled in caller. */
6594290Sdcs	if(ix<=0x3fe921fb)   /* |x| ~<= pi/4 , no need for reduction */
6640843Smsmith	    {y[0] = x; y[1] = 0; return 0;}
6740843Smsmith#endif
6851786Sdcs	if (ix <= 0x400f6a7a) {		/* |x| ~<= 5pi/4 */
6940843Smsmith	    if ((ix & 0xfffff) == 0x921fb)  /* |x| ~= pi/2 or 2pi/2 */
7051786Sdcs		goto medium;		/* cancellation -- use medium case */
7140843Smsmith	    if (ix <= 0x4002d97c) {	/* |x| ~<= 3pi/4 */
7240843Smsmith		if (hx > 0) {
7340843Smsmith		    z = x - pio2_1;	/* one round good to 85 bits */
7440843Smsmith		    y[0] = z - pio2_1t;
7540843Smsmith		    y[1] = (z-y[0])-pio2_1t;
7640843Smsmith		    return 1;
7740843Smsmith		} else {
7840843Smsmith		    z = x + pio2_1;
7940843Smsmith		    y[0] = z + pio2_1t;
8040843Smsmith		    y[1] = (z-y[0])+pio2_1t;
8140843Smsmith		    return -1;
8240843Smsmith		}
8340843Smsmith	    } else {
8440843Smsmith		if (hx > 0) {
8540843Smsmith		    z = x - 2*pio2_1;
8660959Sdcs		    y[0] = z - 2*pio2_1t;
8760959Sdcs		    y[1] = (z-y[0])-2*pio2_1t;
8840843Smsmith		    return 2;
8940843Smsmith		} else {
9040843Smsmith		    z = x + 2*pio2_1;
9140843Smsmith		    y[0] = z + 2*pio2_1t;
9240843Smsmith		    y[1] = (z-y[0])+2*pio2_1t;
9340843Smsmith		    return -2;
9440843Smsmith		}
9540843Smsmith	    }
9640843Smsmith	}
9740843Smsmith	if (ix <= 0x401c463b) {		/* |x| ~<= 9pi/4 */
9840843Smsmith	    if (ix <= 0x4015fdbc) {	/* |x| ~<= 7pi/4 */
9940843Smsmith		if (ix == 0x4012d97c)	/* |x| ~= 3pi/2 */
10040843Smsmith		    goto medium;
10140843Smsmith		if (hx > 0) {
10276116Sdcs		    z = x - 3*pio2_1;
10376116Sdcs		    y[0] = z - 3*pio2_1t;
10476116Sdcs		    y[1] = (z-y[0])-3*pio2_1t;
10576116Sdcs		    return 3;
10676116Sdcs		} else {
10776116Sdcs		    z = x + 3*pio2_1;
10876116Sdcs		    y[0] = z + 3*pio2_1t;
10976116Sdcs		    y[1] = (z-y[0])+3*pio2_1t;
11040843Smsmith		    return -3;
11140843Smsmith		}
11240843Smsmith	    } else {
11376116Sdcs		if (ix == 0x401921fb)	/* |x| ~= 4pi/2 */
11476116Sdcs		    goto medium;
11576116Sdcs		if (hx > 0) {
11676116Sdcs		    z = x - 4*pio2_1;
11740843Smsmith		    y[0] = z - 4*pio2_1t;
11840843Smsmith		    y[1] = (z-y[0])-4*pio2_1t;
11940843Smsmith		    return 4;
12040843Smsmith		} else {
12140843Smsmith		    z = x + 4*pio2_1;
12276116Sdcs		    y[0] = z + 4*pio2_1t;
12376116Sdcs		    y[1] = (z-y[0])+4*pio2_1t;
12476116Sdcs		    return -4;
12540843Smsmith		}
12676116Sdcs	    }
12740843Smsmith	}
12876116Sdcs	if(ix<0x413921fb) {	/* |x| ~< 2^20*(pi/2), medium size */
12976116Sdcsmedium:
13040843Smsmith	    /* Use a specialized rint() to get fn.  Assume round-to-nearest. */
13176116Sdcs	    STRICT_ASSIGN(double,fn,x*invpio2+0x1.8p52);
13276116Sdcs	    fn = fn-0x1.8p52;
13376116Sdcs#ifdef HAVE_EFFICIENT_IRINT
13476116Sdcs	    n  = irint(fn);
13576116Sdcs#else
13676116Sdcs	    n  = (int32_t)fn;
13776116Sdcs#endif
13876116Sdcs	    r  = x-fn*pio2_1;
13976116Sdcs	    w  = fn*pio2_1t;	/* 1st round good to 85 bit */
14076116Sdcs	    {
14176116Sdcs	        u_int32_t high;
14240843Smsmith	        j  = ix>>20;
14340843Smsmith	        y[0] = r-w;
14440843Smsmith		GET_HIGH_WORD(high,y[0]);
14540843Smsmith	        i = j-((high>>20)&0x7ff);
14640843Smsmith	        if(i>16) {  /* 2nd iteration needed, good to 118 */
14740843Smsmith		    t  = r;
14840843Smsmith		    w  = fn*pio2_2;
14940843Smsmith		    r  = t-w;
15040843Smsmith		    w  = fn*pio2_2t-((t-r)-w);
15140843Smsmith		    y[0] = r-w;
15240843Smsmith		    GET_HIGH_WORD(high,y[0]);
15340843Smsmith		    i = j-((high>>20)&0x7ff);
15440843Smsmith		    if(i>49)  {	/* 3rd iteration need, 151 bits acc */
15540843Smsmith		    	t  = r;	/* will cover all possible cases */
15676116Sdcs		    	w  = fn*pio2_3;
15776116Sdcs		    	r  = t-w;
15876116Sdcs		    	w  = fn*pio2_3t-((t-r)-w);
15976116Sdcs		    	y[0] = r-w;
16076116Sdcs		    }
16176116Sdcs		}
16276116Sdcs	    }
16376116Sdcs	    y[1] = (r-y[0])-w;
16476116Sdcs	    return n;
16576116Sdcs	}
16676116Sdcs    /*
16740843Smsmith     * all other (large) arguments
16876116Sdcs     */
16976116Sdcs	if(ix>=0x7ff00000) {		/* x is inf or NaN */
17076116Sdcs	    y[0]=y[1]=x-x; return 0;
17176116Sdcs	}
17240843Smsmith    /* set z = scalbn(|x|,ilogb(x)-23) */
17394290Sdcs	GET_LOW_WORD(low,x);
17494290Sdcs	SET_LOW_WORD(z,low);
17540843Smsmith	e0 	= (ix>>20)-1046;	/* e0 = ilogb(z)-23; */
17694290Sdcs	SET_HIGH_WORD(z, ix - ((int32_t)(e0<<20)));
17794290Sdcs	for(i=0;i<2;i++) {
17894290Sdcs		tx[i] = (double)((int32_t)(z));
17994290Sdcs		z     = (z-tx[i])*two24;
18094290Sdcs	}
18194290Sdcs	tx[2] = z;
18294290Sdcs	nx = 3;
18394290Sdcs	while(tx[nx-1]==zero) nx--;	/* skip zero term */
18494290Sdcs	n  =  __kernel_rem_pio2(tx,ty,e0,nx,1);
18594290Sdcs	if(hx<0) {y[0] = -ty[0]; y[1] = -ty[1]; return -n;}
18694290Sdcs	y[0] = ty[0]; y[1] = ty[1]; return n;
18794290Sdcs}
18894290Sdcs