summaryrefslogtreecommitdiff
path: root/big.c
diff options
context:
space:
mode:
Diffstat (limited to 'big.c')
-rw-r--r--big.c86
1 files changed, 43 insertions, 43 deletions
diff --git a/big.c b/big.c
index 356a8d2..bbd1e38 100644
--- a/big.c
+++ b/big.c
@@ -40,7 +40,6 @@ struct big_div {
word big_one;
-static word big_plus(word, word, int);
static word big_sub(word, word);
static word len(word);
static struct big_div big__longdiv(word, word);
@@ -50,6 +49,45 @@ static word msd(word);
static word shift(word, word);
static word stimes(word, word);
+/* ignore input signs, treat x,y as positive */
+word big__plus(word x, word y, int signbit)
+{
+ word d = digit0(x) + digit0(y);
+ 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) {
+ d = carry + digit(x) + digit(y);
+ carry = ((d & IBASE) != 0);
+ *z = make(INT, d & MAXDIGIT, 0);
+ x = rest(x);
+ y = rest(y);
+ z = &rest(*z);
+ }
+
+ if (y) {
+ x = y; /* by convention x is the longer one */
+ }
+
+ while (x) {
+ d = carry + digit(x);
+ carry = ((d & IBASE) != 0);
+ *z = make(INT, d & MAXDIGIT, 0);
+ x = rest(x);
+ z = &rest(*z);
+ }
+
+ if (carry) {
+ *z = make(INT, 1, 0);
+ }
+
+ return r;
+}
+
void bigsetup(void)
{
big_one = make(INT, 1, 0);
@@ -121,7 +159,7 @@ word bigplus(word x, word y)
{
if (big_is_positive(x)) {
if (big_is_positive(y)) {
- return (big_plus(x, y, 0));
+ return (big__plus(x, y, 0));
}
return (big_sub(x, y));
}
@@ -130,47 +168,9 @@ word bigplus(word x, word y)
return (big_sub(y, x));
}
- return (big_plus(x, y, SIGNBIT)); /* both negative */
+ return (big__plus(x, y, SIGNBIT)); /* both negative */
}
-/* ignore input signs, treat x,y as positive */
-word big_plus(word x, word y, int signbit)
-{
- word d = digit0(x) + digit0(y);
- 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) {
- d = carry + digit(x) + digit(y);
- carry = ((d & IBASE) != 0);
- *z = make(INT, d & MAXDIGIT, 0);
- x = rest(x);
- y = rest(y);
- z = &rest(*z);
- }
-
- if (y) {
- x = y; /* by convention x is the longer one */
- }
-
- while (x) {
- d = carry + digit(x);
- carry = ((d & IBASE) != 0);
- *z = make(INT, d & MAXDIGIT, 0);
- x = rest(x);
- z = &rest(*z);
- }
-
- if (carry) {
- *z = make(INT, 1, 0);
- }
-
- return r;
-}
word bigsub(word x, word y)
{
@@ -178,11 +178,11 @@ word bigsub(word x, word y)
if (big_is_positive(y)) {
return (big_sub(x, y)); /* both positive */
}
- return (big_plus(x, y, 0)); /* positive x, negative y */
+ return (big__plus(x, y, 0)); /* positive x, negative y */
}
if (big_is_positive(y)) {
- return (big_plus(x, y, SIGNBIT)); /* negative x, positive y */
+ return (big__plus(x, y, SIGNBIT)); /* negative x, positive y */
}
return (big_sub(y, x)); /* both negative */