From 9949da4f926a2e53e4f04e12041ac6831bd9c22c Mon Sep 17 00:00:00 2001 From: Mauller <26652186+Mauller@users.noreply.github.com> Date: Sat, 5 Jul 2025 09:50:01 +0100 Subject: [PATCH] [CORE] Refactor XferCRC to make it branchless and to remove winsock dependency --- Core/GameEngine/Include/Common/XferCRC.h | 2 +- .../Source/Common/System/XferCRC.cpp | 47 ++++++------------- 2 files changed, 16 insertions(+), 33 deletions(-) diff --git a/Core/GameEngine/Include/Common/XferCRC.h b/Core/GameEngine/Include/Common/XferCRC.h index 5c07d5bc4a..35bee5c6be 100644 --- a/Core/GameEngine/Include/Common/XferCRC.h +++ b/Core/GameEngine/Include/Common/XferCRC.h @@ -64,7 +64,7 @@ class XferCRC : public Xfer virtual void xferImplementation( void *data, Int dataSize ); - void addCRC( UnsignedInt val ); ///< CRC a 4-byte block + inline void addCRC( UnsignedInt val ); ///< CRC a 4-byte block UnsignedInt m_crc; diff --git a/Core/GameEngine/Source/Common/System/XferCRC.cpp b/Core/GameEngine/Source/Common/System/XferCRC.cpp index b90e643386..8d505ce6df 100644 --- a/Core/GameEngine/Source/Common/System/XferCRC.cpp +++ b/Core/GameEngine/Source/Common/System/XferCRC.cpp @@ -34,7 +34,7 @@ #include "Common/XferDeepCRC.h" #include "Common/crc.h" #include "Common/Snapshot.h" -#include "winsock2.h" // for htonl +#include "utility/endian_compat.h" //------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------- @@ -97,22 +97,8 @@ void XferCRC::endBlock( void ) //------------------------------------------------------------------------------------------------- void XferCRC::addCRC( UnsignedInt val ) { - int hibit; - val = htonl(val); - - if (m_crc & 0x80000000) - { - hibit = 1; - } - else - { - hibit = 0; - } - - m_crc <<= 1; - m_crc += val; - m_crc += hibit; + m_crc = (m_crc << 1) + htobe(val) +((m_crc >> 31) & 0x01); } // end addCRC @@ -140,30 +126,27 @@ void XferCRC::xferSnapshot( Snapshot *snapshot ) void XferCRC::xferImplementation( void *data, Int dataSize ) { - if (!data || dataSize < 1) - { - return; - } - const UnsignedInt *uintPtr = (const UnsignedInt *) (data); + int validData = (data != NULL) & (dataSize > 0); + int dataBytes = (dataSize / 4) * validData; - for (Int i=0 ; i 0); + + UnsignedInt val = 0; + const unsigned char *c = (const unsigned char *)uintPtr; + for (i=0; i> 31) & 0x01)) * validData; } // end xferImplementation @@ -179,7 +162,7 @@ void XferCRC::skip( Int dataSize ) UnsignedInt XferCRC::getCRC( void ) { - return htonl(m_crc); + return htobe(m_crc); } // end skip