how to ensure loop unrolling with template programming

Frank Winter frank.winter@desy.de
Fri Jul 9 09:33:00 GMT 2010


Is there a way to make sure, that the compiler does loop unrolling
quantized by a template paramter? The standard says that the compiler 
"may" unroll the loop. How does G++ behave here?

A tricky thing in my case might be, that the loop to be unrolled contains 
a function call. There is no way around that at the moment. The function 
is declared inline tough.


template<typename T>
inline
void part( const T & a )
{
 	// some code
}


template<typename T, int n>
void func( const T & a )
{
 	for ( int i = 0 ; i < n ; ++i )   // loop to unroll
 	  part(a);
}


Function part() should be inlined n times so that after the loop unrolling 
the compiler can do optimizations on the temporaries created in part() and 
interleaf code.

How can I make sure the compiler unrolls the loop and inlines all 
function calls? Do I have to use the preprocessor in my case?


Frank Winter



More information about the Gcc-help mailing list