diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:33:11 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:33:11 -0400 |
commit | a624e05d31c367d90c3fa8015bb1e12d3d1c2e45 (patch) | |
tree | d9fbc7ba85d24749ff69856fc294f2a785d61c18 | |
parent | cfe7ab38754d9b5855c1537c5b55fa7b44c1c578 (diff) |
modernize bigtimes()
-rw-r--r-- | big.c | 31 |
1 files changed, 18 insertions, 13 deletions
@@ -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 */ |