xref: /illumos-gate/usr/src/lib/libc/sparc/crt/_ftou.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include <sys/types.h>
29 #include "libc.h"
30 
31 unsigned
32 __dtou(double d)
33 {
34 	/* Convert double to unsigned. */
35 
36 	int id;
37 
38 	/*
39 	 * id = d is correct if 0 <= d < 2**31, and good enough if d is NaN
40 	 * or d < 0 or d >= 2**32.  Otherwise, since the result (int)d of
41 	 * converting 2**31 <= d < 2**32 is unknown, adjust d before the
42 	 * conversion.
43 	 */
44 
45 	if (d >= 2147483648.0)
46 		id = 0x80000000 | (int)(d - 2147483648.0);
47 	else
48 		id = (int)d;
49 	return ((unsigned)id);
50 }
51 
52 unsigned
53 __ftou(float d)
54 {
55 	/* Convert float to unsigned. */
56 
57 	int id;
58 	/*
59 	 * id = d is correct if 0 <= d < 2**31, and good enough if d is NaN
60 	 * or d < 0 or d >= 2**32.  Otherwise, since the result (int)d of
61 	 * converting 2**31 <= d < 2**32 is unknown, adjust d before the
62 	 * conversion.
63 	 */
64 
65 	if (d >= 2147483648.0)
66 		id = 0x80000000 | (int)(d - 2147483648.0);
67 	else
68 		id = (int)d;
69 	return ((unsigned)id);
70 }
71