Blame sysdeps/generic/dl-hash.h

Packit 6c4009
/* Compute hash value for given string according to ELF standard.
Packit 6c4009
   Copyright (C) 1995-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef _DL_HASH_H
Packit 6c4009
#define _DL_HASH_H	1
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* This is the hashing function specified by the ELF ABI.  In the
Packit 6c4009
   first five operations no overflow is possible so we optimized it a
Packit 6c4009
   bit.  */
Packit 6c4009
static unsigned int
Packit 6c4009
__attribute__ ((unused))
Packit 6c4009
_dl_elf_hash (const char *name_arg)
Packit 6c4009
{
Packit 6c4009
  const unsigned char *name = (const unsigned char *) name_arg;
Packit 6c4009
  unsigned long int hash = *name;
Packit 6c4009
  if (hash != 0 && name[1] != '\0')
Packit 6c4009
    {
Packit 6c4009
      hash = (hash << 4) + name[1];
Packit 6c4009
      if (name[2] != '\0')
Packit 6c4009
	{
Packit 6c4009
	  hash = (hash << 4) + name[2];
Packit 6c4009
	  if (name[3] != '\0')
Packit 6c4009
	    {
Packit 6c4009
	      hash = (hash << 4) + name[3];
Packit 6c4009
	      if (name[4] != '\0')
Packit 6c4009
		{
Packit 6c4009
		  hash = (hash << 4) + name[4];
Packit 6c4009
		  name += 5;
Packit 6c4009
		  while (*name != '\0')
Packit 6c4009
		    {
Packit 6c4009
		      unsigned long int hi;
Packit 6c4009
		      hash = (hash << 4) + *name++;
Packit 6c4009
		      hi = hash & 0xf0000000;
Packit 6c4009
Packit 6c4009
		      /* The algorithm specified in the ELF ABI is as
Packit 6c4009
			 follows:
Packit 6c4009
Packit 6c4009
			 if (hi != 0)
Packit 6c4009
			   hash ^= hi >> 24;
Packit 6c4009
Packit 6c4009
			 hash &= ~hi;
Packit 6c4009
Packit 6c4009
			 But the following is equivalent and a lot
Packit 6c4009
			 faster, especially on modern processors.  */
Packit 6c4009
Packit 6c4009
		      hash ^= hi >> 24;
Packit 6c4009
		    }
Packit 6c4009
Packit 6c4009
		  /* Second part of the modified formula.  This
Packit 6c4009
		     operation can be lifted outside the loop.  */
Packit 6c4009
		  hash &= 0x0fffffff;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return hash;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* dl-hash.h */