From f2f8adaa9955cf4e5188075d8eb452a7f6e268c6 Mon Sep 17 00:00:00 2001 From: Matthew Wilcox Date: Fri, 20 Nov 2009 14:30:50 -0700 Subject: [PATCH] Improve large BAR display lspci currently displays anything larger than 2GB as a raw number. I have a device with a larger BAR and wanted to see that reported as a multiple of a GB. I took the opportunity to rewrite this routine to make it easier to add higher powers in the future. It's also slightly shorter, which is nice. I also changed it to use the binary prefixes, much as I dislike them personally. Signed-off-by: Matthew Wilcox --- lspci.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lspci.c b/lspci.c index e453f1b..ce00316 100644 --- a/lspci.c +++ b/lspci.c @@ -330,18 +330,16 @@ show_terse(struct device *d) static void show_size(pciaddr_t x) { + static const char suffix[][4] = { "", "KiB", "MiB", "GiB", "TiB" }; + unsigned i; if (!x) return; - printf(" [size="); - if (x < 1024) - printf("%d", (int) x); - else if (x < 1048576) - printf("%dK", (int)(x / 1024)); - else if (x < 0x80000000) - printf("%dM", (int)(x / 1048576)); - else - printf(PCIADDR_T_FMT, x); - putchar(']'); + for (i = 0; i < (sizeof(suffix) / sizeof(*suffix) - 1); i++) { + if (x < 1024) + break; + x /= 1024; + } + printf(" [size=%u%s]", (unsigned)x, suffix[i]); } static void -- 2.39.2