diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 16:21:24 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 16:21:24 -0400 |
commit | 2f867c236c40b5ad41132ff59693801f96007c95 (patch) | |
tree | b5bf85294ac6e733cdbd2513352fdd94f61523f3 | |
parent | 78b0ad5c296c186b04926621a7663a5c5bc139b6 (diff) |
move big_plus() to top to avoid redundant static decl, rename big__plus()
-rw-r--r-- | big.c | 86 |
1 files changed, 43 insertions, 43 deletions
@@ -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 */ |