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)
68 /* Based on http://burtleburtle.net/bob/c/lookup3.c, by Bob
69 Jenkins <bob_jenkins@burtleburtle.net>, as retrieved on April
70 8, 2009. The license information there says the following:
71 "You can use this free for any purpose. It's in the public
72 domain. It has no warranty." and "You may use this code any
73 way you wish, private, educational, or commercial. It's
76 #define HASH_ROT(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
78 #define HASH_MIX(a, b, c) \
81 a -= c; a ^= HASH_ROT (c, 4); c += b; \
82 b -= a; b ^= HASH_ROT (a, 6); a += c; \
83 c -= b; c ^= HASH_ROT (b, 8); b += a; \
84 a -= c; a ^= HASH_ROT (c, 16); c += b; \
85 b -= a; b ^= HASH_ROT (a, 19); a += c; \
86 c -= b; c ^= HASH_ROT (b, 4); b += a; \
90 #define HASH_FINAL(a, b, c) \
93 c ^= b; c -= HASH_ROT (b, 14); \
94 a ^= c; a -= HASH_ROT (c, 11); \
95 b ^= a; b -= HASH_ROT (a, 25); \
96 c ^= b; c -= HASH_ROT (b, 16); \
97 a ^= c; a -= HASH_ROT (c, 4); \
98 b ^= a; b -= HASH_ROT (a, 14); \
99 c ^= b; c -= HASH_ROT (b, 24); \
103 /* Returns a hash value for the N bytes starting at P, starting
106 hash_bytes (const void *p_, size_t n, unsigned int basis)
108 const uint8_t *p = p_;
112 a = b = c = 0xdeadbeef + n + basis;
134 HASH_FINAL (a, b, c);
138 /* Returns a hash value for null-terminated string S, starting
141 hash_string (const char *s, unsigned int basis)
143 return hash_bytes (s, strlen (s), basis);
146 /* Returns a hash value for integer X, starting from BASIS. */
148 hash_int (int x, unsigned int basis)
160 /* Returns a hash value for double D, starting from BASIS. */
162 hash_double (double d, unsigned int basis)
164 if (sizeof (double) == 8)
169 a = b = c = 0xdeadbeefU + 8 + basis;
174 HASH_FINAL (a, b, c);
178 return hash_bytes (&d, sizeof d, basis);
181 /* Returns a hash value for pointer P, starting from BASIS. */
183 hash_pointer (const void *p, unsigned int basis)
185 /* Casting to uintptr_t before casting to int suppresses a GCC warning about
186 on 64-bit platforms. */
187 return hash_int ((int) (uintptr_t) p, basis);