I was reading some code in C++ and I read the following:
CACHELINE = 64;
...
/* allocate the three matrices and align to cache lines */
a = (double *)malloc(nmax*nmax*sizeof(double)+CACHELINE);
b = (double *)malloc(nmax*nmax*sizeof(double)+CACHELINE);
c = (double *)malloc(nmax*nmax*sizeof(double)+CACHELINE);
a = (double *)(((unsigned long)a+CACHELINE)&~(CACHELINE-1));
b = (double *)(((unsigned long)b+CACHELINE)&~(CACHELINE-1));
c = (double *)(((unsigned long)c+CACHELINE)&~(CACHELINE-1));
Why does this code create matrices which are aligned with cache lines? I especially do not understand what this instruction does:
a = (double *)(((unsigned long)a+CACHELINE)&~(CACHELINE-1));
Thank you!