xref: /original-bsd/lib/libc/quad/fixdfdi.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[] = "@(#)fixdfdi.c	5.3 (Berkeley) 06/02/92";
14 #endif /* LIBC_SCCS and not lint */
15 
16 #include "quad.h"
17 
18 #ifndef QUAD_MAX	/* should be in <limits.h> maybe? */
19 #define	QUAD_MAX ((quad)(((u_quad)1 << (QUAD_BITS - 1)) - 1))
20 #define	QUAD_MIN (-QUAD_MAX - 1)
21 #endif
22 
23 /*
24  * Convert double to (signed) quad.
25  * We clamp anything that is out of range.
26  */
27 quad
28 __fixdfdi(double x)
29 {
30 	if (x < 0)
31 		if (x <= QUAD_MIN)
32 			return (QUAD_MIN);
33 		else
34 			return ((quad)-(u_quad)-x);
35 	else
36 		if (x >= QUAD_MAX)
37 			return (QUAD_MAX);
38 		else
39 			return ((quad)(u_quad)x);
40 }
41