Blame gl/hash-pjw-bare.c

Packit 549fdc
/* hash-pjw-bare.c -- compute a hash value from a provided buffer.
Packit 549fdc
Packit 549fdc
   Copyright (C) 2012-2016 Free Software Foundation, Inc.
Packit 549fdc
Packit 549fdc
   This program is free software: you can redistribute it and/or modify it
Packit 549fdc
   under the terms of the GNU Lesser General Public License as published
Packit 549fdc
   by the Free Software Foundation; either version 2.1 of the License, or
Packit 549fdc
   (at your option) any later version.
Packit 549fdc
Packit 549fdc
   This program is distributed in the hope that it will be useful,
Packit 549fdc
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
   Lesser General Public License for more details.
Packit 549fdc
Packit 549fdc
   You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 549fdc
Packit 549fdc
#include <config.h>
Packit 549fdc
Packit 549fdc
#include "hash-pjw-bare.h"
Packit 549fdc
Packit 549fdc
#include <limits.h>
Packit 549fdc
Packit 549fdc
#define SIZE_BITS (sizeof (size_t) * CHAR_BIT)
Packit 549fdc
Packit 549fdc
/* Return a hash of the N bytes of X using the method described by
Packit 549fdc
   Bruno Haible in http://www.haible.de/bruno/hashfunc.html.
Packit 549fdc
   Note that while many hash functions reduce their result via modulo
Packit 549fdc
   to a 0..table_size-1 range, this function does not do that.  */
Packit 549fdc
Packit 549fdc
size_t
Packit 549fdc
hash_pjw_bare (const void *x, size_t n)
Packit 549fdc
{
Packit 549fdc
  const unsigned char *s = x;
Packit 549fdc
  size_t h = 0;
Packit 549fdc
  unsigned i;
Packit 549fdc
Packit 549fdc
  for (i = 0; i < n; i++)
Packit 549fdc
    h = s[i] + ((h << 9) | (h >> (SIZE_BITS - 9)));
Packit 549fdc
Packit 549fdc
  return h;
Packit 549fdc
}