NAME: ELF-32
CREATOR: A variant of Peter J. Weinberger's HashPJW.
PB AUTHOR: 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.
NOTES: Used in UNIX object files that use the ELF format.
SOURCE: http://www.powerbasic.com/support/forums/Forum6/HTML/001338.html
Viewing source from elf32.bas   1311 bytes   Last modified Wed, 20 September 2006

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

Back to The Archives