xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_add.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_add(pz, ox, oy) sets *pz = *ox + *oy.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate void
_Qp_add(union longdouble * pz,const union longdouble * ox,const union longdouble * oy)35*7c478bd9Sstevel@tonic-gate _Qp_add(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_add(ox, oy) returns *ox + *oy.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate union longdouble
44*7c478bd9Sstevel@tonic-gate _Q_add(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 
53*7c478bd9Sstevel@tonic-gate 	/* sort so |x| >= |y| */
54*7c478bd9Sstevel@tonic-gate 	xm = ox->l.msw & 0x7fffffff;
55*7c478bd9Sstevel@tonic-gate 	ym = oy->l.msw & 0x7fffffff;
56*7c478bd9Sstevel@tonic-gate 	if (ym > xm || ym == xm && (oy->l.frac2 > ox->l.frac2 ||
57*7c478bd9Sstevel@tonic-gate 	    oy->l.frac2 == ox->l.frac2 && (oy->l.frac3 > ox->l.frac3 ||
58*7c478bd9Sstevel@tonic-gate 	    oy->l.frac3 == ox->l.frac3 && oy->l.frac4 > ox->l.frac4))) {
59*7c478bd9Sstevel@tonic-gate 		y = ox;
60*7c478bd9Sstevel@tonic-gate 		x = oy;
61*7c478bd9Sstevel@tonic-gate 		tm = xm;
62*7c478bd9Sstevel@tonic-gate 		xm = ym;
63*7c478bd9Sstevel@tonic-gate 		ym = tm;
64*7c478bd9Sstevel@tonic-gate 	} else {
65*7c478bd9Sstevel@tonic-gate 		x = ox;
66*7c478bd9Sstevel@tonic-gate 		y = oy;
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	/* get the fsr */
70*7c478bd9Sstevel@tonic-gate 	__quad_getfsrp(&fsr);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/* handle nan and inf cases */
73*7c478bd9Sstevel@tonic-gate 	if (xm >= 0x7fff0000) {
74*7c478bd9Sstevel@tonic-gate 		/* x is nan or inf */
75*7c478bd9Sstevel@tonic-gate 		if (ym >= 0x7fff0000) {
76*7c478bd9Sstevel@tonic-gate 			/* y is nan or inf */
77*7c478bd9Sstevel@tonic-gate 			if ((ym & 0xffff) | y->l.frac2 | y->l.frac3 |
78*7c478bd9Sstevel@tonic-gate 			    y->l.frac4) {
79*7c478bd9Sstevel@tonic-gate 				/* y is nan; x must be nan too */
80*7c478bd9Sstevel@tonic-gate 				/* the following logic implements V9 app. B */
81*7c478bd9Sstevel@tonic-gate 				if (!(ym & 0x8000)) {
82*7c478bd9Sstevel@tonic-gate 					/* y is snan, signal invalid */
83*7c478bd9Sstevel@tonic-gate 					if (fsr & FSR_NVM) {
84*7c478bd9Sstevel@tonic-gate 						__quad_faddq(ox, oy, &Z);
85*7c478bd9Sstevel@tonic-gate 					} else {
86*7c478bd9Sstevel@tonic-gate 						Z = (xm & 0x8000)? *y : *oy;
87*7c478bd9Sstevel@tonic-gate 						Z.l.msw |= 0x8000;
88*7c478bd9Sstevel@tonic-gate 						fsr = (fsr & ~FSR_CEXC) |
89*7c478bd9Sstevel@tonic-gate 						    FSR_NVA | FSR_NVC;
90*7c478bd9Sstevel@tonic-gate 						__quad_setfsrp(&fsr);
91*7c478bd9Sstevel@tonic-gate 					}
92*7c478bd9Sstevel@tonic-gate 					QUAD_RETURN(Z);
93*7c478bd9Sstevel@tonic-gate 				}
94*7c478bd9Sstevel@tonic-gate 				/* x and y are both qnan */
95*7c478bd9Sstevel@tonic-gate 				Z = *oy;
96*7c478bd9Sstevel@tonic-gate 				QUAD_RETURN(Z);
97*7c478bd9Sstevel@tonic-gate 			}
98*7c478bd9Sstevel@tonic-gate 			if (!((xm & 0xffff) | x->l.frac2 | x->l.frac3 |
99*7c478bd9Sstevel@tonic-gate 			    x->l.frac4)) {
100*7c478bd9Sstevel@tonic-gate 				/* x and y are both inf */
101*7c478bd9Sstevel@tonic-gate 				if ((x->l.msw ^ y->l.msw) & 0x80000000) {
102*7c478bd9Sstevel@tonic-gate 					/* inf - inf, signal invalid */
103*7c478bd9Sstevel@tonic-gate 					if (fsr & FSR_NVM) {
104*7c478bd9Sstevel@tonic-gate 						__quad_faddq(ox, oy, &Z);
105*7c478bd9Sstevel@tonic-gate 					} else {
106*7c478bd9Sstevel@tonic-gate 						Z.l.msw = 0x7fffffff;
107*7c478bd9Sstevel@tonic-gate 						Z.l.frac2 = Z.l.frac3 =
108*7c478bd9Sstevel@tonic-gate 						    Z.l.frac4 = 0xffffffff;
109*7c478bd9Sstevel@tonic-gate 						fsr = (fsr & ~FSR_CEXC) |
110*7c478bd9Sstevel@tonic-gate 						    FSR_NVA | FSR_NVC;
111*7c478bd9Sstevel@tonic-gate 						__quad_setfsrp(&fsr);
112*7c478bd9Sstevel@tonic-gate 					}
113*7c478bd9Sstevel@tonic-gate 					QUAD_RETURN(Z);
114*7c478bd9Sstevel@tonic-gate 				}
115*7c478bd9Sstevel@tonic-gate 				/* inf + inf, return inf */
116*7c478bd9Sstevel@tonic-gate 				Z = *x;
117*7c478bd9Sstevel@tonic-gate 				QUAD_RETURN(Z);
118*7c478bd9Sstevel@tonic-gate 			}
119*7c478bd9Sstevel@tonic-gate 		}
120*7c478bd9Sstevel@tonic-gate 		if ((xm & 0xffff) | x->l.frac2 | x->l.frac3 | x->l.frac4) {
121*7c478bd9Sstevel@tonic-gate 			/* x is nan */
122*7c478bd9Sstevel@tonic-gate 			if (!(xm & 0x8000)) {
123*7c478bd9Sstevel@tonic-gate 				/* snan, signal invalid */
124*7c478bd9Sstevel@tonic-gate 				if (fsr & FSR_NVM) {
125*7c478bd9Sstevel@tonic-gate 					__quad_faddq(ox, oy, &Z);
126*7c478bd9Sstevel@tonic-gate 				} else {
127*7c478bd9Sstevel@tonic-gate 					Z = *x;
128*7c478bd9Sstevel@tonic-gate 					Z.l.msw |= 0x8000;
129*7c478bd9Sstevel@tonic-gate 					fsr = (fsr & ~FSR_CEXC) | FSR_NVA |
130*7c478bd9Sstevel@tonic-gate 					    FSR_NVC;
131*7c478bd9Sstevel@tonic-gate 					__quad_setfsrp(&fsr);
132*7c478bd9Sstevel@tonic-gate 				}
133*7c478bd9Sstevel@tonic-gate 				QUAD_RETURN(Z);
134*7c478bd9Sstevel@tonic-gate 			}
135*7c478bd9Sstevel@tonic-gate 			Z = *x;
136*7c478bd9Sstevel@tonic-gate 			QUAD_RETURN(Z);
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 		/* x is inf */
139*7c478bd9Sstevel@tonic-gate 		Z = *x;
140*7c478bd9Sstevel@tonic-gate 		QUAD_RETURN(Z);
141*7c478bd9Sstevel@tonic-gate 	}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	/* now x and y are finite and |x| >= |y| */
144*7c478bd9Sstevel@tonic-gate 	fsr &= ~FSR_CEXC;
145*7c478bd9Sstevel@tonic-gate 	z.l.msw = (x->l.msw & 0x80000000);
146*7c478bd9Sstevel@tonic-gate 	if ((x->l.msw ^ y->l.msw) & 0x80000000)
147*7c478bd9Sstevel@tonic-gate 		__quad_mag_sub(x, y, &z, &fsr);
148*7c478bd9Sstevel@tonic-gate 	else
149*7c478bd9Sstevel@tonic-gate 		__quad_mag_add(x, y, &z, &fsr);
150*7c478bd9Sstevel@tonic-gate 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
151*7c478bd9Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
152*7c478bd9Sstevel@tonic-gate 		__quad_faddq(ox, oy, &Z);
153*7c478bd9Sstevel@tonic-gate 	} else {
154*7c478bd9Sstevel@tonic-gate 		Z = z;
155*7c478bd9Sstevel@tonic-gate 		fsr |= (fsr & 0x1f) << 5;
156*7c478bd9Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 	QUAD_RETURN(Z);
159*7c478bd9Sstevel@tonic-gate }
160