1
0
mirror of https://github.com/golang/go synced 2024-11-15 12:20:32 -07:00

[release-branch.go1] runtime: improved continuity in hash computation

««« backport c8d163b7930e
runtime: improved continuity in hash computation

Fixes #3695.

R=r, dave, rsc
CC=golang-dev
https://golang.org/cl/6304062

»»»
This commit is contained in:
Jan Ziak 2012-06-13 16:25:00 -04:00 committed by Russ Cox
parent 4a0549ffc6
commit 96ab6e4a84

View File

@ -5,6 +5,9 @@
#include "runtime.h" #include "runtime.h"
#include "type.h" #include "type.h"
#define M0 (sizeof(uintptr)==4 ? 2860486313UL : 33054211828000289ULL)
#define M1 (sizeof(uintptr)==4 ? 3267000013UL : 23344194077549503ULL)
/* /*
* map and chan helpers for * map and chan helpers for
* dealing with unknown types * dealing with unknown types
@ -16,19 +19,13 @@ runtime·memhash(uintptr *h, uintptr s, void *a)
uintptr hash; uintptr hash;
b = a; b = a;
if(sizeof(hash) == 4) hash = M0;
hash = 2860486313U;
else
hash = 33054211828000289ULL;
while(s > 0) { while(s > 0) {
if(sizeof(hash) == 4) hash = (hash ^ *b) * M1;
hash = (hash ^ *b) * 3267000013UL;
else
hash = (hash ^ *b) * 23344194077549503ULL;
b++; b++;
s--; s--;
} }
*h ^= hash; *h = (*h ^ hash) * M1;
} }
void void
@ -252,7 +249,7 @@ runtime·f32hash(uintptr *h, uintptr s, void *a)
hash = runtime·fastrand1(); // any kind of NaN hash = runtime·fastrand1(); // any kind of NaN
else else
hash = *(uint32*)a; hash = *(uint32*)a;
*h ^= (*h ^ hash ^ 2860486313U) * 3267000013U; *h = (*h ^ hash ^ M0) * M1;
} }
void void
@ -271,14 +268,11 @@ runtime·f64hash(uintptr *h, uintptr s, void *a)
else { else {
u = *(uint64*)a; u = *(uint64*)a;
if(sizeof(uintptr) == 4) if(sizeof(uintptr) == 4)
hash = ((uint32)(u>>32) * 3267000013UL) ^ (uint32)u; hash = ((uint32)(u>>32) * M1) ^ (uint32)u;
else else
hash = u; hash = u;
} }
if(sizeof(uintptr) == 4) *h = (*h ^ hash ^ M0) * M1;
*h = (*h ^ hash ^ 2860486313U) * 3267000013U;
else
*h = (*h ^ hash ^ 33054211828000289ULL) * 23344194077549503ULL;
} }
void void
@ -357,7 +351,7 @@ void
runtime·interhash(uintptr *h, uintptr s, void *a) runtime·interhash(uintptr *h, uintptr s, void *a)
{ {
USED(s); USED(s);
*h ^= runtime·ifacehash(*(Iface*)a); *h = (*h ^ runtime·ifacehash(*(Iface*)a)) * M1;
} }
void void
@ -391,7 +385,7 @@ void
runtime·nilinterhash(uintptr *h, uintptr s, void *a) runtime·nilinterhash(uintptr *h, uintptr s, void *a)
{ {
USED(s); USED(s);
*h ^= runtime·efacehash(*(Eface*)a); *h = (*h ^ runtime·efacehash(*(Eface*)a)) * M1;
} }
void void