xref: /openbsd/gnu/usr.bin/texinfo/intl/hash-string.h (revision a1acfa9b)
13fb98d4aSespie /* Description of GNU message catalog format: string hashing function.
2*a1acfa9bSespie    Copyright (C) 1995, 1997-1998, 2000-2003 Free Software Foundation, Inc.
3840175f0Skstailey 
43fb98d4aSespie    This program is free software; you can redistribute it and/or modify it
53fb98d4aSespie    under the terms of the GNU Library General Public License as published
63fb98d4aSespie    by the Free Software Foundation; either version 2, or (at your option)
7840175f0Skstailey    any later version.
8840175f0Skstailey 
9840175f0Skstailey    This program is distributed in the hope that it will be useful,
10840175f0Skstailey    but WITHOUT ANY WARRANTY; without even the implied warranty of
113fb98d4aSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
123fb98d4aSespie    Library General Public License for more details.
13840175f0Skstailey 
1428ea187bSespie    You should have received a copy of the GNU Library General Public
153fb98d4aSespie    License along with this program; if not, write to the Free Software
163fb98d4aSespie    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
173fb98d4aSespie    USA.  */
18840175f0Skstailey 
19840175f0Skstailey /* @@ end of prolog @@ */
20840175f0Skstailey 
21840175f0Skstailey /* We assume to have `unsigned long int' value with at least 32 bits.  */
22840175f0Skstailey #define HASHWORDBITS 32
23840175f0Skstailey 
24840175f0Skstailey 
25840175f0Skstailey /* Defines the so called `hashpjw' function by P.J. Weinberger
26840175f0Skstailey    [see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
27840175f0Skstailey    1986, 1987 Bell Telephone Laboratories, Inc.]  */
283fb98d4aSespie static inline unsigned long int
hash_string(const char * str_param)29*a1acfa9bSespie hash_string (const char *str_param)
30840175f0Skstailey {
31840175f0Skstailey   unsigned long int hval, g;
32840175f0Skstailey   const char *str = str_param;
33840175f0Skstailey 
34840175f0Skstailey   /* Compute the hash value for the given string.  */
35840175f0Skstailey   hval = 0;
36840175f0Skstailey   while (*str != '\0')
37840175f0Skstailey     {
38840175f0Skstailey       hval <<= 4;
39*a1acfa9bSespie       hval += (unsigned char) *str++;
403fb98d4aSespie       g = hval & ((unsigned long int) 0xf << (HASHWORDBITS - 4));
41840175f0Skstailey       if (g != 0)
42840175f0Skstailey 	{
43840175f0Skstailey 	  hval ^= g >> (HASHWORDBITS - 8);
44840175f0Skstailey 	  hval ^= g;
45840175f0Skstailey 	}
46840175f0Skstailey     }
47840175f0Skstailey   return hval;
48840175f0Skstailey }
49