volatile pointer to volatile data

316 Views Asked by At

I am trying to evaluate different version of my program to see how fast they are. I found out a weird thing in Visual Studio 2010 (using windows 10). I have not tried it yet on different compilers or systems. See the code below:

uint64 t=0x45484856167;
int k=0;
(volatile) uint64* (volatile) source=new uint64[2048];
source[0]=0x74451523156321;
for (int k=1;k<1024;k++) {source[k]=(source[k-1]<<14)+713*k}//initilisation
uint64 start = Gettime();
//main loop
for (uint64 i = 0; i < 900000000;i+=1) {
    const uint64 mask=source[k+1];
    ((t&mask)==(source[k]&mask))? k=(k+2)&2047 : k=(k-2)&2047 ;
}
//main loop end
uint64 end = Gettime();

Now, when both volatiles were used, it has the same running time as in the case where none was used. Same happened when just the second volatile is used. But when only the first one is used, it slows down. However, deleting the initialization loop or putting some more code between the initialization and the for second for loop, removes this phenomenon and any combination of volatiles have the same running time. I get why those two modifications might cause speeding up the main loop in case of volatile data, but why is the volatile * volatile faster than volatile *?

0

There are 0 best solutions below