improve hash mixing

original commit: d7469cedd67a950931a561ce14388fe7e628770d
This commit is contained in:
Matthew Flatt 2019-04-07 09:34:30 +02:00
parent 77e84faa24
commit ffc02a9877
2 changed files with 9 additions and 9 deletions

View File

@ -188,19 +188,18 @@ void S_resize_oblist(void) {
S_G.oblist = new_oblist;
}
/* hash function: multiplier weights each character, h = n factors in the length */
#define multiplier 3
#define MIX_HASH(hc) (hc += (hc << 10), hc ^= (hc >> 6))
static iptr hash(const unsigned char *s, iptr n) {
iptr h = n + 401887359;
while (n--) h = h * multiplier + *s++;
return h & most_positive_fixnum;
uptr h = (uptr)n + 401887359;
while (n--) { h += *s++; MIX_HASH(h); }
return (iptr)h & most_positive_fixnum;
}
static iptr hash_sc(const string_char *s, iptr n) {
iptr h = n + 401887359;
while (n--) h = h * multiplier + Schar_value(*s++);
return h & most_positive_fixnum;
uptr h = (uptr)n + 401887359;
while (n--) { h += Schar_value(*s++); MIX_HASH(h); }
return (iptr)h & most_positive_fixnum;
}
static iptr hash_uname(const string_char *s, iptr n) {

View File

@ -920,7 +920,8 @@ Documentation notes:
(define (hcabs hc) (if (fx< hc 0) (fxnot hc) hc))
(define (update hc k)
(fxlogxor (#3%fx+ (#3%fxsll hc 2) hc) k))
(let ([hc2 (#3%fx+ hc (#3%fxsll (#3%fx+ hc k) 10))])
(fxlogxor hc2 (fxsrl hc2 6))))
(define bytevector-hash
(lambda (bv)