benchmark silo added
[c11concurrency-benchmarks.git] / silo / masstree / hashcode.hh
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2013 President and Fellows of Harvard College
4  * Copyright (c) 2012-2013 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 #ifndef CLICK_HASHCODE_HH
17 #define CLICK_HASHCODE_HH
18 #include <stddef.h>
19 #include <inttypes.h>
20 #if HAVE_STD_HASH
21 #include <functional>
22 #endif
23
24 // Notes about the hashcode template: On GCC 4.3.0, "template <>" is required
25 // on the specializations or they aren't used.  Just plain overloaded
26 // functions aren't used.  The specializations must be e.g. "const char &",
27 // not "char", or GCC complains about a specialization not matching the
28 // general template.  The main template takes a const reference for two
29 // reasons.  First, providing both "hashcode_t hashcode(T)" and "hashcode_t
30 // hashcode(const T&)" leads to ambiguity errors.  Second, providing only
31 // "hashcode_t hashcode(T)" is slower by looks like 8% when T is a String,
32 // because of copy constructors; for types with more expensive non-default
33 // copy constructors this would probably be worse.
34
35 typedef size_t hashcode_t;      ///< Typical type for a hashcode() value.
36
37 template <typename T>
38 inline hashcode_t hashcode(T const &x) {
39     return x.hashcode();
40 }
41
42 template <>
43 inline hashcode_t hashcode(char const &x) {
44     return x;
45 }
46
47 template <>
48 inline hashcode_t hashcode(signed char const &x) {
49     return x;
50 }
51
52 template <>
53 inline hashcode_t hashcode(unsigned char const &x) {
54     return x;
55 }
56
57 template <>
58 inline hashcode_t hashcode(short const &x) {
59     return x;
60 }
61
62 template <>
63 inline hashcode_t hashcode(unsigned short const &x) {
64     return x;
65 }
66
67 template <>
68 inline hashcode_t hashcode(int const &x) {
69     return x;
70 }
71
72 template <>
73 inline hashcode_t hashcode(unsigned const &x) {
74     return x;
75 }
76
77 template <>
78 inline hashcode_t hashcode(long const &x) {
79     return x;
80 }
81
82 template <>
83 inline hashcode_t hashcode(unsigned long const &x) {
84     return x;
85 }
86
87 template <>
88 inline hashcode_t hashcode(long long const &x) {
89     return (x >> 32) ^ x;
90 }
91
92 template <>
93 inline hashcode_t hashcode(unsigned long long const &x) {
94     return (x >> 32) ^ x;
95 }
96
97 #if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG
98 template <>
99 inline hashcode_t hashcode(int64_t const &x) {
100     return (x >> 32) ^ x;
101 }
102
103 template <>
104 inline hashcode_t hashcode(uint64_t const &x) {
105     return (x >> 32) ^ x;
106 }
107 #endif
108
109 template <typename T>
110 inline hashcode_t hashcode(T * const &x) {
111     return reinterpret_cast<uintptr_t>(x) >> 3;
112 }
113
114 template <typename T>
115 inline typename T::key_const_reference hashkey(const T &x) {
116     return x.hashkey();
117 }
118
119 #endif