Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm

This commit is contained in:
Bob Ippolito 2006-05-25 18:20:23 +00:00
parent 598710c727
commit a85bf202ac
1 changed files with 50 additions and 10 deletions

View File

@ -844,11 +844,36 @@ PyLong_AsVoidPtr(PyObject *vv)
PyObject * PyObject *
PyLong_FromLongLong(PY_LONG_LONG ival) PyLong_FromLongLong(PY_LONG_LONG ival)
{ {
PY_LONG_LONG bytes = ival; PyLongObject *v;
int one = 1; unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
return _PyLong_FromByteArray( int ndigits = 0;
(unsigned char *)&bytes, int negative = 0;
SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
if (ival < 0) {
ival = -ival;
negative = 1;
}
/* Count the number of Python digits.
We used to pick 5 ("big enough for anything"), but that's a
waste of time and space given that 5*15 = 75 bits are rarely
needed. */
t = (unsigned PY_LONG_LONG)ival;
while (t) {
++ndigits;
t >>= SHIFT;
}
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->ob_digit;
v->ob_size = negative ? -ndigits : ndigits;
t = (unsigned PY_LONG_LONG)ival;
while (t) {
*p++ = (digit)(t & MASK);
t >>= SHIFT;
}
}
return (PyObject *)v;
} }
/* Create a new long int object from a C unsigned PY_LONG_LONG int. */ /* Create a new long int object from a C unsigned PY_LONG_LONG int. */
@ -856,11 +881,26 @@ PyLong_FromLongLong(PY_LONG_LONG ival)
PyObject * PyObject *
PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
{ {
unsigned PY_LONG_LONG bytes = ival; PyLongObject *v;
int one = 1; unsigned PY_LONG_LONG t;
return _PyLong_FromByteArray( int ndigits = 0;
(unsigned char *)&bytes,
SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); /* Count the number of Python digits. */
t = (unsigned PY_LONG_LONG)ival;
while (t) {
++ndigits;
t >>= SHIFT;
}
v = _PyLong_New(ndigits);
if (v != NULL) {
digit *p = v->ob_digit;
v->ob_size = ndigits;
while (ival) {
*p++ = (digit)(ival & MASK);
ival >>= SHIFT;
}
}
return (PyObject *)v;
} }
/* Create a new long int object from a C Py_ssize_t. */ /* Create a new long int object from a C Py_ssize_t. */