summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-28 14:09:34 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-28 14:09:34 -0400
commit1b3e4865fcca40e49d02fe2878c3173de92087cc (patch)
treedc7bde235e1fbbcbfad8b743ebd1af0a6c66786e
parent6831094e2b4d9918b32741c7509416495142ae61 (diff)
modernize big_plus()
-rw-r--r--big.c22
1 files changed, 14 insertions, 8 deletions
diff --git a/big.c b/big.c
index 919390d..f791b4d 100644
--- a/big.c
+++ b/big.c
@@ -117,17 +117,17 @@ word bigplus(word x, word y)
}
/* ignore input signs, treat x,y as positive */
-word big_plus(x, y, signbit)
-word x, y;
-int signbit;
+word big_plus(word x, word y, int signbit)
{
word d = digit0(x) + digit0(y);
- word carry = ((d & IBASE) != 0);
word r = make(INT, signbit | d & MAXDIGIT, 0); /* result */
word *z = &rest(r); /* pointer to rest of result */
+
+ word carry = ((d & IBASE) != 0);
x = rest(x);
y = rest(y);
- while (x && y) { /* this loop has been unwrapped once, see above */
+
+ while (x && y) {
d = carry + digit(x) + digit(y);
carry = ((d & IBASE) != 0);
*z = make(INT, d & MAXDIGIT, 0);
@@ -135,8 +135,11 @@ int signbit;
y = rest(y);
z = &rest(*z);
}
- if (y)
+
+ if (y) {
x = y; /* by convention x is the longer one */
+ }
+
while (x) {
d = carry + digit(x);
carry = ((d & IBASE) != 0);
@@ -144,9 +147,12 @@ int signbit;
x = rest(x);
z = &rest(*z);
}
- if (carry)
+
+ if (carry) {
*z = make(INT, 1, 0);
- return (r);
+ }
+
+ return r;
}
word bigsub(x, y)