e_rem_pio2l.h revision 221234
1/* From: @(#)e_rem_pio2.c 1.4 95/01/18 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
6 *
7 * Developed at SunSoft, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 *
13 * Optimized by Bruce D. Evans.
14 */
15
16#include <sys/cdefs.h>
17__FBSDID("$FreeBSD: head/lib/msun/ld128/e_rem_pio2l.h 221234 2011-04-29 23:13:43Z kargl $");
18
19/* ld128 version of __ieee754_rem_pio2l(x,y)
20 *
21 * return the remainder of x rem pi/2 in y[0]+y[1]
22 * use __kernel_rem_pio2()
23 */
24
25#include <float.h>
26
27#include "math.h"
28#include "math_private.h"
29#include "fpmath.h"
30
31#define	BIAS	(LDBL_MAX_EXP - 1)
32
33/*
34 * XXX need to verify that nonzero integer multiples of pi/2 within the
35 * range get no closer to a long double than 2**-140, or that
36 * ilogb(x) + ilogb(min_delta) < 45 - -140.
37 */
38/*
39 * invpio2:  113 bits of 2/pi
40 * pio2_1:   first  68 bits of pi/2
41 * pio2_1t:  pi/2 - pio2_1
42 * pio2_2:   second 68 bits of pi/2
43 * pio2_2t:  pi/2 - (pio2_1+pio2_2)
44 * pio2_3:   third  68 bits of pi/2
45 * pio2_3t:  pi/2 - (pio2_1+pio2_2+pio2_3)
46 */
47
48static const double
49zero =  0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
50two24 =  1.67772160000000000000e+07; /* 0x41700000, 0x00000000 */
51
52static const long double
53invpio2 =  6.3661977236758134307553505349005747e-01L,	/*  0x145f306dc9c882a53f84eafa3ea6a.0p-113 */
54pio2_1  =  1.5707963267948966192292994253909555e+00L,	/*  0x1921fb54442d18469800000000000.0p-112 */
55pio2_1t =  2.0222662487959507323996846200947577e-21L,	/*  0x13198a2e03707344a4093822299f3.0p-181 */
56pio2_2  =  2.0222662487959507323994779168837751e-21L,	/*  0x13198a2e03707344a400000000000.0p-181 */
57pio2_2t =  2.0670321098263988236496903051604844e-43L,	/*  0x127044533e63a0105df531d89cd91.0p-254 */
58pio2_3  =  2.0670321098263988236499468110329591e-43L,	/*  0x127044533e63a0105e00000000000.0p-254 */
59pio2_3t = -2.5650587247459238361625433492959285e-65L;	/* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */
60
61#ifdef INLINE_REM_PIO2L
62static inline __always_inline
63#endif
64int
65__ieee754_rem_pio2l(long double x, long double *y)
66{
67	union IEEEl2bits u,u1;
68	long double z,w,t,r,fn;
69	double tx[5],ty[3];
70	int64_t n;
71	int e0,ex,i,j,nx;
72	int16_t expsign;
73
74	u.e = x;
75	expsign = u.xbits.expsign;
76	ex = expsign & 0x7fff;
77	if (ex < BIAS + 45 || ex == BIAS + 45 &&
78	    u.bits.manh < 0x921fb54442d1LL) {
79	    /* |x| ~< 2^45*(pi/2), medium size */
80	    /* Use a specialized rint() to get fn.  Assume round-to-nearest. */
81	    fn = x*invpio2+0x1.8p112;
82	    fn = fn-0x1.8p112;
83#ifdef HAVE_EFFICIENT_I64RINT
84	    n  = i64rint(fn);
85#else
86	    n  = fn;
87#endif
88	    r  = x-fn*pio2_1;
89	    w  = fn*pio2_1t;	/* 1st round good to 180 bit */
90	    {
91		union IEEEl2bits u2;
92	        int ex1;
93	        j  = ex;
94	        y[0] = r-w;
95		u2.e = y[0];
96		ex1 = u2.xbits.expsign & 0x7fff;
97	        i = j-ex1;
98	        if(i>51) {  /* 2nd iteration needed, good to 248 */
99		    t  = r;
100		    w  = fn*pio2_2;
101		    r  = t-w;
102		    w  = fn*pio2_2t-((t-r)-w);
103		    y[0] = r-w;
104		    u2.e = y[0];
105		    ex1 = u2.xbits.expsign & 0x7fff;
106		    i = j-ex1;
107		    if(i>119) {	/* 3rd iteration need, 316 bits acc */
108		    	t  = r;	/* will cover all possible cases */
109		    	w  = fn*pio2_3;
110		    	r  = t-w;
111		    	w  = fn*pio2_3t-((t-r)-w);
112		    	y[0] = r-w;
113		    }
114		}
115	    }
116	    y[1] = (r-y[0])-w;
117	    return n;
118	}
119    /*
120     * all other (large) arguments
121     */
122	if(ex==0x7fff) {		/* x is inf or NaN */
123	    y[0]=y[1]=x-x; return 0;
124	}
125    /* set z = scalbn(|x|,ilogb(x)-23) */
126	u1.e = x;
127	e0 = ex - BIAS - 23;		/* e0 = ilogb(|x|)-23; */
128	u1.xbits.expsign = ex - e0;
129	z = u1.e;
130	for(i=0;i<4;i++) {
131		tx[i] = (double)((int32_t)(z));
132		z     = (z-tx[i])*two24;
133	}
134	tx[4] = z;
135	nx = 5;
136	while(tx[nx-1]==zero) nx--;	/* skip zero term */
137	n  =  __kernel_rem_pio2(tx,ty,e0,nx,3);
138	t = (long double)ty[2] + ty[1];
139	r = t + ty[0];
140	w = ty[0] - (r - t);
141	if(expsign<0) {y[0] = -r; y[1] = -w; return -n;}
142	y[0] = r; y[1] = w; return n;
143}
144