const long DIM = 1024*1024*1024*1024*4;
Those are all int (32 bit) literals so it is prolly wrapping around the intermediates before it actually assigns to DIM.
If you used 1024L it should be better, by making the right hand side 64 bit too.
It is similar tofloat x = 1 / 2; // x == 0 because the right hand side is done as ints
float x = 1.0 / 2; // now x == 0.5