Commit 8bc84b43 authored by Tim Peters's avatar Tim Peters

_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving

clarity.  Should have no effect visible to callers.
parent 83213cc0
...@@ -284,7 +284,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n, ...@@ -284,7 +284,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
const unsigned char* p = pstartbyte; const unsigned char* p = pstartbyte;
for (i = 0; i < numsignificantbytes; ++i, p += incr) { for (i = 0; i < numsignificantbytes; ++i, p += incr) {
unsigned int thisbyte = *p; twodigits thisbyte = *p;
/* Compute correction for 2's comp, if needed. */ /* Compute correction for 2's comp, if needed. */
if (is_signed) { if (is_signed) {
thisbyte = (0xff ^ thisbyte) + carry; thisbyte = (0xff ^ thisbyte) + carry;
...@@ -364,17 +364,21 @@ _PyLong_AsByteArray(PyLongObject* v, ...@@ -364,17 +364,21 @@ _PyLong_AsByteArray(PyLongObject* v,
accumbits = 0; accumbits = 0;
carry = do_twos_comp ? 1 : 0; carry = do_twos_comp ? 1 : 0;
for (i = 0; i < ndigits; ++i) { for (i = 0; i < ndigits; ++i) {
unsigned int oldaccumbits = accumbits; unsigned int numnewbits = SHIFT;
twodigits thisdigit = v->ob_digit[i]; twodigits thisdigit = v->ob_digit[i];
if (do_twos_comp) { if (do_twos_comp) {
thisdigit = (thisdigit ^ MASK) + carry; thisdigit = (thisdigit ^ MASK) + carry;
carry = thisdigit >> SHIFT; carry = thisdigit >> SHIFT;
thisdigit &= MASK; thisdigit &= MASK;
} }
if (i < ndigits - 1) /* Because we're going LSB to MSB, thisdigit is more
accumbits += SHIFT; significant than what's already in accum, so needs to be
else { prepended to accum. */
/* The most-significant digit may be partly empty. */ accum |= thisdigit << accumbits;
/* How many new bits did we add? The most-significant digit
may be (probably is) at least partly empty. */
if (i == ndigits - 1) {
twodigits bitmask = 1 << (SHIFT - 1); twodigits bitmask = 1 << (SHIFT - 1);
twodigits signbit = do_twos_comp << (SHIFT - 1); twodigits signbit = do_twos_comp << (SHIFT - 1);
unsigned int nsignbits = 0; unsigned int nsignbits = 0;
...@@ -383,12 +387,11 @@ _PyLong_AsByteArray(PyLongObject* v, ...@@ -383,12 +387,11 @@ _PyLong_AsByteArray(PyLongObject* v,
bitmask >>= 1; bitmask >>= 1;
signbit >>= 1; signbit >>= 1;
} }
accumbits += SHIFT - nsignbits; assert(nsignbits <= SHIFT);
numnewbits -= nsignbits;
} }
/* Because we're going LSB to MSB, thisdigit is more accumbits += numnewbits;
significant than what's already in accum, so needs to be
prepended to accum. */
accum |= thisdigit << oldaccumbits;
/* Store as many bytes as possible. */ /* Store as many bytes as possible. */
while (accumbits >= 8) { while (accumbits >= 8) {
if (j >= n) if (j >= n)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment