From: Matthew Wilcox Date: Fri, 20 Nov 2009 21:30:50 +0000 (-0700) Subject: Improve large BAR display X-Git-Tag: v3.1.5~6 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=f2f8adaa9955cf4e5188075d8eb452a7f6e268c6;p=pciutils.git 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 --- 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