1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include "quad.h"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate #ifdef __sparcv9
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate * _Qp_sub(pz, ox, oy) sets *pz = *ox - *oy.
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate void
_Qp_sub(union longdouble * pz,const union longdouble * ox,const union longdouble * oy)37*0Sstevel@tonic-gate _Qp_sub(union longdouble *pz, const union longdouble *ox,
38*0Sstevel@tonic-gate const union longdouble *oy)
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #else
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate * _Q_sub(ox, oy) returns *ox - *oy.
44*0Sstevel@tonic-gate */
45*0Sstevel@tonic-gate union longdouble
46*0Sstevel@tonic-gate _Q_sub(const union longdouble *ox, const union longdouble *oy)
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #endif /* __sparcv9 */
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate union longdouble z;
52*0Sstevel@tonic-gate const union longdouble *x, *y;
53*0Sstevel@tonic-gate unsigned int xm, ym, tm, fsr;
54*0Sstevel@tonic-gate int flip;
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate /* sort so |x| >= |y| */
57*0Sstevel@tonic-gate xm = ox->l.msw & 0x7fffffff;
58*0Sstevel@tonic-gate ym = oy->l.msw & 0x7fffffff;
59*0Sstevel@tonic-gate if (ym > xm || ym == xm && (oy->l.frac2 > ox->l.frac2 ||
60*0Sstevel@tonic-gate oy->l.frac2 == ox->l.frac2 && (oy->l.frac3 > ox->l.frac3 ||
61*0Sstevel@tonic-gate oy->l.frac3 == ox->l.frac3 && oy->l.frac4 > ox->l.frac4))) {
62*0Sstevel@tonic-gate y = ox;
63*0Sstevel@tonic-gate x = oy;
64*0Sstevel@tonic-gate tm = xm;
65*0Sstevel@tonic-gate xm = ym;
66*0Sstevel@tonic-gate ym = tm;
67*0Sstevel@tonic-gate flip = 0x80000000;
68*0Sstevel@tonic-gate } else {
69*0Sstevel@tonic-gate x = ox;
70*0Sstevel@tonic-gate y = oy;
71*0Sstevel@tonic-gate flip = 0;
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate /* get the fsr */
75*0Sstevel@tonic-gate __quad_getfsrp(&fsr);
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /* handle nan and inf cases */
78*0Sstevel@tonic-gate if (xm >= 0x7fff0000) {
79*0Sstevel@tonic-gate /* x is nan or inf */
80*0Sstevel@tonic-gate if (ym >= 0x7fff0000) {
81*0Sstevel@tonic-gate /* y is nan or inf */
82*0Sstevel@tonic-gate if ((ym & 0xffff) | y->l.frac2 | y->l.frac3 |
83*0Sstevel@tonic-gate y->l.frac4) {
84*0Sstevel@tonic-gate /* y is nan; x must be nan too */
85*0Sstevel@tonic-gate /* the following logic implements V9 app. B */
86*0Sstevel@tonic-gate if (!(ym & 0x8000)) {
87*0Sstevel@tonic-gate /* y is snan, signal invalid */
88*0Sstevel@tonic-gate if (fsr & FSR_NVM) {
89*0Sstevel@tonic-gate __quad_fsubq(ox, oy, &Z);
90*0Sstevel@tonic-gate } else {
91*0Sstevel@tonic-gate Z = (xm & 0x8000)? *y : *oy;
92*0Sstevel@tonic-gate Z.l.msw |= 0x8000;
93*0Sstevel@tonic-gate fsr = (fsr & ~FSR_CEXC) |
94*0Sstevel@tonic-gate FSR_NVA | FSR_NVC;
95*0Sstevel@tonic-gate __quad_setfsrp(&fsr);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate QUAD_RETURN(Z);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate /* x and y are both qnan */
100*0Sstevel@tonic-gate Z = *oy;
101*0Sstevel@tonic-gate QUAD_RETURN(Z);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate if (!((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
104*0Sstevel@tonic-gate x->l.frac4)) {
105*0Sstevel@tonic-gate /* x and y are both inf */
106*0Sstevel@tonic-gate if (!((x->l.msw ^ y->l.msw) & 0x80000000)) {
107*0Sstevel@tonic-gate /* inf - inf, signal invalid */
108*0Sstevel@tonic-gate if (fsr & FSR_NVM) {
109*0Sstevel@tonic-gate __quad_fsubq(ox, oy, &Z);
110*0Sstevel@tonic-gate } else {
111*0Sstevel@tonic-gate Z.l.msw = 0x7fffffff;
112*0Sstevel@tonic-gate Z.l.frac2 = Z.l.frac3 =
113*0Sstevel@tonic-gate Z.l.frac4 = 0xffffffff;
114*0Sstevel@tonic-gate fsr = (fsr & ~FSR_CEXC) |
115*0Sstevel@tonic-gate FSR_NVA | FSR_NVC;
116*0Sstevel@tonic-gate __quad_setfsrp(&fsr);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate QUAD_RETURN(Z);
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate /* inf + inf, return inf */
121*0Sstevel@tonic-gate Z = *x;
122*0Sstevel@tonic-gate Z.l.msw ^= flip;
123*0Sstevel@tonic-gate QUAD_RETURN(Z);
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 | x->l.frac4) {
127*0Sstevel@tonic-gate /* x is nan */
128*0Sstevel@tonic-gate if (!(xm & 0x8000)) {
129*0Sstevel@tonic-gate /* snan, signal invalid */
130*0Sstevel@tonic-gate if (fsr & FSR_NVM) {
131*0Sstevel@tonic-gate __quad_fsubq(ox, oy, &Z);
132*0Sstevel@tonic-gate } else {
133*0Sstevel@tonic-gate Z = *x;
134*0Sstevel@tonic-gate Z.l.msw |= 0x8000;
135*0Sstevel@tonic-gate fsr = (fsr & ~FSR_CEXC) | FSR_NVA |
136*0Sstevel@tonic-gate FSR_NVC;
137*0Sstevel@tonic-gate __quad_setfsrp(&fsr);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate QUAD_RETURN(Z);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate Z = *x;
142*0Sstevel@tonic-gate QUAD_RETURN(Z);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate /* x is inf */
145*0Sstevel@tonic-gate Z = *x;
146*0Sstevel@tonic-gate Z.l.msw ^= flip;
147*0Sstevel@tonic-gate QUAD_RETURN(Z);
148*0Sstevel@tonic-gate }
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate /* now x and y are finite and |x| >= |y| */
151*0Sstevel@tonic-gate fsr &= ~FSR_CEXC;
152*0Sstevel@tonic-gate z.l.msw = (x->l.msw & 0x80000000) ^ flip;
153*0Sstevel@tonic-gate if ((x->l.msw ^ y->l.msw) & 0x80000000)
154*0Sstevel@tonic-gate __quad_mag_add(x, y, &z, &fsr);
155*0Sstevel@tonic-gate else
156*0Sstevel@tonic-gate __quad_mag_sub(x, y, &z, &fsr);
157*0Sstevel@tonic-gate if ((fsr & FSR_CEXC) & (fsr >> 23)) {
158*0Sstevel@tonic-gate __quad_setfsrp(&fsr);
159*0Sstevel@tonic-gate __quad_fsubq(ox, oy, &Z);
160*0Sstevel@tonic-gate } else {
161*0Sstevel@tonic-gate Z = z;
162*0Sstevel@tonic-gate fsr |= (fsr & 0x1f) << 5;
163*0Sstevel@tonic-gate __quad_setfsrp(&fsr);
164*0Sstevel@tonic-gate }
165*0Sstevel@tonic-gate QUAD_RETURN(Z);
166*0Sstevel@tonic-gate }
167