xref: /freebsd/lib/msun/src/s_frexp.c (revision fd45b686)
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 /*
13  * for non-zero x
14  *	x = frexp(arg,&exp);
15  * return a double fp quantity x such that 0.5 <= |x| <1.0
16  * and the corresponding binary exponent "exp". That is
17  *	arg = x*2^exp.
18  * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg
19  * with *exp=0.
20  */
21 
22 #include <float.h>
23 
24 #include "math.h"
25 #include "math_private.h"
26 
27 static const double
28 two54 =  1.80143985094819840000e+16; /* 0x43500000, 0x00000000 */
29 
30 double
31 frexp(double x, int *eptr)
32 {
33 	int32_t hx, ix, lx;
34 	EXTRACT_WORDS(hx,lx,x);
35 	ix = 0x7fffffff&hx;
36 	*eptr = 0;
37 	if(ix>=0x7ff00000||((ix|lx)==0)) return x;	/* 0,inf,nan */
38 	if (ix<0x00100000) {		/* subnormal */
39 	    x *= two54;
40 	    GET_HIGH_WORD(hx,x);
41 	    ix = hx&0x7fffffff;
42 	    *eptr = -54;
43 	}
44 	*eptr += (ix>>20)-1022;
45 	hx = (hx&0x800fffff)|0x3fe00000;
46 	SET_HIGH_WORD(x,hx);
47 	return x;
48 }
49 
50 #if (LDBL_MANT_DIG == 53)
51 __weak_reference(frexp, frexpl);
52 #endif
53