From 579c7a34c96e51429a8cba7e9bfb5e4c23b37a78 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 14:44:41 -0400 Subject: modernize bigmod() --- big.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/big.c b/big.c index d5341ac..fc9a452 100644 --- a/big.c +++ b/big.c @@ -398,25 +398,33 @@ word bigdiv(word x, word y) return q; } -word bigmod(x, y) /* may assume y~=0 */ -word x, y; +/* may assume y~=0 */ +word bigmod(word x, word y) { - word s1, s2; + word s1 = big_is_negative(y); + word s2 = s1; + /* make x,y positive and remember signs */ - if (s1 = big_is_negative(y)) + if (s1) { y = make(INT, digit0(y), rest(y)); - if (big_is_negative(x)) - x = make(INT, digit0(x), rest(x)), s2 = !s1; - else - s2 = s1; + } + + if (big_is_negative(x)) { + x = make(INT, digit0(x), rest(x)); + s2 = !s1; + } + /* effect: s1 set iff y negative, s2 set iff signs mixed */ - if (rest(y)) + if (rest(y)) { longdiv(x, y); - else + } else { shortdiv(x, digit(y)); + } + if (s2) { - if (!bigzero(b_rem)) + if (!bigzero(b_rem)) { b_rem = bigsub(y, b_rem); + } } return (s1 ? bignegate(b_rem) : b_rem); } -- cgit v1.2.1