To: | Atle Smelvær |
From: | Duncan Parsons |
Newsgroup: | embarcadero.public.delphi.language.basm |
29-Feb-2012
Re: MurmurHash3 32bit BASM implementation [Edit]> {quote:title=Atle Smelvær wrote:}{quote} > Hi there > > Have anyone tried to convert the MurmurHash3 to a optimized BASM version? > This could really be a nice BASM challenge. > > Code snippet from MurmurHash3.cpp by Austin Appleby: > > void MurmurHash3_x86_32 ( const void * key, int len, > uint32_t seed, void * out ) > {...} > > -Atle Hi
I needed an Object Pascal port of MurmurHash3 (I had been using MurmurHash2, but see it's been superseded), so I'll offer that up here. Alas, it's not BASM, but a pure Pascal implementation is still pretty good.
function rotl32 (x: Cardinal; r: byte): Cardinal; begin Result := (x shl r) or (x shr (32 -r)); end;
procedure MurmurHash3 (key: Pointer; len: Cardinal; seed: Cardinal; out: Pointer);
const c1: Cardinal = $CC9E2D51; c2: Cardinal = $1B873593;
blocks := PCardinal( Integer(data) + (nblocks * 4) ); //Pointer to the end of the buffer (rounded to nearest 4)
//Read forward for i := -nblocks to 0 do begin
k1 := getblock(blocks, i); //read out the integer representation of the current block
k1 := k1 * c1; k1 := rotl32(k1, 15); k1 := k1 * c2;
tail := PByte( Integer(data) + (nblocks * 4) ); //Pointer to the last few bytes
k1 := 0;
As noted in comments, this isn't 64bit safe, so those with XE2 or using FPC, do beware :-)
Hope it's of some benefit to someone other than me! |