xref: /freebsd/sys/powerpc/fpu/fpu_add.c (revision 36712a94975f5bd0d26c85377283b49a2369c82f)
1 /*	$NetBSD: fpu_add.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This software was developed by the Computer Systems Engineering group
10  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11  * contributed to Berkeley.
12  *
13  * All advertising materials mentioning features or use of this software
14  * must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Lawrence Berkeley Laboratory.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *	@(#)fpu_add.c	8.1 (Berkeley) 6/11/93
43  */
44 
45 /*
46  * Perform an FPU add (return x + y).
47  *
48  * To subtract, negate y and call add.
49  */
50 
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53 
54 #include <sys/types.h>
55 #include <sys/systm.h>
56 
57 #include <machine/fpu.h>
58 #include <machine/ieeefp.h>
59 #include <machine/reg.h>
60 
61 #include <powerpc/fpu/fpu_arith.h>
62 #include <powerpc/fpu/fpu_emu.h>
63 
64 struct fpn *
65 fpu_add(struct fpemu *fe)
66 {
67 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
68 	u_int r0, r1, r2, r3;
69 	int rd;
70 
71 	/*
72 	 * Put the `heavier' operand on the right (see fpu_emu.h).
73 	 * Then we will have one of the following cases, taken in the
74 	 * following order:
75 	 *
76 	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
77 	 *	The result is y.
78 	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
79 	 *    case was taken care of earlier).
80 	 *	If x = -y, the result is NaN.  Otherwise the result
81 	 *	is y (an Inf of whichever sign).
82 	 *  - y is 0.  Implied: x = 0.
83 	 *	If x and y differ in sign (one positive, one negative),
84 	 *	the result is +0 except when rounding to -Inf.  If same:
85 	 *	+0 + +0 = +0; -0 + -0 = -0.
86 	 *  - x is 0.  Implied: y != 0.
87 	 *	Result is y.
88 	 *  - other.  Implied: both x and y are numbers.
89 	 *	Do addition a la Hennessey & Patterson.
90 	 */
91 	DPRINTF(FPE_REG, ("fpu_add:\n"));
92 	DUMPFPN(FPE_REG, x);
93 	DUMPFPN(FPE_REG, y);
94 	DPRINTF(FPE_REG, ("=>\n"));
95 	ORDER(x, y);
96 	if (ISNAN(y)) {
97 		fe->fe_cx |= FPSCR_VXSNAN;
98 		DUMPFPN(FPE_REG, y);
99 		return (y);
100 	}
101 	if (ISINF(y)) {
102 		if (ISINF(x) && x->fp_sign != y->fp_sign) {
103 			fe->fe_cx |= FPSCR_VXISI;
104 			return (fpu_newnan(fe));
105 		}
106 		DUMPFPN(FPE_REG, y);
107 		return (y);
108 	}
109 	rd = ((fe->fe_fpscr) & FPSCR_RN);
110 	if (ISZERO(y)) {
111 		if (rd != FP_RM)	/* only -0 + -0 gives -0 */
112 			y->fp_sign &= x->fp_sign;
113 		else			/* any -0 operand gives -0 */
114 			y->fp_sign |= x->fp_sign;
115 		DUMPFPN(FPE_REG, y);
116 		return (y);
117 	}
118 	if (ISZERO(x)) {
119 		DUMPFPN(FPE_REG, y);
120 		return (y);
121 	}
122 	/*
123 	 * We really have two numbers to add, although their signs may
124 	 * differ.  Make the exponents match, by shifting the smaller
125 	 * number right (e.g., 1.011 => 0.1011) and increasing its
126 	 * exponent (2^3 => 2^4).  Note that we do not alter the exponents
127 	 * of x and y here.
128 	 */
129 	r = &fe->fe_f3;
130 	r->fp_class = FPC_NUM;
131 	if (x->fp_exp == y->fp_exp) {
132 		r->fp_exp = x->fp_exp;
133 		r->fp_sticky = 0;
134 	} else {
135 		if (x->fp_exp < y->fp_exp) {
136 			/*
137 			 * Try to avoid subtract case iii (see below).
138 			 * This also guarantees that x->fp_sticky = 0.
139 			 */
140 			SWAP(x, y);
141 		}
142 		/* now x->fp_exp > y->fp_exp */
143 		r->fp_exp = x->fp_exp;
144 		r->fp_sticky = fpu_shr(y, x->fp_exp - y->fp_exp);
145 	}
146 	r->fp_sign = x->fp_sign;
147 	if (x->fp_sign == y->fp_sign) {
148 		FPU_DECL_CARRY
149 
150 		/*
151 		 * The signs match, so we simply add the numbers.  The result
152 		 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
153 		 * 11.111...0).  If so, a single bit shift-right will fix it
154 		 * (but remember to adjust the exponent).
155 		 */
156 		/* r->fp_mant = x->fp_mant + y->fp_mant */
157 		FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
158 		FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
159 		FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
160 		FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
161 		if ((r->fp_mant[0] = r0) >= FP_2) {
162 			(void) fpu_shr(r, 1);
163 			r->fp_exp++;
164 		}
165 	} else {
166 		FPU_DECL_CARRY
167 
168 		/*
169 		 * The signs differ, so things are rather more difficult.
170 		 * H&P would have us negate the negative operand and add;
171 		 * this is the same as subtracting the negative operand.
172 		 * This is quite a headache.  Instead, we will subtract
173 		 * y from x, regardless of whether y itself is the negative
174 		 * operand.  When this is done one of three conditions will
175 		 * hold, depending on the magnitudes of x and y:
176 		 *   case i)   |x| > |y|.  The result is just x - y,
177 		 *	with x's sign, but it may need to be normalized.
178 		 *   case ii)  |x| = |y|.  The result is 0 (maybe -0)
179 		 *	so must be fixed up.
180 		 *   case iii) |x| < |y|.  We goofed; the result should
181 		 *	be (y - x), with the same sign as y.
182 		 * We could compare |x| and |y| here and avoid case iii,
183 		 * but that would take just as much work as the subtract.
184 		 * We can tell case iii has occurred by an overflow.
185 		 *
186 		 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
187 		 */
188 		/* r->fp_mant = x->fp_mant - y->fp_mant */
189 		FPU_SET_CARRY(y->fp_sticky);
190 		FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
191 		FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
192 		FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
193 		FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
194 		if (r0 < FP_2) {
195 			/* cases i and ii */
196 			if ((r0 | r1 | r2 | r3) == 0) {
197 				/* case ii */
198 				r->fp_class = FPC_ZERO;
199 				r->fp_sign = rd == FP_RM;
200 				return (r);
201 			}
202 		} else {
203 			/*
204 			 * Oops, case iii.  This can only occur when the
205 			 * exponents were equal, in which case neither
206 			 * x nor y have sticky bits set.  Flip the sign
207 			 * (to y's sign) and negate the result to get y - x.
208 			 */
209 #ifdef DIAGNOSTIC
210 			if (x->fp_exp != y->fp_exp || r->fp_sticky)
211 				panic("fpu_add");
212 #endif
213 			r->fp_sign = y->fp_sign;
214 			FPU_SUBS(r3, 0, r3);
215 			FPU_SUBCS(r2, 0, r2);
216 			FPU_SUBCS(r1, 0, r1);
217 			FPU_SUBC(r0, 0, r0);
218 		}
219 		r->fp_mant[3] = r3;
220 		r->fp_mant[2] = r2;
221 		r->fp_mant[1] = r1;
222 		r->fp_mant[0] = r0;
223 		if (r0 < FP_1)
224 			fpu_norm(r);
225 	}
226 	DUMPFPN(FPE_REG, r);
227 	return (r);
228 }
229