Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created May 23, 2012 15:24
Show Gist options
  • Save TinkerWorX/2775873 to your computer and use it in GitHub Desktop.
Save TinkerWorX/2775873 to your computer and use it in GitHub Desktop.
...
// c++
#define FNV_32_PRIME ((unsigned int)0x01000193)
#define FNV_32_INIT ((unsigned int )2166136261)
#define FNV_MASK_8 (((unsigned int)1<<8)-1)
uint fnv_32_a_buf(void* buf, uint len)
{
uint hval = (uint)FNV_32_INIT;
byte* bp = (byte*)buf;
byte* be = bp + len;
while (bp < be)
{
hval ^= (uint)*bp++;
hval *= FNV_32_PRIME;
}
return hval;
}
// C#
private const uint FNV_32_PRIME = 0x01000193;
private const ulong FNV_32_INIT = 0x2166136261;
private const uint FNV_MASK_8 = (1 << 8) - 1;
internal static uint fnv_32_a_buf(byte[] buffer, uint len)
{
var hval = FNV_32_INIT;
for (var i = 0; i < len;)
{
hval ^= buffer[i++];
hval *= FNV_32_PRIME;
}
return (uint)hval;
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment