_PyLong_NumBits(): The definition of this was too specific to the quirky

needs of pickling longs.  Backed off to a definition that's much easier
to understand.  The pickler will have to work a little harder, but other
uses are more likely to be correct <0.5 wink>.

_PyLong_Sign():  New teensy function to characterize a long, as to <0, ==0,
or >0.
This commit is contained in:
Tim Peters 2003-01-31 15:52:05 +00:00
parent 89fc4f3e56
commit 5b8132ffa3
3 changed files with 55 additions and 34 deletions

View file

@ -260,25 +260,34 @@ PyLong_AsUnsignedLong(PyObject *vv)
return x;
}
int
_PyLong_Sign(PyObject *vv)
{
PyLongObject *v = (PyLongObject *)vv;
const int ndigits = v->ob_size;
assert(v != NULL);
assert(PyLong_Check(v));
assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
return ndigits == 0 ? 0 : (ndigits < 0 ? -1 : 1);
}
size_t
_PyLong_NumBits(PyObject *vv)
{
PyLongObject *v = (PyLongObject *)vv;
size_t result = 1; /* for the sign bit */
size_t ndigits = ABS(v->ob_size);
size_t result = 0;
int ndigits = ABS(v->ob_size);
assert(v != NULL);
assert(PyLong_Check(v));
assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
if (ndigits > 0) {
size_t product;
digit msd = v->ob_digit[ndigits - 1];
product = (ndigits - 1) * SHIFT;
if (product / SHIFT != ndigits - 1)
goto Overflow;
result += product;
if (result < product)
result = (ndigits - 1) * SHIFT;
if (result / SHIFT != ndigits - 1)
goto Overflow;
do {
++result;