xref: /titanic_44/usr/src/lib/libc/sparc/fp/_Q_qtou.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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_qtou	_Qp_qtoui
33 #endif
34 
35 /*
36  * _Q_qtou(x) returns (unsigned int)*x.
37  */
38 unsigned
_Q_qtou(const union longdouble * x)39 _Q_qtou(const union longdouble *x)
40 {
41 	union longdouble	z;
42 	unsigned int		xm, fsr;
43 	int			i, round;
44 
45 	xm = x->l.msw & 0x7fffffff;
46 
47 	__quad_getfsrp(&fsr);
48 
49 	/* handle nan, inf, and out-of-range cases */
50 	if (xm >= 0x401e0000) {
51 		if (x->l.msw < 0x401f0000) {
52 			i = 0x80000000 | ((xm & 0xffff) << 15) |
53 			    (x->l.frac2 >> 17);
54 			if ((x->l.frac2 & 0x1ffff) | x->l.frac3 | x->l.frac4) {
55 				/* signal inexact */
56 				if (fsr & FSR_NXM) {
57 					/* z = x - 2^31 */
58 					if (xm & 0xffff ||
59 					    x->l.frac2 & 0xffff0000) {
60 						z.l.msw = xm & 0xffff;
61 						z.l.frac2 = x->l.frac2;
62 						z.l.frac3 = x->l.frac3;
63 						z.l.frac4 = x->l.frac4;
64 					} else if (x->l.frac2 & 0xffff ||
65 					    x->l.frac3 & 0xffff0000) {
66 						z.l.msw = x->l.frac2;
67 						z.l.frac2 = x->l.frac3;
68 						z.l.frac3 = x->l.frac4;
69 						z.l.frac4 = 0;
70 					} else if (x->l.frac3 & 0xffff ||
71 					    x->l.frac4 & 0xffff0000) {
72 						z.l.msw = x->l.frac3;
73 						z.l.frac2 = x->l.frac4;
74 						z.l.frac3 = z.l.frac4 = 0;
75 					} else {
76 						z.l.msw = x->l.frac4;
77 						z.l.frac2 = z.l.frac3 =
78 						    z.l.frac4 = 0;
79 					}
80 					xm = 0x401e;
81 					while ((z.l.msw & 0x10000) == 0) {
82 						z.l.msw = (z.l.msw << 1) |
83 						    (z.l.frac2 >> 31);
84 						z.l.frac2 = (z.l.frac2 << 1) |
85 						    (z.l.frac3 >> 31);
86 						z.l.frac3 = (z.l.frac3 << 1) |
87 						    (z.l.frac4 >> 31);
88 						z.l.frac4 <<= 1;
89 						xm--;
90 					}
91 					z.l.msw |= (xm << 16);
92 					__quad_fqtoi(&z, &i);
93 					i |= 0x80000000;
94 				} else {
95 					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
96 					    FSR_NXC;
97 					__quad_setfsrp(&fsr);
98 				}
99 			}
100 			return (i);
101 		}
102 		if (x->l.msw == 0xc01e0000 && (x->l.frac2 & 0xfffe0000) == 0) {
103 			/* return largest negative int */
104 			i = 0x80000000;
105 			if ((x->l.frac2 & 0x1ffff) | x->l.frac3 | x->l.frac4) {
106 				/* signal inexact */
107 				if (fsr & FSR_NXM) {
108 					__quad_fqtoi(x, &i);
109 				} else {
110 					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
111 					    FSR_NXC;
112 					__quad_setfsrp(&fsr);
113 				}
114 			}
115 			return (i);
116 		}
117 		i = ((x->l.msw & 0x80000000)? 0x80000000 : 0x7fffffff);
118 		if (fsr & FSR_NVM) {
119 			__quad_fqtoi(x, &i);
120 		} else {
121 			fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
122 			__quad_setfsrp(&fsr);
123 		}
124 		return (i);
125 	}
126 	if (xm < 0x3fff0000) {
127 		if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
128 			/* signal inexact */
129 			i = 0;
130 			if (fsr & FSR_NXM) {
131 				__quad_fqtoi(x, &i);
132 			} else {
133 				fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
134 				__quad_setfsrp(&fsr);
135 			}
136 			return (i);
137 		}
138 		return (0);
139 	}
140 
141 	/* now x is in the range of int */
142 	i = 0x40000000 | ((xm & 0xffff) << 14) | (x->l.frac2 >> 18);
143 	round = i & ((1 << (0x401d - (xm >> 16))) - 1);
144 	i >>= (0x401d - (xm >> 16));
145 	if (x->l.msw & 0x80000000)
146 		i = -i;
147 	if (round | (x->l.frac2 & 0x3ffff) | x->l.frac3 | x->l.frac4) {
148 		/* signal inexact */
149 		if (fsr & FSR_NXM) {
150 			__quad_fqtoi(x, &i);
151 		} else {
152 			fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
153 			__quad_setfsrp(&fsr);
154 		}
155 	}
156 	return (i);
157 }
158