diff options
-rw-r--r-- | big.c | 55 |
1 files changed, 35 insertions, 20 deletions
@@ -438,29 +438,44 @@ word bigmod(word x, word y) magnitudes invariant under change of sign, remainder has sign of dividend, quotient negative if signs of divi(sor/dend) mixed */ -word shortdiv(x, n) /* divide big x by single digit n returning big quotient - and setting external b_rem as side effect */ - /* may assume - x>=0,n>0 */ -word x, n; -{ - word d = digit(x), s_rem, q = 0; - while (x = rest(x)) /* reverse rest(x) into q */ - q = make(INT, d, q), d = digit(x); /* leaving most sig. digit in d */ - { - word tmp; - x = q; +/* divide big x by single digit n returning big quotient + and setting external b_rem as side effect */ +/* may assume - x>=0,n>0 */ +word shortdiv(word x, word n) +{ + word d = digit(x); + word q = 0; + + /* reverse rest(x) into q */ + while (x = rest(x)) { + q = make(INT, d, q); + d = digit(x); /* leaving most sig. digit in d */ + } + + x = q; + word s_rem = d % n; + d = d / n; + + /* put back first digit (if not leading 0) */ + if (d || !q) { + q = make(INT, d, 0); + } else { + q = 0; + } + + /* in situ division of q by n AND destructive reversal */ + while (x) { + d = s_rem * IBASE + digit(x); + digit(x) = d / n; s_rem = d % n; - d = d / n; - if (d || !q) - q = make(INT, d, 0); /* put back first digit (if not leading 0) */ - else - q = 0; - while (x) /* in situ division of q by n AND destructive reversal */ - d = s_rem * IBASE + digit(x), digit(x) = d / n, s_rem = d % n, - tmp = x, x = rest(x), rest(tmp) = q, q = tmp; + word tmp = x; + x = rest(x); + rest(tmp) = q; + q = tmp; } + b_rem = make(INT, s_rem, 0); - return (q); + return q; } word longdiv(x, y) /* divide big x by big y returning quotient, leaving |