diff options
-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 */ |