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