From 177b38dfe9423dd714a99e9fc34abaa7f032df66 Mon Sep 17 00:00:00 2001 From: florian Date: Fri, 1 Oct 2021 23:05:48 +0200 Subject: [PATCH] * modified (cosmetics) patch by Rika: replace DJB2 with MurmurHash3, resolves #39377 --- compiler/cclasses.pas | 78 ++++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/compiler/cclasses.pas b/compiler/cclasses.pas index 82228e2281..b09baa092c 100644 --- a/compiler/cclasses.pas +++ b/compiler/cclasses.pas @@ -1241,38 +1241,64 @@ end; TFPHashList *****************************************************************************} - - function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord; - Var - pmax : pchar; - begin +// MurmurHash3_32 +function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord; +const + C1 = uint32($cc9e2d51); + C2 = uint32($1b873593); +var + h, tail: uint32; + e4: pChar; + len4, nTail: SizeUint; +begin {$push} {$q-,r-} - result:=Tag; - pmax:=p+len; - while (p 0 then + begin + { tail is 1 to 3 bytes } + case nTail of + 3: tail := unaligned(pUint16(p)^) or uint32(p[2]) shl 16; { unaligned(pUint16(p^)) can be LEtoNed for portability } + 2: tail := unaligned(pUint16(p)^); { unaligned(pUint16(p^)) can be LEtoNed for portability } + {1:} else tail := uint32(p^); + end; + h := h xor (RolDWord(tail * C1, 15) * C2); + end; + + h := h xor uint32(len); + h := (h xor (h shr 16)) * $85ebca6b; + h := (h xor (h shr 13)) * $c2b2ae35; + result := h xor (h shr 16); {$pop} - end; +end; - function FPHash(P: PChar; Len: Integer): LongWord; inline; - begin - result:=fphash(P,Len, 5381); - end; +function FPHash(P: PChar; Len: Integer): LongWord; inline; +begin + result:=fphash(P,Len, 0); +end; - function FPHash(const s: shortstring): LongWord; inline; - begin - result:=fphash(pchar(@s[1]),length(s)); - end; - function FPHash(const a: ansistring): LongWord; inline; - begin - result:=fphash(pchar(a),length(a)); - end; +function FPHash(const s: shortstring): LongWord; inline; +begin + result:=fphash(pchar(@s[1]),length(s)); +end; + + +function FPHash(const a: ansistring): LongWord; inline; +begin + result:=fphash(pchar(a),length(a)); +end; procedure TFPHashList.RaiseIndexError(Index : Integer);