Imported Upstream version 0.153
[platform/upstream/elfutils.git] / libelf / dl-hash.h
1 /* Compute hash value for given string according to ELF standard.
2    Copyright (C) 2006 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 1995.
5
6    Red Hat elfutils is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by the
8    Free Software Foundation; version 2 of the License.
9
10    Red Hat elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with Red Hat elfutils; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19    In addition, as a special exception, Red Hat, Inc. gives You the
20    additional right to link the code of Red Hat elfutils with code licensed
21    under an Open Source Initiative certified open source license
22    (http://www.opensource.org/licenses/index.php) and to distribute linked
23    combinations including the two.  Non-GPL Code permitted under this
24    exception must only link to the code of Red Hat elfutils through those
25    well defined interfaces identified in the file named EXCEPTION found in
26    the source code files (the "Approved Interfaces").  The files of Non-GPL
27    Code may instantiate templates or use macros or inline functions from
28    the Approved Interfaces without causing the resulting work to be covered
29    by the GNU General Public License.  Only Red Hat, Inc. may make changes
30    or additions to the list of Approved Interfaces.  Red Hat's grant of
31    this exception is conditioned upon your not adding any new exceptions.
32    If you wish to add a new Approved Interface or exception, please contact
33    Red Hat.  You must obey the GNU General Public License in all respects
34    for all of the Red Hat elfutils code and other code used in conjunction
35    with Red Hat elfutils except the Non-GPL Code covered by this exception.
36    If you modify this file, you may extend this exception to your version
37    of the file, but you are not obligated to do so.  If you do not wish to
38    provide this exception without modification, you must delete this
39    exception statement from your version and license this file solely under
40    the GPL without exception.
41
42    Red Hat elfutils is an included package of the Open Invention Network.
43    An included package of the Open Invention Network is a package for which
44    Open Invention Network licensees cross-license their patents.  No patent
45    license is granted, either expressly or impliedly, by designation as an
46    included package.  Should you wish to participate in the Open Invention
47    Network licensing program, please visit www.openinventionnetwork.com
48    <http://www.openinventionnetwork.com>.  */
49
50 #ifndef _DL_HASH_H
51 #define _DL_HASH_H      1
52
53
54 /* This is the hashing function specified by the ELF ABI.  In the
55    first five operations no overflow is possible so we optimized it a
56    bit.  */
57 static inline unsigned int
58 __attribute__ ((__pure__))
59 _dl_elf_hash (const char *name)
60 {
61   const unsigned char *iname = (const unsigned char *) name;
62   unsigned int hash = (unsigned int) *iname++;
63   if (*iname != '\0')
64     {
65       hash = (hash << 4) + (unsigned int) *iname++;
66       if (*iname != '\0')
67         {
68           hash = (hash << 4) + (unsigned int) *iname++;
69           if (*iname != '\0')
70             {
71               hash = (hash << 4) + (unsigned int) *iname++;
72               if (*iname != '\0')
73                 {
74                   hash = (hash << 4) + (unsigned int) *iname++;
75                   while (*iname != '\0')
76                     {
77                       unsigned int hi;
78                       hash = (hash << 4) + (unsigned int) *iname++;
79                       hi = hash & 0xf0000000;
80
81                       /* The algorithm specified in the ELF ABI is as
82                          follows:
83
84                          if (hi != 0)
85                          hash ^= hi >> 24;
86
87                          hash &= ~hi;
88
89                          But the following is equivalent and a lot
90                          faster, especially on modern processors.  */
91
92                       hash ^= hi;
93                       hash ^= hi >> 24;
94                     }
95                 }
96             }
97         }
98     }
99   return hash;
100 }
101
102 #endif /* dl-hash.h */