Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | From 41e7c76b0853bf5241b38b8167dfd57c27fef1eb Mon Sep 17 00:00:00 2001 |
| 2 | From: Andrea Adami <andrea.adami@gmail.com> |
| 3 | Date: Sun, 28 Jan 2018 21:47:59 +0100 |
| 4 | Subject: [PATCH 7/9] mtd-utils: common.c: convert to integer arithmetic |
| 5 | |
| 6 | We use floating point just to print out KiB, MiB, GiB. |
| 7 | Avoid that to be klibc friendly. |
| 8 | |
| 9 | Fixes compilation for aarch64 against klibc: |
| 10 | |
| 11 | error: '-mgeneral-regs-only' is incompatible with floating-point argument |
| 12 | | printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024)); |
| 13 | etc. |
| 14 | |
| 15 | Note: |
| 16 | * In the KiB case, we could apparently multiply by 100 before dividing |
| 17 | without risking overflow. This code simply avoids multiplications. |
| 18 | |
| 19 | Upstream-Status: Submitted |
| 20 | |
| 21 | Signed-off-by: Andrea Adami <andrea.adami@gmail.com> |
| 22 | --- |
| 23 | ubi-utils/ubiutils-common.c | 18 ++++++++++++------ |
| 24 | 1 file changed, 12 insertions(+), 6 deletions(-) |
| 25 | |
| 26 | diff --git a/ubi-utils/ubiutils-common.c b/ubi-utils/ubiutils-common.c |
| 27 | index 6609a6b..0ded2a4 100644 |
| 28 | --- a/ubi-utils/ubiutils-common.c |
| 29 | +++ b/ubi-utils/ubiutils-common.c |
| 30 | @@ -107,6 +107,9 @@ long long ubiutils_get_bytes(const char *str) |
| 31 | void ubiutils_print_bytes(long long bytes, int bracket) |
| 32 | { |
| 33 | const char *p; |
| 34 | + int GiB = 1024 * 1024 * 1024; |
| 35 | + int MiB = 1024 * 1024; |
| 36 | + int KiB = 1024; |
| 37 | |
| 38 | if (bracket) |
| 39 | p = " ("; |
| 40 | @@ -115,12 +118,15 @@ void ubiutils_print_bytes(long long bytes, int bracket) |
| 41 | |
| 42 | printf("%lld bytes", bytes); |
| 43 | |
| 44 | - if (bytes > 1024 * 1024 * 1024) |
| 45 | - printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024)); |
| 46 | - else if (bytes > 1024 * 1024) |
| 47 | - printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024)); |
| 48 | - else if (bytes > 1024 && bytes != 0) |
| 49 | - printf("%s%.1f KiB", p, (double)bytes / 1024); |
| 50 | + if (bytes > GiB) |
| 51 | + printf("%s%lld.%lld GiB", p, |
| 52 | + bytes / GiB, bytes % GiB / (GiB / 10)); |
| 53 | + else if (bytes > MiB) |
| 54 | + printf("%s%lld.%lld MiB", p, |
| 55 | + bytes / MiB, bytes % MiB / (MiB / 10)); |
| 56 | + else if (bytes > KiB && bytes != 0) |
| 57 | + printf("%s%lld.%lld KiB", p, |
| 58 | + bytes / KiB, bytes % KiB / (KiB / 10)); |
| 59 | else |
| 60 | return; |
| 61 | |
| 62 | -- |
| 63 | 2.7.4 |
| 64 | |