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