diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 16:23:49 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 16:23:49 -0400 |
commit | fcea3028a0b5249c8908a0c3ed797c5a4eb32c5b (patch) | |
tree | 76e6792f2a171ea0d8cbd2178d4497dfc10a66a4 | |
parent | ed29c791d3c58ef126c47b34921b3816db3f1d96 (diff) |
move and rename len(), msd(), and ms2d()
-rw-r--r-- | big.c | 84 |
1 files changed, 41 insertions, 43 deletions
@@ -40,14 +40,46 @@ struct big_div { word big_one; -static word len(word); static struct big_div big__longdiv(word, word); static struct big_div shortdiv(word, word); -static word ms2d(word); -static word msd(word); static word shift(word, word); static word stimes(word, word); +/* no of digits in big x */ +word big__len(word x) +{ + word n = 1; + while ((x = rest(x)) != 0) { + n++; + } + + return n; +} + +/* most significant digit of big x */ +word big__msd(word x) +{ + while (rest(x)) { + x = rest(x); + } + + return digit(x); /* sign? */ +} + +/* most significant 2 digits of big x (len>=2) */ +word big__ms2d(word x) +{ + word d = digit(x); + x = rest(x); + while (rest(x)) { + d = digit(x); + x = rest(x); + } + + return (digit(x) * IBASE + d); +} + + /* ignore input signs, treat x,y as positive */ static word big__plus(word x, word y, int signbit) { @@ -316,7 +348,7 @@ int bigcmp(word x, word y) word bigtimes(word x, word y) { /* important optimisation */ - if (len(x) < len(y)) { + if (big__len(x) < big__len(y)) { word hold = x; x = y; y = hold; @@ -505,19 +537,19 @@ struct big_div big__longdiv(word x, word y) return bd; } - word y1 = msd(y); + word y1 = big__msd(y); /* rescale if necessary */ word scale = IBASE / (y1 + 1); if (scale > 1) { x = stimes(x, scale); y = stimes(y, scale); - y1 = msd(y); + y1 = big__msd(y); } word n = 0; word q = 0; - word ly = len(y); + word ly = big__len(y); while (bigcmp(x, y = make(INT, 0, y)) >= 0) { n++; } @@ -525,7 +557,7 @@ struct big_div big__longdiv(word x, word y) y = rest(y); /* want largest y not exceeding x */ ly += n; for (;;) { - word d, lx = len(x); + word d, lx = big__len(x); if (lx < ly) { d = 0; @@ -538,7 +570,7 @@ struct big_div big__longdiv(word x, word y) } } else { - d = ms2d(x) / y1; + d = big__ms2d(x) / y1; if (d > MAXDIGIT) { d = MAXDIGIT; } @@ -571,40 +603,6 @@ struct big_div big__longdiv(word x, word y) } } /* see Bird & Wadler p82 for explanation */ -/* no of digits in big x */ -word len(word x) -{ - word n = 1; - while ((x = rest(x)) != 0) { - n++; - } - - return n; -} - -/* most significant digit of big x */ -word msd(word x) -{ - while (rest(x)) { - x = rest(x); - } - - return digit(x); /* sign? */ -} - -/* most significant 2 digits of big x (len>=2) */ -word ms2d(word x) -{ - word d = digit(x); - x = rest(x); - while (rest(x)) { - d = digit(x); - x = rest(x); - } - - return (digit(x) * IBASE + d); -} - /* assumes y big_is_positive */ word bigpow(word x, word y) { |