aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/bc.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 0dc3f843c..2e3c4139e 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -1407,6 +1407,11 @@ static void bc_num_init(BcNum *n, size_t req)
1407 n->cap = req; 1407 n->cap = req;
1408} 1408}
1409 1409
1410static void bc_num_init_DEF_SIZE(BcNum *n)
1411{
1412 bc_num_init(n, BC_NUM_DEF_SIZE);
1413}
1414
1410static void bc_num_expand(BcNum *n, size_t req) 1415static void bc_num_expand(BcNum *n, size_t req)
1411{ 1416{
1412 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; 1417 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
@@ -2260,8 +2265,8 @@ static void bc_num_parseBase(BcNum *n, const char *val, BcNum *base)
2260 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); 2265 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
2261 if (zero) return; 2266 if (zero) return;
2262 2267
2263 bc_num_init(&temp, BC_NUM_DEF_SIZE); 2268 bc_num_init_DEF_SIZE(&temp);
2264 bc_num_init(&mult, BC_NUM_DEF_SIZE); 2269 bc_num_init_DEF_SIZE(&mult);
2265 2270
2266 for (i = 0; i < len; ++i) { 2271 for (i = 0; i < len; ++i) {
2267 2272
@@ -2584,7 +2589,7 @@ static BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale)
2584 2589
2585 bc_num_init(&num1, len); 2590 bc_num_init(&num1, len);
2586 bc_num_init(&num2, len); 2591 bc_num_init(&num2, len);
2587 bc_num_init(&half, BC_NUM_DEF_SIZE); 2592 bc_num_init_DEF_SIZE(&half);
2588 2593
2589 bc_num_one(&half); 2594 bc_num_one(&half);
2590 half.num[0] = 5; 2595 half.num[0] = 5;
@@ -2701,7 +2706,7 @@ static BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d)
2701 bc_num_expand(d, c->len); 2706 bc_num_expand(d, c->len);
2702 bc_num_init(&base, c->len); 2707 bc_num_init(&base, c->len);
2703 bc_num_init(&exp, b->len); 2708 bc_num_init(&exp, b->len);
2704 bc_num_init(&two, BC_NUM_DEF_SIZE); 2709 bc_num_init_DEF_SIZE(&two);
2705 bc_num_init(&temp, b->len); 2710 bc_num_init(&temp, b->len);
2706 2711
2707 bc_num_one(&two); 2712 bc_num_one(&two);
@@ -2792,7 +2797,7 @@ static void bc_array_expand(BcVec *a, size_t len)
2792 2797
2793 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) { 2798 if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
2794 while (len > a->len) { 2799 while (len > a->len) {
2795 bc_num_init(&data.n, BC_NUM_DEF_SIZE); 2800 bc_num_init_DEF_SIZE(&data.n);
2796 bc_vec_push(a, &data.n); 2801 bc_vec_push(a, &data.n);
2797 } 2802 }
2798 } 2803 }
@@ -5530,7 +5535,7 @@ static BcStatus bc_program_op(char inst)
5530 5535
5531 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); 5536 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
5532 if (s) return s; 5537 if (s) return s;
5533 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 5538 bc_num_init_DEF_SIZE(&res.d.n);
5534 5539
5535 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale); 5540 s = bc_program_ops[inst - BC_INST_POWER](n1, n2, &res.d.n, G.prog.scale);
5536 if (s) goto err; 5541 if (s) goto err;
@@ -5784,7 +5789,7 @@ static BcStatus bc_program_logical(char inst)
5784 5789
5785 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); 5790 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
5786 if (s) return s; 5791 if (s) return s;
5787 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 5792 bc_num_init_DEF_SIZE(&res.d.n);
5788 5793
5789 if (inst == BC_INST_BOOL_AND) 5794 if (inst == BC_INST_BOOL_AND)
5790 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero); 5795 cond = bc_num_cmp(n1, &G.prog.zero) && bc_num_cmp(n2, &G.prog.zero);
@@ -5896,7 +5901,7 @@ static BcStatus bc_program_copyToVar(char *name, bool var)
5896 v = bc_program_search(name, var); 5901 v = bc_program_search(name, var);
5897 5902
5898 if (var) { 5903 if (var) {
5899 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); 5904 bc_num_init_DEF_SIZE(&r.d.n);
5900 bc_num_copy(&r.d.n, n); 5905 bc_num_copy(&r.d.n, n);
5901 } 5906 }
5902 else { 5907 else {
@@ -6035,7 +6040,7 @@ static BcStatus bc_program_pushVar(char *code, size_t *bgn,
6035 6040
6036 r.t = BC_RESULT_TEMP; 6041 r.t = BC_RESULT_TEMP;
6037 6042
6038 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); 6043 bc_num_init_DEF_SIZE(&r.d.n);
6039 bc_num_copy(&r.d.n, num); 6044 bc_num_copy(&r.d.n, num);
6040 } 6045 }
6041 else { 6046 else {
@@ -6164,7 +6169,7 @@ static BcStatus bc_program_call(char *code, size_t *idx)
6164 v = bc_program_search(a->name, a->idx); 6169 v = bc_program_search(a->name, a->idx);
6165 6170
6166 if (a->idx) { 6171 if (a->idx) {
6167 bc_num_init(&param.n, BC_NUM_DEF_SIZE); 6172 bc_num_init_DEF_SIZE(&param.n);
6168 bc_vec_push(v, &param.n); 6173 bc_vec_push(v, &param.n);
6169 } 6174 }
6170 else { 6175 else {
@@ -6203,7 +6208,7 @@ static BcStatus bc_program_return(char inst)
6203 bc_num_copy(&res.d.n, num); 6208 bc_num_copy(&res.d.n, num);
6204 } 6209 }
6205 else { 6210 else {
6206 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 6211 bc_num_init_DEF_SIZE(&res.d.n);
6207 bc_num_zero(&res.d.n); 6212 bc_num_zero(&res.d.n);
6208 } 6213 }
6209 6214
@@ -6261,7 +6266,7 @@ static BcStatus bc_program_builtin(char inst)
6261 return bc_error_variable_is_wrong_type(); 6266 return bc_error_variable_is_wrong_type();
6262#endif 6267#endif
6263 6268
6264 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 6269 bc_num_init_DEF_SIZE(&res.d.n);
6265 6270
6266 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale); 6271 if (inst == BC_INST_SQRT) s = bc_num_sqrt(num, &res.d.n, G.prog.scale);
6267#if ENABLE_BC 6272#if ENABLE_BC
@@ -6299,7 +6304,7 @@ static BcStatus bc_program_divmod(void)
6299 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false); 6304 s = bc_program_binOpPrep(&opd1, &n1, &opd2, &n2, false);
6300 if (s) return s; 6305 if (s) return s;
6301 6306
6302 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 6307 bc_num_init_DEF_SIZE(&res.d.n);
6303 bc_num_init(&res2.d.n, n2->len); 6308 bc_num_init(&res2.d.n, n2->len);
6304 6309
6305 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale); 6310 s = bc_num_divmod(n1, n2, &res2.d.n, &res.d.n, G.prog.scale);
@@ -6369,7 +6374,7 @@ static void bc_program_stackLen(void)
6369 6374
6370 res.t = BC_RESULT_TEMP; 6375 res.t = BC_RESULT_TEMP;
6371 6376
6372 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 6377 bc_num_init_DEF_SIZE(&res.d.n);
6373 bc_num_ulong2num(&res.d.n, len); 6378 bc_num_ulong2num(&res.d.n, len);
6374 bc_vec_push(&G.prog.results, &res); 6379 bc_vec_push(&G.prog.results, &res);
6375} 6380}
@@ -6392,7 +6397,7 @@ static BcStatus bc_program_asciify(void)
6392 6397
6393 if (BC_PROG_NUM(r, num)) { 6398 if (BC_PROG_NUM(r, num)) {
6394 6399
6395 bc_num_init(&n, BC_NUM_DEF_SIZE); 6400 bc_num_init_DEF_SIZE(&n);
6396 bc_num_copy(&n, num); 6401 bc_num_copy(&n, num);
6397 bc_num_truncate(&n, n.rdx); 6402 bc_num_truncate(&n, n.rdx);
6398 6403
@@ -6610,7 +6615,7 @@ static void bc_program_pushGlobal(char inst)
6610 else 6615 else
6611 val = (unsigned long) G.prog.ob_t; 6616 val = (unsigned long) G.prog.ob_t;
6612 6617
6613 bc_num_init(&res.d.n, BC_NUM_DEF_SIZE); 6618 bc_num_init_DEF_SIZE(&res.d.n);
6614 bc_num_ulong2num(&res.d.n, val); 6619 bc_num_ulong2num(&res.d.n, val);
6615 bc_vec_push(&G.prog.results, &res); 6620 bc_vec_push(&G.prog.results, &res);
6616} 6621}
@@ -6760,7 +6765,7 @@ static BcStatus bc_program_exec(void)
6760 case BC_INST_BOOL_NOT: 6765 case BC_INST_BOOL_NOT:
6761 s = bc_program_prep(&ptr, &num); 6766 s = bc_program_prep(&ptr, &num);
6762 if (s) return s; 6767 if (s) return s;
6763 bc_num_init(&r.d.n, BC_NUM_DEF_SIZE); 6768 bc_num_init_DEF_SIZE(&r.d.n);
6764 if (!bc_num_cmp(num, &G.prog.zero)) 6769 if (!bc_num_cmp(num, &G.prog.zero))
6765 bc_num_one(&r.d.n); 6770 bc_num_one(&r.d.n);
6766 else 6771 else
@@ -7356,31 +7361,30 @@ static void bc_program_init(void)
7356 memset(&ip, 0, sizeof(BcInstPtr)); 7361 memset(&ip, 0, sizeof(BcInstPtr));
7357 7362
7358 /* G.prog.nchars = G.prog.scale = 0; - already is */ 7363 /* G.prog.nchars = G.prog.scale = 0; - already is */
7359 7364 bc_num_init_DEF_SIZE(&G.prog.ib);
7360 bc_num_init(&G.prog.ib, BC_NUM_DEF_SIZE);
7361 bc_num_ten(&G.prog.ib); 7365 bc_num_ten(&G.prog.ib);
7362 G.prog.ib_t = 10; 7366 G.prog.ib_t = 10;
7363 7367
7364 bc_num_init(&G.prog.ob, BC_NUM_DEF_SIZE); 7368 bc_num_init_DEF_SIZE(&G.prog.ob);
7365 bc_num_ten(&G.prog.ob); 7369 bc_num_ten(&G.prog.ob);
7366 G.prog.ob_t = 10; 7370 G.prog.ob_t = 10;
7367 7371
7368 bc_num_init(&G.prog.hexb, BC_NUM_DEF_SIZE); 7372 bc_num_init_DEF_SIZE(&G.prog.hexb);
7369 bc_num_ten(&G.prog.hexb); 7373 bc_num_ten(&G.prog.hexb);
7370 G.prog.hexb.num[0] = 6; 7374 G.prog.hexb.num[0] = 6;
7371 7375
7372#if ENABLE_DC 7376#if ENABLE_DC
7373 bc_num_init(&G.prog.strmb, BC_NUM_DEF_SIZE); 7377 bc_num_init_DEF_SIZE(&G.prog.strmb);
7374 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1); 7378 bc_num_ulong2num(&G.prog.strmb, UCHAR_MAX + 1);
7375#endif 7379#endif
7376 7380
7377 bc_num_init(&G.prog.last, BC_NUM_DEF_SIZE); 7381 bc_num_init_DEF_SIZE(&G.prog.last);
7378 bc_num_zero(&G.prog.last); 7382 bc_num_zero(&G.prog.last);
7379 7383
7380 bc_num_init(&G.prog.zero, BC_NUM_DEF_SIZE); 7384 bc_num_init_DEF_SIZE(&G.prog.zero);
7381 bc_num_zero(&G.prog.zero); 7385 bc_num_zero(&G.prog.zero);
7382 7386
7383 bc_num_init(&G.prog.one, BC_NUM_DEF_SIZE); 7387 bc_num_init_DEF_SIZE(&G.prog.one);
7384 bc_num_one(&G.prog.one); 7388 bc_num_one(&G.prog.one);
7385 7389
7386 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free); 7390 bc_vec_init(&G.prog.fns, sizeof(BcFunc), bc_func_free);