libFirm
hashptr.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5 
11 #ifndef FIRM_ADT_HASHPTR_H
12 #define FIRM_ADT_HASHPTR_H
13 
14 #include <stddef.h>
15 
16 #include "../begin.h"
17 
26 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
27 #define _FIRM_FNV_FNV_PRIME 16777619U
28 
34 static inline unsigned hash_data(unsigned char const *data, size_t bytes)
35 {
36  unsigned hash = _FIRM_FNV_OFFSET_BASIS;
37  for (size_t i = 0; i < bytes; ++i) {
38  hash *= _FIRM_FNV_FNV_PRIME;
39  hash ^= data[i];
40  }
41 
42  return hash;
43 }
44 
50 static inline unsigned hash_str(char const *const str)
51 {
52  unsigned hash = _FIRM_FNV_OFFSET_BASIS;
53  for(char const *c = str; *c != '\0'; ++c) {
54  hash *= _FIRM_FNV_FNV_PRIME;
55  hash ^= *c;
56  }
57 
58  return hash;
59 }
60 
66 static inline unsigned hash_ptr(void const *const ptr)
67 {
68  return (unsigned)(((char const *)ptr - (char const *)0) >> 3);
69 }
70 
77 static inline unsigned hash_combine(unsigned const x, unsigned const y)
78 {
79  unsigned hash = _FIRM_FNV_OFFSET_BASIS;
80  hash *= _FIRM_FNV_FNV_PRIME;
81  hash ^= x;
82  hash *= _FIRM_FNV_FNV_PRIME;
83  hash ^= y;
84  return hash;
85 }
86 
89 #include "../end.h"
90 
91 #endif
static unsigned hash_str(char const *const str)
Returns a hash value for a string.
Definition: hashptr.h:50
static unsigned hash_ptr(void const *const ptr)
Returns a hash value for a pointer.
Definition: hashptr.h:66
static unsigned hash_combine(unsigned const x, unsigned const y)
Combines 2 hash values.
Definition: hashptr.h:77
static unsigned hash_data(unsigned char const *data, size_t bytes)
Returns a hash value for a block of data.
Definition: hashptr.h:34