[git commit] bc: simplify bc_num_ulong2num()

Denys Vlasenko vda.linux at googlemail.com
Mon Dec 10 11:22:15 UTC 2018


commit: https://git.busybox.net/busybox/commit/?id=b696d9ec2044a1fd7906c1b2a2a747aff7c3f79f
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
bc_num_ulong2num                                      64      59      -5

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 miscutils/bc.c | 17 +++++++++++++----
 1 file 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,


More information about the busybox-cvs mailing list