we trying speedup code under clang , visual c++ (gcc , icc ok). thought use constexpr tell clang value compile time constant causing compile error:
$ clang++ -g2 -o3 -std=c++11 test.cxx -o test.exe test.cxx:11:46: error: function parameter cannot constexpr unsigned int rightrotate(unsigned int value, constexpr unsigned int rotate) ^ 1 error generated. here reduced case:
$ cat test.cxx #include <iostream> unsigned int rightrotate(unsigned int value, constexpr unsigned int rotate); int main(int argc, char* argv[]) { std::cout << "rotated: " << rightrotate(argc, 2) << std::endl; return 0; } unsigned int rightrotate(unsigned int value, constexpr unsigned int rotate) { // x = value; y = rotate __asm__ ("rorl %1, %0" : "+mq" (value) : "i" ((unsigned char)rotate)); return value; } gcc , icc right thing. recognize value 2 in expression rightrotate(argc, 2) cannot change under laws of physical universe know them, , treat 2 compile time constant , propagate assembly code.
if remove constexpr, clang , vc++ aseembles function rotate register, 3x slower rotate immediate.
how tell clang function parameter rotate compile time constant, , should assembled rotate immediate rather rotate register?
you use non-type template arguments this:
template <unsigned int rotate> rightrotate(unsigned int value) { ... } you'd invoke as
rightrotate<137>(argument); // rotate 137 here
Comments
Post a Comment