Home | History | Annotate | Line # | Download | only in fpe
fpu_rem.c revision 1.7
      1 /*	$NetBSD: fpu_rem.c,v 1.7 2009/03/14 14:46:01 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995  Ken Nakata
      5  *	All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the author nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)fpu_rem.c	10/24/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: fpu_rem.c,v 1.7 2009/03/14 14:46:01 dsl Exp $");
     36 
     37 #include <sys/types.h>
     38 #include <sys/signal.h>
     39 #include <machine/frame.h>
     40 
     41 #include "fpu_emulate.h"
     42 
     43 /*
     44  *       ALGORITHM
     45  *
     46  *       Step 1.  Save and strip signs of X and Y: signX := sign(X),
     47  *                signY := sign(Y), X := *X*, Y := *Y*,
     48  *                signQ := signX EOR signY. Record whether MOD or REM
     49  *                is requested.
     50  *
     51  *       Step 2.  Set L := expo(X)-expo(Y), k := 0, Q := 0.
     52  *                If (L < 0) then
     53  *                   R := X, go to Step 4.
     54  *                else
     55  *                   R := 2^(-L)X, j := L.
     56  *                endif
     57  *
     58  *       Step 3.  Perform MOD(X,Y)
     59  *            3.1 If R = Y, go to Step 9.
     60  *            3.2 If R > Y, then { R := R - Y, Q := Q + 1}
     61  *            3.3 If j = 0, go to Step 4.
     62  *            3.4 k := k + 1, j := j - 1, Q := 2Q, R := 2R. Go to
     63  *                Step 3.1.
     64  *
     65  *       Step 4.  At this point, R = X - QY = MOD(X,Y). Set
     66  *                Last_Subtract := false (used in Step 7 below). If
     67  *                MOD is requested, go to Step 6.
     68  *
     69  *       Step 5.  R = MOD(X,Y), but REM(X,Y) is requested.
     70  *            5.1 If R < Y/2, then R = MOD(X,Y) = REM(X,Y). Go to
     71  *                Step 6.
     72  *            5.2 If R > Y/2, then { set Last_Subtract := true,
     73  *                Q := Q + 1, Y := signY*Y }. Go to Step 6.
     74  *            5.3 This is the tricky case of R = Y/2. If Q is odd,
     75  *                then { Q := Q + 1, signX := -signX }.
     76  *
     77  *       Step 6.  R := signX*R.
     78  *
     79  *       Step 7.  If Last_Subtract = true, R := R - Y.
     80  *
     81  *       Step 8.  Return signQ, last 7 bits of Q, and R as required.
     82  *
     83  *       Step 9.  At this point, R = 2^(-j)*X - Q Y = Y. Thus,
     84  *                X = 2^(j)*(Q+1)Y. set Q := 2^(j)*(Q+1),
     85  *                R := 0. Return signQ, last 7 bits of Q, and R.
     86  */
     87 
     88 static struct fpn * __fpu_modrem(struct fpemu *fe, int modrem);
     89 
     90 static struct fpn *
     91 __fpu_modrem(fe, modrem)
     92      struct fpemu *fe;
     93      int modrem;
     94 {
     95     static struct fpn X, Y;
     96     struct fpn *x, *y, *r;
     97     u_int signX, signY, signQ;
     98     int j, k, l, q;
     99     int Last_Subtract;
    100 
    101     CPYFPN(&X, &fe->fe_f1);
    102     CPYFPN(&Y, &fe->fe_f2);
    103     x = &X;
    104     y = &Y;
    105     r = &fe->fe_f2;
    106 
    107     /*
    108      * Step 1
    109      */
    110     signX = x->fp_sign;
    111     signY = y->fp_sign;
    112     signQ = (signX ^ signY);
    113     x->fp_sign = y->fp_sign = 0;
    114 
    115     /*
    116      * Step 2
    117      */
    118     l = x->fp_exp - y->fp_exp;
    119     k = 0;
    120     q = 0;
    121     if (l >= 0) {
    122 	CPYFPN(r, x);
    123 	r->fp_exp -= l;
    124 	j = l;
    125 
    126 	/*
    127 	 * Step 3
    128 	 */
    129 	while (y->fp_exp != r->fp_exp || y->fp_mant[0] != r->fp_mant[0] ||
    130 	       y->fp_mant[1] != r->fp_mant[1] ||
    131 	       y->fp_mant[2] != r->fp_mant[2]) {
    132 
    133 	    /* Step 3.2 */
    134 	    if (y->fp_exp < r->fp_exp || y->fp_mant[0] < r->fp_mant[0] ||
    135 		y->fp_mant[1] < r->fp_mant[1] ||
    136 		y->fp_mant[2] < r->fp_mant[2]) {
    137 		CPYFPN(&fe->fe_f1, r);
    138 		CPYFPN(&fe->fe_f2, y);
    139 		fe->fe_f2.fp_sign = 1;
    140 		r = fpu_add(fe);
    141 		q++;
    142 	    }
    143 
    144 	    /* Step 3.3 */
    145 	    if (j == 0)
    146 		goto Step4;
    147 
    148 	    /* Step 3.4 */
    149 	    k++;
    150 	    j--;
    151 	    q += q;
    152 	    r->fp_exp++;
    153 	}
    154 	/* Step 9 */
    155 	goto Step9;
    156     }
    157  Step4:
    158     Last_Subtract = 0;
    159     if (modrem == 0)
    160 	goto Step6;
    161 
    162     /*
    163      * Step 5
    164      */
    165     /* Step 5.1 */
    166     if (r->fp_exp + 1 < y->fp_exp ||
    167 	(r->fp_exp + 1 == y->fp_exp &&
    168 	 (r->fp_mant[0] < y->fp_mant[0] || r->fp_mant[1] < y->fp_mant[1] ||
    169 	  r->fp_mant[2] < y->fp_mant[2])))
    170 	/* if r < y/2 */
    171 	goto Step6;
    172     /* Step 5.2 */
    173     if (r->fp_exp + 1 != y->fp_exp ||
    174 	r->fp_mant[0] != y->fp_mant[0] || r->fp_mant[1] != y->fp_mant[1] ||
    175 	r->fp_mant[2] != y->fp_mant[2]) {
    176 	/* if (!(r < y/2) && !(r == y/2)) */
    177 	Last_Subtract = 1;
    178 	q++;
    179 	y->fp_sign = signY;
    180     } else {
    181 	/* Step 5.3 */
    182 	/* r == y/2 */
    183 	if (q % 2) {
    184 	    q++;
    185 	    signX = !signX;
    186 	}
    187     }
    188 
    189  Step6:
    190     r->fp_sign = signX;
    191 
    192     /*
    193      * Step 7
    194      */
    195     if (Last_Subtract) {
    196 	CPYFPN(&fe->fe_f1, r);
    197 	CPYFPN(&fe->fe_f2, y);
    198 	fe->fe_f2.fp_sign = !y->fp_sign;
    199 	r = fpu_add(fe);
    200     }
    201     /*
    202      * Step 8
    203      */
    204     q &= 0x7f;
    205     q |= (signQ << 7);
    206     fe->fe_fpframe->fpf_fpsr =
    207 	fe->fe_fpsr =
    208 	    (fe->fe_fpsr & ~FPSR_QTT) | (q << 16);
    209     return r;
    210 
    211  Step9:
    212     fe->fe_f1.fp_class = FPC_ZERO;
    213     q++;
    214     q &= 0x7f;
    215     q |= (signQ << 7);
    216     fe->fe_fpframe->fpf_fpsr =
    217 	fe->fe_fpsr =
    218 	    (fe->fe_fpsr & ~FPSR_QTT) | (q << 16);
    219     return &fe->fe_f1;
    220 }
    221 
    222 struct fpn *
    223 fpu_rem(fe)
    224      struct fpemu *fe;
    225 {
    226   return __fpu_modrem(fe, 1);
    227 }
    228 
    229 struct fpn *
    230 fpu_mod(fe)
    231      struct fpemu *fe;
    232 {
    233   return __fpu_modrem(fe, 0);
    234 }
    235