1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-01-05 17:13:36 +08:00

Update bf_write/read classes

Also use correct variant of WriteUBitVar/ReadUBitVar
This commit is contained in:
GAMMACASE 2024-03-18 15:57:08 +03:00
parent ab21c70896
commit 139e609877
2 changed files with 32 additions and 38 deletions

View File

@ -263,12 +263,11 @@ public:
int m_iCurBit; int m_iCurBit;
private: private:
const char *m_pDebugName;
// Errors? // Errors?
bool m_bOverflow; bool m_bOverflow;
bool m_bAssertOnOverflow; bool m_bAssertOnOverflow;
const char *m_pDebugName;
}; };
@ -440,25 +439,20 @@ BITBUF_INLINE void bf_write::WriteUBitLong( unsigned int curData, int numbits, b
} }
// writes an unsigned integer with variable bit length // writes an unsigned integer with variable bit length
BITBUF_INLINE void bf_write::WriteUBitVar( unsigned int data ) BITBUF_INLINE void bf_write::WriteUBitVar( unsigned int n )
{ {
/* Reference: if(n < 16)
if ( data < 0x10u ) WriteUBitLong( n, 6 );
WriteUBitLong( 0, 2 ), WriteUBitLong( data, 4 ); else
else if ( data < 0x100u ) if(n < 256)
WriteUBitLong( 1, 2 ), WriteUBitLong( data, 8 ); WriteUBitLong( (n & 15) | 16 | ((n & (128 | 64 | 32 | 16)) << 2), 10 );
else if ( data < 0x1000u ) else
WriteUBitLong( 2, 2 ), WriteUBitLong( data, 12 ); if(n < 4096)
WriteUBitLong( (n & 15) | 32 | ((n & (2048 | 1024 | 512 | 256 | 128 | 64 | 32 | 16)) << 2), 14 );
else else
WriteUBitLong( 3, 2 ), WriteUBitLong( data, 32 );
*/
// a < b ? -1 : 0 translates into a CMP, SBB instruction pair
// with no flow control. should also be branchless on consoles.
int n = (data < 0x10u ? -1 : 0) + (data < 0x100u ? -1 : 0) + (data < 0x1000u ? -1 : 0);
WriteUBitLong( data*4 + n + 3, 6 + n*4 + 12 );
if ( data >= 0x1000u )
{ {
WriteUBitLong( data >> 16, 16 ); WriteUBitLong( (n & 15) | 48, 6 );
WriteUBitLong( (n >> 4), 32 - 4 );
} }
} }
@ -572,7 +566,6 @@ public:
// reads an unsigned integer with variable bit length // reads an unsigned integer with variable bit length
unsigned int ReadUBitVar(); unsigned int ReadUBitVar();
unsigned int ReadUBitVarInternal( int encodingType );
// reads a varint encoded integer // reads a varint encoded integer
uint32 ReadVarInt32(); uint32 ReadVarInt32();
@ -660,13 +653,13 @@ public:
private: private:
const char *m_pDebugName;
// Errors? // Errors?
bool m_bOverflow; bool m_bOverflow;
// For debugging.. // For debugging..
bool m_bAssertOnOverflow; bool m_bAssertOnOverflow;
const char *m_pDebugName;
}; };
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -755,15 +748,24 @@ inline float bf_read::ReadBitFloat()
BITBUF_INLINE unsigned int bf_read::ReadUBitVar() BITBUF_INLINE unsigned int bf_read::ReadUBitVar()
{ {
// six bits: low 2 bits for encoding + first 4 bits of value unsigned int ret = ReadUBitLong( 6 );
unsigned int sixbits = ReadUBitLong(6); switch(ret & (16 | 32))
unsigned int encoding = sixbits & 3;
if ( encoding )
{ {
// this function will seek back four bits and read the full value case 16:
return ReadUBitVarInternal( encoding ); ret = (ret & 15) | (ReadUBitLong( 4 ) << 4);
Assert( ret >= 16 );
break;
case 32:
ret = (ret & 15) | (ReadUBitLong( 8 ) << 4);
Assert( ret >= 256 );
break;
case 48:
ret = (ret & 15) | (ReadUBitLong( 32 - 4 ) << 4);
Assert( ret >= 4096 );
break;
} }
return sixbits >> 2; return ret;
} }
BITBUF_INLINE unsigned int bf_read::ReadUBitLong( int numbits ) RESTRICT BITBUF_INLINE unsigned int bf_read::ReadUBitLong( int numbits ) RESTRICT

View File

@ -997,14 +997,6 @@ unsigned int bf_read::ReadUBitLongNoInline( int numbits )
return ReadUBitLong( numbits ); return ReadUBitLong( numbits );
} }
unsigned int bf_read::ReadUBitVarInternal( int encodingType )
{
m_iCurBit -= 4;
// int bits = { 4, 8, 12, 32 }[ encodingType ];
int bits = 4 + encodingType*4 + (((2 - encodingType) >> 31) & 16);
return ReadUBitLong( bits );
}
// Append numbits least significant bits from data to the current bit stream // Append numbits least significant bits from data to the current bit stream
int bf_read::ReadSBitLong( int numbits ) int bf_read::ReadSBitLong( int numbits )
{ {