From c1caf149d7974592b69c6a0655c7890deaadb852 Mon Sep 17 00:00:00 2001 From: terminx Date: Sun, 18 Nov 2018 18:13:33 +0000 Subject: [PATCH] Compile without -fno-strict-aliasing. git-svn-id: https://svn.eduke32.com/eduke32@7224 1a8010ca-5511-0410-912e-c29ae57300e0 --- Common.mak | 2 +- source/build/include/compat.h | 12 ++++++++++-- source/duke3d/src/m32exec.cpp | 19 +++++++++++++------ source/duke3d/src/m32vars.cpp | 11 +++++++---- source/enet/src/win32.c | 5 ++++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Common.mak b/Common.mak index 3226d7f46..d84272f3b 100644 --- a/Common.mak +++ b/Common.mak @@ -691,7 +691,7 @@ ifneq (0,$(KRANDDEBUG)) COMMONFLAGS += -fno-inline -fno-inline-functions -fno-inline-functions-called-once endif -COMMONFLAGS += -fno-strict-aliasing -fno-threadsafe-statics $(F_JUMP_TABLES) $(F_NO_STACK_PROTECTOR) +COMMONFLAGS += -fno-threadsafe-statics $(F_JUMP_TABLES) $(F_NO_STACK_PROTECTOR) $(F_NO_STRICT_ALIASING) ##### Warnings diff --git a/source/build/include/compat.h b/source/build/include/compat.h index 9b1227bbb..38c479a46 100644 --- a/source/build/include/compat.h +++ b/source/build/include/compat.h @@ -129,6 +129,14 @@ # define inline __inline #endif +#ifndef MAY_ALIAS +# ifdef _MSC_VER +# define MAY_ALIAS +# else +# define MAY_ALIAS __attribute__((may_alias)) +# endif +#endif + #ifndef FORCE_INLINE # ifdef _MSC_VER # define FORCE_INLINE __forceinline @@ -783,7 +791,7 @@ typedef reg_t unative_t; #endif EDUKE32_STATIC_ASSERT(sizeof(native_t) == sizeof(unative_t)); -typedef struct { +typedef struct MAY_ALIAS { int32_t x, y; } vec2_t; @@ -795,7 +803,7 @@ typedef struct { uint32_t x, y; } vec2u_t; -typedef struct { +typedef struct MAY_ALIAS { int32_t x, y, z; } vec3_t; diff --git a/source/duke3d/src/m32exec.cpp b/source/duke3d/src/m32exec.cpp index ebd56909a..da8757b55 100644 --- a/source/duke3d/src/m32exec.cpp +++ b/source/duke3d/src/m32exec.cpp @@ -833,8 +833,10 @@ skip_check: case CON_FTOI: insptr++; { - int32_t bits=Gv_GetVarX(*insptr), scale=*(insptr+1); - float fval = *((float *)&bits); + union { int32_t ival; float fval; }; + + ival=Gv_GetVarX(*insptr); + int32_t const scale=*(insptr+1); // rounding must absolutely be! //OSD_Printf("ftoi: bits:%8x, scale=%d, fval=%f, (int32_t)(fval*scale)=%d\n", bits, scale, fval, (int32_t)(fval*scale)); Gv_SetVarX(*insptr, (int32_t)Blrintf(fval * scale)); @@ -845,9 +847,12 @@ skip_check: case CON_ITOF: insptr++; { - int32_t scaled=Gv_GetVarX(*insptr), scale=*(insptr+1); - float fval = (float)scaled/(float)scale; - Gv_SetVarX(*insptr, *((int32_t *)&fval)); + union { int32_t ival; float fval; }; + + ival=Gv_GetVarX(*insptr); + int32_t const scale=*(insptr+1); + fval = (float)ival/(float)scale; + Gv_SetVarX(*insptr, ival); } insptr += 2; continue; @@ -2737,8 +2742,10 @@ badindex: { char buf[64]; int32_t ii = 0; + union { int32_t ival; float fval; }; + ival = arg[i++]; - Bsprintf(buf, "%f", *((float *)&arg[i++])); + Bsprintf(buf, "%f", fval); ii = Bstrlen(buf); Bmemcpy(&tmpbuf[j], buf, ii); diff --git a/source/duke3d/src/m32vars.cpp b/source/duke3d/src/m32vars.cpp index 42f5f671c..11521b38e 100644 --- a/source/duke3d/src/m32vars.cpp +++ b/source/duke3d/src/m32vars.cpp @@ -350,10 +350,12 @@ int32_t __fastcall Gv_GetVarX(int32_t id) return (aGameVars[id].val.plValues[vm.g_st] ^ -negateResult) + negateResult; case GAMEVAR_FLOATPTR: { - float fval = *(float *)aGameVars[id].val.lValue; + union { int32_t ival; float fval; }; + + fval = *(float *)aGameVars[id].val.plValues; if (negateResult) fval *= -1; - return *(int32_t *)&fval; + return ival; } case GAMEVAR_INTPTR: return (*((int32_t *)aGameVars[id].val.lValue) ^ -negateResult) + negateResult; @@ -465,8 +467,9 @@ void __fastcall Gv_SetVarX(int32_t id, int32_t lValue) return; case GAMEVAR_FLOATPTR: { - int32_t ival = lValue; - float fval = *(float *)&ival; + union { int32_t ival; float fval; }; + ival = lValue; + if (fval!=fval || fval<-3.4e38 || fval > 3.4e38) { M32_ERROR("Gv_SetVarX(): tried to set float var to NaN or infinity"); diff --git a/source/enet/src/win32.c b/source/enet/src/win32.c index 8843257dd..06210daa6 100644 --- a/source/enet/src/win32.c +++ b/source/enet/src/win32.c @@ -83,7 +83,10 @@ enet_address_set_host (ENetAddress * address, const char * name) int enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength) { - char * addr = inet_ntoa (* (struct in_addr const *) & address -> host); + struct in_addr in; + memcpy(&in, &address->host, sizeof(struct in_addr)); + char *addr = inet_ntoa (in); + if (addr == NULL) return -1; else