1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
12 #define BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
14 #include <boost/compute/function.hpp>
15 #include <boost/compute/types/fundamental.hpp>
22 std::string make_hash_function_name()
24 return std::string("boost_hash_") + type_name<Key>();
28 inline std::string make_hash_function_source()
30 std::stringstream source;
31 source << "inline ulong " << make_hash_function_name<Key>()
32 << "(const " << type_name<Key>() << " x)\n"
34 // note we reinterpret the argument as a 32-bit uint and
35 // then promote it to a 64-bit ulong for the result type
36 << " ulong a = as_uint(x);\n"
37 << " a = (a ^ 61) ^ (a >> 16);\n"
38 << " a = a + (a << 3);\n"
39 << " a = a ^ (a >> 4);\n"
40 << " a = a * 0x27d4eb2d;\n"
41 << " a = a ^ (a >> 15);\n"
50 typedef Key argument_type;
51 typedef ulong_ result_type;
56 m_function = make_function_from_source<result_type(argument_type)>(
57 make_hash_function_name<argument_type>(),
58 make_hash_function_source<argument_type>()
63 invoked_function<result_type, boost::tuple<Arg> >
64 operator()(const Arg &arg) const
66 return m_function(arg);
69 function<result_type(argument_type)> m_function;
72 } // end detail namespace
74 /// The hash function returns a hash value for the input value.
76 /// The return type is \c ulong_ (the OpenCL unsigned long type).
77 template<class Key> struct hash;
80 template<> struct hash<int_> : detail::hash_impl<int_> { };
83 template<> struct hash<uint_> : detail::hash_impl<uint_> { };
86 template<> struct hash<float_> : detail::hash_impl<float_> { };
88 } // end compute namespace
89 } // end boost namespace
91 #endif // BOOST_COMPUTE_FUNCTIONAL_HASH_HPP