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