1*946379e7Schristos /* Implements a string hashing function.
2*946379e7Schristos Copyright (C) 1995, 1997, 1998, 2000, 2003 Free Software Foundation, Inc.
3*946379e7Schristos This file is part of the GNU C Library.
4*946379e7Schristos
5*946379e7Schristos The GNU C Library is free software; you can redistribute it and/or
6*946379e7Schristos modify it under the terms of the GNU Lesser General Public
7*946379e7Schristos License as published by the Free Software Foundation; either
8*946379e7Schristos version 2.1 of the License, or (at your option) any later version.
9*946379e7Schristos
10*946379e7Schristos The GNU C Library is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*946379e7Schristos Lesser General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU Lesser General Public
16*946379e7Schristos License along with the GNU C Library; if not, write to the Free
17*946379e7Schristos Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18*946379e7Schristos Boston, MA 02110-1301, USA. */
19*946379e7Schristos
20*946379e7Schristos #ifdef HAVE_CONFIG_H
21*946379e7Schristos # include <config.h>
22*946379e7Schristos #endif
23*946379e7Schristos
24*946379e7Schristos /* Specification. */
25*946379e7Schristos #include "hash-string.h"
26*946379e7Schristos
27*946379e7Schristos
28*946379e7Schristos /* Defines the so called `hashpjw' function by P.J. Weinberger
29*946379e7Schristos [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
30*946379e7Schristos 1986, 1987 Bell Telephone Laboratories, Inc.] */
31*946379e7Schristos unsigned long int
__hash_string(const char * str_param)32*946379e7Schristos __hash_string (const char *str_param)
33*946379e7Schristos {
34*946379e7Schristos unsigned long int hval, g;
35*946379e7Schristos const char *str = str_param;
36*946379e7Schristos
37*946379e7Schristos /* Compute the hash value for the given string. */
38*946379e7Schristos hval = 0;
39*946379e7Schristos while (*str != '\0')
40*946379e7Schristos {
41*946379e7Schristos hval <<= 4;
42*946379e7Schristos hval += (unsigned char) *str++;
43*946379e7Schristos g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
44*946379e7Schristos if (g != 0)
45*946379e7Schristos {
46*946379e7Schristos hval ^= g >> (HASHWORDBITS - 8);
47*946379e7Schristos hval ^= g;
48*946379e7Schristos }
49*946379e7Schristos }
50*946379e7Schristos return hval;
51*946379e7Schristos }
52