mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-26 20:51:11 +00:00
52 lines
1.7 KiB
C
52 lines
1.7 KiB
C
/* Implements hashing function for string with known length.
|
|
Copyright (C) 1996-2017 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include <sys/types.h>
|
|
|
|
/* We assume to have `size_t' value with at least 32 bits. */
|
|
#define HASHWORDBITS 32
|
|
|
|
|
|
/* Defines the so called `hashpjw' function by P.J. Weinberger
|
|
[see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
|
|
1986, 1987 Bell Telephone Laboratories, Inc.] */
|
|
static size_t hash_string (const char *__str_param, size_t __len);
|
|
|
|
static inline size_t
|
|
hash_string (const char *str_param, size_t len)
|
|
{
|
|
size_t hval, g;
|
|
const char *end_str = str_param + len;
|
|
|
|
/* Compute the hash value for the given string. */
|
|
hval = len;
|
|
while (str_param != end_str)
|
|
{
|
|
hval <<= 4;
|
|
hval += (size_t) *str_param++;
|
|
g = hval & ((size_t) 0xf << (HASHWORDBITS - 4));
|
|
if (g != 0)
|
|
{
|
|
hval ^= g >> (HASHWORDBITS - 8);
|
|
hval ^= g;
|
|
}
|
|
}
|
|
return hval;
|
|
}
|