Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: only use -fno-strict-aliasing on files requiring it #1159

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,15 @@ function(AddApplication)
target_link_libraries(test-${A_Target} GTest::gmock)
endif()

if(FLAG_NO_STRICT_ALIASING)
foreach(NOSTRICTALIASINGFILE ${NOSTRICTALIASINGLIST})
if (${NOSTRICTALIASINGFILE} IN_LIST A_Files)
set_source_files_properties(${NOSTRICTALIASINGFILE}
PROPERTIES COMPILE_FLAGS -fno-strict-aliasing)
endif()
endforeach()
endif()

ADD_PRECOMPILED_HEADER(${A_Target}-objects)
endfunction()

Expand Down
5 changes: 4 additions & 1 deletion cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ if(MINGW AND USE_BREAKPAD)
set_linker_flag("-Wl,--build-id")
endif()

# Only check if it's available but do not set it by default,
# it will only be used with a couple of files.
check_CXX_compiler_flag(-fno-strict-aliasing FLAG_NO_STRICT_ALIASING)

if (MSVC)
set_c_cxx_flag("/MP")
set_c_cxx_flag("/fp:fast")
Expand Down Expand Up @@ -189,7 +193,6 @@ elseif (NACL)
set_c_cxx_flag("-O0 -g3" DEBUG)
else()
set_c_cxx_flag("-ffast-math")
set_c_cxx_flag("-fno-strict-aliasing")

# Among the required hardware features, the NX bit (No eXecute bit)
# feature may be required for NativeClient to work. Some early
Expand Down
8 changes: 8 additions & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ set(RENDERERLIST
${ENGINE_DIR}/renderer/tr_animation.cpp
${ENGINE_DIR}/renderer/tr_backend.cpp
${ENGINE_DIR}/renderer/tr_bsp.cpp
${ENGINE_DIR}/renderer/tr_bsp_punned.cpp
${ENGINE_DIR}/renderer/tr_cmds.cpp
${ENGINE_DIR}/renderer/tr_curve.cpp
${ENGINE_DIR}/renderer/tr_decals.cpp
Expand Down Expand Up @@ -287,6 +288,7 @@ set(QCOMMONLIST
${ENGINE_DIR}/qcommon/files.cpp
${ENGINE_DIR}/qcommon/huffman.cpp
${ENGINE_DIR}/qcommon/msg.cpp
${ENGINE_DIR}/qcommon/msg_punned.cpp
${ENGINE_DIR}/qcommon/net_chan.cpp
${ENGINE_DIR}/qcommon/net_ip.cpp
${ENGINE_DIR}/qcommon/net_types.h
Expand Down Expand Up @@ -364,4 +366,10 @@ set(DEDSERVERLIST
${ENGINE_DIR}/null/null_input.cpp
)

# List of known files to be built with the -fno-strict-aliasing flag.
set(NOSTRICTALIASINGLIST
${ENGINE_DIR}/qcommon/msg_punned.cpp
${ENGINE_DIR}/renderer/tr_bsp_punned.cpp
)

set(WIN_RC ${ENGINE_DIR}/sys/windows-resource/icon.rc)
25 changes: 0 additions & 25 deletions src/engine/qcommon/msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,31 +614,6 @@ int MSG_ReadDelta( msg_t *msg, int oldV, int bits )
return oldV;
}

void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV )
{
if ( oldV == newV )
{
MSG_WriteBits( msg, 0, 1 );
return;
}

MSG_WriteBits( msg, 1, 1 );
MSG_WriteBits( msg, * ( int * ) &newV, 32 );
}

float MSG_ReadDeltaFloat( msg_t *msg, float oldV )
{
if ( MSG_ReadBits( msg, 1 ) )
{
float newV;

* ( int * ) &newV = MSG_ReadBits( msg, 32 );
return newV;
}

return oldV;
}

/*
============================================================================

Expand Down
63 changes: 63 additions & 0 deletions src/engine/qcommon/msg_punned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
===========================================================================

Daemon GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.

This file is part of the Daemon GPL Source Code (Daemon Source Code).

Daemon Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Daemon Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon Source Code. If not, see <http://www.gnu.org/licenses/>.

In addition, the Daemon Source Code is also subject to certain additional terms.
You should have received a copy of these additional terms immediately following the
terms and conditions of the GNU General Public License which accompanied the Daemon
Source Code. If not, please request a copy in writing from id Software at the address
below.

If you have questions concerning this license or the applicable additional terms, you
may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville,
Maryland 20850 USA.

===========================================================================
*/

// This file should be built with the -fno-strict-aliasing flag.

#include "qcommon.h"

void MSG_WriteDeltaFloat( msg_t *msg, float oldV, float newV )
{
if ( oldV == newV )
{
MSG_WriteBits( msg, 0, 1 );
return;
}

MSG_WriteBits( msg, 1, 1 );
MSG_WriteBits( msg, * ( int * ) &newV, 32 );
}

float MSG_ReadDeltaFloat( msg_t *msg, float oldV )
{
if ( MSG_ReadBits( msg, 1 ) )
{
float newV;

* ( int * ) &newV = MSG_ReadBits( msg, 32 );
return newV;
}

return oldV;
}

33 changes: 0 additions & 33 deletions src/engine/renderer/tr_bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6285,46 +6285,13 @@ void R_PrecacheInteractions()
Log::Debug("lights precaching time = %5.2f seconds", ( endTime - startTime ) / 1000.0 );
}

static const int HASHTABLE_SIZE = 7919; // 32749 // 2039 /* prime, use % */
#define HASH_USE_EPSILON

#ifdef HASH_USE_EPSILON
#define HASH_XYZ_EPSILON 0.01f
#define HASH_XYZ_EPSILONSPACE_MULTIPLIER 1.f / HASH_XYZ_EPSILON
#endif

unsigned int VertexCoordGenerateHash( const vec3_t xyz )
{
unsigned int hash = 0;

#ifndef HASH_USE_EPSILON
hash += ~( * ( ( unsigned int * ) &xyz[ 0 ] ) << 15 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 0 ] ) >> 10 );
hash += ( * ( ( unsigned int * ) &xyz[ 1 ] ) << 3 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 1 ] ) >> 6 );
hash += ~( * ( ( unsigned int * ) &xyz[ 2 ] ) << 11 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 2 ] ) >> 16 );
#else
vec3_t xyz_epsilonspace;

VectorScale( xyz, HASH_XYZ_EPSILONSPACE_MULTIPLIER, xyz_epsilonspace );
xyz_epsilonspace[ 0 ] = floor( xyz_epsilonspace[ 0 ] );
xyz_epsilonspace[ 1 ] = floor( xyz_epsilonspace[ 1 ] );
xyz_epsilonspace[ 2 ] = floor( xyz_epsilonspace[ 2 ] );

hash += ~( * ( ( unsigned int * ) &xyz_epsilonspace[ 0 ] ) << 15 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 0 ] ) >> 10 );
hash += ( * ( ( unsigned int * ) &xyz_epsilonspace[ 1 ] ) << 3 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 1 ] ) >> 6 );
hash += ~( * ( ( unsigned int * ) &xyz_epsilonspace[ 2 ] ) << 11 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 2 ] ) >> 16 );

#endif

hash = hash % ( HASHTABLE_SIZE );
return hash;
}

vertexHash_t **NewVertexHashTable()
{
vertexHash_t **hashTable = (vertexHash_t**) Z_Calloc( HASHTABLE_SIZE * sizeof( vertexHash_t * ) );
Expand Down
59 changes: 59 additions & 0 deletions src/engine/renderer/tr_bsp_punned.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <[email protected]>
Copyright (C) 2009 Peter McNeill <[email protected]>

This file is part of Daemon source code.

Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/

// This file should be built with the -fno-strict-aliasing flag.

#include "tr_local.h"

unsigned int VertexCoordGenerateHash( const vec3_t xyz )
{
unsigned int hash = 0;

#ifndef HASH_USE_EPSILON
hash += ~( * ( ( unsigned int * ) &xyz[ 0 ] ) << 15 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 0 ] ) >> 10 );
hash += ( * ( ( unsigned int * ) &xyz[ 1 ] ) << 3 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 1 ] ) >> 6 );
hash += ~( * ( ( unsigned int * ) &xyz[ 2 ] ) << 11 );
hash ^= ( * ( ( unsigned int * ) &xyz[ 2 ] ) >> 16 );
#else
vec3_t xyz_epsilonspace;

VectorScale( xyz, HASH_XYZ_EPSILONSPACE_MULTIPLIER, xyz_epsilonspace );
xyz_epsilonspace[ 0 ] = floor( xyz_epsilonspace[ 0 ] );
xyz_epsilonspace[ 1 ] = floor( xyz_epsilonspace[ 1 ] );
xyz_epsilonspace[ 2 ] = floor( xyz_epsilonspace[ 2 ] );

hash += ~( * ( ( unsigned int * ) &xyz_epsilonspace[ 0 ] ) << 15 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 0 ] ) >> 10 );
hash += ( * ( ( unsigned int * ) &xyz_epsilonspace[ 1 ] ) << 3 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 1 ] ) >> 6 );
hash += ~( * ( ( unsigned int * ) &xyz_epsilonspace[ 2 ] ) << 11 );
hash ^= ( * ( ( unsigned int * ) &xyz_epsilonspace[ 2 ] ) >> 16 );

#endif

hash = hash % ( HASHTABLE_SIZE );
return hash;
}
2 changes: 2 additions & 0 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -4007,6 +4007,8 @@ inline bool checkGLErrors()
void R_BuildCubeMaps();
void R_FindTwoNearestCubeMaps( const vec3_t position, cubemapProbe_t **cubeProbeNearest, cubemapProbe_t **cubeProbeSecondNearest );

const size_t HASHTABLE_SIZE = 7919; // 32749 // 2039 /* prime, use % */
unsigned int VertexCoordGenerateHash( const vec3_t xyz );
void FreeVertexHashTable( vertexHash_t **hashTable );

// font stuff
Expand Down