Graphical mode synchronization
[lttv.git] / lttv / lttv / lookup3.h
CommitLineData
800dfee0
BP
1/*
2-------------------------------------------------------------------------------
3lookup3.c, by Bob Jenkins, May 2006, Public Domain.
4
5These are functions for producing 32-bit hashes for hash table lookup.
6hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
7are externally useful functions. Routines to test the hash are included
8if SELF_TEST is defined. You can use this free for any purpose. It's in
9the public domain. It has no warranty.
10
11You probably want to use hashlittle(). hashlittle() and hashbig()
12hash byte arrays. hashlittle() is is faster than hashbig() on
13little-endian machines. Intel and AMD are little-endian machines.
14On second thought, you probably want hashlittle2(), which is identical to
15hashlittle() except it returns two 32-bit hashes for the price of one.
16You could implement hashbig2() if you wanted but I haven't bothered here.
17
18If you want to find a hash of, say, exactly 7 integers, do
19 a = i1; b = i2; c = i3;
20 mix(a,b,c);
21 a += i4; b += i5; c += i6;
22 mix(a,b,c);
23 a += i7;
24 final(a,b,c);
25then use c as the hash value. If you have a variable length array of
264-byte integers to hash, use hashword(). If you have a byte array (like
27a character string), use hashlittle(). If you have several byte arrays, or
28a mix of things, see the comments above hashlittle().
29
30Why is this so big? I read 12 bytes at a time into 3 4-byte integers,
31then mix those integers. This is fast (you can do a lot more thorough
32mixing with 12*3 instructions on 3 integers than you can with 3 instructions
33on 1 byte), but shoehorning those bytes into integers efficiently is messy.
34-------------------------------------------------------------------------------
35*/
36
37/*
38 * Only minimal parts kept, see http://burtleburtle.net/bob/hash/doobs.html for
39 * full file and great info.
40 */
41
42#ifndef LOOKUP_H
43#define LOOKUP_H
44
45#include <stdint.h> /* defines uint32_t etc */
46
47
48#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
49
50
51/*
52-------------------------------------------------------------------------------
53mix -- mix 3 32-bit values reversibly.
54
55This is reversible, so any information in (a,b,c) before mix() is
56still in (a,b,c) after mix().
57
58If four pairs of (a,b,c) inputs are run through mix(), or through
59mix() in reverse, there are at least 32 bits of the output that
60are sometimes the same for one pair and different for another pair.
61This was tested for:
62* pairs that differed by one bit, by two bits, in any combination
63 of top bits of (a,b,c), or in any combination of bottom bits of
64 (a,b,c).
65* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
66 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
67 is commonly produced by subtraction) look like a single 1-bit
68 difference.
69* the base values were pseudorandom, all zero but one bit set, or
70 all zero plus a counter that starts at zero.
71
72Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
73satisfy this are
74 4 6 8 16 19 4
75 9 15 3 18 27 15
76 14 9 3 7 17 3
77Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
78for "differ" defined as + with a one-bit base and a two-bit delta. I
79used http://burtleburtle.net/bob/hash/avalanche.html to choose
80the operations, constants, and arrangements of the variables.
81
82This does not achieve avalanche. There are input bits of (a,b,c)
83that fail to affect some output bits of (a,b,c), especially of a. The
84most thoroughly mixed value is c, but it doesn't really even achieve
85avalanche in c.
86
87This allows some parallelism. Read-after-writes are good at doubling
88the number of bits affected, so the goal of mixing pulls in the opposite
89direction as the goal of parallelism. I did what I could. Rotates
90seem to cost as much as shifts on every machine I could lay my hands
91on, and rotates are much kinder to the top and bottom bits, so I used
92rotates.
93-------------------------------------------------------------------------------
94*/
95#define mix(a,b,c) \
96{ \
97 a -= c; a ^= rot(c, 4); c += b; \
98 b -= a; b ^= rot(a, 6); a += c; \
99 c -= b; c ^= rot(b, 8); b += a; \
100 a -= c; a ^= rot(c,16); c += b; \
101 b -= a; b ^= rot(a,19); a += c; \
102 c -= b; c ^= rot(b, 4); b += a; \
103}
104
105
106/*
107-------------------------------------------------------------------------------
108final -- final mixing of 3 32-bit values (a,b,c) into c
109
110Pairs of (a,b,c) values differing in only a few bits will usually
111produce values of c that look totally different. This was tested for
112* pairs that differed by one bit, by two bits, in any combination
113 of top bits of (a,b,c), or in any combination of bottom bits of
114 (a,b,c).
115* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
116 the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
117 is commonly produced by subtraction) look like a single 1-bit
118 difference.
119* the base values were pseudorandom, all zero but one bit set, or
120 all zero plus a counter that starts at zero.
121
122These constants passed:
123 14 11 25 16 4 14 24
124 12 14 25 16 4 14 24
125and these came close:
126 4 8 15 26 3 22 24
127 10 8 15 26 3 22 24
128 11 8 15 26 3 22 24
129-------------------------------------------------------------------------------
130*/
131#define final(a,b,c) \
132{ \
133 c ^= b; c -= rot(b,14); \
134 a ^= c; a -= rot(c,11); \
135 b ^= a; b -= rot(a,25); \
136 c ^= b; c -= rot(b,16); \
137 a ^= c; a -= rot(c,4); \
138 b ^= a; b -= rot(a,14); \
139 c ^= b; c -= rot(b,24); \
140}
141
142
143#endif
This page took 0.036795 seconds and 4 git commands to generate.