File: wang.c

package info (click to toggle)
haskell-hashable 1.2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 196 kB
  • ctags: 39
  • sloc: haskell: 975; ansic: 456; makefile: 3
file content (29 lines) | stat: -rw-r--r-- 718 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * These hash functions were developed by Thomas Wang.
 *
 * http://www.concentric.net/~ttwang/tech/inthash.htm
 */

#include <stdint.h>

uint32_t hashable_wang_32(uint32_t a)
{
    a = (a ^ 61) ^ (a >> 16);
    a = a + (a << 3);
    a = a ^ (a >> 4);
    a = a * 0x27d4eb2d;
    a = a ^ (a >> 15);
    return a;
}

uint64_t hashable_wang_64(uint64_t key)
{
    key = (~key) + (key << 21); // key = (key << 21) - key - 1;
    key = key ^ ((key >> 24) | (key << 40));
    key = (key + (key << 3)) + (key << 8); // key * 265
    key = key ^ ((key >> 14) | (key << 50));
    key = (key + (key << 2)) + (key << 4); // key * 21
    key = key ^ ((key >> 28) | (key << 36));
    key = key + (key << 31);
    return key;
}