This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Enabling loop unrolling in small sections of code.


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]