This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients vstinner
Date 2008-11-10.13:12:25
SpamBayes Score 1.9503577e-07
Marked as misclassified No
Message-id <1226322752.92.0.0671581209413.issue4294@psf.upfronthosting.co.za>
In-reply-to
Content
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) {
        ...
History
Date User Action Args
2008-11-10 13:12:33vstinnersetrecipients: + vstinner
2008-11-10 13:12:32vstinnersetmessageid: <1226322752.92.0.0671581209413.issue4294@psf.upfronthosting.co.za>
2008-11-10 13:12:32vstinnerlinkissue4294 messages
2008-11-10 13:12:30vstinnercreate