Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possibility to force compiler roll out a for loop

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] = ...;
      ...
   }
   ...
}
like image 353
Matthias Avatar asked Nov 19 '25 03:11

Matthias


1 Answers

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.

like image 67
Columbo Avatar answered Nov 20 '25 16:11

Columbo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!