News for hackers. Stuff that matters.
Re:Why physicists still use Fortran by kazinator on Tuesday December 20, 2016 @10:34AM (#13220667)
You can also quite simply use local variables, and unroll loops manually:

   for (i ...) {
     double a0 = a[i];
     double a1 = a[i+1];
     double b0 = b[i];
     double b1 = b[1+1];
     c[i] = a0 + b0;
     c[1+1] = a1 + b1;
   }
The hand-unrolling is predicated on the knowledge that the arrays do not overlap. It is obvious to the compiler that the locals do not alias with the arrays, or with each other.

The stores to c[] could alias with a[] or b[], but that hardly matters; we assume that all the array loads and stores take place. We have done ourselves the unrolling that was hindered by the suspicion that the arrays overlap, and could do more of it.

When coding in C for tightness, it helps to pretend that locals (at least basic type scalar ones) are registers and references to arrays and structs are memory loads and stores. Then think like an assembly language programmer, somewhat. It's better to use a few more locals than a few more loads and stores. Try to load once, process with locals, store once.

There are no comments on this comment yet.