* Copyright (c) 1992 The Regents of the University of California.
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratories.
* %sccs.include.redist.c%
* @(#)fpu_add.c 7.3 (Berkeley) %G%
* from: $Header: fpu_add.c,v 1.3 92/06/17 18:11:43 mccanne Exp $
* Perform an FPU add (return x + y).
* To subtract, negate y and call add.
#include <sparc/fpu/fpu_arith.h>
#include <sparc/fpu/fpu_emu.h>
register struct fpemu
*fe
;
register struct fpn
*x
= &fe
->fe_f1
, *y
= &fe
->fe_f2
, *r
;
register u_int r0
, r1
, r2
, r3
;
* Put the `heavier' operand on the right (see fpu_emu.h).
* Then we will have one of the following cases, taken in the
* - y = NaN. Implied: if only one is a signalling NaN, y is.
* - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN
* case was taken care of earlier).
* If x = -y, the result is NaN. Otherwise the result
* is y (an Inf of whichever sign).
* - y is 0. Implied: x = 0.
* If x and y differ in sign (one positive, one negative),
* the result is +0 except when rounding to -Inf. If same:
* +0 + +0 = +0; -0 + -0 = -0.
* - x is 0. Implied: y != 0.
* - other. Implied: both x and y are numbers.
* Do addition a la Hennessey & Patterson.
if (ISINF(x
) && x
->fp_sign
!= y
->fp_sign
)
rd
= ((fe
->fe_fsr
>> FSR_RD_SHIFT
) & FSR_RD_MASK
);
if (rd
!= FSR_RD_RM
) /* only -0 + -0 gives -0 */
y
->fp_sign
&= x
->fp_sign
;
else /* any -0 operand gives -0 */
y
->fp_sign
|= x
->fp_sign
;
* We really have two numbers to add, although their signs may
* differ. Make the exponents match, by shifting the smaller
* number right (e.g., 1.011 => 0.1011) and increasing its
* exponent (2^3 => 2^4). Note that we do not alter the exponents
if (x
->fp_exp
== y
->fp_exp
) {
if (x
->fp_exp
< y
->fp_exp
) {
* Try to avoid subtract case iii (see below).
* This also guarantees that x->fp_sticky = 0.
/* now x->fp_exp > y->fp_exp */
r
->fp_sticky
= fpu_shr(y
, x
->fp_exp
- y
->fp_exp
);
if (x
->fp_sign
== y
->fp_sign
) {
* The signs match, so we simply add the numbers. The result
* may be `supernormal' (as big as 1.111...1 + 1.111...1, or
* 11.111...0). If so, a single bit shift-right will fix it
* (but remember to adjust the exponent).
/* r->fp_mant = x->fp_mant + y->fp_mant */
FPU_ADDS(r
->fp_mant
[3], x
->fp_mant
[3], y
->fp_mant
[3]);
FPU_ADDCS(r
->fp_mant
[2], x
->fp_mant
[2], y
->fp_mant
[2]);
FPU_ADDCS(r
->fp_mant
[1], x
->fp_mant
[1], y
->fp_mant
[1]);
FPU_ADDC(r0
, x
->fp_mant
[0], y
->fp_mant
[0]);
if ((r
->fp_mant
[0] = r0
) >= FP_2
) {
* The signs differ, so things are rather more difficult.
* H&P would have us negate the negative operand and add;
* this is the same as subtracting the negative operand.
* This is quite a headache. Instead, we will subtract
* y from x, regardless of whether y itself is the negative
* operand. When this is done one of three conditions will
* hold, depending on the magnitudes of x and y:
* case i) |x| > |y|. The result is just x - y,
* with x's sign, but it may need to be normalized.
* case ii) |x| = |y|. The result is 0 (maybe -0)
* case iii) |x| < |y|. We goofed; the result should
* be (y - x), with the same sign as y.
* We could compare |x| and |y| here and avoid case iii,
* but that would take just as much work as the subtract.
* We can tell case iii has occurred by an overflow.
* N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0.
/* r->fp_mant = x->fp_mant - y->fp_mant */
FPU_SET_CARRY(y
->fp_sticky
);
FPU_SUBCS(r3
, x
->fp_mant
[3], y
->fp_mant
[3]);
FPU_SUBCS(r2
, x
->fp_mant
[2], y
->fp_mant
[2]);
FPU_SUBCS(r1
, x
->fp_mant
[1], y
->fp_mant
[1]);
FPU_SUBC(r0
, x
->fp_mant
[0], y
->fp_mant
[0]);
if ((r0
| r1
| r2
| r3
) == 0) {
r
->fp_sign
= rd
== FSR_RD_RM
;
* Oops, case iii. This can only occur when the
* exponents were equal, in which case neither
* x nor y have sticky bits set. Flip the sign
* (to y's sign) and negate the result to get y - x.
if (x
->fp_exp
!= y
->fp_exp
|| r
->fp_sticky
)