Message75694
It's hard to read Objects/longobject.c because the code depends to
much on the implementation. I wrote macros to simplify the code:
- PyLong_SIGN(x)
- PyLong_EQUALS_ZERO(x)
- PyLong_FITS_INT(x)
- PyLong_GET_INT(x)
- PyLong_NDIGITS(x)
Macros are declared in Include/longintrepr.h to be able to use them
outside longobject.c. Eg. I used it in Modules/mathmodule.c.
The patch also contains extra changes:
- create sdigit type (used by PyLong_GET_INT(x))
- use memcpy() in _PyLong_Copy()
- use stwodigits in long_mul()
- replace many Py_SIZE() hacks by my macros (eg. see long_hash)
Using my patch, it will be easier to change long integer
implementation, like:
- Use 30-bit digits instead of 15-bit digits (issue #4258)
- Use GMP for PyLong (issue #1814)
Example of changes:
i = Py_SIZE(v);
x = 0;
if (i < 0) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to unsigned
int");
return (unsigned long) -1;
}
switch (i) {
case 0: return 0;
case 1: return v->ob_digit[0];
}
while (--i >= 0) {
...
is replaced by:
if (PyLong_SIGN(v) < 0) {
PyErr_SetString(PyExc_OverflowError,
"can't convert negative value to unsigned
int");
return (unsigned long) -1;
}
if (PyLong_FITS_INT(v))
return (unsigned long) PyLong_GET_INT(v);
x = 0;
i = PyLong_NDIGITS(v);
while (--i >= 0) {
... |
|
Date |
User |
Action |
Args |
2008-11-10 13:12:33 | vstinner | set | recipients:
+ vstinner |
2008-11-10 13:12:32 | vstinner | set | messageid: <1226322752.92.0.0671581209413.issue4294@psf.upfronthosting.co.za> |
2008-11-10 13:12:32 | vstinner | link | issue4294 messages |
2008-11-10 13:12:30 | vstinner | create | |
|