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