From a624e05d31c367d90c3fa8015bb1e12d3d1c2e45 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 14:33:11 -0400 Subject: modernize bigtimes() --- big.c | 31 ++++++++++++++++++------------- 1 file 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 */ -- cgit v1.2.1