Branching to constexpr-evaluation / overloading to constexpr

Setup:

I have a function that uses SIMD intrinsics and would like to use it inside some constexpr functions.

For this I need to do constexpr. However, SIMD-intrinsics are not marked as constexpr, and a constant compiler evaluator cannot process them.

I tried replacing SIMD-intrinsics with a C ++ implementation of constexpr that does the same. At runtime, the function became 3.5 times slower, but I was able to use it at compile time (yay?).

Problem :

How can I use this function inside constant expressions without slowing down my program at runtime?

Some ideas:

  • Adding support for the constant evaluation of all built-in SIMDs for the analyzer of constants for compiler expressions for all compilers: perhaps the right solution, but an impossible titanic task.

More pragmatic decisions can be either:

  • overloads the function depending on whether it is executed inside the constant expression (that is, it provides the constexpr version and non-conference spread).
  • or, somehow, the branch inside the constexpr function between the constexpr implementation and the run-time (i.e., it detects in the branch whether the function is running inside the constant expression).

Anyway, I am open to any suggestion that solves my problem.

Advice

  • @RMartinhoFernandes Lounge __builtin_constant_p, , , , , .

:

  • @Jarod42 . , , . , , constexpr . . constexpr, , ? constexpr , "" constexpr . "" , , , .
+4
1

:

constexpr int doit(int input, bool inconst = false) {
   return inconst ? doitconsty(input) : doitfast(input);
}

doit constexpr, - ,

constexpr int f(int n, bool inconst = false) {
   /* ... */
   int importantInt = doit(n / 42, inconst);
   /* ... */
   return magicResult;
}

constexpr , , . inconst

enum foo { bar = f(256, true) }

, f,

int main() { std::cout << "test-case: " << f(256); }

, , boolean . -, ( , int bool, ).

template<typename T>
struct maybe_const_value {
   T t;
   bool isconst;
};

enum foo { bar = maybe_const_value{256, true} % magicTransform }; 

int main() { return maybe_const_value{265} % magicTransform; }

input.isconst input.t .

+4

Source: https://habr.com/ru/post/1669244/


All Articles