xref: /original-bsd/sys/sparc/fpu/fpu_add.c (revision 602360b2)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)fpu_add.c	7.1 (Berkeley) 07/13/92
12  *
13  * from: $Header: fpu_add.c,v 1.3 92/06/17 18:11:43 mccanne Exp $
14  */
15 
16 /*
17  * Perform an FPU add (return x + y).
18  *
19  * To subtract, negate y and call add.
20  */
21 
22 #include "sys/types.h"
23 
24 #include "machine/reg.h"
25 
26 #include "fpu_arith.h"
27 #include "fpu_emu.h"
28 
29 struct fpn *
30 fpu_add(fe)
31 	register struct fpemu *fe;
32 {
33 	register struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
34 	register u_int r0, r1, r2, r3;
35 	register int rd;
36 
37 	/*
38 	 * Put the `heavier' operand on the right (see fpu_emu.h).
39 	 * Then we will have one of the following cases, taken in the
40 	 * following order:
41 	 *
42 	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
43 	 *	The result is y.
44 	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
45 	 *    case was taken care of earlier).
46 	 *	If x = -y, the result is NaN.  Otherwise the result
47 	 *	is y (an Inf of whichever sign).
48 	 *  - y is 0.  Implied: x = 0.
49 	 *	If x and y differ in sign (one positive, one negative),
50 	 *	the result is +0 except when rounding to -Inf.  If same:
51 	 *	+0 + +0 = +0; -0 + -0 = -0.
52 	 *  - x is 0.  Implied: y != 0.
53 	 *	Result is y.
54 	 *  - other.  Implied: both x and y are numbers.
55 	 *	Do addition a la Hennessey & Patterson.
56 	 */
57 	ORDER(x, y);
58 	if (ISNAN(y))
59 		return (y);
60 	if (ISINF(y)) {
61 		if (ISINF(x) && x->fp_sign != y->fp_sign)
62 			return (fpu_newnan(fe));
63 		return (y);
64 	}
65 	rd = ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK);
66 	if (ISZERO(y)) {
67 		if (rd != FSR_RD_RM)	/* only -0 + -0 gives -0 */
68 			y->fp_sign &= x->fp_sign;
69 		else			/* any -0 operand gives -0 */
70 			y->fp_sign |= x->fp_sign;
71 		return (y);
72 	}
73 	if (ISZERO(x))
74 		return (y);
75 	/*
76 	 * We really have two numbers to add, although their signs may
77 	 * differ.  Make the exponents match, by shifting the smaller
78 	 * number right (e.g., 1.011 => 0.1011) and increasing its
79 	 * exponent (2^3 => 2^4).  Note that we do not alter the exponents
80 	 * of x and y here.
81 	 */
82 	r = &fe->fe_f3;
83 	r->fp_class = FPC_NUM;
84 	if (x->fp_exp == y->fp_exp) {
85 		r->fp_exp = x->fp_exp;
86 		r->fp_sticky = 0;
87 	} else {
88 		if (x->fp_exp < y->fp_exp) {
89 			/*
90 			 * Try to avoid subtract case iii (see below).
91 			 * This also guarantees that x->fp_sticky = 0.
92 			 */
93 			SWAP(x, y);
94 		}
95 		/* now x->fp_exp > y->fp_exp */
96 		r->fp_exp = x->fp_exp;
97 		r->fp_sticky = fpu_shr(y, x->fp_exp - y->fp_exp);
98 	}
99 	r->fp_sign = x->fp_sign;
100 	if (x->fp_sign == y->fp_sign) {
101 		FPU_DECL_CARRY
102 
103 		/*
104 		 * The signs match, so we simply add the numbers.  The result
105 		 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
106 		 * 11.111...0).  If so, a single bit shift-right will fix it
107 		 * (but remember to adjust the exponent).
108 		 */
109 		/* r->fp_mant = x->fp_mant + y->fp_mant */
110 		FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
111 		FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
112 		FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
113 		FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
114 		if ((r->fp_mant[0] = r0) >= FP_2) {
115 			(void) fpu_shr(r, 1);
116 			r->fp_exp++;
117 		}
118 	} else {
119 		FPU_DECL_CARRY
120 
121 		/*
122 		 * The signs differ, so things are rather more difficult.
123 		 * H&P would have us negate the negative operand and add;
124 		 * this is the same as subtracting the negative operand.
125 		 * This is quite a headache.  Instead, we will subtract
126 		 * y from x, regardless of whether y itself is the negative
127 		 * operand.  When this is done one of three conditions will
128 		 * hold, depending on the magnitudes of x and y:
129 		 *   case i)   |x| > |y|.  The result is just x - y,
130 		 *	with x's sign, but it may need to be normalized.
131 		 *   case ii)  |x| = |y|.  The result is 0 (maybe -0)
132 		 *	so must be fixed up.
133 		 *   case iii) |x| < |y|.  We goofed; the result should
134 		 *	be (y - x), with the same sign as y.
135 		 * We could compare |x| and |y| here and avoid case iii,
136 		 * but that would take just as much work as the subtract.
137 		 * We can tell case iii has occurred by an overflow.
138 		 *
139 		 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
140 		 */
141 		/* r->fp_mant = x->fp_mant - y->fp_mant */
142 		FPU_SET_CARRY(y->fp_sticky);
143 		FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
144 		FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
145 		FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
146 		FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
147 		if (r0 < FP_2) {
148 			/* cases i and ii */
149 			if ((r0 | r1 | r2 | r3) == 0) {
150 				/* case ii */
151 				r->fp_class = FPC_ZERO;
152 				r->fp_sign = rd == FSR_RD_RM;
153 				return (r);
154 			}
155 		} else {
156 			/*
157 			 * Oops, case iii.  This can only occur when the
158 			 * exponents were equal, in which case neither
159 			 * x nor y have sticky bits set.  Flip the sign
160 			 * (to y's sign) and negate the result to get y - x.
161 			 */
162 #ifdef DIAGNOSTIC
163 			if (x->fp_exp != y->fp_exp || r->fp_sticky)
164 				panic("fpu_add");
165 #endif
166 			r->fp_sign = y->fp_sign;
167 			FPU_SUBS(r3, 0, r3);
168 			FPU_SUBCS(r2, 0, r2);
169 			FPU_SUBCS(r1, 0, r1);
170 			FPU_SUBC(r0, 0, r0);
171 		}
172 		r->fp_mant[3] = r3;
173 		r->fp_mant[2] = r2;
174 		r->fp_mant[1] = r1;
175 		r->fp_mant[0] = r0;
176 		if (r0 < FP_1)
177 			fpu_norm(r);
178 	}
179 	return (r);
180 }
181