xref: /openbsd-src/gnu/usr.bin/binutils/intl/hash-string.h (revision d2201f2f89f0be1a0be6f7568000ed297414a06d)
1*f7cc78ecSespie /* Implements a string hashing function.
2*f7cc78ecSespie    Copyright (C) 1995, 1997 Free Software Foundation, Inc.
3*f7cc78ecSespie 
4*f7cc78ecSespie    This program is free software; you can redistribute it and/or modify
5*f7cc78ecSespie    it under the terms of the GNU General Public License as published by
6*f7cc78ecSespie    the Free Software Foundation; either version 2, or (at your option)
7*f7cc78ecSespie    any later version.
8*f7cc78ecSespie 
9*f7cc78ecSespie    This program is distributed in the hope that it will be useful,
10*f7cc78ecSespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*f7cc78ecSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*f7cc78ecSespie    GNU General Public License for more details.
13*f7cc78ecSespie 
14*f7cc78ecSespie    You should have received a copy of the GNU Library General Public
15*f7cc78ecSespie    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16*f7cc78ecSespie    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17*f7cc78ecSespie    Boston, MA 02111-1307, USA.  */
18*f7cc78ecSespie 
19*f7cc78ecSespie /* @@ end of prolog @@ */
20*f7cc78ecSespie 
21*f7cc78ecSespie #ifndef PARAMS
22*f7cc78ecSespie # if __STDC__
23*f7cc78ecSespie #  define PARAMS(Args) Args
24*f7cc78ecSespie # else
25*f7cc78ecSespie #  define PARAMS(Args) ()
26*f7cc78ecSespie # endif
27*f7cc78ecSespie #endif
28*f7cc78ecSespie 
29*f7cc78ecSespie /* We assume to have `unsigned long int' value with at least 32 bits.  */
30*f7cc78ecSespie #define HASHWORDBITS 32
31*f7cc78ecSespie 
32*f7cc78ecSespie 
33*f7cc78ecSespie /* Defines the so called `hashpjw' function by P.J. Weinberger
34*f7cc78ecSespie    [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
35*f7cc78ecSespie    1986, 1987 Bell Telephone Laboratories, Inc.]  */
36*f7cc78ecSespie static unsigned long hash_string PARAMS ((const char *__str_param));
37*f7cc78ecSespie 
38*f7cc78ecSespie static inline unsigned long
hash_string(str_param)39*f7cc78ecSespie hash_string (str_param)
40*f7cc78ecSespie      const char *str_param;
41*f7cc78ecSespie {
42*f7cc78ecSespie   unsigned long int hval, g;
43*f7cc78ecSespie   const char *str = str_param;
44*f7cc78ecSespie 
45*f7cc78ecSespie   /* Compute the hash value for the given string.  */
46*f7cc78ecSespie   hval = 0;
47*f7cc78ecSespie   while (*str != '\0')
48*f7cc78ecSespie     {
49*f7cc78ecSespie       hval <<= 4;
50*f7cc78ecSespie       hval += (unsigned long) *str++;
51*f7cc78ecSespie       g = hval & ((unsigned long) 0xf << (HASHWORDBITS - 4));
52*f7cc78ecSespie       if (g != 0)
53*f7cc78ecSespie 	{
54*f7cc78ecSespie 	  hval ^= g >> (HASHWORDBITS - 8);
55*f7cc78ecSespie 	  hval ^= g;
56*f7cc78ecSespie 	}
57*f7cc78ecSespie     }
58*f7cc78ecSespie   return hval;
59*f7cc78ecSespie }
60