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_qtox(x) returns (long)*x.
31 */
32 long
_Qp_qtox(const union longdouble * x)33 _Qp_qtox(const union longdouble *x)
34 {
35 long i, round;
36 unsigned int xm, fsr;
37
38 xm = x->l.msw & 0x7fffffff;
39
40 __quad_getfsrp(&fsr);
41
42 /* handle nan, inf, and out-of-range cases */
43 if (xm >= 0x403e0000) {
44 if (x->l.msw == 0xc03e0000 && x->l.frac2 == 0 &&
45 (x->l.frac3 & 0xfffe0000) == 0) {
46 /* return largest negative 64 bit int */
47 i = 0x8000000000000000ul;
48 if ((x->l.frac3 & 0x1ffff) | x->l.frac4) {
49 /* signal inexact */
50 if (fsr & FSR_NXM) {
51 __quad_fqtox(x, &i);
52 } else {
53 fsr = (fsr & ~FSR_CEXC) | FSR_NXA |
54 FSR_NXC;
55 __quad_setfsrp(&fsr);
56 }
57 }
58 return (i);
59 }
60 i = ((x->l.msw & 0x80000000)? 0x8000000000000000ul :
61 0x7fffffffffffffffl);
62 if (fsr & FSR_NVM) {
63 __quad_fqtox(x, &i);
64 } else {
65 fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
66 __quad_setfsrp(&fsr);
67 }
68 return (i);
69 }
70 if (xm < 0x3fff0000) {
71 i = 0l;
72 if (xm | x->l.frac2 | x->l.frac3 | x->l.frac4) {
73 /* signal inexact */
74 if (fsr & FSR_NXM) {
75 __quad_fqtox(x, &i);
76 } else {
77 fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
78 __quad_setfsrp(&fsr);
79 }
80 }
81 return (i);
82 }
83
84 /* now x is in the range of 64 bit int */
85 i = 0x4000000000000000l | ((long) (xm & 0xffff) << 46) |
86 ((long) x->l.frac2 << 14) | (x->l.frac3 >> 18);
87 round = i & ((1l << (0x403d - (xm >> 16))) - 1);
88 i >>= (0x403d - (xm >> 16));
89 if (x->l.msw & 0x80000000)
90 i = -i;
91 if (round | (x->l.frac3 & 0x3ffff) | x->l.frac4) {
92 /* signal inexact */
93 if (fsr & FSR_NXM) {
94 __quad_fqtox(x, &i);
95 } else {
96 fsr = (fsr & ~FSR_CEXC) | FSR_NXA | FSR_NXC;
97 __quad_setfsrp(&fsr);
98 }
99 }
100 return (i);
101 }
102