xref: /csrg-svn/lib/libc/quad/floatdidf.c (revision 53794)
153438Sbostic /*-
253438Sbostic  * Copyright (c) 1992 The Regents of the University of California.
353438Sbostic  * All rights reserved.
453438Sbostic  *
5*53794Sbostic  * This software was developed by the Computer Systems Engineering group
6*53794Sbostic  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7*53794Sbostic  * contributed to Berkeley.
8*53794Sbostic  *
953438Sbostic  * %sccs.include.redist.c%
1053438Sbostic  */
1153438Sbostic 
1253438Sbostic #if defined(LIBC_SCCS) && !defined(lint)
13*53794Sbostic static char sccsid[] = "@(#)floatdidf.c	5.3 (Berkeley) 06/02/92";
1453438Sbostic #endif /* LIBC_SCCS and not lint */
1553438Sbostic 
16*53794Sbostic #include "quad.h"
1753459Sbostic 
18*53794Sbostic /*
19*53794Sbostic  * Convert (signed) quad to double.
20*53794Sbostic  */
2153438Sbostic double
22*53794Sbostic __floatdidf(quad x)
2353438Sbostic {
24*53794Sbostic 	double d;
25*53794Sbostic 	union uu u;
26*53794Sbostic 	int neg;
2753438Sbostic 
28*53794Sbostic 	/*
29*53794Sbostic 	 * Get an unsigned number first, by negating if necessary.
30*53794Sbostic 	 */
31*53794Sbostic 	if (x < 0)
32*53794Sbostic 		u.q = -x, neg = 1;
33*53794Sbostic 	else
34*53794Sbostic 		u.q = x, neg = 0;
3553438Sbostic 
36*53794Sbostic 	/*
37*53794Sbostic 	 * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L]
38*53794Sbostic 	 * has the units.  Ideally we could just set d, add LONG_BITS to
39*53794Sbostic 	 * its exponent, and then add the units, but this is portable
40*53794Sbostic 	 * code and does not know how to get at an exponent.  Machine-
41*53794Sbostic 	 * specific code may be able to do this more efficiently.
42*53794Sbostic 	 */
43*53794Sbostic 	d = (double)u.ul[H] * ((1 << (LONG_BITS - 2)) * 4.0);
44*53794Sbostic 	d += u.ul[L];
4553438Sbostic 
46*53794Sbostic 	return (neg ? -d : d);
4753438Sbostic }
48