Skip to content

Commit

Permalink
src: move code using type-punned pointers to dedicated files
Browse files Browse the repository at this point in the history
This allows to only use the -fno-strict-aliasing compilation flag
on that code, nothing else.
  • Loading branch information
illwieckz committed May 27, 2024
1 parent 446fb7f commit 78fbe9b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 60 deletions.
6 changes: 4 additions & 2 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 @@ -366,8 +368,8 @@ set(DEDSERVERLIST

# Known file list to be built with the -fno-strict-aliasing flag.
set(NOSTRICTALIASINGLIST
${ENGINE_DIR}/qcommon/msg.cpp
${ENGINE_DIR}/renderer/tr_bsp.cpp
${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

0 comments on commit 78fbe9b

Please sign in to comment.