bc: get rid of BcNum BcProgram::ib,hexb

function                                             old     new   delta
zbc_program_num                                      907     943     +36
zbc_program_assign                                   485     474     -11
bc_vm_init                                           757     739     -18
bc_num_ten                                            30       -     -30
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/2 up/down: 36/-59)            Total: -23 bytes
   text	   data	    bss	    dec	    hex	filename
 981532	    485	   7296	 989313	  f1881	busybox_old
 981509	    485	   7296	 989290	  f186a	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2018-12-17 21:05:09 +01:00
parent 0f31a5c79e
commit f6e3f8511e

View File

@ -228,7 +228,6 @@ typedef struct BcNum {
bool neg; bool neg;
} BcNum; } BcNum;
#define BC_NUM_MIN_BASE ((unsigned long) 2)
#define BC_NUM_MAX_IBASE ((unsigned long) 16) #define BC_NUM_MAX_IBASE ((unsigned long) 16)
// larger value might speed up BIGNUM calculations a bit: // larger value might speed up BIGNUM calculations a bit:
#define BC_NUM_DEF_SIZE (16) #define BC_NUM_DEF_SIZE (16)
@ -680,12 +679,9 @@ typedef struct BcProgram {
size_t len; size_t len;
size_t scale; size_t scale;
BcNum ib;
size_t ib_t; size_t ib_t;
BcNum ob;
size_t ob_t; size_t ob_t;
BcNum ob;
BcNum hexb;
#if ENABLE_DC #if ENABLE_DC
BcNum strmb; BcNum strmb;
@ -2240,10 +2236,11 @@ static void bc_num_parseDecimal(BcNum *n, const char *val)
// Note: n is already "bc_num_zero()"ed, // Note: n is already "bc_num_zero()"ed,
// leading zeroes in "val" are removed // leading zeroes in "val" are removed
static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base) static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
{ {
BcStatus s; BcStatus s;
BcNum temp, mult, result; BcNum temp, mult, result;
BcNum base;
BcDig c = '\0'; BcDig c = '\0';
unsigned long v; unsigned long v;
size_t i, digits; size_t i, digits;
@ -2257,6 +2254,8 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
bc_num_init_DEF_SIZE(&temp); bc_num_init_DEF_SIZE(&temp);
bc_num_init_DEF_SIZE(&mult); bc_num_init_DEF_SIZE(&mult);
bc_num_init_DEF_SIZE(&base);
bc_num_ulong2num(&base, base_t);
for (;;) { for (;;) {
c = *val++; c = *val++;
@ -2265,14 +2264,14 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
s = zbc_num_mul(n, base, &mult, 0); s = zbc_num_mul(n, &base, &mult, 0);
if (s) goto int_err; if (s) goto int_err;
bc_num_ulong2num(&temp, v); bc_num_ulong2num(&temp, v);
s = zbc_num_add(&mult, &temp, n, 0); s = zbc_num_add(&mult, &temp, n, 0);
if (s) goto int_err; if (s) goto int_err;
} }
bc_num_init(&result, base->len); bc_num_init(&result, base.len);
//bc_num_zero(&result); - already is //bc_num_zero(&result); - already is
bc_num_one(&mult); bc_num_one(&mult);
@ -2284,12 +2283,12 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
s = zbc_num_mul(&result, base, &result, 0); s = zbc_num_mul(&result, &base, &result, 0);
if (s) goto err; if (s) goto err;
bc_num_ulong2num(&temp, v); bc_num_ulong2num(&temp, v);
s = zbc_num_add(&result, &temp, &result, 0); s = zbc_num_add(&result, &temp, &result, 0);
if (s) goto err; if (s) goto err;
s = zbc_num_mul(&mult, base, &mult, 0); s = zbc_num_mul(&mult, &base, &mult, 0);
if (s) goto err; if (s) goto err;
} }
@ -2306,12 +2305,12 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
err: err:
bc_num_free(&result); bc_num_free(&result);
int_err: int_err:
bc_num_free(&base);
bc_num_free(&mult); bc_num_free(&mult);
bc_num_free(&temp); bc_num_free(&temp);
} }
static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base, static BC_STATUS zbc_num_parse(BcNum *n, const char *val, unsigned base_t)
size_t base_t)
{ {
if (!bc_num_strValid(val, base_t)) if (!bc_num_strValid(val, base_t))
RETURN_STATUS(bc_error("bad number string")); RETURN_STATUS(bc_error("bad number string"));
@ -2322,7 +2321,7 @@ static BC_STATUS zbc_num_parse(BcNum *n, const char *val, BcNum *base,
if (base_t == 10) if (base_t == 10)
bc_num_parseDecimal(n, val); bc_num_parseDecimal(n, val);
else else
bc_num_parseBase(n, val, base); bc_num_parseBase(n, val, base_t);
RETURN_STATUS(BC_STATUS_SUCCESS); RETURN_STATUS(BC_STATUS_SUCCESS);
} }
@ -5044,15 +5043,14 @@ static BC_STATUS zbc_program_num(BcResult *r, BcNum **num, bool hex)
{ {
BcStatus s; BcStatus s;
char **str = bc_vec_item(&G.prog.consts, r->d.id.idx); char **str = bc_vec_item(&G.prog.consts, r->d.id.idx);
size_t base_t, len = strlen(*str); unsigned base_t;
BcNum *base; size_t len = strlen(*str);
bc_num_init(&r->d.n, len); bc_num_init(&r->d.n, len);
hex = hex && len == 1; hex = hex && len == 1;
base = hex ? &G.prog.hexb : &G.prog.ib; base_t = hex ? 16 : G.prog.ib_t;
base_t = hex ? BC_NUM_MAX_IBASE : G.prog.ib_t; s = zbc_num_parse(&r->d.n, *str, base_t);
s = zbc_num_parse(&r->d.n, *str, base, base_t);
if (s) { if (s) {
bc_num_free(&r->d.n); bc_num_free(&r->d.n);
@ -5475,8 +5473,7 @@ static BC_STATUS zbc_num_printBase(BcNum *n)
if (G.prog.ob_t <= BC_NUM_MAX_IBASE) { if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
width = 1; width = 1;
print = bc_num_printHex; print = bc_num_printHex;
} } else {
else {
for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width) for (i = G.prog.ob_t - 1, width = 0; i != 0; i /= 10, ++width)
continue; continue;
print = bc_num_printDigits; print = bc_num_printDigits;
@ -5506,8 +5503,7 @@ static BC_STATUS zbc_num_print(BcNum *n, bool newline)
if (n->len == 0) { if (n->len == 0) {
bb_putchar('0'); bb_putchar('0');
++G.prog.nchars; ++G.prog.nchars;
} } else if (G.prog.ob_t == 10)
else if (G.prog.ob_t == 10)
bc_num_printDecimal(n); bc_num_printDecimal(n);
else else
s = zbc_num_printBase(n); s = zbc_num_printBase(n);
@ -5765,7 +5761,8 @@ static BC_STATUS zbc_program_assign(char inst)
"bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE "bad obase; must be [2,"BC_MAX_OBASE_STR"]", //BC_RESULT_OBASE
}; };
size_t *ptr; size_t *ptr;
unsigned long val, max; size_t max;
unsigned long val;
s = zbc_num_ulong(l, &val); s = zbc_num_ulong(l, &val);
if (s) RETURN_STATUS(s); if (s) RETURN_STATUS(s);
@ -5773,9 +5770,8 @@ static BC_STATUS zbc_program_assign(char inst)
if (sc) { if (sc) {
max = BC_MAX_SCALE; max = BC_MAX_SCALE;
ptr = &G.prog.scale; ptr = &G.prog.scale;
} } else {
else { if (val < 2)
if (val < BC_NUM_MIN_BASE)
RETURN_STATUS(bc_error(msg[s])); RETURN_STATUS(bc_error(msg[s]));
max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE; max = ib ? BC_NUM_MAX_IBASE : BC_MAX_OBASE;
ptr = ib ? &G.prog.ib_t : &G.prog.ob_t; ptr = ib ? &G.prog.ib_t : &G.prog.ob_t;
@ -5783,8 +5779,8 @@ static BC_STATUS zbc_program_assign(char inst)
if (val > max) if (val > max)
RETURN_STATUS(bc_error(msg[s])); RETURN_STATUS(bc_error(msg[s]));
if (!sc) if (!sc && !ib)
bc_num_copy(ib ? &G.prog.ib : &G.prog.ob, l); bc_num_copy(&G.prog.ob, l);
*ptr = (size_t) val; *ptr = (size_t) val;
s = BC_STATUS_SUCCESS; s = BC_STATUS_SUCCESS;
@ -7057,9 +7053,7 @@ static BC_STATUS zbc_vm_exec(void)
#if ENABLE_FEATURE_CLEAN_UP #if ENABLE_FEATURE_CLEAN_UP
static void bc_program_free(void) static void bc_program_free(void)
{ {
bc_num_free(&G.prog.ib);
bc_num_free(&G.prog.ob); bc_num_free(&G.prog.ob);
bc_num_free(&G.prog.hexb);
# if ENABLE_DC # if ENABLE_DC
bc_num_free(&G.prog.strmb); bc_num_free(&G.prog.strmb);
# endif # endif
@ -7097,18 +7091,12 @@ static void bc_program_init(void)
memset(&ip, 0, sizeof(BcInstPtr)); memset(&ip, 0, sizeof(BcInstPtr));
// G.prog.nchars = G.prog.scale = 0; - already is // G.prog.nchars = G.prog.scale = 0; - already is
bc_num_init_DEF_SIZE(&G.prog.ib);
bc_num_ten(&G.prog.ib);
G.prog.ib_t = 10; G.prog.ib_t = 10;
bc_num_init_DEF_SIZE(&G.prog.ob); bc_num_init_DEF_SIZE(&G.prog.ob);
bc_num_ten(&G.prog.ob); bc_num_ten(&G.prog.ob);
G.prog.ob_t = 10; G.prog.ob_t = 10;
bc_num_init_DEF_SIZE(&G.prog.hexb);
bc_num_ten(&G.prog.hexb);
G.prog.hexb.num[0] = 6;
#if ENABLE_DC #if ENABLE_DC
bc_num_init_DEF_SIZE(&G.prog.strmb); bc_num_init_DEF_SIZE(&G.prog.strmb);
bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);