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