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