1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2008 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/>. */
18 #include <libpspp/hash-functions.h>
23 /* Fowler-Noll-Vo hash constants, for 32-bit word sizes. */
24 #define FNV_32_PRIME 16777619u
25 #define FNV_32_BASIS 2166136261u
27 /* Fowler-Noll-Vo 32-bit hash, for bytes. */
29 hsh_hash_bytes (const void *buf_, size_t size)
31 const unsigned char *buf = (const unsigned char *) buf_;
38 hash = (hash * FNV_32_PRIME) ^ *buf++;
43 /* Fowler-Noll-Vo 32-bit hash, for strings. */
45 hsh_hash_string (const char *s_)
47 const unsigned char *s = (const unsigned char *) s_;
54 hash = (hash * FNV_32_PRIME) ^ *s++;
59 /* Fowler-Noll-Vo 32-bit hash, for case-insensitive strings. */
61 hsh_hash_case_string (const char *s_)
63 const unsigned char *s = (const unsigned char *) s_;
70 hash = (hash * FNV_32_PRIME) ^ toupper (*s++);
79 return hsh_hash_bytes (&i, sizeof i);
82 /* Hash for double. */
84 hsh_hash_double (double d)
87 return hsh_hash_bytes (&d, sizeof d);