Did I used the pre-fetch instruction correctly to reduce memory latency?
Can I do better than this?
When I compile the code with -O3, g++ seems to unroll the inner loop (code at godbolt.org).
The architecture of the CPU is Broadwell.
Thanks.
Backward loop over an array and read/write elements.
Each calculation depends on the previous calculation.
#include <stdlib.h>
#include <iostream>
int main() {
    const int N = 25000000;
    float* x = reinterpret_cast<float*>(
            aligned_alloc(16, 4*N)
    );  // 0.1 GB
    x[N - 1] = 1.0f;
    // fetch last cache line of the array
    __builtin_prefetch(&x[N - 16], 0, 3);
    // Backward loop over the i^th cache line.
    for (int i = N - 16; i >= 0; i -= 16) {
        for (int j = 15; j >= 1; --j) {
            x[i + j - 1] += x[i + j];
        }
        __builtin_prefetch(&x[i - 16], 0, 3);
        x[i - 1] = x[i];
    }
    std::cout << x[0] << "\n";
    free(x);
}
