diff mbox series

[1/2] mtd-utils: common.c: convert to integer arithmetic

Message ID 58232c8a9192b7fc279389099d2cd1ff7b5abcd3.1516868053.git.andrea.adami@gmail.com
State Superseded
Delegated to: David Oberhollenzer
Headers show
Series mtd-utils: improve compatibility with klibc | expand

Commit Message

Andrea Adami Jan. 25, 2018, 8:52 a.m. UTC
We use floating point just to print out KiB, MiB, GiB.
Avoid that to be klibc friendly.

Fixes compilation for aarch64 against klibc:

error: '-mgeneral-regs-only' is incompatible with floating-point argument
|    printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024));
etc.

Note:
* In the KiB case, we could apparently multiply by 100 before dividing
  without risking overflow. This code simply avoids multiplications.

Signed-off-by: Andrea Adami <andrea.adami@gmail.com>
---
 lib/common.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

Comments

Andrea Adami Jan. 28, 2018, 12:37 a.m. UTC | #1
On Thu, Jan 25, 2018 at 9:52 AM, Andrea Adami <andrea.adami@gmail.com> wrote:
> We use floating point just to print out KiB, MiB, GiB.
> Avoid that to be klibc friendly.
>
> Fixes compilation for aarch64 against klibc:
>
> error: '-mgeneral-regs-only' is incompatible with floating-point argument
> |    printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024));
> etc.
>
> Note:
> * In the KiB case, we could apparently multiply by 100 before dividing
>   without risking overflow. This code simply avoids multiplications.
>
> Signed-off-by: Andrea Adami <andrea.adami@gmail.com>
> ---
>  lib/common.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/lib/common.c b/lib/common.c
> index 69b03b3..5e28853 100644
> --- a/lib/common.c
> +++ b/lib/common.c
> @@ -107,6 +107,9 @@ long long util_get_bytes(const char *str)
>  void util_print_bytes(long long bytes, int bracket)
>  {
>         const char *p;
> +       int GiB = 1024 * 1024 * 1024;
> +       int MiB = 1024 * 1024;
> +       int KiB = 1024;
>
>         if (bracket)
>                 p = " (";
> @@ -115,12 +118,12 @@ void util_print_bytes(long long bytes, int bracket)
>
>         printf("%lld bytes", bytes);
>
> -       if (bytes > 1024 * 1024 * 1024)
> -               printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024));
> -       else if (bytes > 1024 * 1024)
> -               printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024));
> -       else if (bytes > 1024 && bytes != 0)
> -               printf("%s%.1f KiB", p, (double)bytes / 1024);
> +       if (bytes > GiB)
> +               printf("%s%d.%d GiB", p, bytes / GiB, bytes % GiB / (GiB / 10));
> +       else if (bytes > MiB)
> +               printf("%s%d.%d MiB", p, bytes / MiB, bytes % MiB / (MiB / 10));
> +       else if (bytes > KiB && bytes != 0)
> +               printf("%s%d.%d KiB", p, bytes / KiB, bytes % KiB / (KiB / 10));
>         else
>                 return;
>
> --
> 2.7.4
>

Missing cast to integer, the right specifier here would be %lld and not %d.
Building 1.5.2-klibc I had no warnings but now I see them with 2.0.1.
I'll send a v2.

Regards
Andrea
diff mbox series

Patch

diff --git a/lib/common.c b/lib/common.c
index 69b03b3..5e28853 100644
--- a/lib/common.c
+++ b/lib/common.c
@@ -107,6 +107,9 @@  long long util_get_bytes(const char *str)
 void util_print_bytes(long long bytes, int bracket)
 {
 	const char *p;
+	int GiB = 1024 * 1024 * 1024;
+	int MiB = 1024 * 1024;
+	int KiB = 1024;
 
 	if (bracket)
 		p = " (";
@@ -115,12 +118,12 @@  void util_print_bytes(long long bytes, int bracket)
 
 	printf("%lld bytes", bytes);
 
-	if (bytes > 1024 * 1024 * 1024)
-		printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024));
-	else if (bytes > 1024 * 1024)
-		printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024));
-	else if (bytes > 1024 && bytes != 0)
-		printf("%s%.1f KiB", p, (double)bytes / 1024);
+	if (bytes > GiB)
+		printf("%s%d.%d GiB", p, bytes / GiB, bytes % GiB / (GiB / 10));
+	else if (bytes > MiB)
+		printf("%s%d.%d MiB", p, bytes / MiB, bytes % MiB / (MiB / 10));
+	else if (bytes > KiB && bytes != 0)
+		printf("%s%d.%d KiB", p, bytes / KiB, bytes % KiB / (KiB / 10));
 	else
 		return;