xref: /illumos-gate/usr/src/lib/libc/sparcv9/fp/_Qp_qtoux.c (revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65)
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 /*
32  * _Qp_qtoux(x) returns (unsigned long)*x.
33  */
34 unsigned long
35 _Qp_qtoux(const union longdouble *x)
36 {
37 	union longdouble	z;
38 	unsigned long		i, round;
39 	unsigned int		xm, fsr;
40 
41 	xm = x->l.msw & 0x7fffffff;
42 
43 	__quad_getfsrp(&fsr);
44 
45 	/* handle nan, inf, and out-of-range cases */
46 	if (xm >= 0x403e0000) {
47 		if (x->l.msw < 0x403f0000) {
48 			i = 0x8000000000000000ul |
49 			    ((long) (xm & 0xffff) << 47) |
50 			    ((long) x->l.frac2 << 15) | (x->l.frac3 >> 17);
51 			if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
52 				/* signal inexact */
53 				if (fsr & FSR_NXM) {
54 					/* z = x - 2^63 */
55 					if (xm & 0xffff ||
56 					    x->l.frac2 & 0xffff0000) {
57 						z.l.msw = xm & 0xffff;
58 						z.l.frac2 = x->l.frac2;
59 						z.l.frac3 = x->l.frac3;
60 						z.l.frac4 = x->l.frac4;
61 					} else if (x->l.frac2 & 0xffff ||
62 					    x->l.frac3 & 0xffff0000) {
63 						z.l.msw = x->l.frac2;
64 						z.l.frac2 = x->l.frac3;
65 						z.l.frac3 = x->l.frac4;
66 						z.l.frac4 = 0;
67 					} else if (x->l.frac3 & 0xffff ||
68 					    x->l.frac4 & 0xffff0000) {
69 						z.l.msw = x->l.frac3;
70 						z.l.frac2 = x->l.frac4;
71 						z.l.frac3 = z.l.frac4 = 0;
72 					} else {
73 						z.l.msw = x->l.frac4;
74 						z.l.frac2 = z.l.frac3 =
75 						    z.l.frac4 = 0;
76 					}
77 					xm = 0x403e;
78 					while ((z.l.msw & 0x10000) == 0) {
79 						z.l.msw = (z.l.msw << 1) |
80 						    (z.l.frac2 >> 31);
81 						z.l.frac2 = (z.l.frac2 << 1) |
82 						    (z.l.frac3 >> 31);
83 						z.l.frac3 = (z.l.frac3 << 1) |
84 						    (z.l.frac4 >> 31);
85 						z.l.frac4 <<= 1;
86 						xm--;
87 					}
88 					z.l.msw |= (xm << 16);
89 					__quad_fqtox(&z, (long *)&i);
90 					i |= 0x8000000000000000ul;
91 				} else {
92 					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
93 					    FSR_NXC;
94 					__quad_setfsrp(&fsr);
95 				}
96 			}
97 			return (i);
98 		}
99  		if (x->l.msw == 0xc03e0000 && x->l.frac2 == 0 &&
100  		    (x->l.frac3 & 0xfffe0000) == 0) {
101  			/* return largest negative 64 bit int */
102  			i = 0x8000000000000000ul;
103  			if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
104  				/* signal inexact */
105  				if (fsr & FSR_NXM) {
106  					__quad_fqtox(x, (long *)&i);
107  				} else {
108  					fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
109  					    FSR_NXC;
110  					__quad_setfsrp(&fsr);
111  				}
112  			}
113  			return (i);
114  		}
115 		i = 0x7fffffffffffffffl;
116 		if (fsr & FSR_NVM) {
117 			__quad_fqtox(x, (long *)&i);
118 		} else {
119 			fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
120 			__quad_setfsrp(&fsr);
121 		}
122 		return (i);
123 	}
124 	if (xm < 0x3fff0000) {
125 		i = 0l;
126 		if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
127 			/* signal inexact */
128 			if (fsr & FSR_NXM) {
129 				__quad_fqtox(x, (long *)&i);
130 			} else {
131 				fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
132 				__quad_setfsrp(&fsr);
133 			}
134 		}
135 		return (i);
136 	}
137 
138 	/* now x is in the range of long */
139 	i = 0x4000000000000000l | ((long) (xm & 0xffff) << 46) |
140 	    ((long) x->l.frac2 << 14) | (x->l.frac3 >> 18);
141 	round = i & ((1l << (0x403d - (xm >> 16))) - 1);
142 	i >>= (0x403d - (xm >> 16));
143 	if (x->l.msw & 0x80000000)
144 		i = -i;
145 	if (round | (x->l.frac3 & 0x3ffff) | x->l.frac4) {
146 		/* signal inexact */
147 		if (fsr & FSR_NXM) {
148 			__quad_fqtox(x, (long *)&i);
149 		} else {
150 			fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
151 			__quad_setfsrp(&fsr);
152 		}
153 	}
154 	return (i);
155 }
156