/* $NetBSD: fpu_div.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Lawrence Berkeley Laboratory.16*17* Redistribution and use in source and binary forms, with or without18* modification, are permitted provided that the following conditions19* are met:20* 1. Redistributions of source code must retain the above copyright21* notice, this list of conditions and the following disclaimer.22* 2. Redistributions in binary form must reproduce the above copyright23* notice, this list of conditions and the following disclaimer in the24* documentation and/or other materials provided with the distribution.25* 3. Neither the name of the University nor the names of its contributors26* may be used to endorse or promote products derived from this software27* without specific prior written permission.28*29* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND30* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*/4142/*43* Perform an FPU divide (return x / y).44*/4546#include <sys/types.h>47#include <sys/systm.h>4849#include <machine/fpu.h>5051#include <powerpc/fpu/fpu_arith.h>52#include <powerpc/fpu/fpu_emu.h>5354/*55* Division of normal numbers is done as follows:56*57* x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.58* If X and Y are the mantissas (1.bbbb's), the quotient is then:59*60* q = (X / Y) * 2^((x exponent) - (y exponent))61*62* Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)63* will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only64* if X < Y. In that case, it will have to be shifted left one bit to65* become a normal number, and the exponent decremented. Thus, the66* desired exponent is:67*68* left_shift = x->fp_mant < y->fp_mant;69* result_exp = x->fp_exp - y->fp_exp - left_shift;70*71* The quotient mantissa X/Y can then be computed one bit at a time72* using the following algorithm:73*74* Q = 0; -- Initial quotient.75* R = X; -- Initial remainder,76* if (left_shift) -- but fixed up in advance.77* R *= 2;78* for (bit = FP_NMANT; --bit >= 0; R *= 2) {79* if (R >= Y) {80* Q |= 1 << bit;81* R -= Y;82* }83* }84*85* The subtraction R -= Y always removes the uppermost bit from R (and86* can sometimes remove additional lower-order 1 bits); this proof is87* left to the reader.88*89* This loop correctly calculates the guard and round bits since they are90* included in the expanded internal representation. The sticky bit91* is to be set if and only if any other bits beyond guard and round92* would be set. From the above it is obvious that this is true if and93* only if the remainder R is nonzero when the loop terminates.94*95* Examining the loop above, we can see that the quotient Q is built96* one bit at a time ``from the top down''. This means that we can97* dispense with the multi-word arithmetic and just build it one word98* at a time, writing each result word when it is done.99*100* Furthermore, since X and Y are both in [1.0,2.0), we know that,101* initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and102* is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1103* set, and R can be set initially to either X - Y (when X >= Y) or104* 2X - Y (when X < Y). In addition, comparing R and Y is difficult,105* so we will simply calculate R - Y and see if that underflows.106* This leads to the following revised version of the algorithm:107*108* R = X;109* bit = FP_1;110* D = R - Y;111* if (D >= 0) {112* result_exp = x->fp_exp - y->fp_exp;113* R = D;114* q = bit;115* bit >>= 1;116* } else {117* result_exp = x->fp_exp - y->fp_exp - 1;118* q = 0;119* }120* R <<= 1;121* do {122* D = R - Y;123* if (D >= 0) {124* q |= bit;125* R = D;126* }127* R <<= 1;128* } while ((bit >>= 1) != 0);129* Q[0] = q;130* for (i = 1; i < 4; i++) {131* q = 0, bit = 1 << 31;132* do {133* D = R - Y;134* if (D >= 0) {135* q |= bit;136* R = D;137* }138* R <<= 1;139* } while ((bit >>= 1) != 0);140* Q[i] = q;141* }142*143* This can be refined just a bit further by moving the `R <<= 1'144* calculations to the front of the do-loops and eliding the first one.145* The process can be terminated immediately whenever R becomes 0, but146* this is relatively rare, and we do not bother.147*/148149struct fpn *150fpu_div(struct fpemu *fe)151{152struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;153u_int q, bit;154u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;155FPU_DECL_CARRY156157/*158* Since divide is not commutative, we cannot just use ORDER.159* Check either operand for NaN first; if there is at least one,160* order the signalling one (if only one) onto the right, then161* return it. Otherwise we have the following cases:162*163* Inf / Inf = NaN, plus NV exception164* Inf / num = Inf [i.e., return x]165* Inf / 0 = Inf [i.e., return x]166* 0 / Inf = 0 [i.e., return x]167* 0 / num = 0 [i.e., return x]168* 0 / 0 = NaN, plus NV exception169* num / Inf = 0170* num / num = num (do the divide)171* num / 0 = Inf, plus DZ exception172*/173DPRINTF(FPE_REG, ("fpu_div:\n"));174DUMPFPN(FPE_REG, x);175DUMPFPN(FPE_REG, y);176DPRINTF(FPE_REG, ("=>\n"));177if (ISNAN(x) || ISNAN(y)) {178ORDER(x, y);179fe->fe_cx |= FPSCR_VXSNAN;180DUMPFPN(FPE_REG, y);181return (y);182}183/*184* Need to split the following out cause they generate different185* exceptions.186*/187if (ISINF(x)) {188if (x->fp_class == y->fp_class) {189fe->fe_cx |= FPSCR_VXIDI;190return (fpu_newnan(fe));191}192DUMPFPN(FPE_REG, x);193return (x);194}195if (ISZERO(x)) {196fe->fe_cx |= FPSCR_ZX;197if (x->fp_class == y->fp_class) {198fe->fe_cx |= FPSCR_VXZDZ;199return (fpu_newnan(fe));200}201DUMPFPN(FPE_REG, x);202return (x);203}204205/* all results at this point use XOR of operand signs */206x->fp_sign ^= y->fp_sign;207if (ISINF(y)) {208x->fp_class = FPC_ZERO;209DUMPFPN(FPE_REG, x);210return (x);211}212if (ISZERO(y)) {213fe->fe_cx = FPSCR_ZX;214x->fp_class = FPC_INF;215DUMPFPN(FPE_REG, x);216return (x);217}218219/*220* Macros for the divide. See comments at top for algorithm.221* Note that we expand R, D, and Y here.222*/223224#define SUBTRACT /* D = R - Y */ \225FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \226FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)227228#define NONNEGATIVE /* D >= 0 */ \229((int)d0 >= 0)230231#ifdef FPU_SHL1_BY_ADD232#define SHL1 /* R <<= 1 */ \233FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \234FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)235#else236#define SHL1 \237r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \238r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1239#endif240241#define LOOP /* do ... while (bit >>= 1) */ \242do { \243SHL1; \244SUBTRACT; \245if (NONNEGATIVE) { \246q |= bit; \247r0 = d0, r1 = d1, r2 = d2, r3 = d3; \248} \249} while ((bit >>= 1) != 0)250251#define WORD(r, i) /* calculate r->fp_mant[i] */ \252q = 0; \253bit = 1 << 31; \254LOOP; \255(x)->fp_mant[i] = q256257/* Setup. Note that we put our result in x. */258r0 = x->fp_mant[0];259r1 = x->fp_mant[1];260r2 = x->fp_mant[2];261r3 = x->fp_mant[3];262y0 = y->fp_mant[0];263y1 = y->fp_mant[1];264y2 = y->fp_mant[2];265y3 = y->fp_mant[3];266267bit = FP_1;268SUBTRACT;269if (NONNEGATIVE) {270x->fp_exp -= y->fp_exp;271r0 = d0, r1 = d1, r2 = d2, r3 = d3;272q = bit;273bit >>= 1;274} else {275x->fp_exp -= y->fp_exp + 1;276q = 0;277}278LOOP;279x->fp_mant[0] = q;280WORD(x, 1);281WORD(x, 2);282WORD(x, 3);283x->fp_sticky = r0 | r1 | r2 | r3;284285DUMPFPN(FPE_REG, x);286return (x);287}288289290