blob: e286d2e83b53bce9754f844a24a0f7ae12848a8b [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Compute hash value for given string according to ELF standard.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00002 Copyright (C) 2006 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 1995.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifndef _DL_HASH_H
31#define _DL_HASH_H 1
32
33
34/* This is the hashing function specified by the ELF ABI. In the
35 first five operations no overflow is possible so we optimized it a
36 bit. */
37static inline unsigned int
38__attribute__ ((__pure__))
39_dl_elf_hash (const char *name)
40{
41 const unsigned char *iname = (const unsigned char *) name;
42 unsigned int hash = (unsigned int) *iname++;
43 if (*iname != '\0')
44 {
45 hash = (hash << 4) + (unsigned int) *iname++;
46 if (*iname != '\0')
47 {
48 hash = (hash << 4) + (unsigned int) *iname++;
49 if (*iname != '\0')
50 {
51 hash = (hash << 4) + (unsigned int) *iname++;
52 if (*iname != '\0')
53 {
54 hash = (hash << 4) + (unsigned int) *iname++;
55 while (*iname != '\0')
56 {
57 unsigned int hi;
58 hash = (hash << 4) + (unsigned int) *iname++;
59 hi = hash & 0xf0000000;
60
61 /* The algorithm specified in the ELF ABI is as
62 follows:
63
64 if (hi != 0)
65 hash ^= hi >> 24;
66
67 hash &= ~hi;
68
69 But the following is equivalent and a lot
70 faster, especially on modern processors. */
71
72 hash ^= hi;
73 hash ^= hi >> 24;
74 }
75 }
76 }
77 }
78 }
79 return hash;
80}
81
82#endif /* dl-hash.h */