blob: 32e550f4d333132c240cd71b80a710cd5ea1bd9d (
plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
#include <u.h>
#include <libc.h>
enum {
MULT = 31,
NHASH = 37,
};
uint
hash(char *s)
{
uint h;
h = 0;
while(*s != 0)
h = MULT * h + (uchar)*s++;
return h % NHASH;
}
uint
djb2(char *s)
{
uint h;
h = 5381;
while(*s != 0)
h = ((h << 5) + h) + (uchar)*s++;
return h % NHASH;
}
uint
djb2a(char *s)
{
uint h;
h = 5381;
while(*s != 0)
h = ((h << 5) + h) ^ (uchar)*s++;
return h % NHASH;
}
uint
fnv1(char *s)
{
uint h;
h = 0x811c9dc5;
while(*s != 0)
h = (h*0x1000193) ^ (uchar)*s++;
return h % NHASH;
}
uint
fnv1a(char *s)
{
uint h;
h = 0x811c9dc5;
while(*s != 0)
h = (h^(uchar)*s++) * 0x1000193;
return h % NHASH;
}
void
usage(void)
{
fprint(2, "usage: %s word\n", argv0);
exits("usage");
}
void
main(int argc, char *argv[])
{
ARGBEGIN{
default: usage();
}ARGEND;
if(argc != 1)
usage();
print("tpop:\t%ud\n", hash(*argv));
print("djb2:\t%ud\n", djb2(*argv));
print("djb2a:\t%ud\n", djb2a(*argv));
print("fnv1:\t%ud\n", fnv1(*argv));
print("fnv1a:\t%ud\n", fnv1a(*argv));
exits(0);
}
|