linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Reduce the number of expensive division instructions done by _parse_integer()
@ 2012-02-09 15:48 David Howells
  2012-02-09 16:28 ` Eric Dumazet
  0 siblings, 1 reply; 13+ messages in thread
From: David Howells @ 2012-02-09 15:48 UTC (permalink / raw)
  To: adobriyan; +Cc: torvalds, dhowells, linux-kernel

_parse_integer() does one or two division instructions (which are slow) per
digit parsed to perform the overflow check.

Furthermore, these are particularly expensive examples of division instruction
as the number of clock cycles required to complete them may go up with the
position of the most significant set bit in the dividend:

	if (*res > div_u64(ULLONG_MAX - val, base))

which is as maximal as possible.

Worse, on 32-bit arches, more than one of these division instructions may be
required per digit.

So, assuming we don't support a base of more than 16, skip the check if the
top nibble of the result is not set at this point.

Signed-off-by: David Howells <dhowells@redhat.com>
---

 lib/kstrtox.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/kstrtox.c b/lib/kstrtox.c
index 7a94c8f..f80c896 100644
--- a/lib/kstrtox.c
+++ b/lib/kstrtox.c
@@ -64,7 +64,7 @@ unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long
 
 		if (val >= base)
 			break;
-		if (*res > div_u64(ULLONG_MAX - val, base))
+		if (unlikely(*res >> 60) && *res > div_u64(ULLONG_MAX - val, base))
 			overflow = 1;
 		*res = *res * base + val;
 		rv++;


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

end of thread, other threads:[~2012-02-10 13:50 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-02-09 15:48 [PATCH] Reduce the number of expensive division instructions done by _parse_integer() David Howells
2012-02-09 16:28 ` Eric Dumazet
2012-02-09 16:42   ` Eric Dumazet
2012-02-09 16:58   ` Linus Torvalds
2012-02-09 17:08     ` Linus Torvalds
2012-02-09 17:46     ` David Howells
2012-02-09 17:56       ` Linus Torvalds
2012-02-09 18:07       ` David Howells
2012-02-09 18:08         ` Linus Torvalds
2012-02-09 18:50         ` David Howells
2012-02-09 19:14           ` Linus Torvalds
2012-02-10 13:50             ` Alexey Dobriyan
2012-02-09 19:23           ` David Howells

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).