fix strict aliasing issues

Patch by Przemysław Iskra (#3805)
This commit is contained in:
Ludwig Nussel 2008-11-03 17:03:54 +00:00
parent f86f8e8ed8
commit c754d6fdfb
15 changed files with 91 additions and 108 deletions

View file

@ -234,7 +234,7 @@ ifeq ($(PLATFORM),linux)
endif endif
endif endif
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \ BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
-pipe -DUSE_ICON $(shell sdl-config --cflags) -pipe -DUSE_ICON $(shell sdl-config --cflags)
ifeq ($(USE_OPENAL),1) ifeq ($(USE_OPENAL),1)
@ -350,7 +350,7 @@ ifeq ($(PLATFORM),darwin)
BASE_CFLAGS += -mstackrealign BASE_CFLAGS += -mstackrealign
endif endif
BASE_CFLAGS += -fno-strict-aliasing -DMACOS_X -fno-common -pipe BASE_CFLAGS += -DMACOS_X -fno-common -pipe
ifeq ($(USE_OPENAL),1) ifeq ($(USE_OPENAL),1)
BASE_CFLAGS += -DUSE_OPENAL BASE_CFLAGS += -DUSE_OPENAL
@ -421,7 +421,7 @@ ifeq ($(PLATFORM),mingw32)
ARCH=x86 ARCH=x86
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \ BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
-DUSE_ICON -DUSE_ICON
# In the absence of wspiapi.h, require Windows XP or later # In the absence of wspiapi.h, require Windows XP or later
@ -514,7 +514,7 @@ ifeq ($(PLATFORM),freebsd)
endif #alpha test endif #alpha test
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \ BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
-DUSE_ICON $(shell sdl-config --cflags) -DUSE_ICON $(shell sdl-config --cflags)
ifeq ($(USE_OPENAL),1) ifeq ($(USE_OPENAL),1)
@ -580,7 +580,7 @@ ifeq ($(PLATFORM),openbsd)
ARCH=i386 ARCH=i386
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \ BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
-DUSE_ICON $(shell sdl-config --cflags) -DUSE_ICON $(shell sdl-config --cflags)
ifeq ($(USE_OPENAL),1) ifeq ($(USE_OPENAL),1)
@ -642,7 +642,7 @@ ifeq ($(PLATFORM),netbsd)
SHLIBLDFLAGS=-shared $(LDFLAGS) SHLIBLDFLAGS=-shared $(LDFLAGS)
THREAD_LIBS=-lpthread THREAD_LIBS=-lpthread
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes
ifneq ($(ARCH),i386) ifneq ($(ARCH),i386)
BASE_CFLAGS += -DNO_VM_COMPILED BASE_CFLAGS += -DNO_VM_COMPILED
@ -706,7 +706,7 @@ ifeq ($(PLATFORM),sunos)
endif endif
BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \ BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
-pipe -DUSE_ICON $(shell sdl-config --cflags) -pipe -DUSE_ICON $(shell sdl-config --cflags)
OPTIMIZE = -O3 -ffast-math -funroll-loops OPTIMIZE = -O3 -ffast-math -funroll-loops
@ -1018,7 +1018,7 @@ makedirs:
# QVM BUILD TOOLS # QVM BUILD TOOLS
############################################################################# #############################################################################
TOOLS_OPTIMIZE = -g -O2 -Wall -fno-strict-aliasing TOOLS_OPTIMIZE = -g -O2 -Wall
TOOLS_CFLAGS = $(TOOLS_OPTIMIZE) \ TOOLS_CFLAGS = $(TOOLS_OPTIMIZE) \
-DTEMPDIR=\"$(TEMPDIR)\" -DSYSTEM=\"\" \ -DTEMPDIR=\"$(TEMPDIR)\" -DSYSTEM=\"\" \
-I$(Q3LCCSRCDIR) \ -I$(Q3LCCSRCDIR) \

View file

@ -37,9 +37,9 @@ void dllEntry( intptr_t (QDECL *syscallptr)( intptr_t arg,... ) ) {
int PASSFLOAT( float x ) { int PASSFLOAT( float x ) {
float floatTemp; floatint_t fi;
floatTemp = x; fi.f = x;
return *(int *)&floatTemp; return fi.i;
} }
void trap_Print( const char *fmt ) { void trap_Print( const char *fmt ) {

View file

@ -400,11 +400,9 @@ void CL_ShutdownCGame( void ) {
} }
static int FloatAsInt( float f ) { static int FloatAsInt( float f ) {
int temp; floatint_t fi;
fi.f = f;
*(float *)&temp = f; return fi.i;
return temp;
} }
/* /*

View file

@ -691,11 +691,9 @@ FloatAsInt
==================== ====================
*/ */
static int FloatAsInt( float f ) { static int FloatAsInt( float f ) {
int temp; floatint_t fi;
fi.f = f;
*(float *)&temp = f; return fi.i;
return temp;
} }
/* /*

View file

@ -36,9 +36,9 @@ void dllEntry( intptr_t (QDECL *syscallptr)( intptr_t arg,... ) ) {
} }
int PASSFLOAT( float x ) { int PASSFLOAT( float x ) {
float floatTemp; floatint_t fi;
floatTemp = x; fi.f = x;
return *(int *)&floatTemp; return fi.i;
} }
void trap_Printf( const char *fmt ) { void trap_Printf( const char *fmt ) {
@ -290,9 +290,9 @@ void trap_AAS_PresenceTypeBoundingBox(int presencetype, vec3_t mins, vec3_t maxs
} }
float trap_AAS_Time(void) { float trap_AAS_Time(void) {
int temp; floatint_t fi;
temp = syscall( BOTLIB_AAS_TIME ); fi.i = syscall( BOTLIB_AAS_TIME );
return (*(float*)&temp); return fi.f;
} }
int trap_AAS_PointAreaNum(vec3_t point) { int trap_AAS_PointAreaNum(vec3_t point) {
@ -476,15 +476,15 @@ void trap_BotFreeCharacter(int character) {
} }
float trap_Characteristic_Float(int character, int index) { float trap_Characteristic_Float(int character, int index) {
int temp; floatint_t fi;
temp = syscall( BOTLIB_AI_CHARACTERISTIC_FLOAT, character, index ); fi.i = syscall( BOTLIB_AI_CHARACTERISTIC_FLOAT, character, index );
return (*(float*)&temp); return fi.f;
} }
float trap_Characteristic_BFloat(int character, int index, float min, float max) { float trap_Characteristic_BFloat(int character, int index, float min, float max) {
int temp; floatint_t fi;
temp = syscall( BOTLIB_AI_CHARACTERISTIC_BFLOAT, character, index, PASSFLOAT(min), PASSFLOAT(max) ); fi.i = syscall( BOTLIB_AI_CHARACTERISTIC_BFLOAT, character, index, PASSFLOAT(min), PASSFLOAT(max) );
return (*(float*)&temp); return fi.f;
} }
int trap_Characteristic_Integer(int character, int index) { int trap_Characteristic_Integer(int character, int index) {
@ -652,9 +652,9 @@ int trap_BotGetMapLocationGoal(char *name, void /* struct bot_goal_s */ *goal) {
} }
float trap_BotAvoidGoalTime(int goalstate, int number) { float trap_BotAvoidGoalTime(int goalstate, int number) {
int temp; floatint_t fi;
temp = syscall( BOTLIB_AI_AVOID_GOAL_TIME, goalstate, number ); fi.i = syscall( BOTLIB_AI_AVOID_GOAL_TIME, goalstate, number );
return (*(float*)&temp); return fi.f;
} }
void trap_BotSetAvoidGoalTime(int goalstate, int number, float avoidtime) { void trap_BotSetAvoidGoalTime(int goalstate, int number, float avoidtime) {

View file

@ -131,10 +131,7 @@ SquareRootFloat
================ ================
*/ */
float SquareRootFloat(float number) { float SquareRootFloat(float number) {
union { floatint_t t;
float f;
int i;
} t;
float x, y; float x, y;
const float f = 1.5F; const float f = 1.5F;

View file

@ -291,13 +291,9 @@ void MSG_WriteLong( msg_t *sb, int c ) {
} }
void MSG_WriteFloat( msg_t *sb, float f ) { void MSG_WriteFloat( msg_t *sb, float f ) {
union { floatint_t dat;
float f;
int l;
} dat;
dat.f = f; dat.f = f;
MSG_WriteBits( sb, dat.l, 32 ); MSG_WriteBits( sb, dat.i, 32 );
} }
void MSG_WriteString( msg_t *sb, const char *s ) { void MSG_WriteString( msg_t *sb, const char *s ) {
@ -423,13 +419,9 @@ int MSG_ReadLong( msg_t *msg ) {
} }
float MSG_ReadFloat( msg_t *msg ) { float MSG_ReadFloat( msg_t *msg ) {
union { floatint_t dat;
byte b[4];
float f;
int l;
} dat;
dat.l = MSG_ReadBits( msg, 32 ); dat.i = MSG_ReadBits( msg, 32 );
if ( msg->readcount > msg->cursize ) { if ( msg->readcount > msg->cursize ) {
dat.f = -1; dat.f = -1;
} }
@ -563,20 +555,22 @@ int MSG_ReadDelta( msg_t *msg, int oldV, int bits ) {
} }
void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV ) { void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV ) {
floatint_t fi;
if ( oldV == newV ) { if ( oldV == newV ) {
MSG_WriteBits( msg, 0, 1 ); MSG_WriteBits( msg, 0, 1 );
return; return;
} }
fi.f = newV;
MSG_WriteBits( msg, 1, 1 ); MSG_WriteBits( msg, 1, 1 );
MSG_WriteBits( msg, *(int *)&newV, 32 ); MSG_WriteBits( msg, fi.i, 32 );
} }
float MSG_ReadDeltaFloat( msg_t *msg, float oldV ) { float MSG_ReadDeltaFloat( msg_t *msg, float oldV ) {
if ( MSG_ReadBits( msg, 1 ) ) { if ( MSG_ReadBits( msg, 1 ) ) {
float newV; floatint_t fi;
*(int *)&newV = MSG_ReadBits( msg, 32 ); fi.i = MSG_ReadBits( msg, 32 );
return newV; return fi.f;
} }
return oldV; return oldV;
} }
@ -617,20 +611,22 @@ int MSG_ReadDeltaKey( msg_t *msg, int key, int oldV, int bits ) {
} }
void MSG_WriteDeltaKeyFloat( msg_t *msg, int key, float oldV, float newV ) { void MSG_WriteDeltaKeyFloat( msg_t *msg, int key, float oldV, float newV ) {
floatint_t fi;
if ( oldV == newV ) { if ( oldV == newV ) {
MSG_WriteBits( msg, 0, 1 ); MSG_WriteBits( msg, 0, 1 );
return; return;
} }
fi.f = newV;
MSG_WriteBits( msg, 1, 1 ); MSG_WriteBits( msg, 1, 1 );
MSG_WriteBits( msg, (*(int *)&newV) ^ key, 32 ); MSG_WriteBits( msg, fi.i ^ key, 32 );
} }
float MSG_ReadDeltaKeyFloat( msg_t *msg, int key, float oldV ) { float MSG_ReadDeltaKeyFloat( msg_t *msg, int key, float oldV ) {
if ( MSG_ReadBits( msg, 1 ) ) { if ( MSG_ReadBits( msg, 1 ) ) {
float newV; floatint_t fi;
*(int *)&newV = MSG_ReadBits( msg, 32 ) ^ key; fi.i = MSG_ReadBits( msg, 32 ) ^ key;
return newV; return fi.f;
} }
return oldV; return oldV;
} }

View file

@ -501,10 +501,7 @@ void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
*/ */
float Q_rsqrt( float number ) float Q_rsqrt( float number )
{ {
union { floatint_t t;
float f;
int i;
} t;
float x2, y; float x2, y;
const float threehalfs = 1.5F; const float threehalfs = 1.5F;
@ -519,9 +516,10 @@ float Q_rsqrt( float number )
} }
float Q_fabs( float f ) { float Q_fabs( float f ) {
int tmp = * ( int * ) &f; floatint_t fi;
tmp &= 0x7FFFFFFF; fi.f = f;
return * ( float * ) &tmp; fi.i &= 0x7FFFFFFF;
return fi.f;
} }
#endif #endif
@ -1301,15 +1299,11 @@ Don't pass doubles to this
*/ */
int Q_isnan( float x ) int Q_isnan( float x )
{ {
union floatint_t fi;
{
float f;
unsigned int i;
} t;
t.f = x; fi.f = x;
t.i &= 0x7FFFFFFF; fi.ui &= 0x7FFFFFFF;
t.i = 0x7F800000 - t.i; fi.ui = 0x7F800000 - fi.ui;
return (int)( (unsigned int)t.i >> 31 ); return (int)( (unsigned int)fi.ui >> 31 );
} }

View file

@ -205,16 +205,11 @@ qint64 Long64NoSwap (qint64 ll)
return ll; return ll;
} }
typedef union {
float f;
unsigned int i;
} _FloatByteUnion;
float FloatSwap (const float *f) { float FloatSwap (const float *f) {
_FloatByteUnion out; floatint_t out;
out.f = *f; out.f = *f;
out.i = LongSwap(out.i); out.ui = LongSwap(out.ui);
return out.f; return out.f;
} }

View file

@ -151,6 +151,12 @@ typedef unsigned char byte;
typedef enum {qfalse, qtrue} qboolean; typedef enum {qfalse, qtrue} qboolean;
typedef union {
float f;
int i;
unsigned int ui;
} floatint_t;
typedef int qhandle_t; typedef int qhandle_t;
typedef int sfxHandle_t; typedef int sfxHandle_t;
typedef int fileHandle_t; typedef int fileHandle_t;

View file

@ -356,12 +356,9 @@ void *VM_ExplicitArgPtr( vm_t *vm, intptr_t intValue );
#define VMA(x) VM_ArgPtr(args[x]) #define VMA(x) VM_ArgPtr(args[x])
static ID_INLINE float _vmf(intptr_t x) static ID_INLINE float _vmf(intptr_t x)
{ {
union { floatint_t fi;
int i; fi.i = (int) x;
float f; return fi.f;
} t;
t.i = (int)x;
return t.f;
} }
#define VMF(x) _vmf(args[x]) #define VMF(x) _vmf(args[x])

View file

@ -288,14 +288,9 @@ void SV_GetUsercmd( int clientNum, usercmd_t *cmd ) {
//============================================== //==============================================
static int FloatAsInt( float f ) { static int FloatAsInt( float f ) {
union floatint_t fi;
{ fi.f = f;
int i; return fi.i;
float f;
} temp;
temp.f = f;
return temp.i;
} }
/* /*

View file

@ -40,8 +40,9 @@ static void I(defconst)(int suffix, int size, Value v) {
case P: print("byte %d %U\n", size, (unsigned long)v.p); return; case P: print("byte %d %U\n", size, (unsigned long)v.p); return;
case F: case F:
if (size == 4) { if (size == 4) {
float f = v.d; floatint_t fi;
print("byte 4 %u\n", *(unsigned *)&f); fi.f = v.d;
print("byte 4 %u\n", fi.ui);
} else { } else {
unsigned *p = (unsigned *)&v.d; unsigned *p = (unsigned *)&v.d;
print("byte 4 %u\n", p[swap]); print("byte 4 %u\n", p[swap]);
@ -67,10 +68,10 @@ static void I(defsymbol)(Symbol p) {
case P: p->x.name = stringf("%U", p->u.c.v.p); break; case P: p->x.name = stringf("%U", p->u.c.v.p); break;
case F: case F:
{ // JDC: added this to get inline floats { // JDC: added this to get inline floats
unsigned temp; floatint_t temp;
*(float *)&temp = p->u.c.v.d; temp.f = p->u.c.v.d;
p->x.name = stringf("%U", temp ); p->x.name = stringf("%U", temp.ui );
} }
break;// JDC: added this break;// JDC: added this
default: assert(0); default: assert(0);

View file

@ -98,6 +98,12 @@ typedef struct {
void *xt; void *xt;
} Xtype; } Xtype;
typedef union {
float f;
int i;
unsigned int ui;
} floatint_t;
#include "config.h" #include "config.h"
typedef struct metrics { typedef struct metrics {
unsigned char size, align, outofline; unsigned char size, align, outofline;

View file

@ -35,9 +35,9 @@ void dllEntry( intptr_t (QDECL *syscallptr)( intptr_t arg,... ) ) {
} }
int PASSFLOAT( float x ) { int PASSFLOAT( float x ) {
float floatTemp; floatint_t fi;
floatTemp = x; fi.f = x;
return *(int *)&floatTemp; return fi.i;
} }
void trap_Print( const char *string ) { void trap_Print( const char *string ) {
@ -65,9 +65,9 @@ void trap_Cvar_Set( const char *var_name, const char *value ) {
} }
float trap_Cvar_VariableValue( const char *var_name ) { float trap_Cvar_VariableValue( const char *var_name ) {
int temp; floatint_t fi;
temp = syscall( UI_CVAR_VARIABLEVALUE, var_name ); fi.i = syscall( UI_CVAR_VARIABLEVALUE, var_name );
return (*(float*)&temp); return fi.f;
} }
void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ) { void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ) {