Refactor CVariableInt::Pack

This commit is contained in:
Robert Müller 2021-12-27 13:17:39 +01:00 committed by Robert Müller
parent 839aa15e69
commit 6337a4c2a9

View file

@ -7,23 +7,21 @@
// Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign
unsigned char *CVariableInt::Pack(unsigned char *pDst, int i)
{
*pDst = (i >> 25) & 0x40; // set sign bit if i<0
i ^= i >> 31; // if(i<0) i = ~i
*pDst = 0;
if(i < 0)
{
*pDst |= 0x40; // set sign bit
i = ~i;
}
*pDst |= i & 0x3F; // pack 6bit into dst
i >>= 6; // discard 6 bits
if(i)
while(i)
{
*pDst |= 0x80; // set extend bit
while(true)
{
pDst++;
*pDst = i & 0x7F; // pack 7bit
i >>= 7; // discard 7 bits
*pDst |= (i != 0) << 7; // set extend bit (may branch)
if(!i)
break;
}
}
pDst++;