xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_dtoq.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_dtoq(pz, x) sets *pz = (long double)x.
33  */
34 void
35 _Qp_dtoq(union longdouble *pz, double x)
36 
37 #else
38 
39 /*
40  * _Q_dtoq(x) returns (long double)x.
41  */
42 union longdouble
43 _Q_dtoq(double x)
44 
45 #endif /* __sparcv9 */
46 
47 {
48 #ifndef __sparcv9
49 	union longdouble	z;
50 #endif
51 	union xdouble		u;
52 	unsigned int		m, lhi, llo, fsr;
53 
54 	/* extract the exponent */
55 	u.d = x;
56 	m = ((u.l.hi & 0x7ff00000) >> 4) + 0x3c000000;
57 	if (m == 0x3c000000) {
58 		/* x is zero or denormal */
59 		if ((u.l.hi & 0xfffff) | u.l.lo) {
60 			/* x is denormal, normalize it */
61 			m = 0x3c010000;
62 			lhi = u.l.hi & 0xfffff;
63 			llo = u.l.lo;
64 			do {
65 				lhi = (lhi << 1) | (llo >> 31);
66 				llo <<= 1;
67 				m -= 0x10000;
68 			} while ((lhi & 0x7ff00000) == 0);
69 			u.l.hi = (u.l.hi & 0x80000000) | lhi;
70 			u.l.lo = llo;
71 		} else {
72 			m = 0;
73 		}
74 	} else if (m == 0x43ff0000) {
75 		/* x is inf or nan */
76 		m = 0x7fff0000;
77 		if (((u.l.hi & 0x7ffff) | u.l.lo) && (u.l.hi & 0x80000) == 0) {
78 			/* snan, signal invalid */
79 			__quad_getfsrp(&fsr);
80 			if (fsr & FSR_NVM) {
81 				__quad_fdtoq(&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.hi |= 0x80000;
88 		}
89 	}
90 	Z.l.msw = m | (u.l.hi & 0x80000000) | ((u.l.hi & 0xffff0) >> 4);
91 	Z.l.frac2 = (u.l.hi << 28) | (u.l.lo >> 4);
92 	Z.l.frac3 = u.l.lo << 28;
93 	Z.l.frac4 = 0;
94 	QUAD_RETURN(Z);
95 }
96