xref: /onnv-gate/usr/src/lib/libc/sparc/fp/_Q_qtos.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1994-1997, by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include "quad.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #ifdef __sparcv9
32*0Sstevel@tonic-gate #define	_Q_qtos	_Qp_qtos
33*0Sstevel@tonic-gate #endif
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * _Q_qtos(x) returns (float)*x.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate float
_Q_qtos(const union longdouble * x)39*0Sstevel@tonic-gate _Q_qtos(const union longdouble *x)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	union {
42*0Sstevel@tonic-gate 		float		f;
43*0Sstevel@tonic-gate 		unsigned int	l;
44*0Sstevel@tonic-gate 	} u;
45*0Sstevel@tonic-gate 	unsigned int		xm, round, sticky, fsr, rm;
46*0Sstevel@tonic-gate 	int			subnormal, e;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	xm = x->l.msw & 0x7fffffff;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	/* get the rounding mode, fudging directed rounding modes */
51*0Sstevel@tonic-gate 	/* as though the result were positive */
52*0Sstevel@tonic-gate 	__quad_getfsrp(&fsr);
53*0Sstevel@tonic-gate 	rm = fsr >> 30;
54*0Sstevel@tonic-gate 	if (x->l.msw & 0x80000000)
55*0Sstevel@tonic-gate 		rm ^= (rm >> 1);
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate 	/* handle nan, inf, and out-of-range cases */
58*0Sstevel@tonic-gate 	if (xm >= 0x407f0000) {
59*0Sstevel@tonic-gate 		if (xm >= 0x7fff0000) {
60*0Sstevel@tonic-gate 			if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
61*0Sstevel@tonic-gate 			    x->l.frac4) {
62*0Sstevel@tonic-gate 				/* x is nan */
63*0Sstevel@tonic-gate 				u.l = (x->l.msw & 0x80000000) | 0x7fc00000;
64*0Sstevel@tonic-gate 				u.l |= ((xm & 0x7fff) << 7) |
65*0Sstevel@tonic-gate 				    (x->l.frac2 >> 25);
66*0Sstevel@tonic-gate 				if (!(xm & 0x8000)) {
67*0Sstevel@tonic-gate 					/* snan, signal invalid */
68*0Sstevel@tonic-gate 					if (fsr & FSR_NVM) {
69*0Sstevel@tonic-gate 						__quad_fqtos(x, &u.f);
70*0Sstevel@tonic-gate 					} else {
71*0Sstevel@tonic-gate 						fsr = (fsr & ~FSR_CEXC) |
72*0Sstevel@tonic-gate 						    FSR_NVA | FSR_NVC;
73*0Sstevel@tonic-gate 						__quad_setfsrp(&fsr);
74*0Sstevel@tonic-gate 					}
75*0Sstevel@tonic-gate 				}
76*0Sstevel@tonic-gate 				return (u.f);
77*0Sstevel@tonic-gate 			}
78*0Sstevel@tonic-gate 			/* x is inf */
79*0Sstevel@tonic-gate 			u.l = (x->l.msw & 0x80000000) | 0x7f800000;
80*0Sstevel@tonic-gate 			return (u.f);
81*0Sstevel@tonic-gate 		}
82*0Sstevel@tonic-gate 		/* x is too big, overflow */
83*0Sstevel@tonic-gate 		if (rm == FSR_RN || rm == FSR_RP)
84*0Sstevel@tonic-gate 			u.l = 0x7f800000;
85*0Sstevel@tonic-gate 		else
86*0Sstevel@tonic-gate 			u.l = 0x7f7fffff;
87*0Sstevel@tonic-gate 		u.l |= (x->l.msw & 0x80000000);
88*0Sstevel@tonic-gate 		if (fsr & (FSR_OFM | FSR_NXM)) {
89*0Sstevel@tonic-gate 			__quad_fqtos(x, &u.f);
90*0Sstevel@tonic-gate 		} else {
91*0Sstevel@tonic-gate 			fsr = (fsr & ~FSR_CEXC) | FSR_OFA | FSR_OFC |
92*0Sstevel@tonic-gate 			    FSR_NXA | FSR_NXC;
93*0Sstevel@tonic-gate 			__quad_setfsrp(&fsr);
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 		return (u.f);
96*0Sstevel@tonic-gate 	}
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	subnormal = 0;
99*0Sstevel@tonic-gate 	if (xm < 0x3f810000) {
100*0Sstevel@tonic-gate 		if (xm < 0x3f690000) {
101*0Sstevel@tonic-gate 			if (QUAD_ISZERO(*x)) {
102*0Sstevel@tonic-gate 				u.l = (x->l.msw & 0x80000000);
103*0Sstevel@tonic-gate 				return (u.f);
104*0Sstevel@tonic-gate 			}
105*0Sstevel@tonic-gate 			/* x is too small, underflow */
106*0Sstevel@tonic-gate 			u.l = ((rm == FSR_RP)? 1 : 0);
107*0Sstevel@tonic-gate 			u.l |= (x->l.msw & 0x80000000);
108*0Sstevel@tonic-gate 			if (fsr & (FSR_UFM | FSR_NXM)) {
109*0Sstevel@tonic-gate 				__quad_fqtos(x, &u.f);
110*0Sstevel@tonic-gate 			} else {
111*0Sstevel@tonic-gate 				fsr = (fsr & ~FSR_CEXC) | FSR_UFA | FSR_UFC |
112*0Sstevel@tonic-gate 				    FSR_NXA | FSR_NXC;
113*0Sstevel@tonic-gate 				__quad_setfsrp(&fsr);
114*0Sstevel@tonic-gate 			}
115*0Sstevel@tonic-gate 			return (u.f);
116*0Sstevel@tonic-gate 		}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 		/* x is in the subnormal range for single */
119*0Sstevel@tonic-gate 		subnormal = 1;
120*0Sstevel@tonic-gate 		u.l = 0x800000 | ((xm & 0xffff) << 7) | (x->l.frac2 >> 25);
121*0Sstevel@tonic-gate 		e = 0x3f80 - (xm >> 16);
122*0Sstevel@tonic-gate 		round = u.l & (1 << e);
123*0Sstevel@tonic-gate 		sticky = (u.l & ((1 << e) - 1)) | (x->l.frac2 & 0x1ffffff) |
124*0Sstevel@tonic-gate 			x->l.frac3 | x->l.frac4;
125*0Sstevel@tonic-gate 		u.l >>= e + 1;
126*0Sstevel@tonic-gate 	} else {
127*0Sstevel@tonic-gate 		/* x is in the normal range for single */
128*0Sstevel@tonic-gate 		u.l = ((xm - 0x3f800000) << 7) | (x->l.frac2 >> 25);
129*0Sstevel@tonic-gate 		round = x->l.frac2 & 0x1000000;
130*0Sstevel@tonic-gate 		sticky = (x->l.frac2 & 0xffffff) | x->l.frac3 | x->l.frac4;
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	/* see if we need to round */
134*0Sstevel@tonic-gate 	fsr &= ~FSR_CEXC;
135*0Sstevel@tonic-gate 	if (round | sticky) {
136*0Sstevel@tonic-gate 		fsr |= FSR_NXC;
137*0Sstevel@tonic-gate 		if (subnormal)
138*0Sstevel@tonic-gate 			fsr |= FSR_UFC;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 		/* round up if necessary */
141*0Sstevel@tonic-gate 		if (rm == FSR_RP || (rm == FSR_RN && round && (sticky ||
142*0Sstevel@tonic-gate 		    (u.l & 1)))) {
143*0Sstevel@tonic-gate 			/* round up and check for overflow */
144*0Sstevel@tonic-gate 			if (++u.l >= 0x7f800000)
145*0Sstevel@tonic-gate 				fsr |= FSR_OFC;
146*0Sstevel@tonic-gate 		}
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* if result is exact and subnormal but underflow trapping is */
150*0Sstevel@tonic-gate 	/* enabled, signal underflow */
151*0Sstevel@tonic-gate 	else if (subnormal && (fsr & FSR_UFM))
152*0Sstevel@tonic-gate 		fsr |= FSR_UFC;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/* attach the sign and raise exceptions as need be */
155*0Sstevel@tonic-gate 	u.l |= (x->l.msw & 0x80000000);
156*0Sstevel@tonic-gate 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
157*0Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
158*0Sstevel@tonic-gate 		__quad_fqtos(x, &u.f);
159*0Sstevel@tonic-gate 	} else {
160*0Sstevel@tonic-gate 		fsr |= (fsr & 0x1f) << 5;
161*0Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
162*0Sstevel@tonic-gate 	}
163*0Sstevel@tonic-gate 	return (u.f);
164*0Sstevel@tonic-gate }
165