Fixed: GCC misoptimized the SWAP macros in farchive.cpp.

SVN r33 (trunk)
This commit is contained in:
Randy Heit 2006-04-12 05:30:27 +00:00
parent 63adffee3d
commit 78a367a2d7
2 changed files with 44 additions and 46 deletions

View file

@ -1,6 +1,6 @@
April 11, 2006 April 11, 2006
- Fixed: Compiling with mingw once again works, although savegame loading - Fixed: GCC misoptimized the SWAP macros in farchive.cpp.
problems are not yet fixed. - Fixed: Compiling with mingw once again works.
- Fixed: ACS improperly calculated the address of local variables when - Fixed: ACS improperly calculated the address of local variables when
returning from one function to another function when the function that returning from one function to another function when the function that
was called was used as part of an expression. was called was used as part of an expression.

View file

@ -80,22 +80,30 @@
#define OLD_SPRITE ((BYTE)12) // Reference to an old sprite name follows #define OLD_SPRITE ((BYTE)12) // Reference to an old sprite name follows
#ifdef WORDS_BIGENDIAN #ifdef WORDS_BIGENDIAN
#define SWAP_WORD(x) static inline WORD SWAP_WORD(x) { return x; }
#define SWAP_DWORD(x) static inline DWORD SWAP_DWORD(x) { return x; }
#define SWAP_QWORD(x) static inline QWORD SWAP_QWORD(x) { return x; }
#define SWAP_FLOAT(x) static inline float SWAP_FLOAT(x) { return x; }
#define SWAP_DOUBLE(x) static inline double SWAP_DOUBLE(x) { return x; }
#else #else
#define SWAP_WORD(x) { x = (((x)<<8) | ((x)>>8)); } #ifdef _MSC_VER
#define SWAP_DWORD(x) { x = (((x)>>24) | (((x)>>8)&0xff00) | ((x)<<8)&0xff0000 | ((x)<<24)); } static inline WORD SWAP_WORD(WORD x) { return _byteswap_ushort(x); }
#if 0 static inline DWORD SWAP_DWORD(DWORD x) { return _byteswap_ulong(x); }
#define SWAP_QWORD(x) { x = (((x)>>56) | (((x)>>40)&(0xff<<8)) | (((x)>>24)&(0xff<<16)) | (((x)>>8)&(0xff<<24)) |\ static inline QWORD SWAP_QWORD(QWORD x) { return _byteswap_uint64(x); }
(((x)<<8)&(QWORD)0xff00000000) | (((x)<<24)&(QWORD)0xff0000000000) | (((x)<<40)&(QWORD)0xff000000000000) | ((x)<<56))); }
#else #else
#define SWAP_QWORD(x) { DWORD *y = (DWORD *)&x; DWORD t=y[0]; y[0]=y[1]; y[1]=t; SWAP_DWORD(y[0]); SWAP_DWORD(y[1]); } static inline WORD SWAP_WORD(WORD x) { return (((x)<<8) | ((x)>>8)); }
static inline DWORD SWAP_DWORD(DWORD x) { return x = (((x)>>24) | (((x)>>8)&0xff00) | ((x)<<8)&0xff0000 | ((x)<<24)); }
static inline QWORD SWAP_QWORD(QWORD x)
{
union { QWORD q; DWORD d[2]; } t, u;
t.q = x;
u.d[0] = SWAP_DWORD(t.d[1]);
u.d[1] = SWAP_DWORD(t.d[0]);
return u.q;
}
#endif #endif
#define SWAP_FLOAT(x) { DWORD dw = *(DWORD *)&x; SWAP_DWORD(dw); x = *(float *)&dw; } static inline float SWAP_FLOAT(float x) { DWORD t = *(DWORD *)&x; t = SWAP_DWORD(t); return *(float *)&t; }
#define SWAP_DOUBLE(x) { QWORD qw = *(QWORD *)&x; SWAP_QWORD(qw); x = *(double *)&qw; } static inline double SWAP_DOUBLE(double x) { QWORD t = *(QWORD *)&x; t = SWAP_QWORD(t); return *(double *)&t; }
#endif #endif
// Output buffer size for compression; need some extra space. // Output buffer size for compression; need some extra space.
@ -175,13 +183,13 @@ void FCompressedFile::PostOpen ()
{ {
DWORD sizes[2]; DWORD sizes[2];
fread (sizes, sizeof(DWORD), 2, m_File); fread (sizes, sizeof(DWORD), 2, m_File);
SWAP_DWORD (sizes[0]); sizes[0] = SWAP_DWORD (sizes[0]);
SWAP_DWORD (sizes[1]); sizes[1] = SWAP_DWORD (sizes[1]);
unsigned int len = sizes[0] == 0 ? sizes[1] : sizes[0]; unsigned int len = sizes[0] == 0 ? sizes[1] : sizes[0];
m_Buffer = (byte *)Malloc (len+8); m_Buffer = (byte *)Malloc (len+8);
fread (m_Buffer+8, len, 1, m_File); fread (m_Buffer+8, len, 1, m_File);
SWAP_DWORD (sizes[0]); sizes[0] = SWAP_DWORD (sizes[0]);
SWAP_DWORD (sizes[1]); sizes[1] = SWAP_DWORD (sizes[1]);
((DWORD *)m_Buffer)[0] = sizes[0]; ((DWORD *)m_Buffer)[0] = sizes[0];
((DWORD *)m_Buffer)[1] = sizes[1]; ((DWORD *)m_Buffer)[1] = sizes[1];
Explode (); Explode ();
@ -483,10 +491,8 @@ void FCompressedMemFile::Serialize (FArchive &arc)
arc.Write (ZSig, 4); arc.Write (ZSig, 4);
DWORD sizes[2]; DWORD sizes[2];
sizes[0] = ((DWORD *)m_ImplodedBuffer)[0]; sizes[0] = SWAP_DWORD (((DWORD *)m_ImplodedBuffer)[0]);
sizes[1] = ((DWORD *)m_ImplodedBuffer)[1]; sizes[1] = SWAP_DWORD (((DWORD *)m_ImplodedBuffer)[1]);
SWAP_DWORD (sizes[0]);
SWAP_DWORD (sizes[1]);
arc.Write (m_ImplodedBuffer, (sizes[0] ? sizes[0] : sizes[1])+8); arc.Write (m_ImplodedBuffer, (sizes[0] ? sizes[0] : sizes[1])+8);
} }
else else
@ -506,10 +512,8 @@ void FCompressedMemFile::Serialize (FArchive &arc)
DWORD len = sizes[0] == 0 ? sizes[1] : sizes[0]; DWORD len = sizes[0] == 0 ? sizes[1] : sizes[0];
m_Buffer = (BYTE *)Malloc (len+8); m_Buffer = (BYTE *)Malloc (len+8);
SWAP_DWORD (sizes[0]); ((DWORD *)m_Buffer)[0] = SWAP_DWORD(sizes[0]);
SWAP_DWORD (sizes[1]); ((DWORD *)m_Buffer)[1] = SWAP_DWORD(sizes[1]);
((DWORD *)m_Buffer)[0] = sizes[0];
((DWORD *)m_Buffer)[1] = sizes[1];
arc.Read (m_Buffer+8, len); arc.Read (m_Buffer+8, len);
m_ImplodedBuffer = m_Buffer; m_ImplodedBuffer = m_Buffer;
m_Buffer = NULL; m_Buffer = NULL;
@ -554,7 +558,7 @@ void FPNGChunkFile::Close ()
data[1] = m_ChunkID; data[1] = m_ChunkID;
fwrite (data, 8, 1, m_File); fwrite (data, 8, 1, m_File);
fwrite (m_Buffer, m_BufferSize, 1, m_File); fwrite (m_Buffer, m_BufferSize, 1, m_File);
SWAP_DWORD (crc); crc = SWAP_DWORD (crc);
fwrite (&crc, 4, 1, m_File); fwrite (&crc, 4, 1, m_File);
} }
m_File = NULL; m_File = NULL;
@ -823,14 +827,13 @@ FArchive &FArchive::operator<< (WORD &w)
{ {
if (m_Storing) if (m_Storing)
{ {
WORD temp = w; WORD temp = SWAP_WORD(w);
SWAP_WORD(temp);
Write (&temp, sizeof(WORD)); Write (&temp, sizeof(WORD));
} }
else else
{ {
Read (&w, sizeof(WORD)); Read (&w, sizeof(WORD));
SWAP_WORD(w); w = SWAP_WORD(w);
} }
return *this; return *this;
} }
@ -839,14 +842,13 @@ FArchive &FArchive::operator<< (DWORD &w)
{ {
if (m_Storing) if (m_Storing)
{ {
DWORD temp = w; DWORD temp = SWAP_DWORD(w);
SWAP_DWORD(temp);
Write (&temp, sizeof(DWORD)); Write (&temp, sizeof(DWORD));
} }
else else
{ {
Read (&w, sizeof(DWORD)); Read (&w, sizeof(DWORD));
SWAP_DWORD(w); w = SWAP_DWORD(w);
} }
return *this; return *this;
} }
@ -855,14 +857,13 @@ FArchive &FArchive::operator<< (QWORD &w)
{ {
if (m_Storing) if (m_Storing)
{ {
QWORD temp = w; QWORD temp = SWAP_QWORD(w);
SWAP_QWORD(temp);
Write (&temp, sizeof(QWORD)); Write (&temp, sizeof(QWORD));
} }
else else
{ {
Read (&w, sizeof(QWORD)); Read (&w, sizeof(QWORD));
SWAP_QWORD(w); w = SWAP_QWORD(w);
} }
return *this; return *this;
} }
@ -871,14 +872,13 @@ FArchive &FArchive::operator<< (float &w)
{ {
if (m_Storing) if (m_Storing)
{ {
float temp = w; float temp = SWAP_FLOAT(w);
SWAP_FLOAT(temp);
Write (&temp, sizeof(float)); Write (&temp, sizeof(float));
} }
else else
{ {
Read (&w, sizeof(float)); Read (&w, sizeof(float));
SWAP_FLOAT(w); w = SWAP_FLOAT(w);
} }
return *this; return *this;
} }
@ -887,14 +887,13 @@ FArchive &FArchive::operator<< (double &w)
{ {
if (m_Storing) if (m_Storing)
{ {
double temp = w; double temp = SWAP_DOUBLE(w);
SWAP_DOUBLE(temp);
Write (&temp, sizeof(double)); Write (&temp, sizeof(double));
} }
else else
{ {
Read (&w, sizeof(double)); Read (&w, sizeof(double));
SWAP_DOUBLE(w); w = SWAP_DOUBLE(w);
} }
return *this; return *this;
} }
@ -907,8 +906,7 @@ FArchive &FArchive::SerializePointer (void *ptrbase, BYTE **ptr, DWORD elemSize)
{ {
if (*ptr) if (*ptr)
{ {
w = (*ptr - (byte *)ptrbase) / elemSize; w = SWAP_WORD((*ptr - (byte *)ptrbase) / elemSize);
SWAP_WORD(w);
} }
else else
{ {
@ -919,7 +917,7 @@ FArchive &FArchive::SerializePointer (void *ptrbase, BYTE **ptr, DWORD elemSize)
else else
{ {
Read (&w, sizeof(WORD)); Read (&w, sizeof(WORD));
SWAP_WORD (w); w = SWAP_WORD (w);
if (w != 0xffff) if (w != 0xffff)
{ {
*ptr = (byte *)ptrbase + w * elemSize; *ptr = (byte *)ptrbase + w * elemSize;