summaryrefslogtreecommitdiff
path: root/big.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-28 14:33:11 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-28 14:33:11 -0400
commita624e05d31c367d90c3fa8015bb1e12d3d1c2e45 (patch)
treed9fbc7ba85d24749ff69856fc294f2a785d61c18 /big.c
parentcfe7ab38754d9b5855c1537c5b55fa7b44c1c578 (diff)
modernize bigtimes()
Diffstat (limited to 'big.c')
-rw-r--r--big.c31
1 files changed, 18 insertions, 13 deletions
diff --git a/big.c b/big.c
index 0ebd8fa..78049eb 100644
--- a/big.c
+++ b/big.c
@@ -296,29 +296,34 @@ int bigcmp(word x, word y)
}
}
-word bigtimes(x, y) /* naive multiply - quadratic */
-word x, y;
+/* naive multiply - quadratic */
+word bigtimes(word x, word y)
{
+ /* important optimisation */
if (len(x) < len(y)) {
word hold = x;
x = y;
y = hold;
- } /* important optimisation */
+ }
+
word r = make(INT, 0, 0);
- word d = digit0(y);
+
+ /* short cut */
+ if (bigzero(x)) {
+ return (r);
+ }
+
+ //word d = digit0(y);
word s = big_is_negative(y);
- word n = 0;
- if (bigzero(x))
- return (r); /* short cut */
- for (;;) {
- if (d)
+ for (word n = 0, d = digit0(y); y; n++, d = digit(y)) {
+ if (d) {
r = bigplus(r, shift(n, stimes(x, d)));
- n++;
+ }
+
y = rest(y);
- if (!y)
- return (s != big_is_negative(x) ? bignegate(r) : r);
- d = digit(y);
+ //d = digit(y);
}
+ return (s != big_is_negative(x) ? bignegate(r) : r);
}
word shift(n, x) /* multiply big x by n'th power of IBASE */