Commit ede05091 authored by Tim Peters's avatar Tim Peters

_PyLong_AsByteArray: simplify the logic for dealing with the most-

significant digits sign bits.  Again no change in semantics.
parent ce9de2f7
...@@ -388,7 +388,6 @@ _PyLong_AsByteArray(PyLongObject* v, ...@@ -388,7 +388,6 @@ _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 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;
...@@ -399,22 +398,25 @@ _PyLong_AsByteArray(PyLongObject* v, ...@@ -399,22 +398,25 @@ _PyLong_AsByteArray(PyLongObject* v,
significant than what's already in accum, so needs to be significant than what's already in accum, so needs to be
prepended to accum. */ prepended to accum. */
accum |= thisdigit << accumbits; accum |= thisdigit << accumbits;
accumbits += SHIFT;
/* How many new bits did we add? The most-significant digit /* The most-significant digit may be (probably is) at least
may be (probably is) at least partly empty. */ partly empty. */
if (i == ndigits - 1) { if (i == ndigits - 1) {
twodigits bitmask = 1 << (SHIFT - 1); /* Count # of sign bits -- they needn't be stored,
twodigits signbit = do_twos_comp << (SHIFT - 1); * although for signed conversion we need later to
* make sure at least one sign bit gets stored.
* First shift conceptual sign bit to real sign bit.
*/
stwodigits s = (stwodigits)(thisdigit <<
(8*sizeof(stwodigits) - SHIFT));
unsigned int nsignbits = 0; unsigned int nsignbits = 0;
while ((thisdigit & bitmask) == signbit && bitmask) { while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
++nsignbits; ++nsignbits;
bitmask >>= 1; s <<= 1;
signbit >>= 1;
} }
assert(nsignbits <= SHIFT); accumbits -= nsignbits;
numnewbits -= nsignbits;
} }
accumbits += numnewbits;
/* Store as many bytes as possible. */ /* Store as many bytes as possible. */
while (accumbits >= 8) { while (accumbits >= 8) {
......
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