Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame^] | 1 | /* Compute hash value for given string according to ELF standard. |
| 2 | Copyright (C) 1995-1998, 2002, 2004 Free Software Foundation, Inc. |
| 3 | Written by Ulrich Drepper <drepper@redhat.com>, 1995. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, version 2. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software Foundation, |
| 16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 17 | |
| 18 | #ifndef _DL_HASH_H |
| 19 | #define _DL_HASH_H 1 |
| 20 | |
| 21 | |
| 22 | /* This is the hashing function specified by the ELF ABI. In the |
| 23 | first five operations no overflow is possible so we optimized it a |
| 24 | bit. */ |
| 25 | static inline unsigned int |
| 26 | __attribute__ ((__pure__)) |
| 27 | _dl_elf_hash (const char *name) |
| 28 | { |
| 29 | const unsigned char *iname = (const unsigned char *) name; |
| 30 | unsigned int hash = (unsigned int) *iname++; |
| 31 | if (*iname != '\0') |
| 32 | { |
| 33 | hash = (hash << 4) + (unsigned int) *iname++; |
| 34 | if (*iname != '\0') |
| 35 | { |
| 36 | hash = (hash << 4) + (unsigned int) *iname++; |
| 37 | if (*iname != '\0') |
| 38 | { |
| 39 | hash = (hash << 4) + (unsigned int) *iname++; |
| 40 | if (*iname != '\0') |
| 41 | { |
| 42 | hash = (hash << 4) + (unsigned int) *iname++; |
| 43 | while (*iname != '\0') |
| 44 | { |
| 45 | unsigned int hi; |
| 46 | hash = (hash << 4) + (unsigned int) *iname++; |
| 47 | hi = hash & 0xf0000000; |
| 48 | |
| 49 | /* The algorithm specified in the ELF ABI is as |
| 50 | follows: |
| 51 | |
| 52 | if (hi != 0) |
| 53 | hash ^= hi >> 24; |
| 54 | |
| 55 | hash &= ~hi; |
| 56 | |
| 57 | But the following is equivalent and a lot |
| 58 | faster, especially on modern processors. */ |
| 59 | |
| 60 | hash ^= hi; |
| 61 | hash ^= hi >> 24; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return hash; |
| 68 | } |
| 69 | |
| 70 | #endif /* dl-hash.h */ |