From 2b0d84761577dea4fc5282593305440290ab1696 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 14:16:57 -0400 Subject: modernize big_sub() --- big.c | 59 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 17 deletions(-) (limited to 'big.c') diff --git a/big.c b/big.c index c35ce99..49d9598 100644 --- a/big.c +++ b/big.c @@ -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 */ -- cgit v1.2.1