/* From: @(#)e_rem_pio2.c 1.4 95/01/18 */
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
* Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*
* Optimized by Bruce D. Evans.
*/
static inline __always_inline int
__ieee754_rem_pio2l(long double x, long double *y)
{
union ieee_ext_u u, u1;
long double z,w,t,r,fn;
double tx[3],ty[2];
int e0,ex,i,j,nx,n;
int16_t expsign, expsign1;
u.extu_ld = x;
ex = u.extu_exp;
expsign = GET_EXPSIGN(&u);
if (ex < BIAS + 25 || (ex == BIAS + 25 && u.extu_frach < 0xc90fdaa2U)) {
/* |x| ~< 2^25*(pi/2), medium size */
fn = rnintl(x*invpio2);
n = irint(fn);
r = x-fn*pio2_1;
w = fn*pio2_1t; /* 1st round good to 102 bit */
{
union ieee_ext_u u2;
int ex1;
j = ex;
y[0] = r-w;
u2.extu_ld = y[0];
ex1 = u2.extu_exp;
i = j-ex1;
if(i>22) { /* 2nd iteration needed, good to 141 */
t = r;
w = fn*pio2_2;
r = t-w;
w = fn*pio2_2t-((t-r)-w);
y[0] = r-w;
u2.extu_ld = y[0];
ex1 = u2.extu_exp;
i = j-ex1;
if(i>61) { /* 3rd iteration need, 180 bits acc */
t = r; /* will cover all possible cases */
w = fn*pio2_3;
r = t-w;
w = fn*pio2_3t-((t-r)-w);
y[0] = r-w;
}
}
}
y[1] = (r-y[0])-w;
return n;
}
/*
* all other (large) arguments
*/
if(ex==0x7fff) { /* x is inf or NaN */
y[0]=y[1]=x-x; return 0;
}
/* set z = scalbn(|x|,ilogb(x)-23) */
u1.extu_ld = x;
e0 = ex - BIAS - 23; /* e0 = ilogb(|x|)-23; */
expsign1 = ex - e0;
SET_EXPSIGN(&u1, expsign1);
z = u1.extu_ld;
for(i=0;i<2;i++) {
tx[i] = (double)((int32_t)(z));
z = (z-tx[i])*two24;
}
tx[2] = z;
nx = 3;
while(tx[nx-1]==zero) nx--; /* skip zero term */
n = __kernel_rem_pio2(tx,ty,e0,nx,2);
r = (long double)ty[0] + ty[1];
w = ty[1] - (r - ty[0]);
if(expsign<0) {y[0] = -r; y[1] = -w; return -n;}
y[0] = r; y[1] = w; return n;
}