xref: /csrg-svn/lib/libc/sparc/gen/frexp.c (revision 61168)
154386Storek /*
2*61168Sbostic  * Copyright (c) 1992, 1993
3*61168Sbostic  *	The Regents of the University of California.  All rights reserved.
454386Storek  *
554386Storek  * This software was developed by the Computer Systems Engineering group
654386Storek  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
754386Storek  * contributed to Berkeley.
854386Storek  *
954386Storek  * %sccs.include.redist.c%
1054386Storek  *
1154386Storek  * from: $Header: frexp.c,v 1.1 91/07/07 04:45:01 torek Exp $
1254386Storek  */
1354386Storek 
1454386Storek #if defined(LIBC_SCCS) && !defined(lint)
15*61168Sbostic static char sccsid[] = "@(#)frexp.c	8.1 (Berkeley) 06/04/93";
1654386Storek #endif /* LIBC_SCCS and not lint */
1754386Storek 
1854386Storek #include <sys/types.h>
1954386Storek #include <machine/ieee.h>
2054386Storek 
2154386Storek /*
2254386Storek  * Split the given value into a fraction in the range [0.5, 1.0) and
2354386Storek  * an exponent, such that frac * (2^exp) == value.  If value is 0,
2454386Storek  * return 0.
2554386Storek  */
2654386Storek double
frexp(value,eptr)2754386Storek frexp(value, eptr)
2854386Storek 	double value;
2954386Storek 	int *eptr;
3054386Storek {
3154386Storek 	union {
3254386Storek                 double v;
3354386Storek 		struct ieee_double s;
3454386Storek 	} u;
3554386Storek 
3654386Storek 	if (value) {
3754386Storek 		/*
3854386Storek 		 * Fractions in [0.5..1.0) have an exponent of 2^-1.
3954386Storek 		 * Leave Inf and NaN alone, however.
4054386Storek 		 * WHAT ABOUT DENORMS?
4154386Storek 		 */
4254386Storek 		u.v = value;
4354386Storek 		if (u.s.dbl_exp != DBL_EXP_INFNAN) {
4454386Storek 			*eptr = u.s.dbl_exp - (DBL_EXP_BIAS - 1);
4554386Storek 			u.s.dbl_exp = DBL_EXP_BIAS - 1;
4654386Storek 		}
4754386Storek 		return (u.v);
4854386Storek 	} else {
4954386Storek 		*eptr = 0;
5054386Storek 		return ((double)0);
5154386Storek 	}
5254386Storek }
53