From 495690d6be9325a5eeae02412adb1a0cfa622432 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 14:49:10 -0400 Subject: modernize shortdiv() --- big.c | 55 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/big.c b/big.c index fc9a452..806cd11 100644 --- a/big.c +++ b/big.c @@ -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 -- cgit v1.2.1