1176491Smarcel/*	$NetBSD: fpu_add.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */
2176491Smarcel
3176491Smarcel/*
4176491Smarcel * Copyright (c) 1992, 1993
5176491Smarcel *	The Regents of the University of California.  All rights reserved.
6176491Smarcel *
7176491Smarcel * This software was developed by the Computer Systems Engineering group
8176491Smarcel * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9176491Smarcel * contributed to Berkeley.
10176491Smarcel *
11176491Smarcel * All advertising materials mentioning features or use of this software
12176491Smarcel * must display the following acknowledgement:
13176491Smarcel *	This product includes software developed by the University of
14176491Smarcel *	California, Lawrence Berkeley Laboratory.
15176491Smarcel *
16176491Smarcel * Redistribution and use in source and binary forms, with or without
17176491Smarcel * modification, are permitted provided that the following conditions
18176491Smarcel * are met:
19176491Smarcel * 1. Redistributions of source code must retain the above copyright
20176491Smarcel *    notice, this list of conditions and the following disclaimer.
21176491Smarcel * 2. Redistributions in binary form must reproduce the above copyright
22176491Smarcel *    notice, this list of conditions and the following disclaimer in the
23176491Smarcel *    documentation and/or other materials provided with the distribution.
24176491Smarcel * 3. Neither the name of the University nor the names of its contributors
25176491Smarcel *    may be used to endorse or promote products derived from this software
26176491Smarcel *    without specific prior written permission.
27176491Smarcel *
28176491Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29176491Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30176491Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31176491Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32176491Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33176491Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34176491Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35176491Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36176491Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37176491Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38176491Smarcel * SUCH DAMAGE.
39176491Smarcel *
40176491Smarcel *	@(#)fpu_add.c	8.1 (Berkeley) 6/11/93
41176491Smarcel */
42176491Smarcel
43176491Smarcel/*
44176491Smarcel * Perform an FPU add (return x + y).
45176491Smarcel *
46176491Smarcel * To subtract, negate y and call add.
47176491Smarcel */
48176491Smarcel
49176491Smarcel#include <sys/cdefs.h>
50176491Smarcel__FBSDID("$FreeBSD$");
51176491Smarcel
52178030Sgrehan#include <sys/types.h>
53176491Smarcel#include <sys/systm.h>
54176491Smarcel
55176491Smarcel#include <machine/fpu.h>
56176491Smarcel#include <machine/ieeefp.h>
57176491Smarcel#include <machine/reg.h>
58176491Smarcel
59176491Smarcel#include <powerpc/fpu/fpu_arith.h>
60176491Smarcel#include <powerpc/fpu/fpu_emu.h>
61176491Smarcel
62176491Smarcelstruct fpn *
63176491Smarcelfpu_add(struct fpemu *fe)
64176491Smarcel{
65176491Smarcel	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
66176491Smarcel	u_int r0, r1, r2, r3;
67176491Smarcel	int rd;
68176491Smarcel
69176491Smarcel	/*
70176491Smarcel	 * Put the `heavier' operand on the right (see fpu_emu.h).
71176491Smarcel	 * Then we will have one of the following cases, taken in the
72176491Smarcel	 * following order:
73176491Smarcel	 *
74176491Smarcel	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
75176491Smarcel	 *	The result is y.
76176491Smarcel	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
77176491Smarcel	 *    case was taken care of earlier).
78176491Smarcel	 *	If x = -y, the result is NaN.  Otherwise the result
79176491Smarcel	 *	is y (an Inf of whichever sign).
80176491Smarcel	 *  - y is 0.  Implied: x = 0.
81176491Smarcel	 *	If x and y differ in sign (one positive, one negative),
82176491Smarcel	 *	the result is +0 except when rounding to -Inf.  If same:
83176491Smarcel	 *	+0 + +0 = +0; -0 + -0 = -0.
84176491Smarcel	 *  - x is 0.  Implied: y != 0.
85176491Smarcel	 *	Result is y.
86176491Smarcel	 *  - other.  Implied: both x and y are numbers.
87176491Smarcel	 *	Do addition a la Hennessey & Patterson.
88176491Smarcel	 */
89176491Smarcel	DPRINTF(FPE_REG, ("fpu_add:\n"));
90176491Smarcel	DUMPFPN(FPE_REG, x);
91176491Smarcel	DUMPFPN(FPE_REG, y);
92176491Smarcel	DPRINTF(FPE_REG, ("=>\n"));
93176491Smarcel	ORDER(x, y);
94176491Smarcel	if (ISNAN(y)) {
95176491Smarcel		fe->fe_cx |= FPSCR_VXSNAN;
96176491Smarcel		DUMPFPN(FPE_REG, y);
97176491Smarcel		return (y);
98176491Smarcel	}
99176491Smarcel	if (ISINF(y)) {
100176491Smarcel		if (ISINF(x) && x->fp_sign != y->fp_sign) {
101176491Smarcel			fe->fe_cx |= FPSCR_VXISI;
102176491Smarcel			return (fpu_newnan(fe));
103176491Smarcel		}
104176491Smarcel		DUMPFPN(FPE_REG, y);
105176491Smarcel		return (y);
106176491Smarcel	}
107176491Smarcel	rd = ((fe->fe_fpscr) & FPSCR_RN);
108176491Smarcel	if (ISZERO(y)) {
109176491Smarcel		if (rd != FP_RM)	/* only -0 + -0 gives -0 */
110176491Smarcel			y->fp_sign &= x->fp_sign;
111176491Smarcel		else			/* any -0 operand gives -0 */
112176491Smarcel			y->fp_sign |= x->fp_sign;
113176491Smarcel		DUMPFPN(FPE_REG, y);
114176491Smarcel		return (y);
115176491Smarcel	}
116176491Smarcel	if (ISZERO(x)) {
117176491Smarcel		DUMPFPN(FPE_REG, y);
118176491Smarcel		return (y);
119176491Smarcel	}
120176491Smarcel	/*
121176491Smarcel	 * We really have two numbers to add, although their signs may
122176491Smarcel	 * differ.  Make the exponents match, by shifting the smaller
123176491Smarcel	 * number right (e.g., 1.011 => 0.1011) and increasing its
124176491Smarcel	 * exponent (2^3 => 2^4).  Note that we do not alter the exponents
125176491Smarcel	 * of x and y here.
126176491Smarcel	 */
127176491Smarcel	r = &fe->fe_f3;
128176491Smarcel	r->fp_class = FPC_NUM;
129176491Smarcel	if (x->fp_exp == y->fp_exp) {
130176491Smarcel		r->fp_exp = x->fp_exp;
131176491Smarcel		r->fp_sticky = 0;
132176491Smarcel	} else {
133176491Smarcel		if (x->fp_exp < y->fp_exp) {
134176491Smarcel			/*
135176491Smarcel			 * Try to avoid subtract case iii (see below).
136176491Smarcel			 * This also guarantees that x->fp_sticky = 0.
137176491Smarcel			 */
138176491Smarcel			SWAP(x, y);
139176491Smarcel		}
140176491Smarcel		/* now x->fp_exp > y->fp_exp */
141176491Smarcel		r->fp_exp = x->fp_exp;
142176491Smarcel		r->fp_sticky = fpu_shr(y, x->fp_exp - y->fp_exp);
143176491Smarcel	}
144176491Smarcel	r->fp_sign = x->fp_sign;
145176491Smarcel	if (x->fp_sign == y->fp_sign) {
146176491Smarcel		FPU_DECL_CARRY
147176491Smarcel
148176491Smarcel		/*
149176491Smarcel		 * The signs match, so we simply add the numbers.  The result
150176491Smarcel		 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
151176491Smarcel		 * 11.111...0).  If so, a single bit shift-right will fix it
152176491Smarcel		 * (but remember to adjust the exponent).
153176491Smarcel		 */
154176491Smarcel		/* r->fp_mant = x->fp_mant + y->fp_mant */
155176491Smarcel		FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
156176491Smarcel		FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
157176491Smarcel		FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
158176491Smarcel		FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
159176491Smarcel		if ((r->fp_mant[0] = r0) >= FP_2) {
160176491Smarcel			(void) fpu_shr(r, 1);
161176491Smarcel			r->fp_exp++;
162176491Smarcel		}
163176491Smarcel	} else {
164176491Smarcel		FPU_DECL_CARRY
165176491Smarcel
166176491Smarcel		/*
167176491Smarcel		 * The signs differ, so things are rather more difficult.
168176491Smarcel		 * H&P would have us negate the negative operand and add;
169176491Smarcel		 * this is the same as subtracting the negative operand.
170176491Smarcel		 * This is quite a headache.  Instead, we will subtract
171176491Smarcel		 * y from x, regardless of whether y itself is the negative
172176491Smarcel		 * operand.  When this is done one of three conditions will
173176491Smarcel		 * hold, depending on the magnitudes of x and y:
174176491Smarcel		 *   case i)   |x| > |y|.  The result is just x - y,
175176491Smarcel		 *	with x's sign, but it may need to be normalized.
176176491Smarcel		 *   case ii)  |x| = |y|.  The result is 0 (maybe -0)
177176491Smarcel		 *	so must be fixed up.
178176491Smarcel		 *   case iii) |x| < |y|.  We goofed; the result should
179176491Smarcel		 *	be (y - x), with the same sign as y.
180176491Smarcel		 * We could compare |x| and |y| here and avoid case iii,
181176491Smarcel		 * but that would take just as much work as the subtract.
182176491Smarcel		 * We can tell case iii has occurred by an overflow.
183176491Smarcel		 *
184176491Smarcel		 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
185176491Smarcel		 */
186176491Smarcel		/* r->fp_mant = x->fp_mant - y->fp_mant */
187176491Smarcel		FPU_SET_CARRY(y->fp_sticky);
188176491Smarcel		FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
189176491Smarcel		FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
190176491Smarcel		FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
191176491Smarcel		FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
192176491Smarcel		if (r0 < FP_2) {
193176491Smarcel			/* cases i and ii */
194176491Smarcel			if ((r0 | r1 | r2 | r3) == 0) {
195176491Smarcel				/* case ii */
196176491Smarcel				r->fp_class = FPC_ZERO;
197176491Smarcel				r->fp_sign = rd == FP_RM;
198176491Smarcel				return (r);
199176491Smarcel			}
200176491Smarcel		} else {
201176491Smarcel			/*
202176491Smarcel			 * Oops, case iii.  This can only occur when the
203176491Smarcel			 * exponents were equal, in which case neither
204176491Smarcel			 * x nor y have sticky bits set.  Flip the sign
205176491Smarcel			 * (to y's sign) and negate the result to get y - x.
206176491Smarcel			 */
207176491Smarcel#ifdef DIAGNOSTIC
208176491Smarcel			if (x->fp_exp != y->fp_exp || r->fp_sticky)
209176491Smarcel				panic("fpu_add");
210176491Smarcel#endif
211176491Smarcel			r->fp_sign = y->fp_sign;
212176491Smarcel			FPU_SUBS(r3, 0, r3);
213176491Smarcel			FPU_SUBCS(r2, 0, r2);
214176491Smarcel			FPU_SUBCS(r1, 0, r1);
215176491Smarcel			FPU_SUBC(r0, 0, r0);
216176491Smarcel		}
217176491Smarcel		r->fp_mant[3] = r3;
218176491Smarcel		r->fp_mant[2] = r2;
219176491Smarcel		r->fp_mant[1] = r1;
220176491Smarcel		r->fp_mant[0] = r0;
221176491Smarcel		if (r0 < FP_1)
222176491Smarcel			fpu_norm(r);
223176491Smarcel	}
224176491Smarcel	DUMPFPN(FPE_REG, r);
225176491Smarcel	return (r);
226176491Smarcel}
227