r1481 - in trunk: . code/cgame code/client code/game code/qcommon code/server code/tools/lcc/src code/ui

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Mon Nov 3 12:03:54 EST 2008


Author: ludwig
Date: 2008-11-03 12:03:54 -0500 (Mon, 03 Nov 2008)
New Revision: 1481

Modified:
   trunk/Makefile
   trunk/code/cgame/cg_syscalls.c
   trunk/code/client/cl_cgame.c
   trunk/code/client/cl_ui.c
   trunk/code/game/g_syscalls.c
   trunk/code/qcommon/cm_trace.c
   trunk/code/qcommon/msg.c
   trunk/code/qcommon/q_math.c
   trunk/code/qcommon/q_shared.c
   trunk/code/qcommon/q_shared.h
   trunk/code/qcommon/qcommon.h
   trunk/code/server/sv_game.c
   trunk/code/tools/lcc/src/bytecode.c
   trunk/code/tools/lcc/src/c.h
   trunk/code/ui/ui_syscalls.c
Log:
fix strict aliasing issues

Patch by Przemys?\197?\130aw Iskra (#3805)

Modified: trunk/Makefile
===================================================================
--- trunk/Makefile	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/Makefile	2008-11-03 17:03:54 UTC (rev 1481)
@@ -234,7 +234,7 @@
   endif
   endif
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
     -pipe -DUSE_ICON $(shell sdl-config --cflags)
 
   ifeq ($(USE_OPENAL),1)
@@ -350,7 +350,7 @@
     BASE_CFLAGS += -mstackrealign
   endif
 
-  BASE_CFLAGS += -fno-strict-aliasing -DMACOS_X -fno-common -pipe
+  BASE_CFLAGS += -DMACOS_X -fno-common -pipe
 
   ifeq ($(USE_OPENAL),1)
     BASE_CFLAGS += -DUSE_OPENAL
@@ -421,7 +421,7 @@
 
   ARCH=x86
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
     -DUSE_ICON
 
   # In the absence of wspiapi.h, require Windows XP or later
@@ -514,7 +514,7 @@
   endif #alpha test
 
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
     -DUSE_ICON $(shell sdl-config --cflags)
 
   ifeq ($(USE_OPENAL),1)
@@ -580,7 +580,7 @@
   ARCH=i386
 
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
     -DUSE_ICON $(shell sdl-config --cflags)
 
   ifeq ($(USE_OPENAL),1)
@@ -642,7 +642,7 @@
   SHLIBLDFLAGS=-shared $(LDFLAGS)
   THREAD_LIBS=-lpthread
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes
 
   ifneq ($(ARCH),i386)
     BASE_CFLAGS += -DNO_VM_COMPILED
@@ -706,7 +706,7 @@
   endif
 
 
-  BASE_CFLAGS = -Wall -fno-strict-aliasing -Wimplicit -Wstrict-prototypes \
+  BASE_CFLAGS = -Wall -Wimplicit -Wstrict-prototypes \
     -pipe -DUSE_ICON $(shell sdl-config --cflags)
 
   OPTIMIZE = -O3 -ffast-math -funroll-loops
@@ -1018,7 +1018,7 @@
 # QVM BUILD TOOLS
 #############################################################################
 
-TOOLS_OPTIMIZE = -g -O2 -Wall -fno-strict-aliasing
+TOOLS_OPTIMIZE = -g -O2 -Wall
 TOOLS_CFLAGS = $(TOOLS_OPTIMIZE) \
                -DTEMPDIR=\"$(TEMPDIR)\" -DSYSTEM=\"\" \
                -I$(Q3LCCSRCDIR) \

Modified: trunk/code/cgame/cg_syscalls.c
===================================================================
--- trunk/code/cgame/cg_syscalls.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/cgame/cg_syscalls.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -37,9 +37,9 @@
 
 
 int PASSFLOAT( float x ) {
-	float	floatTemp;
-	floatTemp = x;
-	return *(int *)&floatTemp;
+	floatint_t fi;
+	fi.f = x;
+	return fi.i;
 }
 
 void	trap_Print( const char *fmt ) {

Modified: trunk/code/client/cl_cgame.c
===================================================================
--- trunk/code/client/cl_cgame.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/client/cl_cgame.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -400,11 +400,9 @@
 }
 
 static int	FloatAsInt( float f ) {
-	int		temp;
-
-	*(float *)&temp = f;
-
-	return temp;
+	floatint_t fi;
+	fi.f = f;
+	return fi.i;
 }
 
 /*

Modified: trunk/code/client/cl_ui.c
===================================================================
--- trunk/code/client/cl_ui.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/client/cl_ui.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -691,11 +691,9 @@
 ====================
 */
 static int FloatAsInt( float f ) {
-	int		temp;
-
-	*(float *)&temp = f;
-
-	return temp;
+	floatint_t fi;
+	fi.f = f;
+	return fi.i;
 }
 
 /*

Modified: trunk/code/game/g_syscalls.c
===================================================================
--- trunk/code/game/g_syscalls.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/game/g_syscalls.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -36,9 +36,9 @@
 }
 
 int PASSFLOAT( float x ) {
-	float	floatTemp;
-	floatTemp = x;
-	return *(int *)&floatTemp;
+	floatint_t fi;
+	fi.f = x;
+	return fi.i;
 }
 
 void	trap_Printf( const char *fmt ) {
@@ -290,9 +290,9 @@
 }
 
 float trap_AAS_Time(void) {
-	int temp;
-	temp = syscall( BOTLIB_AAS_TIME );
-	return (*(float*)&temp);
+	floatint_t fi;
+	fi.i = syscall( BOTLIB_AAS_TIME );
+	return fi.f;
 }
 
 int trap_AAS_PointAreaNum(vec3_t point) {
@@ -476,15 +476,15 @@
 }
 
 float trap_Characteristic_Float(int character, int index) {
-	int temp;
-	temp = syscall( BOTLIB_AI_CHARACTERISTIC_FLOAT, character, index );
-	return (*(float*)&temp);
+	floatint_t fi;
+	fi.i = syscall( BOTLIB_AI_CHARACTERISTIC_FLOAT, character, index );
+	return fi.f;
 }
 
 float trap_Characteristic_BFloat(int character, int index, float min, float max) {
-	int temp;
-	temp = syscall( BOTLIB_AI_CHARACTERISTIC_BFLOAT, character, index, PASSFLOAT(min), PASSFLOAT(max) );
-	return (*(float*)&temp);
+	floatint_t fi;
+	fi.i = syscall( BOTLIB_AI_CHARACTERISTIC_BFLOAT, character, index, PASSFLOAT(min), PASSFLOAT(max) );
+	return fi.f;
 }
 
 int trap_Characteristic_Integer(int character, int index) {
@@ -652,9 +652,9 @@
 }
 
 float trap_BotAvoidGoalTime(int goalstate, int number) {
-	int temp;
-	temp = syscall( BOTLIB_AI_AVOID_GOAL_TIME, goalstate, number );
-	return (*(float*)&temp);
+	floatint_t fi;
+	fi.i = syscall( BOTLIB_AI_AVOID_GOAL_TIME, goalstate, number );
+	return fi.f;
 }
 
 void trap_BotSetAvoidGoalTime(int goalstate, int number, float avoidtime) {

Modified: trunk/code/qcommon/cm_trace.c
===================================================================
--- trunk/code/qcommon/cm_trace.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/cm_trace.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -131,10 +131,7 @@
 ================
 */
 float SquareRootFloat(float number) {
-	union {
-		float f;
-		int i;
-	} t;
+	floatint_t t;
 	float x, y;
 	const float f = 1.5F;
 

Modified: trunk/code/qcommon/msg.c
===================================================================
--- trunk/code/qcommon/msg.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/msg.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -291,13 +291,9 @@
 }
 
 void MSG_WriteFloat( msg_t *sb, float f ) {
-	union {
-		float	f;
-		int	l;
-	} dat;
-	
+	floatint_t dat;
 	dat.f = f;
-	MSG_WriteBits( sb, dat.l, 32 );
+	MSG_WriteBits( sb, dat.i, 32 );
 }
 
 void MSG_WriteString( msg_t *sb, const char *s ) {
@@ -423,13 +419,9 @@
 }
 
 float MSG_ReadFloat( msg_t *msg ) {
-	union {
-		byte	b[4];
-		float	f;
-		int	l;
-	} dat;
+	floatint_t dat;
 	
-	dat.l = MSG_ReadBits( msg, 32 );
+	dat.i = MSG_ReadBits( msg, 32 );
 	if ( msg->readcount > msg->cursize ) {
 		dat.f = -1;
 	}	
@@ -563,20 +555,22 @@
 }
 
 void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV ) {
+	floatint_t fi;
 	if ( oldV == newV ) {
 		MSG_WriteBits( msg, 0, 1 );
 		return;
 	}
+	fi.f = newV;
 	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 ) {
 	if ( MSG_ReadBits( msg, 1 ) ) {
-		float	newV;
+		floatint_t fi;
 
-		*(int *)&newV = MSG_ReadBits( msg, 32 );
-		return newV;
+		fi.i = MSG_ReadBits( msg, 32 );
+		return fi.f;
 	}
 	return oldV;
 }
@@ -617,20 +611,22 @@
 }
 
 void MSG_WriteDeltaKeyFloat( msg_t *msg, int key, float oldV, float newV ) {
+	floatint_t fi;
 	if ( oldV == newV ) {
 		MSG_WriteBits( msg, 0, 1 );
 		return;
 	}
+	fi.f = newV;
 	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 ) {
 	if ( MSG_ReadBits( msg, 1 ) ) {
-		float	newV;
+		floatint_t fi;
 
-		*(int *)&newV = MSG_ReadBits( msg, 32 ) ^ key;
-		return newV;
+		fi.i = MSG_ReadBits( msg, 32 ) ^ key;
+		return fi.f;
 	}
 	return oldV;
 }

Modified: trunk/code/qcommon/q_math.c
===================================================================
--- trunk/code/qcommon/q_math.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/q_math.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -501,10 +501,7 @@
 */
 float Q_rsqrt( float number )
 {
-	union {
-		float f;
-		int i;
-	} t;
+	floatint_t t;
 	float x2, y;
 	const float threehalfs = 1.5F;
 
@@ -519,9 +516,10 @@
 }
 
 float Q_fabs( float f ) {
-	int tmp = * ( int * ) &f;
-	tmp &= 0x7FFFFFFF;
-	return * ( float * ) &tmp;
+	floatint_t fi;
+	fi.f = f;
+	fi.i &= 0x7FFFFFFF;
+	return fi.f;
 }
 #endif
 
@@ -1301,15 +1299,11 @@
 */
 int Q_isnan( float x )
 {
-	union
-	{
-		float f;
-		unsigned int i;
-	} t;
+	floatint_t fi;
 
-	t.f = x;
-	t.i &= 0x7FFFFFFF;
-	t.i = 0x7F800000 - t.i;
+	fi.f = x;
+	fi.ui &= 0x7FFFFFFF;
+	fi.ui = 0x7F800000 - fi.ui;
 
-	return (int)( (unsigned int)t.i >> 31 );
+	return (int)( (unsigned int)fi.ui >> 31 );
 }

Modified: trunk/code/qcommon/q_shared.c
===================================================================
--- trunk/code/qcommon/q_shared.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/q_shared.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -205,16 +205,11 @@
 	return ll;
 }
 
-typedef union {
-    float	f;
-    unsigned int i;
-} _FloatByteUnion;
-
 float FloatSwap (const float *f) {
-	_FloatByteUnion out;
+	floatint_t out;
 
 	out.f = *f;
-	out.i = LongSwap(out.i);
+	out.ui = LongSwap(out.ui);
 
 	return out.f;
 }

Modified: trunk/code/qcommon/q_shared.h
===================================================================
--- trunk/code/qcommon/q_shared.h	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/q_shared.h	2008-11-03 17:03:54 UTC (rev 1481)
@@ -151,6 +151,12 @@
 
 typedef enum {qfalse, qtrue}	qboolean;
 
+typedef union {
+	float f;
+	int i;
+	unsigned int ui;
+} floatint_t;
+
 typedef int		qhandle_t;
 typedef int		sfxHandle_t;
 typedef int		fileHandle_t;

Modified: trunk/code/qcommon/qcommon.h
===================================================================
--- trunk/code/qcommon/qcommon.h	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/qcommon/qcommon.h	2008-11-03 17:03:54 UTC (rev 1481)
@@ -356,12 +356,9 @@
 #define	VMA(x) VM_ArgPtr(args[x])
 static ID_INLINE float _vmf(intptr_t x)
 {
-	union {
-		int i;
-		float f;
-	} t;
-	t.i = (int)x;
-	return t.f;
+	floatint_t fi;
+	fi.i = (int) x;
+	return fi.f;
 }
 #define	VMF(x)	_vmf(args[x])
 

Modified: trunk/code/server/sv_game.c
===================================================================
--- trunk/code/server/sv_game.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/server/sv_game.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -288,14 +288,9 @@
 //==============================================
 
 static int	FloatAsInt( float f ) {
-	union
-	{
-	    int i;
-	    float f;
-	} temp;
-	
-	temp.f = f;
-	return temp.i;
+	floatint_t fi;
+	fi.f = f;
+	return fi.i;
 }
 
 /*

Modified: trunk/code/tools/lcc/src/bytecode.c
===================================================================
--- trunk/code/tools/lcc/src/bytecode.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/tools/lcc/src/bytecode.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -40,8 +40,9 @@
 	case P: print("byte %d %U\n", size, (unsigned long)v.p); return;
 	case F:
 		if (size == 4) {
-			float f = v.d;
-			print("byte 4 %u\n", *(unsigned *)&f);
+			floatint_t fi;
+			fi.f = v.d;
+			print("byte 4 %u\n", fi.ui);
 		} else {
 			unsigned *p = (unsigned *)&v.d;
 			print("byte 4 %u\n", p[swap]);
@@ -67,10 +68,10 @@
 		case P: p->x.name = stringf("%U", p->u.c.v.p); break;
 		case F:
 			{	// JDC: added this to get inline floats
-				unsigned temp;
+				floatint_t temp;
 
-				*(float *)&temp = p->u.c.v.d;
-				p->x.name = stringf("%U", temp );
+				temp.f = p->u.c.v.d;
+				p->x.name = stringf("%U", temp.ui );
 			}
 			break;// JDC: added this
 		default: assert(0);

Modified: trunk/code/tools/lcc/src/c.h
===================================================================
--- trunk/code/tools/lcc/src/c.h	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/tools/lcc/src/c.h	2008-11-03 17:03:54 UTC (rev 1481)
@@ -98,6 +98,12 @@
 	void *xt;
 } Xtype;
 
+typedef union {
+	float f;
+	int i;
+	unsigned int ui;
+} floatint_t;
+
 #include "config.h"
 typedef struct metrics {
 	unsigned char size, align, outofline;

Modified: trunk/code/ui/ui_syscalls.c
===================================================================
--- trunk/code/ui/ui_syscalls.c	2008-11-03 17:03:44 UTC (rev 1480)
+++ trunk/code/ui/ui_syscalls.c	2008-11-03 17:03:54 UTC (rev 1481)
@@ -35,9 +35,9 @@
 }
 
 int PASSFLOAT( float x ) {
-	float	floatTemp;
-	floatTemp = x;
-	return *(int *)&floatTemp;
+	floatint_t fi;
+	fi.f = x;
+	return fi.i;
 }
 
 void trap_Print( const char *string ) {
@@ -65,9 +65,9 @@
 }
 
 float trap_Cvar_VariableValue( const char *var_name ) {
-	int temp;
-	temp = syscall( UI_CVAR_VARIABLEVALUE, var_name );
-	return (*(float*)&temp);
+	floatint_t fi;
+	fi.i = syscall( UI_CVAR_VARIABLEVALUE, var_name );
+	return fi.f;
 }
 
 void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize ) {




More information about the quake3-commits mailing list