xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_qtoi.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 #include "quad.h"
28 
29 #ifdef __sparcv9
30 #define	_Q_qtoi	_Qp_qtoi
31 #endif
32 
33 /*
34  * _Q_qtoi(x) returns (int)*x.
35  */
36 int
37 _Q_qtoi(const union longdouble *x)
38 {
39 	unsigned int	xm, fsr;
40 	int		i, round;
41 
42 	xm = x->l.msw & 0x7fffffff;
43 
44 	__quad_getfsrp(&fsr);
45 
46 	/* handle nan, inf, and out-of-range cases */
47 	if (xm >= 0x401e0000) {
48 		if (x->l.msw == 0xc01e0000 && (x->l.frac2 & 0xfffe0000) == 0) {
49 			/* return largest negative int */
50 			i = 0x80000000;
51 			if ((x->l.frac2 & 0x1ffff) | x->l.frac3 | x->l.frac4) {
52 				/* signal inexact */
53 				if (fsr & FSR_NXM) {
54 					__quad_fqtoi(x, &i);
55 				} else {
56 					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
57 					    FSR_NXC;
58 					__quad_setfsrp(&fsr);
59 				}
60 			}
61 			return (i);
62 		}
63 		i = ((x->l.msw & 0x80000000)? 0x80000000 : 0x7fffffff);
64 		if (fsr & FSR_NVM) {
65 			__quad_fqtoi(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 = 0;
74 		if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
75 			/* signal inexact */
76 			if (fsr & FSR_NXM) {
77 				__quad_fqtoi(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 int */
87 	i = (int) (0x40000000 | ((xm & 0xffff) << 14) | (x->l.frac2 >> 18));
88 	round = i & ((1 << (0x401d - (xm >> 16))) - 1);
89 	i >>= (0x401d - (xm >> 16));
90 	if (x->l.msw & 0x80000000)
91 		i = -i;
92 	if (round | (x->l.frac2 & 0x3ffff) | x->l.frac3 | x->l.frac4) {
93 		/* signal inexact */
94 		if (fsr & FSR_NXM) {
95 			__quad_fqtoi(x, &i);
96 		} else {
97 			fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
98 			__quad_setfsrp(&fsr);
99 		}
100 	}
101 	return (i);
102 }
103