| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1 | /* Long (arbitrary precision) integer object implementation */ | 
 | 2 |  | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3 | /* XXX The functional organization of this file is terrible */ | 
 | 4 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5 | #include "Python.h" | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 6 | #include "longintrepr.h" | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 7 |  | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 8 | #include "formatter_unicode.h" | 
 | 9 |  | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 10 | #include <ctype.h> | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 11 |  | 
| Guido van Rossum | 2220122 | 2007-08-07 22:02:18 +0000 | [diff] [blame] | 12 | long | 
 | 13 | PyInt_GetMax(void) | 
 | 14 | { | 
 | 15 | 	return LONG_MAX;	/* To initialize sys.maxint */ | 
 | 16 | } | 
 | 17 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 18 | #ifndef NSMALLPOSINTS | 
 | 19 | #define NSMALLPOSINTS		257 | 
 | 20 | #endif | 
 | 21 | #ifndef NSMALLNEGINTS | 
 | 22 | #define NSMALLNEGINTS		5 | 
 | 23 | #endif | 
 | 24 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 
 | 25 | /* Small integers are preallocated in this array so that they | 
 | 26 |    can be shared. | 
 | 27 |    The integers that are preallocated are those in the range | 
 | 28 |    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). | 
 | 29 | */ | 
 | 30 | static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; | 
 | 31 | #ifdef COUNT_ALLOCS | 
 | 32 | int quick_int_allocs, quick_neg_int_allocs; | 
 | 33 | #endif | 
 | 34 |  | 
| Guido van Rossum | 7eaf822 | 2007-06-18 17:58:50 +0000 | [diff] [blame] | 35 | static PyObject * | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 36 | get_small_int(int ival) | 
 | 37 | { | 
 | 38 | 	PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); | 
 | 39 | 	Py_INCREF(v); | 
 | 40 | #ifdef COUNT_ALLOCS | 
 | 41 | 	if (ival >= 0) | 
 | 42 | 		quick_int_allocs++; | 
 | 43 | 	else | 
 | 44 | 		quick_neg_int_allocs++; | 
 | 45 | #endif | 
 | 46 | 	return v; | 
 | 47 | } | 
 | 48 | #define CHECK_SMALL_INT(ival) \ | 
 | 49 | 	do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ | 
 | 50 | 		return get_small_int(ival); \ | 
 | 51 | 	} while(0) | 
 | 52 |  | 
 | 53 | #else | 
 | 54 | #define CHECK_SMALL_INT(ival) | 
 | 55 | #endif | 
 | 56 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 57 | #define MEDIUM_VALUE(x) (Py_Size(x) < 0 ? -(x)->ob_digit[0] : (Py_Size(x) == 0 ? 0 : (x)->ob_digit[0])) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 58 | /* If a freshly-allocated long is already shared, it must | 
 | 59 |    be a small integer, so negating it must go to PyLong_FromLong */ | 
 | 60 | #define NEGATE(x) \ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 61 | 	do if (Py_Refcnt(x) == 1) Py_Size(x) = -Py_Size(x);  \ | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 62 | 	   else { PyObject* tmp=PyInt_FromLong(-MEDIUM_VALUE(x));  \ | 
 | 63 | 		   Py_DECREF(x); (x) = (PyLongObject*)tmp; }	   \ | 
 | 64 |         while(0) | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 65 | /* For long multiplication, use the O(N**2) school algorithm unless | 
 | 66 |  * both operands contain more than KARATSUBA_CUTOFF digits (this | 
 | 67 |  * being an internal Python long digit, in base BASE). | 
 | 68 |  */ | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 69 | #define KARATSUBA_CUTOFF 70 | 
 | 70 | #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 71 |  | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 72 | /* For exponentiation, use the binary left-to-right algorithm | 
 | 73 |  * unless the exponent contains more than FIVEARY_CUTOFF digits. | 
 | 74 |  * In that case, do 5 bits at a time.  The potential drawback is that | 
 | 75 |  * a table of 2**5 intermediate results is computed. | 
 | 76 |  */ | 
 | 77 | #define FIVEARY_CUTOFF 8 | 
 | 78 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 79 | #define ABS(x) ((x) < 0 ? -(x) : (x)) | 
 | 80 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 81 | #undef MIN | 
 | 82 | #undef MAX | 
 | 83 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) | 
 | 84 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) | 
 | 85 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 86 | /* Forward */ | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 87 | static PyLongObject *long_normalize(PyLongObject *); | 
 | 88 | static PyLongObject *mul1(PyLongObject *, wdigit); | 
 | 89 | static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 90 | static PyLongObject *divrem1(PyLongObject *, digit, digit *); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 91 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 92 | #define SIGCHECK(PyTryBlock) \ | 
| Skip Montanaro | d581d77 | 2002-09-03 20:10:45 +0000 | [diff] [blame] | 93 | 	if (--_Py_Ticker < 0) { \ | 
 | 94 | 		_Py_Ticker = _Py_CheckInterval; \ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 95 | 		if (PyErr_CheckSignals()) PyTryBlock \ | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 96 | 	} | 
 | 97 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 98 | /* Normalize (remove leading zeros from) a long int object. | 
 | 99 |    Doesn't attempt to free the storage--in most cases, due to the nature | 
 | 100 |    of the algorithms used, this could save at most be one word anyway. */ | 
 | 101 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 103 | long_normalize(register PyLongObject *v) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 104 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 105 | 	Py_ssize_t j = ABS(Py_Size(v)); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 106 | 	Py_ssize_t i = j; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 107 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 108 | 	while (i > 0 && v->ob_digit[i-1] == 0) | 
 | 109 | 		--i; | 
 | 110 | 	if (i != j) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 111 | 		Py_Size(v) = (Py_Size(v) < 0) ? -(i) : i; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 112 | 	return v; | 
 | 113 | } | 
 | 114 |  | 
 | 115 | /* Allocate a new long int object with size digits. | 
 | 116 |    Return NULL and set exception if we run out of memory. */ | 
 | 117 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | PyLongObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 119 | _PyLong_New(Py_ssize_t size) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 120 | { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 121 | 	PyLongObject *result; | 
 | 122 | 	/* Can't use sizeof(PyLongObject) here, since the | 
 | 123 | 	   compiler takes padding at the end into account. | 
 | 124 | 	   As the consequence, this would waste 2 bytes on | 
 | 125 | 	   a 32-bit system, and 6 bytes on a 64-bit system. | 
 | 126 | 	   This computation would be incorrect on systems | 
 | 127 | 	   which have padding before the digits; with 16-bit | 
 | 128 | 	   digits this should not happen. */ | 
 | 129 | 	result = PyObject_MALLOC(sizeof(PyVarObject) +  | 
 | 130 | 				 size*sizeof(digit)); | 
 | 131 | 	if (!result) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 132 | 		PyErr_NoMemory(); | 
 | 133 | 		return NULL; | 
 | 134 | 	} | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 135 | 	return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 136 | } | 
 | 137 |  | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 138 | PyObject * | 
 | 139 | _PyLong_Copy(PyLongObject *src) | 
 | 140 | { | 
 | 141 | 	PyLongObject *result; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 142 | 	Py_ssize_t i; | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 143 |  | 
 | 144 | 	assert(src != NULL); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 145 | 	i = Py_Size(src); | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 146 | 	if (i < 0) | 
 | 147 | 		i = -(i); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 148 | 	if (i < 2) { | 
 | 149 | 		int ival = src->ob_digit[0]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 150 | 		if (Py_Size(src) < 0) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 151 | 			ival = -ival; | 
 | 152 | 		CHECK_SMALL_INT(ival); | 
 | 153 | 	} | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 154 | 	result = _PyLong_New(i); | 
 | 155 | 	if (result != NULL) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 156 | 		Py_Size(result) = Py_Size(src); | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 157 | 		while (--i >= 0) | 
 | 158 | 			result->ob_digit[i] = src->ob_digit[i]; | 
 | 159 | 	} | 
 | 160 | 	return (PyObject *)result; | 
 | 161 | } | 
 | 162 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 163 | /* Create a new long int object from a C long int */ | 
 | 164 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 166 | PyLong_FromLong(long ival) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 167 | { | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 168 | 	PyLongObject *v; | 
 | 169 | 	unsigned long t;  /* unsigned so >> doesn't propagate sign bit */ | 
 | 170 | 	int ndigits = 0; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 171 | 	int sign = 1; | 
 | 172 |  | 
 | 173 | 	CHECK_SMALL_INT(ival); | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 174 |  | 
 | 175 | 	if (ival < 0) { | 
 | 176 | 		ival = -ival; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 177 | 		sign = -1; | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 178 | 	} | 
 | 179 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 180 | 	/* Fast path for single-digits ints */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 181 | 	if (!(ival>>PyLong_SHIFT)) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 182 | 		v = _PyLong_New(1); | 
 | 183 | 		if (v) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 184 | 			Py_Size(v) = sign; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 185 | 			v->ob_digit[0] = ival; | 
 | 186 | 		} | 
 | 187 | 		return (PyObject*)v; | 
 | 188 | 	} | 
 | 189 |  | 
 | 190 | 	/* 2 digits */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 191 | 	if (!(ival >> 2*PyLong_SHIFT)) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 192 | 		v = _PyLong_New(2); | 
 | 193 | 		if (v) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 194 | 			Py_Size(v) = 2*sign; | 
 | 195 | 			v->ob_digit[0] = (digit)ival & PyLong_MASK; | 
 | 196 | 			v->ob_digit[1] = ival >> PyLong_SHIFT; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 197 | 		} | 
 | 198 | 		return (PyObject*)v; | 
 | 199 | 	} | 
 | 200 |  | 
 | 201 | 	/* Larger numbers: loop to determine number of digits */ | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 202 | 	t = (unsigned long)ival; | 
 | 203 | 	while (t) { | 
 | 204 | 		++ndigits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 205 | 		t >>= PyLong_SHIFT; | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 206 | 	} | 
 | 207 | 	v = _PyLong_New(ndigits); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 208 | 	if (v != NULL) { | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 209 | 		digit *p = v->ob_digit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 210 | 		Py_Size(v) = ndigits*sign; | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 211 | 		t = (unsigned long)ival; | 
 | 212 | 		while (t) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 213 | 			*p++ = (digit)(t & PyLong_MASK); | 
 | 214 | 			t >>= PyLong_SHIFT; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 215 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 216 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 217 | 	return (PyObject *)v; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 218 | } | 
 | 219 |  | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 220 | /* Create a new long int object from a C unsigned long int */ | 
 | 221 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 222 | PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 223 | PyLong_FromUnsignedLong(unsigned long ival) | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 224 | { | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 225 | 	PyLongObject *v; | 
 | 226 | 	unsigned long t; | 
 | 227 | 	int ndigits = 0; | 
 | 228 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 229 | 	if (ival < PyLong_BASE) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 230 | 		return PyLong_FromLong(ival); | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 231 | 	/* Count the number of Python digits. */ | 
 | 232 | 	t = (unsigned long)ival; | 
 | 233 | 	while (t) { | 
 | 234 | 		++ndigits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 235 | 		t >>= PyLong_SHIFT; | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 236 | 	} | 
 | 237 | 	v = _PyLong_New(ndigits); | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 238 | 	if (v != NULL) { | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 239 | 		digit *p = v->ob_digit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 240 | 		Py_Size(v) = ndigits; | 
| Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 241 | 		while (ival) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 242 | 			*p++ = (digit)(ival & PyLong_MASK); | 
 | 243 | 			ival >>= PyLong_SHIFT; | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 244 | 		} | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 245 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 246 | 	return (PyObject *)v; | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 247 | } | 
 | 248 |  | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 249 | /* Create a new long int object from a C double */ | 
 | 250 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | PyObject * | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 252 | PyLong_FromDouble(double dval) | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 253 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 254 | 	PyLongObject *v; | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 255 | 	double frac; | 
 | 256 | 	int i, ndig, expo, neg; | 
 | 257 | 	neg = 0; | 
| Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 258 | 	if (Py_IS_INFINITY(dval)) { | 
| Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 259 | 		PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 260 | 			"cannot convert float infinity to int"); | 
| Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 261 | 		return NULL; | 
 | 262 | 	} | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 263 | 	if (dval < 0.0) { | 
 | 264 | 		neg = 1; | 
 | 265 | 		dval = -dval; | 
 | 266 | 	} | 
 | 267 | 	frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ | 
 | 268 | 	if (expo <= 0) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 269 | 		return PyLong_FromLong(0L); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 270 | 	ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 271 | 	v = _PyLong_New(ndig); | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 272 | 	if (v == NULL) | 
 | 273 | 		return NULL; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 274 | 	frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 275 | 	for (i = ndig; --i >= 0; ) { | 
 | 276 | 		long bits = (long)frac; | 
| Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 277 | 		v->ob_digit[i] = (digit) bits; | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 278 | 		frac = frac - (double)bits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 279 | 		frac = ldexp(frac, PyLong_SHIFT); | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 280 | 	} | 
 | 281 | 	if (neg) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 282 | 		Py_Size(v) = -(Py_Size(v)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 283 | 	return (PyObject *)v; | 
| Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 284 | } | 
 | 285 |  | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 286 | /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define | 
 | 287 |  * anything about what happens when a signed integer operation overflows, | 
 | 288 |  * and some compilers think they're doing you a favor by being "clever" | 
 | 289 |  * then.  The bit pattern for the largest postive signed long is | 
 | 290 |  * (unsigned long)LONG_MAX, and for the smallest negative signed long | 
 | 291 |  * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. | 
 | 292 |  * However, some other compilers warn about applying unary minus to an | 
 | 293 |  * unsigned operand.  Hence the weird "0-". | 
 | 294 |  */ | 
 | 295 | #define PY_ABS_LONG_MIN		(0-(unsigned long)LONG_MIN) | 
 | 296 | #define PY_ABS_SSIZE_T_MIN	(0-(size_t)PY_SSIZE_T_MIN) | 
 | 297 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 298 | /* Get a C long int from a long int object. | 
 | 299 |    Returns -1 and sets an error condition if overflow occurs. */ | 
 | 300 |  | 
 | 301 | long | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 302 | PyLong_AsLong(PyObject *vv) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 303 | { | 
| Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 304 | 	/* This version by Tim Peters */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | 	register PyLongObject *v; | 
| Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 306 | 	unsigned long x, prev; | 
| Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 307 | 	long res; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 308 | 	Py_ssize_t i; | 
 | 309 | 	int sign; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 310 | 	int do_decref = 0; /* if nb_int was called */ | 
| Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 311 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 312 | 	if (vv == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 314 | 		return -1; | 
 | 315 | 	} | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 316 |  | 
 | 317 | 	if (!PyLong_Check(vv)) { | 
 | 318 | 		PyNumberMethods *nb; | 
 | 319 | 		if ((nb = vv->ob_type->tp_as_number) == NULL || | 
 | 320 | 		    nb->nb_int == NULL) { | 
 | 321 | 			PyErr_SetString(PyExc_TypeError, "an integer is required"); | 
 | 322 | 			return -1; | 
 | 323 | 		} | 
 | 324 | 		vv = (*nb->nb_int) (vv); | 
 | 325 | 		if (vv == NULL) | 
 | 326 | 			return -1; | 
 | 327 | 		do_decref = 1; | 
 | 328 | 		if (!PyLong_Check(vv)) { | 
 | 329 | 			Py_DECREF(vv); | 
 | 330 | 			PyErr_SetString(PyExc_TypeError, | 
 | 331 | 					"nb_int should return int object"); | 
 | 332 | 			return -1; | 
 | 333 | 		} | 
 | 334 | 	} | 
 | 335 |  | 
| Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 336 | 	res = -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 338 | 	i = Py_Size(v); | 
| Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 339 |  | 
| Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 340 | 	switch (i) { | 
 | 341 | 	case -1: | 
 | 342 | 		res = -v->ob_digit[0]; | 
 | 343 | 		break; | 
 | 344 | 	case 0: | 
 | 345 | 		res = 0; | 
 | 346 | 		break; | 
 | 347 | 	case 1: | 
 | 348 | 		res = v->ob_digit[0]; | 
 | 349 | 		break; | 
 | 350 | 	default: | 
 | 351 | 		sign = 1; | 
 | 352 | 		x = 0; | 
 | 353 | 		if (i < 0) { | 
 | 354 | 			sign = -1; | 
 | 355 | 			i = -(i); | 
 | 356 | 		} | 
 | 357 | 		while (--i >= 0) { | 
 | 358 | 			prev = x; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 359 | 			x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
 | 360 | 			if ((x >> PyLong_SHIFT) != prev) { | 
| Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 361 | 				PyErr_SetString(PyExc_OverflowError, | 
 | 362 | 					"Python int too large to convert to C long"); | 
 | 363 | 				goto exit; | 
 | 364 | 			} | 
 | 365 | 		} | 
 | 366 | 		/* Haven't lost any bits, but casting to long requires extra care | 
 | 367 | 		 * (see comment above). | 
 | 368 | 	         */ | 
 | 369 | 		if (x <= (unsigned long)LONG_MAX) { | 
 | 370 | 			res = (long)x * sign; | 
 | 371 | 		} | 
 | 372 | 		else if (sign < 0 && x == PY_ABS_LONG_MIN) { | 
 | 373 | 			res = LONG_MIN; | 
 | 374 | 		} | 
 | 375 | 		else { | 
 | 376 | 			PyErr_SetString(PyExc_OverflowError, | 
 | 377 | 				"Python int too large to convert to C long"); | 
 | 378 | 		}	 | 
 | 379 | 	} | 
 | 380 |  exit: | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 381 | 	if (do_decref) { | 
 | 382 | 		Py_DECREF(vv); | 
 | 383 | 	} | 
| Georg Brandl | 61c31b0 | 2007-02-26 14:46:30 +0000 | [diff] [blame] | 384 | 	return res; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 385 | } | 
 | 386 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 387 | int | 
 | 388 | _PyLong_FitsInLong(PyObject *vv) | 
 | 389 | { | 
 | 390 | 	int size; | 
 | 391 | 	if (!PyLong_CheckExact(vv)) { | 
 | 392 | 		PyErr_BadInternalCall(); | 
 | 393 | 		return 0; | 
 | 394 | 	} | 
 | 395 | 	/* conservative estimate */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 396 | 	size = Py_Size(vv); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 397 | 	return -2 <= size && size <= 2; | 
 | 398 | } | 
 | 399 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 400 | /* Get a Py_ssize_t from a long int object. | 
 | 401 |    Returns -1 and sets an error condition if overflow occurs. */ | 
 | 402 |  | 
 | 403 | Py_ssize_t | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 404 | PyLong_AsSsize_t(PyObject *vv) { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 405 | 	register PyLongObject *v; | 
 | 406 | 	size_t x, prev; | 
 | 407 | 	Py_ssize_t i; | 
 | 408 | 	int sign; | 
 | 409 |  | 
 | 410 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 411 | 		PyErr_BadInternalCall(); | 
 | 412 | 		return -1; | 
 | 413 | 	} | 
 | 414 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 415 | 	i = Py_Size(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 416 | 	switch (i) { | 
 | 417 | 	case -1: return -v->ob_digit[0]; | 
 | 418 | 	case 0: return 0; | 
 | 419 | 	case 1: return v->ob_digit[0]; | 
 | 420 | 	} | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 421 | 	sign = 1; | 
 | 422 | 	x = 0; | 
 | 423 | 	if (i < 0) { | 
 | 424 | 		sign = -1; | 
 | 425 | 		i = -(i); | 
 | 426 | 	} | 
 | 427 | 	while (--i >= 0) { | 
 | 428 | 		prev = x; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 429 | 		x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
 | 430 | 		if ((x >> PyLong_SHIFT) != prev) | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 431 | 			goto overflow; | 
 | 432 | 	} | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 433 | 	/* Haven't lost any bits, but casting to a signed type requires | 
 | 434 | 	 * extra care (see comment above). | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 435 | 	 */ | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 436 | 	if (x <= (size_t)PY_SSIZE_T_MAX) { | 
 | 437 | 		return (Py_ssize_t)x * sign; | 
 | 438 | 	} | 
 | 439 | 	else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { | 
 | 440 | 		return PY_SSIZE_T_MIN; | 
 | 441 | 	} | 
 | 442 | 	/* else overflow */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 443 |  | 
 | 444 |  overflow: | 
 | 445 | 	PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 446 | 			"Python int too large to convert to C ssize_t"); | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 447 | 	return -1; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 448 | } | 
 | 449 |  | 
| Guido van Rossum | d8c8048 | 2002-08-13 00:24:58 +0000 | [diff] [blame] | 450 | /* Get a C unsigned long int from a long int object. | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 451 |    Returns -1 and sets an error condition if overflow occurs. */ | 
 | 452 |  | 
 | 453 | unsigned long | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 454 | PyLong_AsUnsignedLong(PyObject *vv) | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 455 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 456 | 	register PyLongObject *v; | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 457 | 	unsigned long x, prev; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 458 | 	Py_ssize_t i; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 459 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 460 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 461 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 462 | 		return (unsigned long) -1; | 
 | 463 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 465 | 	i = Py_Size(v); | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 466 | 	x = 0; | 
 | 467 | 	if (i < 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 468 | 		PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 469 | 			   "can't convert negative value to unsigned int"); | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 470 | 		return (unsigned long) -1; | 
 | 471 | 	} | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 472 | 	switch (i) { | 
 | 473 | 	case 0: return 0; | 
 | 474 | 	case 1: return v->ob_digit[0]; | 
 | 475 | 	} | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 476 | 	while (--i >= 0) { | 
 | 477 | 		prev = x; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 478 | 		x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
 | 479 | 		if ((x >> PyLong_SHIFT) != prev) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | 			PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 481 | 			 "python int too large to convert to C unsigned long"); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 482 | 			return (unsigned long) -1; | 
 | 483 | 		} | 
 | 484 | 	} | 
 | 485 | 	return x; | 
 | 486 | } | 
 | 487 |  | 
 | 488 | /* Get a C unsigned long int from a long int object. | 
 | 489 |    Returns -1 and sets an error condition if overflow occurs. */ | 
 | 490 |  | 
 | 491 | size_t | 
 | 492 | PyLong_AsSize_t(PyObject *vv) | 
 | 493 | { | 
 | 494 | 	register PyLongObject *v; | 
 | 495 | 	size_t x, prev; | 
 | 496 | 	Py_ssize_t i; | 
 | 497 |  | 
 | 498 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 499 | 		PyErr_BadInternalCall(); | 
 | 500 | 		return (unsigned long) -1; | 
 | 501 | 	} | 
 | 502 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 503 | 	i = Py_Size(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 504 | 	x = 0; | 
 | 505 | 	if (i < 0) { | 
 | 506 | 		PyErr_SetString(PyExc_OverflowError, | 
 | 507 | 			   "can't convert negative value to size_t"); | 
 | 508 | 		return (size_t) -1; | 
 | 509 | 	} | 
 | 510 | 	switch (i) { | 
 | 511 | 	case 0: return 0; | 
 | 512 | 	case 1: return v->ob_digit[0]; | 
 | 513 | 	} | 
 | 514 | 	while (--i >= 0) { | 
 | 515 | 		prev = x; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 516 | 		x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
 | 517 | 		if ((x >> PyLong_SHIFT) != prev) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 518 | 			PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 519 | 			    "Python int too large to convert to C size_t"); | 
| Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 520 | 			return (unsigned long) -1; | 
 | 521 | 		} | 
 | 522 | 	} | 
 | 523 | 	return x; | 
 | 524 | } | 
 | 525 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 526 | /* Get a C unsigned long int from a long int object, ignoring the high bits. | 
 | 527 |    Returns -1 and sets an error condition if an error occurs. */ | 
 | 528 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 529 | static unsigned long | 
 | 530 | _PyLong_AsUnsignedLongMask(PyObject *vv) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 531 | { | 
 | 532 | 	register PyLongObject *v; | 
 | 533 | 	unsigned long x; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 534 | 	Py_ssize_t i; | 
 | 535 | 	int sign; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 536 |  | 
 | 537 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 538 | 		PyErr_BadInternalCall(); | 
 | 539 | 		return (unsigned long) -1; | 
 | 540 | 	} | 
 | 541 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 542 | 	i = Py_Size(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 543 | 	switch (i) { | 
 | 544 | 	case 0: return 0; | 
 | 545 | 	case 1: return v->ob_digit[0]; | 
 | 546 | 	} | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 547 | 	sign = 1; | 
 | 548 | 	x = 0; | 
 | 549 | 	if (i < 0) { | 
 | 550 | 		sign = -1; | 
 | 551 | 		i = -i; | 
 | 552 | 	} | 
 | 553 | 	while (--i >= 0) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 554 | 		x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 555 | 	} | 
 | 556 | 	return x * sign; | 
 | 557 | } | 
 | 558 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 559 | unsigned long | 
 | 560 | PyLong_AsUnsignedLongMask(register PyObject *op) | 
 | 561 | { | 
 | 562 | 	PyNumberMethods *nb; | 
 | 563 | 	PyLongObject *lo; | 
 | 564 | 	unsigned long val; | 
 | 565 |  | 
 | 566 | 	if (op && PyLong_Check(op)) | 
 | 567 | 		return _PyLong_AsUnsignedLongMask(op); | 
 | 568 |  | 
 | 569 | 	if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || | 
 | 570 | 	    nb->nb_int == NULL) { | 
 | 571 | 		PyErr_SetString(PyExc_TypeError, "an integer is required"); | 
 | 572 | 		return (unsigned long)-1; | 
 | 573 | 	} | 
 | 574 |  | 
 | 575 | 	lo = (PyLongObject*) (*nb->nb_int) (op); | 
 | 576 | 	if (lo == NULL) | 
 | 577 | 		return (unsigned long)-1; | 
 | 578 | 	if (PyLong_Check(lo)) { | 
 | 579 | 		val = _PyLong_AsUnsignedLongMask((PyObject *)lo); | 
 | 580 | 		Py_DECREF(lo); | 
 | 581 | 		if (PyErr_Occurred()) | 
 | 582 | 			return (unsigned long)-1; | 
 | 583 | 		return val; | 
 | 584 | 	} | 
 | 585 | 	else | 
 | 586 | 	{ | 
 | 587 | 		Py_DECREF(lo); | 
 | 588 | 		PyErr_SetString(PyExc_TypeError, | 
 | 589 | 				"nb_int should return int object"); | 
 | 590 | 		return (unsigned long)-1; | 
 | 591 | 	} | 
 | 592 | } | 
 | 593 |  | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 594 | int | 
 | 595 | _PyLong_Sign(PyObject *vv) | 
 | 596 | { | 
 | 597 | 	PyLongObject *v = (PyLongObject *)vv; | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 598 |  | 
 | 599 | 	assert(v != NULL); | 
 | 600 | 	assert(PyLong_Check(v)); | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 601 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 602 | 	return Py_Size(v) == 0 ? 0 : (Py_Size(v) < 0 ? -1 : 1); | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 603 | } | 
 | 604 |  | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 605 | size_t | 
 | 606 | _PyLong_NumBits(PyObject *vv) | 
 | 607 | { | 
 | 608 | 	PyLongObject *v = (PyLongObject *)vv; | 
| Tim Peters | 5b8132f | 2003-01-31 15:52:05 +0000 | [diff] [blame] | 609 | 	size_t result = 0; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 610 | 	Py_ssize_t ndigits; | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 611 |  | 
 | 612 | 	assert(v != NULL); | 
 | 613 | 	assert(PyLong_Check(v)); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 614 | 	ndigits = ABS(Py_Size(v)); | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 615 | 	assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); | 
 | 616 | 	if (ndigits > 0) { | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 617 | 		digit msd = v->ob_digit[ndigits - 1]; | 
 | 618 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 619 | 		result = (ndigits - 1) * PyLong_SHIFT; | 
 | 620 | 		if (result / PyLong_SHIFT != (size_t)(ndigits - 1)) | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 621 | 			goto Overflow; | 
 | 622 | 		do { | 
 | 623 | 			++result; | 
 | 624 | 			if (result == 0) | 
 | 625 | 				goto Overflow; | 
 | 626 | 			msd >>= 1; | 
 | 627 | 		} while (msd); | 
 | 628 | 	} | 
 | 629 | 	return result; | 
 | 630 |  | 
 | 631 | Overflow: | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 632 | 	PyErr_SetString(PyExc_OverflowError, "int has too many bits " | 
| Tim Peters | baefd9e | 2003-01-28 20:37:45 +0000 | [diff] [blame] | 633 | 			"to express in a platform size_t"); | 
 | 634 | 	return (size_t)-1; | 
 | 635 | } | 
 | 636 |  | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 637 | PyObject * | 
 | 638 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, | 
 | 639 | 		      int little_endian, int is_signed) | 
 | 640 | { | 
 | 641 | 	const unsigned char* pstartbyte;/* LSB of bytes */ | 
 | 642 | 	int incr;			/* direction to move pstartbyte */ | 
 | 643 | 	const unsigned char* pendbyte;	/* MSB of bytes */ | 
 | 644 | 	size_t numsignificantbytes;	/* number of bytes that matter */ | 
 | 645 | 	size_t ndigits;			/* number of Python long digits */ | 
 | 646 | 	PyLongObject* v;		/* result */ | 
 | 647 | 	int idigit = 0;  		/* next free index in v->ob_digit */ | 
 | 648 |  | 
 | 649 | 	if (n == 0) | 
 | 650 | 		return PyLong_FromLong(0L); | 
 | 651 |  | 
 | 652 | 	if (little_endian) { | 
 | 653 | 		pstartbyte = bytes; | 
 | 654 | 		pendbyte = bytes + n - 1; | 
 | 655 | 		incr = 1; | 
 | 656 | 	} | 
 | 657 | 	else { | 
 | 658 | 		pstartbyte = bytes + n - 1; | 
 | 659 | 		pendbyte = bytes; | 
 | 660 | 		incr = -1; | 
 | 661 | 	} | 
 | 662 |  | 
 | 663 | 	if (is_signed) | 
 | 664 | 		is_signed = *pendbyte >= 0x80; | 
 | 665 |  | 
 | 666 | 	/* Compute numsignificantbytes.  This consists of finding the most | 
 | 667 | 	   significant byte.  Leading 0 bytes are insignficant if the number | 
 | 668 | 	   is positive, and leading 0xff bytes if negative. */ | 
 | 669 | 	{ | 
 | 670 | 		size_t i; | 
 | 671 | 		const unsigned char* p = pendbyte; | 
 | 672 | 		const int pincr = -incr;  /* search MSB to LSB */ | 
 | 673 | 		const unsigned char insignficant = is_signed ? 0xff : 0x00; | 
 | 674 |  | 
 | 675 | 		for (i = 0; i < n; ++i, p += pincr) { | 
 | 676 | 			if (*p != insignficant) | 
 | 677 | 				break; | 
 | 678 | 		} | 
 | 679 | 		numsignificantbytes = n - i; | 
 | 680 | 		/* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so | 
 | 681 | 		   actually has 2 significant bytes.  OTOH, 0xff0001 == | 
 | 682 | 		   -0x00ffff, so we wouldn't *need* to bump it there; but we | 
 | 683 | 		   do for 0xffff = -0x0001.  To be safe without bothering to | 
 | 684 | 		   check every case, bump it regardless. */ | 
 | 685 | 		if (is_signed && numsignificantbytes < n) | 
 | 686 | 			++numsignificantbytes; | 
 | 687 | 	} | 
 | 688 |  | 
 | 689 | 	/* How many Python long digits do we need?  We have | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 690 | 	   8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 691 | 	   bits, so it's the ceiling of the quotient. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 692 | 	ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 693 | 	if (ndigits > (size_t)INT_MAX) | 
 | 694 | 		return PyErr_NoMemory(); | 
 | 695 | 	v = _PyLong_New((int)ndigits); | 
 | 696 | 	if (v == NULL) | 
 | 697 | 		return NULL; | 
 | 698 |  | 
 | 699 | 	/* Copy the bits over.  The tricky parts are computing 2's-comp on | 
 | 700 | 	   the fly for signed numbers, and dealing with the mismatch between | 
 | 701 | 	   8-bit bytes and (probably) 15-bit Python digits.*/ | 
 | 702 | 	{ | 
 | 703 | 		size_t i; | 
| Tim Peters | f251d06 | 2001-06-13 21:09:15 +0000 | [diff] [blame] | 704 | 		twodigits carry = 1;		/* for 2's-comp calculation */ | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 705 | 		twodigits accum = 0;		/* sliding register */ | 
 | 706 | 		unsigned int accumbits = 0; 	/* number of bits in accum */ | 
 | 707 | 		const unsigned char* p = pstartbyte; | 
 | 708 |  | 
 | 709 | 		for (i = 0; i < numsignificantbytes; ++i, p += incr) { | 
| Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 710 | 			twodigits thisbyte = *p; | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 711 | 			/* Compute correction for 2's comp, if needed. */ | 
 | 712 | 			if (is_signed) { | 
 | 713 | 				thisbyte = (0xff ^ thisbyte) + carry; | 
 | 714 | 				carry = thisbyte >> 8; | 
 | 715 | 				thisbyte &= 0xff; | 
 | 716 | 			} | 
 | 717 | 			/* Because we're going LSB to MSB, thisbyte is | 
 | 718 | 			   more significant than what's already in accum, | 
 | 719 | 			   so needs to be prepended to accum. */ | 
 | 720 | 			accum |= thisbyte << accumbits; | 
 | 721 | 			accumbits += 8; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 722 | 			if (accumbits >= PyLong_SHIFT) { | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 723 | 				/* There's enough to fill a Python digit. */ | 
 | 724 | 				assert(idigit < (int)ndigits); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 725 | 				v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 726 | 				++idigit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 727 | 				accum >>= PyLong_SHIFT; | 
 | 728 | 				accumbits -= PyLong_SHIFT; | 
 | 729 | 				assert(accumbits < PyLong_SHIFT); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 730 | 			} | 
 | 731 | 		} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 732 | 		assert(accumbits < PyLong_SHIFT); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 733 | 		if (accumbits) { | 
 | 734 | 			assert(idigit < (int)ndigits); | 
 | 735 | 			v->ob_digit[idigit] = (digit)accum; | 
 | 736 | 			++idigit; | 
 | 737 | 		} | 
 | 738 | 	} | 
 | 739 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 740 | 	Py_Size(v) = is_signed ? -idigit : idigit; | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 741 | 	return (PyObject *)long_normalize(v); | 
 | 742 | } | 
 | 743 |  | 
 | 744 | int | 
 | 745 | _PyLong_AsByteArray(PyLongObject* v, | 
 | 746 | 		    unsigned char* bytes, size_t n, | 
 | 747 | 		    int little_endian, int is_signed) | 
 | 748 | { | 
 | 749 | 	int i;			/* index into v->ob_digit */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 750 | 	Py_ssize_t ndigits;		/* |v->ob_size| */ | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 751 | 	twodigits accum;	/* sliding register */ | 
 | 752 | 	unsigned int accumbits; /* # bits in accum */ | 
 | 753 | 	int do_twos_comp;	/* store 2's-comp?  is_signed and v < 0 */ | 
 | 754 | 	twodigits carry;	/* for computing 2's-comp */ | 
 | 755 | 	size_t j;		/* # bytes filled */ | 
 | 756 | 	unsigned char* p;	/* pointer to next byte in bytes */ | 
 | 757 | 	int pincr;		/* direction to move p */ | 
 | 758 |  | 
 | 759 | 	assert(v != NULL && PyLong_Check(v)); | 
 | 760 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 761 | 	if (Py_Size(v) < 0) { | 
 | 762 | 		ndigits = -(Py_Size(v)); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 763 | 		if (!is_signed) { | 
 | 764 | 			PyErr_SetString(PyExc_TypeError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 765 | 				"can't convert negative int to unsigned"); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 766 | 			return -1; | 
 | 767 | 		} | 
 | 768 | 		do_twos_comp = 1; | 
 | 769 | 	} | 
 | 770 | 	else { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 771 | 		ndigits = Py_Size(v); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 772 | 		do_twos_comp = 0; | 
 | 773 | 	} | 
 | 774 |  | 
 | 775 | 	if (little_endian) { | 
 | 776 | 		p = bytes; | 
 | 777 | 		pincr = 1; | 
 | 778 | 	} | 
 | 779 | 	else { | 
 | 780 | 		p = bytes + n - 1; | 
 | 781 | 		pincr = -1; | 
 | 782 | 	} | 
 | 783 |  | 
| Tim Peters | 898cf85 | 2001-06-13 20:50:08 +0000 | [diff] [blame] | 784 | 	/* Copy over all the Python digits. | 
 | 785 | 	   It's crucial that every Python digit except for the MSD contribute | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 786 | 	   exactly PyLong_SHIFT bits to the total, so first assert that the long is | 
| Tim Peters | 898cf85 | 2001-06-13 20:50:08 +0000 | [diff] [blame] | 787 | 	   normalized. */ | 
 | 788 | 	assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 789 | 	j = 0; | 
 | 790 | 	accum = 0; | 
 | 791 | 	accumbits = 0; | 
 | 792 | 	carry = do_twos_comp ? 1 : 0; | 
 | 793 | 	for (i = 0; i < ndigits; ++i) { | 
 | 794 | 		twodigits thisdigit = v->ob_digit[i]; | 
 | 795 | 		if (do_twos_comp) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 796 | 			thisdigit = (thisdigit ^ PyLong_MASK) + carry; | 
 | 797 | 			carry = thisdigit >> PyLong_SHIFT; | 
 | 798 | 			thisdigit &= PyLong_MASK; | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 799 | 		} | 
| Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 800 | 		/* Because we're going LSB to MSB, thisdigit is more | 
 | 801 | 		   significant than what's already in accum, so needs to be | 
 | 802 | 		   prepended to accum. */ | 
 | 803 | 		accum |= thisdigit << accumbits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 804 | 		accumbits += PyLong_SHIFT; | 
| Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 805 |  | 
| Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 806 | 		/* The most-significant digit may be (probably is) at least | 
 | 807 | 		   partly empty. */ | 
| Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 808 | 		if (i == ndigits - 1) { | 
| Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 809 | 			/* Count # of sign bits -- they needn't be stored, | 
 | 810 | 			 * although for signed conversion we need later to | 
 | 811 | 			 * make sure at least one sign bit gets stored. | 
 | 812 | 			 * First shift conceptual sign bit to real sign bit. | 
 | 813 | 			 */ | 
 | 814 | 			stwodigits s = (stwodigits)(thisdigit << | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 815 | 				(8*sizeof(stwodigits) - PyLong_SHIFT)); | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 816 | 			unsigned int nsignbits = 0; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 817 | 			while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) { | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 818 | 				++nsignbits; | 
| Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 819 | 				s <<= 1; | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 820 | 			} | 
| Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 821 | 			accumbits -= nsignbits; | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 822 | 		} | 
| Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 823 |  | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 824 | 		/* Store as many bytes as possible. */ | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 825 | 		while (accumbits >= 8) { | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 826 | 			if (j >= n) | 
 | 827 | 				goto Overflow; | 
 | 828 | 			++j; | 
 | 829 | 			*p = (unsigned char)(accum & 0xff); | 
 | 830 | 			p += pincr; | 
 | 831 | 			accumbits -= 8; | 
 | 832 | 			accum >>= 8; | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 833 | 		} | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 834 | 	} | 
 | 835 |  | 
 | 836 | 	/* Store the straggler (if any). */ | 
 | 837 | 	assert(accumbits < 8); | 
 | 838 | 	assert(carry == 0);  /* else do_twos_comp and *every* digit was 0 */ | 
| Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 839 | 	if (accumbits > 0) { | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 840 | 		if (j >= n) | 
 | 841 | 			goto Overflow; | 
 | 842 | 		++j; | 
 | 843 | 		if (do_twos_comp) { | 
 | 844 | 			/* Fill leading bits of the byte with sign bits | 
 | 845 | 			   (appropriately pretending that the long had an | 
 | 846 | 			   infinite supply of sign bits). */ | 
 | 847 | 			accum |= (~(twodigits)0) << accumbits; | 
 | 848 | 		} | 
 | 849 | 		*p = (unsigned char)(accum & 0xff); | 
 | 850 | 		p += pincr; | 
 | 851 | 	} | 
| Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 852 | 	else if (j == n && n > 0 && is_signed) { | 
 | 853 | 		/* The main loop filled the byte array exactly, so the code | 
 | 854 | 		   just above didn't get to ensure there's a sign bit, and the | 
 | 855 | 		   loop below wouldn't add one either.  Make sure a sign bit | 
 | 856 | 		   exists. */ | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 857 | 		unsigned char msb = *(p - pincr); | 
| Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 858 | 		int sign_bit_set = msb >= 0x80; | 
 | 859 | 		assert(accumbits == 0); | 
 | 860 | 		if (sign_bit_set == do_twos_comp) | 
 | 861 | 			return 0; | 
 | 862 | 		else | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 863 | 			goto Overflow; | 
 | 864 | 	} | 
| Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 865 |  | 
 | 866 | 	/* Fill remaining bytes with copies of the sign bit. */ | 
 | 867 | 	{ | 
 | 868 | 		unsigned char signbyte = do_twos_comp ? 0xffU : 0U; | 
 | 869 | 		for ( ; j < n; ++j, p += pincr) | 
 | 870 | 			*p = signbyte; | 
 | 871 | 	} | 
 | 872 |  | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 873 | 	return 0; | 
 | 874 |  | 
 | 875 | Overflow: | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 876 | 	PyErr_SetString(PyExc_OverflowError, "int too big to convert"); | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 877 | 	return -1; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 878 |  | 
| Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 879 | } | 
 | 880 |  | 
| Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 881 | double | 
 | 882 | _PyLong_AsScaledDouble(PyObject *vv, int *exponent) | 
 | 883 | { | 
 | 884 | /* NBITS_WANTED should be > the number of bits in a double's precision, | 
 | 885 |    but small enough so that 2**NBITS_WANTED is within the normal double | 
 | 886 |    range.  nbitsneeded is set to 1 less than that because the most-significant | 
 | 887 |    Python digit contains at least 1 significant bit, but we don't want to | 
 | 888 |    bother counting them (catering to the worst case cheaply). | 
 | 889 |  | 
 | 890 |    57 is one more than VAX-D double precision; I (Tim) don't know of a double | 
 | 891 |    format with more precision than that; it's 1 larger so that we add in at | 
 | 892 |    least one round bit to stand in for the ignored least-significant bits. | 
 | 893 | */ | 
 | 894 | #define NBITS_WANTED 57 | 
 | 895 | 	PyLongObject *v; | 
 | 896 | 	double x; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 897 | 	const double multiplier = (double)(1L << PyLong_SHIFT); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 898 | 	Py_ssize_t i; | 
 | 899 | 	int sign; | 
| Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 900 | 	int nbitsneeded; | 
 | 901 |  | 
 | 902 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 903 | 		PyErr_BadInternalCall(); | 
 | 904 | 		return -1; | 
 | 905 | 	} | 
 | 906 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 907 | 	i = Py_Size(v); | 
| Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 908 | 	sign = 1; | 
 | 909 | 	if (i < 0) { | 
 | 910 | 		sign = -1; | 
 | 911 | 		i = -(i); | 
 | 912 | 	} | 
 | 913 | 	else if (i == 0) { | 
 | 914 | 		*exponent = 0; | 
 | 915 | 		return 0.0; | 
 | 916 | 	} | 
 | 917 | 	--i; | 
 | 918 | 	x = (double)v->ob_digit[i]; | 
 | 919 | 	nbitsneeded = NBITS_WANTED - 1; | 
 | 920 | 	/* Invariant:  i Python digits remain unaccounted for. */ | 
 | 921 | 	while (i > 0 && nbitsneeded > 0) { | 
 | 922 | 		--i; | 
 | 923 | 		x = x * multiplier + (double)v->ob_digit[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 924 | 		nbitsneeded -= PyLong_SHIFT; | 
| Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 925 | 	} | 
 | 926 | 	/* There are i digits we didn't shift in.  Pretending they're all | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 927 | 	   zeroes, the true value is x * 2**(i*PyLong_SHIFT). */ | 
| Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 928 | 	*exponent = i; | 
 | 929 | 	assert(x > 0.0); | 
 | 930 | 	return x * sign; | 
 | 931 | #undef NBITS_WANTED | 
 | 932 | } | 
 | 933 |  | 
| Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 934 | /* Get a C double from a long int object. */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 935 |  | 
 | 936 | double | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 937 | PyLong_AsDouble(PyObject *vv) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 938 | { | 
| Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 939 | 	int e = -1; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 940 | 	double x; | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 941 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 942 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 943 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 944 | 		return -1; | 
 | 945 | 	} | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 946 | 	x = _PyLong_AsScaledDouble(vv, &e); | 
 | 947 | 	if (x == -1.0 && PyErr_Occurred()) | 
 | 948 | 		return -1.0; | 
| Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 949 | 	/* 'e' initialized to -1 to silence gcc-4.0.x, but it should be | 
 | 950 | 	   set correctly after a successful _PyLong_AsScaledDouble() call */ | 
 | 951 | 	assert(e >= 0); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 952 | 	if (e > INT_MAX / PyLong_SHIFT) | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 953 | 		goto overflow; | 
 | 954 | 	errno = 0; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 955 | 	x = ldexp(x, e * PyLong_SHIFT); | 
| Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 956 | 	if (Py_OVERFLOWED(x)) | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 957 | 		goto overflow; | 
 | 958 | 	return x; | 
 | 959 |  | 
 | 960 | overflow: | 
 | 961 | 	PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | 523d4f9 | 2007-01-15 00:31:49 +0000 | [diff] [blame] | 962 | 		"Python int too large to convert to C double"); | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 963 | 	return -1.0; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 964 | } | 
 | 965 |  | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 966 | /* Create a new long (or int) object from a C pointer */ | 
 | 967 |  | 
 | 968 | PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 969 | PyLong_FromVoidPtr(void *p) | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 970 | { | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 971 | #ifndef HAVE_LONG_LONG | 
 | 972 | #   error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" | 
 | 973 | #endif | 
 | 974 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 975 | #   error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 976 | #endif | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 977 | 	/* special-case null pointer */ | 
 | 978 | 	if (!p) | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 979 | 		return PyInt_FromLong(0); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 980 | 	return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 981 |  | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 982 | } | 
 | 983 |  | 
 | 984 | /* Get a C pointer from a long object (or an int object in some cases) */ | 
 | 985 |  | 
 | 986 | void * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 987 | PyLong_AsVoidPtr(PyObject *vv) | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 988 | { | 
 | 989 | 	/* This function will allow int or long objects. If vv is neither, | 
 | 990 | 	   then the PyLong_AsLong*() functions will raise the exception: | 
 | 991 | 	   PyExc_SystemError, "bad argument to internal function" | 
 | 992 | 	*/ | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 993 | #if SIZEOF_VOID_P <= SIZEOF_LONG | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 994 | 	long x; | 
 | 995 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 996 | 	if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 997 | 		x = PyLong_AsLong(vv); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 998 | 	else | 
 | 999 | 		x = PyLong_AsUnsignedLong(vv); | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1000 | #else | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1001 |  | 
 | 1002 | #ifndef HAVE_LONG_LONG | 
 | 1003 | #   error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" | 
 | 1004 | #endif | 
 | 1005 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1006 | #   error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1007 | #endif | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1008 | 	PY_LONG_LONG x; | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1009 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1010 | 	if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1011 | 		x = PyLong_AsLongLong(vv); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1012 | 	else | 
 | 1013 | 		x = PyLong_AsUnsignedLongLong(vv); | 
| Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 1014 |  | 
 | 1015 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ | 
| Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 1016 |  | 
 | 1017 | 	if (x == -1 && PyErr_Occurred()) | 
 | 1018 | 		return NULL; | 
 | 1019 | 	return (void *)x; | 
 | 1020 | } | 
 | 1021 |  | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1022 | #ifdef HAVE_LONG_LONG | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1023 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1024 | /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1025 |  * rewritten to use the newer PyLong_{As,From}ByteArray API. | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1026 |  */ | 
 | 1027 |  | 
| Tim Peters | cf37dfc | 2001-06-14 18:42:50 +0000 | [diff] [blame] | 1028 | #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1029 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1030 | /* Create a new long int object from a C PY_LONG_LONG int. */ | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1031 |  | 
 | 1032 | PyObject * | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1033 | PyLong_FromLongLong(PY_LONG_LONG ival) | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1034 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1035 | 	PyLongObject *v; | 
 | 1036 | 	unsigned PY_LONG_LONG t;  /* unsigned so >> doesn't propagate sign bit */ | 
 | 1037 | 	int ndigits = 0; | 
 | 1038 | 	int negative = 0; | 
 | 1039 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1040 | 	CHECK_SMALL_INT(ival); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1041 | 	if (ival < 0) { | 
 | 1042 | 		ival = -ival; | 
 | 1043 | 		negative = 1; | 
 | 1044 | 	} | 
 | 1045 |  | 
 | 1046 | 	/* Count the number of Python digits. | 
 | 1047 | 	   We used to pick 5 ("big enough for anything"), but that's a | 
 | 1048 | 	   waste of time and space given that 5*15 = 75 bits are rarely | 
 | 1049 | 	   needed. */ | 
 | 1050 | 	t = (unsigned PY_LONG_LONG)ival; | 
 | 1051 | 	while (t) { | 
 | 1052 | 		++ndigits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1053 | 		t >>= PyLong_SHIFT; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1054 | 	} | 
 | 1055 | 	v = _PyLong_New(ndigits); | 
 | 1056 | 	if (v != NULL) { | 
 | 1057 | 		digit *p = v->ob_digit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1058 | 		Py_Size(v) = negative ? -ndigits : ndigits; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1059 | 		t = (unsigned PY_LONG_LONG)ival; | 
 | 1060 | 		while (t) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1061 | 			*p++ = (digit)(t & PyLong_MASK); | 
 | 1062 | 			t >>= PyLong_SHIFT; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1063 | 		} | 
 | 1064 | 	} | 
 | 1065 | 	return (PyObject *)v; | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1066 | } | 
 | 1067 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1068 | /* Create a new long int object from a C unsigned PY_LONG_LONG int. */ | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1069 |  | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1070 | PyObject * | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1071 | PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1072 | { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1073 | 	PyLongObject *v; | 
 | 1074 | 	unsigned PY_LONG_LONG t; | 
 | 1075 | 	int ndigits = 0; | 
 | 1076 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1077 | 	if (ival < PyLong_BASE) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1078 | 		return PyLong_FromLong(ival); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1079 | 	/* Count the number of Python digits. */ | 
 | 1080 | 	t = (unsigned PY_LONG_LONG)ival; | 
 | 1081 | 	while (t) { | 
 | 1082 | 		++ndigits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1083 | 		t >>= PyLong_SHIFT; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1084 | 	} | 
 | 1085 | 	v = _PyLong_New(ndigits); | 
 | 1086 | 	if (v != NULL) { | 
 | 1087 | 		digit *p = v->ob_digit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1088 | 		Py_Size(v) = ndigits; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1089 | 		while (ival) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1090 | 			*p++ = (digit)(ival & PyLong_MASK); | 
 | 1091 | 			ival >>= PyLong_SHIFT; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1092 | 		} | 
 | 1093 | 	} | 
 | 1094 | 	return (PyObject *)v; | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1095 | } | 
 | 1096 |  | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1097 | /* Create a new long int object from a C Py_ssize_t. */ | 
 | 1098 |  | 
 | 1099 | PyObject * | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1100 | PyLong_FromSsize_t(Py_ssize_t ival) | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1101 | { | 
 | 1102 | 	Py_ssize_t bytes = ival; | 
 | 1103 | 	int one = 1; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1104 | 	if (ival < PyLong_BASE) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1105 | 		return PyLong_FromLong(ival); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1106 | 	return _PyLong_FromByteArray( | 
 | 1107 | 			(unsigned char *)&bytes, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1108 | 			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1109 | } | 
 | 1110 |  | 
 | 1111 | /* Create a new long int object from a C size_t. */ | 
 | 1112 |  | 
 | 1113 | PyObject * | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1114 | PyLong_FromSize_t(size_t ival) | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1115 | { | 
 | 1116 | 	size_t bytes = ival; | 
 | 1117 | 	int one = 1; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1118 | 	if (ival < PyLong_BASE) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1119 | 		return PyLong_FromLong(ival); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1120 | 	return _PyLong_FromByteArray( | 
 | 1121 | 			(unsigned char *)&bytes, | 
 | 1122 | 			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0); | 
 | 1123 | } | 
 | 1124 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1125 | /* Get a C PY_LONG_LONG int from a long int object. | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1126 |    Return -1 and set an error if overflow occurs. */ | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1127 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1128 | PY_LONG_LONG | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1129 | PyLong_AsLongLong(PyObject *vv) | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1130 | { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1131 | 	PyLongObject *v; | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1132 | 	PY_LONG_LONG bytes; | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1133 | 	int one = 1; | 
 | 1134 | 	int res; | 
 | 1135 |  | 
| Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 1136 | 	if (vv == NULL) { | 
 | 1137 | 		PyErr_BadInternalCall(); | 
 | 1138 | 		return -1; | 
 | 1139 | 	} | 
 | 1140 | 	if (!PyLong_Check(vv)) { | 
| Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1141 | 		PyNumberMethods *nb; | 
 | 1142 | 		PyObject *io; | 
| Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1143 | 		if ((nb = vv->ob_type->tp_as_number) == NULL || | 
 | 1144 | 		    nb->nb_int == NULL) { | 
 | 1145 | 			PyErr_SetString(PyExc_TypeError, "an integer is required"); | 
 | 1146 | 			return -1; | 
 | 1147 | 		} | 
 | 1148 | 		io = (*nb->nb_int) (vv); | 
 | 1149 | 		if (io == NULL) | 
 | 1150 | 			return -1; | 
| Martin v. Löwis | 6ce7ed2 | 2005-03-03 12:26:35 +0000 | [diff] [blame] | 1151 | 		if (PyLong_Check(io)) { | 
 | 1152 | 			bytes = PyLong_AsLongLong(io); | 
 | 1153 | 			Py_DECREF(io); | 
 | 1154 | 			return bytes; | 
 | 1155 | 		} | 
 | 1156 | 		Py_DECREF(io); | 
 | 1157 | 		PyErr_SetString(PyExc_TypeError, "integer conversion failed"); | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1158 | 		return -1; | 
 | 1159 | 	} | 
 | 1160 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1161 | 	v = (PyLongObject*)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1162 | 	switch(Py_Size(v)) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1163 | 	case -1: return -v->ob_digit[0]; | 
 | 1164 | 	case 0: return 0; | 
 | 1165 | 	case 1: return v->ob_digit[0]; | 
 | 1166 | 	} | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1167 | 	res = _PyLong_AsByteArray( | 
 | 1168 | 			(PyLongObject *)vv, (unsigned char *)&bytes, | 
 | 1169 | 			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1170 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1171 | 	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ | 
| Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1172 | 	if (res < 0) | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1173 | 		return (PY_LONG_LONG)-1; | 
| Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1174 | 	else | 
 | 1175 | 		return bytes; | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1176 | } | 
 | 1177 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1178 | /* Get a C unsigned PY_LONG_LONG int from a long int object. | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1179 |    Return -1 and set an error if overflow occurs. */ | 
 | 1180 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1181 | unsigned PY_LONG_LONG | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1182 | PyLong_AsUnsignedLongLong(PyObject *vv) | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1183 | { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1184 | 	PyLongObject *v; | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1185 | 	unsigned PY_LONG_LONG bytes; | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1186 | 	int one = 1; | 
 | 1187 | 	int res; | 
 | 1188 |  | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1189 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 1190 | 		PyErr_BadInternalCall(); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1191 | 		return (unsigned PY_LONG_LONG)-1; | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1192 | 	} | 
 | 1193 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1194 | 	v = (PyLongObject*)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1195 | 	switch(Py_Size(v)) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1196 | 	case 0: return 0; | 
 | 1197 | 	case 1: return v->ob_digit[0]; | 
 | 1198 | 	} | 
 | 1199 |  | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1200 | 	res = _PyLong_AsByteArray( | 
 | 1201 | 			(PyLongObject *)vv, (unsigned char *)&bytes, | 
 | 1202 | 			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1203 |  | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1204 | 	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ | 
| Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1205 | 	if (res < 0) | 
| Martin v. Löwis | b9a0f91 | 2003-03-29 10:06:18 +0000 | [diff] [blame] | 1206 | 		return (unsigned PY_LONG_LONG)res; | 
| Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 1207 | 	else | 
 | 1208 | 		return bytes; | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1209 | } | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1210 |  | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1211 | /* Get a C unsigned long int from a long int object, ignoring the high bits. | 
 | 1212 |    Returns -1 and sets an error condition if an error occurs. */ | 
 | 1213 |  | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1214 | static unsigned PY_LONG_LONG | 
 | 1215 | _PyLong_AsUnsignedLongLongMask(PyObject *vv) | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1216 | { | 
 | 1217 | 	register PyLongObject *v; | 
 | 1218 | 	unsigned PY_LONG_LONG x; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1219 | 	Py_ssize_t i; | 
 | 1220 | 	int sign; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1221 |  | 
 | 1222 | 	if (vv == NULL || !PyLong_Check(vv)) { | 
 | 1223 | 		PyErr_BadInternalCall(); | 
 | 1224 | 		return (unsigned long) -1; | 
 | 1225 | 	} | 
 | 1226 | 	v = (PyLongObject *)vv; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1227 | 	switch(Py_Size(v)) { | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1228 | 	case 0: return 0; | 
 | 1229 | 	case 1: return v->ob_digit[0]; | 
 | 1230 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1231 | 	i = Py_Size(v); | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1232 | 	sign = 1; | 
 | 1233 | 	x = 0; | 
 | 1234 | 	if (i < 0) { | 
 | 1235 | 		sign = -1; | 
 | 1236 | 		i = -i; | 
 | 1237 | 	} | 
 | 1238 | 	while (--i >= 0) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1239 | 		x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 
| Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1240 | 	} | 
 | 1241 | 	return x * sign; | 
 | 1242 | } | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1243 |  | 
 | 1244 | unsigned PY_LONG_LONG | 
 | 1245 | PyLong_AsUnsignedLongLongMask(register PyObject *op) | 
 | 1246 | { | 
 | 1247 | 	PyNumberMethods *nb; | 
 | 1248 | 	PyLongObject *lo; | 
 | 1249 | 	unsigned PY_LONG_LONG val; | 
 | 1250 |  | 
 | 1251 | 	if (op && PyLong_Check(op)) | 
 | 1252 | 		return _PyLong_AsUnsignedLongLongMask(op); | 
 | 1253 |  | 
 | 1254 | 	if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || | 
 | 1255 | 	    nb->nb_int == NULL) { | 
 | 1256 | 		PyErr_SetString(PyExc_TypeError, "an integer is required"); | 
 | 1257 | 		return (unsigned PY_LONG_LONG)-1; | 
 | 1258 | 	} | 
 | 1259 |  | 
 | 1260 | 	lo = (PyLongObject*) (*nb->nb_int) (op); | 
 | 1261 | 	if (lo == NULL) | 
 | 1262 | 		return (unsigned PY_LONG_LONG)-1; | 
 | 1263 | 	if (PyLong_Check(lo)) { | 
 | 1264 | 		val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); | 
 | 1265 | 		Py_DECREF(lo); | 
 | 1266 | 		if (PyErr_Occurred()) | 
 | 1267 | 			return (unsigned PY_LONG_LONG)-1; | 
 | 1268 | 		return val; | 
 | 1269 | 	} | 
 | 1270 | 	else | 
 | 1271 | 	{ | 
 | 1272 | 		Py_DECREF(lo); | 
 | 1273 | 		PyErr_SetString(PyExc_TypeError, | 
 | 1274 | 				"nb_int should return int object"); | 
 | 1275 | 		return (unsigned PY_LONG_LONG)-1; | 
 | 1276 | 	} | 
 | 1277 | } | 
| Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 1278 | #undef IS_LITTLE_ENDIAN | 
 | 1279 |  | 
| Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1280 | #endif /* HAVE_LONG_LONG */ | 
 | 1281 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1282 |  | 
 | 1283 | static int | 
 | 1284 | convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1285 | 	if (PyLong_Check(v)) { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1286 | 		*a = (PyLongObject *) v; | 
 | 1287 | 		Py_INCREF(v); | 
 | 1288 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1289 | 	else { | 
 | 1290 | 		return 0; | 
 | 1291 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1292 | 	if (PyLong_Check(w)) { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1293 | 		*b = (PyLongObject *) w; | 
 | 1294 | 		Py_INCREF(w); | 
 | 1295 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1296 | 	else { | 
 | 1297 | 		Py_DECREF(*a); | 
 | 1298 | 		return 0; | 
 | 1299 | 	} | 
 | 1300 | 	return 1; | 
 | 1301 | } | 
 | 1302 |  | 
 | 1303 | #define CONVERT_BINOP(v, w, a, b) \ | 
 | 1304 | 	if (!convert_binop(v, w, a, b)) { \ | 
 | 1305 | 		Py_INCREF(Py_NotImplemented); \ | 
 | 1306 | 		return Py_NotImplemented; \ | 
 | 1307 | 	} | 
 | 1308 |  | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1309 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n] | 
 | 1310 |  * is modified in place, by adding y to it.  Carries are propagated as far as | 
 | 1311 |  * x[m-1], and the remaining carry (0 or 1) is returned. | 
 | 1312 |  */ | 
 | 1313 | static digit | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1314 | v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1315 | { | 
 | 1316 | 	int i; | 
 | 1317 | 	digit carry = 0; | 
 | 1318 |  | 
 | 1319 | 	assert(m >= n); | 
 | 1320 | 	for (i = 0; i < n; ++i) { | 
 | 1321 | 		carry += x[i] + y[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1322 | 		x[i] = carry & PyLong_MASK; | 
 | 1323 | 		carry >>= PyLong_SHIFT; | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1324 | 		assert((carry & 1) == carry); | 
 | 1325 | 	} | 
 | 1326 | 	for (; carry && i < m; ++i) { | 
 | 1327 | 		carry += x[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1328 | 		x[i] = carry & PyLong_MASK; | 
 | 1329 | 		carry >>= PyLong_SHIFT; | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1330 | 		assert((carry & 1) == carry); | 
 | 1331 | 	} | 
 | 1332 | 	return carry; | 
 | 1333 | } | 
 | 1334 |  | 
 | 1335 | /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n] | 
 | 1336 |  * is modified in place, by subtracting y from it.  Borrows are propagated as | 
 | 1337 |  * far as x[m-1], and the remaining borrow (0 or 1) is returned. | 
 | 1338 |  */ | 
 | 1339 | static digit | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1340 | v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1341 | { | 
 | 1342 | 	int i; | 
 | 1343 | 	digit borrow = 0; | 
 | 1344 |  | 
 | 1345 | 	assert(m >= n); | 
 | 1346 | 	for (i = 0; i < n; ++i) { | 
 | 1347 | 		borrow = x[i] - y[i] - borrow; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1348 | 		x[i] = borrow & PyLong_MASK; | 
 | 1349 | 		borrow >>= PyLong_SHIFT; | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1350 | 		borrow &= 1;	/* keep only 1 sign bit */ | 
 | 1351 | 	} | 
 | 1352 | 	for (; borrow && i < m; ++i) { | 
 | 1353 | 		borrow = x[i] - borrow; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1354 | 		x[i] = borrow & PyLong_MASK; | 
 | 1355 | 		borrow >>= PyLong_SHIFT; | 
| Tim Peters | 877a212 | 2002-08-12 05:09:36 +0000 | [diff] [blame] | 1356 | 		borrow &= 1; | 
 | 1357 | 	} | 
 | 1358 | 	return borrow; | 
 | 1359 | } | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1360 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1361 | /* Multiply by a single digit, ignoring the sign. */ | 
 | 1362 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1363 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1364 | mul1(PyLongObject *a, wdigit n) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1365 | { | 
 | 1366 | 	return muladd1(a, n, (digit)0); | 
 | 1367 | } | 
 | 1368 |  | 
 | 1369 | /* Multiply by a single digit and add a single digit, ignoring the sign. */ | 
 | 1370 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1371 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1372 | muladd1(PyLongObject *a, wdigit n, wdigit extra) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1373 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1374 | 	Py_ssize_t size_a = ABS(Py_Size(a)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1375 | 	PyLongObject *z = _PyLong_New(size_a+1); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1376 | 	twodigits carry = extra; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1377 | 	Py_ssize_t i; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1378 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1379 | 	if (z == NULL) | 
 | 1380 | 		return NULL; | 
 | 1381 | 	for (i = 0; i < size_a; ++i) { | 
 | 1382 | 		carry += (twodigits)a->ob_digit[i] * n; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1383 | 		z->ob_digit[i] = (digit) (carry & PyLong_MASK); | 
 | 1384 | 		carry >>= PyLong_SHIFT; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1385 | 	} | 
| Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1386 | 	z->ob_digit[i] = (digit) carry; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1387 | 	return long_normalize(z); | 
 | 1388 | } | 
 | 1389 |  | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1390 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient | 
 | 1391 |    in pout, and returning the remainder.  pin and pout point at the LSD. | 
 | 1392 |    It's OK for pin == pout on entry, which saves oodles of mallocs/frees in | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1393 |    _PyLong_Format, but that should be done with great care since longs are | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1394 |    immutable. */ | 
 | 1395 |  | 
 | 1396 | static digit | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1397 | inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1398 | { | 
 | 1399 | 	twodigits rem = 0; | 
 | 1400 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1401 | 	assert(n > 0 && n <= PyLong_MASK); | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1402 | 	pin += size; | 
 | 1403 | 	pout += size; | 
 | 1404 | 	while (--size >= 0) { | 
 | 1405 | 		digit hi; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1406 | 		rem = (rem << PyLong_SHIFT) + *--pin; | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1407 | 		*--pout = hi = (digit)(rem / n); | 
 | 1408 | 		rem -= hi * n; | 
 | 1409 | 	} | 
 | 1410 | 	return (digit)rem; | 
 | 1411 | } | 
 | 1412 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1413 | /* Divide a long integer by a digit, returning both the quotient | 
 | 1414 |    (as function result) and the remainder (through *prem). | 
 | 1415 |    The sign of a is ignored; n should not be zero. */ | 
 | 1416 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1417 | static PyLongObject * | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1418 | divrem1(PyLongObject *a, digit n, digit *prem) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1419 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1420 | 	const Py_ssize_t size = ABS(Py_Size(a)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1421 | 	PyLongObject *z; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1422 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1423 | 	assert(n > 0 && n <= PyLong_MASK); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1424 | 	z = _PyLong_New(size); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1425 | 	if (z == NULL) | 
 | 1426 | 		return NULL; | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1427 | 	*prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1428 | 	return long_normalize(z); | 
 | 1429 | } | 
 | 1430 |  | 
 | 1431 | /* Convert a long int object to a string, using a given conversion base. | 
| Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 1432 |    Return a string object. | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1433 |    If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1434 |  | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1435 | PyObject * | 
 | 1436 | _PyLong_Format(PyObject *aa, int base) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1437 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1438 | 	register PyLongObject *a = (PyLongObject *)aa; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1439 | 	PyObject *str; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1440 | 	Py_ssize_t i, j, sz; | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1441 | 	Py_ssize_t size_a; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1442 | 	Py_UNICODE *p; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1443 | 	int bits; | 
 | 1444 | 	char sign = '\0'; | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1445 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1446 | 	if (a == NULL || !PyLong_Check(a)) { | 
 | 1447 | 		PyErr_BadInternalCall(); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1448 | 		return NULL; | 
 | 1449 | 	} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1450 | 	assert(base >= 2 && base <= 36); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1451 | 	size_a = ABS(Py_Size(a)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1452 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1453 | 	/* Compute a rough upper bound for the length of the string */ | 
 | 1454 | 	i = base; | 
 | 1455 | 	bits = 0; | 
 | 1456 | 	while (i > 1) { | 
 | 1457 | 		++bits; | 
 | 1458 | 		i >>= 1; | 
 | 1459 | 	} | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1460 | 	i = 5; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1461 | 	j = size_a*PyLong_SHIFT + bits-1; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1462 | 	sz = i + j / bits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1463 | 	if (j / PyLong_SHIFT < size_a || sz < i) { | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1464 | 		PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1465 | 				"int is too large to format"); | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1466 | 		return NULL; | 
 | 1467 | 	} | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1468 | 	str = PyUnicode_FromUnicode(NULL, sz); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1469 | 	if (str == NULL) | 
 | 1470 | 		return NULL; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1471 | 	p = PyUnicode_AS_UNICODE(str) + sz; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1472 | 	*p = '\0'; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1473 | 	if (Py_Size(a) < 0) | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1474 | 		sign = '-'; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1475 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1476 | 	if (Py_Size(a) == 0) { | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1477 | 		*--p = '0'; | 
 | 1478 | 	} | 
 | 1479 | 	else if ((base & (base - 1)) == 0) { | 
 | 1480 | 		/* JRH: special case for power-of-2 bases */ | 
| Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1481 | 		twodigits accum = 0; | 
 | 1482 | 		int accumbits = 0;	/* # of bits in accum */ | 
 | 1483 | 		int basebits = 1;	/* # of bits in base-1 */ | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1484 | 		i = base; | 
| Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1485 | 		while ((i >>= 1) > 1) | 
 | 1486 | 			++basebits; | 
| Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1487 |  | 
 | 1488 | 		for (i = 0; i < size_a; ++i) { | 
| Tim Peters | 0d2d87d | 2002-08-20 19:00:22 +0000 | [diff] [blame] | 1489 | 			accum |= (twodigits)a->ob_digit[i] << accumbits; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1490 | 			accumbits += PyLong_SHIFT; | 
| Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1491 | 			assert(accumbits >= basebits); | 
 | 1492 | 			do { | 
| Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 1493 | 				char cdigit = (char)(accum & (base - 1)); | 
| Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 1494 | 				cdigit += (cdigit < 10) ? '0' : 'a'-10; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1495 | 				assert(p > PyUnicode_AS_UNICODE(str)); | 
| Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 1496 | 				*--p = cdigit; | 
| Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 1497 | 				accumbits -= basebits; | 
 | 1498 | 				accum >>= basebits; | 
 | 1499 | 			} while (i < size_a-1 ? accumbits >= basebits : | 
 | 1500 | 					 	accum > 0); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1501 | 		} | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1502 | 	} | 
 | 1503 | 	else { | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1504 | 		/* Not 0, and base not a power of 2.  Divide repeatedly by | 
 | 1505 | 		   base, but for speed use the highest power of base that | 
 | 1506 | 		   fits in a digit. */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1507 | 		Py_ssize_t size = size_a; | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1508 | 		digit *pin = a->ob_digit; | 
 | 1509 | 		PyLongObject *scratch; | 
 | 1510 | 		/* powbasw <- largest power of base that fits in a digit. */ | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1511 | 		digit powbase = base;  /* powbase == base ** power */ | 
 | 1512 | 		int power = 1; | 
 | 1513 | 		for (;;) { | 
 | 1514 | 			unsigned long newpow = powbase * (unsigned long)base; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1515 | 			if (newpow >> PyLong_SHIFT)  /* doesn't fit in a digit */ | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1516 | 				break; | 
 | 1517 | 			powbase = (digit)newpow; | 
 | 1518 | 			++power; | 
 | 1519 | 		} | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1520 |  | 
 | 1521 | 		/* Get a scratch area for repeated division. */ | 
 | 1522 | 		scratch = _PyLong_New(size); | 
 | 1523 | 		if (scratch == NULL) { | 
 | 1524 | 			Py_DECREF(str); | 
 | 1525 | 			return NULL; | 
 | 1526 | 		} | 
 | 1527 |  | 
 | 1528 | 		/* Repeatedly divide by powbase. */ | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1529 | 		do { | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1530 | 			int ntostore = power; | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1531 | 			digit rem = inplace_divrem1(scratch->ob_digit, | 
 | 1532 | 						     pin, size, powbase); | 
 | 1533 | 			pin = scratch->ob_digit; /* no need to use a again */ | 
 | 1534 | 			if (pin[size - 1] == 0) | 
 | 1535 | 				--size; | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1536 | 			SIGCHECK({ | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1537 | 				Py_DECREF(scratch); | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1538 | 				Py_DECREF(str); | 
 | 1539 | 				return NULL; | 
 | 1540 | 			}) | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1541 |  | 
 | 1542 | 			/* Break rem into digits. */ | 
| Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 1543 | 			assert(ntostore > 0); | 
 | 1544 | 			do { | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1545 | 				digit nextrem = (digit)(rem / base); | 
 | 1546 | 				char c = (char)(rem - nextrem * base); | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1547 | 				assert(p > PyUnicode_AS_UNICODE(str)); | 
| Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 1548 | 				c += (c < 10) ? '0' : 'a'-10; | 
| Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 1549 | 				*--p = c; | 
 | 1550 | 				rem = nextrem; | 
| Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 1551 | 				--ntostore; | 
 | 1552 | 				/* Termination is a bit delicate:  must not | 
 | 1553 | 				   store leading zeroes, so must get out if | 
| Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 1554 | 				   remaining quotient and rem are both 0. */ | 
 | 1555 | 			} while (ntostore && (size || rem)); | 
 | 1556 | 		} while (size != 0); | 
 | 1557 | 		Py_DECREF(scratch); | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1558 | 	} | 
 | 1559 |  | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1560 | 	if (base == 16) { | 
| Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 1561 | 		*--p = 'x'; | 
 | 1562 | 		*--p = '0'; | 
 | 1563 | 	} | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1564 | 	else if (base == 8) { | 
 | 1565 | 		*--p = 'o'; | 
 | 1566 | 		*--p = '0'; | 
 | 1567 | 	} | 
 | 1568 | 	else if (base == 2) { | 
 | 1569 | 		*--p = 'b'; | 
 | 1570 | 		*--p = '0'; | 
 | 1571 | 	} | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1572 | 	else if (base != 10) { | 
 | 1573 | 		*--p = '#'; | 
 | 1574 | 		*--p = '0' + base%10; | 
 | 1575 | 		if (base > 10) | 
 | 1576 | 			*--p = '0' + base/10; | 
 | 1577 | 	} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1578 | 	if (sign) | 
 | 1579 | 		*--p = sign; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1580 | 	if (p != PyUnicode_AS_UNICODE(str)) { | 
 | 1581 | 		Py_UNICODE *q = PyUnicode_AS_UNICODE(str); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1582 | 		assert(p > q); | 
 | 1583 | 		do { | 
 | 1584 | 		} while ((*q++ = *p++) != '\0'); | 
| Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1585 | 		q--; | 
| Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 1586 | 		if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) { | 
 | 1587 | 			Py_DECREF(str); | 
 | 1588 | 			return NULL; | 
 | 1589 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1590 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1591 | 	return (PyObject *)str; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1592 | } | 
 | 1593 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1594 | /* Table of digit values for 8-bit string -> integer conversion. | 
 | 1595 |  * '0' maps to 0, ..., '9' maps to 9. | 
 | 1596 |  * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. | 
 | 1597 |  * All other indices map to 37. | 
 | 1598 |  * Note that when converting a base B string, a char c is a legitimate | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1599 |  * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1600 |  */ | 
 | 1601 | int _PyLong_DigitValue[256] = { | 
 | 1602 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1603 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1604 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1605 | 	0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  37, 37, 37, 37, 37, 37, | 
 | 1606 | 	37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | 
 | 1607 | 	25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, | 
 | 1608 | 	37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | 
 | 1609 | 	25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, | 
 | 1610 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1611 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1612 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1613 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1614 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1615 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1616 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1617 | 	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 
 | 1618 | }; | 
 | 1619 |  | 
 | 1620 | /* *str points to the first digit in a string of base `base` digits.  base | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1621 |  * is a power of 2 (2, 4, 8, 16, or 32).  *str is set to point to the first | 
 | 1622 |  * non-digit (which may be *str!).  A normalized long is returned. | 
 | 1623 |  * The point to this routine is that it takes time linear in the number of | 
 | 1624 |  * string characters. | 
 | 1625 |  */ | 
 | 1626 | static PyLongObject * | 
 | 1627 | long_from_binary_base(char **str, int base) | 
 | 1628 | { | 
 | 1629 | 	char *p = *str; | 
 | 1630 | 	char *start = p; | 
 | 1631 | 	int bits_per_char; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1632 | 	Py_ssize_t n; | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1633 | 	PyLongObject *z; | 
 | 1634 | 	twodigits accum; | 
 | 1635 | 	int bits_in_accum; | 
 | 1636 | 	digit *pdigit; | 
 | 1637 |  | 
 | 1638 | 	assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); | 
 | 1639 | 	n = base; | 
 | 1640 | 	for (bits_per_char = -1; n; ++bits_per_char) | 
 | 1641 | 		n >>= 1; | 
 | 1642 | 	/* n <- total # of bits needed, while setting p to end-of-string */ | 
 | 1643 | 	n = 0; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1644 | 	while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1645 | 		++p; | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1646 | 	*str = p; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1647 | 	/* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ | 
 | 1648 | 	n = (p - start) * bits_per_char + PyLong_SHIFT - 1; | 
| Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1649 | 	if (n / bits_per_char < p - start) { | 
| Tim Peters | 1a3b19a | 2003-02-02 17:33:53 +0000 | [diff] [blame] | 1650 | 		PyErr_SetString(PyExc_ValueError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1651 | 				"int string too large to convert"); | 
| Tim Peters | 1a3b19a | 2003-02-02 17:33:53 +0000 | [diff] [blame] | 1652 | 		return NULL; | 
 | 1653 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1654 | 	n = n / PyLong_SHIFT; | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1655 | 	z = _PyLong_New(n); | 
 | 1656 | 	if (z == NULL) | 
 | 1657 | 		return NULL; | 
 | 1658 | 	/* Read string from right, and fill in long from left; i.e., | 
 | 1659 | 	 * from least to most significant in both. | 
 | 1660 | 	 */ | 
 | 1661 | 	accum = 0; | 
 | 1662 | 	bits_in_accum = 0; | 
 | 1663 | 	pdigit = z->ob_digit; | 
 | 1664 | 	while (--p >= start) { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1665 | 		int k = _PyLong_DigitValue[Py_CHARMASK(*p)]; | 
| Tim Peters | c7bc0b9 | 2003-05-05 20:39:43 +0000 | [diff] [blame] | 1666 | 		assert(k >= 0 && k < base); | 
 | 1667 | 		accum |= (twodigits)(k << bits_in_accum); | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1668 | 		bits_in_accum += bits_per_char; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1669 | 		if (bits_in_accum >= PyLong_SHIFT) { | 
 | 1670 | 			*pdigit++ = (digit)(accum & PyLong_MASK); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1671 | 			assert(pdigit - z->ob_digit <= (int)n); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1672 | 			accum >>= PyLong_SHIFT; | 
 | 1673 | 			bits_in_accum -= PyLong_SHIFT; | 
 | 1674 | 			assert(bits_in_accum < PyLong_SHIFT); | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1675 | 		} | 
 | 1676 | 	} | 
 | 1677 | 	if (bits_in_accum) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1678 | 		assert(bits_in_accum <= PyLong_SHIFT); | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1679 | 		*pdigit++ = (digit)accum; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1680 | 		assert(pdigit - z->ob_digit <= (int)n); | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1681 | 	} | 
 | 1682 | 	while (pdigit - z->ob_digit < n) | 
 | 1683 | 		*pdigit++ = 0; | 
 | 1684 | 	return long_normalize(z); | 
 | 1685 | } | 
 | 1686 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1687 | PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1688 | PyLong_FromString(char *str, char **pend, int base) | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1689 | { | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1690 | 	int sign = 1, error_if_nonzero = 0; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1691 | 	char *start, *orig_str = str; | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1692 | 	PyLongObject *z = NULL; | 
| Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 1693 | 	PyObject *strobj; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1694 | 	Py_ssize_t slen; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1695 |  | 
| Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 1696 | 	if ((base != 0 && base < 2) || base > 36) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1697 | 		PyErr_SetString(PyExc_ValueError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1698 | 				"int() arg 2 must be >= 2 and <= 36"); | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1699 | 		return NULL; | 
 | 1700 | 	} | 
| Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1701 | 	while (*str != '\0' && isspace(Py_CHARMASK(*str))) | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1702 | 		str++; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1703 | 	if (*str == '+') | 
 | 1704 | 		++str; | 
 | 1705 | 	else if (*str == '-') { | 
 | 1706 | 		++str; | 
 | 1707 | 		sign = -1; | 
 | 1708 | 	} | 
| Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1709 | 	while (*str != '\0' && isspace(Py_CHARMASK(*str))) | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1710 | 		str++; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1711 | 	if (base == 0) { | 
 | 1712 | 		if (str[0] != '0') | 
 | 1713 | 			base = 10; | 
 | 1714 | 		else if (str[1] == 'x' || str[1] == 'X') | 
 | 1715 | 			base = 16; | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1716 | 		else if (str[1] == 'o' || str[1] == 'O') | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1717 | 			base = 8; | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1718 | 		else if (str[1] == 'b' || str[1] == 'B') | 
 | 1719 | 			base = 2; | 
 | 1720 | 		else { | 
 | 1721 | 			/* "old" (C-style) octal literal, now invalid. | 
 | 1722 | 			   it might still be zero though */ | 
 | 1723 | 			error_if_nonzero = 1; | 
 | 1724 | 			base = 10; | 
 | 1725 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1726 | 	} | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1727 | 	if (str[0] == '0' && | 
 | 1728 | 	    ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || | 
 | 1729 | 	     (base == 8  && (str[1] == 'o' || str[1] == 'O')) || | 
 | 1730 | 	     (base == 2  && (str[1] == 'b' || str[1] == 'B')))) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1731 | 		str += 2; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1732 |  | 
| Guido van Rossum | e676297 | 1998-06-22 03:54:46 +0000 | [diff] [blame] | 1733 | 	start = str; | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1734 | 	if ((base & (base - 1)) == 0) | 
 | 1735 | 		z = long_from_binary_base(&str, base); | 
 | 1736 | 	else { | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1737 | /*** | 
 | 1738 | Binary bases can be converted in time linear in the number of digits, because | 
 | 1739 | Python's representation base is binary.  Other bases (including decimal!) use | 
 | 1740 | the simple quadratic-time algorithm below, complicated by some speed tricks. | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 1741 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1742 | First some math:  the largest integer that can be expressed in N base-B digits | 
 | 1743 | is B**N-1.  Consequently, if we have an N-digit input in base B, the worst- | 
 | 1744 | case number of Python digits needed to hold it is the smallest integer n s.t. | 
 | 1745 |  | 
 | 1746 |     BASE**n-1 >= B**N-1  [or, adding 1 to both sides] | 
 | 1747 |     BASE**n >= B**N      [taking logs to base BASE] | 
 | 1748 |     n >= log(B**N)/log(BASE) = N * log(B)/log(BASE) | 
 | 1749 |  | 
 | 1750 | The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute | 
 | 1751 | this quickly.  A Python long with that much space is reserved near the start, | 
 | 1752 | and the result is computed into it. | 
 | 1753 |  | 
 | 1754 | The input string is actually treated as being in base base**i (i.e., i digits | 
 | 1755 | are processed at a time), where two more static arrays hold: | 
 | 1756 |  | 
 | 1757 |     convwidth_base[base] = the largest integer i such that base**i <= BASE | 
 | 1758 |     convmultmax_base[base] = base ** convwidth_base[base] | 
 | 1759 |  | 
 | 1760 | The first of these is the largest i such that i consecutive input digits | 
 | 1761 | must fit in a single Python digit.  The second is effectively the input | 
 | 1762 | base we're really using. | 
 | 1763 |  | 
 | 1764 | Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base | 
 | 1765 | convmultmax_base[base], the result is "simply" | 
 | 1766 |  | 
 | 1767 |    (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1 | 
 | 1768 |  | 
 | 1769 | where B = convmultmax_base[base]. | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1770 |  | 
 | 1771 | Error analysis:  as above, the number of Python digits `n` needed is worst- | 
 | 1772 | case | 
 | 1773 |  | 
 | 1774 |     n >= N * log(B)/log(BASE) | 
 | 1775 |  | 
 | 1776 | where `N` is the number of input digits in base `B`.  This is computed via | 
 | 1777 |  | 
 | 1778 |     size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; | 
 | 1779 |  | 
 | 1780 | below.  Two numeric concerns are how much space this can waste, and whether | 
 | 1781 | the computed result can be too small.  To be concrete, assume BASE = 2**15, | 
 | 1782 | which is the default (and it's unlikely anyone changes that). | 
 | 1783 |  | 
 | 1784 | Waste isn't a problem:  provided the first input digit isn't 0, the difference | 
 | 1785 | between the worst-case input with N digits and the smallest input with N | 
 | 1786 | digits is about a factor of B, but B is small compared to BASE so at most | 
 | 1787 | one allocated Python digit can remain unused on that count.  If | 
 | 1788 | N*log(B)/log(BASE) is mathematically an exact integer, then truncating that | 
 | 1789 | and adding 1 returns a result 1 larger than necessary.  However, that can't | 
 | 1790 | happen:  whenever B is a power of 2, long_from_binary_base() is called | 
 | 1791 | instead, and it's impossible for B**i to be an integer power of 2**15 when | 
 | 1792 | B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be | 
 | 1793 | an exact integer when B is not a power of 2, since B**i has a prime factor | 
 | 1794 | other than 2 in that case, but (2**15)**j's only prime factor is 2). | 
 | 1795 |  | 
 | 1796 | The computed result can be too small if the true value of N*log(B)/log(BASE) | 
 | 1797 | is a little bit larger than an exact integer, but due to roundoff errors (in | 
 | 1798 | computing log(B), log(BASE), their quotient, and/or multiplying that by N) | 
 | 1799 | yields a numeric result a little less than that integer.  Unfortunately, "how | 
 | 1800 | close can a transcendental function get to an integer over some range?" | 
 | 1801 | questions are generally theoretically intractable.  Computer analysis via | 
 | 1802 | continued fractions is practical:  expand log(B)/log(BASE) via continued | 
 | 1803 | fractions, giving a sequence i/j of "the best" rational approximations.  Then | 
 | 1804 | j*log(B)/log(BASE) is approximately equal to (the integer) i.  This shows that | 
 | 1805 | we can get very close to being in trouble, but very rarely.  For example, | 
 | 1806 | 76573 is a denominator in one of the continued-fraction approximations to | 
 | 1807 | log(10)/log(2**15), and indeed: | 
 | 1808 |  | 
 | 1809 |     >>> log(10)/log(2**15)*76573 | 
 | 1810 |     16958.000000654003 | 
 | 1811 |  | 
 | 1812 | is very close to an integer.  If we were working with IEEE single-precision, | 
 | 1813 | rounding errors could kill us.  Finding worst cases in IEEE double-precision | 
 | 1814 | requires better-than-double-precision log() functions, and Tim didn't bother. | 
 | 1815 | Instead the code checks to see whether the allocated space is enough as each | 
 | 1816 | new Python digit is added, and copies the whole thing to a larger long if not. | 
 | 1817 | This should happen extremely rarely, and in fact I don't have a test case | 
 | 1818 | that triggers it(!).  Instead the code was tested by artificially allocating | 
 | 1819 | just 1 digit at the start, so that the copying code was exercised for every | 
 | 1820 | digit beyond the first. | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1821 | ***/ | 
 | 1822 | 		register twodigits c;	/* current input character */ | 
 | 1823 | 		Py_ssize_t size_z; | 
 | 1824 | 		int i; | 
 | 1825 | 		int convwidth; | 
 | 1826 | 		twodigits convmultmax, convmult; | 
 | 1827 | 		digit *pz, *pzstop; | 
 | 1828 | 		char* scan; | 
 | 1829 |  | 
 | 1830 | 		static double log_base_BASE[37] = {0.0e0,}; | 
 | 1831 | 		static int convwidth_base[37] = {0,}; | 
 | 1832 | 		static twodigits convmultmax_base[37] = {0,}; | 
 | 1833 |  | 
 | 1834 | 		if (log_base_BASE[base] == 0.0) { | 
 | 1835 | 			twodigits convmax = base; | 
 | 1836 | 			int i = 1; | 
 | 1837 |  | 
 | 1838 | 			log_base_BASE[base] = log((double)base) / | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1839 | 						log((double)PyLong_BASE); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1840 | 			for (;;) { | 
 | 1841 | 				twodigits next = convmax * base; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1842 | 				if (next > PyLong_BASE) | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1843 | 					break; | 
 | 1844 | 				convmax = next; | 
 | 1845 | 				++i; | 
 | 1846 | 			} | 
 | 1847 | 			convmultmax_base[base] = convmax; | 
 | 1848 | 			assert(i > 0); | 
 | 1849 | 			convwidth_base[base] = i; | 
 | 1850 | 		} | 
 | 1851 |  | 
 | 1852 | 		/* Find length of the string of numeric characters. */ | 
 | 1853 | 		scan = str; | 
 | 1854 | 		while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) | 
 | 1855 | 			++scan; | 
 | 1856 |  | 
 | 1857 | 		/* Create a long object that can contain the largest possible | 
 | 1858 | 		 * integer with this base and length.  Note that there's no | 
 | 1859 | 		 * need to initialize z->ob_digit -- no slot is read up before | 
 | 1860 | 		 * being stored into. | 
 | 1861 | 		 */ | 
 | 1862 | 		size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1863 | 		/* Uncomment next line to test exceedingly rare copy code */ | 
 | 1864 | 		/* size_z = 1; */ | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1865 | 		assert(size_z > 0); | 
 | 1866 | 		z = _PyLong_New(size_z); | 
 | 1867 | 		if (z == NULL) | 
 | 1868 | 			return NULL; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1869 | 		Py_Size(z) = 0; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1870 |  | 
 | 1871 | 		/* `convwidth` consecutive input digits are treated as a single | 
 | 1872 | 		 * digit in base `convmultmax`. | 
 | 1873 | 		 */ | 
 | 1874 | 		convwidth = convwidth_base[base]; | 
 | 1875 | 		convmultmax = convmultmax_base[base]; | 
 | 1876 |  | 
 | 1877 | 		/* Work ;-) */ | 
 | 1878 | 		while (str < scan) { | 
 | 1879 | 			/* grab up to convwidth digits from the input string */ | 
 | 1880 | 			c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; | 
 | 1881 | 			for (i = 1; i < convwidth && str != scan; ++i, ++str) { | 
 | 1882 | 				c = (twodigits)(c *  base + | 
 | 1883 | 					_PyLong_DigitValue[Py_CHARMASK(*str)]); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1884 | 				assert(c < PyLong_BASE); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1885 | 			} | 
 | 1886 |  | 
 | 1887 | 			convmult = convmultmax; | 
 | 1888 | 			/* Calculate the shift only if we couldn't get | 
 | 1889 | 			 * convwidth digits. | 
 | 1890 | 			 */ | 
 | 1891 | 			if (i != convwidth) { | 
 | 1892 | 				convmult = base; | 
 | 1893 | 				for ( ; i > 1; --i) | 
 | 1894 | 					convmult *= base; | 
 | 1895 | 			} | 
 | 1896 |  | 
 | 1897 | 			/* Multiply z by convmult, and add c. */ | 
 | 1898 | 			pz = z->ob_digit; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1899 | 			pzstop = pz + Py_Size(z); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1900 | 			for (; pz < pzstop; ++pz) { | 
 | 1901 | 				c += (twodigits)*pz * convmult; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1902 | 				*pz = (digit)(c & PyLong_MASK); | 
 | 1903 | 				c >>= PyLong_SHIFT; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1904 | 			} | 
 | 1905 | 			/* carry off the current end? */ | 
 | 1906 | 			if (c) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1907 | 				assert(c < PyLong_BASE); | 
 | 1908 | 				if (Py_Size(z) < size_z) { | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1909 | 					*pz = (digit)c; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1910 | 					++Py_Size(z); | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1911 | 				} | 
 | 1912 | 				else { | 
 | 1913 | 					PyLongObject *tmp; | 
 | 1914 | 					/* Extremely rare.  Get more space. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1915 | 					assert(Py_Size(z) == size_z); | 
| Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1916 | 					tmp = _PyLong_New(size_z + 1); | 
 | 1917 | 					if (tmp == NULL) { | 
 | 1918 | 						Py_DECREF(z); | 
 | 1919 | 						return NULL; | 
 | 1920 | 					} | 
 | 1921 | 					memcpy(tmp->ob_digit, | 
 | 1922 | 					       z->ob_digit, | 
 | 1923 | 					       sizeof(digit) * size_z); | 
 | 1924 | 					Py_DECREF(z); | 
 | 1925 | 					z = tmp; | 
 | 1926 | 					z->ob_digit[size_z] = (digit)c; | 
 | 1927 | 					++size_z; | 
 | 1928 | 				} | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1929 | 			} | 
| Tim Peters | bf2674b | 2003-02-02 07:51:32 +0000 | [diff] [blame] | 1930 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1931 | 	} | 
| Guido van Rossum | ac6a37a | 1998-08-04 15:04:06 +0000 | [diff] [blame] | 1932 | 	if (z == NULL) | 
 | 1933 | 		return NULL; | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1934 | 	if (error_if_nonzero) { | 
 | 1935 | 		/* reset the base to 0, else the exception message | 
 | 1936 | 		   doesn't make too much sense */ | 
 | 1937 | 		base = 0; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1938 | 		if (Py_Size(z) != 0) | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1939 | 			goto onError; | 
 | 1940 | 		/* there might still be other problems, therefore base | 
 | 1941 | 		   remains zero here for the same reason */ | 
 | 1942 | 	} | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1943 | 	if (str == start) | 
 | 1944 | 		goto onError; | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1945 | 	if (sign < 0) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1946 | 		Py_Size(z) = -(Py_Size(z)); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1947 | 	if (*str == 'L' || *str == 'l') | 
 | 1948 | 		str++; | 
 | 1949 | 	while (*str && isspace(Py_CHARMASK(*str))) | 
 | 1950 | 		str++; | 
 | 1951 | 	if (*str != '\0') | 
 | 1952 | 		goto onError; | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1953 | 	if (pend) | 
 | 1954 | 		*pend = str; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1955 | 	return (PyObject *) z; | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1956 |  | 
 | 1957 |  onError: | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1958 | 	Py_XDECREF(z); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1959 | 	slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; | 
| Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 1960 | 	strobj = PyUnicode_FromStringAndSize(orig_str, slen); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1961 | 	if (strobj == NULL) | 
 | 1962 | 		return NULL; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1963 | 	PyErr_Format(PyExc_ValueError, | 
| Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 1964 | 		     "invalid literal for int() with base %d: %R", | 
 | 1965 | 		     base, strobj); | 
 | 1966 | 	Py_DECREF(strobj); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1967 | 	return NULL; | 
 | 1968 | } | 
 | 1969 |  | 
 | 1970 | PyObject * | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1971 | PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1972 | { | 
| Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 1973 | 	PyObject *result; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1974 | 	char *buffer = (char *)PyMem_MALLOC(length+1); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1975 |  | 
| Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 1976 | 	if (buffer == NULL) | 
 | 1977 | 		return NULL; | 
 | 1978 |  | 
 | 1979 | 	if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) { | 
 | 1980 | 		PyMem_FREE(buffer); | 
| Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1981 | 		return NULL; | 
 | 1982 | 	} | 
| Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 1983 | 	result = PyLong_FromString(buffer, NULL, base); | 
 | 1984 | 	PyMem_FREE(buffer); | 
 | 1985 | 	return result; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1986 | } | 
 | 1987 |  | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1988 | /* forward */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1989 | static PyLongObject *x_divrem | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1990 | 	(PyLongObject *, PyLongObject *, PyLongObject **); | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 1991 | static PyObject *long_long(PyObject *v); | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1992 | static int long_divrem(PyLongObject *, PyLongObject *, | 
 | 1993 | 	PyLongObject **, PyLongObject **); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1994 |  | 
 | 1995 | /* Long division with remainder, top-level routine */ | 
 | 1996 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1997 | static int | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1998 | long_divrem(PyLongObject *a, PyLongObject *b, | 
 | 1999 | 	    PyLongObject **pdiv, PyLongObject **prem) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2000 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2001 | 	Py_ssize_t size_a = ABS(Py_Size(a)), size_b = ABS(Py_Size(b)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2002 | 	PyLongObject *z; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2003 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2004 | 	if (size_b == 0) { | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 2005 | 		PyErr_SetString(PyExc_ZeroDivisionError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2006 | 				"integer division or modulo by zero"); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2007 | 		return -1; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2008 | 	} | 
 | 2009 | 	if (size_a < size_b || | 
| Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 2010 | 	    (size_a == size_b && | 
 | 2011 | 	     a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2012 | 		/* |a| < |b|. */ | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2013 | 		*pdiv = (PyLongObject*)PyLong_FromLong(0); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2014 | 		if (*pdiv == NULL) | 
 | 2015 | 			return -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2016 | 		Py_INCREF(a); | 
 | 2017 | 		*prem = (PyLongObject *) a; | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2018 | 		return 0; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2019 | 	} | 
 | 2020 | 	if (size_b == 1) { | 
 | 2021 | 		digit rem = 0; | 
 | 2022 | 		z = divrem1(a, b->ob_digit[0], &rem); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2023 | 		if (z == NULL) | 
 | 2024 | 			return -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2025 | 		*prem = (PyLongObject *) PyLong_FromLong((long)rem); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2026 | 		if (*prem == NULL) { | 
 | 2027 | 			Py_DECREF(z); | 
 | 2028 | 			return -1; | 
 | 2029 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2030 | 	} | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2031 | 	else { | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2032 | 		z = x_divrem(a, b, prem); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2033 | 		if (z == NULL) | 
 | 2034 | 			return -1; | 
 | 2035 | 	} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2036 | 	/* Set the signs. | 
 | 2037 | 	   The quotient z has the sign of a*b; | 
 | 2038 | 	   the remainder r has the sign of a, | 
 | 2039 | 	   so a = b*z + r. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2040 | 	if ((Py_Size(a) < 0) != (Py_Size(b) < 0)) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2041 | 		NEGATE(z); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2042 | 	if (Py_Size(a) < 0 && Py_Size(*prem) != 0) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2043 | 		NEGATE(*prem); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2044 | 	*pdiv = z; | 
 | 2045 | 	return 0; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2046 | } | 
 | 2047 |  | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2048 | /* Unsigned long division with remainder -- the algorithm */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2049 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2050 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2051 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2052 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2053 | 	Py_ssize_t size_v = ABS(Py_Size(v1)), size_w = ABS(Py_Size(w1)); | 
 | 2054 | 	digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2055 | 	PyLongObject *v = mul1(v1, d); | 
 | 2056 | 	PyLongObject *w = mul1(w1, d); | 
 | 2057 | 	PyLongObject *a; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2058 | 	Py_ssize_t j, k; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2059 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2060 | 	if (v == NULL || w == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2061 | 		Py_XDECREF(v); | 
 | 2062 | 		Py_XDECREF(w); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2063 | 		return NULL; | 
 | 2064 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2065 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2066 | 	assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2067 | 	assert(Py_Refcnt(v) == 1); /* Since v will be used as accumulator! */ | 
 | 2068 | 	assert(size_w == ABS(Py_Size(w))); /* That's how d was calculated */ | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2069 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2070 | 	size_v = ABS(Py_Size(v)); | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2071 | 	k = size_v - size_w; | 
 | 2072 | 	a = _PyLong_New(k + 1); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2073 |  | 
| Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2074 | 	for (j = size_v; a != NULL && k >= 0; --j, --k) { | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2075 | 		digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; | 
 | 2076 | 		twodigits q; | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2077 | 		stwodigits carry = 0; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2078 | 		int i; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2079 |  | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2080 | 		SIGCHECK({ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2081 | 			Py_DECREF(a); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2082 | 			a = NULL; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2083 | 			break; | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2084 | 		}) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2085 | 		if (vj == w->ob_digit[size_w-1]) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2086 | 			q = PyLong_MASK; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2087 | 		else | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2088 | 			q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) / | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2089 | 				w->ob_digit[size_w-1]; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2090 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2091 | 		while (w->ob_digit[size_w-2]*q > | 
 | 2092 | 				(( | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2093 | 					((twodigits)vj << PyLong_SHIFT) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2094 | 					+ v->ob_digit[j-1] | 
 | 2095 | 					- q*w->ob_digit[size_w-1] | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2096 | 								) << PyLong_SHIFT) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2097 | 				+ v->ob_digit[j-2]) | 
 | 2098 | 			--q; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2099 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2100 | 		for (i = 0; i < size_w && i+k < size_v; ++i) { | 
 | 2101 | 			twodigits z = w->ob_digit[i] * q; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2102 | 			digit zz = (digit) (z >> PyLong_SHIFT); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2103 | 			carry += v->ob_digit[i+k] - z | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2104 | 				+ ((twodigits)zz << PyLong_SHIFT); | 
 | 2105 | 			v->ob_digit[i+k] = (digit)(carry & PyLong_MASK); | 
| Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2106 | 			carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2107 | 							  carry, PyLong_SHIFT); | 
| Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2108 | 			carry -= zz; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2109 | 		} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2110 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2111 | 		if (i+k < size_v) { | 
 | 2112 | 			carry += v->ob_digit[i+k]; | 
 | 2113 | 			v->ob_digit[i+k] = 0; | 
 | 2114 | 		} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2115 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2116 | 		if (carry == 0) | 
| Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 2117 | 			a->ob_digit[k] = (digit) q; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2118 | 		else { | 
 | 2119 | 			assert(carry == -1); | 
| Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 2120 | 			a->ob_digit[k] = (digit) q-1; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2121 | 			carry = 0; | 
 | 2122 | 			for (i = 0; i < size_w && i+k < size_v; ++i) { | 
 | 2123 | 				carry += v->ob_digit[i+k] + w->ob_digit[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2124 | 				v->ob_digit[i+k] = (digit)(carry & PyLong_MASK); | 
| Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 2125 | 				carry = Py_ARITHMETIC_RIGHT_SHIFT( | 
 | 2126 | 						BASE_TWODIGITS_TYPE, | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2127 | 						carry, PyLong_SHIFT); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2128 | 			} | 
 | 2129 | 		} | 
 | 2130 | 	} /* for j, k */ | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2131 |  | 
| Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 2132 | 	if (a == NULL) | 
 | 2133 | 		*prem = NULL; | 
 | 2134 | 	else { | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2135 | 		a = long_normalize(a); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2136 | 		*prem = divrem1(v, d, &d); | 
 | 2137 | 		/* d receives the (unused) remainder */ | 
 | 2138 | 		if (*prem == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2139 | 			Py_DECREF(a); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2140 | 			a = NULL; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2141 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2142 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2143 | 	Py_DECREF(v); | 
 | 2144 | 	Py_DECREF(w); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2145 | 	return a; | 
 | 2146 | } | 
 | 2147 |  | 
 | 2148 | /* Methods */ | 
 | 2149 |  | 
 | 2150 | static void | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2151 | long_dealloc(PyObject *v) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2152 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2153 | 	Py_Type(v)->tp_free(v); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2154 | } | 
 | 2155 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2156 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2157 | long_repr(PyObject *v) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2158 | { | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 2159 | 	return _PyLong_Format(v, 10); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2160 | } | 
 | 2161 |  | 
 | 2162 | static int | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2163 | long_compare(PyLongObject *a, PyLongObject *b) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2164 | { | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2165 | 	Py_ssize_t sign; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2166 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2167 | 	if (Py_Size(a) != Py_Size(b)) { | 
 | 2168 | 		if (ABS(Py_Size(a)) == 0 && ABS(Py_Size(b)) == 0) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2169 | 			sign = 0; | 
 | 2170 | 		else | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2171 | 			sign = Py_Size(a) - Py_Size(b); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2172 | 	} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2173 | 	else { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2174 | 		Py_ssize_t i = ABS(Py_Size(a)); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2175 | 		while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | 
 | 2176 | 			; | 
 | 2177 | 		if (i < 0) | 
 | 2178 | 			sign = 0; | 
| Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 2179 | 		else { | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2180 | 			sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2181 | 			if (Py_Size(a) < 0) | 
| Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 2182 | 				sign = -sign; | 
 | 2183 | 		} | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2184 | 	} | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2185 | 	return sign < 0 ? -1 : sign > 0 ? 1 : 0; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2186 | } | 
 | 2187 |  | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2188 | static PyObject * | 
 | 2189 | long_richcompare(PyObject *self, PyObject *other, int op) | 
 | 2190 | { | 
 | 2191 | 	PyLongObject *a, *b; | 
| Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2192 | 	PyObject *result; | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2193 | 	CONVERT_BINOP((PyObject *)self, (PyObject *)other, &a, &b); | 
| Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2194 | 	result = Py_CmpToRich(op, long_compare(a, b)); | 
 | 2195 | 	Py_DECREF(a); | 
 | 2196 | 	Py_DECREF(b); | 
 | 2197 | 	return result; | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 2198 | } | 
 | 2199 |  | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2200 | static long | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2201 | long_hash(PyLongObject *v) | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2202 | { | 
 | 2203 | 	long x; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2204 | 	Py_ssize_t i; | 
 | 2205 | 	int sign; | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2206 |  | 
 | 2207 | 	/* This is designed so that Python ints and longs with the | 
 | 2208 | 	   same value hash to the same value, otherwise comparisons | 
 | 2209 | 	   of mapping keys will turn out weird */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2210 | 	i = Py_Size(v); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2211 | 	switch(i) { | 
 | 2212 | 	case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0]; | 
 | 2213 | 	case 0: return 0; | 
 | 2214 | 	case 1: return v->ob_digit[0]; | 
 | 2215 | 	} | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2216 | 	sign = 1; | 
 | 2217 | 	x = 0; | 
 | 2218 | 	if (i < 0) { | 
 | 2219 | 		sign = -1; | 
 | 2220 | 		i = -(i); | 
 | 2221 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2222 | #define LONG_BIT_PyLong_SHIFT	(8*sizeof(long) - PyLong_SHIFT) | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2223 | 	while (--i >= 0) { | 
| Neal Norwitz | d5a65a7 | 2003-02-23 23:11:41 +0000 | [diff] [blame] | 2224 | 		/* Force a native long #-bits (32 or 64) circular shift */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2225 | 		x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK); | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2226 | 		x += v->ob_digit[i]; | 
 | 2227 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2228 | #undef LONG_BIT_PyLong_SHIFT | 
| Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2229 | 	x = x * sign; | 
 | 2230 | 	if (x == -1) | 
 | 2231 | 		x = -2; | 
 | 2232 | 	return x; | 
 | 2233 | } | 
 | 2234 |  | 
 | 2235 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2236 | /* Add the absolute values of two long integers. */ | 
 | 2237 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2238 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2239 | x_add(PyLongObject *a, PyLongObject *b) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2240 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2241 | 	Py_ssize_t size_a = ABS(Py_Size(a)), size_b = ABS(Py_Size(b)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2242 | 	PyLongObject *z; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2243 | 	int i; | 
 | 2244 | 	digit carry = 0; | 
| Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2245 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2246 | 	/* Ensure a is the larger of the two: */ | 
 | 2247 | 	if (size_a < size_b) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2248 | 		{ PyLongObject *temp = a; a = b; b = temp; } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2249 | 		{ Py_ssize_t size_temp = size_a; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2250 | 		  size_a = size_b; | 
 | 2251 | 		  size_b = size_temp; } | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2252 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2253 | 	z = _PyLong_New(size_a+1); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2254 | 	if (z == NULL) | 
 | 2255 | 		return NULL; | 
 | 2256 | 	for (i = 0; i < size_b; ++i) { | 
 | 2257 | 		carry += a->ob_digit[i] + b->ob_digit[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2258 | 		z->ob_digit[i] = carry & PyLong_MASK; | 
 | 2259 | 		carry >>= PyLong_SHIFT; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2260 | 	} | 
 | 2261 | 	for (; i < size_a; ++i) { | 
 | 2262 | 		carry += a->ob_digit[i]; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2263 | 		z->ob_digit[i] = carry & PyLong_MASK; | 
 | 2264 | 		carry >>= PyLong_SHIFT; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2265 | 	} | 
 | 2266 | 	z->ob_digit[i] = carry; | 
 | 2267 | 	return long_normalize(z); | 
 | 2268 | } | 
 | 2269 |  | 
 | 2270 | /* Subtract the absolute values of two integers. */ | 
 | 2271 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2272 | static PyLongObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2273 | x_sub(PyLongObject *a, PyLongObject *b) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2274 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2275 | 	Py_ssize_t size_a = ABS(Py_Size(a)), size_b = ABS(Py_Size(b)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2276 | 	PyLongObject *z; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2277 | 	Py_ssize_t i; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2278 | 	int sign = 1; | 
 | 2279 | 	digit borrow = 0; | 
| Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2280 |  | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2281 | 	/* Ensure a is the larger of the two: */ | 
 | 2282 | 	if (size_a < size_b) { | 
 | 2283 | 		sign = -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2284 | 		{ PyLongObject *temp = a; a = b; b = temp; } | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2285 | 		{ Py_ssize_t size_temp = size_a; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2286 | 		  size_a = size_b; | 
 | 2287 | 		  size_b = size_temp; } | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2288 | 	} | 
 | 2289 | 	else if (size_a == size_b) { | 
 | 2290 | 		/* Find highest digit where a and b differ: */ | 
 | 2291 | 		i = size_a; | 
 | 2292 | 		while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | 
 | 2293 | 			; | 
 | 2294 | 		if (i < 0) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2295 | 			return _PyLong_New(0); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2296 | 		if (a->ob_digit[i] < b->ob_digit[i]) { | 
 | 2297 | 			sign = -1; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2298 | 			{ PyLongObject *temp = a; a = b; b = temp; } | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2299 | 		} | 
 | 2300 | 		size_a = size_b = i+1; | 
 | 2301 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2302 | 	z = _PyLong_New(size_a); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2303 | 	if (z == NULL) | 
 | 2304 | 		return NULL; | 
 | 2305 | 	for (i = 0; i < size_b; ++i) { | 
 | 2306 | 		/* The following assumes unsigned arithmetic | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2307 | 		   works module 2**N for some N>PyLong_SHIFT. */ | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2308 | 		borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2309 | 		z->ob_digit[i] = borrow & PyLong_MASK; | 
 | 2310 | 		borrow >>= PyLong_SHIFT; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2311 | 		borrow &= 1; /* Keep only one sign bit */ | 
 | 2312 | 	} | 
 | 2313 | 	for (; i < size_a; ++i) { | 
 | 2314 | 		borrow = a->ob_digit[i] - borrow; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2315 | 		z->ob_digit[i] = borrow & PyLong_MASK; | 
 | 2316 | 		borrow >>= PyLong_SHIFT; | 
| Tim Peters | 43f04a3 | 2000-07-08 02:26:47 +0000 | [diff] [blame] | 2317 | 		borrow &= 1; /* Keep only one sign bit */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2318 | 	} | 
 | 2319 | 	assert(borrow == 0); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2320 | 	if (sign < 0) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2321 | 		NEGATE(z); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2322 | 	return long_normalize(z); | 
 | 2323 | } | 
 | 2324 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2325 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2326 | long_add(PyLongObject *v, PyLongObject *w) | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2327 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2328 | 	PyLongObject *a, *b, *z; | 
| Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 2329 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2330 | 	CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); | 
 | 2331 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2332 | 	if (ABS(Py_Size(a)) <= 1 && ABS(Py_Size(b)) <= 1) { | 
| Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2333 | 		PyObject *result = PyInt_FromLong(MEDIUM_VALUE(a) + | 
 | 2334 | 						  MEDIUM_VALUE(b)); | 
 | 2335 | 		Py_DECREF(a); | 
 | 2336 | 		Py_DECREF(b); | 
 | 2337 | 		return result; | 
 | 2338 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2339 | 	if (Py_Size(a) < 0) { | 
 | 2340 | 		if (Py_Size(b) < 0) { | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2341 | 			z = x_add(a, b); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2342 | 			if (z != NULL && Py_Size(z) != 0) | 
 | 2343 | 				Py_Size(z) = -(Py_Size(z)); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2344 | 		} | 
 | 2345 | 		else | 
 | 2346 | 			z = x_sub(b, a); | 
 | 2347 | 	} | 
 | 2348 | 	else { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2349 | 		if (Py_Size(b) < 0) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2350 | 			z = x_sub(a, b); | 
 | 2351 | 		else | 
 | 2352 | 			z = x_add(a, b); | 
 | 2353 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2354 | 	Py_DECREF(a); | 
 | 2355 | 	Py_DECREF(b); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2356 | 	return (PyObject *)z; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2357 | } | 
 | 2358 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2359 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2360 | long_sub(PyLongObject *v, PyLongObject *w) | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2361 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2362 | 	PyLongObject *a, *b, *z; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2363 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2364 | 	CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); | 
 | 2365 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2366 | 	if (ABS(Py_Size(a)) <= 1 && ABS(Py_Size(b)) <= 1) { | 
| Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2367 | 		PyObject* r; | 
 | 2368 | 		r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); | 
 | 2369 | 		Py_DECREF(a); | 
 | 2370 | 		Py_DECREF(b); | 
 | 2371 | 		return r; | 
 | 2372 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2373 | 	if (Py_Size(a) < 0) { | 
 | 2374 | 		if (Py_Size(b) < 0) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2375 | 			z = x_sub(a, b); | 
 | 2376 | 		else | 
 | 2377 | 			z = x_add(a, b); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2378 | 		if (z != NULL && Py_Size(z) != 0) | 
 | 2379 | 			Py_Size(z) = -(Py_Size(z)); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2380 | 	} | 
 | 2381 | 	else { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2382 | 		if (Py_Size(b) < 0) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2383 | 			z = x_add(a, b); | 
 | 2384 | 		else | 
 | 2385 | 			z = x_sub(a, b); | 
 | 2386 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2387 | 	Py_DECREF(a); | 
 | 2388 | 	Py_DECREF(b); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2389 | 	return (PyObject *)z; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2390 | } | 
 | 2391 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2392 | /* Grade school multiplication, ignoring the signs. | 
 | 2393 |  * Returns the absolute value of the product, or NULL if error. | 
 | 2394 |  */ | 
 | 2395 | static PyLongObject * | 
 | 2396 | x_mul(PyLongObject *a, PyLongObject *b) | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2397 | { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2398 | 	PyLongObject *z; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2399 | 	Py_ssize_t size_a = ABS(Py_Size(a)); | 
 | 2400 | 	Py_ssize_t size_b = ABS(Py_Size(b)); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2401 | 	Py_ssize_t i; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2402 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2403 |      	z = _PyLong_New(size_a + size_b); | 
 | 2404 | 	if (z == NULL) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2405 | 		return NULL; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2406 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2407 | 	memset(z->ob_digit, 0, Py_Size(z) * sizeof(digit)); | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2408 | 	if (a == b) { | 
 | 2409 | 		/* Efficient squaring per HAC, Algorithm 14.16: | 
 | 2410 | 		 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf | 
 | 2411 | 		 * Gives slightly less than a 2x speedup when a == b, | 
 | 2412 | 		 * via exploiting that each entry in the multiplication | 
 | 2413 | 		 * pyramid appears twice (except for the size_a squares). | 
 | 2414 | 		 */ | 
 | 2415 | 		for (i = 0; i < size_a; ++i) { | 
 | 2416 | 			twodigits carry; | 
 | 2417 | 			twodigits f = a->ob_digit[i]; | 
 | 2418 | 			digit *pz = z->ob_digit + (i << 1); | 
 | 2419 | 			digit *pa = a->ob_digit + i + 1; | 
 | 2420 | 			digit *paend = a->ob_digit + size_a; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2421 |  | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2422 | 			SIGCHECK({ | 
 | 2423 | 				Py_DECREF(z); | 
 | 2424 | 				return NULL; | 
 | 2425 | 			}) | 
 | 2426 |  | 
 | 2427 | 			carry = *pz + f * f; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2428 | 			*pz++ = (digit)(carry & PyLong_MASK); | 
 | 2429 | 			carry >>= PyLong_SHIFT; | 
 | 2430 | 			assert(carry <= PyLong_MASK); | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2431 |  | 
 | 2432 | 			/* Now f is added in twice in each column of the | 
 | 2433 | 			 * pyramid it appears.  Same as adding f<<1 once. | 
 | 2434 | 			 */ | 
 | 2435 | 			f <<= 1; | 
 | 2436 | 			while (pa < paend) { | 
 | 2437 | 				carry += *pz + *pa++ * f; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2438 | 				*pz++ = (digit)(carry & PyLong_MASK); | 
 | 2439 | 				carry >>= PyLong_SHIFT; | 
 | 2440 | 				assert(carry <= (PyLong_MASK << 1)); | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2441 | 			} | 
 | 2442 | 			if (carry) { | 
 | 2443 | 				carry += *pz; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2444 | 				*pz++ = (digit)(carry & PyLong_MASK); | 
 | 2445 | 				carry >>= PyLong_SHIFT; | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2446 | 			} | 
 | 2447 | 			if (carry) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2448 | 				*pz += (digit)(carry & PyLong_MASK); | 
 | 2449 | 			assert((carry >> PyLong_SHIFT) == 0); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2450 | 		} | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2451 | 	} | 
 | 2452 | 	else {	/* a is not the same as b -- gradeschool long mult */ | 
 | 2453 | 		for (i = 0; i < size_a; ++i) { | 
 | 2454 | 			twodigits carry = 0; | 
 | 2455 | 			twodigits f = a->ob_digit[i]; | 
 | 2456 | 			digit *pz = z->ob_digit + i; | 
 | 2457 | 			digit *pb = b->ob_digit; | 
 | 2458 | 			digit *pbend = b->ob_digit + size_b; | 
 | 2459 |  | 
 | 2460 | 			SIGCHECK({ | 
 | 2461 | 				Py_DECREF(z); | 
 | 2462 | 				return NULL; | 
 | 2463 | 			}) | 
 | 2464 |  | 
 | 2465 | 			while (pb < pbend) { | 
 | 2466 | 				carry += *pz + *pb++ * f; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2467 | 				*pz++ = (digit)(carry & PyLong_MASK); | 
 | 2468 | 				carry >>= PyLong_SHIFT; | 
 | 2469 | 				assert(carry <= PyLong_MASK); | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2470 | 			} | 
 | 2471 | 			if (carry) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2472 | 				*pz += (digit)(carry & PyLong_MASK); | 
 | 2473 | 			assert((carry >> PyLong_SHIFT) == 0); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2474 | 		} | 
 | 2475 | 	} | 
| Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2476 | 	return long_normalize(z); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2477 | } | 
 | 2478 |  | 
 | 2479 | /* A helper for Karatsuba multiplication (k_mul). | 
 | 2480 |    Takes a long "n" and an integer "size" representing the place to | 
 | 2481 |    split, and sets low and high such that abs(n) == (high << size) + low, | 
 | 2482 |    viewing the shift as being by digits.  The sign bit is ignored, and | 
 | 2483 |    the return values are >= 0. | 
 | 2484 |    Returns 0 on success, -1 on failure. | 
 | 2485 | */ | 
 | 2486 | static int | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2487 | kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low) | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2488 | { | 
 | 2489 | 	PyLongObject *hi, *lo; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2490 | 	Py_ssize_t size_lo, size_hi; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2491 | 	const Py_ssize_t size_n = ABS(Py_Size(n)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2492 |  | 
 | 2493 | 	size_lo = MIN(size_n, size); | 
 | 2494 | 	size_hi = size_n - size_lo; | 
 | 2495 |  | 
 | 2496 | 	if ((hi = _PyLong_New(size_hi)) == NULL) | 
 | 2497 | 		return -1; | 
 | 2498 | 	if ((lo = _PyLong_New(size_lo)) == NULL) { | 
 | 2499 | 		Py_DECREF(hi); | 
 | 2500 | 		return -1; | 
 | 2501 | 	} | 
 | 2502 |  | 
 | 2503 | 	memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); | 
 | 2504 | 	memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)); | 
 | 2505 |  | 
 | 2506 | 	*high = long_normalize(hi); | 
 | 2507 | 	*low = long_normalize(lo); | 
 | 2508 | 	return 0; | 
 | 2509 | } | 
 | 2510 |  | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2511 | static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); | 
 | 2512 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2513 | /* Karatsuba multiplication.  Ignores the input signs, and returns the | 
 | 2514 |  * absolute value of the product (or NULL if error). | 
 | 2515 |  * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). | 
 | 2516 |  */ | 
 | 2517 | static PyLongObject * | 
 | 2518 | k_mul(PyLongObject *a, PyLongObject *b) | 
 | 2519 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2520 | 	Py_ssize_t asize = ABS(Py_Size(a)); | 
 | 2521 | 	Py_ssize_t bsize = ABS(Py_Size(b)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2522 | 	PyLongObject *ah = NULL; | 
 | 2523 | 	PyLongObject *al = NULL; | 
 | 2524 | 	PyLongObject *bh = NULL; | 
 | 2525 | 	PyLongObject *bl = NULL; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2526 | 	PyLongObject *ret = NULL; | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2527 | 	PyLongObject *t1, *t2, *t3; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2528 | 	Py_ssize_t shift;	/* the number of digits we split off */ | 
 | 2529 | 	Py_ssize_t i; | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2530 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2531 | 	/* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl | 
 | 2532 | 	 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh  + ah*bh + al*bl | 
 | 2533 | 	 * Then the original product is | 
| Tim Peters | 18c15b9 | 2002-08-12 02:43:58 +0000 | [diff] [blame] | 2534 | 	 *     ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2535 | 	 * By picking X to be a power of 2, "*X" is just shifting, and it's | 
 | 2536 | 	 * been reduced to 3 multiplies on numbers half the size. | 
 | 2537 | 	 */ | 
 | 2538 |  | 
| Tim Peters | fc07e56 | 2002-08-12 02:54:10 +0000 | [diff] [blame] | 2539 | 	/* We want to split based on the larger number; fiddle so that b | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2540 | 	 * is largest. | 
 | 2541 | 	 */ | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2542 | 	if (asize > bsize) { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2543 | 		t1 = a; | 
 | 2544 | 		a = b; | 
 | 2545 | 		b = t1; | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2546 |  | 
 | 2547 | 		i = asize; | 
 | 2548 | 		asize = bsize; | 
 | 2549 | 		bsize = i; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2550 | 	} | 
 | 2551 |  | 
 | 2552 | 	/* Use gradeschool math when either number is too small. */ | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2553 | 	i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; | 
 | 2554 | 	if (asize <= i) { | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2555 | 		if (asize == 0) | 
| Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2556 | 			return _PyLong_New(0); | 
 | 2557 | 		else | 
 | 2558 | 			return x_mul(a, b); | 
 | 2559 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2560 |  | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2561 | 	/* If a is small compared to b, splitting on b gives a degenerate | 
 | 2562 | 	 * case with ah==0, and Karatsuba may be (even much) less efficient | 
 | 2563 | 	 * than "grade school" then.  However, we can still win, by viewing | 
 | 2564 | 	 * b as a string of "big digits", each of width a->ob_size.  That | 
 | 2565 | 	 * leads to a sequence of balanced calls to k_mul. | 
 | 2566 | 	 */ | 
 | 2567 | 	if (2 * asize <= bsize) | 
 | 2568 | 		return k_lopsided_mul(a, b); | 
 | 2569 |  | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2570 | 	/* Split a & b into hi & lo pieces. */ | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2571 | 	shift = bsize >> 1; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2572 | 	if (kmul_split(a, shift, &ah, &al) < 0) goto fail; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2573 | 	assert(Py_Size(ah) > 0);	/* the split isn't degenerate */ | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2574 |  | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2575 | 	if (a == b) { | 
 | 2576 | 		bh = ah; | 
 | 2577 | 		bl = al; | 
 | 2578 | 		Py_INCREF(bh); | 
 | 2579 | 		Py_INCREF(bl); | 
 | 2580 | 	} | 
 | 2581 | 	else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2582 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2583 | 	/* The plan: | 
 | 2584 | 	 * 1. Allocate result space (asize + bsize digits:  that's always | 
 | 2585 | 	 *    enough). | 
 | 2586 | 	 * 2. Compute ah*bh, and copy into result at 2*shift. | 
 | 2587 | 	 * 3. Compute al*bl, and copy into result at 0.  Note that this | 
 | 2588 | 	 *    can't overlap with #2. | 
 | 2589 | 	 * 4. Subtract al*bl from the result, starting at shift.  This may | 
 | 2590 | 	 *    underflow (borrow out of the high digit), but we don't care: | 
 | 2591 | 	 *    we're effectively doing unsigned arithmetic mod | 
 | 2592 | 	 *    BASE**(sizea + sizeb), and so long as the *final* result fits, | 
 | 2593 | 	 *    borrows and carries out of the high digit can be ignored. | 
 | 2594 | 	 * 5. Subtract ah*bh from the result, starting at shift. | 
 | 2595 | 	 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting | 
 | 2596 | 	 *    at shift. | 
 | 2597 | 	 */ | 
 | 2598 |  | 
 | 2599 | 	/* 1. Allocate result space. */ | 
| Tim Peters | 70b041b | 2002-08-12 19:38:01 +0000 | [diff] [blame] | 2600 | 	ret = _PyLong_New(asize + bsize); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2601 | 	if (ret == NULL) goto fail; | 
 | 2602 | #ifdef Py_DEBUG | 
 | 2603 | 	/* Fill with trash, to catch reference to uninitialized digits. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2604 | 	memset(ret->ob_digit, 0xDF, Py_Size(ret) * sizeof(digit)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2605 | #endif | 
| Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2606 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2607 | 	/* 2. t1 <- ah*bh, and copy into high digits of result. */ | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2608 | 	if ((t1 = k_mul(ah, bh)) == NULL) goto fail; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2609 | 	assert(Py_Size(t1) >= 0); | 
 | 2610 | 	assert(2*shift + Py_Size(t1) <= Py_Size(ret)); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2611 | 	memcpy(ret->ob_digit + 2*shift, t1->ob_digit, | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2612 | 	       Py_Size(t1) * sizeof(digit)); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2613 |  | 
 | 2614 | 	/* Zero-out the digits higher than the ah*bh copy. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2615 | 	i = Py_Size(ret) - 2*shift - Py_Size(t1); | 
| Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2616 | 	if (i) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2617 | 		memset(ret->ob_digit + 2*shift + Py_Size(t1), 0, | 
| Tim Peters | 44121a6 | 2002-08-12 06:17:58 +0000 | [diff] [blame] | 2618 | 		       i * sizeof(digit)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2619 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2620 | 	/* 3. t2 <- al*bl, and copy into the low digits. */ | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2621 | 	if ((t2 = k_mul(al, bl)) == NULL) { | 
 | 2622 | 		Py_DECREF(t1); | 
 | 2623 | 		goto fail; | 
 | 2624 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2625 | 	assert(Py_Size(t2) >= 0); | 
 | 2626 | 	assert(Py_Size(t2) <= 2*shift); /* no overlap with high digits */ | 
 | 2627 | 	memcpy(ret->ob_digit, t2->ob_digit, Py_Size(t2) * sizeof(digit)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2628 |  | 
 | 2629 | 	/* Zero out remaining digits. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2630 | 	i = 2*shift - Py_Size(t2);	/* number of uninitialized digits */ | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2631 | 	if (i) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2632 | 		memset(ret->ob_digit + Py_Size(t2), 0, i * sizeof(digit)); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2633 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2634 | 	/* 4 & 5. Subtract ah*bh (t1) and al*bl (t2).  We do al*bl first | 
 | 2635 | 	 * because it's fresher in cache. | 
 | 2636 | 	 */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2637 | 	i = Py_Size(ret) - shift;  /* # digits after shift */ | 
 | 2638 | 	(void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_Size(t2)); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2639 | 	Py_DECREF(t2); | 
 | 2640 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2641 | 	(void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_Size(t1)); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2642 | 	Py_DECREF(t1); | 
 | 2643 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2644 | 	/* 6. t3 <- (ah+al)(bh+bl), and add into result. */ | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2645 | 	if ((t1 = x_add(ah, al)) == NULL) goto fail; | 
 | 2646 | 	Py_DECREF(ah); | 
 | 2647 | 	Py_DECREF(al); | 
 | 2648 | 	ah = al = NULL; | 
 | 2649 |  | 
| Tim Peters | 0973b99 | 2004-08-29 22:16:50 +0000 | [diff] [blame] | 2650 | 	if (a == b) { | 
 | 2651 | 		t2 = t1; | 
 | 2652 | 		Py_INCREF(t2); | 
 | 2653 | 	} | 
 | 2654 | 	else if ((t2 = x_add(bh, bl)) == NULL) { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2655 | 		Py_DECREF(t1); | 
 | 2656 | 		goto fail; | 
 | 2657 | 	} | 
 | 2658 | 	Py_DECREF(bh); | 
 | 2659 | 	Py_DECREF(bl); | 
 | 2660 | 	bh = bl = NULL; | 
 | 2661 |  | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2662 | 	t3 = k_mul(t1, t2); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2663 | 	Py_DECREF(t1); | 
 | 2664 | 	Py_DECREF(t2); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2665 | 	if (t3 == NULL) goto fail; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2666 | 	assert(Py_Size(t3) >= 0); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2667 |  | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2668 | 	/* Add t3.  It's not obvious why we can't run out of room here. | 
 | 2669 | 	 * See the (*) comment after this function. | 
| Tim Peters | d8b2173 | 2002-08-12 19:30:26 +0000 | [diff] [blame] | 2670 | 	 */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2671 | 	(void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_Size(t3)); | 
| Tim Peters | 738eda7 | 2002-08-12 15:08:20 +0000 | [diff] [blame] | 2672 | 	Py_DECREF(t3); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2673 |  | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2674 | 	return long_normalize(ret); | 
 | 2675 |  | 
 | 2676 |  fail: | 
 | 2677 |  	Py_XDECREF(ret); | 
 | 2678 | 	Py_XDECREF(ah); | 
 | 2679 | 	Py_XDECREF(al); | 
 | 2680 | 	Py_XDECREF(bh); | 
 | 2681 | 	Py_XDECREF(bl); | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2682 | 	return NULL; | 
 | 2683 | } | 
 | 2684 |  | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2685 | /* (*) Why adding t3 can't "run out of room" above. | 
 | 2686 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2687 | Let f(x) mean the floor of x and c(x) mean the ceiling of x.  Some facts | 
 | 2688 | to start with: | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2689 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2690 | 1. For any integer i, i = c(i/2) + f(i/2).  In particular, | 
 | 2691 |    bsize = c(bsize/2) + f(bsize/2). | 
 | 2692 | 2. shift = f(bsize/2) | 
 | 2693 | 3. asize <= bsize | 
 | 2694 | 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this | 
 | 2695 |    routine, so asize > bsize/2 >= f(bsize/2) in this routine. | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2696 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2697 | We allocated asize + bsize result digits, and add t3 into them at an offset | 
 | 2698 | of shift.  This leaves asize+bsize-shift allocated digit positions for t3 | 
 | 2699 | to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) = | 
 | 2700 | asize + c(bsize/2) available digit positions. | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2701 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2702 | bh has c(bsize/2) digits, and bl at most f(size/2) digits.  So bh+hl has | 
 | 2703 | at most c(bsize/2) digits + 1 bit. | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2704 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2705 | If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2) | 
 | 2706 | digits, and al has at most f(bsize/2) digits in any case.  So ah+al has at | 
 | 2707 | most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit. | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2708 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2709 | The product (ah+al)*(bh+bl) therefore has at most | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2710 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2711 |     c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2712 |  | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2713 | and we have asize + c(bsize/2) available digit positions.  We need to show | 
 | 2714 | this is always enough.  An instance of c(bsize/2) cancels out in both, so | 
 | 2715 | the question reduces to whether asize digits is enough to hold | 
 | 2716 | (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits.  If asize < bsize, | 
 | 2717 | then we're asking whether asize digits >= f(bsize/2) digits + 2 bits.  By #4, | 
 | 2718 | asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1 | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2719 | digit is enough to hold 2 bits.  This is so since PyLong_SHIFT=15 >= 2.  If | 
| Tim Peters | ab86c2b | 2002-08-15 20:06:00 +0000 | [diff] [blame] | 2720 | asize == bsize, then we're asking whether bsize digits is enough to hold | 
| Tim Peters | e417de0 | 2002-08-15 20:10:45 +0000 | [diff] [blame] | 2721 | c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits | 
 | 2722 | is enough to hold 2 bits.  This is so if bsize >= 2, which holds because | 
 | 2723 | bsize >= KARATSUBA_CUTOFF >= 2. | 
| Tim Peters | 8e966ee | 2002-08-14 16:36:23 +0000 | [diff] [blame] | 2724 |  | 
| Tim Peters | 48d52c0 | 2002-08-14 17:07:32 +0000 | [diff] [blame] | 2725 | Note that since there's always enough room for (ah+al)*(bh+bl), and that's | 
 | 2726 | clearly >= each of ah*bh and al*bl, there's always enough room to subtract | 
 | 2727 | ah*bh and al*bl too. | 
| Tim Peters | d6974a5 | 2002-08-13 20:37:51 +0000 | [diff] [blame] | 2728 | */ | 
 | 2729 |  | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2730 | /* b has at least twice the digits of a, and a is big enough that Karatsuba | 
 | 2731 |  * would pay off *if* the inputs had balanced sizes.  View b as a sequence | 
 | 2732 |  * of slices, each with a->ob_size digits, and multiply the slices by a, | 
 | 2733 |  * one at a time.  This gives k_mul balanced inputs to work with, and is | 
 | 2734 |  * also cache-friendly (we compute one double-width slice of the result | 
 | 2735 |  * at a time, then move on, never bactracking except for the helpful | 
 | 2736 |  * single-width slice overlap between successive partial sums). | 
 | 2737 |  */ | 
 | 2738 | static PyLongObject * | 
 | 2739 | k_lopsided_mul(PyLongObject *a, PyLongObject *b) | 
 | 2740 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2741 | 	const Py_ssize_t asize = ABS(Py_Size(a)); | 
 | 2742 | 	Py_ssize_t bsize = ABS(Py_Size(b)); | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2743 | 	Py_ssize_t nbdone;	/* # of b digits already multiplied */ | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2744 | 	PyLongObject *ret; | 
 | 2745 | 	PyLongObject *bslice = NULL; | 
 | 2746 |  | 
 | 2747 | 	assert(asize > KARATSUBA_CUTOFF); | 
 | 2748 | 	assert(2 * asize <= bsize); | 
 | 2749 |  | 
 | 2750 | 	/* Allocate result space, and zero it out. */ | 
 | 2751 | 	ret = _PyLong_New(asize + bsize); | 
 | 2752 | 	if (ret == NULL) | 
 | 2753 | 		return NULL; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2754 | 	memset(ret->ob_digit, 0, Py_Size(ret) * sizeof(digit)); | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2755 |  | 
 | 2756 | 	/* Successive slices of b are copied into bslice. */ | 
| Tim Peters | 1203403 | 2002-08-12 22:10:00 +0000 | [diff] [blame] | 2757 | 	bslice = _PyLong_New(asize); | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2758 | 	if (bslice == NULL) | 
 | 2759 | 		goto fail; | 
 | 2760 |  | 
 | 2761 | 	nbdone = 0; | 
 | 2762 | 	while (bsize > 0) { | 
 | 2763 | 		PyLongObject *product; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2764 | 		const Py_ssize_t nbtouse = MIN(bsize, asize); | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2765 |  | 
 | 2766 | 		/* Multiply the next slice of b by a. */ | 
 | 2767 | 		memcpy(bslice->ob_digit, b->ob_digit + nbdone, | 
 | 2768 | 		       nbtouse * sizeof(digit)); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2769 | 		Py_Size(bslice) = nbtouse; | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2770 | 		product = k_mul(a, bslice); | 
 | 2771 | 		if (product == NULL) | 
 | 2772 | 			goto fail; | 
 | 2773 |  | 
 | 2774 | 		/* Add into result. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2775 | 		(void)v_iadd(ret->ob_digit + nbdone, Py_Size(ret) - nbdone, | 
 | 2776 | 			     product->ob_digit, Py_Size(product)); | 
| Tim Peters | 6000464 | 2002-08-12 22:01:34 +0000 | [diff] [blame] | 2777 | 		Py_DECREF(product); | 
 | 2778 |  | 
 | 2779 | 		bsize -= nbtouse; | 
 | 2780 | 		nbdone += nbtouse; | 
 | 2781 | 	} | 
 | 2782 |  | 
 | 2783 | 	Py_DECREF(bslice); | 
 | 2784 | 	return long_normalize(ret); | 
 | 2785 |  | 
 | 2786 |  fail: | 
 | 2787 | 	Py_DECREF(ret); | 
 | 2788 | 	Py_XDECREF(bslice); | 
 | 2789 | 	return NULL; | 
 | 2790 | } | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2791 |  | 
 | 2792 | static PyObject * | 
 | 2793 | long_mul(PyLongObject *v, PyLongObject *w) | 
 | 2794 | { | 
 | 2795 | 	PyLongObject *a, *b, *z; | 
 | 2796 |  | 
 | 2797 | 	if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2798 | 		Py_INCREF(Py_NotImplemented); | 
 | 2799 | 		return Py_NotImplemented; | 
 | 2800 | 	} | 
 | 2801 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2802 | 	if (ABS(Py_Size(v)) <= 1 && ABS(Py_Size(w)) <= 1) { | 
| Martin v. Löwis | 14b6d92 | 2007-02-06 21:05:30 +0000 | [diff] [blame] | 2803 | 		PyObject *r; | 
 | 2804 | 		r = PyLong_FromLong(MEDIUM_VALUE(v)*MEDIUM_VALUE(w)); | 
 | 2805 | 		Py_DECREF(a); | 
 | 2806 | 		Py_DECREF(b); | 
 | 2807 | 		return r; | 
 | 2808 | 	} | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2809 |  | 
| Tim Peters | d64c1de | 2002-08-12 17:36:03 +0000 | [diff] [blame] | 2810 | 	z = k_mul(a, b); | 
| Tim Peters | 9973d74 | 2002-08-15 19:41:06 +0000 | [diff] [blame] | 2811 | 	/* Negate if exactly one of the inputs is negative. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2812 | 	if (((Py_Size(a) ^ Py_Size(b)) < 0) && z) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2813 | 		NEGATE(z); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2814 | 	Py_DECREF(a); | 
 | 2815 | 	Py_DECREF(b); | 
| Tim Peters | 9973d74 | 2002-08-15 19:41:06 +0000 | [diff] [blame] | 2816 | 	return (PyObject *)z; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2817 | } | 
 | 2818 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2819 | /* The / and % operators are now defined in terms of divmod(). | 
 | 2820 |    The expression a mod b has the value a - b*floor(a/b). | 
 | 2821 |    The long_divrem function gives the remainder after division of | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2822 |    |a| by |b|, with the sign of a.  This is also expressed | 
 | 2823 |    as a - b*trunc(a/b), if trunc truncates towards zero. | 
 | 2824 |    Some examples: | 
 | 2825 |    	 a	 b	a rem b		a mod b | 
 | 2826 |    	 13	 10	 3		 3 | 
 | 2827 |    	-13	 10	-3		 7 | 
 | 2828 |    	 13	-10	 3		-7 | 
 | 2829 |    	-13	-10	-3		-3 | 
 | 2830 |    So, to get from rem to mod, we have to add b if a and b | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2831 |    have different signs.  We then subtract one from the 'div' | 
 | 2832 |    part of the outcome to keep the invariant intact. */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2833 |  | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2834 | /* Compute | 
 | 2835 |  *     *pdiv, *pmod = divmod(v, w) | 
 | 2836 |  * NULL can be passed for pdiv or pmod, in which case that part of | 
 | 2837 |  * the result is simply thrown away.  The caller owns a reference to | 
 | 2838 |  * each of these it requests (does not pass NULL for). | 
 | 2839 |  */ | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2840 | static int | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2841 | l_divmod(PyLongObject *v, PyLongObject *w, | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2842 | 	 PyLongObject **pdiv, PyLongObject **pmod) | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2843 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2844 | 	PyLongObject *div, *mod; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2845 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2846 | 	if (long_divrem(v, w, &div, &mod) < 0) | 
 | 2847 | 		return -1; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2848 | 	if ((Py_Size(mod) < 0 && Py_Size(w) > 0) || | 
 | 2849 | 	    (Py_Size(mod) > 0 && Py_Size(w) < 0)) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2850 | 		PyLongObject *temp; | 
 | 2851 | 		PyLongObject *one; | 
 | 2852 | 		temp = (PyLongObject *) long_add(mod, w); | 
 | 2853 | 		Py_DECREF(mod); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2854 | 		mod = temp; | 
 | 2855 | 		if (mod == NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2856 | 			Py_DECREF(div); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2857 | 			return -1; | 
 | 2858 | 		} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2859 | 		one = (PyLongObject *) PyLong_FromLong(1L); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2860 | 		if (one == NULL || | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2861 | 		    (temp = (PyLongObject *) long_sub(div, one)) == NULL) { | 
 | 2862 | 			Py_DECREF(mod); | 
 | 2863 | 			Py_DECREF(div); | 
 | 2864 | 			Py_XDECREF(one); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2865 | 			return -1; | 
 | 2866 | 		} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2867 | 		Py_DECREF(one); | 
 | 2868 | 		Py_DECREF(div); | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2869 | 		div = temp; | 
 | 2870 | 	} | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2871 | 	if (pdiv != NULL) | 
 | 2872 | 		*pdiv = div; | 
 | 2873 | 	else | 
 | 2874 | 		Py_DECREF(div); | 
 | 2875 |  | 
 | 2876 | 	if (pmod != NULL) | 
 | 2877 | 		*pmod = mod; | 
 | 2878 | 	else | 
 | 2879 | 		Py_DECREF(mod); | 
 | 2880 |  | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2881 | 	return 0; | 
 | 2882 | } | 
 | 2883 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2884 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2885 | long_div(PyObject *v, PyObject *w) | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2886 | { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2887 | 	PyLongObject *a, *b, *div; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2888 |  | 
 | 2889 | 	CONVERT_BINOP(v, w, &a, &b); | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2890 | 	if (l_divmod(a, b, &div, NULL) < 0) | 
 | 2891 | 		div = NULL; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2892 | 	Py_DECREF(a); | 
 | 2893 | 	Py_DECREF(b); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2894 | 	return (PyObject *)div; | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2895 | } | 
 | 2896 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2897 | static PyObject * | 
| Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 2898 | long_true_divide(PyObject *v, PyObject *w) | 
 | 2899 | { | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2900 | 	PyLongObject *a, *b; | 
 | 2901 | 	double ad, bd; | 
| Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 2902 | 	int failed, aexp = -1, bexp = -1; | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2903 |  | 
 | 2904 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 2905 | 	ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp); | 
 | 2906 | 	bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp); | 
| Tim Peters | 6f97e49 | 2001-11-04 23:09:40 +0000 | [diff] [blame] | 2907 | 	failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred(); | 
 | 2908 | 	Py_DECREF(a); | 
 | 2909 | 	Py_DECREF(b); | 
 | 2910 | 	if (failed) | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2911 | 		return NULL; | 
| Thomas Wouters | 553489a | 2006-02-01 21:32:04 +0000 | [diff] [blame] | 2912 | 	/* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x, | 
 | 2913 | 	   but should really be set correctly after sucessful calls to | 
 | 2914 | 	   _PyLong_AsScaledDouble() */ | 
 | 2915 | 	assert(aexp >= 0 && bexp >= 0); | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2916 |  | 
 | 2917 | 	if (bd == 0.0) { | 
 | 2918 | 		PyErr_SetString(PyExc_ZeroDivisionError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2919 | 			"int division or modulo by zero"); | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2920 | 		return NULL; | 
 | 2921 | 	} | 
 | 2922 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2923 | 	/* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */ | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2924 | 	ad /= bd;	/* overflow/underflow impossible here */ | 
 | 2925 | 	aexp -= bexp; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2926 | 	if (aexp > INT_MAX / PyLong_SHIFT) | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2927 | 		goto overflow; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2928 | 	else if (aexp < -(INT_MAX / PyLong_SHIFT)) | 
| Tim Peters | e56ed9b | 2001-09-06 21:59:14 +0000 | [diff] [blame] | 2929 | 		return PyFloat_FromDouble(0.0);	/* underflow to 0 */ | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2930 | 	errno = 0; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2931 | 	ad = ldexp(ad, aexp * PyLong_SHIFT); | 
| Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 2932 | 	if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */ | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2933 | 		goto overflow; | 
 | 2934 | 	return PyFloat_FromDouble(ad); | 
 | 2935 |  | 
 | 2936 | overflow: | 
 | 2937 | 	PyErr_SetString(PyExc_OverflowError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2938 | 		"int/int too large for a float"); | 
| Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 2939 | 	return NULL; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 2940 |  | 
| Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 2941 | } | 
 | 2942 |  | 
 | 2943 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2944 | long_mod(PyObject *v, PyObject *w) | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2945 | { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2946 | 	PyLongObject *a, *b, *mod; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2947 |  | 
 | 2948 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 2949 |  | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2950 | 	if (l_divmod(a, b, NULL, &mod) < 0) | 
 | 2951 | 		mod = NULL; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2952 | 	Py_DECREF(a); | 
 | 2953 | 	Py_DECREF(b); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2954 | 	return (PyObject *)mod; | 
| Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 2955 | } | 
 | 2956 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2957 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2958 | long_divmod(PyObject *v, PyObject *w) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2959 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2960 | 	PyLongObject *a, *b, *div, *mod; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2961 | 	PyObject *z; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2962 |  | 
 | 2963 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 2964 |  | 
 | 2965 | 	if (l_divmod(a, b, &div, &mod) < 0) { | 
 | 2966 | 		Py_DECREF(a); | 
 | 2967 | 		Py_DECREF(b); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2968 | 		return NULL; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2969 | 	} | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2970 | 	z = PyTuple_New(2); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2971 | 	if (z != NULL) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2972 | 		PyTuple_SetItem(z, 0, (PyObject *) div); | 
 | 2973 | 		PyTuple_SetItem(z, 1, (PyObject *) mod); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2974 | 	} | 
 | 2975 | 	else { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2976 | 		Py_DECREF(div); | 
 | 2977 | 		Py_DECREF(mod); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2978 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2979 | 	Py_DECREF(a); | 
 | 2980 | 	Py_DECREF(b); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2981 | 	return z; | 
 | 2982 | } | 
 | 2983 |  | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2984 | /* pow(v, w, x) */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2985 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2986 | long_pow(PyObject *v, PyObject *w, PyObject *x) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2987 | { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2988 | 	PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ | 
 | 2989 | 	int negativeOutput = 0;  /* if x<0 return negative output */ | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2990 |  | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2991 | 	PyLongObject *z = NULL;  /* accumulated result */ | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2992 | 	Py_ssize_t i, j, k;             /* counters */ | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 2993 | 	PyLongObject *temp = NULL; | 
 | 2994 |  | 
 | 2995 | 	/* 5-ary values.  If the exponent is large enough, table is | 
 | 2996 | 	 * precomputed so that table[i] == a**i % c for i in range(32). | 
 | 2997 | 	 */ | 
 | 2998 | 	PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | 
 | 2999 | 				   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | 
 | 3000 |  | 
 | 3001 | 	/* a, b, c = v, w, x */ | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3002 | 	CONVERT_BINOP(v, w, &a, &b); | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3003 | 	if (PyLong_Check(x)) { | 
 | 3004 | 		c = (PyLongObject *)x; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3005 | 		Py_INCREF(x); | 
 | 3006 | 	} | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3007 | 	else if (x == Py_None) | 
 | 3008 | 		c = NULL; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3009 | 	else { | 
 | 3010 | 		Py_DECREF(a); | 
 | 3011 | 		Py_DECREF(b); | 
 | 3012 | 		Py_INCREF(Py_NotImplemented); | 
 | 3013 | 		return Py_NotImplemented; | 
 | 3014 | 	} | 
| Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3015 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3016 | 	if (Py_Size(b) < 0) {  /* if exponent is negative */ | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3017 | 		if (c) { | 
| Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 3018 | 			PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3019 | 			    "cannot be negative when 3rd argument specified"); | 
| Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3020 | 			goto Error; | 
| Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 3021 | 		} | 
| Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 3022 | 		else { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3023 | 			/* else return a float.  This works because we know | 
 | 3024 | 			   that this calls float_pow() which converts its | 
 | 3025 | 			   arguments to double. */ | 
 | 3026 | 			Py_DECREF(a); | 
 | 3027 | 			Py_DECREF(b); | 
 | 3028 | 			return PyFloat_Type.tp_as_number->nb_power(v, w, x); | 
| Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 3029 | 		} | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 3030 | 	} | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3031 |  | 
 | 3032 | 	if (c) { | 
 | 3033 | 		/* if modulus == 0: | 
 | 3034 | 		       raise ValueError() */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3035 | 		if (Py_Size(c) == 0) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3036 | 			PyErr_SetString(PyExc_ValueError, | 
 | 3037 | 					"pow() 3rd argument cannot be 0"); | 
| Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3038 | 			goto Error; | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3039 | 		} | 
 | 3040 |  | 
 | 3041 | 		/* if modulus < 0: | 
 | 3042 | 		       negativeOutput = True | 
 | 3043 | 		       modulus = -modulus */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3044 | 		if (Py_Size(c) < 0) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3045 | 			negativeOutput = 1; | 
 | 3046 | 			temp = (PyLongObject *)_PyLong_Copy(c); | 
 | 3047 | 			if (temp == NULL) | 
 | 3048 | 				goto Error; | 
 | 3049 | 			Py_DECREF(c); | 
 | 3050 | 			c = temp; | 
 | 3051 | 			temp = NULL; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3052 | 			NEGATE(c); | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3053 | 		} | 
 | 3054 |  | 
 | 3055 | 		/* if modulus == 1: | 
 | 3056 | 		       return 0 */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3057 | 		if ((Py_Size(c) == 1) && (c->ob_digit[0] == 1)) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3058 | 			z = (PyLongObject *)PyLong_FromLong(0L); | 
 | 3059 | 			goto Done; | 
 | 3060 | 		} | 
 | 3061 |  | 
 | 3062 | 		/* if base < 0: | 
 | 3063 | 		       base = base % modulus | 
 | 3064 | 		   Having the base positive just makes things easier. */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3065 | 		if (Py_Size(a) < 0) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3066 | 			if (l_divmod(a, c, NULL, &temp) < 0) | 
| Tim Peters | cd97da3 | 2004-08-30 02:58:59 +0000 | [diff] [blame] | 3067 | 				goto Error; | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3068 | 			Py_DECREF(a); | 
 | 3069 | 			a = temp; | 
 | 3070 | 			temp = NULL; | 
 | 3071 | 		} | 
 | 3072 | 	} | 
 | 3073 |  | 
 | 3074 | 	/* At this point a, b, and c are guaranteed non-negative UNLESS | 
 | 3075 | 	   c is NULL, in which case a may be negative. */ | 
 | 3076 |  | 
 | 3077 | 	z = (PyLongObject *)PyLong_FromLong(1L); | 
 | 3078 | 	if (z == NULL) | 
 | 3079 | 		goto Error; | 
 | 3080 |  | 
 | 3081 | 	/* Perform a modular reduction, X = X % c, but leave X alone if c | 
 | 3082 | 	 * is NULL. | 
 | 3083 | 	 */ | 
 | 3084 | #define REDUCE(X)					\ | 
 | 3085 | 	if (c != NULL) {				\ | 
 | 3086 | 		if (l_divmod(X, c, NULL, &temp) < 0)	\ | 
 | 3087 | 			goto Error;			\ | 
 | 3088 | 		Py_XDECREF(X);				\ | 
 | 3089 | 		X = temp;				\ | 
 | 3090 | 		temp = NULL;				\ | 
 | 3091 | 	} | 
 | 3092 |  | 
 | 3093 | 	/* Multiply two values, then reduce the result: | 
 | 3094 | 	   result = X*Y % c.  If c is NULL, skip the mod. */ | 
 | 3095 | #define MULT(X, Y, result)				\ | 
 | 3096 | {							\ | 
 | 3097 | 	temp = (PyLongObject *)long_mul(X, Y);		\ | 
 | 3098 | 	if (temp == NULL)				\ | 
 | 3099 | 		goto Error;				\ | 
 | 3100 | 	Py_XDECREF(result);				\ | 
 | 3101 | 	result = temp;					\ | 
 | 3102 | 	temp = NULL;					\ | 
 | 3103 | 	REDUCE(result)					\ | 
 | 3104 | } | 
 | 3105 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3106 | 	if (Py_Size(b) <= FIVEARY_CUTOFF) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3107 | 		/* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ | 
 | 3108 | 		/* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf    */ | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3109 | 		for (i = Py_Size(b) - 1; i >= 0; --i) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3110 | 			digit bi = b->ob_digit[i]; | 
 | 3111 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3112 | 			for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3113 | 				MULT(z, z, z) | 
 | 3114 | 				if (bi & j) | 
 | 3115 | 					MULT(z, a, z) | 
 | 3116 | 			} | 
 | 3117 | 		} | 
 | 3118 | 	} | 
 | 3119 | 	else { | 
 | 3120 | 		/* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ | 
 | 3121 | 		Py_INCREF(z);	/* still holds 1L */ | 
 | 3122 | 		table[0] = z; | 
 | 3123 | 		for (i = 1; i < 32; ++i) | 
 | 3124 | 			MULT(table[i-1], a, table[i]) | 
 | 3125 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3126 | 		for (i = Py_Size(b) - 1; i >= 0; --i) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3127 | 			const digit bi = b->ob_digit[i]; | 
 | 3128 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3129 | 			for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3130 | 				const int index = (bi >> j) & 0x1f; | 
 | 3131 | 				for (k = 0; k < 5; ++k) | 
 | 3132 | 					MULT(z, z, z) | 
 | 3133 | 				if (index) | 
 | 3134 | 					MULT(z, table[index], z) | 
 | 3135 | 			} | 
 | 3136 | 		} | 
 | 3137 | 	} | 
 | 3138 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3139 | 	if (negativeOutput && (Py_Size(z) != 0)) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3140 | 		temp = (PyLongObject *)long_sub(z, c); | 
 | 3141 | 		if (temp == NULL) | 
 | 3142 | 			goto Error; | 
 | 3143 | 		Py_DECREF(z); | 
 | 3144 | 		z = temp; | 
 | 3145 | 		temp = NULL; | 
 | 3146 | 	} | 
 | 3147 | 	goto Done; | 
 | 3148 |  | 
 | 3149 |  Error: | 
 | 3150 |  	if (z != NULL) { | 
 | 3151 |  		Py_DECREF(z); | 
 | 3152 |  		z = NULL; | 
 | 3153 |  	} | 
 | 3154 | 	/* fall through */ | 
 | 3155 |  Done: | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3156 | 	if (Py_Size(b) > FIVEARY_CUTOFF) { | 
| Tim Peters | 47e52ee | 2004-08-30 02:44:38 +0000 | [diff] [blame] | 3157 | 		for (i = 0; i < 32; ++i) | 
 | 3158 | 			Py_XDECREF(table[i]); | 
 | 3159 | 	} | 
| Tim Peters | de7990b | 2005-07-17 23:45:23 +0000 | [diff] [blame] | 3160 | 	Py_DECREF(a); | 
 | 3161 | 	Py_DECREF(b); | 
 | 3162 | 	Py_XDECREF(c); | 
 | 3163 | 	Py_XDECREF(temp); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3164 | 	return (PyObject *)z; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3165 | } | 
 | 3166 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3167 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3168 | long_invert(PyLongObject *v) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3169 | { | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3170 | 	/* Implement ~x as -(x+1) */ | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3171 | 	PyLongObject *x; | 
 | 3172 | 	PyLongObject *w; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3173 | 	if (ABS(Py_Size(v)) <=1) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3174 | 		return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3175 | 	w = (PyLongObject *)PyLong_FromLong(1L); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3176 | 	if (w == NULL) | 
 | 3177 | 		return NULL; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3178 | 	x = (PyLongObject *) long_add(v, w); | 
 | 3179 | 	Py_DECREF(w); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3180 | 	if (x == NULL) | 
 | 3181 | 		return NULL; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3182 | 	Py_Size(x) = -(Py_Size(x)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3183 | 	return (PyObject *)x; | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3184 | } | 
 | 3185 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3186 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3187 | long_neg(PyLongObject *v) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3188 | { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3189 | 	PyLongObject *z; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3190 | 	if (ABS(Py_Size(v)) <= 1) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3191 | 		return PyLong_FromLong(-MEDIUM_VALUE(v)); | 
| Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3192 | 	z = (PyLongObject *)_PyLong_Copy(v); | 
 | 3193 | 	if (z != NULL) | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3194 | 		Py_Size(z) = -(Py_Size(v)); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3195 | 	return (PyObject *)z; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3196 | } | 
 | 3197 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3198 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3199 | long_abs(PyLongObject *v) | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3200 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3201 | 	if (Py_Size(v) < 0) | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3202 | 		return long_neg(v); | 
| Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 3203 | 	else | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3204 | 		return long_long((PyObject *)v); | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3205 | } | 
 | 3206 |  | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3207 | static int | 
| Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3208 | long_bool(PyLongObject *v) | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3209 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3210 | 	return ABS(Py_Size(v)) != 0; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3211 | } | 
 | 3212 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3213 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3214 | long_rshift(PyLongObject *v, PyLongObject *w) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3215 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3216 | 	PyLongObject *a, *b; | 
 | 3217 | 	PyLongObject *z = NULL; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3218 | 	long shiftby; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3219 | 	Py_ssize_t newsize, wordshift, loshift, hishift, i, j; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3220 | 	digit lomask, himask; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3221 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3222 | 	CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); | 
 | 3223 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3224 | 	if (Py_Size(a) < 0) { | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3225 | 		/* Right shifting negative numbers is harder */ | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3226 | 		PyLongObject *a1, *a2; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3227 | 		a1 = (PyLongObject *) long_invert(a); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3228 | 		if (a1 == NULL) | 
 | 3229 | 			goto rshift_error; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3230 | 		a2 = (PyLongObject *) long_rshift(a1, b); | 
 | 3231 | 		Py_DECREF(a1); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3232 | 		if (a2 == NULL) | 
 | 3233 | 			goto rshift_error; | 
 | 3234 | 		z = (PyLongObject *) long_invert(a2); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3235 | 		Py_DECREF(a2); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3236 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3237 | 	else { | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3238 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3239 | 		shiftby = PyLong_AsLong((PyObject *)b); | 
 | 3240 | 		if (shiftby == -1L && PyErr_Occurred()) | 
 | 3241 | 			goto rshift_error; | 
 | 3242 | 		if (shiftby < 0) { | 
 | 3243 | 			PyErr_SetString(PyExc_ValueError, | 
 | 3244 | 					"negative shift count"); | 
 | 3245 | 			goto rshift_error; | 
 | 3246 | 		} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3247 | 		wordshift = shiftby / PyLong_SHIFT; | 
 | 3248 | 		newsize = ABS(Py_Size(a)) - wordshift; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3249 | 		if (newsize <= 0) { | 
 | 3250 | 			z = _PyLong_New(0); | 
 | 3251 | 			Py_DECREF(a); | 
 | 3252 | 			Py_DECREF(b); | 
 | 3253 | 			return (PyObject *)z; | 
 | 3254 | 		} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3255 | 		loshift = shiftby % PyLong_SHIFT; | 
 | 3256 | 		hishift = PyLong_SHIFT - loshift; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3257 | 		lomask = ((digit)1 << hishift) - 1; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3258 | 		himask = PyLong_MASK ^ lomask; | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3259 | 		z = _PyLong_New(newsize); | 
 | 3260 | 		if (z == NULL) | 
 | 3261 | 			goto rshift_error; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3262 | 		if (Py_Size(a) < 0) | 
 | 3263 | 			Py_Size(z) = -(Py_Size(z)); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3264 | 		for (i = 0, j = wordshift; i < newsize; i++, j++) { | 
 | 3265 | 			z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; | 
 | 3266 | 			if (i+1 < newsize) | 
 | 3267 | 				z->ob_digit[i] |= | 
 | 3268 | 				  (a->ob_digit[j+1] << hishift) & himask; | 
 | 3269 | 		} | 
 | 3270 | 		z = long_normalize(z); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3271 | 	} | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3272 | rshift_error: | 
 | 3273 | 	Py_DECREF(a); | 
 | 3274 | 	Py_DECREF(b); | 
 | 3275 | 	return (PyObject *) z; | 
 | 3276 |  | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3277 | } | 
 | 3278 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3279 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3280 | long_lshift(PyObject *v, PyObject *w) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3281 | { | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3282 | 	/* This version due to Tim Peters */ | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3283 | 	PyLongObject *a, *b; | 
 | 3284 | 	PyLongObject *z = NULL; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3285 | 	long shiftby; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3286 | 	Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3287 | 	twodigits accum; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3288 |  | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3289 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 3290 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3291 | 	shiftby = PyLong_AsLong((PyObject *)b); | 
 | 3292 | 	if (shiftby == -1L && PyErr_Occurred()) | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3293 | 		goto lshift_error; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3294 | 	if (shiftby < 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3295 | 		PyErr_SetString(PyExc_ValueError, "negative shift count"); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3296 | 		goto lshift_error; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3297 | 	} | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3298 | 	if ((long)(int)shiftby != shiftby) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3299 | 		PyErr_SetString(PyExc_ValueError, | 
 | 3300 | 				"outrageous left shift count"); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3301 | 		goto lshift_error; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3302 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3303 | 	/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ | 
 | 3304 | 	wordshift = (int)shiftby / PyLong_SHIFT; | 
 | 3305 | 	remshift  = (int)shiftby - wordshift * PyLong_SHIFT; | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3306 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3307 | 	oldsize = ABS(Py_Size(a)); | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3308 | 	newsize = oldsize + wordshift; | 
 | 3309 | 	if (remshift) | 
 | 3310 | 		++newsize; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3311 | 	z = _PyLong_New(newsize); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3312 | 	if (z == NULL) | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3313 | 		goto lshift_error; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3314 | 	if (Py_Size(a) < 0) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3315 | 		NEGATE(z); | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3316 | 	for (i = 0; i < wordshift; i++) | 
 | 3317 | 		z->ob_digit[i] = 0; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3318 | 	accum = 0; | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3319 | 	for (i = wordshift, j = 0; j < oldsize; i++, j++) { | 
| Tim Peters | 0d2d87d | 2002-08-20 19:00:22 +0000 | [diff] [blame] | 3320 | 		accum |= (twodigits)a->ob_digit[j] << remshift; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3321 | 		z->ob_digit[i] = (digit)(accum & PyLong_MASK); | 
 | 3322 | 		accum >>= PyLong_SHIFT; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3323 | 	} | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3324 | 	if (remshift) | 
 | 3325 | 		z->ob_digit[newsize-1] = (digit)accum; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3326 | 	else | 
| Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 3327 | 		assert(!accum); | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3328 | 	z = long_normalize(z); | 
 | 3329 | lshift_error: | 
 | 3330 | 	Py_DECREF(a); | 
 | 3331 | 	Py_DECREF(b); | 
 | 3332 | 	return (PyObject *) z; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3333 | } | 
 | 3334 |  | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3335 |  | 
 | 3336 | /* Bitwise and/xor/or operations */ | 
 | 3337 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3338 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3339 | long_bitwise(PyLongObject *a, | 
 | 3340 | 	     int op,  /* '&', '|', '^' */ | 
 | 3341 | 	     PyLongObject *b) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3342 | { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3343 | 	digit maska, maskb; /* 0 or PyLong_MASK */ | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3344 | 	int negz; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3345 | 	Py_ssize_t size_a, size_b, size_z; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3346 | 	PyLongObject *z; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3347 | 	int i; | 
| Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 3348 | 	digit diga, digb; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3349 | 	PyObject *v; | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3350 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3351 | 	if (Py_Size(a) < 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3352 | 		a = (PyLongObject *) long_invert(a); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3353 | 		if (a == NULL) | 
 | 3354 | 			return NULL; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3355 | 		maska = PyLong_MASK; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3356 | 	} | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3357 | 	else { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3358 | 		Py_INCREF(a); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3359 | 		maska = 0; | 
| Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 3360 | 	} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3361 | 	if (Py_Size(b) < 0) { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3362 | 		b = (PyLongObject *) long_invert(b); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3363 | 		if (b == NULL) { | 
 | 3364 | 			Py_DECREF(a); | 
 | 3365 | 			return NULL; | 
 | 3366 | 		} | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3367 | 		maskb = PyLong_MASK; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3368 | 	} | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3369 | 	else { | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3370 | 		Py_INCREF(b); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3371 | 		maskb = 0; | 
 | 3372 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3373 |  | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3374 | 	negz = 0; | 
 | 3375 | 	switch (op) { | 
 | 3376 | 	case '^': | 
 | 3377 | 		if (maska != maskb) { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3378 | 			maska ^= PyLong_MASK; | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3379 | 			negz = -1; | 
 | 3380 | 		} | 
 | 3381 | 		break; | 
| Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 3382 | 	case '&': | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3383 | 		if (maska && maskb) { | 
 | 3384 | 			op = '|'; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3385 | 			maska ^= PyLong_MASK; | 
 | 3386 | 			maskb ^= PyLong_MASK; | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3387 | 			negz = -1; | 
 | 3388 | 		} | 
 | 3389 | 		break; | 
 | 3390 | 	case '|': | 
 | 3391 | 		if (maska || maskb) { | 
 | 3392 | 			op = '&'; | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3393 | 			maska ^= PyLong_MASK; | 
 | 3394 | 			maskb ^= PyLong_MASK; | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3395 | 			negz = -1; | 
 | 3396 | 		} | 
 | 3397 | 		break; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3398 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3399 |  | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3400 | 	/* JRH: The original logic here was to allocate the result value (z) | 
 | 3401 | 	   as the longer of the two operands.  However, there are some cases | 
 | 3402 | 	   where the result is guaranteed to be shorter than that: AND of two | 
 | 3403 | 	   positives, OR of two negatives: use the shorter number.  AND with | 
 | 3404 | 	   mixed signs: use the positive number.  OR with mixed signs: use the | 
 | 3405 | 	   negative number.  After the transformations above, op will be '&' | 
 | 3406 | 	   iff one of these cases applies, and mask will be non-0 for operands | 
 | 3407 | 	   whose length should be ignored. | 
 | 3408 | 	*/ | 
 | 3409 |  | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3410 | 	size_a = Py_Size(a); | 
 | 3411 | 	size_b = Py_Size(b); | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3412 | 	size_z = op == '&' | 
 | 3413 | 		? (maska | 
 | 3414 | 		   ? size_b | 
 | 3415 | 		   : (maskb ? size_a : MIN(size_a, size_b))) | 
 | 3416 | 		: MAX(size_a, size_b); | 
 | 3417 | 	z = _PyLong_New(size_z); | 
| Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3418 | 	if (z == NULL) { | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3419 | 		Py_DECREF(a); | 
 | 3420 | 		Py_DECREF(b); | 
| Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 3421 | 		return NULL; | 
 | 3422 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3423 |  | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3424 | 	for (i = 0; i < size_z; ++i) { | 
 | 3425 | 		diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; | 
 | 3426 | 		digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; | 
 | 3427 | 		switch (op) { | 
 | 3428 | 		case '&': z->ob_digit[i] = diga & digb; break; | 
 | 3429 | 		case '|': z->ob_digit[i] = diga | digb; break; | 
 | 3430 | 		case '^': z->ob_digit[i] = diga ^ digb; break; | 
 | 3431 | 		} | 
| Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 3432 | 	} | 
| Tim Peters | 5af4e6c | 2002-08-12 02:31:19 +0000 | [diff] [blame] | 3433 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3434 | 	Py_DECREF(a); | 
 | 3435 | 	Py_DECREF(b); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3436 | 	z = long_normalize(z); | 
 | 3437 | 	if (negz == 0) | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3438 | 		return (PyObject *) z; | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3439 | 	v = long_invert(z); | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3440 | 	Py_DECREF(z); | 
| Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 3441 | 	return v; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3442 | } | 
 | 3443 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3444 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3445 | long_and(PyObject *v, PyObject *w) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3446 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3447 | 	PyLongObject *a, *b; | 
 | 3448 | 	PyObject *c; | 
 | 3449 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 3450 | 	c = long_bitwise(a, '&', b); | 
 | 3451 | 	Py_DECREF(a); | 
 | 3452 | 	Py_DECREF(b); | 
 | 3453 | 	return c; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3454 | } | 
 | 3455 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3456 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3457 | long_xor(PyObject *v, PyObject *w) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3458 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3459 | 	PyLongObject *a, *b; | 
 | 3460 | 	PyObject *c; | 
 | 3461 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 3462 | 	c = long_bitwise(a, '^', b); | 
 | 3463 | 	Py_DECREF(a); | 
 | 3464 | 	Py_DECREF(b); | 
 | 3465 | 	return c; | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3466 | } | 
 | 3467 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3468 | static PyObject * | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3469 | long_or(PyObject *v, PyObject *w) | 
| Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 3470 | { | 
| Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 3471 | 	PyLongObject *a, *b; | 
 | 3472 | 	PyObject *c; | 
 | 3473 | 	CONVERT_BINOP(v, w, &a, &b); | 
 | 3474 | 	c = long_bitwise(a, '|', b); | 
 | 3475 | 	Py_DECREF(a); | 
 | 3476 | 	Py_DECREF(b); | 
 | 3477 | 	return c; | 
| Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 3478 | } | 
 | 3479 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3480 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3481 | long_long(PyObject *v) | 
| Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3482 | { | 
| Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 3483 | 	if (PyLong_CheckExact(v)) | 
 | 3484 | 		Py_INCREF(v); | 
 | 3485 | 	else | 
 | 3486 | 		v = _PyLong_Copy((PyLongObject *)v); | 
| Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3487 | 	return v; | 
 | 3488 | } | 
 | 3489 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3490 | static PyObject * | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3491 | long_float(PyObject *v) | 
| Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3492 | { | 
| Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 3493 | 	double result; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3494 | 	result = PyLong_AsDouble(v); | 
| Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 3495 | 	if (result == -1.0 && PyErr_Occurred()) | 
 | 3496 | 		return NULL; | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3497 | 	return PyFloat_FromDouble(result); | 
| Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3498 | } | 
 | 3499 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3500 | static PyObject * | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3501 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 
| Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 3502 |  | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3503 | static PyObject * | 
 | 3504 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 3505 | { | 
 | 3506 | 	PyObject *x = NULL; | 
 | 3507 | 	int base = -909;		     /* unlikely! */ | 
| Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3508 | 	static char *kwlist[] = {"x", "base", 0}; | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3509 |  | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3510 | 	if (type != &PyLong_Type) | 
 | 3511 | 		return long_subtype_new(type, args, kwds); /* Wimp out */ | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3512 | 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3513 | 					 &x, &base)) | 
 | 3514 | 		return NULL; | 
 | 3515 | 	if (x == NULL) | 
 | 3516 | 		return PyLong_FromLong(0L); | 
 | 3517 | 	if (base == -909) | 
 | 3518 | 		return PyNumber_Long(x); | 
| Neal Norwitz | ed2b739 | 2007-08-26 04:51:10 +0000 | [diff] [blame] | 3519 | 	else if (PyBytes_Check(x)) { | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3520 | 		/* Since PyLong_FromString doesn't have a length parameter, | 
 | 3521 | 		 * check here for possible NULs in the string. */ | 
| Neal Norwitz | ed2b739 | 2007-08-26 04:51:10 +0000 | [diff] [blame] | 3522 | 		char *string = PyBytes_AS_STRING(x); | 
 | 3523 | 		int size = PyBytes_GET_SIZE(x); | 
| Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 3524 | 		if (strlen(string) != size) { | 
| Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 3525 | 			/* We only see this if there's a null byte in x, | 
 | 3526 | 			   x is a str8 or a bytes, *and* a base is given. */ | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3527 | 			PyErr_Format(PyExc_ValueError, | 
| Guido van Rossum | 2523621 | 2007-08-22 23:28:23 +0000 | [diff] [blame] | 3528 | 			    "invalid literal for int() with base %d: %R", | 
 | 3529 | 			    base, x); | 
| Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3530 | 			return NULL; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3531 | 		} | 
| Guido van Rossum | 4581ae5 | 2007-05-22 21:56:47 +0000 | [diff] [blame] | 3532 | 		return PyLong_FromString(string, NULL, base); | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3533 | 	} | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3534 | 	else if (PyUnicode_Check(x)) | 
 | 3535 | 		return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), | 
 | 3536 | 					  PyUnicode_GET_SIZE(x), | 
 | 3537 | 					  base); | 
 | 3538 | 	else { | 
 | 3539 | 		PyErr_SetString(PyExc_TypeError, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3540 | 			"int() can't convert non-string with explicit base"); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3541 | 		return NULL; | 
 | 3542 | 	} | 
 | 3543 | } | 
 | 3544 |  | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3545 | /* Wimpy, slow approach to tp_new calls for subtypes of long: | 
 | 3546 |    first create a regular long from whatever arguments we got, | 
 | 3547 |    then allocate a subtype instance and initialize it from | 
 | 3548 |    the regular long.  The regular long is then thrown away. | 
 | 3549 | */ | 
 | 3550 | static PyObject * | 
 | 3551 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
 | 3552 | { | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3553 | 	PyLongObject *tmp, *newobj; | 
| Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3554 | 	Py_ssize_t i, n; | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3555 |  | 
 | 3556 | 	assert(PyType_IsSubtype(type, &PyLong_Type)); | 
 | 3557 | 	tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); | 
 | 3558 | 	if (tmp == NULL) | 
 | 3559 | 		return NULL; | 
| Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 3560 | 	assert(PyLong_CheckExact(tmp)); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3561 | 	n = Py_Size(tmp); | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3562 | 	if (n < 0) | 
 | 3563 | 		n = -n; | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3564 | 	newobj = (PyLongObject *)type->tp_alloc(type, n); | 
 | 3565 | 	if (newobj == NULL) { | 
| Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 3566 | 		Py_DECREF(tmp); | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3567 | 		return NULL; | 
| Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 3568 | 	} | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3569 | 	assert(PyLong_Check(newobj)); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3570 | 	Py_Size(newobj) = Py_Size(tmp); | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3571 | 	for (i = 0; i < n; i++) | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3572 | 		newobj->ob_digit[i] = tmp->ob_digit[i]; | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3573 | 	Py_DECREF(tmp); | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3574 | 	return (PyObject *)newobj; | 
| Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 3575 | } | 
 | 3576 |  | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3577 | static PyObject * | 
 | 3578 | long_getnewargs(PyLongObject *v) | 
 | 3579 | { | 
 | 3580 | 	return Py_BuildValue("(N)", _PyLong_Copy(v)); | 
 | 3581 | } | 
 | 3582 |  | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3583 | static PyObject * | 
 | 3584 | long_getN(PyLongObject *v, void *context) { | 
 | 3585 | 	return PyLong_FromLong((intptr_t)context); | 
 | 3586 | } | 
 | 3587 |  | 
| Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3588 | static PyObject * | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3589 | long__format__(PyObject *self, PyObject *args) | 
 | 3590 | { | 
 | 3591 |     /* when back porting this to 2.6, check type of the format_spec | 
 | 3592 |        and call either unicode_long__format__ or | 
 | 3593 |        string_long__format__ */ | 
 | 3594 |     return unicode_long__format__(self, args); | 
 | 3595 | } | 
 | 3596 |  | 
 | 3597 |  | 
 | 3598 | static PyObject * | 
| Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3599 | long_round(PyObject *self, PyObject *args) | 
 | 3600 | { | 
 | 3601 | #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */ | 
 | 3602 | 	int ndigits = UNDEF_NDIGITS; | 
 | 3603 | 	double x; | 
 | 3604 | 	PyObject *res; | 
 | 3605 | 	 | 
 | 3606 | 	if (!PyArg_ParseTuple(args, "|i", &ndigits)) | 
 | 3607 | 		return NULL; | 
 | 3608 |  | 
 | 3609 | 	if (ndigits == UNDEF_NDIGITS) | 
 | 3610 | 		return long_long(self); | 
 | 3611 |  | 
 | 3612 | 	/* If called with two args, defer to float.__round__(). */ | 
 | 3613 | 	x = PyLong_AsDouble(self); | 
 | 3614 | 	if (x == -1.0 && PyErr_Occurred()) | 
 | 3615 | 		return NULL; | 
 | 3616 | 	self = PyFloat_FromDouble(x); | 
 | 3617 | 	if (self == NULL) | 
 | 3618 | 		return NULL; | 
 | 3619 | 	res = PyObject_CallMethod(self, "__round__", "i", ndigits); | 
 | 3620 | 	Py_DECREF(self); | 
 | 3621 | 	return res; | 
 | 3622 | #undef UNDEF_NDIGITS | 
 | 3623 | } | 
 | 3624 |  | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3625 | static PyMethodDef long_methods[] = { | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3626 | 	{"conjugate",	(PyCFunction)long_long,	METH_NOARGS, | 
 | 3627 | 	 "Returns self, the complex conjugate of any int."}, | 
| Guido van Rossum | 2fa33db | 2007-08-23 22:07:24 +0000 | [diff] [blame] | 3628 | 	{"__trunc__",	(PyCFunction)long_long,	METH_NOARGS, | 
 | 3629 |          "Truncating an Integral returns itself."}, | 
 | 3630 | 	{"__floor__",	(PyCFunction)long_long,	METH_NOARGS, | 
 | 3631 |          "Flooring an Integral returns itself."}, | 
 | 3632 | 	{"__ceil__",	(PyCFunction)long_long,	METH_NOARGS, | 
 | 3633 |          "Ceiling of an Integral returns itself."}, | 
 | 3634 | 	{"__round__",	(PyCFunction)long_round, METH_VARARGS, | 
 | 3635 |          "Rounding an Integral returns itself.\n" | 
 | 3636 | 	 "Rounding with an ndigits arguments defers to float.__round__."}, | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3637 | 	{"__getnewargs__",	(PyCFunction)long_getnewargs,	METH_NOARGS}, | 
| Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 3638 |         {"__format__", (PyCFunction)long__format__, METH_VARARGS}, | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3639 | 	{NULL,		NULL}		/* sentinel */ | 
 | 3640 | }; | 
 | 3641 |  | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3642 | static PyGetSetDef long_getset[] = { | 
 | 3643 |     {"real",  | 
 | 3644 |      (getter)long_long, (setter)NULL, | 
 | 3645 |      "the real part of a complex number", | 
 | 3646 |      NULL}, | 
 | 3647 |     {"imag",  | 
 | 3648 |      (getter)long_getN, (setter)NULL, | 
 | 3649 |      "the imaginary part of a complex number", | 
 | 3650 |      (void*)0}, | 
 | 3651 |     {"numerator",  | 
 | 3652 |      (getter)long_long, (setter)NULL, | 
 | 3653 |      "the numerator of a rational number in lowest terms", | 
 | 3654 |      NULL}, | 
 | 3655 |     {"denominator",  | 
 | 3656 |      (getter)long_getN, (setter)NULL, | 
 | 3657 |      "the denominator of a rational number in lowest terms", | 
 | 3658 |      (void*)1}, | 
 | 3659 |     {NULL}  /* Sentinel */ | 
 | 3660 | }; | 
 | 3661 |  | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3662 | PyDoc_STRVAR(long_doc, | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3663 | "int(x[, base]) -> integer\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3664 | \n\ | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3665 | Convert a string or number to an integer, if possible.  A floating\n\ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3666 | point argument will be truncated towards zero (this does not include a\n\ | 
 | 3667 | string representation of a floating point number!)  When converting a\n\ | 
 | 3668 | string, use the optional base.  It is an error to supply a base when\n\ | 
| Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3669 | converting a non-string."); | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3670 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3671 | static PyNumberMethods long_as_number = { | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3672 | 	(binaryfunc)	long_add,	/*nb_add*/ | 
 | 3673 | 	(binaryfunc)	long_sub,	/*nb_subtract*/ | 
 | 3674 | 	(binaryfunc)	long_mul,	/*nb_multiply*/ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3675 | 			long_mod,	/*nb_remainder*/ | 
 | 3676 | 			long_divmod,	/*nb_divmod*/ | 
 | 3677 | 			long_pow,	/*nb_power*/ | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3678 | 	(unaryfunc) 	long_neg,	/*nb_negative*/ | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3679 | 	(unaryfunc) 	long_long,	/*tp_positive*/ | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3680 | 	(unaryfunc) 	long_abs,	/*tp_absolute*/ | 
| Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 3681 | 	(inquiry)	long_bool,	/*tp_bool*/ | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3682 | 	(unaryfunc)	long_invert,	/*nb_invert*/ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3683 | 			long_lshift,	/*nb_lshift*/ | 
| Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 3684 | 	(binaryfunc)	long_rshift,	/*nb_rshift*/ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3685 | 			long_and,	/*nb_and*/ | 
 | 3686 | 			long_xor,	/*nb_xor*/ | 
 | 3687 | 			long_or,	/*nb_or*/ | 
| Neal Norwitz | ca81046 | 2006-08-29 07:57:59 +0000 | [diff] [blame] | 3688 | 			0,		/*nb_coerce*/ | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3689 | 			long_long,	/*nb_int*/ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3690 | 			long_long,	/*nb_long*/ | 
 | 3691 | 			long_float,	/*nb_float*/ | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3692 | 			0,		/*nb_oct*/ /* not used */ | 
 | 3693 | 			0,		/*nb_hex*/ /* not used */ | 
| Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3694 | 	0,				/* nb_inplace_add */ | 
 | 3695 | 	0,				/* nb_inplace_subtract */ | 
 | 3696 | 	0,				/* nb_inplace_multiply */ | 
| Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3697 | 	0,				/* nb_inplace_remainder */ | 
 | 3698 | 	0,				/* nb_inplace_power */ | 
 | 3699 | 	0,				/* nb_inplace_lshift */ | 
 | 3700 | 	0,				/* nb_inplace_rshift */ | 
 | 3701 | 	0,				/* nb_inplace_and */ | 
 | 3702 | 	0,				/* nb_inplace_xor */ | 
 | 3703 | 	0,				/* nb_inplace_or */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3704 | 	long_div,			/* nb_floor_divide */ | 
| Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3705 | 	long_true_divide,		/* nb_true_divide */ | 
 | 3706 | 	0,				/* nb_inplace_floor_divide */ | 
 | 3707 | 	0,				/* nb_inplace_true_divide */ | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3708 | 	long_long,			/* nb_index */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3709 | }; | 
 | 3710 |  | 
| Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3711 | PyTypeObject PyLong_Type = { | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3712 | 	PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3713 | 	"int",					/* tp_name */ | 
 | 3714 | 	/* See _PyLong_New for why this isn't | 
 | 3715 | 	   sizeof(PyLongObject) - sizeof(digit) */ | 
 | 3716 | 	sizeof(PyVarObject),			/* tp_basicsize */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3717 | 	sizeof(digit),				/* tp_itemsize */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3718 | 	long_dealloc,				/* tp_dealloc */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3719 | 	0,					/* tp_print */ | 
 | 3720 | 	0,					/* tp_getattr */ | 
 | 3721 | 	0,					/* tp_setattr */ | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3722 | 	0,					/* tp_compare */ | 
| Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3723 | 	long_repr,				/* tp_repr */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3724 | 	&long_as_number,			/* tp_as_number */ | 
 | 3725 | 	0,					/* tp_as_sequence */ | 
 | 3726 | 	0,					/* tp_as_mapping */ | 
 | 3727 | 	(hashfunc)long_hash,			/* tp_hash */ | 
 | 3728 |         0,              			/* tp_call */ | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3729 |         long_repr,				/* tp_str */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3730 | 	PyObject_GenericGetAttr,		/* tp_getattro */ | 
 | 3731 | 	0,					/* tp_setattro */ | 
 | 3732 | 	0,					/* tp_as_buffer */ | 
| Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3733 | 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 
 | 3734 | 		Py_TPFLAGS_LONG_SUBCLASS,	/* tp_flags */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3735 | 	long_doc,				/* tp_doc */ | 
 | 3736 | 	0,					/* tp_traverse */ | 
 | 3737 | 	0,					/* tp_clear */ | 
| Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 3738 | 	long_richcompare,			/* tp_richcompare */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3739 | 	0,					/* tp_weaklistoffset */ | 
 | 3740 | 	0,					/* tp_iter */ | 
 | 3741 | 	0,					/* tp_iternext */ | 
| Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3742 | 	long_methods,				/* tp_methods */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3743 | 	0,					/* tp_members */ | 
| Guido van Rossum | b43daf7 | 2007-08-01 18:08:08 +0000 | [diff] [blame] | 3744 | 	long_getset,				/* tp_getset */ | 
| Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3745 | 	0,					/* tp_base */ | 
 | 3746 | 	0,					/* tp_dict */ | 
 | 3747 | 	0,					/* tp_descr_get */ | 
 | 3748 | 	0,					/* tp_descr_set */ | 
 | 3749 | 	0,					/* tp_dictoffset */ | 
 | 3750 | 	0,					/* tp_init */ | 
 | 3751 | 	0,					/* tp_alloc */ | 
 | 3752 | 	long_new,				/* tp_new */ | 
| Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 3753 | 	PyObject_Del,                           /* tp_free */ | 
| Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 3754 | }; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3755 |  | 
 | 3756 | int | 
 | 3757 | _PyLong_Init(void) | 
 | 3758 | { | 
 | 3759 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 
 | 3760 | 	int ival; | 
 | 3761 | 	PyLongObject *v = small_ints; | 
 | 3762 | 	for (ival = -NSMALLNEGINTS; ival < 0; ival++, v++) { | 
 | 3763 | 		PyObject_INIT(v, &PyLong_Type); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3764 | 		Py_Size(v) = -1; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3765 | 		v->ob_digit[0] = -ival; | 
 | 3766 | 	} | 
 | 3767 | 	for (; ival < NSMALLPOSINTS; ival++, v++) { | 
 | 3768 | 		PyObject_INIT(v, &PyLong_Type); | 
| Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3769 | 		Py_Size(v) = ival ? 1 : 0; | 
| Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3770 | 		v->ob_digit[0] = ival; | 
 | 3771 | 	} | 
 | 3772 | #endif | 
 | 3773 | 	return 1; | 
 | 3774 | } | 
 | 3775 |  | 
 | 3776 | void | 
 | 3777 | PyLong_Fini(void) | 
 | 3778 | { | 
 | 3779 | #if 0 | 
 | 3780 | 	int i; | 
 | 3781 | 	/* This is currently not needed; the small integers | 
 | 3782 | 	   are statically allocated */ | 
 | 3783 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 
 | 3784 |         PyIntObject **q; | 
 | 3785 |  | 
 | 3786 |         i = NSMALLNEGINTS + NSMALLPOSINTS; | 
 | 3787 |         q = small_ints; | 
 | 3788 |         while (--i >= 0) { | 
 | 3789 |                 Py_XDECREF(*q); | 
 | 3790 |                 *q++ = NULL; | 
 | 3791 |         } | 
 | 3792 | #endif | 
 | 3793 | #endif | 
 | 3794 | } |