libFirm 1.20
libfirm/adt/hashptr.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
00003  *
00004  * This file is part of libFirm.
00005  *
00006  * This file may be distributed and/or modified under the terms of the
00007  * GNU General Public License version 2 as published by the Free Software
00008  * Foundation and appearing in the file LICENSE.GPL included in the
00009  * packaging of this file.
00010  *
00011  * Licensees holding valid libFirm Professional Edition licenses may use
00012  * this file in accordance with the libFirm Commercial License.
00013  * Agreement provided with the Software.
00014  *
00015  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017  * PURPOSE.
00018  */
00019 
00025 #ifndef FIRM_ADT_HASHPTR_H
00026 #define FIRM_ADT_HASHPTR_H
00027 
00028 #include <stdlib.h>
00029 #include "../begin.h"
00030 
00039 #define _FIRM_FNV_OFFSET_BASIS 2166136261U
00040 #define _FIRM_FNV_FNV_PRIME 16777619U
00041 
00042 /* Computing x * _FIRM_FNV_FNV_PRIME */
00043 #define _FIRM_FNV_TIMES_PRIME(x) ((x) * _FIRM_FNV_FNV_PRIME)
00044 
00050 static inline unsigned hash_data(const unsigned char *data, size_t bytes)
00051 {
00052     size_t   i;
00053     unsigned hash = _FIRM_FNV_OFFSET_BASIS;
00054 
00055     for(i = 0; i < bytes; ++i) {
00056         hash = _FIRM_FNV_TIMES_PRIME(hash);
00057         hash ^= data[i];
00058     }
00059 
00060     return hash;
00061 }
00062 
00068 static inline unsigned hash_str(const char *str)
00069 {
00070     unsigned i;
00071     unsigned hash = _FIRM_FNV_OFFSET_BASIS;
00072 
00073     for(i = 0; str[i] != '\0'; ++i) {
00074         hash = _FIRM_FNV_TIMES_PRIME(hash);
00075         hash ^= str[i];
00076     }
00077 
00078     return hash;
00079 }
00080 
00086 static inline unsigned hash_ptr(const void *ptr)
00087 {
00088     return ((unsigned)(((char *) (ptr) - (char *)0) >> 3));
00089 }
00090 
00097 static inline unsigned hash_combine(unsigned x, unsigned y)
00098 {
00099     unsigned hash = _FIRM_FNV_TIMES_PRIME(_FIRM_FNV_OFFSET_BASIS);
00100     hash ^= x;
00101     hash  = _FIRM_FNV_TIMES_PRIME(hash);
00102     hash ^= y;
00103     return hash;
00104 }
00105 
00108 #include "../end.h"
00109 
00110 #endif