Compute Hash32 value from ulong, long, ulongint,longint, single, double, pointer, string and zstring ptr

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
D.J.Peters
Posts: 8609
Joined: May 28, 2005 3:28
Contact:

Compute Hash32 value from ulong, long, ulongint,longint, single, double, pointer, string and zstring ptr

Post by D.J.Peters »

Original link www.concentric.net/~Ttwang/tech/inthash.htm are down but i found it on web.archive.org :-)
web.archive.org: http://www.concentric.net/~Ttwang/tech/inthash.htm

Jolshy

Code: Select all

' Hash functions
' http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm
function ComputeHash overload(byref key as const ulong) as ulong
  dim as ulong k = key,t=k shl 15
  k += not t : t = k shr 10
  k xor =  t : t = k shl  3
  k +=     t : t = k shr  6
  k xor =  t : t = k shl 11
  k += not t : t = k shr  6
  k xor =  t
  return k
end function

function ComputeHash(byref key as const long) as ulong
  return ComputeHash(cast(ulong,key))
end function

function ComputeHash(byref key as const ulongint) as ulong
  dim as ulongint k = key, t = k shl 32
  k += not t : t = k shr 22
  k xor =  t : t = k shl 13
  k += not t : t = k shr  8
  k xor =  t : t = k shl  3
  k +=     t : t = k shr 15
  k xor =  t : t = k shl 27
  k += not t : t = k shr 31
  k xor =  t
  k and= &HFFFFFFFF
  return k
end function

function ComputeHash(byref key as const longint) as ulong
  dim as ulongint k=key
  return ComputeHash(k)
end function  
  
function ComputeHash(byref f as const single) as ulong
  return ComputeHash(*cptr(ulong ptr,@f))
end function  

function ComputeHash(byref d as const double) as ulong
  return ComputeHash(*cptr(ulongint ptr,@d))
end function  

function ComputeHash(byval p as any ptr) as ulong
#ifndef __FB_64BIT__
  return ComputeHash(cast(ulong,p))
#else
  return ComputeHash(cast(ulongint,p))
#endif
end function

function ComputeHash(byref s as string) as ulong
  dim as ulong h = 5381,nChars=len(s),u32
  if nChars then
    nChars-=1
    for i as uinteger=0 to nChars
      u32 = s[i] : h = ((h shl 5) + h) xor u32
    next  
    return h
  endif
  return 0
end function

function ComputeHash(byref s as const zstring ptr) as ulong
  dim as ulong h = 5381
  if s then
    dim as ulong nChars=len(s),u32
    if nChars then
      nChars-=1
      for i as uinteger=0 to nChars
        u32 = s[i] : h = ((h shl 5) + h) xor u32
      next
      return h
    endif    
  endif  
  return 0
end function

dim as ulong    u32 = 1234
dim as long     s32 =-1234
dim as ulongint u64 = 12345678
dim as ulongint s64 =-12345678
dim as single   f32 = 123.456
dim as double   f64 = 123.45678
dim as string   s = "Hello, world!"
dim as any ptr  p = strptr(s)

print "u32 hash(u32): 0x" & hex(ComputeHash(u32),8)
print "u32 hash(s32): 0x" & hex(ComputeHash(s32),8)
print "u32 hash(u64): 0x" & hex(ComputeHash(u64),8)
print "u32 hash(s64): 0x" & hex(ComputeHash(s64),8)
print "u32 hash(f32): 0x" & hex(ComputeHash(f32),8)
print "u32 hash(f64): 0x" & hex(ComputeHash(f64),8)
print "u32 hash(ptr): 0x" & hex(ComputeHash(p),8)
print "u32 hash(str): 0x" & hex(ComputeHash(s),8)
print 

function u32_2_float(byval value as ulong) as single
  dim as ulong  u32 = &H3F800000 or (value and &H3FFFFFFF)                         
  dim as single f32 = *cptr(single ptr, @u32)
  return f32 - 1.0f
end function

print "f32 hash(u32): " & u32_2_float(ComputeHash(u32))
print "f32 hash(s32): " & u32_2_float(ComputeHash(s32))
print "f32 hash(u64): " & u32_2_float(ComputeHash(u64))
print "f32 hash(s64): " & u32_2_float(ComputeHash(s64))
print "f32 hash(f32): " & u32_2_float(ComputeHash(f32))
print "f32 hash(f64): " & u32_2_float(ComputeHash(f64))
print "f32 hash(ptr): " & u32_2_float(ComputeHash(p))
print "f32 hash(str): " & u32_2_float(ComputeHash(s))
sleep
Post Reply