summaryrefslogtreecommitdiffstats
path: root/pbn_div.c
blob: b8dd2f3d03d4b1654f05fd2c129221b60c0fb469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as
 *   published by the Free Software Foundation, Inc.,
 *   59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
 *   incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * pbn_div.c
 */

/*
 * quotient, remainder, numerator, denominator
 *
 * The sign on the outputs = sign(numerator) XOR sign(denominator)
 *
 * Returns -1 on error (divide by zero) and frees the inputs.
 * Returns 1 if the remainder is nonzero, otherwise 0.
 */
#include "pbnint.h"

int pbn_div(struct pbn **qp, struct pbn **rp, struct pbn *n, struct pbn *d)
{
    struct pbn *q;	    /* Quotient accumulator */
    pbn_limb_t *un;	    /* Numerator/remainder array */
    const pbn_limb_t *vn;   /* Denominator array */
    pbn_2limb_t qhat;	    /* Estimated quotient digit */
    pbn_2limb_t rhat;	    /* Estimated remainder */
    pbn_2limb_t p;	    /* Product in multiply-subtract loop */
    pbn_2slimb_t t, k;	    /* Multiply-subtract loop temporaries */
    pbn_limb_t c;	    /* Add/subtract carry */
    int nl, dl;		    /* Length of n, d in limbs */
    int s;                  /* Normalization shift */
    int i, j;
    int rv;

    if (!d->bits || pbn_abscmp(n, d) < 0) {
	/* We either had division by zero, or |n| < |d| */
	rv = !d->bits ? -1 : !n->bits ? 0 : 1;
	if (rp) {
	    if (d->minus && n->bits) {
		n = pbn_cow(n, n->len);
		n->minus ^= 1;
	    }
	    *rp = n;
	} else {
	    pbn_free(n);
	}
	pbn_free(d);

	if (qp)
	    *qp = pbn_int(0);

	return rv;		/* Division by zero? */
    }

    /* Numerator and denominator length in limbs */
    nl = (n->bits + PBN_LIMB_BITS - 1) >> PBN_LIMB_SHIFT;
    dl = (d->bits + PBN_LIMB_BITS - 1) >> PBN_LIMB_SHIFT;

    q = pbn_new((((n->bits | (PBN_LIMB_BITS - 1)) -
                  (d->bits | (PBN_LIMB_BITS - 1)))
                 >> PBN_LIMB_SHIFT) + 1);

    q->minus = n->minus ^= d->minus;       /* Set the sign */

    if (dl == 1) {
        /* Single limb denominator */
        const pbn_limb_t dn = d->num[0];

        c = 0;
        for (i = nl - 1; i >= 0; i--)
            DIVIDE(c, n->num[i], dn, q->num[i], c);

        pbn_free(n);

        if (rp) {
            *rp = pbn_uint(c);
            (*rp)->minus = q->minus;
        }
    } else {
        /* Normalize denominator so the MSB of the MSW is on */
        s = -d->bits & (PBN_LIMB_BITS-1);
        d = pbn_shl(d, s);
        n = pbn_cow(pbn_shl(n, s), nl+1);

        un = n->num;
        vn = d->num;

        for (j = nl - dl; j >= 0; j--) {
            pbn_limb_t nx = un[j+dl];
            pbn_limb_t qhatx, rhatx;

            /* Compute initial estimate of the quotient limb */

            rhat = qhat = 0;
            if (nx >= vn[dl-1]) {
		qhat = (pbn_2limb_t)1 << PBN_LIMB_BITS;
		nx -= vn[dl-1];
            }

            DIVIDE(nx, un[j+dl-1], vn[dl-1], qhatx, rhatx);
	    qhat += qhatx;
            rhat += rhatx;

            /*
             * Note: this loop can only be executed at most twice;
             * consider unrolling
             */
            while (rhat < ((pbn_2limb_t)1 << PBN_LIMB_BITS) &&
                   (pbn_2limb_t)qhat * vn[dl-2] >
                   (rhat << PBN_LIMB_BITS) + un[j+dl-2]) {
                qhat--;
                rhat += vn[dl-1];
            }

            /* Multiply-subtract loop */
            k = 0;
            for (i = 0; i < dl; i++) {
                p = qhat * vn[i];
                t = un[i+j] - k - (pbn_limb_t)p;
                un[i+j] = (pbn_limb_t)t;
                k = (p >> PBN_LIMB_BITS) - (t >> PBN_LIMB_BITS);
            }
            t = un[j+dl] - k;
            un[j+dl] = (pbn_limb_t)t;

            /* Correct in case of oversubtraction */
            if (unlikely(t < 0)) {
                qhat--;
                c = 0;
                for (i = 0; i < dl; i++) {
                    t = (pbn_2limb_t)un[i+j] + vn[i] + c;
                    un[i+j] = (pbn_limb_t)t;
                    c = t >> PBN_LIMB_BITS;
                }
                un[j+dl] += c;
            }

            q->num[j] = qhat;
        }

        if (rp) {
            n = pbn_shr(pbn_adjust_bits(n), s);
	    rv = !!n->bits;
	    *rp = n;
	} else {
	    pbn_limb_t z = un[0];
	    for (i = 1; i < dl; i++)
		z |= un[i];
	    rv = !!z;
            pbn_free(n);
	}
    }

    pbn_free(d);

    if (qp)
        *qp = pbn_adjust_bits(q);
    else
        pbn_free(q);

    return rv;
}