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