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