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