summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-28 14:44:41 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-28 14:44:41 -0400
commit579c7a34c96e51429a8cba7e9bfb5e4c23b37a78 (patch)
tree9f4ff6b6582436d26a5579053073b35722fa85d5
parent4a4816afd45c787bd9d12b20a3c515b5085991c0 (diff)
modernize bigmod()
-rw-r--r--big.c30
1 files 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);
}