Copyright (c) 2006 Microsoft Corporation
Module Name:
hash.cpp
Abstract:
Basic hash computation support.
Author:
Leonardo de Moura (leonardo) 2006-09-11.
Revision History:
--*/
#include "util/debug.h"
#include "util/hash.h"
#include <string.h>
static unsigned read_unsigned(const char *s) {
unsigned n;
memcpy(&n, s, sizeof(unsigned));
return n;
}
unsigned string_hash(const char * str, unsigned length, unsigned init_value) {
unsigned a, b, c, len;
len = length;
a = b = 0x9e3779b9;
c = init_value;
SASSERT(sizeof(unsigned) == 4);
while (len >= 12) {
a += read_unsigned(str);
b += read_unsigned(str+4);
c += read_unsigned(str+8);
mix(a,b,c);
str += 12; len -= 12;
}
c += length;
switch(len) {
case 11:
c+=((unsigned)str[10]<<24);
Z3_fallthrough;
case 10:
c+=((unsigned)str[9]<<16);
Z3_fallthrough;
case 9 :
c+=((unsigned)str[8]<<8);
Z3_fallthrough;
case 8 :
b+=((unsigned)str[7]<<24);
Z3_fallthrough;
case 7 :
b+=((unsigned)str[6]<<16);
Z3_fallthrough;
case 6 :
b+=((unsigned)str[5]<<8);
Z3_fallthrough;
case 5 :
b+=str[4];
Z3_fallthrough;
case 4 :
a+=((unsigned)str[3]<<24);
Z3_fallthrough;
case 3 :
a+=((unsigned)str[2]<<16);
Z3_fallthrough;
case 2 :
a+=((unsigned)str[1]<<8);
Z3_fallthrough;
case 1 :
a+=str[0];
break;
}
mix(a,b,c);
return c;
}