diff --git a/include/boost/compute/detail/meta_kernel.hpp b/include/boost/compute/detail/meta_kernel.hpp index 2d6bd5de7..1b8e52f27 100644 --- a/include/boost/compute/detail/meta_kernel.hpp +++ b/include/boost/compute/detail/meta_kernel.hpp @@ -335,7 +335,8 @@ class meta_kernel std::string source = this->source(); // generate cache key - std::string cache_key = "__boost_meta_kernel_" + detail::sha1(source); + std::string cache_key = "__boost_meta_kernel_" + + static_cast(detail::sha1(source)); // load program cache boost::shared_ptr cache = diff --git a/include/boost/compute/detail/sha1.hpp b/include/boost/compute/detail/sha1.hpp index 4d56b2766..2c0d6daa6 100644 --- a/include/boost/compute/detail/sha1.hpp +++ b/include/boost/compute/detail/sha1.hpp @@ -19,20 +19,31 @@ namespace boost { namespace compute { namespace detail { -// Returns SHA1 hash of the string parameter. -inline std::string sha1(const std::string &src) { - boost::uuids::detail::sha1 sha1; - sha1.process_bytes(src.c_str(), src.size()); - - unsigned int hash[5]; - sha1.get_digest(hash); - - std::ostringstream buf; - for(int i = 0; i < 5; ++i) - buf << std::hex << std::setfill('0') << std::setw(8) << hash[i]; - - return buf.str(); -} +// Accumulates SHA1 hash of the passed strings. +class sha1 { + public: + sha1(const std::string &s = "") { + if (!s.empty()) this->process(s); + } + + sha1& process(const std::string &s) { + h.process_bytes(s.c_str(), s.size()); + return *this; + } + + operator std::string() { + unsigned int digest[5]; + h.get_digest(digest); + + std::ostringstream buf; + for(int i = 0; i < 5; ++i) + buf << std::hex << std::setfill('0') << std::setw(8) << digest[i]; + + return buf.str(); + } + private: + boost::uuids::detail::sha1 h; +}; } // end detail namespace } // end compute namespace diff --git a/include/boost/compute/program.hpp b/include/boost/compute/program.hpp index c85378dfc..83aea796e 100644 --- a/include/boost/compute/program.hpp +++ b/include/boost/compute/program.hpp @@ -505,19 +505,16 @@ class program { #ifdef BOOST_COMPUTE_USE_OFFLINE_CACHE // Get hash string for the kernel. - std::string hash; - { - device d(context.get_device()); - platform p = d.platform(); - - std::ostringstream src; - src << "// " << p.name() << " v" << p.version() << "\n" - << "// " << context.get_device().name() << "\n" - << "// " << options << "\n\n" - << source; - - hash = detail::sha1(src.str()); - } + device d = context.get_device(); + platform p = d.platform(); + + detail::sha1 hash; + hash.process( p.name() ) + .process( p.version() ) + .process( d.name() ) + .process( options ) + .process( source ) + ; // Try to get cached program binaries: try {