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_POPCOUNT_HPP
0012 #define BOOST_COMPUTE_FUNCTIONAL_POPCOUNT_HPP
0013 
0014 #include <boost/compute/function.hpp>
0015 #include <boost/compute/type_traits/type_name.hpp>
0016 
0017 namespace boost {
0018 namespace compute {
0019 
0020 /// Returns the number of non-zero bits in \p x.
0021 ///
0022 /// \see_opencl_ref{popcount}
0023 template<class T>
0024 class popcount : public function<T(T)>
0025 {
0026 public:
0027     popcount()
0028         : function<T(T)>("boost_popcount")
0029     {
0030         std::stringstream s;
0031         s << "inline " << type_name<T>() << " boost_popcount"
0032           << "(const " << type_name<T>() << " x)\n"
0033           << "{\n"
0034           // use built-in popcount if opencl 1.2 is supported
0035           << "#if __OPENCL_VERSION__ >= 120\n"
0036           << "    return popcount(x);\n"
0037           // fallback to generic popcount() implementation
0038           << "#else\n"
0039           << "    " << type_name<T>() << " count = 0;\n"
0040           << "    for(" << type_name<T>() << " i = 0; i < sizeof(i) * CHAR_BIT; i++){\n"
0041           << "        if(x & (" << type_name<T>() << ") 1 << i){\n"
0042           << "            count++;\n"
0043           << "        }\n"
0044           << "    }\n"
0045           << "    return count;\n"
0046           << "#endif\n"
0047           << "}\n";
0048         this->set_source(s.str());
0049     }
0050 };
0051 
0052 } // end compute namespace
0053 } // end boost namespace
0054 
0055 #endif // BOOST_COMPUTE_FUNCTIONAL_POPCOUNT_HPP