summaryrefslogtreecommitdiff
path: root/miscutils/bc.c
diff options
context:
space:
mode:
authorDenys Vlasenko2018-12-10 12:22:15 +0100
committerDenys Vlasenko2018-12-10 12:22:15 +0100
commitb696d9ec2044a1fd7906c1b2a2a747aff7c3f79f (patch)
tree0c283b76bc81cba83fb7973a9d19e0cc288b756b /miscutils/bc.c
parent9311e018030314e0c3c625c244b8d63e3ece6ed3 (diff)
downloadbusybox-b696d9ec2044a1fd7906c1b2a2a747aff7c3f79f.zip
busybox-b696d9ec2044a1fd7906c1b2a2a747aff7c3f79f.tar.gz
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/bc.c')
-rw-r--r--miscutils/bc.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 5b48f84..33abe93 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,