bc: avoid having to twiddle b->neg in zbc_num_p()
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 22 Dec 2018 20:52:30 +0000 (21:52 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 22 Dec 2018 20:52:30 +0000 (21:52 +0100)
function                                             old     new   delta
zbc_num_ulong_abs                                      -      70     +70
zbc_num_p                                            424     413     -11
zbc_num_ulong                                         81      21     -60
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 70/-71)             Total: -1 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
miscutils/bc.c

index d7595ce8ef4c310824f7105b52e44bce1cdfc3dc..ae5105b01ff36930f6b911acb222ed90f275350e 100644 (file)
@@ -1439,13 +1439,11 @@ static void bc_num_copy(BcNum *d, BcNum *s)
        }
 }
 
-static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
+static BC_STATUS zbc_num_ulong_abs(BcNum *n, unsigned long *result_p)
 {
        size_t i;
        unsigned long result;
 
-       if (n->neg) RETURN_STATUS(bc_error("negative number"));
-
        result = 0;
        i = n->len;
        while (i > n->rdx) {
@@ -1462,6 +1460,14 @@ static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
 
        RETURN_STATUS(BC_STATUS_SUCCESS);
 }
+#define zbc_num_ulong_abs(...) (zbc_num_ulong_abs(__VA_ARGS__) COMMA_SUCCESS)
+
+static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
+{
+       if (n->neg) RETURN_STATUS(bc_error("negative number"));
+
+       RETURN_STATUS(zbc_num_ulong_abs(n, result_p));
+}
 #define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
 
 #if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
@@ -2109,9 +2115,7 @@ static FAST_FUNC BC_STATUS zbc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size
        }
 
        neg = b->neg;
-       b->neg = false;
-       s = zbc_num_ulong(b, &pow);
-       b->neg = neg;
+       s = zbc_num_ulong_abs(b, &pow);
        if (s) RETURN_STATUS(s);
        // b is not used beyond this point
 
@@ -6150,7 +6154,6 @@ static BC_STATUS zdc_program_asciify(void)
        char *str;
        char c;
        size_t idx;
-       unsigned long val;
 
        if (!STACK_HAS_MORE_THAN(&G.prog.results, 0))
                RETURN_STATUS(bc_error_stack_has_too_few_elements());
@@ -6160,6 +6163,7 @@ static BC_STATUS zdc_program_asciify(void)
        if (s) RETURN_STATUS(s);
 
        if (BC_PROG_NUM(r, num)) {
+               unsigned long val;
                BcNum strmb;
                BcDig strmb_digs[ULONG_NUM_BUFSIZE];