diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:58:20 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:58:20 -0400 |
commit | f9a8d0b1205d3772d27c0e8e8a8dccda2f3c66ca (patch) | |
tree | ff610e896d28556ffa9cc86710e8651b0e5ec470 | |
parent | 8a1de9cc709b2a9470541a229d2134f022a28301 (diff) |
modernize bigpow()
-rw-r--r-- | big.c | 25 |
1 files changed, 17 insertions, 8 deletions
@@ -585,30 +585,39 @@ word ms2d(word x) return (digit(x) * IBASE + d); } -word bigpow(x, y) /* assumes y big_is_positive */ -word x, y; +/* assumes y big_is_positive */ +word bigpow(word x, word y) { - word d, r = make(INT, 1, 0); - while (rest(y)) { /* this loop has been unwrapped once, see below */ + word d; + word r = make(INT, 1, 0); + + /* this loop has been unwrapped once, see below */ + while (rest(y)) { word i = DIGITWIDTH; d = digit(y); while (i--) { - if (d & 1) + if (d & 1) { r = bigtimes(r, x); + } x = bigtimes(x, x); d >>= 1; } y = rest(y); } + d = digit(y); - if (d & 1) + if (d & 1) { r = bigtimes(r, x); + } + while (d >>= 1) { x = bigtimes(x, x); - if (d & 1) + if (d & 1) { r = bigtimes(r, x); + } } - return (r); + + return r; } double bigtodbl(x) |