fpu_rem.c revision 1.4 1 /* $NetBSD: fpu_rem.c,v 1.4 1999/05/30 20:17:48 briggs 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/types.h>
35 #include <sys/signal.h>
36 #include <machine/frame.h>
37
38 #include "fpu_emulate.h"
39
40 /*
41 * ALGORITHM
42 *
43 * Step 1. Save and strip signs of X and Y: signX := sign(X),
44 * signY := sign(Y), X := *X*, Y := *Y*,
45 * signQ := signX EOR signY. Record whether MOD or REM
46 * is requested.
47 *
48 * Step 2. Set L := expo(X)-expo(Y), k := 0, Q := 0.
49 * If (L < 0) then
50 * R := X, go to Step 4.
51 * else
52 * R := 2^(-L)X, j := L.
53 * endif
54 *
55 * Step 3. Perform MOD(X,Y)
56 * 3.1 If R = Y, go to Step 9.
57 * 3.2 If R > Y, then { R := R - Y, Q := Q + 1}
58 * 3.3 If j = 0, go to Step 4.
59 * 3.4 k := k + 1, j := j - 1, Q := 2Q, R := 2R. Go to
60 * Step 3.1.
61 *
62 * Step 4. At this point, R = X - QY = MOD(X,Y). Set
63 * Last_Subtract := false (used in Step 7 below). If
64 * MOD is requested, go to Step 6.
65 *
66 * Step 5. R = MOD(X,Y), but REM(X,Y) is requested.
67 * 5.1 If R < Y/2, then R = MOD(X,Y) = REM(X,Y). Go to
68 * Step 6.
69 * 5.2 If R > Y/2, then { set Last_Subtract := true,
70 * Q := Q + 1, Y := signY*Y }. Go to Step 6.
71 * 5.3 This is the tricky case of R = Y/2. If Q is odd,
72 * then { Q := Q + 1, signX := -signX }.
73 *
74 * Step 6. R := signX*R.
75 *
76 * Step 7. If Last_Subtract = true, R := R - Y.
77 *
78 * Step 8. Return signQ, last 7 bits of Q, and R as required.
79 *
80 * Step 9. At this point, R = 2^(-j)*X - Q Y = Y. Thus,
81 * X = 2^(j)*(Q+1)Y. set Q := 2^(j)*(Q+1),
82 * R := 0. Return signQ, last 7 bits of Q, and R.
83 */
84
85 static struct fpn * __fpu_modrem __P((struct fpemu *fe, int modrem));
86
87 static struct fpn *
88 __fpu_modrem(fe, modrem)
89 struct fpemu *fe;
90 int modrem;
91 {
92 static struct fpn X, Y;
93 struct fpn *x, *y, *r;
94 u_int signX, signY, signQ;
95 int j, k, l, q;
96 int Last_Subtract;
97
98 CPYFPN(&X, &fe->fe_f1);
99 CPYFPN(&Y, &fe->fe_f2);
100 x = &X;
101 y = &Y;
102 r = &fe->fe_f2;
103
104 /*
105 * Step 1
106 */
107 signX = x->fp_sign;
108 signY = y->fp_sign;
109 signQ = (signX ^ signY);
110 x->fp_sign = y->fp_sign = 0;
111
112 /*
113 * Step 2
114 */
115 l = x->fp_exp - y->fp_exp;
116 k = 0;
117 q = 0;
118 if (l >= 0) {
119 CPYFPN(r, x);
120 r->fp_exp -= l;
121 j = l;
122
123 /*
124 * Step 3
125 */
126 while (y->fp_exp != r->fp_exp || y->fp_mant[0] != r->fp_mant[0] ||
127 y->fp_mant[1] != r->fp_mant[1] ||
128 y->fp_mant[2] != r->fp_mant[2]) {
129
130 /* Step 3.2 */
131 if (y->fp_exp < r->fp_exp || y->fp_mant[0] < r->fp_mant[0] ||
132 y->fp_mant[1] < r->fp_mant[1] ||
133 y->fp_mant[2] < r->fp_mant[2]) {
134 CPYFPN(&fe->fe_f1, r);
135 CPYFPN(&fe->fe_f2, y);
136 fe->fe_f2.fp_sign = 1;
137 r = fpu_add(fe);
138 q++;
139 }
140
141 /* Step 3.3 */
142 if (j == 0)
143 goto Step4;
144
145 /* Step 3.4 */
146 k++;
147 j--;
148 q += q;
149 r->fp_exp++;
150 }
151 /* Step 9 */
152 goto Step9;
153 }
154 Step4:
155 Last_Subtract = 0;
156 if (modrem == 0)
157 goto Step6;
158
159 /*
160 * Step 5
161 */
162 /* Step 5.1 */
163 if (r->fp_exp + 1 < y->fp_exp ||
164 (r->fp_exp + 1 == y->fp_exp &&
165 (r->fp_mant[0] < y->fp_mant[0] || r->fp_mant[1] < y->fp_mant[1] ||
166 r->fp_mant[2] < y->fp_mant[2])))
167 /* if r < y/2 */
168 goto Step6;
169 /* Step 5.2 */
170 if (r->fp_exp + 1 != y->fp_exp ||
171 r->fp_mant[0] != y->fp_mant[0] || r->fp_mant[1] != y->fp_mant[1] ||
172 r->fp_mant[2] != y->fp_mant[2]) {
173 /* if (!(r < y/2) && !(r == y/2)) */
174 Last_Subtract = 1;
175 q++;
176 y->fp_sign = signY;
177 } else {
178 /* Step 5.3 */
179 /* r == y/2 */
180 if (q % 2) {
181 q++;
182 signX = !signX;
183 }
184 }
185
186 Step6:
187 r->fp_sign = signX;
188
189 /*
190 * Step 7
191 */
192 if (Last_Subtract) {
193 CPYFPN(&fe->fe_f1, r);
194 CPYFPN(&fe->fe_f2, y);
195 fe->fe_f2.fp_sign = !y->fp_sign;
196 r = fpu_add(fe);
197 }
198 /*
199 * Step 8
200 */
201 q &= 0x7f;
202 q |= (signQ << 7);
203 fe->fe_fpframe->fpf_fpsr =
204 fe->fe_fpsr =
205 (fe->fe_fpsr & ~FPSR_QTT) | (q << 16);
206 return r;
207
208 Step9:
209 fe->fe_f1.fp_class = FPC_ZERO;
210 q++;
211 q &= 0x7f;
212 q |= (signQ << 7);
213 fe->fe_fpframe->fpf_fpsr =
214 fe->fe_fpsr =
215 (fe->fe_fpsr & ~FPSR_QTT) | (q << 16);
216 return &fe->fe_f1;
217 }
218
219 struct fpn *
220 fpu_rem(fe)
221 struct fpemu *fe;
222 {
223 return __fpu_modrem(fe, 1);
224 }
225
226 struct fpn *
227 fpu_mod(fe)
228 struct fpemu *fe;
229 {
230 return __fpu_modrem(fe, 0);
231 }
232