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