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