gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Hash.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef HASH_H
7 #define HASH_H
8 
9 // FNV (Fowler–Noll–Vo) hashing parameters
10 
11 #if defined(HAVE_64BIT_SIZE_T)
12 #define FNV_PRIME 1099511628211UL
13 #define FNV_OFFSET_BASIS 14695981039346656037UL
14 #else
15 #define FNV_PRIME 16777619UL
16 #define FNV_OFFSET_BASIS 2166136261UL
17 #endif
18 
19 // Hash FNV1a implemented via for loop. "key" has size "len" bytes.
20 
21 inline size_t hash_FNV1a(const void *const key, const int len)
22 {
23  const unsigned char *p = static_cast<const unsigned char *>(key);
24  size_t hash = FNV_OFFSET_BASIS;
25  for(int n = len; n--;) hash = (hash ^ static_cast<size_t>(*p++)) * FNV_PRIME;
26  return hash;
27 }
28 
29 // Hash FNV1a implemented via template-metaprogramming loop. This should be
30 // used if the length N is known at compile time. "key" has size "N" bytes.
31 // Use the entry point HashFNV1a<N>::eval(key).
32 
33 template <int N> struct Hash1FNV1a {
34  static size_t eval(size_t hash, const unsigned char *p)
35  {
36  return Hash1FNV1a<N - 1>::eval((hash ^ static_cast<size_t>(*p)) * FNV_PRIME,
37  p + 1);
38  }
39 };
40 
41 template <> struct Hash1FNV1a<1> {
42  static size_t eval(size_t hash, const unsigned char *p)
43  {
44  return (hash ^ static_cast<size_t>(*p)) * FNV_PRIME;
45  }
46 };
47 
48 // Entry point
49 template <int N> struct HashFNV1a {
50  static size_t eval(const void *const key)
51  {
52  size_t hash = FNV_OFFSET_BASIS;
53  return Hash1FNV1a<N>::eval(hash, static_cast<const unsigned char *>(key));
54  }
55 };
56 
57 #endif
HashFNV1a::eval
static size_t eval(const void *const key)
Definition: Hash.h:50
Hash1FNV1a::eval
static size_t eval(size_t hash, const unsigned char *p)
Definition: Hash.h:34
FNV_PRIME
#define FNV_PRIME
Definition: Hash.h:15
hash_FNV1a
size_t hash_FNV1a(const void *const key, const int len)
Definition: Hash.h:21
Hash1FNV1a
Definition: Hash.h:33
HashFNV1a
Definition: Hash.h:49
Hash1FNV1a< 1 >::eval
static size_t eval(size_t hash, const unsigned char *p)
Definition: Hash.h:42
FNV_OFFSET_BASIS
#define FNV_OFFSET_BASIS
Definition: Hash.h:16