fix
[libfirm] / ir / adt / hashptr.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Hash function for pointers
23  * @author      Michael Beck, Sebastian Hack
24  * @version     $Id$
25  */
26 #ifndef FIRM_ADT_HASHPTR_H
27 #define FIRM_ADT_HASHPTR_H
28
29 #include "firm_config.h"
30
31 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
32 #define _FIRM_FNV_FNV_PRIME 16777619U
33
34 /* Computing x * _FIRM_FNV_FNV_PRIME */
35 #define _FIRM_FNV_TIMES_PRIME(x) \
36   (((x) << 24) + ((x) << 8) + ((x) << 7) + ((x) << 4) + ((x) << 1) + 1)
37
38 static INLINE unsigned firm_fnv_hash(const unsigned char *data, unsigned bytes)
39 {
40         unsigned i;
41         unsigned hash = _FIRM_FNV_OFFSET_BASIS;
42
43         for(i = 0; i < bytes; ++i) {
44                 hash = _FIRM_FNV_TIMES_PRIME(hash);
45                 hash ^= data[i];
46         }
47
48         return hash;
49 }
50
51 /**
52  * hash a pointer value: Pointer addresses are mostly aligned to 4
53  * or 8 bytes. So we remove the lowest 3 bits
54  */
55 #define HASH_PTR(ptr)    (((char *) (ptr) - (char *)0) >> 3)
56
57 /**
58  * Hash a string.
59  * @param str The string (can be const).
60  * @param len The length of the string.
61  * @return A hash value for the string.
62  */
63 #define HASH_STR(str,len) firm_fnv_hash((const unsigned char *) (str), (len))
64
65 #ifdef _MSC_VER
66 #pragma warning(disable:4307)
67 #endif /* _MSC_VER */
68
69 static INLINE unsigned _hash_combine(unsigned x, unsigned y)
70 {
71   unsigned hash = _FIRM_FNV_TIMES_PRIME(_FIRM_FNV_OFFSET_BASIS);
72   hash ^= x;
73   hash  = _FIRM_FNV_TIMES_PRIME(hash);
74   hash ^= y;
75   return hash;
76 }
77
78 #ifdef _MSC_VER
79 #pragma warning(default:4307)
80 #endif /* _MSC_VER */
81
82 /**
83  * Make one hash value out of two others.
84  * @param a One hash value.
85  * @param b Another hash value.
86  * @return A hash value computed from the both.
87  */
88 #define HASH_COMBINE(a,b) _hash_combine(a, b)
89
90 #endif