diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:16:57 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 14:16:57 -0400 |
commit | 2b0d84761577dea4fc5282593305440290ab1696 (patch) | |
tree | f70d20dc17d79618b44af5622bd03281e7810fd9 | |
parent | db28fcf89f08307ca5f13cbb9d562ac0e5f9431e (diff) |
modernize big_sub()
-rw-r--r-- | big.c | 59 |
1 files changed, 42 insertions, 17 deletions
@@ -171,8 +171,8 @@ word bigsub(word x, word y) return (big_sub(y, x)); /* both negative */ } -word big_sub(x, y) /* ignore input signs, treat x,y as positive */ -word x, y; +/* ignore input signs, treat x,y as positive */ +word big_sub(word x, word y) { word d = digit0(x) - digit0(y); word borrow = (d & IBASE) != 0; @@ -181,63 +181,88 @@ word x, y; word *p = NULL; /* pointer to trailing zeros, if any */ x = rest(x); y = rest(y); - while (x && y) { /* this loop has been unwrapped once, see above */ + + /* this loop has been unwrapped once, see above */ + while (x && y) { d = digit(x) - digit(y) - borrow; borrow = (d & IBASE) != 0; d = d & MAXDIGIT; *z = make(INT, d, 0); - if (d) + + if (d) { p = NULL; - else if (!p) + } else if (!p) { p = z; + } + x = rest(x); y = rest(y); z = &rest(*z); } - while (y) { /* at most one of these two loops will be invoked */ + + /* at most one of these two loops will be invoked */ + while (y) { d = -digit(y) - borrow; borrow = ((d & IBASE) != 0); d = d & MAXDIGIT; *z = make(INT, d, 0); - if (d) + + if (d) { p = NULL; - else if (!p) + } else if (!p) { p = z; + } + y = rest(y); z = &rest(*z); } - while (x) { /* alternative loop */ + + /* alternative loop */ + while (x) { d = digit(x) - borrow; borrow = ((d & IBASE) != 0); d = d & MAXDIGIT; *z = make(INT, d, 0); - if (d) + + if (d) { p = NULL; - else if (!p) + } else if (!p) { p = z; + } + x = rest(x); z = &rest(*z); } - if (borrow) { /* result is negative - take complement and add 1 */ + + /* result is negative - take complement and add 1 */ + if (borrow) { p = NULL; d = (digit(r) ^ MAXDIGIT) + 1; borrow = ((d & IBASE) != 0); /* borrow now means `carry' (sorry) */ digit(r) = SIGNBIT | d; /* set sign bit of result */ z = &rest(r); + while (*z) { d = (digit(*z) ^ MAXDIGIT) + borrow; borrow = ((d & IBASE) != 0); digit(*z) = d = d & MAXDIGIT; - if (d) + + if (d) { p = NULL; - else if (!p) + } else if (!p) { p = z; + } + z = &rest(*z); } } - if (p) - *p = 0; /* remove redundant (ie trailing) zeros */ - return (r); + + /* remove redundant (ie trailing) zeros */ + if (p) { + *p = 0; + } + + return r; } int bigcmp(x, y) /* returns +ve,0,-ve as x greater than, equal, less than y */ |