Cache friendly matrix shift function
<p>I want to shift the first row of a 2D square matrix to the last row. So if I have a matrix like A, I want to get B.</p>

<p><a href="https://i.stack.imgur.com/4YQBS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4YQBS.png" alt="visual of process"></a></p>

<p>I can do this using two simple for loops. E.g.</p>

<pre><code>void shift(int M, int N, int A[M][N]){
    int i, j,temp;
    for (i = 1; i &lt; M; i++){
        for (j = 0; j &lt; N; j++){
            temp=A[i][j];
            A[i][j]=A[i-1][j];
            A[i-1][j]=temp;
        }
    }
}
</code></pre>

<p>But I want to get as few cache misses as possible. Any tip on how to do that?</p>