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