diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-10 12:22:15 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-12-10 12:22:15 +0100 |
commit | b696d9ec2044a1fd7906c1b2a2a747aff7c3f79f (patch) | |
tree | 0c283b76bc81cba83fb7973a9d19e0cc288b756b /miscutils | |
parent | 9311e018030314e0c3c625c244b8d63e3ece6ed3 (diff) |
bc: simplify bc_num_ulong2num()
function old new delta
bc_num_ulong2num 64 59 -5
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r-- | miscutils/bc.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c index 5b48f84a1..33abe9366 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -1467,16 +1467,25 @@ static BcStatus bc_num_ulong(BcNum *n, unsigned long *result_p) static void bc_num_ulong2num(BcNum *n, unsigned long val) { - size_t len; BcDig *ptr; - unsigned long i; bc_num_zero(n); if (val == 0) return; - for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) bc_num_expand(n, len); - for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10; + if (ULONG_MAX == 0xffffffffUL) + bc_num_expand(n, 10); // 10 digits: 4294967295 + if (ULONG_MAX == 0xffffffffffffffffUL) + bc_num_expand(n, 20); // 20 digits: 18446744073709551615 + BUILD_BUG_ON(ULONG_MAX > 0xffffffffffffffffUL); + + ptr = n->num; + for (;;) { + n->len++; + *ptr++ = val % 10; + val /= 10; + if (val == 0) break; + } } static void bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, |