summaryrefslogtreecommitdiffstats log msg author committer range
path: root/pbn_div.c
blob: 88cb0602354ab6f0f45b17ca4aa92458cacd34a3 (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 ``` ``````/* ----------------------------------------------------------------------- * * * 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. This is doing it the slow way, which is bitwise long division. If this turns out to be a bottleneck, then it should be replaced by smarter algorithms, but this algorithm should at least be correct, so it should be usable for now. */ #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_limb_t qhat; /* Estimated quotient digit */ pbn_2limb_t rhat; /* Estimated remainder */ pbn_2slimb_t p, t, k; /* Multiply-subtract loop intermediates */ int nl, dl; /* Length of n, d in limbs */ int s; /* Normalization shift */ int i, j; int rv; if ((rv = d->bits == 0) || pbn_abscmp(n, d) < 0) { /* We either had division by zero, or |n| < |d| */ 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 */ pbn_limb_t c; 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 rhatx; /* Compute initial estimate of the quotient limb */ rhat = 0; if (nx >= vn[dl-1]) { rhat = vn[dl-1]; nx -= vn[dl-1]; } DIVIDE(nx, un[j+dl-1], vn[dl-1], qhat, rhatx); 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 = (pbn_2limb_t)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--; k = 0; for (i = 0; i < dl; i++) { t = k + un[i+j] + vn[i]; un[i+j] = (pbn_limb_t)t; k = t >> PBN_LIMB_BITS; } un[j+dl] += (pbn_limb_t)k; } q->num[j] = qhat; } if (rp) *rp = pbn_shr(pbn_adjust_bits(n), s); else pbn_free(n); } pbn_free(d); if (qp) *qp = pbn_adjust_bits(q); else pbn_free(q); return 0; } ``````