e_rem_pio2l.h revision 176466
1141296Sdas 2141296Sdas/* @(#)e_rem_pio2.c 1.4 95/01/18 */ 32116Sjkh/* 42116Sjkh * ==================================================== 52116Sjkh * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 62116Sjkh * 7141296Sdas * Developed at SunSoft, a Sun Microsystems, Inc. business. 82116Sjkh * Permission to use, copy, modify, and distribute this 9141296Sdas * software is freely granted, provided that this notice 102116Sjkh * is preserved. 112116Sjkh * ==================================================== 12141296Sdas * 132116Sjkh */ 142116Sjkh 15176385Sbde#include <sys/cdefs.h> 16176385Sbde__FBSDID("$FreeBSD: head/lib/msun/src/e_rem_pio2.c 176466 2008-02-22 17:26:24Z bde $"); 172116Sjkh 182116Sjkh/* __ieee754_rem_pio2(x,y) 19141296Sdas * 20141296Sdas * return the remainder of x rem pi/2 in y[0]+y[1] 212116Sjkh * use __kernel_rem_pio2() 222116Sjkh */ 232116Sjkh 24176465Sbde#include <float.h> 25176465Sbde 262116Sjkh#include "math.h" 272116Sjkh#include "math_private.h" 282116Sjkh 292116Sjkh/* 302116Sjkh * invpio2: 53 bits of 2/pi 312116Sjkh * pio2_1: first 33 bit of pi/2 322116Sjkh * pio2_1t: pi/2 - pio2_1 332116Sjkh * pio2_2: second 33 bit of pi/2 342116Sjkh * pio2_2t: pi/2 - (pio2_1+pio2_2) 352116Sjkh * pio2_3: third 33 bit of pi/2 362116Sjkh * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) 372116Sjkh */ 382116Sjkh 398870Srgrimesstatic const double 402116Sjkhzero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ 412116Sjkhhalf = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ 422116Sjkhtwo24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ 432116Sjkhinvpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */ 442116Sjkhpio2_1 = 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */ 452116Sjkhpio2_1t = 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */ 462116Sjkhpio2_2 = 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */ 472116Sjkhpio2_2t = 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */ 482116Sjkhpio2_3 = 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */ 492116Sjkhpio2_3t = 8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */ 502116Sjkh 51176385Sbde#ifdef INLINE_REM_PIO2 52176385Sbdeextern inline 53176385Sbde#endif 54176385Sbdeint 55176385Sbde__ieee754_rem_pio2(double x, double *y) 562116Sjkh{ 572116Sjkh double z,w,t,r,fn; 582116Sjkh double tx[3]; 592116Sjkh int32_t e0,i,j,nx,n,ix,hx; 602116Sjkh u_int32_t low; 612116Sjkh 622116Sjkh GET_HIGH_WORD(hx,x); /* high word of x */ 632116Sjkh ix = hx&0x7fffffff; 64176409Sbde#if 0 /* Must be handled in caller. */ 652116Sjkh if(ix<=0x3fe921fb) /* |x| ~<= pi/4 , no need for reduction */ 662116Sjkh {y[0] = x; y[1] = 0; return 0;} 67176409Sbde#endif 68176409Sbde if (ix <= 0x400f6a7a) { /* |x| ~<= 5pi/4 */ 69176409Sbde if ((ix & 0xfffff) == 0x921fb) /* |x| ~= pi/2 or 2pi/2 */ 70176409Sbde goto medium; /* cancellation -- use medium case */ 71176409Sbde if (ix <= 0x4002d97c) { /* |x| ~<= 3pi/4 */ 72176409Sbde if (hx > 0) { 73176409Sbde z = x - pio2_1; /* one round good to 85 bits */ 747659Sbde y[0] = z - pio2_1t; 757659Sbde y[1] = (z-y[0])-pio2_1t; 76176409Sbde return 1; 77176409Sbde } else { 78176409Sbde z = x + pio2_1; 797659Sbde y[0] = z + pio2_1t; 807659Sbde y[1] = (z-y[0])+pio2_1t; 81176409Sbde return -1; 827659Sbde } 83176409Sbde } else { 84176409Sbde if (hx > 0) { 85176409Sbde z = x - 2*pio2_1; 86176409Sbde y[0] = z - 2*pio2_1t; 87176409Sbde y[1] = (z-y[0])-2*pio2_1t; 88176409Sbde return 2; 89176409Sbde } else { 90176409Sbde z = x + 2*pio2_1; 91176409Sbde y[0] = z + 2*pio2_1t; 92176409Sbde y[1] = (z-y[0])+2*pio2_1t; 93176409Sbde return -2; 94176409Sbde } 957659Sbde } 967659Sbde } 97176409Sbde if (ix <= 0x401c463b) { /* |x| ~<= 9pi/4 */ 98176409Sbde if (ix <= 0x4015fdbc) { /* |x| ~<= 7pi/4 */ 99176409Sbde if (ix == 0x4012d97c) /* |x| ~= 3pi/2 */ 100176409Sbde goto medium; 101176409Sbde if (hx > 0) { 102176409Sbde z = x - 3*pio2_1; 103176409Sbde y[0] = z - 3*pio2_1t; 104176409Sbde y[1] = (z-y[0])-3*pio2_1t; 105176409Sbde return 3; 106176409Sbde } else { 107176409Sbde z = x + 3*pio2_1; 108176409Sbde y[0] = z + 3*pio2_1t; 109176409Sbde y[1] = (z-y[0])+3*pio2_1t; 110176409Sbde return -3; 111176409Sbde } 112176409Sbde } else { 113176409Sbde if (ix == 0x401921fb) /* |x| ~= 4pi/2 */ 114176409Sbde goto medium; 115176409Sbde if (hx > 0) { 116176409Sbde z = x - 4*pio2_1; 117176409Sbde y[0] = z - 4*pio2_1t; 118176409Sbde y[1] = (z-y[0])-4*pio2_1t; 119176409Sbde return 4; 120176409Sbde } else { 121176409Sbde z = x + 4*pio2_1; 122176409Sbde y[0] = z + 4*pio2_1t; 123176409Sbde y[1] = (z-y[0])+4*pio2_1t; 124176409Sbde return -4; 125176409Sbde } 126176409Sbde } 127176409Sbde } 1282116Sjkh if(ix<=0x413921fb) { /* |x| ~<= 2^19*(pi/2), medium size */ 129176409Sbdemedium: 1302116Sjkh t = fabs(x); 131176465Sbde#ifdef HAVE_EFFICIENT_IRINT 132176465Sbde /* Use a specialized rint() to get fn. Assume round-to-nearest. */ 133176465Sbde STRICT_ASSIGN(double,fn,t*invpio2+0x1.8p52); 134176465Sbde fn = fn-0x1.8p52; 135176465Sbde n = irint(fn); 136176465Sbde#else 1372116Sjkh n = (int32_t) (t*invpio2+half); 1382116Sjkh fn = (double)n; 139176465Sbde#endif 1402116Sjkh r = t-fn*pio2_1; 1412116Sjkh w = fn*pio2_1t; /* 1st round good to 85 bit */ 142176466Sbde { 1432116Sjkh u_int32_t high; 1442116Sjkh j = ix>>20; 145141296Sdas y[0] = r-w; 1462116Sjkh GET_HIGH_WORD(high,y[0]); 1472116Sjkh i = j-((high>>20)&0x7ff); 1482116Sjkh if(i>16) { /* 2nd iteration needed, good to 118 */ 1492116Sjkh t = r; 150141296Sdas w = fn*pio2_2; 1512116Sjkh r = t-w; 152141296Sdas w = fn*pio2_2t-((t-r)-w); 1532116Sjkh y[0] = r-w; 1542116Sjkh GET_HIGH_WORD(high,y[0]); 1552116Sjkh i = j-((high>>20)&0x7ff); 1562116Sjkh if(i>49) { /* 3rd iteration need, 151 bits acc */ 1572116Sjkh t = r; /* will cover all possible cases */ 158141296Sdas w = fn*pio2_3; 1592116Sjkh r = t-w; 160141296Sdas w = fn*pio2_3t-((t-r)-w); 1612116Sjkh y[0] = r-w; 1622116Sjkh } 1632116Sjkh } 1642116Sjkh } 1652116Sjkh y[1] = (r-y[0])-w; 1662116Sjkh if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} 1672116Sjkh else return n; 1682116Sjkh } 169141296Sdas /* 1702116Sjkh * all other (large) arguments 1712116Sjkh */ 1722116Sjkh if(ix>=0x7ff00000) { /* x is inf or NaN */ 1732116Sjkh y[0]=y[1]=x-x; return 0; 1742116Sjkh } 1752116Sjkh /* set z = scalbn(|x|,ilogb(x)-23) */ 1762116Sjkh GET_LOW_WORD(low,x); 1772116Sjkh SET_LOW_WORD(z,low); 1782116Sjkh e0 = (ix>>20)-1046; /* e0 = ilogb(z)-23; */ 1792116Sjkh SET_HIGH_WORD(z, ix - ((int32_t)(e0<<20))); 1802116Sjkh for(i=0;i<2;i++) { 1812116Sjkh tx[i] = (double)((int32_t)(z)); 1822116Sjkh z = (z-tx[i])*two24; 1832116Sjkh } 1842116Sjkh tx[2] = z; 1852116Sjkh nx = 3; 1862116Sjkh while(tx[nx-1]==zero) nx--; /* skip zero term */ 187176356Sdas n = __kernel_rem_pio2(tx,y,e0,nx,2); 1882116Sjkh if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;} 1892116Sjkh return n; 1902116Sjkh} 191