xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_qtos.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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_qtos	_Qp_qtos
31 #endif
32 
33 /*
34  * _Q_qtos(x) returns (float)*x.
35  */
36 float
37 _Q_qtos(const union longdouble *x)
38 {
39 	union {
40 		float		f;
41 		unsigned int	l;
42 	} u;
43 	unsigned int		xm, round, sticky, fsr, rm;
44 	int			subnormal, e;
45 
46 	xm = x->l.msw & 0x7fffffff;
47 
48 	/* get the rounding mode, fudging directed rounding modes */
49 	/* as though the result were positive */
50 	__quad_getfsrp(&fsr);
51 	rm = fsr >> 30;
52 	if (x->l.msw & 0x80000000)
53 		rm ^= (rm >> 1);
54 
55 	/* handle nan, inf, and out-of-range cases */
56 	if (xm >= 0x407f0000) {
57 		if (xm >= 0x7fff0000) {
58 			if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
59 			    x->l.frac4) {
60 				/* x is nan */
61 				u.l = (x->l.msw & 0x80000000) | 0x7fc00000;
62 				u.l |= ((xm & 0x7fff) << 7) |
63 				    (x->l.frac2 >> 25);
64 				if (!(xm & 0x8000)) {
65 					/* snan, signal invalid */
66 					if (fsr & FSR_NVM) {
67 						__quad_fqtos(x, &u.f);
68 					} else {
69 						fsr = (fsr & ~FSR_CEXC) |
70 						    FSR_NVA | FSR_NVC;
71 						__quad_setfsrp(&fsr);
72 					}
73 				}
74 				return (u.f);
75 			}
76 			/* x is inf */
77 			u.l = (x->l.msw & 0x80000000) | 0x7f800000;
78 			return (u.f);
79 		}
80 		/* x is too big, overflow */
81 		if (rm == FSR_RN || rm == FSR_RP)
82 			u.l = 0x7f800000;
83 		else
84 			u.l = 0x7f7fffff;
85 		u.l |= (x->l.msw & 0x80000000);
86 		if (fsr & (FSR_OFM | FSR_NXM)) {
87 			__quad_fqtos(x, &u.f);
88 		} else {
89 			fsr = (fsr & ~FSR_CEXC) | FSR_OFA | FSR_OFC |
90 			    FSR_NXA | FSR_NXC;
91 			__quad_setfsrp(&fsr);
92 		}
93 		return (u.f);
94 	}
95 
96 	subnormal = 0;
97 	if (xm < 0x3f810000) {
98 		if (xm < 0x3f690000) {
99 			if (QUAD_ISZERO(*x)) {
100 				u.l = (x->l.msw & 0x80000000);
101 				return (u.f);
102 			}
103 			/* x is too small, underflow */
104 			u.l = ((rm == FSR_RP)? 1 : 0);
105 			u.l |= (x->l.msw & 0x80000000);
106 			if (fsr & (FSR_UFM | FSR_NXM)) {
107 				__quad_fqtos(x, &u.f);
108 			} else {
109 				fsr = (fsr & ~FSR_CEXC) | FSR_UFA | FSR_UFC |
110 				    FSR_NXA | FSR_NXC;
111 				__quad_setfsrp(&fsr);
112 			}
113 			return (u.f);
114 		}
115 
116 		/* x is in the subnormal range for single */
117 		subnormal = 1;
118 		u.l = 0x800000 | ((xm & 0xffff) << 7) | (x->l.frac2 >> 25);
119 		e = 0x3f80 - (xm >> 16);
120 		round = u.l & (1 << e);
121 		sticky = (u.l & ((1 << e) - 1)) | (x->l.frac2 & 0x1ffffff) |
122 			x->l.frac3 | x->l.frac4;
123 		u.l >>= e + 1;
124 	} else {
125 		/* x is in the normal range for single */
126 		u.l = ((xm - 0x3f800000) << 7) | (x->l.frac2 >> 25);
127 		round = x->l.frac2 & 0x1000000;
128 		sticky = (x->l.frac2 & 0xffffff) | x->l.frac3 | x->l.frac4;
129 	}
130 
131 	/* see if we need to round */
132 	fsr &= ~FSR_CEXC;
133 	if (round | sticky) {
134 		fsr |= FSR_NXC;
135 		if (subnormal)
136 			fsr |= FSR_UFC;
137 
138 		/* round up if necessary */
139 		if (rm == FSR_RP || (rm == FSR_RN && round && (sticky ||
140 		    (u.l & 1)))) {
141 			/* round up and check for overflow */
142 			if (++u.l >= 0x7f800000)
143 				fsr |= FSR_OFC;
144 		}
145 	}
146 
147 	/* if result is exact and subnormal but underflow trapping is */
148 	/* enabled, signal underflow */
149 	else if (subnormal && (fsr & FSR_UFM))
150 		fsr |= FSR_UFC;
151 
152 	/* attach the sign and raise exceptions as need be */
153 	u.l |= (x->l.msw & 0x80000000);
154 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
155 		__quad_setfsrp(&fsr);
156 		__quad_fqtos(x, &u.f);
157 	} else {
158 		fsr |= (fsr & 0x1f) << 5;
159 		__quad_setfsrp(&fsr);
160 	}
161 	return (u.f);
162 }
163