2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * LTTng hash table helpers.
9 #ifndef _UST_COMMON_HASH_H
10 #define _UST_COMMON_HASH_H
15 #include <urcu/compiler.h>
19 * Source: http://burtleburtle.net/bob/c/lookup3.c
20 * Originally Public Domain
23 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
25 #define mix(a, b, c) \
27 a -= c; a ^= rot(c, 4); c += b; \
28 b -= a; b ^= rot(a, 6); a += c; \
29 c -= b; c ^= rot(b, 8); b += a; \
30 a -= c; a ^= rot(c, 16); c += b; \
31 b -= a; b ^= rot(a, 19); a += c; \
32 c -= b; c ^= rot(b, 4); b += a; \
35 #define final(a, b, c) \
37 c ^= b; c -= rot(b, 14); \
38 a ^= c; a -= rot(c, 11); \
39 b ^= a; b -= rot(a, 25); \
40 c ^= b; c -= rot(b, 16); \
41 a ^= c; a -= rot(c, 4);\
42 b ^= a; b -= rot(a, 14); \
43 c ^= b; c -= rot(b, 24); \
47 uint32_t lttng_hash_u32(const uint32_t *k
, size_t length
, uint32_t initval
)
48 __attribute__((unused
));
50 uint32_t lttng_hash_u32(
51 const uint32_t *k
, /* the key, an array of uint32_t values */
52 size_t length
, /* the length of the key, in uint32_ts */
53 uint32_t initval
) /* the previous hash, or an arbitrary value */
57 /* Set up the internal state */
58 a
= b
= c
= 0xdeadbeef + (((uint32_t) length
) << 2) + initval
;
60 /*----------------------------------------- handle most of the key */
70 /*----------------------------------- handle the last 3 uint32_t's */
71 switch (length
) { /* all the case statements fall through */
72 case 3: c
+= k
[2]; /* fall through */
73 case 2: b
+= k
[1]; /* fall through */
76 case 0: /* case 0: nothing left to add */
79 /*---------------------------------------------- report the result */
85 const uint32_t *k
, /* the key, an array of uint32_t values */
86 size_t length
, /* the length of the key, in uint32_ts */
87 uint32_t *pc
, /* IN: seed OUT: primary hash value */
88 uint32_t *pb
) /* IN: more seed OUT: secondary hash value */
92 /* Set up the internal state */
93 a
= b
= c
= 0xdeadbeef + ((uint32_t) (length
<< 2)) + *pc
;
96 /*----------------------------------------- handle most of the key */
106 /*----------------------------------- handle the last 3 uint32_t's */
107 switch (length
) { /* all the case statements fall through */
108 case 3: c
+= k
[2]; /* fall through */
109 case 2: b
+= k
[1]; /* fall through */
111 final(a
, b
, c
); /* fall through */
112 case 0: /* case 0: nothing left to add */
115 /*---------------------------------------------- report the result */
120 #if (CAA_BITS_PER_LONG == 32)
122 unsigned long lttng_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
124 unsigned int key
= (unsigned int) _key
;
126 assert(length
== sizeof(unsigned int));
127 return lttng_hash_u32(&key
, 1, seed
);
131 unsigned long lttng_hash_mix(const void *_key
, size_t length
, unsigned long seed
)
142 assert(length
== sizeof(unsigned long));
143 v
.v64
= (uint64_t) seed
;
144 key
.v64
= (uint64_t) _key
;
145 lttng_hashword2(key
.v32
, 2, &v
.v32
[0], &v
.v32
[1]);
150 #endif /* _UST_COMMON_HASH_H */
This page took 0.037506 seconds and 4 git commands to generate.