Ability to force the compiler to throw out the for loop

To improve runtime performance, is there any way to force the compiler to unwrap such a for loop with a known number of compiler iterations?
So this code.

template <int dim>
void foo()
{
   ...
   int temp[i];
   for(int i=0; i<dim; i++)
   {
      temp[i] = ...;
      ...
   }
   ...
}

      

created the way it would be

template <int dim>
void foo()
{
   ...
   int temp[i];

      temp[0] = ...;
      temp[1] = ...;
      temp[2] = ...;
      (...)
      temp[dim-1] = ...;
      ...
   }
   ...
}

      

+3


source to share


1 answer


While I don't understand why you didn't let the compiler determine if deployment was a performance benefit (which is often not the case), you can be compiler independent to use variadic templates with a package extension, 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 can use Pragmas. This answer describes how to do it for GCC.

+5


source







All Articles