xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_stoq.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 
31 /*
32  * _Qp_stoq(pz, x) sets *pz = (long double)x.
33  */
34 void
35 _Qp_stoq(union longdouble *pz, float x)
36 
37 #else
38 
39 /*
40  * _Qp_stoq(x) returns (long double)x.
41  */
42 union longdouble
43 _Q_stoq(float x)
44 
45 #endif /* __sparcv9 */
46 
47 {
48 #ifndef __sparcv9
49 	union longdouble	z;
50 #endif
51 	union {
52 		float		f;
53 		unsigned int	l;
54 	} u;
55 	unsigned int		m, f, fsr;
56 
57 	/* extract the exponent */
58 	u.f = x;
59 	m = ((u.l & 0x7f800000) >> 7) + 0x3f800000;
60 	if (m == 0x3f800000) {
61 		/* x is zero or denormal */
62 		if (u.l & 0x7fffff) {
63 			/* x is denormal, normalize it */
64 			m = 0x3f810000;
65 			f = u.l & 0x7fffff;
66 			do {
67 				f <<= 1;
68 				m -= 0x10000;
69 			} while ((f & 0x7f800000) == 0);
70 			u.l = (u.l & 0x80000000) | f;
71 		} else {
72 			m = 0;
73 		}
74 	} else if (m == 0x407f0000) {
75 		/* x is inf or nan */
76 		m = 0x7fff0000;
77 		if ((u.l & 0x3fffff) && (u.l & 0x400000) == 0) {
78 			/* snan, signal invalid */
79 			__quad_getfsrp(&fsr);
80 			if (fsr & FSR_NVM) {
81 				__quad_fstoq(&x, &Z);
82 				QUAD_RETURN(Z);
83 			} else {
84 				fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
85 				__quad_setfsrp(&fsr);
86 			}
87 			u.l |= 0x400000;
88 		}
89 	}
90 	Z.l.msw = m | (u.l & 0x80000000) | ((u.l & 0x7fff80) >> 7);
91 	Z.l.frac2 = u.l << 25;
92 	Z.l.frac3 = Z.l.frac4 = 0;
93 	QUAD_RETURN(Z);
94 }
95