3

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] = ...;
      ...
   }
   ...
}
Matthias
  • 908
  • 1
  • 7
  • 22

1 Answers1

5

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.

Community
  • 1
  • 1
Columbo
  • 60,038
  • 8
  • 155
  • 203