fpu_div.c revision 1.3 1 1.3 agc /* $NetBSD: fpu_div.c,v 1.3 2003/08/07 16:29:17 agc Exp $ */
2 1.1 simonb
3 1.1 simonb /*
4 1.1 simonb * Copyright (c) 1992, 1993
5 1.1 simonb * The Regents of the University of California. All rights reserved.
6 1.1 simonb *
7 1.1 simonb * This software was developed by the Computer Systems Engineering group
8 1.1 simonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 simonb * contributed to Berkeley.
10 1.1 simonb *
11 1.1 simonb * All advertising materials mentioning features or use of this software
12 1.1 simonb * must display the following acknowledgement:
13 1.1 simonb * This product includes software developed by the University of
14 1.1 simonb * California, Lawrence Berkeley Laboratory.
15 1.1 simonb *
16 1.1 simonb * Redistribution and use in source and binary forms, with or without
17 1.1 simonb * modification, are permitted provided that the following conditions
18 1.1 simonb * are met:
19 1.1 simonb * 1. Redistributions of source code must retain the above copyright
20 1.1 simonb * notice, this list of conditions and the following disclaimer.
21 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 simonb * notice, this list of conditions and the following disclaimer in the
23 1.1 simonb * documentation and/or other materials provided with the distribution.
24 1.3 agc * 3. Neither the name of the University nor the names of its contributors
25 1.1 simonb * may be used to endorse or promote products derived from this software
26 1.1 simonb * without specific prior written permission.
27 1.1 simonb *
28 1.1 simonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 simonb * SUCH DAMAGE.
39 1.1 simonb *
40 1.1 simonb * @(#)fpu_div.c 8.1 (Berkeley) 6/11/93
41 1.1 simonb */
42 1.1 simonb
43 1.1 simonb /*
44 1.1 simonb * Perform an FPU divide (return x / y).
45 1.1 simonb */
46 1.2 lukem
47 1.2 lukem #include <sys/cdefs.h>
48 1.3 agc __KERNEL_RCSID(0, "$NetBSD: fpu_div.c,v 1.3 2003/08/07 16:29:17 agc Exp $");
49 1.1 simonb
50 1.1 simonb #include <sys/types.h>
51 1.1 simonb #if defined(DIAGNOSTIC)||defined(DEBUG)
52 1.1 simonb #include <sys/systm.h>
53 1.1 simonb #endif
54 1.1 simonb
55 1.1 simonb #include <machine/reg.h>
56 1.1 simonb #include <machine/fpu.h>
57 1.1 simonb
58 1.1 simonb #include <powerpc/fpu/fpu_arith.h>
59 1.1 simonb #include <powerpc/fpu/fpu_emu.h>
60 1.1 simonb
61 1.1 simonb /*
62 1.1 simonb * Division of normal numbers is done as follows:
63 1.1 simonb *
64 1.1 simonb * x and y are floating point numbers, i.e., in the form 1.bbbb * 2^e.
65 1.1 simonb * If X and Y are the mantissas (1.bbbb's), the quotient is then:
66 1.1 simonb *
67 1.1 simonb * q = (X / Y) * 2^((x exponent) - (y exponent))
68 1.1 simonb *
69 1.1 simonb * Since X and Y are both in [1.0,2.0), the quotient's mantissa (X / Y)
70 1.1 simonb * will be in [0.5,2.0). Moreover, it will be less than 1.0 if and only
71 1.1 simonb * if X < Y. In that case, it will have to be shifted left one bit to
72 1.1 simonb * become a normal number, and the exponent decremented. Thus, the
73 1.1 simonb * desired exponent is:
74 1.1 simonb *
75 1.1 simonb * left_shift = x->fp_mant < y->fp_mant;
76 1.1 simonb * result_exp = x->fp_exp - y->fp_exp - left_shift;
77 1.1 simonb *
78 1.1 simonb * The quotient mantissa X/Y can then be computed one bit at a time
79 1.1 simonb * using the following algorithm:
80 1.1 simonb *
81 1.1 simonb * Q = 0; -- Initial quotient.
82 1.1 simonb * R = X; -- Initial remainder,
83 1.1 simonb * if (left_shift) -- but fixed up in advance.
84 1.1 simonb * R *= 2;
85 1.1 simonb * for (bit = FP_NMANT; --bit >= 0; R *= 2) {
86 1.1 simonb * if (R >= Y) {
87 1.1 simonb * Q |= 1 << bit;
88 1.1 simonb * R -= Y;
89 1.1 simonb * }
90 1.1 simonb * }
91 1.1 simonb *
92 1.1 simonb * The subtraction R -= Y always removes the uppermost bit from R (and
93 1.1 simonb * can sometimes remove additional lower-order 1 bits); this proof is
94 1.1 simonb * left to the reader.
95 1.1 simonb *
96 1.1 simonb * This loop correctly calculates the guard and round bits since they are
97 1.1 simonb * included in the expanded internal representation. The sticky bit
98 1.1 simonb * is to be set if and only if any other bits beyond guard and round
99 1.1 simonb * would be set. From the above it is obvious that this is true if and
100 1.1 simonb * only if the remainder R is nonzero when the loop terminates.
101 1.1 simonb *
102 1.1 simonb * Examining the loop above, we can see that the quotient Q is built
103 1.1 simonb * one bit at a time ``from the top down''. This means that we can
104 1.1 simonb * dispense with the multi-word arithmetic and just build it one word
105 1.1 simonb * at a time, writing each result word when it is done.
106 1.1 simonb *
107 1.1 simonb * Furthermore, since X and Y are both in [1.0,2.0), we know that,
108 1.1 simonb * initially, R >= Y. (Recall that, if X < Y, R is set to X * 2 and
109 1.1 simonb * is therefore at in [2.0,4.0).) Thus Q is sure to have bit FP_NMANT-1
110 1.1 simonb * set, and R can be set initially to either X - Y (when X >= Y) or
111 1.1 simonb * 2X - Y (when X < Y). In addition, comparing R and Y is difficult,
112 1.1 simonb * so we will simply calculate R - Y and see if that underflows.
113 1.1 simonb * This leads to the following revised version of the algorithm:
114 1.1 simonb *
115 1.1 simonb * R = X;
116 1.1 simonb * bit = FP_1;
117 1.1 simonb * D = R - Y;
118 1.1 simonb * if (D >= 0) {
119 1.1 simonb * result_exp = x->fp_exp - y->fp_exp;
120 1.1 simonb * R = D;
121 1.1 simonb * q = bit;
122 1.1 simonb * bit >>= 1;
123 1.1 simonb * } else {
124 1.1 simonb * result_exp = x->fp_exp - y->fp_exp - 1;
125 1.1 simonb * q = 0;
126 1.1 simonb * }
127 1.1 simonb * R <<= 1;
128 1.1 simonb * do {
129 1.1 simonb * D = R - Y;
130 1.1 simonb * if (D >= 0) {
131 1.1 simonb * q |= bit;
132 1.1 simonb * R = D;
133 1.1 simonb * }
134 1.1 simonb * R <<= 1;
135 1.1 simonb * } while ((bit >>= 1) != 0);
136 1.1 simonb * Q[0] = q;
137 1.1 simonb * for (i = 1; i < 4; i++) {
138 1.1 simonb * q = 0, bit = 1 << 31;
139 1.1 simonb * do {
140 1.1 simonb * D = R - Y;
141 1.1 simonb * if (D >= 0) {
142 1.1 simonb * q |= bit;
143 1.1 simonb * R = D;
144 1.1 simonb * }
145 1.1 simonb * R <<= 1;
146 1.1 simonb * } while ((bit >>= 1) != 0);
147 1.1 simonb * Q[i] = q;
148 1.1 simonb * }
149 1.1 simonb *
150 1.1 simonb * This can be refined just a bit further by moving the `R <<= 1'
151 1.1 simonb * calculations to the front of the do-loops and eliding the first one.
152 1.1 simonb * The process can be terminated immediately whenever R becomes 0, but
153 1.1 simonb * this is relatively rare, and we do not bother.
154 1.1 simonb */
155 1.1 simonb
156 1.1 simonb struct fpn *
157 1.1 simonb fpu_div(struct fpemu *fe)
158 1.1 simonb {
159 1.1 simonb struct fpn *x = &fe->fe_f1, *y = &fe->fe_f2;
160 1.1 simonb u_int q, bit;
161 1.1 simonb u_int r0, r1, r2, r3, d0, d1, d2, d3, y0, y1, y2, y3;
162 1.1 simonb FPU_DECL_CARRY
163 1.1 simonb
164 1.1 simonb /*
165 1.1 simonb * Since divide is not commutative, we cannot just use ORDER.
166 1.1 simonb * Check either operand for NaN first; if there is at least one,
167 1.1 simonb * order the signalling one (if only one) onto the right, then
168 1.1 simonb * return it. Otherwise we have the following cases:
169 1.1 simonb *
170 1.1 simonb * Inf / Inf = NaN, plus NV exception
171 1.1 simonb * Inf / num = Inf [i.e., return x]
172 1.1 simonb * Inf / 0 = Inf [i.e., return x]
173 1.1 simonb * 0 / Inf = 0 [i.e., return x]
174 1.1 simonb * 0 / num = 0 [i.e., return x]
175 1.1 simonb * 0 / 0 = NaN, plus NV exception
176 1.1 simonb * num / Inf = 0
177 1.1 simonb * num / num = num (do the divide)
178 1.1 simonb * num / 0 = Inf, plus DZ exception
179 1.1 simonb */
180 1.1 simonb DPRINTF(FPE_REG, ("fpu_div:\n"));
181 1.1 simonb DUMPFPN(FPE_REG, x);
182 1.1 simonb DUMPFPN(FPE_REG, y);
183 1.1 simonb DPRINTF(FPE_REG, ("=>\n"));
184 1.1 simonb if (ISNAN(x) || ISNAN(y)) {
185 1.1 simonb ORDER(x, y);
186 1.1 simonb fe->fe_cx |= FPSCR_VXSNAN;
187 1.1 simonb DUMPFPN(FPE_REG, y);
188 1.1 simonb return (y);
189 1.1 simonb }
190 1.1 simonb /*
191 1.1 simonb * Need to split the following out cause they generate different
192 1.1 simonb * exceptions.
193 1.1 simonb */
194 1.1 simonb if (ISINF(x)) {
195 1.1 simonb if (x->fp_class == y->fp_class) {
196 1.1 simonb fe->fe_cx |= FPSCR_VXIDI;
197 1.1 simonb return (fpu_newnan(fe));
198 1.1 simonb }
199 1.1 simonb DUMPFPN(FPE_REG, x);
200 1.1 simonb return (x);
201 1.1 simonb }
202 1.1 simonb if (ISZERO(x)) {
203 1.1 simonb fe->fe_cx |= FPSCR_ZX;
204 1.1 simonb if (x->fp_class == y->fp_class) {
205 1.1 simonb fe->fe_cx |= FPSCR_VXZDZ;
206 1.1 simonb return (fpu_newnan(fe));
207 1.1 simonb }
208 1.1 simonb DUMPFPN(FPE_REG, x);
209 1.1 simonb return (x);
210 1.1 simonb }
211 1.1 simonb
212 1.1 simonb /* all results at this point use XOR of operand signs */
213 1.1 simonb x->fp_sign ^= y->fp_sign;
214 1.1 simonb if (ISINF(y)) {
215 1.1 simonb x->fp_class = FPC_ZERO;
216 1.1 simonb DUMPFPN(FPE_REG, x);
217 1.1 simonb return (x);
218 1.1 simonb }
219 1.1 simonb if (ISZERO(y)) {
220 1.1 simonb fe->fe_cx = FPSCR_ZX;
221 1.1 simonb x->fp_class = FPC_INF;
222 1.1 simonb DUMPFPN(FPE_REG, x);
223 1.1 simonb return (x);
224 1.1 simonb }
225 1.1 simonb
226 1.1 simonb /*
227 1.1 simonb * Macros for the divide. See comments at top for algorithm.
228 1.1 simonb * Note that we expand R, D, and Y here.
229 1.1 simonb */
230 1.1 simonb
231 1.1 simonb #define SUBTRACT /* D = R - Y */ \
232 1.1 simonb FPU_SUBS(d3, r3, y3); FPU_SUBCS(d2, r2, y2); \
233 1.1 simonb FPU_SUBCS(d1, r1, y1); FPU_SUBC(d0, r0, y0)
234 1.1 simonb
235 1.1 simonb #define NONNEGATIVE /* D >= 0 */ \
236 1.1 simonb ((int)d0 >= 0)
237 1.1 simonb
238 1.1 simonb #ifdef FPU_SHL1_BY_ADD
239 1.1 simonb #define SHL1 /* R <<= 1 */ \
240 1.1 simonb FPU_ADDS(r3, r3, r3); FPU_ADDCS(r2, r2, r2); \
241 1.1 simonb FPU_ADDCS(r1, r1, r1); FPU_ADDC(r0, r0, r0)
242 1.1 simonb #else
243 1.1 simonb #define SHL1 \
244 1.1 simonb r0 = (r0 << 1) | (r1 >> 31), r1 = (r1 << 1) | (r2 >> 31), \
245 1.1 simonb r2 = (r2 << 1) | (r3 >> 31), r3 <<= 1
246 1.1 simonb #endif
247 1.1 simonb
248 1.1 simonb #define LOOP /* do ... while (bit >>= 1) */ \
249 1.1 simonb do { \
250 1.1 simonb SHL1; \
251 1.1 simonb SUBTRACT; \
252 1.1 simonb if (NONNEGATIVE) { \
253 1.1 simonb q |= bit; \
254 1.1 simonb r0 = d0, r1 = d1, r2 = d2, r3 = d3; \
255 1.1 simonb } \
256 1.1 simonb } while ((bit >>= 1) != 0)
257 1.1 simonb
258 1.1 simonb #define WORD(r, i) /* calculate r->fp_mant[i] */ \
259 1.1 simonb q = 0; \
260 1.1 simonb bit = 1 << 31; \
261 1.1 simonb LOOP; \
262 1.1 simonb (x)->fp_mant[i] = q
263 1.1 simonb
264 1.1 simonb /* Setup. Note that we put our result in x. */
265 1.1 simonb r0 = x->fp_mant[0];
266 1.1 simonb r1 = x->fp_mant[1];
267 1.1 simonb r2 = x->fp_mant[2];
268 1.1 simonb r3 = x->fp_mant[3];
269 1.1 simonb y0 = y->fp_mant[0];
270 1.1 simonb y1 = y->fp_mant[1];
271 1.1 simonb y2 = y->fp_mant[2];
272 1.1 simonb y3 = y->fp_mant[3];
273 1.1 simonb
274 1.1 simonb bit = FP_1;
275 1.1 simonb SUBTRACT;
276 1.1 simonb if (NONNEGATIVE) {
277 1.1 simonb x->fp_exp -= y->fp_exp;
278 1.1 simonb r0 = d0, r1 = d1, r2 = d2, r3 = d3;
279 1.1 simonb q = bit;
280 1.1 simonb bit >>= 1;
281 1.1 simonb } else {
282 1.1 simonb x->fp_exp -= y->fp_exp + 1;
283 1.1 simonb q = 0;
284 1.1 simonb }
285 1.1 simonb LOOP;
286 1.1 simonb x->fp_mant[0] = q;
287 1.1 simonb WORD(x, 1);
288 1.1 simonb WORD(x, 2);
289 1.1 simonb WORD(x, 3);
290 1.1 simonb x->fp_sticky = r0 | r1 | r2 | r3;
291 1.1 simonb
292 1.1 simonb DUMPFPN(FPE_REG, x);
293 1.1 simonb return (x);
294 1.1 simonb }
295