xref: /onnv-gate/usr/src/uts/sparc/fpu/addsub.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 (c) 1988 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS-4.1 1.8 88/12/06 */
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h>
29*0Sstevel@tonic-gate #include <sys/fpu/globals.h>
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate static void
true_add(px,py,pz)32*0Sstevel@tonic-gate true_add(px, py, pz)
33*0Sstevel@tonic-gate 	unpacked	*px, *py, *pz;
34*0Sstevel@tonic-gate {
35*0Sstevel@tonic-gate 	unsigned 	c;
36*0Sstevel@tonic-gate 	unpacked	*pt;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate 	if ((int) px->fpclass <= (int) py->fpclass) {	/* Reverse. */
39*0Sstevel@tonic-gate 		pt = py;
40*0Sstevel@tonic-gate 		py = px;
41*0Sstevel@tonic-gate 		px = pt;
42*0Sstevel@tonic-gate 	}
43*0Sstevel@tonic-gate 	/* Now class(x) >= class(y). */
44*0Sstevel@tonic-gate 	switch (px->fpclass) {
45*0Sstevel@tonic-gate 	case fp_quiet:		/* NaN + x -> NaN */
46*0Sstevel@tonic-gate 	case fp_signaling:	/* NaN + x -> NaN */
47*0Sstevel@tonic-gate 	case fp_infinity:	/* Inf + x -> Inf */
48*0Sstevel@tonic-gate 	case fp_zero:		/* 0 + 0 -> 0 */
49*0Sstevel@tonic-gate 		*pz = *px;
50*0Sstevel@tonic-gate 		return;
51*0Sstevel@tonic-gate 	default:
52*0Sstevel@tonic-gate 		if (py->fpclass == fp_zero) {
53*0Sstevel@tonic-gate 			*pz = *px;
54*0Sstevel@tonic-gate 			return;
55*0Sstevel@tonic-gate 		}
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 	/* Now z is normal or subnormal. */
58*0Sstevel@tonic-gate 	/* Now y is normal or subnormal. */
59*0Sstevel@tonic-gate 	if (px->exponent < py->exponent) {	/* Reverse. */
60*0Sstevel@tonic-gate 		pt = py;
61*0Sstevel@tonic-gate 		py = px;
62*0Sstevel@tonic-gate 		px = pt;
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	/* Now class(x) >= class(y). */
65*0Sstevel@tonic-gate 	pz->fpclass = px->fpclass;
66*0Sstevel@tonic-gate 	pz->sign = px->sign;
67*0Sstevel@tonic-gate 	pz->exponent = px->exponent;
68*0Sstevel@tonic-gate 	pz->rounded = pz->sticky  = 0;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if (px->exponent != py->exponent) {	/* pre-alignment required */
71*0Sstevel@tonic-gate 		fpu_rightshift(py, pz->exponent - py->exponent);
72*0Sstevel@tonic-gate 		pz->rounded = py->rounded;
73*0Sstevel@tonic-gate 		pz->sticky  = py->sticky;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	c = 0;
76*0Sstevel@tonic-gate 	c = fpu_add3wc(&(pz->significand[3]), px->significand[3],
77*0Sstevel@tonic-gate 						py->significand[3], c);
78*0Sstevel@tonic-gate 	c = fpu_add3wc(&(pz->significand[2]), px->significand[2],
79*0Sstevel@tonic-gate 						py->significand[2], c);
80*0Sstevel@tonic-gate 	c = fpu_add3wc(&(pz->significand[1]), px->significand[1],
81*0Sstevel@tonic-gate 						py->significand[1], c);
82*0Sstevel@tonic-gate 	c = fpu_add3wc(&(pz->significand[0]), px->significand[0],
83*0Sstevel@tonic-gate 						py->significand[0], c);
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* Handle carry out of msb. */
86*0Sstevel@tonic-gate 	if (pz->significand[0] >= 0x20000) {
87*0Sstevel@tonic-gate 		fpu_rightshift(pz, 1);	/* Carried out bit. */
88*0Sstevel@tonic-gate 		pz->exponent++;		/* Renormalize. */
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static void
true_sub(pfpsd,px,py,pz)93*0Sstevel@tonic-gate true_sub(pfpsd, px, py, pz)
94*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd;		/* Pointer to simulator data */
95*0Sstevel@tonic-gate 	unpacked	*px, *py, *pz;
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	unsigned	*z, g, s, r, c;
98*0Sstevel@tonic-gate 	unpacked	*pt;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	if ((int) px->fpclass <= (int) py->fpclass) {	/* Reverse. */
101*0Sstevel@tonic-gate 		pt = py;
102*0Sstevel@tonic-gate 		py = px;
103*0Sstevel@tonic-gate 		px = pt;
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	/* Now class(x) >= class(y). */
106*0Sstevel@tonic-gate 	*pz = *px;		/* Tentative difference: x. */
107*0Sstevel@tonic-gate 	switch (pz->fpclass) {
108*0Sstevel@tonic-gate 	case fp_quiet:		/* NaN - x -> NaN */
109*0Sstevel@tonic-gate 	case fp_signaling:	/* NaN - x -> NaN */
110*0Sstevel@tonic-gate 		return;
111*0Sstevel@tonic-gate 	case fp_infinity:	/* Inf - x -> Inf */
112*0Sstevel@tonic-gate 		if (py->fpclass == fp_infinity) {
113*0Sstevel@tonic-gate 			fpu_error_nan(pfpsd, pz);	/* Inf - Inf -> NaN */
114*0Sstevel@tonic-gate 			pz->fpclass = fp_quiet;
115*0Sstevel@tonic-gate 		}
116*0Sstevel@tonic-gate 		return;
117*0Sstevel@tonic-gate 	case fp_zero:		/* 0 - 0 -> 0 */
118*0Sstevel@tonic-gate 		pz->sign = (pfpsd->fp_direction == fp_negative);
119*0Sstevel@tonic-gate 		return;
120*0Sstevel@tonic-gate 	default:
121*0Sstevel@tonic-gate 		if (py->fpclass == fp_zero)
122*0Sstevel@tonic-gate 			return;
123*0Sstevel@tonic-gate 	}
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/* x and y are both normal or subnormal. */
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (px->exponent < py->exponent) { /* Reverse. */
128*0Sstevel@tonic-gate 		pt = py;
129*0Sstevel@tonic-gate 		py = px;
130*0Sstevel@tonic-gate 		px = pt;
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	/* Now exp(x) >= exp(y). */
133*0Sstevel@tonic-gate 	pz->fpclass = px->fpclass;
134*0Sstevel@tonic-gate 	pz->sign = px->sign;
135*0Sstevel@tonic-gate 	pz->exponent = px->exponent;
136*0Sstevel@tonic-gate 	pz->rounded = 0;
137*0Sstevel@tonic-gate 	pz->sticky = 0;
138*0Sstevel@tonic-gate 	z = pz->significand;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (px->exponent == py->exponent) {	/* no pre-alignment required */
141*0Sstevel@tonic-gate 		c = 0;
142*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[3], px->significand[3],
143*0Sstevel@tonic-gate 				py->significand[3], c);
144*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[2], px->significand[2],
145*0Sstevel@tonic-gate 				py->significand[2], c);
146*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[1], px->significand[1],
147*0Sstevel@tonic-gate 				py->significand[1], c);
148*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[0], px->significand[0],
149*0Sstevel@tonic-gate 				py->significand[0], c);
150*0Sstevel@tonic-gate 		if ((z[0]|z[1]|z[2]|z[3]) == 0) {	/* exact zero result */
151*0Sstevel@tonic-gate 			pz->sign = (pfpsd->fp_direction == fp_negative);
152*0Sstevel@tonic-gate 			pz->fpclass = fp_zero;
153*0Sstevel@tonic-gate 			return;
154*0Sstevel@tonic-gate 		}
155*0Sstevel@tonic-gate 		if (z[0] >= 0x20000) {	/* sign reversal occurred */
156*0Sstevel@tonic-gate 			pz->sign = py->sign;
157*0Sstevel@tonic-gate 			c = 0;
158*0Sstevel@tonic-gate 			c = fpu_neg2wc(&z[3], z[3], c);
159*0Sstevel@tonic-gate 			c = fpu_neg2wc(&z[2], z[2], c);
160*0Sstevel@tonic-gate 			c = fpu_neg2wc(&z[1], z[1], c);
161*0Sstevel@tonic-gate 			c = fpu_neg2wc(&z[0], z[0], c);
162*0Sstevel@tonic-gate 		}
163*0Sstevel@tonic-gate 		fpu_normalize(pz);
164*0Sstevel@tonic-gate 		return;
165*0Sstevel@tonic-gate 	} else {		/* pre-alignment required */
166*0Sstevel@tonic-gate 		fpu_rightshift(py, pz->exponent - py->exponent - 1);
167*0Sstevel@tonic-gate 		r = py->rounded; 	/* rounded bit */
168*0Sstevel@tonic-gate 		s = py->sticky;		/* sticky bit */
169*0Sstevel@tonic-gate 		fpu_rightshift(py, 1);
170*0Sstevel@tonic-gate 		g = py->rounded;	/* guard bit */
171*0Sstevel@tonic-gate 		if (s != 0) r = (r == 0);
172*0Sstevel@tonic-gate 		if ((r|s) != 0) g = (g == 0); /* guard and rounded bits of z */
173*0Sstevel@tonic-gate 		c = ((g|r|s) != 0);
174*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[3], px->significand[3],
175*0Sstevel@tonic-gate 				py->significand[3], c);
176*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[2], px->significand[2],
177*0Sstevel@tonic-gate 				py->significand[2], c);
178*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[1], px->significand[1],
179*0Sstevel@tonic-gate 				py->significand[1], c);
180*0Sstevel@tonic-gate 		c = fpu_sub3wc(&z[0], px->significand[0],
181*0Sstevel@tonic-gate 				py->significand[0], c);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 		if (z[0] >= 0x10000) { 	/* don't need post-shifted */
184*0Sstevel@tonic-gate 			pz->sticky = s|r;
185*0Sstevel@tonic-gate 			pz->rounded = g;
186*0Sstevel@tonic-gate 		} else {		/* post-shifted left 1 bit */
187*0Sstevel@tonic-gate 			pz->sticky = s;
188*0Sstevel@tonic-gate 			pz->rounded = r;
189*0Sstevel@tonic-gate 			pz->significand[0] = (z[0]<<1)|((z[1]&0x80000000)>>31);
190*0Sstevel@tonic-gate 			pz->significand[1] = (z[1]<<1)|((z[2]&0x80000000)>>31);
191*0Sstevel@tonic-gate 			pz->significand[2] = (z[2]<<1)|((z[3]&0x80000000)>>31);
192*0Sstevel@tonic-gate 			pz->significand[3] = (z[3]<<1)|g;
193*0Sstevel@tonic-gate 			pz->exponent -= 1;
194*0Sstevel@tonic-gate 			if (z[0] < 0x10000) fpu_normalize(pz);
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		return;
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate void
_fp_add(pfpsd,px,py,pz)201*0Sstevel@tonic-gate _fp_add(pfpsd, px, py, pz)
202*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd;
203*0Sstevel@tonic-gate 	unpacked	*px, *py, *pz;
204*0Sstevel@tonic-gate {
205*0Sstevel@tonic-gate 	if (px->sign == py->sign)
206*0Sstevel@tonic-gate 		true_add(px, py, pz);
207*0Sstevel@tonic-gate 	else
208*0Sstevel@tonic-gate 		true_sub(pfpsd, px, py, pz);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate void
_fp_sub(pfpsd,px,py,pz)212*0Sstevel@tonic-gate _fp_sub(pfpsd, px, py, pz)
213*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd;
214*0Sstevel@tonic-gate 	unpacked	*px, *py, *pz;
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	if (py->fpclass < fp_quiet) py->sign = 1 - py->sign;
217*0Sstevel@tonic-gate 	if (px->sign == py->sign)
218*0Sstevel@tonic-gate 		true_add(px, py, pz);
219*0Sstevel@tonic-gate 	else
220*0Sstevel@tonic-gate 		true_sub(pfpsd, px, py, pz);
221*0Sstevel@tonic-gate }
222