Skip to content
Snippets Groups Projects
hash.c 858 B
Newer Older
/*
 * SPDX-FileCopyrightText: 2022 Bruce Cowan <bruce@bcowan.me.uk>
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "hash.h"

#include <string.h>

#include <xxhash.h>

unsigned
str_hash (const void *data)
{
  size_t length = strlen ((const char *) data);

  XXH64_hash_t hash = XXH3_64bits (data, length);

#if (SIZEOF_UNSIGNED == 2)
  unsigned ret = hash & 0xffff;
  ret ^= (unsigned) (hash >> 16 & 0xffff);
  ret ^= (unsigned) (hash >> 32 & 0xffff);
  ret ^= (unsigned) (hash >> 48 & 0xffff);
  return ret;
#elif (SIZEOF_UNSIGNED == 4)
  return (hash & 0xffffffff) ^ (hash >> 32 & 0xffffffff);
#elif (SIZEOF_UNSIGNED == 8)
  return (unsigned) hash;
#else
#  error "Unsupported unsigned integer size"
#endif
}

bool
str_equal (const void *a,
           const void *b)
{
  return !strcmp ((const char *) a, (const char *) b);