2 (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 See the copyright notice in the ACK home directory, in the file "Copyright".
6 /* $Id: flt_div.c,v 1.13 1997/03/13 18:38:24 ceriel Exp $ */
12 register flt_arith *e1,*e2,*e3;
16 unsigned short u[9], v[5];
18 register unsigned short *u_p = u;
23 e3->flt_sign = e1->flt_sign ^ e2->flt_sign;
24 if ((e2->m1 | e2->m2) == 0) {
25 flt_status = FLT_DIV0;
26 e3->flt_exp = EXT_MAX;
31 if ((e1->m1 | e1->m2) == 0) { /* 0 / anything == 0 */
32 e3->flt_exp = 0; /* make sure */
37 e3->flt_exp = e1->flt_exp - e2->flt_exp;
39 u[4] = (e1->m2 & 1) << 15;
40 flt_b64_sft(&(cpe1.flt_mantissa),1);
42 u[5] = 0; u[6] = 0; u[7] = 0;
44 while (! v[maxv]) maxv--;
50 * Use an algorithm of Knuth (The art of programming, Seminumerical
51 * algorithms), to divide u by v. u and v are both seen as numbers
54 for (j = 0; j <= 3; j++, u_p++) {
59 if (u_p[0] == 0 && u_p[1] < v[1]) continue;
60 temp = ((long)u_p[0] << 16) + u_p[1];
72 q_est = (0x7FFFFFFF/v1)+((temp&0x7FFFFFFF)/v1);
73 rem = (0x7FFFFFFF%v1)+((temp&0x7FFFFFFF)%v1)+1;
80 while (!(temp&0xFFFF0000) &&
81 ucmp((long)v[2]*q_est,(temp<<16)+(long)u_p[2]) > 0) {
85 /* Now, according to Knuth, we have an estimate of the
86 quotient, that is either correct or one too big, but
87 almost always correct.
94 for (i = maxv; i > 0; i--) {
95 long tmp = q_est * (long)v[i] + k + borrow;
96 unsigned short md = tmp & 0xFFFF;
98 borrow = (md > u_p[i]);
100 k = (tmp >> 16) & 0xFFFF;
103 borrow = (long)u_p[0] < k;
107 /* So, this does not happen often; the estimate
108 was one too big; correct this
112 for (i = maxv; i > 0; i--) {
114 = v[i]+(long)u_p[i]+borrow;
116 u_p[i] = tmp & 0xFFFF;
117 borrow = (tmp >> 16) & 0xFFFF;
121 *lp |= (j & 1) ? q_est : (q_est<<16);