mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/3] lib: string_get_size: Remove redundant prefixes
@ 2014-12-12 14:43 Rasmus Villemoes
  2014-12-12 14:43 ` [PATCH 2/3] lib: string_get_size: Use 32 bit arithmetic when possible Rasmus Villemoes
  2014-12-12 14:43 ` [PATCH 3/3] lib: string_get_size: Return void Rasmus Villemoes
  0 siblings, 2 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2014-12-12 14:43 UTC (permalink / raw)
  To: Andrew Morton; +Cc: James Bottomley, Rasmus Villemoes, linux-kernel

While 3c9f3681d0b4 says that Z and Y are included in preparation for
128 bit computers, they just waste .text currently. If and when we get
u128, string_get_size needs updating anyway (and ISO needs to come up
with four more prefixes).

Also there's no need to include and test for the NULL sentinel; once
we reach "E" size is at most 18. [The test is also wrong; it should be
units_str[units][i+1]; if we've reached NULL we're already doomed.]

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 lib/string_helpers.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 58b78ba57439..0d25f7aa732c 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -28,11 +28,10 @@ int string_get_size(u64 size, const enum string_size_units units,
 		    char *buf, int len)
 {
 	static const char *const units_10[] = {
-		"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL
+		"B", "kB", "MB", "GB", "TB", "PB", "EB"
 	};
 	static const char *const units_2[] = {
-		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB",
-		NULL
+		"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
 	};
 	static const char *const *const units_str[] = {
 		[STRING_UNITS_10] = units_10,
@@ -49,7 +48,7 @@ int string_get_size(u64 size, const enum string_size_units units,
 	tmp[0] = '\0';
 	i = 0;
 	if (size >= divisor[units]) {
-		while (size >= divisor[units] && units_str[units][i]) {
+		while (size >= divisor[units]) {
 			remainder = do_div(size, divisor[units]);
 			i++;
 		}
-- 
2.1.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] lib: string_get_size: Use 32 bit arithmetic when possible
  2014-12-12 14:43 [PATCH 1/3] lib: string_get_size: Remove redundant prefixes Rasmus Villemoes
@ 2014-12-12 14:43 ` Rasmus Villemoes
  2014-12-12 14:43 ` [PATCH 3/3] lib: string_get_size: Return void Rasmus Villemoes
  1 sibling, 0 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2014-12-12 14:43 UTC (permalink / raw)
  To: Andrew Morton; +Cc: James Bottomley, Rasmus Villemoes, linux-kernel

The remainder from do_div is always a u32, and after size has been
reduced to be below 1000 (or 1024), it certainly fits in u32. So both
remainder and sf_cap can be made u32s, the format specifiers can be
simplified (%lld wasn't the right thing to use for _unsigned_ long
long anyway), and we can replace a do_div with an ordinary 32/32 bit
division.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 lib/string_helpers.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 0d25f7aa732c..2b3757f84b3b 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -42,7 +42,7 @@ int string_get_size(u64 size, const enum string_size_units units,
 		[STRING_UNITS_2] = 1024,
 	};
 	int i, j;
-	u64 remainder = 0, sf_cap;
+	u32 remainder = 0, sf_cap;
 	char tmp[8];
 
 	tmp[0] = '\0';
@@ -59,14 +59,13 @@ int string_get_size(u64 size, const enum string_size_units units,
 
 		if (j) {
 			remainder *= 1000;
-			do_div(remainder, divisor[units]);
-			snprintf(tmp, sizeof(tmp), ".%03lld",
-				 (unsigned long long)remainder);
+			remainder /= divisor[units];
+			snprintf(tmp, sizeof(tmp), ".%03u", remainder);
 			tmp[j+1] = '\0';
 		}
 	}
 
-	snprintf(buf, len, "%lld%s %s", (unsigned long long)size,
+	snprintf(buf, len, "%u%s %s", (u32)size,
 		 tmp, units_str[units][i]);
 
 	return 0;
-- 
2.1.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] lib: string_get_size: Return void
  2014-12-12 14:43 [PATCH 1/3] lib: string_get_size: Remove redundant prefixes Rasmus Villemoes
  2014-12-12 14:43 ` [PATCH 2/3] lib: string_get_size: Use 32 bit arithmetic when possible Rasmus Villemoes
@ 2014-12-12 14:43 ` Rasmus Villemoes
  1 sibling, 0 replies; 3+ messages in thread
From: Rasmus Villemoes @ 2014-12-12 14:43 UTC (permalink / raw)
  To: Andrew Morton; +Cc: James Bottomley, Rasmus Villemoes, linux-kernel

string_get_size was documented to return an error, but in fact always
returned 0. Since the output always fits in 9 bytes, just document
that and let callers do what they do now: Pass a small stack buffer
and ignore the return value.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 include/linux/string_helpers.h |  4 ++--
 lib/string_helpers.c           | 10 ++++------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index 6eb567ac56bc..657571817260 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -10,8 +10,8 @@ enum string_size_units {
 	STRING_UNITS_2,		/* use binary powers of 2^10 */
 };
 
-int string_get_size(u64 size, enum string_size_units units,
-		    char *buf, int len);
+void string_get_size(u64 size, enum string_size_units units,
+		     char *buf, int len);
 
 #define UNESCAPE_SPACE		0x01
 #define UNESCAPE_OCTAL		0x02
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 2b3757f84b3b..8f8c4417f228 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -20,12 +20,12 @@
  * @len:	length of buffer
  *
  * This function returns a string formatted to 3 significant figures
- * giving the size in the required units.  Returns 0 on success or
- * error on failure.  @buf is always zero terminated.
+ * giving the size in the required units.  @buf should have room for
+ * at least 9 bytes and will always be zero terminated.
  *
  */
-int string_get_size(u64 size, const enum string_size_units units,
-		    char *buf, int len)
+void string_get_size(u64 size, const enum string_size_units units,
+		     char *buf, int len)
 {
 	static const char *const units_10[] = {
 		"B", "kB", "MB", "GB", "TB", "PB", "EB"
@@ -67,8 +67,6 @@ int string_get_size(u64 size, const enum string_size_units units,
 
 	snprintf(buf, len, "%u%s %s", (u32)size,
 		 tmp, units_str[units][i]);
-
-	return 0;
 }
 EXPORT_SYMBOL(string_get_size);
 
-- 
2.1.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-12-12 14:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-12 14:43 [PATCH 1/3] lib: string_get_size: Remove redundant prefixes Rasmus Villemoes
2014-12-12 14:43 ` [PATCH 2/3] lib: string_get_size: Use 32 bit arithmetic when possible Rasmus Villemoes
2014-12-12 14:43 ` [PATCH 3/3] lib: string_get_size: Return void Rasmus Villemoes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®