From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] lib: string_get_size: Use 32 bit arithmetic when possible
Date: Fri, 12 Dec 2014 15:43:47 +0100 [thread overview]
Message-ID: <1418395428-25980-2-git-send-email-linux@rasmusvillemoes.dk> (raw)
In-Reply-To: <1418395428-25980-1-git-send-email-linux@rasmusvillemoes.dk>
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
next prev parent reply other threads:[~2014-12-12 14:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-12-12 14:43 [PATCH 1/3] lib: string_get_size: Remove redundant prefixes Rasmus Villemoes
2014-12-12 14:43 ` Rasmus Villemoes [this message]
2014-12-12 14:43 ` [PATCH 3/3] lib: string_get_size: Return void Rasmus Villemoes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1418395428-25980-2-git-send-email-linux@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=James.Bottomley@HansenPartnership.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®