1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2008, 2009, 2010, 2011, 2012, 2019 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "libpspp/hash-functions.h"
22 /* Enable this code only for testing! Theoretically everything
23 should still work, but very inefficient hash tables will result,
24 meaning that the code will be slow. */
25 #warning "HASHING FUNCTIONS ARE DISABLED! EXPECT LOTS OF HASH COLLISIONS!!!"
27 #include "libpspp/compiler.h"
31 hash_bytes (const void *b UNUSED, size_t s UNUSED, unsigned int basis UNUSED)
37 hash_string (const char *s UNUSED, unsigned int basis UNUSED)
43 hash_int (int i UNUSED, unsigned int basis UNUSED)
49 hash_double (double d UNUSED, unsigned int basis UNUSED)
55 hash_pointer (const void *p UNUSED, unsigned int basis UNUSED)
67 /* Based on http://burtleburtle.net/bob/c/lookup3.c, by Bob
68 Jenkins <bob_jenkins@burtleburtle.net>, as retrieved on April
69 8, 2009. The license information there says the following:
70 "You can use this free for any purpose. It's in the public
71 domain. It has no warranty." and "You may use this code any
72 way you wish, private, educational, or commercial. It's
75 #define HASH_ROT(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
77 #define HASH_MIX(a, b, c) \
80 a -= c; a ^= HASH_ROT (c, 4); c += b; \
81 b -= a; b ^= HASH_ROT (a, 6); a += c; \
82 c -= b; c ^= HASH_ROT (b, 8); b += a; \
83 a -= c; a ^= HASH_ROT (c, 16); c += b; \
84 b -= a; b ^= HASH_ROT (a, 19); a += c; \
85 c -= b; c ^= HASH_ROT (b, 4); b += a; \
89 #define HASH_FINAL(a, b, c) \
92 c ^= b; c -= HASH_ROT (b, 14); \
93 a ^= c; a -= HASH_ROT (c, 11); \
94 b ^= a; b -= HASH_ROT (a, 25); \
95 c ^= b; c -= HASH_ROT (b, 16); \
96 a ^= c; a -= HASH_ROT (c, 4); \
97 b ^= a; b -= HASH_ROT (a, 14); \
98 c ^= b; c -= HASH_ROT (b, 24); \
102 /* Returns a hash value for the N bytes starting at P, starting
105 hash_bytes (const void *p_, size_t n, unsigned int basis)
107 const uint8_t *p = p_;
111 a = b = c = 0xdeadbeef + n + basis;
133 HASH_FINAL (a, b, c);
137 /* Returns a hash value for null-terminated string S, starting
140 hash_string (const char *s, unsigned int basis)
142 return hash_bytes (s, strlen (s), basis);
145 /* Returns a hash value for integer X, starting from BASIS. */
147 hash_int (int x, unsigned int basis)
159 /* Returns a hash value for double D, starting from BASIS. */
161 hash_double (double d, unsigned int basis)
163 if (sizeof (double) == 8)
168 a = b = c = 0xdeadbeefU + 8 + basis;
173 HASH_FINAL (a, b, c);
177 return hash_bytes (&d, sizeof d, basis);
180 /* Returns a hash value for pointer P, starting from BASIS. */
182 hash_pointer (const void *p, unsigned int basis)
184 /* Casting to uintptr_t before casting to int suppresses a GCC warning about
185 on 64-bit platforms. */
186 return hash_int ((int) (uintptr_t) p, basis);