1 /* $OpenBSD: fpu_add.c,v 1.1 2003/07/21 18:41:30 jason 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 * $NetBSD: fpu_add.c,v 1.3 1996/03/14 19:41:52 christos Exp $ 46 */ 47 48 #include <sys/cdefs.h> 49 #if 0 50 __FBSDID("$FreeBSD: src/lib/libc/sparc64/fpu/fpu_add.c,v 1.4 2002/04/27 21:56:28 jake Exp $"); 51 #endif 52 53 /* 54 * Perform an FPU add (return x + y). 55 * 56 * To subtract, negate y and call add. 57 */ 58 59 #include <sys/param.h> 60 61 #include <machine/frame.h> 62 #include <machine/fsr.h> 63 #include <machine/instr.h> 64 65 #include "fpu_arith.h" 66 #include "fpu_emu.h" 67 #include "fpu_extern.h" 68 69 struct fpn * 70 __fpu_add(fe) 71 struct fpemu *fe; 72 { 73 struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2, *r; 74 u_int r0, r1, r2, r3; 75 int rd; 76 77 /* 78 * Put the `heavier' operand on the right (see fpu_emu.h). 79 * Then we will have one of the following cases, taken in the 80 * following order: 81 * 82 * - y = NaN. Implied: if only one is a signalling NaN, y is. 83 * The result is y. 84 * - y = Inf. Implied: x != NaN (is 0, number, or Inf: the NaN 85 * case was taken care of earlier). 86 * If x = -y, the result is NaN. Otherwise the result 87 * is y (an Inf of whichever sign). 88 * - y is 0. Implied: x = 0. 89 * If x and y differ in sign (one positive, one negative), 90 * the result is +0 except when rounding to -Inf. If same: 91 * +0 + +0 = +0; -0 + -0 = -0. 92 * - x is 0. Implied: y != 0. 93 * Result is y. 94 * - other. Implied: both x and y are numbers. 95 * Do addition a la Hennessey & Patterson. 96 */ 97 ORDER(x, y); 98 if (ISNAN(y)) 99 return (y); 100 if (ISINF(y)) { 101 if (ISINF(x) && x->fp_sign != y->fp_sign) 102 return (__fpu_newnan(fe)); 103 return (y); 104 } 105 rd = FSR_GET_RD(fe->fe_fsr); 106 if (ISZERO(y)) { 107 if (rd != FSR_RD_RM) /* only -0 + -0 gives -0 */ 108 y->fp_sign &= x->fp_sign; 109 else /* any -0 operand gives -0 */ 110 y->fp_sign |= x->fp_sign; 111 return (y); 112 } 113 if (ISZERO(x)) 114 return (y); 115 /* 116 * We really have two numbers to add, although their signs may 117 * differ. Make the exponents match, by shifting the smaller 118 * number right (e.g., 1.011 => 0.1011) and increasing its 119 * exponent (2^3 => 2^4). Note that we do not alter the exponents 120 * of x and y here. 121 */ 122 r = &fe->fe_f3; 123 r->fp_class = FPC_NUM; 124 if (x->fp_exp == y->fp_exp) { 125 r->fp_exp = x->fp_exp; 126 r->fp_sticky = 0; 127 } else { 128 if (x->fp_exp < y->fp_exp) { 129 /* 130 * Try to avoid subtract case iii (see below). 131 * This also guarantees that x->fp_sticky = 0. 132 */ 133 SWAP(x, y); 134 } 135 /* now x->fp_exp > y->fp_exp */ 136 r->fp_exp = x->fp_exp; 137 r->fp_sticky = __fpu_shr(y, x->fp_exp - y->fp_exp); 138 } 139 r->fp_sign = x->fp_sign; 140 if (x->fp_sign == y->fp_sign) { 141 FPU_DECL_CARRY 142 143 /* 144 * The signs match, so we simply add the numbers. The result 145 * may be `supernormal' (as big as 1.111...1 + 1.111...1, or 146 * 11.111...0). If so, a single bit shift-right will fix it 147 * (but remember to adjust the exponent). 148 */ 149 /* r->fp_mant = x->fp_mant + y->fp_mant */ 150 FPU_ADDS(r->fp_mant[3], x->fp_mant[3], y->fp_mant[3]); 151 FPU_ADDCS(r->fp_mant[2], x->fp_mant[2], y->fp_mant[2]); 152 FPU_ADDCS(r->fp_mant[1], x->fp_mant[1], y->fp_mant[1]); 153 FPU_ADDC(r0, x->fp_mant[0], y->fp_mant[0]); 154 if ((r->fp_mant[0] = r0) >= FP_2) { 155 (void) __fpu_shr(r, 1); 156 r->fp_exp++; 157 } 158 } else { 159 FPU_DECL_CARRY 160 161 /* 162 * The signs differ, so things are rather more difficult. 163 * H&P would have us negate the negative operand and add; 164 * this is the same as subtracting the negative operand. 165 * This is quite a headache. Instead, we will subtract 166 * y from x, regardless of whether y itself is the negative 167 * operand. When this is done one of three conditions will 168 * hold, depending on the magnitudes of x and y: 169 * case i) |x| > |y|. The result is just x - y, 170 * with x's sign, but it may need to be normalized. 171 * case ii) |x| = |y|. The result is 0 (maybe -0) 172 * so must be fixed up. 173 * case iii) |x| < |y|. We goofed; the result should 174 * be (y - x), with the same sign as y. 175 * We could compare |x| and |y| here and avoid case iii, 176 * but that would take just as much work as the subtract. 177 * We can tell case iii has occurred by an overflow. 178 * 179 * N.B.: since x->fp_exp >= y->fp_exp, x->fp_sticky = 0. 180 */ 181 /* r->fp_mant = x->fp_mant - y->fp_mant */ 182 FPU_SET_CARRY(y->fp_sticky); 183 FPU_SUBCS(r3, x->fp_mant[3], y->fp_mant[3]); 184 FPU_SUBCS(r2, x->fp_mant[2], y->fp_mant[2]); 185 FPU_SUBCS(r1, x->fp_mant[1], y->fp_mant[1]); 186 FPU_SUBC(r0, x->fp_mant[0], y->fp_mant[0]); 187 if (r0 < FP_2) { 188 /* cases i and ii */ 189 if ((r0 | r1 | r2 | r3) == 0) { 190 /* case ii */ 191 r->fp_class = FPC_ZERO; 192 r->fp_sign = rd == FSR_RD_RM; 193 return (r); 194 } 195 } else { 196 /* 197 * Oops, case iii. This can only occur when the 198 * exponents were equal, in which case neither 199 * x nor y have sticky bits set. Flip the sign 200 * (to y's sign) and negate the result to get y - x. 201 */ 202 #ifdef DIAGNOSTIC 203 if (x->fp_exp != y->fp_exp || r->fp_sticky) 204 __utrap_panic("fpu_add"); 205 #endif 206 r->fp_sign = y->fp_sign; 207 FPU_SUBS(r3, 0, r3); 208 FPU_SUBCS(r2, 0, r2); 209 FPU_SUBCS(r1, 0, r1); 210 FPU_SUBC(r0, 0, r0); 211 } 212 r->fp_mant[3] = r3; 213 r->fp_mant[2] = r2; 214 r->fp_mant[1] = r1; 215 r->fp_mant[0] = r0; 216 if (r0 < FP_1) 217 __fpu_norm(r); 218 } 219 return (r); 220 } 221