From f9a8d0b1205d3772d27c0e8e8a8dccda2f3c66ca Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 14:58:20 -0400 Subject: modernize bigpow() --- big.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'big.c') diff --git a/big.c b/big.c index 02753f6..8fba599 100644 --- a/big.c +++ b/big.c @@ -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) -- cgit v1.2.1