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