xref: /original-bsd/lib/libc/quad/floatdidf.c (revision 68d9582f)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * 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	5.3 (Berkeley) 06/02/92";
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(quad x)
23 {
24 	double d;
25 	union uu u;
26 	int neg;
27 
28 	/*
29 	 * Get an unsigned number first, by negating if necessary.
30 	 */
31 	if (x < 0)
32 		u.q = -x, neg = 1;
33 	else
34 		u.q = x, neg = 0;
35 
36 	/*
37 	 * Now u.ul[H] has the factor of 2^32 (or whatever) and u.ul[L]
38 	 * has the units.  Ideally we could just set d, add LONG_BITS to
39 	 * its exponent, and then add the units, but this is portable
40 	 * code and does not know how to get at an exponent.  Machine-
41 	 * specific code may be able to do this more efficiently.
42 	 */
43 	d = (double)u.ul[H] * ((1 << (LONG_BITS - 2)) * 4.0);
44 	d += u.ul[L];
45 
46 	return (neg ? -d : d);
47 }
48