@@ -222,52 +222,47 @@ void DecodeInfo::CopyVars( const DecodeInfo *pOther )
222222
223223void Int_Encode ( const unsigned char *pStruct, DVariant *pVar, const SendProp *pProp, bf_write *pOut, int objectID )
224224{
225- int nValue = pVar->m_Int ;
226-
227225 if ( pProp->GetFlags () & SPROP_VARINT)
228226 {
229227 if ( pProp->GetFlags () & SPROP_UNSIGNED )
230228 {
231- pOut->WriteVarInt32 ( nValue );
229+ pOut->WriteVarInt32 ( pVar-> m_Int );
232230 }
233231 else
234232 {
235- pOut->WriteSignedVarInt32 ( nValue );
233+ pOut->WriteSignedVarInt32 ( pVar-> m_Int );
236234 }
237235 }
238236 else
239237 {
240- // If signed, preserve lower bits and then re-extend sign if nValue < 0;
241- // if unsigned, preserve all 32 bits no matter what. Bonus: branchless.
242- int nPreserveBits = ( 0x7FFFFFFF >> ( 32 - pProp->m_nBits ) );
243- nPreserveBits |= ( pProp->GetFlags () & SPROP_UNSIGNED ) ? 0xFFFFFFFF : 0 ;
244- int nSignExtension = ( nValue >> 31 ) & ~nPreserveBits;
245-
246- nValue &= nPreserveBits;
247- nValue |= nSignExtension;
248-
249238#ifdef DBGFLAG_ASSERT
250239 // Assert that either the property is unsigned and in valid range,
251240 // or signed with a consistent sign extension in the high bits
252241 if ( pProp->m_nBits < 32 )
253242 {
254243 if ( pProp->GetFlags () & SPROP_UNSIGNED )
255244 {
256- AssertMsg3 ( nValue == pVar->m_Int , " Unsigned prop %s needs more bits? Expected %i == %i" , pProp->GetName (), nValue, pVar->m_Int );
245+ int32 nMaskedValue = pVar->m_Int ;
246+ nMaskedValue &= (1u << pProp->m_nBits ) - 1 ;
247+ Assert (nMaskedValue == pVar->m_Int );
257248 }
258249 else
259250 {
260- AssertMsg3 ( nValue == pVar->m_Int , " Signed prop %s needs more bits? Expected %i == %i" , pProp->GetName (), nValue, pVar->m_Int );
251+ int32 nSignExtendedValue = pVar->m_Int ;
252+ nSignExtendedValue <<= 32 - pProp->m_nBits ;
253+ nSignExtendedValue >>= 32 - pProp->m_nBits ;
254+ Assert (nSignExtendedValue == pVar->m_Int );
261255 }
262256 }
257+ #endif
258+ if (pProp->IsSigned ())
259+ {
260+ pOut->WriteSBitLong (pVar->m_Int , pProp->m_nBits );
261+ }
263262 else
264263 {
265- // This should never trigger, but I'm leaving it in for old-time's sake.
266- Assert ( nValue == pVar->m_Int );
264+ pOut->WriteUBitLong ((unsigned int )pVar->m_Int , pProp->m_nBits );
267265 }
268- #endif
269-
270- pOut->WriteUBitLong ( nValue, pProp->m_nBits , false );
271266 }
272267}
273268
@@ -322,7 +317,7 @@ int Int_CompareDeltas( const SendProp *pProp, bf_read *p1, bf_read *p2 )
322317 return p1->ReadSignedVarInt32 () != p2->ReadSignedVarInt32 ();
323318 }
324319
325- return p1->CompareBits (p2, pProp->m_nBits );
320+ return p1->ReadUBitLong (pProp-> m_nBits ) != p2-> ReadUBitLong ( pProp->m_nBits );
326321}
327322
328323const char * Int_GetTypeNameString ()
0 commit comments