Skip to content

[CORE] Refactor XferCRC to make it branchless and to remove winsock dependency #1228

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Core/GameEngine/Include/Common/XferCRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 15 additions & 32 deletions Core/GameEngine/Source/Common/System/XferCRC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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<dataSize/4 ; i++)
Int i = 0;
for (i=0 ; i<dataBytes; i++)
{
addCRC (*uintPtr++);
}

int leftover = dataSize & 3;
if (leftover)
validData = (data != NULL) & (leftover > 0);

UnsignedInt val = 0;
const unsigned char *c = (const unsigned char *)uintPtr;
for (i=0; i<leftover; i++)
Copy link

@Caball009 Caball009 Jul 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could try a switch statement to unroll the loop, though it'd have to be measured to see whether it's an improvement:

switch(leftover)
{
case 3:
	val += (c[2] << (2 * 8));
case 2:
	val += (c[1] << (1 * 8));
case 1:
	val += (c[0] << (0 * 8));
default:
	break;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check this out, but if it works that at least gets rid of the second loop.

{
UnsignedInt val = 0;
const unsigned char *c = (const unsigned char *)uintPtr;
for (Int i=0; i<leftover; i++)
{
val += (c[i] << (i*8));
}
val = htonl(val);
addCRC (val);
val += (c[i] << (i*8));
}

m_crc = (m_crc << (1*validData)) + (val +((m_crc >> 31) & 0x01)) * validData;

} // end xferImplementation

Expand All @@ -179,7 +162,7 @@ void XferCRC::skip( Int dataSize )
UnsignedInt XferCRC::getCRC( void )
{

return htonl(m_crc);
return htobe(m_crc);

} // end skip

Expand Down
Loading