1274958Sdim/* $NetBSD: fpu_div.c,v 1.5 2005/12/11 12:17:52 christos Exp $ */ 2274958Sdim 3353358Sdim/* 4353358Sdim * Copyright (c) 1992, 1993 5353358Sdim * The Regents of the University of California. All rights reserved. 6274958Sdim * 7274958Sdim * This software was developed by the Computer Systems Engineering group 8274958Sdim * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9274958Sdim * contributed to Berkeley. 10274958Sdim * 11274958Sdim * All advertising materials mentioning features or use of this software 12274958Sdim * must display the following acknowledgement: 13274958Sdim * This product includes software developed by the University of 14344779Sdim * California, Lawrence Berkeley Laboratory. 15274958Sdim * 16274958Sdim * Redistribution and use in source and binary forms, with or without 17274958Sdim * modification, are permitted provided that the following conditions 18274958Sdim * are met: 19274958Sdim * 1. Redistributions of source code must retain the above copyright 20274958Sdim * notice, this list of conditions and the following disclaimer. 21274958Sdim * 2. Redistributions in binary form must reproduce the above copyright 22274958Sdim * notice, this list of conditions and the following disclaimer in the 23274958Sdim * documentation and/or other materials provided with the distribution. 24274958Sdim * 3. Neither the name of the University nor the names of its contributors 25274958Sdim * may be used to endorse or promote products derived from this software 26274958Sdim * without specific prior written permission. 27274958Sdim * 28274958Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29274958Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30274958Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31274958Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32274958Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33274958Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34274958Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35274958Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36274958Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37274958Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38274958Sdim * SUCH DAMAGE. 39274958Sdim * 40274958Sdim * @(#)fpu_div.c 8.1 (Berkeley) 6/11/93 41274958Sdim */ 42274958Sdim 43274958Sdim/* 44274958Sdim * Perform an FPU divide (return x / y). 45274958Sdim */ 46274958Sdim 47274958Sdim#include <sys/cdefs.h> 48274958Sdim__KERNEL_RCSID(0, "$NetBSD: fpu_div.c,v 1.5 2005/12/11 12:17:52 christos Exp $"); 49274958Sdim 50274958Sdim#include <sys/types.h> 51274958Sdim 52274958Sdim#include <machine/reg.h> 53274958Sdim 54274958Sdim#include "fpu_arith.h" 55274958Sdim#include "fpu_emulate.h" 56274958Sdim 57341825Sdim/* 58274958Sdim * Division of normal numbers is done as follows: 59274958Sdim * 60274958Sdim * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e. 61274958Sdim * If X and Y are the mantissas (1.bbbb's), the quotient is then: 62274958Sdim * 63274958Sdim * q = (X / Y) * 2^((x exponent) - (y exponent)) 64274958Sdim * 65274958Sdim * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y) 66274958Sdim * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only 67274958Sdim * if X < Y. In that case, it will have to be shifted left one bit to 68274958Sdim * become a normal number, and the exponent decremented. Thus, the 69274958Sdim * desired exponent is: 70274958Sdim * 71274958Sdim * left_shift = x->fp_mant < y->fp_mant; 72360784Sdim * result_exp = x->fp_exp - y->fp_exp - left_shift; 73360784Sdim * 74360784Sdim * The quotient mantissa X/Y can then be computed one bit at a time 75274958Sdim * using the following algorithm: 76274958Sdim * 77274958Sdim * Q = 0; -- Initial quotient. 78274958Sdim * R = X; -- Initial remainder, 79274958Sdim * if (left_shift) -- but fixed up in advance. 80274958Sdim * R *= 2; 81274958Sdim * for (bit = FP_NMANT; --bit >= 0; R *= 2) { 82274958Sdim * if (R >= Y) { 83274958Sdim * Q |= 1 << bit; 84274958Sdim * R -= Y; 85274958Sdim * } 86341825Sdim * } 87274958Sdim * 88274958Sdim * The subtraction R -= Y always removes the uppermost bit from R (and 89274958Sdim * can sometimes remove additional lower-order 1 bits); this proof is 90274958Sdim * left to the reader. 91274958Sdim * 92274958Sdim * This loop correctly calculates the guard and round bits since they are 93274958Sdim * included in the expanded internal representation. The sticky bit 94274958Sdim * is to be set if and only if any other bits beyond guard and round 95360784Sdim * would be set. From the above it is obvious that this is true if and 96360784Sdim * only if the remainder R is nonzero when the loop terminates. 97360784Sdim * 98274958Sdim * Examining the loop above, we can see that the quotient Q is built 99274958Sdim * one bit at a time ``from the top down''. This means that we can 100274958Sdim * dispense with the multi-word arithmetic and just build it one word 101274958Sdim * at a time, writing each result word when it is done. 102274958Sdim * 103274958Sdim * Furthermore, since X and Y are both in [1.0,2.0), we know that, 104274958Sdim * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and 105274958Sdim * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1 106274958Sdim * set, and R can be set initially to either X - Y (when X >= Y) or 107274958Sdim * 2X - Y (when X < Y). In addition, comparing R and Y is difficult, 108274958Sdim * so we will simply calculate R - Y and see if that underflows. 109274958Sdim * This leads to the following revised version of the algorithm: 110274958Sdim * 111274958Sdim * R = X; 112274958Sdim * bit = FP_1; 113274958Sdim * D = R - Y; 114274958Sdim * if (D >= 0) { 115341825Sdim * result_exp = x->fp_exp - y->fp_exp; 116274958Sdim * R = D; 117274958Sdim * q = bit; 118274958Sdim * bit >>= 1; 119274958Sdim * } else { 120274958Sdim * result_exp = x->fp_exp - y->fp_exp - 1; 121274958Sdim * q = 0; 122274958Sdim * } 123274958Sdim * R <<= 1; 124274958Sdim * do { 125274958Sdim * D = R - Y; 126274958Sdim * if (D >= 0) { 127314564Sdim * q |= bit; 128274958Sdim * R = D; 129274958Sdim * } 130274958Sdim * R <<= 1; 131274958Sdim * } while ((bit >>= 1) != 0); 132274958Sdim * Q[0] = q; 133274958Sdim * for (i = 1; i < 4; i++) { 134274958Sdim * q = 0, bit = 1 << 31; 135274958Sdim * do { 136274958Sdim * D = R - Y; 137274958Sdim * if (D >= 0) { 138274958Sdim * q |= bit; 139274958Sdim * R = D; 140274958Sdim * } 141274958Sdim * R <<= 1; 142274958Sdim * } while ((bit >>= 1) != 0); 143274958Sdim * Q[i] = q; 144274958Sdim * } 145274958Sdim * 146274958Sdim * This can be refined just a bit further by moving the `R <<= 1' 147274958Sdim * calculations to the front of the do-loops and eliding the first one. 148274958Sdim * The process can be terminated immediately whenever R becomes 0, but 149274958Sdim * this is relatively rare, and we do not bother. 150274958Sdim */ 151274958Sdim 152274958Sdimstruct fpn * 153274958Sdimfpu_div(register struct fpemu *fe) 154274958Sdim{ 155274958Sdim register struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2; 156274958Sdim register u_int q, bit; 157274958Sdim register u_int r0, r1, r2, d0, d1, d2, y0, y1, y2; 158274958Sdim FPU_DECL_CARRY 159274958Sdim 160274958Sdim fe->fe_fpsr &= ~FPSR_EXCP; /* clear all exceptions */ 161274958Sdim 162274958Sdim /* 163274958Sdim * Since divide is not commutative, we cannot just use ORDER. 164274958Sdim * Check either operand for NaN first; if there is at least one, 165274958Sdim * order the signalling one (if only one) onto the right, then 166296417Sdim * return it. Otherwise we have the following cases: 167274958Sdim * 168274958Sdim * Inf / Inf = NaN, plus NV exception 169274958Sdim * Inf / num = Inf [i.e., return x] 170360784Sdim * Inf / 0 = Inf [i.e., return x] 171288943Sdim * 0 / Inf = 0 [i.e., return x] 172288943Sdim * 0 / num = 0 [i.e., return x] 173288943Sdim * 0 / 0 = NaN, plus NV exception 174274958Sdim * num / Inf = 0 175360784Sdim * num / num = num (do the divide) 176280031Sdim * num / 0 = Inf, plus DZ exception 177288943Sdim */ 178274958Sdim if (ISNAN(x) || ISNAN(y)) { 179274958Sdim ORDER(x, y); 180274958Sdim return (y); 181344779Sdim } 182341825Sdim if (ISINF(x) || ISZERO(x)) { 183274958Sdim if (x->fp_class == y->fp_class) 184274958Sdim return (fpu_newnan(fe)); 185274958Sdim return (x); 186274958Sdim } 187274958Sdim 188274958Sdim /* all results at this point use XOR of operand signs */ 189274958Sdim x->fp_sign ^= y->fp_sign; 190274958Sdim if (ISINF(y)) { 191274958Sdim x->fp_class = FPC_ZERO; 192274958Sdim return (x); 193274958Sdim } 194274958Sdim if (ISZERO(y)) { 195274958Sdim fe->fe_fpsr |= FPSR_DZ; 196274958Sdim x->fp_class = FPC_INF; 197274958Sdim return (x); 198274958Sdim } 199274958Sdim 200274958Sdim /* 201274958Sdim * Macros for the divide. See comments at top for algorithm. 202274958Sdim * Note that we expand R, D, and Y here. 203274958Sdim */ 204274958Sdim 205274958Sdim#define SUBTRACT /* D = R - Y */ \ 206274958Sdim FPU_SUBS(d2, r2, y2); \ 207274958Sdim FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0) 208274958Sdim 209274958Sdim#define NONNEGATIVE /* D >= 0 */ \ 210274958Sdim ((int)d0 >= 0) 211274958Sdim 212274958Sdim#ifdef FPU_SHL1_BY_ADD 213274958Sdim#define SHL1 /* R <<= 1 */ \ 214274958Sdim FPU_ADDS(r2, r2, r2); \ 215274958Sdim FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0) 216274958Sdim#else 217274958Sdim#define SHL1 \ 218274958Sdim r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \ 219274958Sdim r2 <<= 1 220274958Sdim#endif 221274958Sdim 222274958Sdim#define LOOP /* do ... while (bit >>= 1) */ \ 223274958Sdim do { \ 224274958Sdim SHL1; \ 225274958Sdim SUBTRACT; \ 226274958Sdim if (NONNEGATIVE) { \ 227274958Sdim q |= bit; \ 228274958Sdim r0 = d0, r1 = d1, r2 = d2; \ 229274958Sdim } \ 230274958Sdim } while ((bit >>= 1) != 0) 231274958Sdim 232274958Sdim#define WORD(r, i) /* calculate r->fp_mant[i] */ \ 233274958Sdim q = 0; \ 234274958Sdim bit = 1 << 31; \ 235274958Sdim LOOP; \ 236274958Sdim (x)->fp_mant[i] = q 237274958Sdim 238274958Sdim /* Setup. Note that we put our result in x. */ 239274958Sdim r0 = x->fp_mant[0]; 240274958Sdim r1 = x->fp_mant[1]; 241274958Sdim r2 = x->fp_mant[2]; 242274958Sdim y0 = y->fp_mant[0]; 243274958Sdim y1 = y->fp_mant[1]; 244274958Sdim y2 = y->fp_mant[2]; 245274958Sdim 246274958Sdim bit = FP_1; 247274958Sdim SUBTRACT; 248274958Sdim if (NONNEGATIVE) { 249274958Sdim x->fp_exp -= y->fp_exp; 250274958Sdim r0 = d0, r1 = d1, r2 = d2; 251274958Sdim q = bit; 252274958Sdim bit >>= 1; 253274958Sdim } else { 254274958Sdim x->fp_exp -= y->fp_exp + 1; 255274958Sdim q = 0; 256274958Sdim } 257274958Sdim LOOP; 258274958Sdim x->fp_mant[0] = q; 259274958Sdim WORD(x, 1); 260274958Sdim WORD(x, 2); 261274958Sdim x->fp_sticky = r0 | r1 | r2; 262274958Sdim 263353358Sdim return (x); 264353358Sdim} 265353358Sdim