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