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