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