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