xref: /original-bsd/lib/libc/quad/floatdidf.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * %sccs.include.redist.c%
10  */
11 
12 #if defined(LIBC_SCCS) && !defined(lint)
13 static char sccsid[] = "@(#)floatdidf.c	8.1 (Berkeley) 06/04/93";
14 #endif /* LIBC_SCCS and not lint */
15 
16 #include "quad.h"
17 
18 /*
19  * Convert (signed) quad to double.
20  */
21 double
22 __floatdidf(x)
23 	quad_t x;
24 {
25 	double d;
26 	union uu u;
27 	int neg;
28 
29 	/*
30 	 * Get an unsigned number first, by negating if necessary.
31 	 */
32 	if (x < 0)
33 		u.q = -x, neg = 1;
34 	else
35 		u.q = x, neg = 0;
36 
37 	/*
38 	 * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L]
39 	 * has the units.  Ideally we could just set d, add LONG_BITS to
40 	 * its exponent, and then add the units, but this is portable
41 	 * code and does not know how to get at an exponent.  Machine-
42 	 * specific code may be able to do this more efficiently.
43 	 */
44 	d = (double)u.ul[H] * ((1 << (LONG_BITS - 2)) * 4.0);
45 	d += u.ul[L];
46 
47 	return (neg ? -d : d);
48 }
49