aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-12-21 22:36:04 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-12-21 22:36:04 +0100
commite8e7bda63aeefb17b59718baca592246e24d04df (patch)
tree2816d299f0e2531e628be62c5891b9086d86eb7b /miscutils
parent6ed7fb0a877d550428cb289feb3695dcd2812ab8 (diff)
downloadbusybox-w32-e8e7bda63aeefb17b59718baca592246e24d04df.tar.gz
busybox-w32-e8e7bda63aeefb17b59718baca592246e24d04df.tar.bz2
busybox-w32-e8e7bda63aeefb17b59718baca592246e24d04df.zip
bc: use non-allocated BcNum's where appropriate
function old new delta zbc_num_printNum 540 543 +3 zdc_program_asciify 407 403 -4 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 3/-4) Total: -1 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/bc.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index abedbdf0b..58e3bcb52 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -1454,6 +1454,14 @@ static BC_STATUS zbc_num_ulong(BcNum *n, unsigned long *result_p)
1454} 1454}
1455#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS) 1455#define zbc_num_ulong(...) (zbc_num_ulong(__VA_ARGS__) COMMA_SUCCESS)
1456 1456
1457#if ULONG_MAX == 0xffffffffUL // 10 digits: 4294967295
1458# define ULONG_NUM_BUFSIZE (10 > BC_NUM_DEF_SIZE ? 10 : BC_NUM_DEF_SIZE)
1459#elif ULONG_MAX == 0xffffffffffffffffULL // 20 digits: 18446744073709551615
1460# define ULONG_NUM_BUFSIZE (20 > BC_NUM_DEF_SIZE ? 20 : BC_NUM_DEF_SIZE)
1461#endif
1462// minimum BC_NUM_DEF_SIZE, so that bc_num_expand() in bc_num_ulong2num()
1463// would not hit realloc() code path - not good if num[] is not malloced
1464
1457static void bc_num_ulong2num(BcNum *n, unsigned long val) 1465static void bc_num_ulong2num(BcNum *n, unsigned long val)
1458{ 1466{
1459 BcDig *ptr; 1467 BcDig *ptr;
@@ -1462,11 +1470,7 @@ static void bc_num_ulong2num(BcNum *n, unsigned long val)
1462 1470
1463 if (val == 0) return; 1471 if (val == 0) return;
1464 1472
1465 if (ULONG_MAX == 0xffffffffUL) 1473 bc_num_expand(n, ULONG_NUM_BUFSIZE);
1466 bc_num_expand(n, 10); // 10 digits: 4294967295
1467 if (ULONG_MAX == 0xffffffffffffffffULL)
1468 bc_num_expand(n, 20); // 20 digits: 18446744073709551615
1469 BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffULL);
1470 1474
1471 ptr = n->num; 1475 ptr = n->num;
1472 for (;;) { 1476 for (;;) {
@@ -1645,7 +1649,6 @@ static BC_STATUS zbc_num_shift(BcNum *n, size_t places)
1645 1649
1646static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale) 1650static BC_STATUS zbc_num_inv(BcNum *a, BcNum *b, size_t scale)
1647{ 1651{
1648//TODO: nice example of non-allocated BcNum, use in other places as well!
1649 BcNum one; 1652 BcNum one;
1650 BcDig num[2]; 1653 BcDig num[2];
1651 1654
@@ -2264,6 +2267,7 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2264 BcStatus s; 2267 BcStatus s;
2265 BcNum temp, mult, result; 2268 BcNum temp, mult, result;
2266 BcNum base; 2269 BcNum base;
2270 BcDig base_digs[ULONG_NUM_BUFSIZE];
2267 BcDig c = '\0'; 2271 BcDig c = '\0';
2268 unsigned long v; 2272 unsigned long v;
2269 size_t i, digits; 2273 size_t i, digits;
@@ -2277,8 +2281,8 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2277 2281
2278 bc_num_init_DEF_SIZE(&temp); 2282 bc_num_init_DEF_SIZE(&temp);
2279 bc_num_init_DEF_SIZE(&mult); 2283 bc_num_init_DEF_SIZE(&mult);
2280//TODO: have BcNumSmall type, with static buffer 2284 base.cap = ARRAY_SIZE(base_digs);
2281 bc_num_init_DEF_SIZE(&base); 2285 base.num = base_digs;
2282 bc_num_ulong2num(&base, base_t); 2286 bc_num_ulong2num(&base, base_t);
2283 2287
2284 for (;;) { 2288 for (;;) {
@@ -2329,7 +2333,6 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
2329 err: 2333 err:
2330 bc_num_free(&result); 2334 bc_num_free(&result);
2331 int_err: 2335 int_err:
2332 bc_num_free(&base);
2333 bc_num_free(&mult); 2336 bc_num_free(&mult);
2334 bc_num_free(&temp); 2337 bc_num_free(&temp);
2335} 2338}
@@ -5430,6 +5433,7 @@ static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNum
5430 BcStatus s; 5433 BcStatus s;
5431 BcVec stack; 5434 BcVec stack;
5432 BcNum base; 5435 BcNum base;
5436 BcDig base_digs[ULONG_NUM_BUFSIZE];
5433 BcNum intp, fracp, digit, frac_len; 5437 BcNum intp, fracp, digit, frac_len;
5434 unsigned long dig, *ptr; 5438 unsigned long dig, *ptr;
5435 size_t i; 5439 size_t i;
@@ -5447,8 +5451,8 @@ static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNum
5447 bc_num_init(&frac_len, BC_NUM_INT(n)); 5451 bc_num_init(&frac_len, BC_NUM_INT(n));
5448 bc_num_copy(&intp, n); 5452 bc_num_copy(&intp, n);
5449 bc_num_one(&frac_len); 5453 bc_num_one(&frac_len);
5450//TODO: have BcNumSmall type, with static buffer 5454 base.cap = ARRAY_SIZE(base_digs);
5451 bc_num_init_DEF_SIZE(&base); 5455 base.num = base_digs;
5452 bc_num_ulong2num(&base, base_t); 5456 bc_num_ulong2num(&base, base_t);
5453 5457
5454 bc_num_truncate(&intp, intp.rdx); 5458 bc_num_truncate(&intp, intp.rdx);
@@ -5483,7 +5487,6 @@ static BC_STATUS zbc_num_printNum(BcNum *n, unsigned base_t, size_t width, BcNum
5483 if (s) goto err; 5487 if (s) goto err;
5484 } 5488 }
5485 err: 5489 err:
5486 bc_num_free(&base);
5487 bc_num_free(&frac_len); 5490 bc_num_free(&frac_len);
5488 bc_num_free(&digit); 5491 bc_num_free(&digit);
5489 bc_num_free(&fracp); 5492 bc_num_free(&fracp);
@@ -6206,16 +6209,16 @@ static BC_STATUS zdc_program_asciify(void)
6206 6209
6207 if (BC_PROG_NUM(r, num)) { 6210 if (BC_PROG_NUM(r, num)) {
6208 BcNum strmb; 6211 BcNum strmb;
6212 BcDig strmb_digs[ULONG_NUM_BUFSIZE];
6209 6213
6210 bc_num_init_DEF_SIZE(&n); 6214 bc_num_init_DEF_SIZE(&n);
6211 bc_num_copy(&n, num); 6215 bc_num_copy(&n, num);
6212 bc_num_truncate(&n, n.rdx); 6216 bc_num_truncate(&n, n.rdx);
6213 6217
6214//TODO: have BcNumSmall type, with static buffer 6218 strmb.cap = ARRAY_SIZE(strmb_digs);
6215 bc_num_init_DEF_SIZE(&strmb); 6219 strmb.num = strmb_digs;
6216 bc_num_ulong2num(&strmb, 0x100); 6220 bc_num_ulong2num(&strmb, 0x100);
6217 s = zbc_num_mod(&n, &strmb, &n, 0); 6221 s = zbc_num_mod(&n, &strmb, &n, 0);
6218 bc_num_free(&strmb);
6219 6222
6220 if (s) goto num_err; 6223 if (s) goto num_err;
6221 s = zbc_num_ulong(&n, &val); 6224 s = zbc_num_ulong(&n, &val);