xref: /openbsd-src/sys/arch/sparc64/fpu/fpu_add.c (revision 4b64ca3ec48c786968cbb21e835d04b75f1b53c9)
1*4b64ca3eSmiod /*	$OpenBSD: fpu_add.c,v 1.5 2024/03/29 21:08:10 miod Exp $	*/
2839f47eaSjason /*	$NetBSD: fpu_add.c,v 1.3 1996/03/14 19:41:52 christos Exp $ */
3839f47eaSjason 
4839f47eaSjason /*
5839f47eaSjason  * Copyright (c) 1992, 1993
6839f47eaSjason  *	The Regents of the University of California.  All rights reserved.
7839f47eaSjason  *
8839f47eaSjason  * This software was developed by the Computer Systems Engineering group
9839f47eaSjason  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10839f47eaSjason  * contributed to Berkeley.
11839f47eaSjason  *
12839f47eaSjason  * All advertising materials mentioning features or use of this software
13839f47eaSjason  * must display the following acknowledgement:
14839f47eaSjason  *	This product includes software developed by the University of
15839f47eaSjason  *	California, Lawrence Berkeley Laboratory.
16839f47eaSjason  *
17839f47eaSjason  * Redistribution and use in source and binary forms, with or without
18839f47eaSjason  * modification, are permitted provided that the following conditions
19839f47eaSjason  * are met:
20839f47eaSjason  * 1. Redistributions of source code must retain the above copyright
21839f47eaSjason  *    notice, this list of conditions and the following disclaimer.
22839f47eaSjason  * 2. Redistributions in binary form must reproduce the above copyright
23839f47eaSjason  *    notice, this list of conditions and the following disclaimer in the
24839f47eaSjason  *    documentation and/or other materials provided with the distribution.
2529295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
26839f47eaSjason  *    may be used to endorse or promote products derived from this software
27839f47eaSjason  *    without specific prior written permission.
28839f47eaSjason  *
29839f47eaSjason  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30839f47eaSjason  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31839f47eaSjason  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32839f47eaSjason  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33839f47eaSjason  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34839f47eaSjason  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35839f47eaSjason  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36839f47eaSjason  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37839f47eaSjason  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38839f47eaSjason  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39839f47eaSjason  * SUCH DAMAGE.
40839f47eaSjason  *
41839f47eaSjason  *	@(#)fpu_add.c	8.1 (Berkeley) 6/11/93
42839f47eaSjason  */
43839f47eaSjason 
44839f47eaSjason /*
45839f47eaSjason  * Perform an FPU add (return x + y).
46839f47eaSjason  *
47839f47eaSjason  * To subtract, negate y and call add.
48839f47eaSjason  */
49839f47eaSjason 
50839f47eaSjason #include <sys/types.h>
51839f47eaSjason #ifdef DIAGNOSTIC
52839f47eaSjason #include <sys/systm.h>
53839f47eaSjason #endif
54839f47eaSjason 
55*4b64ca3eSmiod #include <machine/fsr.h>
56839f47eaSjason #include <machine/reg.h>
57839f47eaSjason #include <machine/instr.h>
58839f47eaSjason 
59839f47eaSjason #include <sparc64/fpu/fpu_arith.h>
60839f47eaSjason #include <sparc64/fpu/fpu_emu.h>
61839f47eaSjason #include <sparc64/fpu/fpu_extern.h>
62839f47eaSjason 
63839f47eaSjason struct fpn *
fpu_add(struct fpemu * fe)642c7a42e9Smiod fpu_add(struct fpemu *fe)
65839f47eaSjason {
662c7a42e9Smiod 	struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r;
672c7a42e9Smiod 	u_int r0, r1, r2, r3;
682c7a42e9Smiod 	int rd;
69839f47eaSjason 
70839f47eaSjason 	/*
71839f47eaSjason 	 * Put the `heavier' operand on the right (see fpu_emu.h).
72839f47eaSjason 	 * Then we will have one of the following cases, taken in the
73839f47eaSjason 	 * following order:
74839f47eaSjason 	 *
75839f47eaSjason 	 *  - y = NaN.  Implied: if only one is a signalling NaN, y is.
76839f47eaSjason 	 *	The result is y.
77839f47eaSjason 	 *  - y = Inf.  Implied: x != NaN (is 0, number, or Inf: the NaN
78839f47eaSjason 	 *    case was taken care of earlier).
79839f47eaSjason 	 *	If x = -y, the result is NaN.  Otherwise the result
80839f47eaSjason 	 *	is y (an Inf of whichever sign).
81839f47eaSjason 	 *  - y is 0.  Implied: x = 0.
82839f47eaSjason 	 *	If x and y differ in sign (one positive, one negative),
83839f47eaSjason 	 *	the result is +0 except when rounding to -Inf.  If same:
84839f47eaSjason 	 *	+0 + +0 = +0; -0 + -0 = -0.
85839f47eaSjason 	 *  - x is 0.  Implied: y != 0.
86839f47eaSjason 	 *	Result is y.
87839f47eaSjason 	 *  - other.  Implied: both x and y are numbers.
88839f47eaSjason 	 *	Do addition a la Hennessey & Patterson.
89839f47eaSjason 	 */
90839f47eaSjason 	ORDER(x, y);
91839f47eaSjason 	if (ISNAN(y))
92839f47eaSjason 		return (y);
93839f47eaSjason 	if (ISINF(y)) {
94839f47eaSjason 		if (ISINF(x) && x->fp_sign != y->fp_sign)
95839f47eaSjason 			return (fpu_newnan(fe));
96839f47eaSjason 		return (y);
97839f47eaSjason 	}
98839f47eaSjason 	rd = ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK);
99839f47eaSjason 	if (ISZERO(y)) {
100839f47eaSjason 		if (rd != FSR_RD_RM)	/* only -0 + -0 gives -0 */
101839f47eaSjason 			y->fp_sign &= x->fp_sign;
102839f47eaSjason 		else			/* any -0 operand gives -0 */
103839f47eaSjason 			y->fp_sign |= x->fp_sign;
104839f47eaSjason 		return (y);
105839f47eaSjason 	}
106839f47eaSjason 	if (ISZERO(x))
107839f47eaSjason 		return (y);
108839f47eaSjason 	/*
109839f47eaSjason 	 * We really have two numbers to add, although their signs may
110839f47eaSjason 	 * differ.  Make the exponents match, by shifting the smaller
111839f47eaSjason 	 * number right (e.g., 1.011 => 0.1011) and increasing its
112839f47eaSjason 	 * exponent (2^3 => 2^4).  Note that we do not alter the exponents
113839f47eaSjason 	 * of x and y here.
114839f47eaSjason 	 */
115839f47eaSjason 	r = &fe->fe_f3;
116839f47eaSjason 	r->fp_class = FPC_NUM;
117839f47eaSjason 	if (x->fp_exp == y->fp_exp) {
118839f47eaSjason 		r->fp_exp = x->fp_exp;
119839f47eaSjason 		r->fp_sticky = 0;
120839f47eaSjason 	} else {
121839f47eaSjason 		if (x->fp_exp < y->fp_exp) {
122839f47eaSjason 			/*
123839f47eaSjason 			 * Try to avoid subtract case iii (see below).
124839f47eaSjason 			 * This also guarantees that x->fp_sticky = 0.
125839f47eaSjason 			 */
126839f47eaSjason 			SWAP(x, y);
127839f47eaSjason 		}
128839f47eaSjason 		/* now x->fp_exp > y->fp_exp */
129839f47eaSjason 		r->fp_exp = x->fp_exp;
130839f47eaSjason 		r->fp_sticky = fpu_shr(y, x->fp_exp - y->fp_exp);
131839f47eaSjason 	}
132839f47eaSjason 	r->fp_sign = x->fp_sign;
133839f47eaSjason 	if (x->fp_sign == y->fp_sign) {
134839f47eaSjason 		FPU_DECL_CARRY
135839f47eaSjason 
136839f47eaSjason 		/*
137839f47eaSjason 		 * The signs match, so we simply add the numbers.  The result
138839f47eaSjason 		 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or
139839f47eaSjason 		 * 11.111...0).  If so, a single bit shift-right will fix it
140839f47eaSjason 		 * (but remember to adjust the exponent).
141839f47eaSjason 		 */
142839f47eaSjason 		/* r->fp_mant = x->fp_mant + y->fp_mant */
143839f47eaSjason 		FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]);
144839f47eaSjason 		FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]);
145839f47eaSjason 		FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]);
146839f47eaSjason 		FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]);
147839f47eaSjason 		if ((r->fp_mant[0] = r0) >= FP_2) {
148839f47eaSjason 			(void) fpu_shr(r, 1);
149839f47eaSjason 			r->fp_exp++;
150839f47eaSjason 		}
151839f47eaSjason 	} else {
152839f47eaSjason 		FPU_DECL_CARRY
153839f47eaSjason 
154839f47eaSjason 		/*
155839f47eaSjason 		 * The signs differ, so things are rather more difficult.
156839f47eaSjason 		 * H&P would have us negate the negative operand and add;
157839f47eaSjason 		 * this is the same as subtracting the negative operand.
158839f47eaSjason 		 * This is quite a headache.  Instead, we will subtract
159839f47eaSjason 		 * y from x, regardless of whether y itself is the negative
160839f47eaSjason 		 * operand.  When this is done one of three conditions will
161839f47eaSjason 		 * hold, depending on the magnitudes of x and y:
162839f47eaSjason 		 *   case i)   |x| > |y|.  The result is just x - y,
163839f47eaSjason 		 *	with x's sign, but it may need to be normalized.
164839f47eaSjason 		 *   case ii)  |x| = |y|.  The result is 0 (maybe -0)
165839f47eaSjason 		 *	so must be fixed up.
166839f47eaSjason 		 *   case iii) |x| < |y|.  We goofed; the result should
167839f47eaSjason 		 *	be (y - x), with the same sign as y.
168839f47eaSjason 		 * We could compare |x| and |y| here and avoid case iii,
169839f47eaSjason 		 * but that would take just as much work as the subtract.
170839f47eaSjason 		 * We can tell case iii has occurred by an overflow.
171839f47eaSjason 		 *
172839f47eaSjason 		 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
173839f47eaSjason 		 */
174839f47eaSjason 		/* r->fp_mant = x->fp_mant - y->fp_mant */
175839f47eaSjason 		FPU_SET_CARRY(y->fp_sticky);
176839f47eaSjason 		FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]);
177839f47eaSjason 		FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]);
178839f47eaSjason 		FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]);
179839f47eaSjason 		FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]);
180839f47eaSjason 		if (r0 < FP_2) {
181839f47eaSjason 			/* cases i and ii */
182839f47eaSjason 			if ((r0 | r1 | r2 | r3) == 0) {
183839f47eaSjason 				/* case ii */
184839f47eaSjason 				r->fp_class = FPC_ZERO;
185839f47eaSjason 				r->fp_sign = rd == FSR_RD_RM;
186839f47eaSjason 				return (r);
187839f47eaSjason 			}
188839f47eaSjason 		} else {
189839f47eaSjason 			/*
190839f47eaSjason 			 * Oops, case iii.  This can only occur when the
191839f47eaSjason 			 * exponents were equal, in which case neither
192839f47eaSjason 			 * x nor y have sticky bits set.  Flip the sign
193839f47eaSjason 			 * (to y's sign) and negate the result to get y - x.
194839f47eaSjason 			 */
195839f47eaSjason #ifdef DIAGNOSTIC
196839f47eaSjason 			if (x->fp_exp != y->fp_exp || r->fp_sticky)
197839f47eaSjason 				panic("fpu_add");
198839f47eaSjason #endif
199839f47eaSjason 			r->fp_sign = y->fp_sign;
200839f47eaSjason 			FPU_SUBS(r3, 0, r3);
201839f47eaSjason 			FPU_SUBCS(r2, 0, r2);
202839f47eaSjason 			FPU_SUBCS(r1, 0, r1);
203839f47eaSjason 			FPU_SUBC(r0, 0, r0);
204839f47eaSjason 		}
205839f47eaSjason 		r->fp_mant[3] = r3;
206839f47eaSjason 		r->fp_mant[2] = r2;
207839f47eaSjason 		r->fp_mant[1] = r1;
208839f47eaSjason 		r->fp_mant[0] = r0;
209839f47eaSjason 		if (r0 < FP_1)
210839f47eaSjason 			fpu_norm(r);
211839f47eaSjason 	}
212839f47eaSjason 	return (r);
213839f47eaSjason }
214