xref: /illumos-gate/usr/src/lib/libc/sparcv9/fp/_Qp_qtox.c (revision eb9a1df2aeb866bf1de4494433b6d7e5fa07b3ae)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994-1997, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "quad.h"
30 
31 /*
32  * _Qp_qtox(x) returns (long)*x.
33  */
34 long
35 _Qp_qtox(const union longdouble *x)
36 {
37 	long		i, round;
38 	unsigned int	xm, fsr;
39 
40 	xm = x->l.msw & 0x7fffffff;
41 
42 	__quad_getfsrp(&fsr);
43 
44 	/* handle nan, inf, and out-of-range cases */
45 	if (xm >= 0x403e0000) {
46 		if (x->l.msw == 0xc03e0000 && x->l.frac2 == 0 &&
47 		    (x->l.frac3 & 0xfffe0000) == 0) {
48 			/* return largest negative 64 bit int */
49 			i = 0x8000000000000000ul;
50 			if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
51 				/* signal inexact */
52 				if (fsr & FSR_NXM) {
53 					__quad_fqtox(x, &i);
54 				} else {
55 					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
56 					    FSR_NXC;
57 					__quad_setfsrp(&fsr);
58 				}
59 			}
60 			return (i);
61 		}
62 		i = ((x->l.msw & 0x80000000)? 0x8000000000000000ul :
63 		    0x7fffffffffffffffl);
64 		if (fsr & FSR_NVM) {
65 			__quad_fqtox(x, &i);
66 		} else {
67 			fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
68 			__quad_setfsrp(&fsr);
69 		}
70 		return (i);
71 	}
72 	if (xm < 0x3fff0000) {
73 		i = 0l;
74 		if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
75 			/* signal inexact */
76 			if (fsr & FSR_NXM) {
77 				__quad_fqtox(x, &i);
78 			} else {
79 				fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
80 				__quad_setfsrp(&fsr);
81 			}
82 		}
83 		return (i);
84 	}
85 
86 	/* now x is in the range of 64 bit int */
87 	i = 0x4000000000000000l | ((long) (xm & 0xffff) << 46) |
88 	    ((long) x->l.frac2 << 14) | (x->l.frac3 >> 18);
89 	round = i & ((1l << (0x403d - (xm >> 16))) - 1);
90 	i >>= (0x403d - (xm >> 16));
91 	if (x->l.msw & 0x80000000)
92 		i = -i;
93 	if (round | (x->l.frac3 & 0x3ffff) | x->l.frac4) {
94 		/* signal inexact */
95 		if (fsr & FSR_NXM) {
96 			__quad_fqtox(x, &i);
97 		} else {
98 			fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
99 			__quad_setfsrp(&fsr);
100 		}
101 	}
102 	return (i);
103 }
104