1184610Salfred/* $NetBSD: fpu_add.c,v 1.6 2005/12/11 12:17:52 christos Exp $ */ 2184610Salfred 3184610Salfred/* 4184610Salfred * Copyright (c) 1992, 1993 5184610Salfred * The Regents of the University of California. All rights reserved. 6184610Salfred * 7184610Salfred * This software was developed by the Computer Systems Engineering group 8184610Salfred * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9184610Salfred * contributed to Berkeley. 10184610Salfred * 11184610Salfred * All advertising materials mentioning features or use of this software 12184610Salfred * must display the following acknowledgement: 13184610Salfred * This product includes software developed by the University of 14184610Salfred * California, Lawrence Berkeley Laboratory. 15184610Salfred * 16184610Salfred * Redistribution and use in source and binary forms, with or without 17184610Salfred * modification, are permitted provided that the following conditions 18184610Salfred * are met: 19184610Salfred * 1. Redistributions of source code must retain the above copyright 20184610Salfred * notice, this list of conditions and the following disclaimer. 21184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 22184610Salfred * notice, this list of conditions and the following disclaimer in the 23184610Salfred * documentation and/or other materials provided with the distribution. 24184610Salfred * 3. Neither the name of the University nor the names of its contributors 25184610Salfred * may be used to endorse or promote products derived from this software 26184610Salfred * without specific prior written permission. 27184610Salfred * 28184610Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38184610Salfred * SUCH DAMAGE. 39184610Salfred * 40184610Salfred * @(#)fpu_add.c 8.1 (Berkeley) 6/11/93 41184610Salfred */ 42184610Salfred 43184610Salfred/* 44184610Salfred * Perform an FPU add (return x + y). 45184610Salfred * 46184610Salfred * To subtract, negate y and call add. 47184610Salfred */ 48184610Salfred 49184610Salfred#include <sys/cdefs.h> 50184610Salfred__KERNEL_RCSID(0, "$NetBSD: fpu_add.c,v 1.6 2005/12/11 12:17:52 christos Exp $"); 51188942Sthompsa 52188942Sthompsa#include <sys/types.h> 53188942Sthompsa#include <sys/systm.h> 54188942Sthompsa 55184610Salfred#include <machine/reg.h> 56184610Salfred 57184610Salfred#include "fpu_arith.h" 58188942Sthompsa#include "fpu_emulate.h" 59188942Sthompsa 60188942Sthompsastruct fpn * 61188942Sthompsafpu_add(register struct fpemu *fe) 62188942Sthompsa{ 63188942Sthompsa register struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r; 64188942Sthompsa register u_int r0, r1, r2; 65188942Sthompsa register int rd; 66188942Sthompsa 67184610Salfred /* 68188942Sthompsa * Put the `heavier' operand on the right (see fpu_emu.h). 69184610Salfred * Then we will have one of the following cases, taken in the 70184610Salfred * following order: 71184610Salfred * 72184610Salfred * - y = NaN. Implied: if only one is a signalling NaN, y is. 73184610Salfred * The result is y. 74184610Salfred * - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN 75184610Salfred * case was taken care of earlier). 76184610Salfred * If x = -y, the result is NaN. Otherwise the result 77184610Salfred * is y (an Inf of whichever sign). 78184610Salfred * - y is 0. Implied: x = 0. 79184610Salfred * If x and y differ in sign (one positive, one negative), 80184610Salfred * the result is +0 except when rounding to -Inf. If same: 81184610Salfred * +0 + +0 = +0; -0 + -0 = -0. 82184610Salfred * - x is 0. Implied: y != 0. 83184610Salfred * Result is y. 84184610Salfred * - other. Implied: both x and y are numbers. 85184610Salfred * Do addition a la Hennessey & Patterson. 86184610Salfred */ 87184610Salfred ORDER(x, y); 88184610Salfred if (ISNAN(y)) 89192502Sthompsa return (y); 90192502Sthompsa if (ISINF(y)) { 91184610Salfred if (ISINF(x) && x->fp_sign != y->fp_sign) 92184610Salfred return (fpu_newnan(fe)); 93184610Salfred return (y); 94184610Salfred } 95184610Salfred rd = (fe->fe_fpcr & FPCR_ROUND); 96184610Salfred if (ISZERO(y)) { 97184610Salfred if (rd != FPCR_MINF) /* only -0 + -0 gives -0 */ 98184610Salfred y->fp_sign &= x->fp_sign; 99184610Salfred else /* any -0 operand gives -0 */ 100184610Salfred y->fp_sign |= x->fp_sign; 101184610Salfred return (y); 102184610Salfred } 103184610Salfred if (ISZERO(x)) 104184610Salfred return (y); 105184610Salfred /* 106184610Salfred * We really have two numbers to add, although their signs may 107184610Salfred * differ. Make the exponents match, by shifting the smaller 108184610Salfred * number right (e.g., 1.011 => 0.1011) and increasing its 109184610Salfred * exponent (2^3 => 2^4). Note that we do not alter the exponents 110184610Salfred * of x and y here. 111184610Salfred */ 112184610Salfred r = &fe->fe_f3; 113184610Salfred r->fp_class = FPC_NUM; 114184610Salfred if (x->fp_exp == y->fp_exp) { 115184610Salfred r->fp_exp = x->fp_exp; 116184610Salfred r->fp_sticky = 0; 117184610Salfred } else { 118187259Sthompsa if (x->fp_exp < y->fp_exp) { 119187259Sthompsa /* 120187259Sthompsa * Try to avoid subtract case iii (see below). 121187259Sthompsa * This also guarantees that x->fp_sticky = 0. 122187259Sthompsa */ 123187259Sthompsa SWAP(x, y); 124187259Sthompsa } 125184610Salfred /* now x->fp_exp > y->fp_exp */ 126184610Salfred r->fp_exp = x->fp_exp; 127184610Salfred r->fp_sticky = fpu_shr(y, x->fp_exp - y->fp_exp); 128184610Salfred } 129184610Salfred r->fp_sign = x->fp_sign; 130184610Salfred if (x->fp_sign == y->fp_sign) { 131184610Salfred FPU_DECL_CARRY 132184610Salfred 133184610Salfred /* 134184610Salfred * The signs match, so we simply add the numbers. The result 135184610Salfred * may be `supernormal' (as big as 1.111...1 + 1.111...1, or 136184610Salfred * 11.111...0). If so, a single bit shift-right will fix it 137184610Salfred * (but remember to adjust the exponent). 138184610Salfred */ 139184610Salfred /* r->fp_mant = x->fp_mant + y->fp_mant */ 140184610Salfred FPU_ADDS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]); 141184610Salfred FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]); 142184610Salfred FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]); 143184610Salfred if ((r->fp_mant[0] = r0) >= FP_2) { 144184610Salfred (void) fpu_shr(r, 1); 145184610Salfred r->fp_exp++; 146184610Salfred } 147184610Salfred } else { 148184610Salfred FPU_DECL_CARRY 149184610Salfred 150184610Salfred /* 151184610Salfred * The signs differ, so things are rather more difficult. 152184610Salfred * H&P would have us negate the negative operand and add; 153184610Salfred * this is the same as subtracting the negative operand. 154184610Salfred * This is quite a headache. Instead, we will subtract 155184610Salfred * y from x, regardless of whether y itself is the negative 156184610Salfred * operand. When this is done one of three conditions will 157184610Salfred * hold, depending on the magnitudes of x and y: 158184610Salfred * case i) |x| > |y|. The result is just x - y, 159184610Salfred * with x's sign, but it may need to be normalized. 160184610Salfred * case ii) |x| = |y|. The result is 0 (maybe -0) 161184610Salfred * so must be fixed up. 162184610Salfred * case iii) |x| < |y|. We goofed; the result should 163184610Salfred * be (y - x), with the same sign as y. 164184610Salfred * We could compare |x| and |y| here and avoid case iii, 165184610Salfred * but that would take just as much work as the subtract. 166184610Salfred * We can tell case iii has occurred by an overflow. 167184610Salfred * 168184610Salfred * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0. 169184610Salfred */ 170184610Salfred /* r->fp_mant = x->fp_mant - y->fp_mant */ 171184610Salfred FPU_SET_CARRY(y->fp_sticky); 172184610Salfred FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]); 173184610Salfred FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]); 174184610Salfred FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]); 175184610Salfred if (r0 < FP_2) { 176184610Salfred /* cases i and ii */ 177184610Salfred if ((r0 | r1 | r2) == 0) { 178184610Salfred /* case ii */ 179184610Salfred r->fp_class = FPC_ZERO; 180184610Salfred r->fp_sign = (rd == FPCR_MINF); 181184610Salfred return (r); 182184610Salfred } 183184610Salfred } else { 184184610Salfred /* 185184610Salfred * Oops, case iii. This can only occur when the 186184610Salfred * exponents were equal, in which case neither 187184610Salfred * x nor y have sticky bits set. Flip the sign 188184610Salfred * (to y's sign) and negate the result to get y - x. 189184610Salfred */ 190184610Salfred#ifdef DIAGNOSTIC 191184610Salfred if (x->fp_exp != y->fp_exp || r->fp_sticky) 192184610Salfred panic("fpu_add"); 193184610Salfred#endif 194184610Salfred r->fp_sign = y->fp_sign; 195184610Salfred FPU_SUBS(r2, 0, r2); 196184610Salfred FPU_SUBCS(r1, 0, r1); 197184610Salfred FPU_SUBC(r0, 0, r0); 198184610Salfred } 199184610Salfred r->fp_mant[2] = r2; 200184610Salfred r->fp_mant[1] = r1; 201184610Salfred r->fp_mant[0] = r0; 202184610Salfred if (r0 < FP_1) 203184610Salfred fpu_norm(r); 204184610Salfred } 205184610Salfred return (r); 206184610Salfred} 207184610Salfred