* [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64
@ 2012-06-15 22:18 Andrew Hunter
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Hunter @ 2012-06-15 22:18 UTC (permalink / 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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64
@ 2012-07-02 20:25 Andrew Hunter
2012-07-10 13:35 ` Michael Tokarev
0 siblings, 1 reply; 4+ messages in thread
From: Andrew Hunter @ 2012-07-02 20:25 UTC (permalink / raw)
To: linux-kernel
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64
2012-07-02 20:25 [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64 Andrew Hunter
@ 2012-07-10 13:35 ` Michael Tokarev
2012-07-12 20:51 ` Andrew Hunter
0 siblings, 1 reply; 4+ messages in thread
From: Michael Tokarev @ 2012-07-10 13:35 UTC (permalink / raw)
To: Andrew Hunter; +Cc: linux-kernel
On 03.07.2012 00:25, Andrew Hunter wrote:
> 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. */
Hmm. Does this comment make sense here now?
Thanks,
/mjt
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64
2012-07-10 13:35 ` Michael Tokarev
@ 2012-07-12 20:51 ` Andrew Hunter
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Hunter @ 2012-07-12 20:51 UTC (permalink / raw)
To: Michael Tokarev; +Cc: linux-kernel
On Tue, Jul 10, 2012 at 6:35 AM, Michael Tokarev <mjt@tls.msk.ru> wrote:
> On 03.07.2012 00:25, Andrew Hunter wrote:
>> 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. */
>
> Hmm. Does this comment make sense here now?
>
I haven't checked what output gcc provides for 32-bit kernels with
this or a literal multiply. It's not even clear what optimization is
_asked_ for here (possibly the reduction of strength that we probably
don't even want.)
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-07-12 20:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-02 20:25 [PATCH 1/1] core-kernel: use multiply instead of shifts in hash_64 Andrew Hunter
2012-07-10 13:35 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox