#ifndef BOOST_COMPUTE_FUNCTIONAL_POPCOUNT_HPP
#define BOOST_COMPUTE_FUNCTIONAL_POPCOUNT_HPP
#include <boost/compute/function.hpp>
#include <boost/compute/type_traits/type_name.hpp>
namespace boost {
namespace compute {
template<class T>
class popcount : public function<T(T)>
{
public:
popcount()
: function<T(T)>("boost_popcount")
{
std::stringstream s;
s << "inline " << type_name<T>() << " boost_popcount"
<< "(const " << type_name<T>() << " x)\n"
<< "{\n"
<< "#if __OPENCL_VERSION__ >= 120\n"
<< " return popcount(x);\n"
<< "#else\n"
<< " " << type_name<T>() << " count = 0;\n"
<< " for(" << type_name<T>() << " i = 0; i < sizeof(i) * CHAR_BIT; i++){\n"
<< " if(x & (" << type_name<T>() << ") 1 << i){\n"
<< " count++;\n"
<< " }\n"
<< " }\n"
<< " return count;\n"
<< "#endif\n"
<< "}\n";
this->set_source(s.str());
}
};
} }
#endif