Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1490 lines
32 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "bitbuf.h"
  9. #include "coordsize.h"
  10. #include "mathlib/vector.h"
  11. #include "mathlib/mathlib.h"
  12. #include "tier1/strtools.h"
  13. #include "bitvec.h"
  14. // FIXME: Can't use this until we get multithreaded allocations in tier0 working for tools
  15. // This is used by VVIS and fails to link
  16. // NOTE: This must be the last file included!!!
  17. //#include "tier0/memdbgon.h"
  18. #ifdef _X360
  19. // mandatory ... wary of above comment and isolating, tier0 is built as MT though
  20. #include "tier0/memdbgon.h"
  21. #endif
  22. #if _WIN32
  23. #define FAST_BIT_SCAN 1
  24. #if _X360
  25. #define CountLeadingZeros(x) _CountLeadingZeros(x)
  26. inline unsigned int CountTrailingZeros( unsigned int elem )
  27. {
  28. // this implements CountTrailingZeros() / BitScanForward()
  29. unsigned int mask = elem-1;
  30. unsigned int comp = ~elem;
  31. elem = mask & comp;
  32. return (32 - _CountLeadingZeros(elem));
  33. }
  34. #else
  35. #include <intrin.h>
  36. #pragma intrinsic(_BitScanReverse)
  37. #pragma intrinsic(_BitScanForward)
  38. inline unsigned int CountLeadingZeros(unsigned int x)
  39. {
  40. unsigned long firstBit;
  41. if ( _BitScanReverse(&firstBit,x) )
  42. return 31 - firstBit;
  43. return 32;
  44. }
  45. inline unsigned int CountTrailingZeros(unsigned int elem)
  46. {
  47. unsigned long out;
  48. if ( _BitScanForward(&out, elem) )
  49. return out;
  50. return 32;
  51. }
  52. #endif
  53. #else
  54. #define FAST_BIT_SCAN 0
  55. #endif
  56. static BitBufErrorHandler g_BitBufErrorHandler = 0;
  57. inline int BitForBitnum(int bitnum)
  58. {
  59. return GetBitForBitnum(bitnum);
  60. }
  61. void InternalBitBufErrorHandler( BitBufErrorType errorType, const char *pDebugName )
  62. {
  63. if ( g_BitBufErrorHandler )
  64. g_BitBufErrorHandler( errorType, pDebugName );
  65. }
  66. void SetBitBufErrorHandler( BitBufErrorHandler fn )
  67. {
  68. g_BitBufErrorHandler = fn;
  69. }
  70. // #define BB_PROFILING
  71. unsigned long g_LittleBits[32];
  72. // Precalculated bit masks for WriteUBitLong. Using these tables instead of
  73. // doing the calculations gives a 33% speedup in WriteUBitLong.
  74. unsigned long g_BitWriteMasks[32][33];
  75. // (1 << i) - 1
  76. unsigned long g_ExtraMasks[33];
  77. class CBitWriteMasksInit
  78. {
  79. public:
  80. CBitWriteMasksInit()
  81. {
  82. for( unsigned int startbit=0; startbit < 32; startbit++ )
  83. {
  84. for( unsigned int nBitsLeft=0; nBitsLeft < 33; nBitsLeft++ )
  85. {
  86. unsigned int endbit = startbit + nBitsLeft;
  87. g_BitWriteMasks[startbit][nBitsLeft] = BitForBitnum(startbit) - 1;
  88. if(endbit < 32)
  89. g_BitWriteMasks[startbit][nBitsLeft] |= ~(BitForBitnum(endbit) - 1);
  90. }
  91. }
  92. for ( unsigned int maskBit=0; maskBit < 32; maskBit++ )
  93. g_ExtraMasks[maskBit] = BitForBitnum(maskBit) - 1;
  94. g_ExtraMasks[32] = ~0ul;
  95. for ( unsigned int littleBit=0; littleBit < 32; littleBit++ )
  96. StoreLittleDWord( &g_LittleBits[littleBit], 0, 1u<<littleBit );
  97. }
  98. };
  99. static CBitWriteMasksInit g_BitWriteMasksInit;
  100. // ---------------------------------------------------------------------------------------- //
  101. // bf_write
  102. // ---------------------------------------------------------------------------------------- //
  103. bf_write::bf_write()
  104. {
  105. m_pData = NULL;
  106. m_nDataBytes = 0;
  107. m_nDataBits = -1; // set to -1 so we generate overflow on any operation
  108. m_iCurBit = 0;
  109. m_bOverflow = false;
  110. m_bAssertOnOverflow = true;
  111. m_pDebugName = NULL;
  112. }
  113. bf_write::bf_write( const char *pDebugName, void *pData, int nBytes, int nBits )
  114. {
  115. m_bAssertOnOverflow = true;
  116. m_pDebugName = pDebugName;
  117. StartWriting( pData, nBytes, 0, nBits );
  118. }
  119. bf_write::bf_write( void *pData, int nBytes, int nBits )
  120. {
  121. m_bAssertOnOverflow = true;
  122. m_pDebugName = NULL;
  123. StartWriting( pData, nBytes, 0, nBits );
  124. }
  125. void bf_write::StartWriting( void *pData, int nBytes, int iStartBit, int nBits )
  126. {
  127. // Make sure it's dword aligned and padded.
  128. Assert( (nBytes % 4) == 0 );
  129. Assert(((unsigned long)pData & 3) == 0);
  130. // The writing code will overrun the end of the buffer if it isn't dword aligned, so truncate to force alignment
  131. nBytes &= ~3;
  132. m_pData = (unsigned long*)pData;
  133. m_nDataBytes = nBytes;
  134. if ( nBits == -1 )
  135. {
  136. m_nDataBits = nBytes << 3;
  137. }
  138. else
  139. {
  140. Assert( nBits <= nBytes*8 );
  141. m_nDataBits = nBits;
  142. }
  143. m_iCurBit = iStartBit;
  144. m_bOverflow = false;
  145. }
  146. void bf_write::Reset()
  147. {
  148. m_iCurBit = 0;
  149. m_bOverflow = false;
  150. }
  151. void bf_write::SetAssertOnOverflow( bool bAssert )
  152. {
  153. m_bAssertOnOverflow = bAssert;
  154. }
  155. const char* bf_write::GetDebugName()
  156. {
  157. return m_pDebugName;
  158. }
  159. void bf_write::SetDebugName( const char *pDebugName )
  160. {
  161. m_pDebugName = pDebugName;
  162. }
  163. void bf_write::SeekToBit( int bitPos )
  164. {
  165. m_iCurBit = bitPos;
  166. }
  167. // Sign bit comes first
  168. void bf_write::WriteSBitLong( int data, int numbits )
  169. {
  170. // Force the sign-extension bit to be correct even in the case of overflow.
  171. int nValue = data;
  172. int nPreserveBits = ( 0x7FFFFFFF >> ( 32 - numbits ) );
  173. int nSignExtension = ( nValue >> 31 ) & ~nPreserveBits;
  174. nValue &= nPreserveBits;
  175. nValue |= nSignExtension;
  176. AssertMsg2( nValue == data, "WriteSBitLong: 0x%08x does not fit in %d bits", data, numbits );
  177. WriteUBitLong( nValue, numbits, false );
  178. }
  179. void bf_write::WriteVarInt32( uint32 data )
  180. {
  181. // Check if align and we have room, slow path if not
  182. if ( (m_iCurBit & 7) == 0 && (m_iCurBit + bitbuf::kMaxVarint32Bytes * 8 ) <= m_nDataBits)
  183. {
  184. uint8 *target = ((uint8*)m_pData) + (m_iCurBit>>3);
  185. target[0] = static_cast<uint8>(data | 0x80);
  186. if ( data >= (1 << 7) )
  187. {
  188. target[1] = static_cast<uint8>((data >> 7) | 0x80);
  189. if ( data >= (1 << 14) )
  190. {
  191. target[2] = static_cast<uint8>((data >> 14) | 0x80);
  192. if ( data >= (1 << 21) )
  193. {
  194. target[3] = static_cast<uint8>((data >> 21) | 0x80);
  195. if ( data >= (1 << 28) )
  196. {
  197. target[4] = static_cast<uint8>(data >> 28);
  198. m_iCurBit += 5 * 8;
  199. return;
  200. }
  201. else
  202. {
  203. target[3] &= 0x7F;
  204. m_iCurBit += 4 * 8;
  205. return;
  206. }
  207. }
  208. else
  209. {
  210. target[2] &= 0x7F;
  211. m_iCurBit += 3 * 8;
  212. return;
  213. }
  214. }
  215. else
  216. {
  217. target[1] &= 0x7F;
  218. m_iCurBit += 2 * 8;
  219. return;
  220. }
  221. }
  222. else
  223. {
  224. target[0] &= 0x7F;
  225. m_iCurBit += 1 * 8;
  226. return;
  227. }
  228. }
  229. else // Slow path
  230. {
  231. while ( data > 0x7F )
  232. {
  233. WriteUBitLong( (data & 0x7F) | 0x80, 8 );
  234. data >>= 7;
  235. }
  236. WriteUBitLong( data & 0x7F, 8 );
  237. }
  238. }
  239. void bf_write::WriteVarInt64( uint64 data )
  240. {
  241. // Check if align and we have room, slow path if not
  242. if ( (m_iCurBit & 7) == 0 && (m_iCurBit + bitbuf::kMaxVarintBytes * 8 ) <= m_nDataBits )
  243. {
  244. uint8 *target = ((uint8*)m_pData) + (m_iCurBit>>3);
  245. // Splitting into 32-bit pieces gives better performance on 32-bit
  246. // processors.
  247. uint32 part0 = static_cast<uint32>(data );
  248. uint32 part1 = static_cast<uint32>(data >> 28);
  249. uint32 part2 = static_cast<uint32>(data >> 56);
  250. int size;
  251. // Here we can't really optimize for small numbers, since the data is
  252. // split into three parts. Cheking for numbers < 128, for instance,
  253. // would require three comparisons, since you'd have to make sure part1
  254. // and part2 are zero. However, if the caller is using 64-bit integers,
  255. // it is likely that they expect the numbers to often be very large, so
  256. // we probably don't want to optimize for small numbers anyway. Thus,
  257. // we end up with a hardcoded binary search tree...
  258. if ( part2 == 0 )
  259. {
  260. if ( part1 == 0 )
  261. {
  262. if ( part0 < (1 << 14) )
  263. {
  264. if ( part0 < (1 << 7) )
  265. {
  266. size = 1; goto size1;
  267. }
  268. else
  269. {
  270. size = 2; goto size2;
  271. }
  272. }
  273. else
  274. {
  275. if ( part0 < (1 << 21) )
  276. {
  277. size = 3; goto size3;
  278. }
  279. else
  280. {
  281. size = 4; goto size4;
  282. }
  283. }
  284. }
  285. else
  286. {
  287. if ( part1 < (1 << 14) )
  288. {
  289. if ( part1 < (1 << 7) )
  290. {
  291. size = 5; goto size5;
  292. }
  293. else
  294. {
  295. size = 6; goto size6;
  296. }
  297. }
  298. else
  299. {
  300. if ( part1 < (1 << 21) )
  301. {
  302. size = 7; goto size7;
  303. }
  304. else
  305. {
  306. size = 8; goto size8;
  307. }
  308. }
  309. }
  310. }
  311. else
  312. {
  313. if ( part2 < (1 << 7) )
  314. {
  315. size = 9; goto size9;
  316. }
  317. else
  318. {
  319. size = 10; goto size10;
  320. }
  321. }
  322. AssertFatalMsg( false, "Can't get here." );
  323. size10: target[9] = static_cast<uint8>((part2 >> 7) | 0x80);
  324. size9 : target[8] = static_cast<uint8>((part2 ) | 0x80);
  325. size8 : target[7] = static_cast<uint8>((part1 >> 21) | 0x80);
  326. size7 : target[6] = static_cast<uint8>((part1 >> 14) | 0x80);
  327. size6 : target[5] = static_cast<uint8>((part1 >> 7) | 0x80);
  328. size5 : target[4] = static_cast<uint8>((part1 ) | 0x80);
  329. size4 : target[3] = static_cast<uint8>((part0 >> 21) | 0x80);
  330. size3 : target[2] = static_cast<uint8>((part0 >> 14) | 0x80);
  331. size2 : target[1] = static_cast<uint8>((part0 >> 7) | 0x80);
  332. size1 : target[0] = static_cast<uint8>((part0 ) | 0x80);
  333. target[size-1] &= 0x7F;
  334. m_iCurBit += size * 8;
  335. }
  336. else // slow path
  337. {
  338. while ( data > 0x7F )
  339. {
  340. WriteUBitLong( (data & 0x7F) | 0x80, 8 );
  341. data >>= 7;
  342. }
  343. WriteUBitLong( data & 0x7F, 8 );
  344. }
  345. }
  346. void bf_write::WriteSignedVarInt32( int32 data )
  347. {
  348. WriteVarInt32( bitbuf::ZigZagEncode32( data ) );
  349. }
  350. void bf_write::WriteSignedVarInt64( int64 data )
  351. {
  352. WriteVarInt64( bitbuf::ZigZagEncode64( data ) );
  353. }
  354. int bf_write::ByteSizeVarInt32( uint32 data )
  355. {
  356. int size = 1;
  357. while ( data > 0x7F ) {
  358. size++;
  359. data >>= 7;
  360. }
  361. return size;
  362. }
  363. int bf_write::ByteSizeVarInt64( uint64 data )
  364. {
  365. int size = 1;
  366. while ( data > 0x7F ) {
  367. size++;
  368. data >>= 7;
  369. }
  370. return size;
  371. }
  372. int bf_write::ByteSizeSignedVarInt32( int32 data )
  373. {
  374. return ByteSizeVarInt32( bitbuf::ZigZagEncode32( data ) );
  375. }
  376. int bf_write::ByteSizeSignedVarInt64( int64 data )
  377. {
  378. return ByteSizeVarInt64( bitbuf::ZigZagEncode64( data ) );
  379. }
  380. void bf_write::WriteBitLong(unsigned int data, int numbits, bool bSigned)
  381. {
  382. if(bSigned)
  383. WriteSBitLong((int)data, numbits);
  384. else
  385. WriteUBitLong(data, numbits);
  386. }
  387. bool bf_write::WriteBits(const void *pInData, int nBits)
  388. {
  389. #if defined( BB_PROFILING )
  390. VPROF( "bf_write::WriteBits" );
  391. #endif
  392. unsigned char *pOut = (unsigned char*)pInData;
  393. int nBitsLeft = nBits;
  394. // Bounds checking..
  395. if ( (m_iCurBit+nBits) > m_nDataBits )
  396. {
  397. SetOverflowFlag();
  398. CallErrorHandler( BITBUFERROR_BUFFER_OVERRUN, GetDebugName() );
  399. return false;
  400. }
  401. // Align output to dword boundary
  402. while (((unsigned long)pOut & 3) != 0 && nBitsLeft >= 8)
  403. {
  404. WriteUBitLong( *pOut, 8, false );
  405. ++pOut;
  406. nBitsLeft -= 8;
  407. }
  408. if ( IsPC() && (nBitsLeft >= 32) && (m_iCurBit & 7) == 0 )
  409. {
  410. // current bit is byte aligned, do block copy
  411. int numbytes = nBitsLeft >> 3;
  412. int numbits = numbytes << 3;
  413. Q_memcpy( (char*)m_pData+(m_iCurBit>>3), pOut, numbytes );
  414. pOut += numbytes;
  415. nBitsLeft -= numbits;
  416. m_iCurBit += numbits;
  417. }
  418. // X360TBD: Can't write dwords in WriteBits because they'll get swapped
  419. if ( IsPC() && nBitsLeft >= 32 )
  420. {
  421. unsigned long iBitsRight = (m_iCurBit & 31);
  422. unsigned long iBitsLeft = 32 - iBitsRight;
  423. unsigned long bitMaskLeft = g_BitWriteMasks[iBitsRight][32];
  424. unsigned long bitMaskRight = g_BitWriteMasks[0][iBitsRight];
  425. unsigned long *pData = &m_pData[m_iCurBit>>5];
  426. // Read dwords.
  427. while(nBitsLeft >= 32)
  428. {
  429. unsigned long curData = *(unsigned long*)pOut;
  430. pOut += sizeof(unsigned long);
  431. *pData &= bitMaskLeft;
  432. *pData |= curData << iBitsRight;
  433. pData++;
  434. if ( iBitsLeft < 32 )
  435. {
  436. curData >>= iBitsLeft;
  437. *pData &= bitMaskRight;
  438. *pData |= curData;
  439. }
  440. nBitsLeft -= 32;
  441. m_iCurBit += 32;
  442. }
  443. }
  444. // write remaining bytes
  445. while ( nBitsLeft >= 8 )
  446. {
  447. WriteUBitLong( *pOut, 8, false );
  448. ++pOut;
  449. nBitsLeft -= 8;
  450. }
  451. // write remaining bits
  452. if ( nBitsLeft )
  453. {
  454. WriteUBitLong( *pOut, nBitsLeft, false );
  455. }
  456. return !IsOverflowed();
  457. }
  458. bool bf_write::WriteBitsFromBuffer( bf_read *pIn, int nBits )
  459. {
  460. // This could be optimized a little by
  461. while ( nBits > 32 )
  462. {
  463. WriteUBitLong( pIn->ReadUBitLong( 32 ), 32 );
  464. nBits -= 32;
  465. }
  466. WriteUBitLong( pIn->ReadUBitLong( nBits ), nBits );
  467. return !IsOverflowed() && !pIn->IsOverflowed();
  468. }
  469. void bf_write::WriteBitAngle( float fAngle, int numbits )
  470. {
  471. int d;
  472. unsigned int mask;
  473. unsigned int shift;
  474. shift = BitForBitnum(numbits);
  475. mask = shift - 1;
  476. d = (int)( (fAngle / 360.0) * shift );
  477. d &= mask;
  478. WriteUBitLong((unsigned int)d, numbits);
  479. }
  480. void bf_write::WriteBitCoordMP( const float f, bool bIntegral, bool bLowPrecision )
  481. {
  482. #if defined( BB_PROFILING )
  483. VPROF( "bf_write::WriteBitCoordMP" );
  484. #endif
  485. int signbit = (f <= -( bLowPrecision ? COORD_RESOLUTION_LOWPRECISION : COORD_RESOLUTION ));
  486. int intval = (int)abs(f);
  487. int fractval = bLowPrecision ?
  488. ( abs((int)(f*COORD_DENOMINATOR_LOWPRECISION)) & (COORD_DENOMINATOR_LOWPRECISION-1) ) :
  489. ( abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1) );
  490. bool bInBounds = intval < (1 << COORD_INTEGER_BITS_MP );
  491. unsigned int bits, numbits;
  492. if ( bIntegral )
  493. {
  494. // Integer encoding: in-bounds bit, nonzero bit, optional sign bit + integer value bits
  495. if ( intval )
  496. {
  497. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  498. --intval;
  499. bits = intval * 8 + signbit * 4 + 2 + bInBounds;
  500. numbits = 3 + (bInBounds ? COORD_INTEGER_BITS_MP : COORD_INTEGER_BITS);
  501. }
  502. else
  503. {
  504. bits = bInBounds;
  505. numbits = 2;
  506. }
  507. }
  508. else
  509. {
  510. // Float encoding: in-bounds bit, integer bit, sign bit, fraction value bits, optional integer value bits
  511. if ( intval )
  512. {
  513. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  514. --intval;
  515. bits = intval * 8 + signbit * 4 + 2 + bInBounds;
  516. bits += bInBounds ? (fractval << (3+COORD_INTEGER_BITS_MP)) : (fractval << (3+COORD_INTEGER_BITS));
  517. numbits = 3 + (bInBounds ? COORD_INTEGER_BITS_MP : COORD_INTEGER_BITS)
  518. + (bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS);
  519. }
  520. else
  521. {
  522. bits = fractval * 8 + signbit * 4 + 0 + bInBounds;
  523. numbits = 3 + (bLowPrecision ? COORD_FRACTIONAL_BITS_MP_LOWPRECISION : COORD_FRACTIONAL_BITS);
  524. }
  525. }
  526. WriteUBitLong( bits, numbits );
  527. }
  528. void bf_write::WriteBitCoord (const float f)
  529. {
  530. #if defined( BB_PROFILING )
  531. VPROF( "bf_write::WriteBitCoord" );
  532. #endif
  533. int signbit = (f <= -COORD_RESOLUTION);
  534. int intval = (int)abs(f);
  535. int fractval = abs((int)(f*COORD_DENOMINATOR)) & (COORD_DENOMINATOR-1);
  536. // Send the bit flags that indicate whether we have an integer part and/or a fraction part.
  537. WriteOneBit( intval );
  538. WriteOneBit( fractval );
  539. if ( intval || fractval )
  540. {
  541. // Send the sign bit
  542. WriteOneBit( signbit );
  543. // Send the integer if we have one.
  544. if ( intval )
  545. {
  546. // Adjust the integers from [1..MAX_COORD_VALUE] to [0..MAX_COORD_VALUE-1]
  547. intval--;
  548. WriteUBitLong( (unsigned int)intval, COORD_INTEGER_BITS );
  549. }
  550. // Send the fraction if we have one
  551. if ( fractval )
  552. {
  553. WriteUBitLong( (unsigned int)fractval, COORD_FRACTIONAL_BITS );
  554. }
  555. }
  556. }
  557. void bf_write::WriteBitVec3Coord( const Vector& fa )
  558. {
  559. int xflag, yflag, zflag;
  560. xflag = (fa[0] >= COORD_RESOLUTION) || (fa[0] <= -COORD_RESOLUTION);
  561. yflag = (fa[1] >= COORD_RESOLUTION) || (fa[1] <= -COORD_RESOLUTION);
  562. zflag = (fa[2] >= COORD_RESOLUTION) || (fa[2] <= -COORD_RESOLUTION);
  563. WriteOneBit( xflag );
  564. WriteOneBit( yflag );
  565. WriteOneBit( zflag );
  566. if ( xflag )
  567. WriteBitCoord( fa[0] );
  568. if ( yflag )
  569. WriteBitCoord( fa[1] );
  570. if ( zflag )
  571. WriteBitCoord( fa[2] );
  572. }
  573. void bf_write::WriteBitNormal( float f )
  574. {
  575. int signbit = (f <= -NORMAL_RESOLUTION);
  576. // NOTE: Since +/-1 are valid values for a normal, I'm going to encode that as all ones
  577. unsigned int fractval = abs( (int)(f*NORMAL_DENOMINATOR) );
  578. // clamp..
  579. if (fractval > NORMAL_DENOMINATOR)
  580. fractval = NORMAL_DENOMINATOR;
  581. // Send the sign bit
  582. WriteOneBit( signbit );
  583. // Send the fractional component
  584. WriteUBitLong( fractval, NORMAL_FRACTIONAL_BITS );
  585. }
  586. void bf_write::WriteBitVec3Normal( const Vector& fa )
  587. {
  588. int xflag, yflag;
  589. xflag = (fa[0] >= NORMAL_RESOLUTION) || (fa[0] <= -NORMAL_RESOLUTION);
  590. yflag = (fa[1] >= NORMAL_RESOLUTION) || (fa[1] <= -NORMAL_RESOLUTION);
  591. WriteOneBit( xflag );
  592. WriteOneBit( yflag );
  593. if ( xflag )
  594. WriteBitNormal( fa[0] );
  595. if ( yflag )
  596. WriteBitNormal( fa[1] );
  597. // Write z sign bit
  598. int signbit = (fa[2] <= -NORMAL_RESOLUTION);
  599. WriteOneBit( signbit );
  600. }
  601. void bf_write::WriteBitAngles( const QAngle& fa )
  602. {
  603. // FIXME:
  604. Vector tmp( fa.x, fa.y, fa.z );
  605. WriteBitVec3Coord( tmp );
  606. }
  607. void bf_write::WriteChar(int val)
  608. {
  609. WriteSBitLong(val, sizeof(char) << 3);
  610. }
  611. void bf_write::WriteByte(int val)
  612. {
  613. WriteUBitLong(val, sizeof(unsigned char) << 3);
  614. }
  615. void bf_write::WriteShort(int val)
  616. {
  617. WriteSBitLong(val, sizeof(short) << 3);
  618. }
  619. void bf_write::WriteWord(int val)
  620. {
  621. WriteUBitLong(val, sizeof(unsigned short) << 3);
  622. }
  623. void bf_write::WriteLong(long val)
  624. {
  625. WriteSBitLong(val, sizeof(long) << 3);
  626. }
  627. void bf_write::WriteLongLong(int64 val)
  628. {
  629. uint *pLongs = (uint*)&val;
  630. // Insert the two DWORDS according to network endian
  631. const short endianIndex = 0x0100;
  632. byte *idx = (byte*)&endianIndex;
  633. WriteUBitLong(pLongs[*idx++], sizeof(long) << 3);
  634. WriteUBitLong(pLongs[*idx], sizeof(long) << 3);
  635. }
  636. void bf_write::WriteFloat(float val)
  637. {
  638. // Pre-swap the float, since WriteBits writes raw data
  639. LittleFloat( &val, &val );
  640. WriteBits(&val, sizeof(val) << 3);
  641. }
  642. bool bf_write::WriteBytes( const void *pBuf, int nBytes )
  643. {
  644. return WriteBits(pBuf, nBytes << 3);
  645. }
  646. bool bf_write::WriteString(const char *pStr)
  647. {
  648. if(pStr)
  649. {
  650. do
  651. {
  652. WriteChar( *pStr );
  653. ++pStr;
  654. } while( *(pStr-1) != 0 );
  655. }
  656. else
  657. {
  658. WriteChar( 0 );
  659. }
  660. return !IsOverflowed();
  661. }
  662. // ---------------------------------------------------------------------------------------- //
  663. // bf_read
  664. // ---------------------------------------------------------------------------------------- //
  665. bf_read::bf_read()
  666. {
  667. m_pData = NULL;
  668. m_nDataBytes = 0;
  669. m_nDataBits = -1; // set to -1 so we overflow on any operation
  670. m_iCurBit = 0;
  671. m_bOverflow = false;
  672. m_bAssertOnOverflow = true;
  673. m_pDebugName = NULL;
  674. }
  675. bf_read::bf_read( const void *pData, int nBytes, int nBits )
  676. {
  677. m_bAssertOnOverflow = true;
  678. StartReading( pData, nBytes, 0, nBits );
  679. }
  680. bf_read::bf_read( const char *pDebugName, const void *pData, int nBytes, int nBits )
  681. {
  682. m_bAssertOnOverflow = true;
  683. m_pDebugName = pDebugName;
  684. StartReading( pData, nBytes, 0, nBits );
  685. }
  686. void bf_read::StartReading( const void *pData, int nBytes, int iStartBit, int nBits )
  687. {
  688. // Make sure we're dword aligned.
  689. Assert(((size_t)pData & 3) == 0);
  690. m_pData = (unsigned char*)pData;
  691. m_nDataBytes = nBytes;
  692. if ( nBits == -1 )
  693. {
  694. m_nDataBits = m_nDataBytes << 3;
  695. }
  696. else
  697. {
  698. Assert( nBits <= nBytes*8 );
  699. m_nDataBits = nBits;
  700. }
  701. m_iCurBit = iStartBit;
  702. m_bOverflow = false;
  703. }
  704. void bf_read::Reset()
  705. {
  706. m_iCurBit = 0;
  707. m_bOverflow = false;
  708. }
  709. void bf_read::SetAssertOnOverflow( bool bAssert )
  710. {
  711. m_bAssertOnOverflow = bAssert;
  712. }
  713. void bf_read::SetDebugName( const char *pName )
  714. {
  715. m_pDebugName = pName;
  716. }
  717. void bf_read::SetOverflowFlag()
  718. {
  719. if ( m_bAssertOnOverflow )
  720. {
  721. Assert( false );
  722. }
  723. m_bOverflow = true;
  724. }
  725. unsigned int bf_read::CheckReadUBitLong(int numbits)
  726. {
  727. // Ok, just read bits out.
  728. int i, nBitValue;
  729. unsigned int r = 0;
  730. for(i=0; i < numbits; i++)
  731. {
  732. nBitValue = ReadOneBitNoCheck();
  733. r |= nBitValue << i;
  734. }
  735. m_iCurBit -= numbits;
  736. return r;
  737. }
  738. void bf_read::ReadBits(void *pOutData, int nBits)
  739. {
  740. #if defined( BB_PROFILING )
  741. VPROF( "bf_read::ReadBits" );
  742. #endif
  743. unsigned char *pOut = (unsigned char*)pOutData;
  744. int nBitsLeft = nBits;
  745. // align output to dword boundary
  746. while( ((size_t)pOut & 3) != 0 && nBitsLeft >= 8 )
  747. {
  748. *pOut = (unsigned char)ReadUBitLong(8);
  749. ++pOut;
  750. nBitsLeft -= 8;
  751. }
  752. // X360TBD: Can't read dwords in ReadBits because they'll get swapped
  753. if ( IsPC() )
  754. {
  755. // read dwords
  756. while ( nBitsLeft >= 32 )
  757. {
  758. *((unsigned long*)pOut) = ReadUBitLong(32);
  759. pOut += sizeof(unsigned long);
  760. nBitsLeft -= 32;
  761. }
  762. }
  763. // read remaining bytes
  764. while ( nBitsLeft >= 8 )
  765. {
  766. *pOut = ReadUBitLong(8);
  767. ++pOut;
  768. nBitsLeft -= 8;
  769. }
  770. // read remaining bits
  771. if ( nBitsLeft )
  772. {
  773. *pOut = ReadUBitLong(nBitsLeft);
  774. }
  775. }
  776. int bf_read::ReadBitsClamped_ptr(void *pOutData, size_t outSizeBytes, size_t nBits)
  777. {
  778. size_t outSizeBits = outSizeBytes * 8;
  779. size_t readSizeBits = nBits;
  780. int skippedBits = 0;
  781. if ( readSizeBits > outSizeBits )
  782. {
  783. // Should we print a message when we clamp the data being read? Only
  784. // in debug builds I think.
  785. AssertMsg( 0, "Oversized network packet received, and clamped." );
  786. readSizeBits = outSizeBits;
  787. skippedBits = (int)( nBits - outSizeBits );
  788. // What should we do in this case, which should only happen if nBits
  789. // is negative for some reason?
  790. //if ( skippedBits < 0 )
  791. // return 0;
  792. }
  793. ReadBits( pOutData, readSizeBits );
  794. SeekRelative( skippedBits );
  795. // Return the number of bits actually read.
  796. return (int)readSizeBits;
  797. }
  798. float bf_read::ReadBitAngle( int numbits )
  799. {
  800. float fReturn;
  801. int i;
  802. float shift;
  803. shift = (float)( BitForBitnum(numbits) );
  804. i = ReadUBitLong( numbits );
  805. fReturn = (float)i * (360.0 / shift);
  806. return fReturn;
  807. }
  808. unsigned int bf_read::PeekUBitLong( int numbits )
  809. {
  810. unsigned int r;
  811. int i, nBitValue;
  812. #ifdef BIT_VERBOSE
  813. int nShifts = numbits;
  814. #endif
  815. bf_read savebf;
  816. savebf = *this; // Save current state info
  817. r = 0;
  818. for(i=0; i < numbits; i++)
  819. {
  820. nBitValue = ReadOneBit();
  821. // Append to current stream
  822. if ( nBitValue )
  823. {
  824. r |= BitForBitnum(i);
  825. }
  826. }
  827. *this = savebf;
  828. #ifdef BIT_VERBOSE
  829. Con_Printf( "PeekBitLong: %i %i\n", nShifts, (unsigned int)r );
  830. #endif
  831. return r;
  832. }
  833. unsigned int bf_read::ReadUBitLongNoInline( int numbits )
  834. {
  835. return ReadUBitLong( numbits );
  836. }
  837. unsigned int bf_read::ReadUBitVarInternal( int encodingType )
  838. {
  839. m_iCurBit -= 4;
  840. // int bits = { 4, 8, 12, 32 }[ encodingType ];
  841. int bits = 4 + encodingType*4 + (((2 - encodingType) >> 31) & 16);
  842. return ReadUBitLong( bits );
  843. }
  844. // Append numbits least significant bits from data to the current bit stream
  845. int bf_read::ReadSBitLong( int numbits )
  846. {
  847. unsigned int r = ReadUBitLong(numbits);
  848. unsigned int s = 1 << (numbits-1);
  849. if (r >= s)
  850. {
  851. // sign-extend by removing sign bit and then subtracting sign bit again
  852. r = r - s - s;
  853. }
  854. return r;
  855. }
  856. uint32 bf_read::ReadVarInt32()
  857. {
  858. uint32 result = 0;
  859. int count = 0;
  860. uint32 b;
  861. do
  862. {
  863. if ( count == bitbuf::kMaxVarint32Bytes )
  864. {
  865. return result;
  866. }
  867. b = ReadUBitLong( 8 );
  868. result |= (b & 0x7F) << (7 * count);
  869. ++count;
  870. } while (b & 0x80);
  871. return result;
  872. }
  873. uint64 bf_read::ReadVarInt64()
  874. {
  875. uint64 result = 0;
  876. int count = 0;
  877. uint64 b;
  878. do
  879. {
  880. if ( count == bitbuf::kMaxVarintBytes )
  881. {
  882. return result;
  883. }
  884. b = ReadUBitLong( 8 );
  885. result |= static_cast<uint64>(b & 0x7F) << (7 * count);
  886. ++count;
  887. } while (b & 0x80);
  888. return result;
  889. }
  890. int32 bf_read::ReadSignedVarInt32()
  891. {
  892. uint32 value = ReadVarInt32();
  893. return bitbuf::ZigZagDecode32( value );
  894. }
  895. int64 bf_read::ReadSignedVarInt64()
  896. {
  897. uint32 value = ReadVarInt64();
  898. return bitbuf::ZigZagDecode64( value );
  899. }
  900. unsigned int bf_read::ReadBitLong(int numbits, bool bSigned)
  901. {
  902. if(bSigned)
  903. return (unsigned int)ReadSBitLong(numbits);
  904. else
  905. return ReadUBitLong(numbits);
  906. }
  907. // Basic Coordinate Routines (these contain bit-field size AND fixed point scaling constants)
  908. float bf_read::ReadBitCoord (void)
  909. {
  910. #if defined( BB_PROFILING )
  911. VPROF( "bf_read::ReadBitCoord" );
  912. #endif
  913. int intval=0,fractval=0,signbit=0;
  914. float value = 0.0;
  915. // Read the required integer and fraction flags
  916. intval = ReadOneBit();
  917. fractval = ReadOneBit();
  918. // If we got either parse them, otherwise it's a zero.
  919. if ( intval || fractval )
  920. {
  921. // Read the sign bit
  922. signbit = ReadOneBit();
  923. // If there's an integer, read it in
  924. if ( intval )
  925. {
  926. // Adjust the integers from [0..MAX_COORD_VALUE-1] to [1..MAX_COORD_VALUE]
  927. intval = ReadUBitLong( COORD_INTEGER_BITS ) + 1;
  928. }
  929. // If there's a fraction, read it in
  930. if ( fractval )
  931. {
  932. fractval = ReadUBitLong( COORD_FRACTIONAL_BITS );
  933. }
  934. // Calculate the correct floating point value
  935. value = intval + ((float)fractval * COORD_RESOLUTION);
  936. // Fixup the sign if negative.
  937. if ( signbit )
  938. value = -value;
  939. }
  940. return value;
  941. }
  942. float bf_read::ReadBitCoordMP( bool bIntegral, bool bLowPrecision )
  943. {
  944. #if defined( BB_PROFILING )
  945. VPROF( "bf_read::ReadBitCoordMP" );
  946. #endif
  947. // BitCoordMP float encoding: inbounds bit, integer bit, sign bit, optional int bits, float bits
  948. // BitCoordMP integer encoding: inbounds bit, integer bit, optional sign bit, optional int bits.
  949. // int bits are always encoded as (value - 1) since zero is handled by the integer bit
  950. // With integer-only encoding, the presence of the third bit depends on the second
  951. int flags = ReadUBitLong(3 - bIntegral);
  952. enum { INBOUNDS=1, INTVAL=2, SIGN=4 };
  953. if ( bIntegral )
  954. {
  955. if ( flags & INTVAL )
  956. {
  957. // Read the third bit and the integer portion together at once
  958. unsigned int bits = ReadUBitLong( (flags & INBOUNDS) ? COORD_INTEGER_BITS_MP+1 : COORD_INTEGER_BITS+1 );
  959. // Remap from [0,N] to [1,N+1]
  960. int intval = (bits >> 1) + 1;
  961. return (bits & 1) ? -intval : intval;
  962. }
  963. return 0.f;
  964. }
  965. static const float mul_table[4] =
  966. {
  967. 1.f/(1<<COORD_FRACTIONAL_BITS),
  968. -1.f/(1<<COORD_FRACTIONAL_BITS),
  969. 1.f/(1<<COORD_FRACTIONAL_BITS_MP_LOWPRECISION),
  970. -1.f/(1<<COORD_FRACTIONAL_BITS_MP_LOWPRECISION)
  971. };
  972. //equivalent to: float multiply = mul_table[ ((flags & SIGN) ? 1 : 0) + bLowPrecision*2 ];
  973. float multiply = *(float*)((uintptr_t)&mul_table[0] + (flags & 4) + bLowPrecision*8);
  974. static const unsigned char numbits_table[8] =
  975. {
  976. COORD_FRACTIONAL_BITS,
  977. COORD_FRACTIONAL_BITS,
  978. COORD_FRACTIONAL_BITS + COORD_INTEGER_BITS,
  979. COORD_FRACTIONAL_BITS + COORD_INTEGER_BITS_MP,
  980. COORD_FRACTIONAL_BITS_MP_LOWPRECISION,
  981. COORD_FRACTIONAL_BITS_MP_LOWPRECISION,
  982. COORD_FRACTIONAL_BITS_MP_LOWPRECISION + COORD_INTEGER_BITS,
  983. COORD_FRACTIONAL_BITS_MP_LOWPRECISION + COORD_INTEGER_BITS_MP
  984. };
  985. unsigned int bits = ReadUBitLong( numbits_table[ (flags & (INBOUNDS|INTVAL)) + bLowPrecision*4 ] );
  986. if ( flags & INTVAL )
  987. {
  988. // Shuffle the bits to remap the integer portion from [0,N] to [1,N+1]
  989. // and then paste in front of the fractional parts so we only need one
  990. // int-to-float conversion.
  991. uint fracbitsMP = bits >> COORD_INTEGER_BITS_MP;
  992. uint fracbits = bits >> COORD_INTEGER_BITS;
  993. uint intmaskMP = ((1<<COORD_INTEGER_BITS_MP)-1);
  994. uint intmask = ((1<<COORD_INTEGER_BITS)-1);
  995. uint selectNotMP = (flags & INBOUNDS) - 1;
  996. fracbits -= fracbitsMP;
  997. fracbits &= selectNotMP;
  998. fracbits += fracbitsMP;
  999. intmask -= intmaskMP;
  1000. intmask &= selectNotMP;
  1001. intmask += intmaskMP;
  1002. uint intpart = (bits & intmask) + 1;
  1003. uint intbitsLow = intpart << COORD_FRACTIONAL_BITS_MP_LOWPRECISION;
  1004. uint intbits = intpart << COORD_FRACTIONAL_BITS;
  1005. uint selectNotLow = (uint)bLowPrecision - 1;
  1006. intbits -= intbitsLow;
  1007. intbits &= selectNotLow;
  1008. intbits += intbitsLow;
  1009. bits = fracbits | intbits;
  1010. }
  1011. return (int)bits * multiply;
  1012. }
  1013. unsigned int bf_read::ReadBitCoordBits (void)
  1014. {
  1015. #if defined( BB_PROFILING )
  1016. VPROF( "bf_read::ReadBitCoordBits" );
  1017. #endif
  1018. unsigned int flags = ReadUBitLong(2);
  1019. if ( flags == 0 )
  1020. return 0;
  1021. static const int numbits_table[3] =
  1022. {
  1023. COORD_INTEGER_BITS + 1,
  1024. COORD_FRACTIONAL_BITS + 1,
  1025. COORD_INTEGER_BITS + COORD_FRACTIONAL_BITS + 1
  1026. };
  1027. return ReadUBitLong( numbits_table[ flags-1 ] ) * 4 + flags;
  1028. }
  1029. unsigned int bf_read::ReadBitCoordMPBits( bool bIntegral, bool bLowPrecision )
  1030. {
  1031. #if defined( BB_PROFILING )
  1032. VPROF( "bf_read::ReadBitCoordMPBits" );
  1033. #endif
  1034. unsigned int flags = ReadUBitLong(2);
  1035. enum { INBOUNDS=1, INTVAL=2 };
  1036. int numbits = 0;
  1037. if ( bIntegral )
  1038. {
  1039. if ( flags & INTVAL )
  1040. {
  1041. numbits = (flags & INBOUNDS) ? (1 + COORD_INTEGER_BITS_MP) : (1 + COORD_INTEGER_BITS);
  1042. }
  1043. else
  1044. {
  1045. return flags; // no extra bits
  1046. }
  1047. }
  1048. else
  1049. {
  1050. static const unsigned char numbits_table[8] =
  1051. {
  1052. 1 + COORD_FRACTIONAL_BITS,
  1053. 1 + COORD_FRACTIONAL_BITS,
  1054. 1 + COORD_FRACTIONAL_BITS + COORD_INTEGER_BITS,
  1055. 1 + COORD_FRACTIONAL_BITS + COORD_INTEGER_BITS_MP,
  1056. 1 + COORD_FRACTIONAL_BITS_MP_LOWPRECISION,
  1057. 1 + COORD_FRACTIONAL_BITS_MP_LOWPRECISION,
  1058. 1 + COORD_FRACTIONAL_BITS_MP_LOWPRECISION + COORD_INTEGER_BITS,
  1059. 1 + COORD_FRACTIONAL_BITS_MP_LOWPRECISION + COORD_INTEGER_BITS_MP
  1060. };
  1061. numbits = numbits_table[ flags + bLowPrecision*4 ];
  1062. }
  1063. return flags + ReadUBitLong(numbits)*4;
  1064. }
  1065. void bf_read::ReadBitVec3Coord( Vector& fa )
  1066. {
  1067. int xflag, yflag, zflag;
  1068. // This vector must be initialized! Otherwise, If any of the flags aren't set,
  1069. // the corresponding component will not be read and will be stack garbage.
  1070. fa.Init( 0, 0, 0 );
  1071. xflag = ReadOneBit();
  1072. yflag = ReadOneBit();
  1073. zflag = ReadOneBit();
  1074. if ( xflag )
  1075. fa[0] = ReadBitCoord();
  1076. if ( yflag )
  1077. fa[1] = ReadBitCoord();
  1078. if ( zflag )
  1079. fa[2] = ReadBitCoord();
  1080. }
  1081. float bf_read::ReadBitNormal (void)
  1082. {
  1083. // Read the sign bit
  1084. int signbit = ReadOneBit();
  1085. // Read the fractional part
  1086. unsigned int fractval = ReadUBitLong( NORMAL_FRACTIONAL_BITS );
  1087. // Calculate the correct floating point value
  1088. float value = (float)fractval * NORMAL_RESOLUTION;
  1089. // Fixup the sign if negative.
  1090. if ( signbit )
  1091. value = -value;
  1092. return value;
  1093. }
  1094. void bf_read::ReadBitVec3Normal( Vector& fa )
  1095. {
  1096. int xflag = ReadOneBit();
  1097. int yflag = ReadOneBit();
  1098. if (xflag)
  1099. fa[0] = ReadBitNormal();
  1100. else
  1101. fa[0] = 0.0f;
  1102. if (yflag)
  1103. fa[1] = ReadBitNormal();
  1104. else
  1105. fa[1] = 0.0f;
  1106. // The first two imply the third (but not its sign)
  1107. int znegative = ReadOneBit();
  1108. float fafafbfb = fa[0] * fa[0] + fa[1] * fa[1];
  1109. if (fafafbfb < 1.0f)
  1110. fa[2] = sqrt( 1.0f - fafafbfb );
  1111. else
  1112. fa[2] = 0.0f;
  1113. if (znegative)
  1114. fa[2] = -fa[2];
  1115. }
  1116. void bf_read::ReadBitAngles( QAngle& fa )
  1117. {
  1118. Vector tmp;
  1119. ReadBitVec3Coord( tmp );
  1120. fa.Init( tmp.x, tmp.y, tmp.z );
  1121. }
  1122. int64 bf_read::ReadLongLong()
  1123. {
  1124. int64 retval;
  1125. uint *pLongs = (uint*)&retval;
  1126. // Read the two DWORDs according to network endian
  1127. const short endianIndex = 0x0100;
  1128. byte *idx = (byte*)&endianIndex;
  1129. pLongs[*idx++] = ReadUBitLong(sizeof(long) << 3);
  1130. pLongs[*idx] = ReadUBitLong(sizeof(long) << 3);
  1131. return retval;
  1132. }
  1133. float bf_read::ReadFloat()
  1134. {
  1135. float ret;
  1136. Assert( sizeof(ret) == 4 );
  1137. ReadBits(&ret, 32);
  1138. // Swap the float, since ReadBits reads raw data
  1139. LittleFloat( &ret, &ret );
  1140. return ret;
  1141. }
  1142. bool bf_read::ReadBytes(void *pOut, int nBytes)
  1143. {
  1144. ReadBits(pOut, nBytes << 3);
  1145. return !IsOverflowed();
  1146. }
  1147. bool bf_read::ReadString( char *pStr, int maxLen, bool bLine, int *pOutNumChars )
  1148. {
  1149. Assert( maxLen != 0 );
  1150. bool bTooSmall = false;
  1151. int iChar = 0;
  1152. while(1)
  1153. {
  1154. char val = ReadChar();
  1155. if ( val == 0 )
  1156. break;
  1157. else if ( bLine && val == '\n' )
  1158. break;
  1159. if ( iChar < (maxLen-1) )
  1160. {
  1161. pStr[iChar] = val;
  1162. ++iChar;
  1163. }
  1164. else
  1165. {
  1166. bTooSmall = true;
  1167. }
  1168. }
  1169. // Make sure it's null-terminated.
  1170. Assert( iChar < maxLen );
  1171. pStr[iChar] = 0;
  1172. if ( pOutNumChars )
  1173. *pOutNumChars = iChar;
  1174. return !IsOverflowed() && !bTooSmall;
  1175. }
  1176. char* bf_read::ReadAndAllocateString( bool *pOverflow )
  1177. {
  1178. char str[2048];
  1179. int nChars;
  1180. bool bOverflow = !ReadString( str, sizeof( str ), false, &nChars );
  1181. if ( pOverflow )
  1182. *pOverflow = bOverflow;
  1183. // Now copy into the output and return it;
  1184. char *pRet = new char[ nChars + 1 ];
  1185. for ( int i=0; i <= nChars; i++ )
  1186. pRet[i] = str[i];
  1187. return pRet;
  1188. }
  1189. void bf_read::ExciseBits( int startbit, int bitstoremove )
  1190. {
  1191. int endbit = startbit + bitstoremove;
  1192. int remaining_to_end = m_nDataBits - endbit;
  1193. bf_write temp;
  1194. temp.StartWriting( (void *)m_pData, m_nDataBits << 3, startbit );
  1195. Seek( endbit );
  1196. for ( int i = 0; i < remaining_to_end; i++ )
  1197. {
  1198. temp.WriteOneBit( ReadOneBit() );
  1199. }
  1200. Seek( startbit );
  1201. m_nDataBits -= bitstoremove;
  1202. m_nDataBytes = m_nDataBits >> 3;
  1203. }
  1204. int bf_read::CompareBitsAt( int offset, bf_read * RESTRICT other, int otherOffset, int numbits ) RESTRICT
  1205. {
  1206. extern unsigned long g_ExtraMasks[33];
  1207. if ( numbits == 0 )
  1208. return 0;
  1209. int overflow1 = offset + numbits > m_nDataBits;
  1210. int overflow2 = otherOffset + numbits > other->m_nDataBits;
  1211. int x = overflow1 | overflow2;
  1212. if ( x != 0 )
  1213. return x;
  1214. unsigned int iStartBit1 = offset & 31u;
  1215. unsigned int iStartBit2 = otherOffset & 31u;
  1216. unsigned long *pData1 = (unsigned long*)m_pData + (offset >> 5);
  1217. unsigned long *pData2 = (unsigned long*)other->m_pData + (otherOffset >> 5);
  1218. unsigned long *pData1End = pData1 + ((offset + numbits - 1) >> 5);
  1219. unsigned long *pData2End = pData2 + ((otherOffset + numbits - 1) >> 5);
  1220. while ( numbits > 32 )
  1221. {
  1222. x = LoadLittleDWord( (unsigned long*)pData1, 0 ) >> iStartBit1;
  1223. x ^= LoadLittleDWord( (unsigned long*)pData1, 1 ) << (32 - iStartBit1);
  1224. x ^= LoadLittleDWord( (unsigned long*)pData2, 0 ) >> iStartBit2;
  1225. x ^= LoadLittleDWord( (unsigned long*)pData2, 1 ) << (32 - iStartBit2);
  1226. if ( x != 0 )
  1227. {
  1228. return x;
  1229. }
  1230. ++pData1;
  1231. ++pData2;
  1232. numbits -= 32;
  1233. }
  1234. x = LoadLittleDWord( (unsigned long*)pData1, 0 ) >> iStartBit1;
  1235. x ^= LoadLittleDWord( (unsigned long*)pData1End, 0 ) << (32 - iStartBit1);
  1236. x ^= LoadLittleDWord( (unsigned long*)pData2, 0 ) >> iStartBit2;
  1237. x ^= LoadLittleDWord( (unsigned long*)pData2End, 0 ) << (32 - iStartBit2);
  1238. return x & g_ExtraMasks[ numbits ];
  1239. }