e_rem_pio2f.c revision 176558
181464Sbrian/* e_rem_pio2f.c -- float version of e_rem_pio2.c
281464Sbrian * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
381464Sbrian * Debugged and optimized by Bruce D. Evans.
481464Sbrian */
581464Sbrian
629841Sbrian/*
729841Sbrian * ====================================================
881464Sbrian * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
981464Sbrian *
1081464Sbrian * Developed at SunPro, a Sun Microsystems, Inc. business.
1181464Sbrian * Permission to use, copy, modify, and distribute this
1281464Sbrian * software is freely granted, provided that this notice
1381464Sbrian * is preserved.
1481464Sbrian * ====================================================
1581464Sbrian */
1629841Sbrian
1781464Sbrian#include <sys/cdefs.h>
1881464Sbrian__FBSDID("$FreeBSD: head/lib/msun/src/e_rem_pio2f.c 176558 2008-02-25 18:28:58Z bde $");
1981464Sbrian
2081464Sbrian/* __ieee754_rem_pio2f(x,y)
2181464Sbrian *
2281464Sbrian * return the remainder of x rem pi/2 in *y
2381464Sbrian * use double precision for everything except passing x
2481464Sbrian * use __kernel_rem_pio2() for large x
2581464Sbrian */
2681464Sbrian
2781464Sbrian#include <float.h>
2830715Sbrian
2950479Speter#include "math.h"
3029841Sbrian#include "math_private.h"
3129841Sbrian
3244106Sbrian/*
3358040Sbrian * invpio2:  53 bits of 2/pi
3457451Smarkm * pio2_1:   first  33 bit of pi/2
3568344Sbrian * pio2_1t:  pi/2 - pio2_1
3658040Sbrian */
3775212Sbrian
3868344Sbrianstatic const double
3996387Sbrianhalf =  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
4096387Sbrianinvpio2 =  6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */
4196387Sbrianpio2_1  =  1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
4258040Sbrianpio2_1t =  6.07710050650619224932e-11; /* 0x3DD0B461, 0x1A626331 */
4396387Sbrian
4468344Sbrianint
4558040Sbrian__ieee754_rem_pio2f(float x, double *y)
4667910Sbrian{
4730715Sbrian	double w,r,fn;
4829841Sbrian	double tx[1],ty[1];
4929841Sbrian	float z;
5029841Sbrian	int32_t e0,n,ix,hx;
5167910Sbrian
5267910Sbrian	GET_FLOAT_WORD(hx,x);
5367910Sbrian	ix = hx&0x7fffffff;
5467910Sbrian    /* 33+53 bit pi is good enough for medium size */
5567910Sbrian	if(ix<=0x49490f80) {		/* |x| ~<= 2^19*(pi/2), medium size */
5667910Sbrian	    /* Use a specialized rint() to get fn.  Assume round-to-nearest. */
5767910Sbrian	    STRICT_ASSIGN(double,fn,x*invpio2+0x1.8p52);
5867910Sbrian	    fn = fn-0x1.8p52;
5967910Sbrian#ifdef HAVE_EFFICIENT_IRINT
6067910Sbrian	    n  = irint(fn);
6167910Sbrian#else
6267910Sbrian	    n  = (int32_t)fn;
6367910Sbrian#endif
6467910Sbrian	    r  = x-fn*pio2_1;
6567910Sbrian	    w  = fn*pio2_1t;
6667910Sbrian	    *y = r-w;
6767910Sbrian	    return n;
6867910Sbrian	}
6967910Sbrian    /*
7067910Sbrian     * all other (large) arguments
7129841Sbrian     */
7229841Sbrian	if(ix>=0x7f800000) {		/* x is inf or NaN */
7336285Sbrian	    *y=x-x; return 0;
7429841Sbrian	}
7529841Sbrian    /* set z = scalbn(|x|,ilogb(|x|)-23) */
7629841Sbrian	e0 = (ix>>23)-150;		/* e0 = ilogb(|x|)-23; */
7736285Sbrian	SET_FLOAT_WORD(z, ix - ((int32_t)(e0<<23)));
7829841Sbrian	tx[0] = z;
7967912Sbrian	n  =  __kernel_rem_pio2(tx,ty,e0,1,0);
8067912Sbrian	if(hx<0) {*y = -ty[0]; return -n;}
8129841Sbrian	*y = ty[0]; return n;
8229841Sbrian}
8329841Sbrian