e_rem_pio2l.h revision 222508
152419Sjulian/* From: @(#)e_rem_pio2.c 1.4 95/01/18 */ 252419Sjulian/* 379727Sschweikh * ==================================================== 452419Sjulian * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 552419Sjulian * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans. 652419Sjulian * 752419Sjulian * Developed at SunSoft, a Sun Microsystems, Inc. business. 852419Sjulian * Permission to use, copy, modify, and distribute this 952419Sjulian * software is freely granted, provided that this notice 1052419Sjulian * is preserved. 1152419Sjulian * ==================================================== 1252419Sjulian * 1352419Sjulian * Optimized by Bruce D. Evans. 1479727Sschweikh */ 1552419Sjulian 1652419Sjulian#include <sys/cdefs.h> 1752419Sjulian__FBSDID("$FreeBSD: head/lib/msun/ld80/e_rem_pio2l.h 222508 2011-05-30 19:41:28Z kargl $"); 1852419Sjulian 1952419Sjulian/* ld80 version of __ieee754_rem_pio2l(x,y) 2052419Sjulian * 2152419Sjulian * return the remainder of x rem pi/2 in y[0]+y[1] 2252419Sjulian * use __kernel_rem_pio2() 2352419Sjulian */ 2452419Sjulian 2552419Sjulian#include <float.h> 2652419Sjulian 2752419Sjulian#include "math.h" 2852419Sjulian#include "math_private.h" 2952419Sjulian#include "fpmath.h" 3052419Sjulian 3152419Sjulian#define BIAS (LDBL_MAX_EXP - 1) 3279727Sschweikh 3367627Sasmodai/* 3452419Sjulian * invpio2: 64 bits of 2/pi 3552419Sjulian * pio2_1: first 39 bits of pi/2 3652419Sjulian * pio2_1t: pi/2 - pio2_1 3752419Sjulian * pio2_2: second 39 bits of pi/2 38151815Sru * pio2_2t: pi/2 - (pio2_1+pio2_2) 3959982Sarchie * pio2_3: third 39 bits of pi/2 4079538Sru * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) 4152419Sjulian */ 4252419Sjulian 4352419Sjulianstatic const double 4452419Sjulianzero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */ 4584306Srutwo24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ 4652419Sjulianpio2_1 = 1.57079632679597125389e+00, /* 0x3FF921FB, 0x54444000 */ 4752419Sjulianpio2_2 = -1.07463465549783099519e-12, /* -0x12e7b967674000.0p-92 */ 4852419Sjulianpio2_3 = 6.36831716351370313614e-25; /* 0x18a2e037074000.0p-133 */ 49131530Sru 50131530Sru#if defined(__amd64__) || defined(__i386__) 5158016Sarchie/* Long double constants are slow on these arches, and broken on i386. */ 5258016Sarchiestatic const volatile double 5352419Sjulianinvpio2hi = 6.3661977236758138e-01, /* 0x145f306dc9c883.0p-53 */ 5458016Sarchieinvpio2lo = -3.9356538861223811e-17, /* -0x16b00000000000.0p-107 */ 5558016Sarchiepio2_1thi = -1.0746346554971943e-12, /* -0x12e7b9676733af.0p-92 */ 5652419Sjulianpio2_1tlo = 8.8451028997905949e-29, /* 0x1c080000000000.0p-146 */ 5752419Sjulianpio2_2thi = 6.3683171635109499e-25, /* 0x18a2e03707344a.0p-133 */ 5858016Sarchiepio2_2tlo = 2.3183081793789774e-41, /* 0x10280000000000.0p-187 */ 5958016Sarchiepio2_3thi = -2.7529965190440717e-37, /* -0x176b7ed8fbbacc.0p-174 */ 6058016Sarchiepio2_3tlo = -4.2006647512740502e-54; /* -0x19c00000000000.0p-230 */ 6158016Sarchie#define invpio2 ((long double)invpio2hi + invpio2lo) 6258016Sarchie#define pio2_1t ((long double)pio2_1thi + pio2_1tlo) 6358016Sarchie#define pio2_2t ((long double)pio2_2thi + pio2_2tlo) 6452419Sjulian#define pio2_3t ((long double)pio2_3thi + pio2_3tlo) 6552419Sjulian#else 6654927Sjulianstatic const long double 6758016Sarchieinvpio2 = 6.36619772367581343076e-01L, /* 0xa2f9836e4e44152a.0p-64 */ 6854927Sjulianpio2_1t = -1.07463465549719416346e-12L, /* -0x973dcb3b399d747f.0p-103 */ 6952419Sjulianpio2_2t = 6.36831716351095013979e-25L, /* 0xc51701b839a25205.0p-144 */ 7052419Sjulianpio2_3t = -2.75299651904407171810e-37L; /* -0xbb5bf6c7ddd660ce.0p-185 */ 7179727Sschweikh#endif 7258016Sarchie 7358016Sarchiestatic inline __always_inline int 7458016Sarchie__ieee754_rem_pio2l(long double x, long double *y) 7552419Sjulian{ 7658016Sarchie union IEEEl2bits u,u1; 7758016Sarchie long double z,w,t,r,fn; 78123379Sru double tx[3],ty[2]; 7958016Sarchie int e0,ex,i,j,nx,n; 8058016Sarchie int16_t expsign; 8152419Sjulian 8258016Sarchie u.e = x; 8379727Sschweikh expsign = u.xbits.expsign; 8452419Sjulian ex = expsign & 0x7fff; 8552419Sjulian if (ex < BIAS + 25 || ex == BIAS + 25 && u.bits.manh < 0xc90fdaa2) { 86242997Sjoel /* |x| ~< 2^25*(pi/2), medium size */ 87242997Sjoel /* Use a specialized rint() to get fn. Assume round-to-nearest. */ 8852419Sjulian fn = x*invpio2+0x1.8p63; 89242997Sjoel fn = fn-0x1.8p63; 9058016Sarchie#ifdef HAVE_EFFICIENT_IRINT 91242997Sjoel n = irint(fn); 9258016Sarchie#else 93242997Sjoel n = fn; 9452419Sjulian#endif 95242997Sjoel r = x-fn*pio2_1; 9658016Sarchie w = fn*pio2_1t; /* 1st round good to 102 bit */ 97242997Sjoel { 9858016Sarchie union IEEEl2bits u2; 99242997Sjoel int ex1; 10052419Sjulian j = ex; 10152419Sjulian y[0] = r-w; 10252419Sjulian u2.e = y[0]; 10352419Sjulian ex1 = u2.xbits.expsign & 0x7fff; 10452419Sjulian i = j-ex1; 105242997Sjoel if(i>22) { /* 2nd iteration needed, good to 141 */ 106141201Sru t = r; 107141201Sru w = fn*pio2_2; 108141201Sru r = t-w; 109141201Sru w = fn*pio2_2t-((t-r)-w); 110141201Sru y[0] = r-w; 111242997Sjoel u2.e = y[0]; 112151815Sru ex1 = u2.xbits.expsign & 0x7fff; 113242997Sjoel i = j-ex1; 11458016Sarchie if(i>61) { /* 3rd iteration need, 180 bits acc */ 11558016Sarchie t = r; /* will cover all possible cases */ 116242997Sjoel w = fn*pio2_3; 11758016Sarchie r = t-w; 11858016Sarchie w = fn*pio2_3t-((t-r)-w); 119242997Sjoel y[0] = r-w; 12052419Sjulian } 12159982Sarchie } 12252419Sjulian } 12359982Sarchie y[1] = (r-y[0])-w; 12452419Sjulian return n; 12552419Sjulian } 12652419Sjulian /* 12758016Sarchie * all other (large) arguments 12852419Sjulian */ 129131530Sru if(ex==0x7fff) { /* x is inf or NaN */ 130131530Sru y[0]=y[1]=x-x; return 0; 13158016Sarchie } 13258016Sarchie /* set z = scalbn(|x|,ilogb(x)-23) */ 13358016Sarchie u1.e = x; 13458016Sarchie e0 = ex - BIAS - 23; /* e0 = ilogb(|x|)-23; */ 13558016Sarchie u1.xbits.expsign = ex - e0; 13658016Sarchie z = u1.e; 13758016Sarchie for(i=0;i<2;i++) { 13858016Sarchie tx[i] = (double)((int32_t)(z)); 13958016Sarchie z = (z-tx[i])*two24; 14058016Sarchie } 14158016Sarchie tx[2] = z; 142151204Sglebius nx = 3; 143151204Sglebius while(tx[nx-1]==zero) nx--; /* skip zero term */ 144151204Sglebius n = __kernel_rem_pio2(tx,ty,e0,nx,2); 145151204Sglebius r = (long double)ty[0] + ty[1]; 146151204Sglebius w = ty[1] - (r - ty[0]); 147151204Sglebius if(expsign<0) {y[0] = -r; y[1] = -w; return -n;} 148151204Sglebius y[0] = r; y[1] = w; return n; 149151204Sglebius} 150151204Sglebius