What is the function parameter equivalent of constexpr?
Asked Answered
I

1

2

We are trying to speedup some code under Clang and Visual C++ (GCC and ICC is OK). We thought we could use constexpr to tell Clang a value is a compile time constant but its causing a compile error:

$ clang++ -g2 -O3 -std=c++11 test.cxx -o test.exe
test.cxx:11:46: error: function parameter cannot be constexpr
unsigned int RightRotate(unsigned int value, constexpr unsigned int rotate)
                                             ^
1 error generated.

Here is the 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 and ICC will do the right thing. They recognize a value like 2 in the expression RightRotate(argc, 2) cannot change under the laws of the physical universe as we know them, and it will treat 2 as a compile time constant and propagate it into the assembly code.

If we remove the constexpr, then Clang and VC++ aseembles the function into a rotate REGISTER, which is up to 3x slower than a rotate IMMEDIATE.

How do we tell Clang the function parameter rotate is a compile time constant, and it should be assembled into a rotate IMMEDIATE rather than a rotate REGISTER?

Instigate answered 2/9, 2016 at 3:55 Comment(3)
If the value is a compile time constant, then presumably the compiler needs to generate a version of the function for each value of that constant. That sounds to me like a template.Porphyria
@Porphyria - I tried to parameterize and specialize, but it results in function template partial specialization is not allowed. What alternate reality or universe is Clang and the C++ committee operating in?Instigate
@Porphyria - Here's the follow-up question: Parameterization and and “function template partial specialization is not allowed”Instigate
U
5

You could use non-type template arguments for this:

template <unsigned int rotate> RightRotate(unsigned int value) {
     ...
}

You'd then invoke it as

RightRotate<137>(argument); // rotate is 137 here
Unthinking answered 2/9, 2016 at 3:58 Comment(3)
Thanks @templatetypedef. We've got some other constraints that are getting in the way of a full blown template at the moment. I've been thinking seriously about adding a new set of functions so that we can use templates to achieve the good code generation....Instigate
Here's the follow-up question: Parameterization and and “function template partial specialization is not allowed”Instigate
If RightRotate were a constructor instead, or a function with deduced template arguments (ADL), you can use template<unsigned int UI> using UIConst = integral_constant<unsigned int, UI>; in calls like RightRotate(UIConst<rotate>{}). (If you want to deduce the type from a constructor of that style (ADL) e.g. auto rr = RightRotate(UIConst<90>{}); where rr is deduced to be an instance of struct RightRotate<90> you will unfortunately need a helper function in the style of make_tuple.) Hope this helps someone!Sexlimited

© 2022 - 2024 — McMap. All rights reserved.