Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:01

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
0012 #define BOOST_COMPUTE_FUNCTIONAL_HASH_HPP
0013 
0014 #include <boost/compute/function.hpp>
0015 #include <boost/compute/types/fundamental.hpp>
0016 
0017 namespace boost {
0018 namespace compute {
0019 namespace detail {
0020 
0021 template<class Key>
0022 std::string make_hash_function_name()
0023 {
0024     return std::string("boost_hash_") + type_name<Key>();
0025 }
0026 
0027 template<class Key>
0028 inline std::string make_hash_function_source()
0029 {
0030   std::stringstream source;
0031   source << "inline ulong " << make_hash_function_name<Key>()
0032          << "(const " << type_name<Key>() << " x)\n"
0033          << "{\n"
0034          // note we reinterpret the argument as a 32-bit uint and
0035          // then promote it to a 64-bit ulong for the result type
0036          << "    ulong a = as_uint(x);\n"
0037          << "    a = (a ^ 61) ^ (a >> 16);\n"
0038          << "    a = a + (a << 3);\n"
0039          << "    a = a ^ (a >> 4);\n"
0040          << "    a = a * 0x27d4eb2d;\n"
0041          << "    a = a ^ (a >> 15);\n"
0042          << "    return a;\n"
0043          << "}\n";
0044     return source.str();
0045 }
0046 
0047 template<class Key>
0048 struct hash_impl
0049 {
0050     typedef Key argument_type;
0051     typedef ulong_ result_type;
0052 
0053     hash_impl()
0054         : m_function("")
0055     {
0056         m_function = make_function_from_source<result_type(argument_type)>(
0057             make_hash_function_name<argument_type>(),
0058             make_hash_function_source<argument_type>()
0059         );
0060     }
0061 
0062     template<class Arg>
0063     invoked_function<result_type, boost::tuple<Arg> >
0064     operator()(const Arg &arg) const
0065     {
0066         return m_function(arg);
0067     }
0068 
0069     function<result_type(argument_type)> m_function;
0070 };
0071 
0072 } // end detail namespace
0073 
0074 /// The hash function returns a hash value for the input value.
0075 ///
0076 /// The return type is \c ulong_ (the OpenCL unsigned long type).
0077 template<class Key> struct hash;
0078 
0079 /// \internal_
0080 template<> struct hash<int_> : detail::hash_impl<int_> { };
0081 
0082 /// \internal_
0083 template<> struct hash<uint_> : detail::hash_impl<uint_> { };
0084 
0085 /// \internal_
0086 template<> struct hash<float_> : detail::hash_impl<float_> { };
0087 
0088 } // end compute namespace
0089 } // end boost namespace
0090 
0091 #endif // BOOST_COMPUTE_FUNCTIONAL_HASH_HPP