xref: /original-bsd/lib/libc/quad/fixunssfdi.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[] = "@(#)fixunssfdi.c	8.1 (Berkeley) 06/04/93";
14 #endif /* LIBC_SCCS and not lint */
15 
16 #include "quad.h"
17 
18 #define	ONE_FOURTH	(1 << (LONG_BITS - 2))
19 #define	ONE_HALF	(ONE_FOURTH * 2.0)
20 #define	ONE		(ONE_FOURTH * 4.0)
21 
22 /*
23  * Convert float to (unsigned) quad.  We do most of our work in double,
24  * out of sheer paranoia.
25  *
26  * Not sure what to do with negative numbers---for now, anything out
27  * of range becomes UQUAD_MAX.
28  *
29  * N.B.: must use new ANSI syntax (sorry).
30  */
31 u_quad_t
32 __fixunssfdi(float f)
33 {
34 	double x, toppart;
35 	union uu t;
36 
37 	if (f < 0)
38 		return (UQUAD_MAX);	/* ??? should be 0?  ERANGE??? */
39 #ifdef notdef				/* this falls afoul of a GCC bug */
40 	if (f >= UQUAD_MAX)
41 		return (UQUAD_MAX);
42 #else					/* so we wire in 2^64-1 instead */
43 	if (f >= 18446744073709551615.0)
44 		return (UQUAD_MAX);
45 #endif
46 	x = f;
47 	/*
48 	 * Get the upper part of the result.  Note that the divide
49 	 * may round up; we want to avoid this if possible, so we
50 	 * subtract `1/2' first.
51 	 */
52 	toppart = (x - ONE_HALF) / ONE;
53 	/*
54 	 * Now build a u_quad_t out of the top part.  The difference
55 	 * between x and this is the bottom part (this may introduce
56 	 * a few fuzzy bits, but what the heck).  With any luck this
57 	 * difference will be nonnegative: x should wind up in the
58 	 * range [0..ULONG_MAX].  For paranoia, we assume [LONG_MIN..
59 	 * 2*ULONG_MAX] instead.
60 	 */
61 	t.ul[H] = (unsigned long)toppart;
62 	t.ul[L] = 0;
63 	x -= (double)t.uq;
64 	if (x < 0) {
65 		t.ul[H]--;
66 		x += ULONG_MAX;
67 	}
68 	if (x > ULONG_MAX) {
69 		t.ul[H]++;
70 		x -= ULONG_MAX;
71 	}
72 	t.ul[L] = (u_long)x;
73 	return (t.uq);
74 }
75