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