Skip to content

Commit

Permalink
Plugins (gforce): Fix type punning warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaixiong committed Jan 16, 2025
1 parent 70902aa commit 619923e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libvisual-plugins/plugins/actor/gforce/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SET(GFORCE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/plugins/actor/gforce)
SET(GFORCE_DATA_DIR ${LV_PLUGIN_DATA_DIR}/actor/actor_gforce)

SET(GFORCE_COMPILE_DEFS UNIX_X _REENTRANT)
SET(GFORCE_COMPILE_OPTIONS -fno-strict-aliasing -Wno-unknown-warning-option -Wno-sometimes-uninitialized)
SET(GFORCE_COMPILE_OPTIONS -Werror)

# Required on GCC but not Clang for some reason.
IF(NOT MINGW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@
// by Andrew O'Meara

#include "XPtrList.h"
#include <string.h>

class XLongList;

inline float void_ptr_to_float(const void* value)
{
float result;
memcpy( &result, &value, sizeof( result ) );
return result;
}

inline void* float_to_void_ptr(float value)
{
void* result = 0;
memcpy( &result, &value, sizeof( value ) );
return result;
}

class XFloatList {

public:
XFloatList( ListOrderingT inOrdering = cOrderNotImportant ); // See XPtrList.h for ListOrderingT choices

// See XPtrList.h for description of functions.
virtual long Add( float inNum ) { return mList.Add( *((void**) &inNum) ); }
virtual long Add( float inNum ) { return mList.Add( float_to_void_ptr( inNum ) ); }
virtual void Add( const XFloatList& inList ) { mList.Add( inList.mList ); }
virtual bool RemoveElement( long inIndex ) { return mList.RemoveElement( inIndex ); }
virtual void RemoveAll() { mList.RemoveAll(); }
virtual float Fetch( long inIndex ) { long t = (long) mList.Fetch( inIndex ); return *((float*) &t);}
virtual float Fetch( long inIndex ) { return void_ptr_to_float( mList.Fetch( inIndex ) ); }
virtual bool Fetch( long inIndex, float* ioPtrDest ) const { return mList.Fetch( inIndex, (void**)ioPtrDest ); }
virtual long Count() const { return mList.Count(); }

Expand All @@ -36,7 +50,7 @@ class XFloatList {
// Smoothes all the floats in this list
void GaussSmooth( float inSigma );

float operator[] ( const long inIndex ) { long t = (long) mList.Fetch( inIndex ); return *((float*) &t); }
float operator[] ( const long inIndex ) { return void_ptr_to_float(mList.Fetch( inIndex )); }

// Generic utility fcn to gauss-smooth a 1D curve.
static void GaussSmooth( float inSigma, long inN, float inSrceDest[] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void XFloatList::FindMeans( long inNumMeans, float outMeans[], float inSigmaScal

// If this a local max. (Note: this could/should be improved for neighbors that are equal)
if ( ( cen > left && cen >= right ) ) {
sepCandidates.Put( i, *((void**) &cen) );
sepCandidates.Put( i, float_to_void_ptr( cen ) );
}
}

Expand Down Expand Up @@ -284,7 +284,7 @@ void XFloatList::Rank( XLongList& outRank, long inNumToRank ) const {


int XFloatList::sQSFloatComparitor( const void* inA, const void* inB ) {
float diff = *((float*) inB) - *((float*) inA);
float diff = void_ptr_to_float(inB) - void_ptr_to_float(inA);
if ( diff > 0.0 )
return 1;
else if ( diff < 0.0 )
Expand All @@ -296,7 +296,7 @@ int XFloatList::sQSFloatComparitor( const void* inA, const void* inB ) {


int XFloatList::sFloatComparitor( const void* inA, const void* inB ) {
float diff = *((float*) &inB) - *((float*) &inA);
float diff = void_ptr_to_float(inA) - void_ptr_to_float(inB);
if ( diff > 0.0 )
return 1;
else if ( diff < 0.0 )
Expand Down
20 changes: 16 additions & 4 deletions libvisual-plugins/plugins/actor/gforce/Common/io/CEgIStream.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "CEgIStream.h"

//#include <string.h>
#include <stdint.h>
#include <string.h>


UtilStr CEgIStream::sTemp;
Expand Down Expand Up @@ -32,8 +32,20 @@ long CEgIStream::GetLong() {


float CEgIStream::GetFloat() {
long v = GetLong();
return *( (float*) &v );
uint32_t c, n = GetByte();

c = GetByte();
n = n | ( c << 8 );

c = GetByte();
n = n | ( c << 16 );

c = GetByte();
n = n | ( c << 24 );

float result;
memcpy(&result, &n, sizeof(result));
return result;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <libvisual/libvisual.h>
#include <limits>
#include <stdlib.h>

#include <string.h>

#define __addInst( opcode, data16 ) long op = (opcode) | (data16); \
mProgram.Append( &op, sizeof(long) );
Expand Down Expand Up @@ -85,7 +85,7 @@ ExprUserFcn ExprVirtualMachine::sZeroFcn = { 0, { 0 } };
case cABS: r = fabs( r ); break; \
case cSIN: r = sin( r ); break; \
case cCOS: r = cos( r ); break; \
case cSEED: i = *((long*) &r); \
case cSEED: i = pun_float_to_long(r); \
size = i % 31; \
srand( ( i << size ) | ( i >> ( 32 - size ) ) ); break; \
case cTAN: r = tan( r ); break; \
Expand Down Expand Up @@ -114,6 +114,13 @@ ExprUserFcn ExprVirtualMachine::sZeroFcn = { 0, { 0 } };
case '%': { long tt = r2; r1 = (tt != 0) ? (( (long) r1 ) % tt) : 0.0; break; } \
}

inline long pun_float_to_long(float x)
{
long result = 0;
memcpy(&result, &x, sizeof(x));
return result;
}

ExprVirtualMachine::ExprVirtualMachine() {
mPCStart = 0;
mPCEnd = 0;
Expand Down

0 comments on commit 619923e

Please sign in to comment.