In order to increase performance at runtime is there a possibility to force the compiler to role out such a for loop with compiler known count of iterations?
Such that this code.
template <int dim>
void foo()
{
...
int temp[i];
for(int i=0; i<dim; i++)
{
temp[i] = ...;
...
}
...
}
is build as it would be
template <int dim>
void foo()
{
...
int temp[i];
temp[0] = ...;
temp[1] = ...;
temp[2] = ...;
(...)
temp[dim-1] = ...;
...
}
...
}
Although I don't see why you wouldn't let the compiler decice whether unrolling is beneficial to the performance (which it often isn't), you can, to be compiler-independent, use variadic templates with pack-expansions. E.g. in C++14:
namespace detail
{
template <std::size_t... i>
void foo( std::index_sequence<i...> )
{
int temp[sizeof...(i)];
(void)std::initializer_list<int>{ (temp[i] = i)... };
}
}
template <std::size_t dim>
void foo()
{
detail::foo( std::make_index_sequence<dim>() );
}
Demo. Otherwise you could use Pragmas. This answer describes how to do that for GCC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With