Larson hash

Code: Select all

```
Procedure.l LarsonHash(*String); Paul Larson hash
!xor eax, eax
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
!mov rdx, [p.p_String]
!jmp larsonhash_l1
!larsonhash_l0:
!imul eax, 101
CompilerIf #PB_Compiler_Unicode
!add rdx, 2
!add eax, ecx
!larsonhash_l1:
!movzx ecx, word [rdx]
CompilerElse
!add rdx, 1
!add eax, ecx
!larsonhash_l1:
!movzx ecx, byte [rdx]
CompilerEndIf
CompilerElse
!mov edx, [p.p_String]
!jmp larsonhash_l1
!larsonhash_l0:
!imul eax, 101
CompilerIf #PB_Compiler_Unicode
!add edx, 2
!add eax, ecx
!larsonhash_l1:
!movzx ecx, word [edx]
CompilerElse
!add edx, 1
!add eax, ecx
!larsonhash_l1:
!movzx ecx, byte [edx]
CompilerEndIf
CompilerEndIf
!test ecx, ecx
!jnz larsonhash_l0
ProcedureReturn
EndProcedure
s.s = "Paul Larson hash test"
t1 = ElapsedMilliseconds()
For i = 1 To 10000000
hash.l = LarsonHash(@s)
Next
t2 = ElapsedMilliseconds()
MessageRequester(Str(hash),Str(t2-t1))
```

FastHash64 (x64 only)

Code: Select all

```
; FastHash64 algorithm by Zilong Tan
Procedure.q FastHash64(*Buffer, Len, Seed.q=0)
!mov r10, 0x2127599bf4325c37
!mov r11, 0x880355f21e6d1965
!mov rdx, [p.p_Buffer]
!mov rcx, [p.v_Len]
!mov rax, rcx ; h = seed ^ (len * m);
!imul rax, r11
!xor rax, [p.v_Seed]
!sub rcx, 8
!jc .l1
; 8 byte loop
!.l0:
!mov r8, [rdx] ; v = *pos++;
!add rdx, 8
; -- mix(v) start --
!mov r9, r8
!shr r9, 23
!xor r8, r9
!imul r8, r10
!mov r9, r8
!shr r9, 47
!xor r8, r9
; -- mix end --
!xor rax, r8 ; h ^= mix(v);
!imul rax, r11 ; h *= m;
!sub rcx, 8
!jnc .l0
; remaining bytes
!.l1:
!add rcx, 8
!jz .l5
!xor r8, r8
!test rcx, 4
!jz .l2
; get 4 bytes
!mov r8d, [rdx]
!add rdx, 4
!ror r8, 32
!.l2:
!test rcx, 2
!jz .l3
; get 2 bytes
!movzx r9d, word [rdx]
!add rdx, 2
!xor r8, r9
!ror r8, 16
!.l3:
!test rcx, 1
!jz .l4
; get 1 byte
!movzx r9d, byte [rdx]
!xor r8, r9
!ror r8, 8
!.l4:
!and rcx, 7
!shl rcx, 3
!rol r8, cl
; -- mix(v) start --
!mov r9, r8
!shr r9, 23
!xor r8, r9
!imul r8, r10
!mov r9, r8
!shr r9, 47
!xor r8, r9
; -- mix end --
!xor rax, r8 ; h ^= mix(v);
!imul rax, r11 ; h *= m;
; -- mix(h) start --
!.l5:
!mov r9, rax
!shr r9, 23
!xor rax, r9
!imul rax, r10
!mov r9, rax
!shr r9, 47
!xor rax, r9
; -- mix end --
ProcedureReturn ; return mix(h);
EndProcedure
```