xref: /minix3/lib/libm/src/s_frexp.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /* @(#)s_frexp.c 5.1 93/09/24 */
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras  * ====================================================
4*2fe8fb19SBen Gras  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Developed at SunPro, a Sun Microsystems, Inc. business.
7*2fe8fb19SBen Gras  * Permission to use, copy, modify, and distribute this
8*2fe8fb19SBen Gras  * software is freely granted, provided that this notice
9*2fe8fb19SBen Gras  * is preserved.
10*2fe8fb19SBen Gras  * ====================================================
11*2fe8fb19SBen Gras  */
12*2fe8fb19SBen Gras 
13*2fe8fb19SBen Gras #include <sys/cdefs.h>
14*2fe8fb19SBen Gras #if defined(LIBM_SCCS) && !defined(lint)
15*2fe8fb19SBen Gras __RCSID("$NetBSD: s_frexp.c,v 1.13 2008/09/28 18:54:55 christos Exp $");
16*2fe8fb19SBen Gras #endif
17*2fe8fb19SBen Gras 
18*2fe8fb19SBen Gras /*
19*2fe8fb19SBen Gras  * for non-zero x
20*2fe8fb19SBen Gras  *	x = frexp(arg,&exp);
21*2fe8fb19SBen Gras  * return a double fp quantity x such that 0.5 <= |x| <1.0
22*2fe8fb19SBen Gras  * and the corresponding binary exponent "exp". That is
23*2fe8fb19SBen Gras  *	arg = x*2^exp.
24*2fe8fb19SBen Gras  * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
25*2fe8fb19SBen Gras  * with *exp=0.
26*2fe8fb19SBen Gras  */
27*2fe8fb19SBen Gras 
28*2fe8fb19SBen Gras #include "math.h"
29*2fe8fb19SBen Gras #include "math_private.h"
30*2fe8fb19SBen Gras 
31*2fe8fb19SBen Gras static const double
32*2fe8fb19SBen Gras two54 =  1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
33*2fe8fb19SBen Gras 
34*2fe8fb19SBen Gras double
frexp(double x,int * eptr)35*2fe8fb19SBen Gras frexp(double x, int *eptr)
36*2fe8fb19SBen Gras {
37*2fe8fb19SBen Gras 	int32_t hx, ix, lx;
38*2fe8fb19SBen Gras 	EXTRACT_WORDS(hx,lx,x);
39*2fe8fb19SBen Gras 	ix = 0x7fffffff&hx;
40*2fe8fb19SBen Gras 	*eptr = 0;
41*2fe8fb19SBen Gras 	if(ix>=0x7ff00000||((ix|lx)==0)) return x;	/* 0,inf,nan */
42*2fe8fb19SBen Gras 	if (ix<0x00100000) {		/* subnormal */
43*2fe8fb19SBen Gras 	    x *= two54;
44*2fe8fb19SBen Gras 	    GET_HIGH_WORD(hx,x);
45*2fe8fb19SBen Gras 	    ix = hx&0x7fffffff;
46*2fe8fb19SBen Gras 	    *eptr = -54;
47*2fe8fb19SBen Gras 	}
48*2fe8fb19SBen Gras 	*eptr += ((uint32_t)ix>>20)-1022;
49*2fe8fb19SBen Gras 	hx = (hx&0x800fffff)|0x3fe00000;
50*2fe8fb19SBen Gras 	SET_HIGH_WORD(x,hx);
51*2fe8fb19SBen Gras 	return x;
52*2fe8fb19SBen Gras }
53