This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Enabling loop unrolling in small sections of code.
- From: me22 <me22 dot ca at gmail dot com>
- To: "Jason Cipriani" <jason dot cipriani at gmail dot com>
- Cc: GCC-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 6 May 2008 00:23:39 -0400
- Subject: Re: Enabling loop unrolling in small sections of code.
- References: <c09049bf0805052105y5371e2ddqceb88ed5c2968be6@mail.gmail.com>
On Tue, May 6, 2008 at 12:05 AM, Jason Cipriani
<jason.cipriani@gmail.com> wrote:
>
> 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?
>
None of these are nice and "have GCC do it for me", but they might help:
1) Try using std::transform with std::times instead of the explicit
loop. You might get lucky and find out that it's optimized in some
funky way. Low probability of return, but low effort.
2) Use templates to unroll it.
template <typename T, unsigned N>
struct multiply_helper {
static void go(T *a, T const *b) {
*a *= *b;
multiply_helper<T,N-1>::go(a+1,b+1);
}
};
template <typename T>
struct multiply_helper<T,0> {
static void go(T *, T const *) {
}
}
template <int N> class A {
double data[N];
public:
void Multiply (const A<N> &a) {
multiply_helper<double, N>::go(data, a.data);
}
};
Or something like that.
HTH,
~ Scott