diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-12 11:51:32 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-12 11:51:32 +0100 |
commit | 251fbb57be45a8c4835f90f7126fa9fd27176373 (patch) | |
tree | 32627f771381c5e139a1a0caffa94dffdc1d5324 | |
parent | 16494f557fd742ca484943407888fb8a349c01ee (diff) | |
download | busybox-w32-251fbb57be45a8c4835f90f7126fa9fd27176373.tar.gz busybox-w32-251fbb57be45a8c4835f90f7126fa9fd27176373.tar.bz2 busybox-w32-251fbb57be45a8c4835f90f7126fa9fd27176373.zip |
bc: code shrink in bc_num_cmp()
function old new delta
bc_num_cmp 275 231 -44
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/bc.c | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 8426998d0..8f326d20e 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c | |||
@@ -1601,46 +1601,44 @@ static ssize_t bc_num_cmp(BcNum *a, BcNum *b) | |||
1601 | { | 1601 | { |
1602 | size_t i, min, a_int, b_int, diff; | 1602 | size_t i, min, a_int, b_int, diff; |
1603 | BcDig *max_num, *min_num; | 1603 | BcDig *max_num, *min_num; |
1604 | bool a_max, neg = false; | 1604 | bool a_max, neg; |
1605 | ssize_t cmp; | 1605 | ssize_t cmp; |
1606 | 1606 | ||
1607 | if (a == b) return 0; | 1607 | if (a == b) return 0; |
1608 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); | 1608 | if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg); |
1609 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); | 1609 | if (b->len == 0) return BC_NUM_NEG(1, a->neg); |
1610 | if (a->neg) { | 1610 | |
1611 | if (b->neg) | 1611 | if (a->neg != b->neg) // signs of a and b differ |
1612 | neg = true; | 1612 | // +a,-b = a>b = 1 or -a,+b = a<b = -1 |
1613 | else | 1613 | return (int)b->neg - (int)a->neg; |
1614 | return -1; | 1614 | neg = a->neg; // 1 if both negative, 0 if both positive |
1615 | } | ||
1616 | else if (b->neg) | ||
1617 | return 1; | ||
1618 | 1615 | ||
1619 | a_int = BC_NUM_INT(a); | 1616 | a_int = BC_NUM_INT(a); |
1620 | b_int = BC_NUM_INT(b); | 1617 | b_int = BC_NUM_INT(b); |
1621 | a_int -= b_int; | 1618 | a_int -= b_int; |
1622 | a_max = (a->rdx > b->rdx); | ||
1623 | 1619 | ||
1624 | if (a_int != 0) return (ssize_t) a_int; | 1620 | if (a_int != 0) return (ssize_t) a_int; |
1625 | 1621 | ||
1622 | a_max = (a->rdx > b->rdx); | ||
1626 | if (a_max) { | 1623 | if (a_max) { |
1627 | min = b->rdx; | 1624 | min = b->rdx; |
1628 | diff = a->rdx - b->rdx; | 1625 | diff = a->rdx - b->rdx; |
1629 | max_num = a->num + diff; | 1626 | max_num = a->num + diff; |
1630 | min_num = b->num; | 1627 | min_num = b->num; |
1631 | } | 1628 | // neg = (a_max == neg); - NOP (maps 1->1 and 0->0) |
1632 | else { | 1629 | } else { |
1633 | min = a->rdx; | 1630 | min = a->rdx; |
1634 | diff = b->rdx - a->rdx; | 1631 | diff = b->rdx - a->rdx; |
1635 | max_num = b->num + diff; | 1632 | max_num = b->num + diff; |
1636 | min_num = a->num; | 1633 | min_num = a->num; |
1634 | neg = !neg; // same as "neg = (a_max == neg)" | ||
1637 | } | 1635 | } |
1638 | 1636 | ||
1639 | cmp = bc_num_compare(max_num, min_num, b_int + min); | 1637 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
1640 | if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg); | 1638 | if (cmp != 0) return BC_NUM_NEG(cmp, neg); |
1641 | 1639 | ||
1642 | for (max_num -= diff, i = diff - 1; i < diff; --i) { | 1640 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
1643 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); | 1641 | if (max_num[i]) return BC_NUM_NEG(1, neg); |
1644 | } | 1642 | } |
1645 | 1643 | ||
1646 | return 0; | 1644 | return 0; |