Merge branch 'master' into urcu/ht-shrink
[urcu.git] / urcu / jhash.h
CommitLineData
674f7a69
MD
1#ifndef _KCOMPAT_JHASH_H
2#define _KCOMPAT_JHASH_H
3
4/* jhash.h: Jenkins hash support.
5 *
6 * Copyright (C) 1996 Bob Jenkins (bob_jenkins@burtleburtle.net)
7 *
8 * http://burtleburtle.net/bob/hash/
9 *
10 * These are the credits from Bob's sources:
11 *
12 * lookup2.c, by Bob Jenkins, December 1996, Public Domain.
13 * hash(), hash2(), hash3, and mix() are externally useful functions.
14 * Routines to test the hash are included if SELF_TEST is defined.
15 * You can use this free for any purpose. It has no warranty.
16 *
17 * Copyright (C) 2003 David S. Miller (davem@redhat.com)
18 *
19 * I've modified Bob's hash to be useful in the Linux kernel, and
20 * any bugs present are surely my fault. -DaveM
21 */
22
23#include <stdint.h>
24
5e28c532 25typedef uint8_t u8;
674f7a69
MD
26typedef uint32_t u32;
27
28/* NOTE: Arguments are modified. */
29#define __jhash_mix(a, b, c) \
30{ \
31 a -= b; a -= c; a ^= (c>>13); \
32 b -= c; b -= a; b ^= (a<<8); \
33 c -= a; c -= b; c ^= (b>>13); \
34 a -= b; a -= c; a ^= (c>>12); \
35 b -= c; b -= a; b ^= (a<<16); \
36 c -= a; c -= b; c ^= (b>>5); \
37 a -= b; a -= c; a ^= (c>>3); \
38 b -= c; b -= a; b ^= (a<<10); \
39 c -= a; c -= b; c ^= (b>>15); \
40}
41
42/* The golden ration: an arbitrary value */
43#define JHASH_GOLDEN_RATIO 0x9e3779b9
44
45/* The most generic version, hashes an arbitrary sequence
46 * of bytes. No alignment or length assumptions are made about
47 * the input key.
48 */
49static inline u32 jhash(const void *key, u32 length, u32 initval)
50{
51 u32 a, b, c, len;
52 const u8 *k = key;
53
54 len = length;
55 a = b = JHASH_GOLDEN_RATIO;
56 c = initval;
57
58 while (len >= 12) {
59 a += (k[0] +((u32)k[1]<<8) +((u32)k[2]<<16) +((u32)k[3]<<24));
60 b += (k[4] +((u32)k[5]<<8) +((u32)k[6]<<16) +((u32)k[7]<<24));
61 c += (k[8] +((u32)k[9]<<8) +((u32)k[10]<<16)+((u32)k[11]<<24));
62
63 __jhash_mix(a,b,c);
64
65 k += 12;
66 len -= 12;
67 }
68
69 c += length;
70 switch (len) {
71 case 11: c += ((u32)k[10]<<24);
72 case 10: c += ((u32)k[9]<<16);
73 case 9 : c += ((u32)k[8]<<8);
74 case 8 : b += ((u32)k[7]<<24);
75 case 7 : b += ((u32)k[6]<<16);
76 case 6 : b += ((u32)k[5]<<8);
77 case 5 : b += k[4];
78 case 4 : a += ((u32)k[3]<<24);
79 case 3 : a += ((u32)k[2]<<16);
80 case 2 : a += ((u32)k[1]<<8);
81 case 1 : a += k[0];
82 };
83
84 __jhash_mix(a,b,c);
85
86 return c;
87}
88
89#endif /* _KCOMPAT_JHASH_H */
This page took 0.02598 seconds and 4 git commands to generate.