Enabling loop unrolling in small sections of code.
Jason Cipriani
jason.cipriani@gmail.com
Tue May 6 04:24:00 GMT 2008
Is it possible to enable -funroll-loops only for small sections of
code, maybe with some #pragma or something? I have a situation where I
have a template class similar to this defined in a header:
template <int N> class A {
double data[N];
public:
void Multiply (const A<N> &a) {
for (int i = 0; i < N; ++ i)
data[i] *= a.data[i];
}
};
The values of N are small (between 2 and 20, say) and benchmarks show
the code does seem to benefit from loop unrolling. However, that
template header is #included by many source files where loop unrolling
is not appropriate; so I can't just enable it everywhere. I want to
keep the Multiply function inline so I don't want to put it in a
separate source file compiled with -funroll-loops and explicitly
instantiate all the A<N>'s. I also don't really feel like explicitly
specializing 19 copies of this function, and in my real application
there are many such member functions.
So without using -funroll-loops on every file, explicit instantiation
(to put code in its own source file), or explicit specialization (to
unroll loops by hand), can I do what I want to do here? That is, have
GCC automagically unroll these loops for me without affecting anything
else in any of the source files that #includes this template
definition?
Thanks,
Jason
More information about the Gcc-help
mailing list