From: George Spelvin <linux@horizon.com>
To: vda.linux@googlemail.com, mina86@mina86.com
Cc: hughd@google.com, linux-kernel@vger.kernel.org, linux@horizon.com
Subject: [PATCH 2/4] lib: vsprintf: Optimize division by 10000
Date: Fri, 3 Aug 2012 01:21:09 -0400 [thread overview]
Message-ID: <1343971271-13355-2-git-send-email-linux@horizon.com> (raw)
In-Reply-To: <1343971271-13355-1-git-send-email-linux@horizon.com>
The same multiply-by-inverse technique can be used to
convert division by 10000 to a 32x32->64-bit multiply.
Signed-off-by: George Spelvin <linux@horizon.com>
---
lib/vsprintf.c | 60 +++++++++++++++++++++++++++++++-------------------------
1 file changed, 33 insertions(+), 27 deletions(-)
This is something of an RFC, I haven't benchmarked the helper
function. But it sure cleans up the code!
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 2f32fe8..a8e7392 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -245,17 +245,32 @@ char *put_dec(char *buf, unsigned long long n)
/* See comment in put_dec_full9 for choice of constants */
static noinline_for_stack
-char *put_dec_full4(char *buf, unsigned q)
+void put_dec_full4(char *buf, unsigned q)
{
unsigned r;
r = (q * 0xccd) >> 15;
- *buf++ = (q - 10 * r) + '0';
+ buf[0] = (q - 10 * r) + '0';
q = (r * 0xcd) >> 11;
- *buf++ = (r - 10 * q) + '0';
+ buf[1] = (r - 10 * q) + '0';
r = (q * 0xcd) >> 11;
- *buf++ = (q - 10 * r) + '0';
- *buf++ = r + '0';
- return buf;
+ buf[2] = (q - 10 * r) + '0';
+ buf[3] = r + '0';
+}
+
+/*
+ * Call put_dec_full4 on x % 10000, return x / 10000.
+ * The approximation x/10000 == (x * 0x346DC5D7) >> 43
+ * holds for all x < 1,128,869,999. The largest value this
+ * helper will ever be asked to convert is 1,125,520,955.
+ * (d1 in the put_dec code, assuming n is all-ones).
+ */
+static
+unsigned put_dec_helper4(char *buf, unsigned x)
+{
+ uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
+
+ put_dec_full4(buf, x - q * 10000);
+ return q;
}
/* Based on code by Douglas W. Jones found at
@@ -277,28 +292,19 @@ char *put_dec(char *buf, unsigned long long n)
d3 = (h >> 16); /* implicit "& 0xffff" */
q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
+ q = put_dec_helper4(buf, q);
+
+ q += 7671 * d3 + 9496 * d2 + 6 * d1;
+ q = put_dec_helper4(buf+4, q);
+
+ q += 4749 * d3 + 42 * d2;
+ q = put_dec_helper4(buf+8, q);
- buf = put_dec_full4(buf, q % 10000);
- q = q / 10000;
-
- d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
- buf = put_dec_full4(buf, d1 % 10000);
- q = d1 / 10000;
-
- d2 = q + 4749 * d3 + 42 * d2;
- buf = put_dec_full4(buf, d2 % 10000);
- q = d2 / 10000;
-
- d3 = q + 281 * d3;
- if (!d3)
- goto done;
- buf = put_dec_full4(buf, d3 % 10000);
- q = d3 / 10000;
- if (!q)
- goto done;
- buf = put_dec_full4(buf, q);
- done:
- while (buf[-1] == '0')
+ q += 281 * d3;
+ buf += 12;
+ if (q)
+ buf = put_dec_trunc8(buf, q);
+ else while (buf[-1] == '0')
--buf;
return buf;
--
1.7.10.4
next prev parent reply other threads:[~2012-08-03 5:21 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-03 5:21 [PATCH 1/4] lib: vsprintf: Optimize division by 10 for small integers George Spelvin
2012-08-03 5:21 ` George Spelvin [this message]
2012-09-23 17:30 ` [PATCH 2/4] lib: vsprintf: Optimize division by 10000 Michal Nazarewicz
2012-09-24 12:16 ` George Spelvin
2012-09-24 12:41 ` Michal Nazarewicz
2012-09-24 13:56 ` George Spelvin
2012-09-24 15:14 ` Geert Uytterhoeven
2012-09-24 15:48 ` George Spelvin
2012-09-24 9:03 ` Denys Vlasenko
2012-09-24 12:35 ` George Spelvin
2012-09-24 15:02 ` Denys Vlasenko
2012-08-03 5:21 ` [PATCH 3/4] lib: vsprintf: Optimize put_dec_trunc8 George Spelvin
2012-09-23 14:18 ` Rabin Vincent
2012-09-24 11:13 ` George Spelvin
2012-09-24 14:33 ` George Spelvin
2012-09-24 14:53 ` Michal Nazarewicz
2012-09-24 14:57 ` Michal Nazarewicz
2012-09-23 18:22 ` Michal Nazarewicz
2012-09-24 11:46 ` George Spelvin
2012-09-24 12:29 ` Michal Nazarewicz
2012-09-24 13:49 ` George Spelvin
2012-09-24 15:06 ` Michal Nazarewicz
2012-09-25 11:44 ` George Spelvin
2012-09-25 13:00 ` Denys Vlasenko
2012-08-03 5:21 ` [PATCH 4/4] lib: vsprintf: Fix broken comments George Spelvin
2012-09-23 17:22 ` [PATCH 1/4] lib: vsprintf: Optimize division by 10 for small integers Michal Nazarewicz
2012-09-24 14:18 ` George Spelvin
2012-09-24 9:06 ` Denys Vlasenko
2012-09-24 11:27 ` George Spelvin
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=1343971271-13355-2-git-send-email-linux@horizon.com \
--to=linux@horizon.com \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mina86@mina86.com \
--cc=vda.linux@googlemail.com \
/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