_PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's

truly needed; usually saves a little time, but no change in semantics.
This commit is contained in:
Tim Peters 2001-06-13 21:01:27 +00:00
parent 898cf85c25
commit 05607ad4fd

View file

@ -424,18 +424,27 @@ _PyLong_AsByteArray(PyLongObject* v,
*p = (unsigned char)(accum & 0xff); *p = (unsigned char)(accum & 0xff);
p += pincr; p += pincr;
} }
else if (j == n && n > 0 && is_signed) {
/* Fill remaining bytes with copies of the sign bit. */ /* The main loop filled the byte array exactly, so the code
for ( ; j < n; ++j, p += pincr) just above didn't get to ensure there's a sign bit, and the
*p = (unsigned char)(do_twos_comp ? 0xff : 0); loop below wouldn't add one either. Make sure a sign bit
exists. */
/* Check for delicate overflow (not enough room for the sign bit). */
if (j > 0 && is_signed) {
unsigned char msb = *(p - pincr); unsigned char msb = *(p - pincr);
int sign_bit_set = (msb & 0x80) != 0; int sign_bit_set = msb >= 0x80;
if (sign_bit_set != do_twos_comp) assert(accumbits == 0);
if (sign_bit_set == do_twos_comp)
return 0;
else
goto Overflow; goto Overflow;
} }
/* Fill remaining bytes with copies of the sign bit. */
{
unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
for ( ; j < n; ++j, p += pincr)
*p = signbyte;
}
return 0; return 0;
Overflow: Overflow: