2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Hash functions
9 * @author Michael Beck, Sebastian Hack
11 #ifndef FIRM_ADT_HASHPTR_H
12 #define FIRM_ADT_HASHPTR_H
19 * @defgroup hashptr Hash Functions
25 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
26 #define _FIRM_FNV_FNV_PRIME 16777619U
28 /* Computing x * _FIRM_FNV_FNV_PRIME */
29 #define _FIRM_FNV_TIMES_PRIME(x) ((x) * _FIRM_FNV_FNV_PRIME)
34 * Returns a hash value for a block of data.
36 static inline unsigned hash_data(const unsigned char *data, size_t bytes)
39 unsigned hash = _FIRM_FNV_OFFSET_BASIS;
41 for(i = 0; i < bytes; ++i) {
42 hash = _FIRM_FNV_TIMES_PRIME(hash);
50 * Returns a hash value for a string.
51 * @param str The string (can be const).
52 * @return A hash value for the string.
54 static inline unsigned hash_str(const char *str)
57 unsigned hash = _FIRM_FNV_OFFSET_BASIS;
59 for(i = 0; str[i] != '\0'; ++i) {
60 hash = _FIRM_FNV_TIMES_PRIME(hash);
68 * Returns a hash value for a pointer.
69 * Pointer addresses are mostly aligned to 4 or 8 bytes. So we remove the
72 static inline unsigned hash_ptr(const void *ptr)
74 return ((unsigned)(((char *) (ptr) - (char *)0) >> 3));
78 * Combines 2 hash values.
79 * @param x One hash value.
80 * @param y Another hash value.
81 * @return A hash value computed from both.
83 static inline unsigned hash_combine(unsigned x, unsigned y)
85 unsigned hash = _FIRM_FNV_TIMES_PRIME(_FIRM_FNV_OFFSET_BASIS);
87 hash = _FIRM_FNV_TIMES_PRIME(hash);