2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Hash function for pointers
23 * @author Michael Beck, Sebastian Hack
26 #ifndef FIRM_ADT_HASHPTR_H
27 #define FIRM_ADT_HASHPTR_H
29 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
30 #define _FIRM_FNV_FNV_PRIME 16777619U
32 /* Computing x * _FIRM_FNV_FNV_PRIME */
33 #define _FIRM_FNV_TIMES_PRIME(x) ((x) * _FIRM_FNV_FNV_PRIME)
35 static inline unsigned firm_fnv_hash(const unsigned char *data, unsigned bytes)
38 unsigned hash = _FIRM_FNV_OFFSET_BASIS;
40 for(i = 0; i < bytes; ++i) {
41 hash = _FIRM_FNV_TIMES_PRIME(hash);
48 static inline unsigned firm_fnv_hash_str(const char *data)
51 unsigned hash = _FIRM_FNV_OFFSET_BASIS;
53 for(i = 0; data[i] != '\0'; ++i) {
54 hash = _FIRM_FNV_TIMES_PRIME(hash);
62 * hash a pointer value: Pointer addresses are mostly aligned to 4
63 * or 8 bytes. So we remove the lowest 3 bits
65 #define HASH_PTR(ptr) ((unsigned)(((char *) (ptr) - (char *)0) >> 3))
69 * @param str The string (can be const).
70 * @param len The length of the string.
71 * @return A hash value for the string.
73 #define HASH_STR(str,len) firm_fnv_hash((const unsigned char *) (str), (len))
76 #pragma warning(disable:4307)
79 static inline unsigned _hash_combine(unsigned x, unsigned y)
81 unsigned hash = _FIRM_FNV_TIMES_PRIME(_FIRM_FNV_OFFSET_BASIS);
83 hash = _FIRM_FNV_TIMES_PRIME(hash);
89 #pragma warning(default:4307)
93 * Make one hash value out of two others.
94 * @param a One hash value.
95 * @param b Another hash value.
96 * @return A hash value computed from the both.
98 #define HASH_COMBINE(a,b) _hash_combine(a, b)