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