Randi J. Relander wrote
Mattias Engdeg?rd wrote:
Iâm willing to help fixing it), if it is a bug in my
code that only shows up with the VC5 optimizer. Iâm
less inclined (= not at all) to include more or less
dirty workarounds just to compensate for bugs in VC5,
but that might not be the case.
I have the RLE assembly/source output from both compilers
and will be going through the code line-by-line. It crashes
in the decompresser but the problem could just as easily
be in the compressor.
Well, I found the problem. It is in the RLE compression âADD_SEGMENTâ macro
in the âSDL_RLESurfaceâ function. The VC5 optimizer sees a âskip = 0"
statement getting executed over and over again inside a loop and happily
moves it outside the loop. Unfortunately, it also ignores the fact that
"skipâ is being used the first time through the loop before it is assigned

The following loop at the end âADD_SEGMENTâ âŚ
do {
int len = MIN(run, maxn);
ADD_COUNT(skip);
ADD_COUNT(len);
skip = 0;
memcpy(dst, srcbuf, len * bpp);
srcbuf += len * bpp;
dst += len * bpp;
run -= len;
} while(run);
gets âoptimizedâ into this âŚ
skip = 0;
do {
int len = MIN(run, maxn);
ADD_COUNT(0);
ADD_COUNT(len);
memcpy(dst, srcbuf, len * bpp);
srcbuf += len * bpp;
dst += len * bpp;
run -= len;
} while(run);
The loop is the one responsible for breaking up long opaque runs. The result
of the failed âoptimizationâ is that all opaque skip values in the RLE
buffer are set to zero. Normally, only the ones after the first one in a
long run would be. The code crashes when the âSDL_RLEBlitâ is executed on
the corrupted buffer.
While it is obviously the fault of the VC5 optimizer, it conceptually boils
down to a loop doing âone thingâ the first time through and "another thing"
the rest of the times through. Definitely not a bug in the RLE code, but
something that could be modified slightly without being considered a âdirty
workaroundâ 
My bigger worry is that there might be other VC5 optimizer issues elsewhere
in the code, possibly leading to the requirement of using either VC6 or a
cross-compiler for the binary releases. These problems are a bear to track
down and tend to leave you a little gun shy. You start questioning every
line of code that you write.
Regimental Command
Generic Armored Combat System
http://regcom.sourceforge.net