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