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