xref: /onnv-gate/usr/src/lib/libc/sparc/fp/_Q_qtod.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_qtod	_Qp_qtod
33*0Sstevel@tonic-gate #endif
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * _Q_qtod(x) returns (double)*x.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate double
_Q_qtod(const union longdouble * x)39*0Sstevel@tonic-gate _Q_qtod(const union longdouble *x)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	union xdouble	u;
42*0Sstevel@tonic-gate 	unsigned int	xm, round, sticky, fsr, rm;
43*0Sstevel@tonic-gate 	int		subnormal, e;
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate 	xm = x->l.msw & 0x7fffffff;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	/* get the rounding mode, fudging directed rounding modes */
48*0Sstevel@tonic-gate 	/* as though the result were positive */
49*0Sstevel@tonic-gate 	__quad_getfsrp(&fsr);
50*0Sstevel@tonic-gate 	rm = fsr >> 30;
51*0Sstevel@tonic-gate 	if (x->l.msw & 0x80000000)
52*0Sstevel@tonic-gate 		rm ^= (rm >> 1);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	/* handle nan, inf, and out-of-range cases */
55*0Sstevel@tonic-gate 	if (xm >= 0x43ff0000) {
56*0Sstevel@tonic-gate 		if (xm >= 0x7fff0000) {
57*0Sstevel@tonic-gate 			if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
58*0Sstevel@tonic-gate 			    x->l.frac4) {
59*0Sstevel@tonic-gate 				/* x is nan */
60*0Sstevel@tonic-gate 				u.l.hi = (x->l.msw & 0x80000000) | 0x7ff80000;
61*0Sstevel@tonic-gate 				u.l.hi |= ((xm & 0x7fff) << 4) |
62*0Sstevel@tonic-gate 				    (x->l.frac2 >> 28);
63*0Sstevel@tonic-gate 				u.l.lo = (x->l.frac2 << 4) |
64*0Sstevel@tonic-gate 				    (x->l.frac3 >> 28);
65*0Sstevel@tonic-gate 				if (!(xm & 0x8000)) {
66*0Sstevel@tonic-gate 					/* snan, signal invalid */
67*0Sstevel@tonic-gate 					if (fsr & FSR_NVM) {
68*0Sstevel@tonic-gate 						__quad_fqtod(x, &u.d);
69*0Sstevel@tonic-gate 					} else {
70*0Sstevel@tonic-gate 						fsr = (fsr & ~FSR_CEXC) |
71*0Sstevel@tonic-gate 						    FSR_NVA | FSR_NVC;
72*0Sstevel@tonic-gate 						__quad_setfsrp(&fsr);
73*0Sstevel@tonic-gate 					}
74*0Sstevel@tonic-gate 				}
75*0Sstevel@tonic-gate 				return (u.d);
76*0Sstevel@tonic-gate 			}
77*0Sstevel@tonic-gate 			/* x is inf */
78*0Sstevel@tonic-gate 			u.l.hi = (x->l.msw & 0x80000000) | 0x7ff00000;
79*0Sstevel@tonic-gate 			u.l.lo = 0;
80*0Sstevel@tonic-gate 			return (u.d);
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.hi = 0x7ff00000;
85*0Sstevel@tonic-gate 			u.l.lo = 0;
86*0Sstevel@tonic-gate 		} else {
87*0Sstevel@tonic-gate 			u.l.hi = 0x7fefffff;
88*0Sstevel@tonic-gate 			u.l.lo = 0xffffffff;
89*0Sstevel@tonic-gate 		}
90*0Sstevel@tonic-gate 		u.l.hi |= (x->l.msw & 0x80000000);
91*0Sstevel@tonic-gate 		if (fsr & (FSR_OFM | FSR_NXM)) {
92*0Sstevel@tonic-gate 			__quad_fqtod(x, &u.d);
93*0Sstevel@tonic-gate 		} else {
94*0Sstevel@tonic-gate 			fsr = (fsr & ~FSR_CEXC) | FSR_OFA | FSR_OFC |
95*0Sstevel@tonic-gate 			    FSR_NXA | FSR_NXC;
96*0Sstevel@tonic-gate 			__quad_setfsrp(&fsr);
97*0Sstevel@tonic-gate 		}
98*0Sstevel@tonic-gate 		return (u.d);
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	subnormal = 0;
102*0Sstevel@tonic-gate 	if (xm < 0x3c010000) {
103*0Sstevel@tonic-gate 		if (xm < 0x3bcc0000) {
104*0Sstevel@tonic-gate 			if (QUAD_ISZERO(*x)) {
105*0Sstevel@tonic-gate 				u.l.hi = (x->l.msw & 0x80000000);
106*0Sstevel@tonic-gate 				u.l.lo = 0;
107*0Sstevel@tonic-gate 				return (u.d);
108*0Sstevel@tonic-gate 			}
109*0Sstevel@tonic-gate 			/* x is too small, underflow */
110*0Sstevel@tonic-gate 			u.l.hi = (x->l.msw & 0x80000000);
111*0Sstevel@tonic-gate 			u.l.lo = ((rm == FSR_RP)? 1 : 0);
112*0Sstevel@tonic-gate 			if (fsr & (FSR_UFM | FSR_NXM)) {
113*0Sstevel@tonic-gate 				__quad_fqtod(x, &u.d);
114*0Sstevel@tonic-gate 			} else {
115*0Sstevel@tonic-gate 				fsr = (fsr & ~FSR_CEXC) | FSR_UFA | FSR_UFC |
116*0Sstevel@tonic-gate 				    FSR_NXA | FSR_NXC;
117*0Sstevel@tonic-gate 				__quad_setfsrp(&fsr);
118*0Sstevel@tonic-gate 			}
119*0Sstevel@tonic-gate 			return (u.d);
120*0Sstevel@tonic-gate 		}
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 		/* x is in the subnormal range for double */
123*0Sstevel@tonic-gate 		subnormal = 1;
124*0Sstevel@tonic-gate 		u.l.hi = 0x80000 | ((xm & 0xffff) << 3) | (x->l.frac2 >> 29);
125*0Sstevel@tonic-gate 		u.l.lo = (x->l.frac2 << 3) | (x->l.frac3 >> 29);
126*0Sstevel@tonic-gate 		round = x->l.frac3 & 0x10000000;
127*0Sstevel@tonic-gate 		sticky = (x->l.frac3 & 0xfffffff) | x->l.frac4;
128*0Sstevel@tonic-gate 		e = 0x3c00 - (xm >> 16);
129*0Sstevel@tonic-gate 		if (e >= 32) {
130*0Sstevel@tonic-gate 			sticky |= round | (u.l.lo & 0x7fffffff);
131*0Sstevel@tonic-gate 			round = u.l.lo & 0x80000000;
132*0Sstevel@tonic-gate 			u.l.lo = u.l.hi;
133*0Sstevel@tonic-gate 			u.l.hi = 0;
134*0Sstevel@tonic-gate 			e -= 32;
135*0Sstevel@tonic-gate 		}
136*0Sstevel@tonic-gate 		if (e) {
137*0Sstevel@tonic-gate 			sticky |= round | (u.l.lo & ((1 << (e - 1)) - 1));
138*0Sstevel@tonic-gate 			round = u.l.lo & (1 << (e - 1));
139*0Sstevel@tonic-gate 			u.l.lo = (u.l.lo >> e) | (u.l.hi << (32 - e));
140*0Sstevel@tonic-gate 			u.l.hi >>= e;
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 	} else {
143*0Sstevel@tonic-gate 		/* x is in the normal range for double */
144*0Sstevel@tonic-gate 		u.l.hi = ((xm - 0x3c000000) << 4) | (x->l.frac2 >> 28);
145*0Sstevel@tonic-gate 		u.l.lo = (x->l.frac2 << 4) | (x->l.frac3 >> 28);
146*0Sstevel@tonic-gate 		round = x->l.frac3 & 0x8000000;
147*0Sstevel@tonic-gate 		sticky = (x->l.frac3 & 0x7ffffff) | x->l.frac4;
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/* see if we need to round */
151*0Sstevel@tonic-gate 	fsr &= ~FSR_CEXC;
152*0Sstevel@tonic-gate 	if (round | sticky) {
153*0Sstevel@tonic-gate 		fsr |= FSR_NXC;
154*0Sstevel@tonic-gate 		if (subnormal)
155*0Sstevel@tonic-gate 			fsr |= FSR_UFC;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 		/* round up if necessary */
158*0Sstevel@tonic-gate 		if (rm == FSR_RP || (rm == FSR_RN && round && (sticky ||
159*0Sstevel@tonic-gate 		    (u.l.lo & 1)))) {
160*0Sstevel@tonic-gate 			/* round up and check for overflow */
161*0Sstevel@tonic-gate 			if (++u.l.lo == 0)
162*0Sstevel@tonic-gate 				if (++u.l.hi >= 0x7ff00000)
163*0Sstevel@tonic-gate 					fsr |= FSR_OFC;
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/* if result is exact and subnormal but underflow trapping is */
168*0Sstevel@tonic-gate 	/* enabled, signal underflow */
169*0Sstevel@tonic-gate 	else if (subnormal && (fsr & FSR_UFM))
170*0Sstevel@tonic-gate 		fsr |= FSR_UFC;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* attach the sign and raise exceptions as need be */
173*0Sstevel@tonic-gate 	u.l.hi |= (x->l.msw & 0x80000000);
174*0Sstevel@tonic-gate 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
175*0Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
176*0Sstevel@tonic-gate 		__quad_fqtod(x, &u.d);
177*0Sstevel@tonic-gate 	} else {
178*0Sstevel@tonic-gate 		fsr |= (fsr & 0x1f) << 5;
179*0Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate 	return (u.d);
182*0Sstevel@tonic-gate }
183