client_login_timeout: check wait_for_welcome
[pgbouncer.git] / src / hash.c
blobff055960478b0432ec3674330583421a5ff89a77
1 /*
2 * The contents of this file are public domain.
4 * Based on: lookup3.c, by Bob Jenkins, May 2006, Public Domain.
5 */
7 #include "system.h"
8 #include "hash.h"
11 * A simple version of Bob Jenkins' lookup3.c hash.
13 * It is supposed to give same results as hashlittle() on little-endian
14 * and hashbig() on big-endian machines.
16 * Speed seems comparable to Jenkins' optimized version (~ -10%).
17 * Actual difference varies as it depends on cpu/compiler/libc details.
20 /* rotate uint32 */
21 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
23 /* mix 3 32-bit values reversibly */
24 #define mix(a, b, c) do { \
25 a -= c; a ^= rot(c, 4); c += b; \
26 b -= a; b ^= rot(a, 6); a += c; \
27 c -= b; c ^= rot(b, 8); b += a; \
28 a -= c; a ^= rot(c,16); c += b; \
29 b -= a; b ^= rot(a,19); a += c; \
30 c -= b; c ^= rot(b, 4); b += a; \
31 } while (0)
33 /* final mixing of 3 32-bit values (a,b,c) into c */
34 #define final(a, b, c) do { \
35 c ^= b; c -= rot(b,14); \
36 a ^= c; a -= rot(c,11); \
37 b ^= a; b -= rot(a,25); \
38 c ^= b; c -= rot(b,16); \
39 a ^= c; a -= rot(c, 4); \
40 b ^= a; b -= rot(a,14); \
41 c ^= b; c -= rot(b,24); \
42 } while (0)
45 * GCC does not know how to optimize short variable-length copies.
46 * Its faster to do dumb inlined copy than call out to libc.
48 static inline void simple_memcpy(void *dst_, const void *src_, size_t len)
50 const uint8_t *src = src_;
51 uint8_t *dst = dst_;
52 while (len--)
53 *dst++ = *src++;
56 /* short version - let compiler worry about memory access */
57 uint32_t lookup3_hash(const void *data, size_t len)
59 uint32_t a, b, c;
60 uint32_t buf[3];
61 const uint8_t *p = data;
63 a = b = c = 0xdeadbeef + len;
64 if (len == 0)
65 goto done;
67 while (len > 12) {
68 memcpy(buf, p, 12);
69 a += buf[0];
70 b += buf[1];
71 c += buf[2];
72 mix(a, b, c);
73 p += 12;
74 len -= 12;
77 buf[0] = buf[1] = buf[2] = 0;
78 simple_memcpy(buf, p, len);
79 a += buf[0];
80 b += buf[1];
81 c += buf[2];
82 final(a, b, c);
83 done:
84 return c;
89 * Reversible integer hash function by Thomas Wang.
92 uint32_t hash32(uint32_t v)
94 v = ~v + (v << 15);
95 v = v ^ (v >> 12);
96 v = v + (v << 2);
97 v = v ^ (v >> 4);
98 v = v * 2057;
99 v = v ^ (v >> 16);
100 return v;