Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8bc84b4

Browse files
committed
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
clarity. Should have no effect visible to callers.
1 parent 83213cc commit 8bc84b4

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

Objects/longobject.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
284284
const unsigned char* p = pstartbyte;
285285

286286
for (i = 0; i < numsignificantbytes; ++i, p += incr) {
287-
unsigned int thisbyte = *p;
287+
twodigits thisbyte = *p;
288288
/* Compute correction for 2's comp, if needed. */
289289
if (is_signed) {
290290
thisbyte = (0xff ^ thisbyte) + carry;
@@ -364,17 +364,21 @@ _PyLong_AsByteArray(PyLongObject* v,
364364
accumbits = 0;
365365
carry = do_twos_comp ? 1 : 0;
366366
for (i = 0; i < ndigits; ++i) {
367-
unsigned int oldaccumbits = accumbits;
367+
unsigned int numnewbits = SHIFT;
368368
twodigits thisdigit = v->ob_digit[i];
369369
if (do_twos_comp) {
370370
thisdigit = (thisdigit ^ MASK) + carry;
371371
carry = thisdigit >> SHIFT;
372372
thisdigit &= MASK;
373373
}
374-
if (i < ndigits - 1)
375-
accumbits += SHIFT;
376-
else {
377-
/* The most-significant digit may be partly empty. */
374+
/* Because we're going LSB to MSB, thisdigit is more
375+
significant than what's already in accum, so needs to be
376+
prepended to accum. */
377+
accum |= thisdigit << accumbits;
378+
379+
/* How many new bits did we add? The most-significant digit
380+
may be (probably is) at least partly empty. */
381+
if (i == ndigits - 1) {
378382
twodigits bitmask = 1 << (SHIFT - 1);
379383
twodigits signbit = do_twos_comp << (SHIFT - 1);
380384
unsigned int nsignbits = 0;
@@ -383,12 +387,11 @@ _PyLong_AsByteArray(PyLongObject* v,
383387
bitmask >>= 1;
384388
signbit >>= 1;
385389
}
386-
accumbits += SHIFT - nsignbits;
390+
assert(nsignbits <= SHIFT);
391+
numnewbits -= nsignbits;
387392
}
388-
/* Because we're going LSB to MSB, thisdigit is more
389-
significant than what's already in accum, so needs to be
390-
prepended to accum. */
391-
accum |= thisdigit << oldaccumbits;
393+
accumbits += numnewbits;
394+
392395
/* Store as many bytes as possible. */
393396
while (accumbits >= 8) {
394397
if (j >= n)

0 commit comments

Comments
 (0)