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