From 49be4a0329b218a7c35c9275680de3ca177cd2e7 Mon Sep 17 00:00:00 2001 From: Ondrej Zajicek Date: Fri, 8 Nov 2024 18:54:13 +0100 Subject: [PATCH] Lib: Optimized printing of numbers One simple trick makes printing of numbers ~4x faster. --- lib/printf.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/printf.c b/lib/printf.c index 424d545f..68d3bb74 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -73,10 +73,25 @@ static char * number(char * str, u64 num, uint base, int size, int precision, i = 0; if (num == 0) tmp[i++]='0'; - else while (num != 0) { - uint res = num % base; - num = num / base; - tmp[i++] = digits[res]; + else if (base == 10) { + /* Separate cases to have fixed divisors */ + while (num != 0) { + uint res = num % 10; + num = num / 10; + tmp[i++] = digits[res]; + } + } else if (base == 16) { + while (num != 0) { + uint res = num % 16; + num = num / 16; + tmp[i++] = digits[res]; + } + } else { + while (num != 0) { + uint res = num % base; + num = num / base; + tmp[i++] = digits[res]; + } } if (i > precision) precision = i;