public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrew Hunter <ahh@google.com>
To: linux-kernel@vger.kernel.org
Subject: [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64
Date: Mon, 2 Jul 2012 13:25:34 -0700	[thread overview]
Message-ID: <20120702202534.GC844@google.com> (raw)


hash_64(val) = val * (a 64-bit constant).  It is "optimized" by
replacing the multiply by a bunch of shifts and adds.  On modern
machines, this is not an optimization; remove it.

Running this hash function in a independent benchmark, it's about three times
as fast (1ns vs 3ns) with a multiply as with a shift on Westmere. It's also
considerably smaller (and since we inline this function often, that matters.)

Signed-off-by: Andrew Hunter <ahh@google.com>
Reviewed-by: Paul Turner <pjt@google.com>
---
 include/linux/hash.h |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/linux/hash.h b/include/linux/hash.h
index b80506b..daabc3d 100644
--- a/include/linux/hash.h
+++ b/include/linux/hash.h
@@ -34,7 +34,9 @@
 static inline u64 hash_64(u64 val, unsigned int bits)
 {
 	u64 hash = val;
-
+#if BITS_PER_LONG == 64
+	hash *= GOLDEN_RATIO_PRIME_64;
+#else
 	/*  Sigh, gcc can't optimise this alone like it does for 32 bits. */
 	u64 n = hash;
 	n <<= 18;
@@ -49,7 +51,7 @@ static inline u64 hash_64(u64 val, unsigned int bits)
 	hash += n;
 	n <<= 2;
 	hash += n;
-
+#endif
 	/* High bits are more random, so use them. */
 	return hash >> (64 - bits);
 }
-- 
1.7.7.3

             reply	other threads:[~2012-07-02 20:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-02 20:25 Andrew Hunter [this message]
2012-07-10 13:35 ` [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64 Michael Tokarev
2012-07-12 20:51   ` Andrew Hunter
  -- strict thread matches above, loose matches on Subject: below --
2012-06-15 22:18 Andrew Hunter

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=20120702202534.GC844@google.com \
    --to=ahh@google.com \
    --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