Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1309720

[PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]]

From Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Newsgroups linux.kernel
Subject [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]]
Date 2016-01-14 23:30 +0100
Message-ID <qQYz2-5b5-53@gated-at.bofh.it> (permalink)
References <qQYz0-5b5-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


The precision field defines how many digits after period ('.') will be printed.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 Documentation/printk-formats.txt |  7 +++++++
 lib/test_printf.c                | 29 +++++++++++++++++++++++++++++
 lib/vsprintf.c                   | 20 +++++++++++++++++++-
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/Documentation/printk-formats.txt b/Documentation/printk-formats.txt
index 3b41899..534407c 100644
--- a/Documentation/printk-formats.txt
+++ b/Documentation/printk-formats.txt
@@ -295,11 +295,18 @@ unsigned long long value in human-readable form (with IEC binary prefix):
 	%pl		1048575 B	(1048575)
 	%plKM		1023 KiB	(1048575)
 	%pl		1 MiB		(1048576)
+	%.1plKM		1046527.9 MiB	(1097364144125)
 	%plG		2 TiB		(2199023255552)
 
 	For printing given unsigned long long value in human-readable form with
 	automatically choosen IEC binary prefix.
 
+	Precision, if passed, defines how many digits will be printed after
+	period ('.'). In this case the biggest possible unit is used in the
+	given range [From[To]]. The printed value isn't rounded anyhow.
+
+	For other use cases consider to call string_get_size().
+
 	Passed by reference.
 
 bitmap and its derivatives such as cpumask and nodemask:
diff --git a/lib/test_printf.c b/lib/test_printf.c
index ae35885..4a2e30b 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -435,6 +435,30 @@ human_readable(void)
 		{ .v = 1048575ULL,		.r = "1023 KiB" },
 		{ .v = 1047552ULL,		.r = "1023 KiB" },
 	};
+	struct hr_test_data htdpl0KM[] = {
+		{ .v = 1097364144128ULL,	.r = "1046528 MiB" },
+		{ .v = 1097364144125ULL,	.r = "1046527 MiB" },
+		{ .v = 1048578ULL,		.r = "1 MiB" },
+		{ .v = 1048576ULL,		.r = "1 MiB" },
+		{ .v = 1048575ULL,		.r = "1023 KiB" },
+		{ .v = 1047552ULL,		.r = "1023 KiB" },
+	};
+	struct hr_test_data htdpl1KM[] = {
+		{ .v = 1097364144128ULL,	.r = "1046528.0 MiB" },
+		{ .v = 1097364144125ULL,	.r = "1046527.9 MiB" },
+		{ .v = 1048578ULL,		.r = "1.0 MiB" },
+		{ .v = 1048576ULL,		.r = "1.0 MiB" },
+		{ .v = 1048575ULL,		.r = "1023.9 KiB" },
+		{ .v = 1047552ULL,		.r = "1023.0 KiB" },
+	};
+	struct hr_test_data htdpl3KM[] = {
+		{ .v = 1097364144128ULL,	.r = "1046528.000 MiB" },
+		{ .v = 1097364144125ULL,	.r = "1046527.999 MiB" },
+		{ .v = 1048578ULL,		.r = "1.000 MiB" },
+		{ .v = 1048576ULL,		.r = "1.000 MiB" },
+		{ .v = 1048575ULL,		.r = "1023.999 KiB" },
+		{ .v = 1047552ULL,		.r = "1023.000 KiB" },
+	};
 	struct hr_test_array {
 		const char *fmt;
 		struct hr_test_data *data;
@@ -446,6 +470,11 @@ human_readable(void)
 		{ "%plKM",	htdplKM,	ARRAY_SIZE(htdplKM) },
 		/* No reversed %pl[To[From]] order */
 		{ "%plMK",	htdplM,		ARRAY_SIZE(htdplM) },
+		{ "%.0plKM",	htdpl0KM,	ARRAY_SIZE(htdpl0KM) },
+		{ "%.1plKM",	htdpl1KM,	ARRAY_SIZE(htdpl1KM) },
+		{ "%.3plKM",	htdpl3KM,	ARRAY_SIZE(htdpl3KM) },
+		/* For now precision is in range [0..3] */
+		{ "%.4plKM",	htdplKM,	ARRAY_SIZE(htdplKM) },
 	};
 	unsigned int i, j;
 
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index fbcb221..2dd86c9 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1327,7 +1327,9 @@ char *human_readable(char *buf, char *end, const void *addr,
 			to = i;
 	}
 
-	if (value)
+	if (spec.precision >= 0 && spec.precision <= 3 && value)
+		index = (fls64(value) - 1) / 10;
+	else if (value)
 		index = __ffs64(value) / 10;
 	else
 		index = 0;
@@ -1336,6 +1338,22 @@ char *human_readable(char *buf, char *end, const void *addr,
 	/* Print numeric part */
 	buf = number(buf, end, value >> (index * 10), default_dec_spec);
 
+	/* Print precision part if asked */
+	if (spec.precision > 0 && spec.precision <= 3 && index) {
+		unsigned int remainder;
+
+		remainder = (value >> (index - 1) * 10) % 1024;
+		for (i = 0; i < spec.precision; i++)
+			remainder *= 10;
+		remainder >>= 10;
+
+		/* Period... */
+		buf = put_one_char(buf, end, '.');
+
+		/* ...and remainder part */
+		buf = number(buf, end, remainder, spec);
+	}
+
 	/* Space between numeric part and units */
 	buf = put_one_char(buf, end, ' ');
 
-- 
2.6.4

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 08/11] lib/vsprintf: allow range of prefix for %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:50 +0100
  [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 02/11] lib/vsprintf: make default_dec_spec global Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  [PATCH v2 11/11] pcmciamtd: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 04/11] lib/string_helpers: export string_units_{2,10} for others Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 03/11] lib/vsprintf: make default_str_spec global Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 07/11] x86/efi: print size and base in binary units in efi_print_memmap Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 10/11] cxgb4: print value in human-readable form via %.0plKM Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 05/11] lib/string_helpers: fix indentation in few places Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
  [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form Joe Perches <joe@perches.com> - 2016-01-15 04:10 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in human-readable form Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 22:00 +0100
    Re: [PATCH v2 06/11] lib/vsprintf: introduce %pl to print in  human-readable form "H. Peter Anvin" <hpa@zytor.com> - 2016-01-21 22:20 +0100
  [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 09/11] lib/vsprintf: use precision field with %pl[From[To]] Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 23:30 +0100
  [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-14 23:30 +0100
    Re: [PATCH v2 01/11] lib/vsprintf: introduce put_one_char() for 3 line idiom Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-18 21:40 +0100
  Re: [PATCH v2 00/11] lib/vsprintf: refactor and introduce %pl  specifier Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-21 14:00 +0100

csiph-web