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