Home | History | Annotate | Line # | Download | only in quad
      1  1.4      matt /*	$NetBSD: qdivrem.c,v 1.4 2012/03/20 16:21:41 matt Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1992, 1993
      5  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This software was developed by the Computer Systems Engineering group
      8  1.1  christos  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1  christos  * contributed to Berkeley.
     10  1.1  christos  *
     11  1.1  christos  * Redistribution and use in source and binary forms, with or without
     12  1.1  christos  * modification, are permitted provided that the following conditions
     13  1.1  christos  * are met:
     14  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     16  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  christos  *    documentation and/or other materials provided with the distribution.
     19  1.1  christos  * 3. Neither the name of the University nor the names of its contributors
     20  1.1  christos  *    may be used to endorse or promote products derived from this software
     21  1.1  christos  *    without specific prior written permission.
     22  1.1  christos  *
     23  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  christos  * SUCH DAMAGE.
     34  1.1  christos  */
     35  1.1  christos 
     36  1.1  christos #include <sys/cdefs.h>
     37  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     38  1.1  christos #if 0
     39  1.1  christos static char sccsid[] = "@(#)qdivrem.c	8.1 (Berkeley) 6/4/93";
     40  1.1  christos #else
     41  1.4      matt __RCSID("$NetBSD: qdivrem.c,v 1.4 2012/03/20 16:21:41 matt Exp $");
     42  1.1  christos #endif
     43  1.1  christos #endif /* LIBC_SCCS and not lint */
     44  1.1  christos 
     45  1.1  christos /*
     46  1.1  christos  * Multiprecision divide.  This algorithm is from Knuth vol. 2 (2nd ed),
     47  1.1  christos  * section 4.3.1, pp. 257--259.
     48  1.1  christos  */
     49  1.1  christos 
     50  1.1  christos #include "quad.h"
     51  1.1  christos 
     52  1.3  christos #define	B	((int)1 << (unsigned int)HALF_BITS)	/* digit base */
     53  1.1  christos 
     54  1.1  christos /* Combine two `digits' to make a single two-digit number. */
     55  1.3  christos #define	COMBINE(a, b) (((u_int)(a) << (unsigned int)HALF_BITS) | (b))
     56  1.1  christos 
     57  1.1  christos /* select a type for digits in base B: use unsigned short if they fit */
     58  1.1  christos #if UINT_MAX == 0xffffffffU && USHRT_MAX >= 0xffff
     59  1.1  christos typedef unsigned short digit;
     60  1.1  christos #else
     61  1.1  christos typedef u_int digit;
     62  1.1  christos #endif
     63  1.1  christos 
     64  1.4      matt static void shl(digit *p, int len, int sh);
     65  1.1  christos 
     66  1.1  christos /*
     67  1.1  christos  * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
     68  1.1  christos  *
     69  1.1  christos  * We do this in base 2-sup-HALF_BITS, so that all intermediate products
     70  1.1  christos  * fit within u_int.  As a consequence, the maximum length dividend and
     71  1.1  christos  * divisor are 4 `digits' in this base (they are shorter if they have
     72  1.1  christos  * leading zeros).
     73  1.1  christos  */
     74  1.1  christos u_quad_t
     75  1.2    cegger __qdivrem(u_quad_t uq, u_quad_t vq, u_quad_t *arq)
     76  1.1  christos {
     77  1.1  christos 	union uu tmp;
     78  1.1  christos 	digit *u, *v, *q;
     79  1.1  christos 	digit v1, v2;
     80  1.1  christos 	u_int qhat, rhat, t;
     81  1.1  christos 	int m, n, d, j, i;
     82  1.1  christos 	digit uspace[5], vspace[5], qspace[5];
     83  1.1  christos 
     84  1.1  christos 	/*
     85  1.1  christos 	 * Take care of special cases: divide by zero, and u < v.
     86  1.1  christos 	 */
     87  1.1  christos 	if (vq == 0) {
     88  1.1  christos 		/* divide by zero. */
     89  1.1  christos 		static volatile const unsigned int zero = 0;
     90  1.1  christos 
     91  1.1  christos 		tmp.ul[H] = tmp.ul[L] = 1 / zero;
     92  1.1  christos 		if (arq)
     93  1.1  christos 			*arq = uq;
     94  1.1  christos 		return (tmp.q);
     95  1.1  christos 	}
     96  1.1  christos 	if (uq < vq) {
     97  1.1  christos 		if (arq)
     98  1.1  christos 			*arq = uq;
     99  1.1  christos 		return (0);
    100  1.1  christos 	}
    101  1.1  christos 	u = &uspace[0];
    102  1.1  christos 	v = &vspace[0];
    103  1.1  christos 	q = &qspace[0];
    104  1.1  christos 
    105  1.1  christos 	/*
    106  1.1  christos 	 * Break dividend and divisor into digits in base B, then
    107  1.1  christos 	 * count leading zeros to determine m and n.  When done, we
    108  1.1  christos 	 * will have:
    109  1.1  christos 	 *	u = (u[1]u[2]...u[m+n]) sub B
    110  1.1  christos 	 *	v = (v[1]v[2]...v[n]) sub B
    111  1.1  christos 	 *	v[1] != 0
    112  1.1  christos 	 *	1 < n <= 4 (if n = 1, we use a different division algorithm)
    113  1.1  christos 	 *	m >= 0 (otherwise u < v, which we already checked)
    114  1.1  christos 	 *	m + n = 4
    115  1.1  christos 	 * and thus
    116  1.1  christos 	 *	m = 4 - n <= 2
    117  1.1  christos 	 */
    118  1.1  christos 	tmp.uq = uq;
    119  1.1  christos 	u[0] = 0;
    120  1.1  christos 	u[1] = (digit)HHALF(tmp.ul[H]);
    121  1.1  christos 	u[2] = (digit)LHALF(tmp.ul[H]);
    122  1.1  christos 	u[3] = (digit)HHALF(tmp.ul[L]);
    123  1.1  christos 	u[4] = (digit)LHALF(tmp.ul[L]);
    124  1.1  christos 	tmp.uq = vq;
    125  1.1  christos 	v[1] = (digit)HHALF(tmp.ul[H]);
    126  1.1  christos 	v[2] = (digit)LHALF(tmp.ul[H]);
    127  1.1  christos 	v[3] = (digit)HHALF(tmp.ul[L]);
    128  1.1  christos 	v[4] = (digit)LHALF(tmp.ul[L]);
    129  1.1  christos 	for (n = 4; v[1] == 0; v++) {
    130  1.1  christos 		if (--n == 1) {
    131  1.1  christos 			u_int rbj;	/* r*B+u[j] (not root boy jim) */
    132  1.1  christos 			digit q1, q2, q3, q4;
    133  1.1  christos 
    134  1.1  christos 			/*
    135  1.1  christos 			 * Change of plan, per exercise 16.
    136  1.1  christos 			 *	r = 0;
    137  1.1  christos 			 *	for j = 1..4:
    138  1.1  christos 			 *		q[j] = floor((r*B + u[j]) / v),
    139  1.1  christos 			 *		r = (r*B + u[j]) % v;
    140  1.1  christos 			 * We unroll this completely here.
    141  1.1  christos 			 */
    142  1.1  christos 			t = v[2];	/* nonzero, by definition */
    143  1.1  christos 			q1 = (digit)(u[1] / t);
    144  1.1  christos 			rbj = COMBINE(u[1] % t, u[2]);
    145  1.1  christos 			q2 = (digit)(rbj / t);
    146  1.1  christos 			rbj = COMBINE(rbj % t, u[3]);
    147  1.1  christos 			q3 = (digit)(rbj / t);
    148  1.1  christos 			rbj = COMBINE(rbj % t, u[4]);
    149  1.1  christos 			q4 = (digit)(rbj / t);
    150  1.1  christos 			if (arq)
    151  1.1  christos 				*arq = rbj % t;
    152  1.1  christos 			tmp.ul[H] = COMBINE(q1, q2);
    153  1.1  christos 			tmp.ul[L] = COMBINE(q3, q4);
    154  1.1  christos 			return (tmp.q);
    155  1.1  christos 		}
    156  1.1  christos 	}
    157  1.1  christos 
    158  1.1  christos 	/*
    159  1.1  christos 	 * By adjusting q once we determine m, we can guarantee that
    160  1.1  christos 	 * there is a complete four-digit quotient at &qspace[1] when
    161  1.1  christos 	 * we finally stop.
    162  1.1  christos 	 */
    163  1.1  christos 	for (m = 4 - n; u[1] == 0; u++)
    164  1.1  christos 		m--;
    165  1.1  christos 	for (i = 4 - m; --i >= 0;)
    166  1.1  christos 		q[i] = 0;
    167  1.1  christos 	q += 4 - m;
    168  1.1  christos 
    169  1.1  christos 	/*
    170  1.1  christos 	 * Here we run Program D, translated from MIX to C and acquiring
    171  1.1  christos 	 * a few minor changes.
    172  1.1  christos 	 *
    173  1.1  christos 	 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
    174  1.1  christos 	 */
    175  1.1  christos 	d = 0;
    176  1.3  christos 	for (t = v[1]; t < B / 2; t <<= (unsigned int)1)
    177  1.1  christos 		d++;
    178  1.1  christos 	if (d > 0) {
    179  1.1  christos 		shl(&u[0], m + n, d);		/* u <<= d */
    180  1.1  christos 		shl(&v[1], n - 1, d);		/* v <<= d */
    181  1.1  christos 	}
    182  1.1  christos 	/*
    183  1.1  christos 	 * D2: j = 0.
    184  1.1  christos 	 */
    185  1.1  christos 	j = 0;
    186  1.1  christos 	v1 = v[1];	/* for D3 -- note that v[1..n] are constant */
    187  1.1  christos 	v2 = v[2];	/* for D3 */
    188  1.1  christos 	do {
    189  1.1  christos 		digit uj0, uj1, uj2;
    190  1.1  christos 
    191  1.1  christos 		/*
    192  1.1  christos 		 * D3: Calculate qhat (\^q, in TeX notation).
    193  1.1  christos 		 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
    194  1.1  christos 		 * let rhat = (u[j]*B + u[j+1]) mod v[1].
    195  1.1  christos 		 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
    196  1.1  christos 		 * decrement qhat and increase rhat correspondingly.
    197  1.1  christos 		 * Note that if rhat >= B, v[2]*qhat < rhat*B.
    198  1.1  christos 		 */
    199  1.1  christos 		uj0 = u[j + 0];	/* for D3 only -- note that u[j+...] change */
    200  1.1  christos 		uj1 = u[j + 1];	/* for D3 only */
    201  1.1  christos 		uj2 = u[j + 2];	/* for D3 only */
    202  1.1  christos 		if (uj0 == v1) {
    203  1.1  christos 			qhat = B;
    204  1.1  christos 			rhat = uj1;
    205  1.1  christos 			goto qhat_too_big;
    206  1.1  christos 		} else {
    207  1.1  christos 			u_int nn = COMBINE(uj0, uj1);
    208  1.1  christos 			qhat = nn / v1;
    209  1.1  christos 			rhat = nn % v1;
    210  1.1  christos 		}
    211  1.1  christos 		while (v2 * qhat > COMBINE(rhat, uj2)) {
    212  1.1  christos 	qhat_too_big:
    213  1.1  christos 			qhat--;
    214  1.1  christos 			if ((rhat += v1) >= B)
    215  1.1  christos 				break;
    216  1.1  christos 		}
    217  1.1  christos 		/*
    218  1.1  christos 		 * D4: Multiply and subtract.
    219  1.1  christos 		 * The variable `t' holds any borrows across the loop.
    220  1.1  christos 		 * We split this up so that we do not require v[0] = 0,
    221  1.1  christos 		 * and to eliminate a final special case.
    222  1.1  christos 		 */
    223  1.1  christos 		for (t = 0, i = n; i > 0; i--) {
    224  1.1  christos 			t = u[i + j] - v[i] * qhat - t;
    225  1.1  christos 			u[i + j] = (digit)LHALF(t);
    226  1.1  christos 			t = (B - HHALF(t)) & (B - 1);
    227  1.1  christos 		}
    228  1.1  christos 		t = u[j] - t;
    229  1.1  christos 		u[j] = (digit)LHALF(t);
    230  1.1  christos 		/*
    231  1.1  christos 		 * D5: test remainder.
    232  1.1  christos 		 * There is a borrow if and only if HHALF(t) is nonzero;
    233  1.1  christos 		 * in that (rare) case, qhat was too large (by exactly 1).
    234  1.1  christos 		 * Fix it by adding v[1..n] to u[j..j+n].
    235  1.1  christos 		 */
    236  1.1  christos 		if (HHALF(t)) {
    237  1.1  christos 			qhat--;
    238  1.1  christos 			for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
    239  1.1  christos 				t += u[i + j] + v[i];
    240  1.1  christos 				u[i + j] = (digit)LHALF(t);
    241  1.1  christos 				t = HHALF(t);
    242  1.1  christos 			}
    243  1.1  christos 			u[j] = (digit)LHALF(u[j] + t);
    244  1.1  christos 		}
    245  1.1  christos 		q[j] = (digit)qhat;
    246  1.1  christos 	} while (++j <= m);		/* D7: loop on j. */
    247  1.1  christos 
    248  1.1  christos 	/*
    249  1.1  christos 	 * If caller wants the remainder, we have to calculate it as
    250  1.1  christos 	 * u[m..m+n] >> d (this is at most n digits and thus fits in
    251  1.1  christos 	 * u[m+1..m+n], but we may need more source digits).
    252  1.1  christos 	 */
    253  1.1  christos 	if (arq) {
    254  1.1  christos 		if (d) {
    255  1.1  christos 			for (i = m + n; i > m; --i)
    256  1.1  christos 				u[i] = (digit)(((u_int)u[i] >> d) |
    257  1.3  christos 				    LHALF((u_int)u[i - 1] << (unsigned int)(HALF_BITS - d)));
    258  1.1  christos 			u[i] = 0;
    259  1.1  christos 		}
    260  1.1  christos 		tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
    261  1.1  christos 		tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
    262  1.1  christos 		*arq = tmp.q;
    263  1.1  christos 	}
    264  1.1  christos 
    265  1.1  christos 	tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
    266  1.1  christos 	tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
    267  1.1  christos 	return (tmp.q);
    268  1.1  christos }
    269  1.1  christos 
    270  1.1  christos /*
    271  1.1  christos  * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
    272  1.1  christos  * `fall out' the left (there never will be any such anyway).
    273  1.1  christos  * We may assume len >= 0.  NOTE THAT THIS WRITES len+1 DIGITS.
    274  1.1  christos  */
    275  1.1  christos static void
    276  1.1  christos shl(digit *p, int len, int sh)
    277  1.1  christos {
    278  1.1  christos 	int i;
    279  1.1  christos 
    280  1.1  christos 	for (i = 0; i < len; i++)
    281  1.1  christos 		p[i] = (digit)(LHALF((u_int)p[i] << sh) |
    282  1.1  christos 		    ((u_int)p[i + 1] >> (HALF_BITS - sh)));
    283  1.1  christos 	p[i] = (digit)(LHALF((u_int)p[i] << sh));
    284  1.1  christos }
    285