ELF-32
Algorithm creator(s)
A variant of Peter J. Weinberger's HashPJW.
PB author(s)
Wayne Diamond, Dave Navarro
Description
Outputs a 32-bit unsigned hash that is the modulo generated by dividing the returned integer by the size of the table.
Note
Used in UNIX object files that use the ELF format.
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/programming/19721-elfhash-c-translation?t=19192
See also
n/a
Source Code
Download source code file elf32.bas (Right-click -> "Save as ...")
FUNCTION Elf32(sKey AS STRING) AS LONG
'// by Dave Navarro, November 30 2000
'// Bugfix by Wayne Diamond, 24 July 2002
LOCAL key AS BYTE PTR
LOCAL h AS LONG
LOCAL g AS LONG
LOCAL i AS LONG
key = STRPTR(sKey)
h = 0
WHILE @key
SHIFT LEFT h, 4
h = h + @key
key = key + 1
g = h AND &HF0000000???
IF g THEN
i = g
SHIFT RIGHT i, 24
h = h XOR i
END IF
h = h AND (NOT g)
WEND
FUNCTION = h
END FUNCTION
FUNCTION Elf32(sKey AS STRING) AS DWORD
'// by Wayne Diamond, 24 July 2002
#REGISTER NONE
LOCAL lLen AS DWORD, ptrStr AS DWORD, H AS DWORD
lLen = LEN(sKey)
ptrStr = STRPTR(sKey)
! xor ebx, ebx ; ebx = result holder (H)
! mov edx, lLen ; edx = Length
! mov ecx, ptrStr ; ecx = Ptr to string
! xor esi, esi ; esi = temp holder
Elf1:
! xor eax, eax
! shl ebx, 4
! mov al, [ecx]
! add ebx, eax
! inc ecx
! mov eax, ebx
! and eax, &HF0000000???
! cmp eax, 0
! je Elf2
! mov esi, eax
! shr esi, 24
! xor ebx, esi
Elf2:
! not eax
! and ebx,eax
! dec edx
! cmp edx, 0
! jne Elf1
! mov H,ebx
FUNCTION = H
END FUNCTION