LEFT | RIGHT |
1 /* Long (arbitrary precision) integer object implementation */ | 1 /* Long (arbitrary precision) integer object implementation */ |
2 | 2 |
3 /* XXX The functional organization of this file is terrible */ | 3 /* XXX The functional organization of this file is terrible */ |
4 | 4 |
5 #include "Python.h" | 5 #include "Python.h" |
6 #include "longintrepr.h" | 6 #include "longintrepr.h" |
7 #include "structseq.h" | 7 #include "structseq.h" |
8 | 8 |
9 #include <float.h> | 9 #include <float.h> |
10 #include <ctype.h> | 10 #include <ctype.h> |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 | 12 |
13 #ifndef NSMALLPOSINTS | 13 #ifndef NSMALLPOSINTS |
14 #define NSMALLPOSINTS» » 257 | 14 #define NSMALLPOSINTS 257 |
15 #endif | 15 #endif |
16 #ifndef NSMALLNEGINTS | 16 #ifndef NSMALLNEGINTS |
17 #define NSMALLNEGINTS» » 5 | 17 #define NSMALLNEGINTS 5 |
18 #endif | 18 #endif |
19 | 19 |
20 /* convert a PyLong of size 1, 0 or -1 to an sdigit */ | 20 /* convert a PyLong of size 1, 0 or -1 to an sdigit */ |
21 #define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] :» \ | 21 #define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \ |
22 » » » (Py_SIZE(x) == 0 ? (sdigit)0 :»» » \ | 22 (Py_SIZE(x) == 0 ? (sdigit)0 : \ |
23 » » » (sdigit)(x)->ob_digit[0])) | 23 (sdigit)(x)->ob_digit[0])) |
24 #define ABS(x) ((x) < 0 ? -(x) : (x)) | 24 #define ABS(x) ((x) < 0 ? -(x) : (x)) |
25 | 25 |
26 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 26 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
27 /* Small integers are preallocated in this array so that they | 27 /* Small integers are preallocated in this array so that they |
28 can be shared. | 28 can be shared. |
29 The integers that are preallocated are those in the range | 29 The integers that are preallocated are those in the range |
30 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). | 30 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
31 */ | 31 */ |
32 static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; | 32 static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
33 #ifdef COUNT_ALLOCS | 33 #ifdef COUNT_ALLOCS |
34 int quick_int_allocs, quick_neg_int_allocs; | 34 int quick_int_allocs, quick_neg_int_allocs; |
35 #endif | 35 #endif |
36 | 36 |
37 static PyObject * | 37 static PyObject * |
38 get_small_int(sdigit ival) | 38 get_small_int(sdigit ival) |
39 { | 39 { |
40 » PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); | 40 PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS); |
41 » Py_INCREF(v); | 41 Py_INCREF(v); |
42 #ifdef COUNT_ALLOCS | 42 #ifdef COUNT_ALLOCS |
43 » if (ival >= 0) | 43 if (ival >= 0) |
44 » » quick_int_allocs++; | 44 quick_int_allocs++; |
45 » else | 45 else |
46 » » quick_neg_int_allocs++; | 46 quick_neg_int_allocs++; |
47 #endif | 47 #endif |
48 » return v; | 48 return v; |
49 } | 49 } |
50 #define CHECK_SMALL_INT(ival) \ | 50 #define CHECK_SMALL_INT(ival) \ |
51 » do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ | 51 do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ |
52 » » return get_small_int((sdigit)ival); \ | 52 return get_small_int((sdigit)ival); \ |
53 » } while(0) | 53 } while(0) |
54 | 54 |
55 static PyLongObject * | 55 static PyLongObject * |
56 maybe_small_long(PyLongObject *v) | 56 maybe_small_long(PyLongObject *v) |
57 { | 57 { |
58 » if (v && ABS(Py_SIZE(v)) <= 1) { | 58 if (v && ABS(Py_SIZE(v)) <= 1) { |
59 » » sdigit ival = MEDIUM_VALUE(v); | 59 sdigit ival = MEDIUM_VALUE(v); |
60 » » if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { | 60 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
61 » » » Py_DECREF(v); | 61 Py_DECREF(v); |
62 » » » return (PyLongObject *)get_small_int(ival); | 62 return (PyLongObject *)get_small_int(ival); |
63 » » } | 63 } |
64 » } | 64 } |
65 » return v; | 65 return v; |
66 } | 66 } |
67 #else | 67 #else |
68 #define CHECK_SMALL_INT(ival) | 68 #define CHECK_SMALL_INT(ival) |
69 #define maybe_small_long(val) (val) | 69 #define maybe_small_long(val) (val) |
70 #endif | 70 #endif |
71 | 71 |
72 /* If a freshly-allocated long is already shared, it must | 72 /* If a freshly-allocated long is already shared, it must |
73 be a small integer, so negating it must go to PyLong_FromLong */ | 73 be a small integer, so negating it must go to PyLong_FromLong */ |
74 #define NEGATE(x) \ | 74 #define NEGATE(x) \ |
75 » do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \ | 75 do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \ |
76 » else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \ | 76 else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \ |
77 » » Py_DECREF(x); (x) = (PyLongObject*)tmp; }» \ | 77 Py_DECREF(x); (x) = (PyLongObject*)tmp; } \ |
78 while(0) | 78 while(0) |
79 /* For long multiplication, use the O(N**2) school algorithm unless | 79 /* For long multiplication, use the O(N**2) school algorithm unless |
80 * both operands contain more than KARATSUBA_CUTOFF digits (this | 80 * both operands contain more than KARATSUBA_CUTOFF digits (this |
81 * being an internal Python long digit, in base BASE). | 81 * being an internal Python long digit, in base BASE). |
82 */ | 82 */ |
83 #define KARATSUBA_CUTOFF 70 | 83 #define KARATSUBA_CUTOFF 70 |
84 #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) | 84 #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF) |
85 | 85 |
86 /* For exponentiation, use the binary left-to-right algorithm | 86 /* For exponentiation, use the binary left-to-right algorithm |
87 * unless the exponent contains more than FIVEARY_CUTOFF digits. | 87 * unless the exponent contains more than FIVEARY_CUTOFF digits. |
88 * In that case, do 5 bits at a time. The potential drawback is that | 88 * In that case, do 5 bits at a time. The potential drawback is that |
89 * a table of 2**5 intermediate results is computed. | 89 * a table of 2**5 intermediate results is computed. |
90 */ | 90 */ |
91 #define FIVEARY_CUTOFF 8 | 91 #define FIVEARY_CUTOFF 8 |
92 | 92 |
93 #undef MIN | 93 #undef MIN |
94 #undef MAX | 94 #undef MAX |
95 #define MAX(x, y) ((x) < (y) ? (y) : (x)) | 95 #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
96 #define MIN(x, y) ((x) > (y) ? (y) : (x)) | 96 #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
97 | 97 |
98 #define SIGCHECK(PyTryBlock) \ | 98 #define SIGCHECK(PyTryBlock) \ |
99 » if (PyErr_CheckSignals()) PyTryBlock \ | 99 do { \ |
| 100 if (PyErr_CheckSignals()) PyTryBlock \ |
| 101 } while(0) |
100 | 102 |
101 /* Normalize (remove leading zeros from) a long int object. | 103 /* Normalize (remove leading zeros from) a long int object. |
102 Doesn't attempt to free the storage--in most cases, due to the nature | 104 Doesn't attempt to free the storage--in most cases, due to the nature |
103 of the algorithms used, this could save at most be one word anyway. */ | 105 of the algorithms used, this could save at most be one word anyway. */ |
104 | 106 |
105 static PyLongObject * | 107 static PyLongObject * |
106 long_normalize(register PyLongObject *v) | 108 long_normalize(register PyLongObject *v) |
107 { | 109 { |
108 » Py_ssize_t j = ABS(Py_SIZE(v)); | 110 Py_ssize_t j = ABS(Py_SIZE(v)); |
109 » Py_ssize_t i = j; | 111 Py_ssize_t i = j; |
110 | 112 |
111 » while (i > 0 && v->ob_digit[i-1] == 0) | 113 while (i > 0 && v->ob_digit[i-1] == 0) |
112 » » --i; | 114 --i; |
113 » if (i != j) | 115 if (i != j) |
114 » » Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; | 116 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i; |
115 » return v; | 117 return v; |
116 } | 118 } |
117 | 119 |
118 /* Allocate a new long int object with size digits. | 120 /* Allocate a new long int object with size digits. |
119 Return NULL and set exception if we run out of memory. */ | 121 Return NULL and set exception if we run out of memory. */ |
120 | 122 |
121 #define MAX_LONG_DIGITS \ | 123 #define MAX_LONG_DIGITS \ |
122 » ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) | 124 ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit)) |
123 | 125 |
124 PyLongObject * | 126 PyLongObject * |
125 _PyLong_New(Py_ssize_t size) | 127 _PyLong_New(Py_ssize_t size) |
126 { | 128 { |
127 » PyLongObject *result; | 129 PyLongObject *result; |
128 » /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + | 130 /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) + |
129 » sizeof(digit)*size. Previous incarnations of this code used | 131 sizeof(digit)*size. Previous incarnations of this code used |
130 » sizeof(PyVarObject) instead of the offsetof, but this risks being | 132 sizeof(PyVarObject) instead of the offsetof, but this risks being |
131 » incorrect in the presence of padding between the PyVarObject header | 133 incorrect in the presence of padding between the PyVarObject header |
132 » and the digits. */ | 134 and the digits. */ |
133 » if (size > (Py_ssize_t)MAX_LONG_DIGITS) { | 135 if (size > (Py_ssize_t)MAX_LONG_DIGITS) { |
134 » » PyErr_SetString(PyExc_OverflowError, | 136 PyErr_SetString(PyExc_OverflowError, |
135 » » » » "too many digits in integer"); | 137 "too many digits in integer"); |
136 » » return NULL; | 138 return NULL; |
137 » } | 139 } |
138 » result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + | 140 result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) + |
139 » » » » size*sizeof(digit)); | 141 size*sizeof(digit)); |
140 » if (!result) { | 142 if (!result) { |
141 » » PyErr_NoMemory(); | 143 PyErr_NoMemory(); |
142 » » return NULL; | 144 return NULL; |
143 » } | 145 } |
144 » return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); | 146 return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size); |
145 } | 147 } |
146 | 148 |
147 PyObject * | 149 PyObject * |
148 _PyLong_Copy(PyLongObject *src) | 150 _PyLong_Copy(PyLongObject *src) |
149 { | 151 { |
150 » PyLongObject *result; | 152 PyLongObject *result; |
151 » Py_ssize_t i; | 153 Py_ssize_t i; |
152 | 154 |
153 » assert(src != NULL); | 155 assert(src != NULL); |
154 » i = Py_SIZE(src); | 156 i = Py_SIZE(src); |
155 » if (i < 0) | 157 if (i < 0) |
156 » » i = -(i); | 158 i = -(i); |
157 » if (i < 2) { | 159 if (i < 2) { |
158 » » sdigit ival = src->ob_digit[0]; | 160 sdigit ival = src->ob_digit[0]; |
159 » » if (Py_SIZE(src) < 0) | 161 if (Py_SIZE(src) < 0) |
160 » » » ival = -ival; | 162 ival = -ival; |
161 » » CHECK_SMALL_INT(ival); | 163 CHECK_SMALL_INT(ival); |
162 » } | 164 } |
163 » result = _PyLong_New(i); | 165 result = _PyLong_New(i); |
164 » if (result != NULL) { | 166 if (result != NULL) { |
165 » » Py_SIZE(result) = Py_SIZE(src); | 167 Py_SIZE(result) = Py_SIZE(src); |
166 » » while (--i >= 0) | 168 while (--i >= 0) |
167 » » » result->ob_digit[i] = src->ob_digit[i]; | 169 result->ob_digit[i] = src->ob_digit[i]; |
168 » } | 170 } |
169 » return (PyObject *)result; | 171 return (PyObject *)result; |
170 } | 172 } |
171 | 173 |
172 /* Create a new long int object from a C long int */ | 174 /* Create a new long int object from a C long int */ |
173 | 175 |
174 PyObject * | 176 PyObject * |
175 PyLong_FromLong(long ival) | 177 PyLong_FromLong(long ival) |
176 { | 178 { |
177 » PyLongObject *v; | 179 PyLongObject *v; |
178 » unsigned long abs_ival; | 180 unsigned long abs_ival; |
179 » unsigned long t; /* unsigned so >> doesn't propagate sign bit */ | 181 unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
180 » int ndigits = 0; | 182 int ndigits = 0; |
181 » int sign = 1; | 183 int sign = 1; |
182 | 184 |
183 » CHECK_SMALL_INT(ival); | 185 CHECK_SMALL_INT(ival); |
184 | 186 |
185 » if (ival < 0) { | 187 if (ival < 0) { |
186 » » /* negate: can't write this as abs_ival = -ival since that | 188 /* negate: can't write this as abs_ival = -ival since that |
187 » » invokes undefined behaviour when ival is LONG_MIN */ | 189 invokes undefined behaviour when ival is LONG_MIN */ |
188 » » abs_ival = 0U-(unsigned long)ival; | 190 abs_ival = 0U-(unsigned long)ival; |
189 » » sign = -1; | 191 sign = -1; |
190 » } | 192 } |
191 » else { | 193 else { |
192 » » abs_ival = (unsigned long)ival; | 194 abs_ival = (unsigned long)ival; |
193 » } | 195 } |
194 | 196 |
195 » /* Fast path for single-digit ints */ | 197 /* Fast path for single-digit ints */ |
196 » if (!(abs_ival >> PyLong_SHIFT)) { | 198 if (!(abs_ival >> PyLong_SHIFT)) { |
197 » » v = _PyLong_New(1); | 199 v = _PyLong_New(1); |
198 » » if (v) { | 200 if (v) { |
199 » » » Py_SIZE(v) = sign; | 201 Py_SIZE(v) = sign; |
200 » » » v->ob_digit[0] = Py_SAFE_DOWNCAST( | 202 v->ob_digit[0] = Py_SAFE_DOWNCAST( |
201 » » » » abs_ival, unsigned long, digit); | 203 abs_ival, unsigned long, digit); |
202 » » } | 204 } |
203 » » return (PyObject*)v; | 205 return (PyObject*)v; |
204 » } | 206 } |
205 | 207 |
206 #if PyLong_SHIFT==15 | 208 #if PyLong_SHIFT==15 |
207 » /* 2 digits */ | 209 /* 2 digits */ |
208 » if (!(abs_ival >> 2*PyLong_SHIFT)) { | 210 if (!(abs_ival >> 2*PyLong_SHIFT)) { |
209 » » v = _PyLong_New(2); | 211 v = _PyLong_New(2); |
210 » » if (v) { | 212 if (v) { |
211 » » » Py_SIZE(v) = 2*sign; | 213 Py_SIZE(v) = 2*sign; |
212 » » » v->ob_digit[0] = Py_SAFE_DOWNCAST( | 214 v->ob_digit[0] = Py_SAFE_DOWNCAST( |
213 » » » » abs_ival & PyLong_MASK, unsigned long, digit); | 215 abs_ival & PyLong_MASK, unsigned long, digit); |
214 » » » v->ob_digit[1] = Py_SAFE_DOWNCAST( | 216 v->ob_digit[1] = Py_SAFE_DOWNCAST( |
215 » » » abs_ival >> PyLong_SHIFT, unsigned long, digit); | 217 abs_ival >> PyLong_SHIFT, unsigned long, digit); |
216 » » } | 218 } |
217 » » return (PyObject*)v; | 219 return (PyObject*)v; |
218 » } | 220 } |
219 #endif | 221 #endif |
220 | 222 |
221 » /* Larger numbers: loop to determine number of digits */ | 223 /* Larger numbers: loop to determine number of digits */ |
222 » t = abs_ival; | 224 t = abs_ival; |
223 » while (t) { | 225 while (t) { |
224 » » ++ndigits; | 226 ++ndigits; |
225 » » t >>= PyLong_SHIFT; | 227 t >>= PyLong_SHIFT; |
226 » } | 228 } |
227 » v = _PyLong_New(ndigits); | 229 v = _PyLong_New(ndigits); |
228 » if (v != NULL) { | 230 if (v != NULL) { |
229 » » digit *p = v->ob_digit; | 231 digit *p = v->ob_digit; |
230 » » Py_SIZE(v) = ndigits*sign; | 232 Py_SIZE(v) = ndigits*sign; |
231 » » t = abs_ival; | 233 t = abs_ival; |
232 » » while (t) { | 234 while (t) { |
233 » » » *p++ = Py_SAFE_DOWNCAST( | 235 *p++ = Py_SAFE_DOWNCAST( |
234 » » » » t & PyLong_MASK, unsigned long, digit); | 236 t & PyLong_MASK, unsigned long, digit); |
235 » » » t >>= PyLong_SHIFT; | 237 t >>= PyLong_SHIFT; |
236 » » } | 238 } |
237 » } | 239 } |
238 » return (PyObject *)v; | 240 return (PyObject *)v; |
239 } | 241 } |
240 | 242 |
241 /* Create a new long int object from a C unsigned long int */ | 243 /* Create a new long int object from a C unsigned long int */ |
242 | 244 |
243 PyObject * | 245 PyObject * |
244 PyLong_FromUnsignedLong(unsigned long ival) | 246 PyLong_FromUnsignedLong(unsigned long ival) |
245 { | 247 { |
246 » PyLongObject *v; | 248 PyLongObject *v; |
247 » unsigned long t; | 249 unsigned long t; |
248 » int ndigits = 0; | 250 int ndigits = 0; |
249 | 251 |
250 » if (ival < PyLong_BASE) | 252 if (ival < PyLong_BASE) |
251 » » return PyLong_FromLong(ival); | 253 return PyLong_FromLong(ival); |
252 » /* Count the number of Python digits. */ | 254 /* Count the number of Python digits. */ |
253 » t = (unsigned long)ival; | 255 t = (unsigned long)ival; |
254 » while (t) { | 256 while (t) { |
255 » » ++ndigits; | 257 ++ndigits; |
256 » » t >>= PyLong_SHIFT; | 258 t >>= PyLong_SHIFT; |
257 » } | 259 } |
258 » v = _PyLong_New(ndigits); | 260 v = _PyLong_New(ndigits); |
259 » if (v != NULL) { | 261 if (v != NULL) { |
260 » » digit *p = v->ob_digit; | 262 digit *p = v->ob_digit; |
261 » » Py_SIZE(v) = ndigits; | 263 Py_SIZE(v) = ndigits; |
262 » » while (ival) { | 264 while (ival) { |
263 » » » *p++ = (digit)(ival & PyLong_MASK); | 265 *p++ = (digit)(ival & PyLong_MASK); |
264 » » » ival >>= PyLong_SHIFT; | 266 ival >>= PyLong_SHIFT; |
265 » » } | 267 } |
266 » } | 268 } |
267 » return (PyObject *)v; | 269 return (PyObject *)v; |
268 } | 270 } |
269 | 271 |
270 /* Create a new long int object from a C double */ | 272 /* Create a new long int object from a C double */ |
271 | 273 |
272 PyObject * | 274 PyObject * |
273 PyLong_FromDouble(double dval) | 275 PyLong_FromDouble(double dval) |
274 { | 276 { |
275 » PyLongObject *v; | 277 PyLongObject *v; |
276 » double frac; | 278 double frac; |
277 » int i, ndig, expo, neg; | 279 int i, ndig, expo, neg; |
278 » neg = 0; | 280 neg = 0; |
279 » if (Py_IS_INFINITY(dval)) { | 281 if (Py_IS_INFINITY(dval)) { |
280 » » PyErr_SetString(PyExc_OverflowError, | 282 PyErr_SetString(PyExc_OverflowError, |
281 » » » "cannot convert float infinity to integer"); | 283 "cannot convert float infinity to integer"); |
282 » » return NULL; | 284 return NULL; |
283 » } | 285 } |
284 » if (Py_IS_NAN(dval)) { | 286 if (Py_IS_NAN(dval)) { |
285 » » PyErr_SetString(PyExc_ValueError, | 287 PyErr_SetString(PyExc_ValueError, |
286 » » » "cannot convert float NaN to integer"); | 288 "cannot convert float NaN to integer"); |
287 » » return NULL; | 289 return NULL; |
288 » } | 290 } |
289 » if (dval < 0.0) { | 291 if (dval < 0.0) { |
290 » » neg = 1; | 292 neg = 1; |
291 » » dval = -dval; | 293 dval = -dval; |
292 » } | 294 } |
293 » frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ | 295 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
294 » if (expo <= 0) | 296 if (expo <= 0) |
295 » » return PyLong_FromLong(0L); | 297 return PyLong_FromLong(0L); |
296 » ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ | 298 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */ |
297 » v = _PyLong_New(ndig); | 299 v = _PyLong_New(ndig); |
298 » if (v == NULL) | 300 if (v == NULL) |
299 » » return NULL; | 301 return NULL; |
300 » frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); | 302 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1); |
301 » for (i = ndig; --i >= 0; ) { | 303 for (i = ndig; --i >= 0; ) { |
302 » » digit bits = (digit)frac; | 304 digit bits = (digit)frac; |
303 » » v->ob_digit[i] = bits; | 305 v->ob_digit[i] = bits; |
304 » » frac = frac - (double)bits; | 306 frac = frac - (double)bits; |
305 » » frac = ldexp(frac, PyLong_SHIFT); | 307 frac = ldexp(frac, PyLong_SHIFT); |
306 » } | 308 } |
307 » if (neg) | 309 if (neg) |
308 » » Py_SIZE(v) = -(Py_SIZE(v)); | 310 Py_SIZE(v) = -(Py_SIZE(v)); |
309 » return (PyObject *)v; | 311 return (PyObject *)v; |
310 } | 312 } |
311 | 313 |
312 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define | 314 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define |
313 * anything about what happens when a signed integer operation overflows, | 315 * anything about what happens when a signed integer operation overflows, |
314 * and some compilers think they're doing you a favor by being "clever" | 316 * and some compilers think they're doing you a favor by being "clever" |
315 * then. The bit pattern for the largest postive signed long is | 317 * then. The bit pattern for the largest postive signed long is |
316 * (unsigned long)LONG_MAX, and for the smallest negative signed long | 318 * (unsigned long)LONG_MAX, and for the smallest negative signed long |
317 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. | 319 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN. |
318 * However, some other compilers warn about applying unary minus to an | 320 * However, some other compilers warn about applying unary minus to an |
319 * unsigned operand. Hence the weird "0-". | 321 * unsigned operand. Hence the weird "0-". |
320 */ | 322 */ |
321 #define PY_ABS_LONG_MIN»» (0-(unsigned long)LONG_MIN) | 323 #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN) |
322 #define PY_ABS_SSIZE_T_MIN» (0-(size_t)PY_SSIZE_T_MIN) | 324 #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN) |
323 | 325 |
324 /* Get a C long int from a long int object. | 326 /* Get a C long int from a long int object. |
325 Returns -1 and sets an error condition if overflow occurs. */ | 327 Returns -1 and sets an error condition if overflow occurs. */ |
326 | 328 |
327 long | 329 long |
328 PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) | 330 PyLong_AsLongAndOverflow(PyObject *vv, int *overflow) |
329 { | 331 { |
330 » /* This version by Tim Peters */ | 332 /* This version by Tim Peters */ |
331 » register PyLongObject *v; | 333 register PyLongObject *v; |
332 » unsigned long x, prev; | 334 unsigned long x, prev; |
333 » long res; | 335 long res; |
334 » Py_ssize_t i; | 336 Py_ssize_t i; |
335 » int sign; | 337 int sign; |
336 » int do_decref = 0; /* if nb_int was called */ | 338 int do_decref = 0; /* if nb_int was called */ |
337 | 339 |
338 » *overflow = 0; | 340 *overflow = 0; |
339 » if (vv == NULL) { | 341 if (vv == NULL) { |
340 » » PyErr_BadInternalCall(); | 342 PyErr_BadInternalCall(); |
341 » » return -1; | 343 return -1; |
342 » } | 344 } |
343 | 345 |
344 » if (!PyLong_Check(vv)) { | 346 if (!PyLong_Check(vv)) { |
345 » » PyNumberMethods *nb; | 347 PyNumberMethods *nb; |
346 » » nb = vv->ob_type->tp_as_number; | 348 nb = vv->ob_type->tp_as_number; |
347 » » if (nb == NULL || nb->nb_int == NULL) { | 349 if (nb == NULL || nb->nb_int == NULL) { |
348 » » » PyErr_SetString(PyExc_TypeError, | 350 PyErr_SetString(PyExc_TypeError, |
349 » » » » » "an integer is required"); | 351 "an integer is required"); |
350 » » » return -1; | 352 return -1; |
351 » » } | 353 } |
352 » » vv = (*nb->nb_int) (vv); | 354 vv = (*nb->nb_int) (vv); |
353 » » if (vv == NULL) | 355 if (vv == NULL) |
354 » » » return -1; | 356 return -1; |
355 » » do_decref = 1; | 357 do_decref = 1; |
356 » » if (!PyLong_Check(vv)) { | 358 if (!PyLong_Check(vv)) { |
357 » » » Py_DECREF(vv); | 359 Py_DECREF(vv); |
358 » » » PyErr_SetString(PyExc_TypeError, | 360 PyErr_SetString(PyExc_TypeError, |
359 » » » » » "nb_int should return int object"); | 361 "nb_int should return int object"); |
360 » » » return -1; | 362 return -1; |
361 » » } | 363 } |
362 » } | 364 } |
363 | 365 |
364 » res = -1; | 366 res = -1; |
365 » v = (PyLongObject *)vv; | 367 v = (PyLongObject *)vv; |
366 » i = Py_SIZE(v); | 368 i = Py_SIZE(v); |
367 | 369 |
368 » switch (i) { | 370 switch (i) { |
369 » case -1: | 371 case -1: |
370 » » res = -(sdigit)v->ob_digit[0]; | 372 res = -(sdigit)v->ob_digit[0]; |
371 » » break; | 373 break; |
372 » case 0: | 374 case 0: |
373 » » res = 0; | 375 res = 0; |
374 » » break; | 376 break; |
375 » case 1: | 377 case 1: |
376 » » res = v->ob_digit[0]; | 378 res = v->ob_digit[0]; |
377 » » break; | 379 break; |
378 » default: | 380 default: |
379 » » sign = 1; | 381 sign = 1; |
380 » » x = 0; | 382 x = 0; |
381 » » if (i < 0) { | 383 if (i < 0) { |
382 » » » sign = -1; | 384 sign = -1; |
383 » » » i = -(i); | 385 i = -(i); |
384 » » } | 386 } |
385 » » while (--i >= 0) { | 387 while (--i >= 0) { |
386 » » » prev = x; | 388 prev = x; |
387 » » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 389 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
388 » » » if ((x >> PyLong_SHIFT) != prev) { | 390 if ((x >> PyLong_SHIFT) != prev) { |
389 » » » » *overflow = sign; | 391 *overflow = sign; |
390 » » » » goto exit; | 392 goto exit; |
391 » » » } | 393 } |
392 » » } | 394 } |
393 » » /* Haven't lost any bits, but casting to long requires extra | 395 /* Haven't lost any bits, but casting to long requires extra |
394 » » * care (see comment above). | 396 * care (see comment above). |
395 » » */ | 397 */ |
396 » » if (x <= (unsigned long)LONG_MAX) { | 398 if (x <= (unsigned long)LONG_MAX) { |
397 » » » res = (long)x * sign; | 399 res = (long)x * sign; |
398 » » } | 400 } |
399 » » else if (sign < 0 && x == PY_ABS_LONG_MIN) { | 401 else if (sign < 0 && x == PY_ABS_LONG_MIN) { |
400 » » » res = LONG_MIN; | 402 res = LONG_MIN; |
401 » » } | 403 } |
402 » » else { | 404 else { |
403 » » » *overflow = sign; | 405 *overflow = sign; |
404 » » » /* res is already set to -1 */ | 406 /* res is already set to -1 */ |
405 » » } | 407 } |
406 » } | 408 } |
407 exit: | 409 exit: |
408 » if (do_decref) { | 410 if (do_decref) { |
409 » » Py_DECREF(vv); | 411 Py_DECREF(vv); |
410 » } | 412 } |
411 » return res; | 413 return res; |
412 } | 414 } |
413 | 415 |
414 long· | 416 long |
415 PyLong_AsLong(PyObject *obj) | 417 PyLong_AsLong(PyObject *obj) |
416 { | 418 { |
417 » int overflow; | 419 int overflow; |
418 » long result = PyLong_AsLongAndOverflow(obj, &overflow); | 420 long result = PyLong_AsLongAndOverflow(obj, &overflow); |
419 » if (overflow) { | 421 if (overflow) { |
420 » » /* XXX: could be cute and give a different· | 422 /* XXX: could be cute and give a different |
421 » » message for overflow == -1 */ | 423 message for overflow == -1 */ |
422 » » PyErr_SetString(PyExc_OverflowError, | 424 PyErr_SetString(PyExc_OverflowError, |
423 » » » » "Python int too large to convert to C long"); | 425 "Python int too large to convert to C long"); |
424 » } | 426 } |
425 » return result; | 427 return result; |
426 } | 428 } |
427 | 429 |
428 /* Get a Py_ssize_t from a long int object. | 430 /* Get a Py_ssize_t from a long int object. |
429 Returns -1 and sets an error condition if overflow occurs. */ | 431 Returns -1 and sets an error condition if overflow occurs. */ |
430 | 432 |
431 Py_ssize_t | 433 Py_ssize_t |
432 PyLong_AsSsize_t(PyObject *vv) { | 434 PyLong_AsSsize_t(PyObject *vv) { |
433 » register PyLongObject *v; | 435 register PyLongObject *v; |
434 » size_t x, prev; | 436 size_t x, prev; |
435 » Py_ssize_t i; | 437 Py_ssize_t i; |
436 » int sign; | 438 int sign; |
437 | 439 |
438 » if (vv == NULL) { | 440 if (vv == NULL) { |
439 » » PyErr_BadInternalCall(); | 441 PyErr_BadInternalCall(); |
440 » » return -1; | 442 return -1; |
441 » } | 443 } |
442 » if (!PyLong_Check(vv)) { | 444 if (!PyLong_Check(vv)) { |
443 » » PyErr_SetString(PyExc_TypeError, "an integer is required"); | 445 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
444 » » return -1; | 446 return -1; |
445 » } | 447 } |
446 | 448 |
447 » v = (PyLongObject *)vv; | 449 v = (PyLongObject *)vv; |
448 » i = Py_SIZE(v); | 450 i = Py_SIZE(v); |
449 » switch (i) { | 451 switch (i) { |
450 » case -1: return -(sdigit)v->ob_digit[0]; | 452 case -1: return -(sdigit)v->ob_digit[0]; |
451 » case 0: return 0; | 453 case 0: return 0; |
452 » case 1: return v->ob_digit[0]; | 454 case 1: return v->ob_digit[0]; |
453 » } | 455 } |
454 » sign = 1; | 456 sign = 1; |
455 » x = 0; | 457 x = 0; |
456 » if (i < 0) { | 458 if (i < 0) { |
457 » » sign = -1; | 459 sign = -1; |
458 » » i = -(i); | 460 i = -(i); |
459 » } | 461 } |
460 » while (--i >= 0) { | 462 while (--i >= 0) { |
461 » » prev = x; | 463 prev = x; |
462 » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 464 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
463 » » if ((x >> PyLong_SHIFT) != prev) | 465 if ((x >> PyLong_SHIFT) != prev) |
464 » » » goto overflow; | 466 goto overflow; |
465 » } | 467 } |
466 » /* Haven't lost any bits, but casting to a signed type requires | 468 /* Haven't lost any bits, but casting to a signed type requires |
467 » * extra care (see comment above). | 469 * extra care (see comment above). |
468 » */ | 470 */ |
469 » if (x <= (size_t)PY_SSIZE_T_MAX) { | 471 if (x <= (size_t)PY_SSIZE_T_MAX) { |
470 » » return (Py_ssize_t)x * sign; | 472 return (Py_ssize_t)x * sign; |
471 » } | 473 } |
472 » else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { | 474 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) { |
473 » » return PY_SSIZE_T_MIN; | 475 return PY_SSIZE_T_MIN; |
474 » } | 476 } |
475 » /* else overflow */ | 477 /* else overflow */ |
476 | 478 |
477 overflow: | 479 overflow: |
478 » PyErr_SetString(PyExc_OverflowError, | 480 PyErr_SetString(PyExc_OverflowError, |
479 » » » "Python int too large to convert to C ssize_t"); | 481 "Python int too large to convert to C ssize_t"); |
480 » return -1; | 482 return -1; |
481 } | 483 } |
482 | 484 |
483 /* Get a C unsigned long int from a long int object. | 485 /* Get a C unsigned long int from a long int object. |
484 Returns -1 and sets an error condition if overflow occurs. */ | 486 Returns -1 and sets an error condition if overflow occurs. */ |
485 | 487 |
486 unsigned long | 488 unsigned long |
487 PyLong_AsUnsignedLong(PyObject *vv) | 489 PyLong_AsUnsignedLong(PyObject *vv) |
488 { | 490 { |
489 » register PyLongObject *v; | 491 register PyLongObject *v; |
490 » unsigned long x, prev; | 492 unsigned long x, prev; |
491 » Py_ssize_t i; | 493 Py_ssize_t i; |
492 | 494 |
493 » if (vv == NULL) { | 495 if (vv == NULL) { |
494 » » PyErr_BadInternalCall(); | 496 PyErr_BadInternalCall(); |
495 » » return (unsigned long)-1; | 497 return (unsigned long)-1; |
496 » } | 498 } |
497 » if (!PyLong_Check(vv)) { | 499 if (!PyLong_Check(vv)) { |
498 » » PyErr_SetString(PyExc_TypeError, "an integer is required"); | 500 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
499 » » return (unsigned long)-1; | 501 return (unsigned long)-1; |
500 » } | 502 } |
501 | 503 |
502 » v = (PyLongObject *)vv; | 504 v = (PyLongObject *)vv; |
503 » i = Py_SIZE(v); | 505 i = Py_SIZE(v); |
504 » x = 0; | 506 x = 0; |
505 » if (i < 0) { | 507 if (i < 0) { |
506 » » PyErr_SetString(PyExc_OverflowError, | 508 PyErr_SetString(PyExc_OverflowError, |
507 » » » "can't convert negative value to unsigned int"); | 509 "can't convert negative value to unsigned int"); |
508 » » return (unsigned long) -1; | 510 return (unsigned long) -1; |
509 » } | 511 } |
510 » switch (i) { | 512 switch (i) { |
511 » case 0: return 0; | 513 case 0: return 0; |
512 » case 1: return v->ob_digit[0]; | 514 case 1: return v->ob_digit[0]; |
513 » } | 515 } |
514 » while (--i >= 0) { | 516 while (--i >= 0) { |
515 » » prev = x; | 517 prev = x; |
516 » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 518 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
517 » » if ((x >> PyLong_SHIFT) != prev) { | 519 if ((x >> PyLong_SHIFT) != prev) { |
518 » » » PyErr_SetString(PyExc_OverflowError, | 520 PyErr_SetString(PyExc_OverflowError, |
519 » » » "python int too large to convert to C unsigned long"); | 521 "python int too large to convert " |
520 » » » return (unsigned long) -1; | 522 "to C unsigned long"); |
521 » » } | 523 return (unsigned long) -1; |
522 » } | 524 } |
523 » return x; | 525 } |
| 526 return x; |
524 } | 527 } |
525 | 528 |
526 /* Get a C unsigned long int from a long int object. | 529 /* Get a C unsigned long int from a long int object. |
527 Returns -1 and sets an error condition if overflow occurs. */ | 530 Returns -1 and sets an error condition if overflow occurs. */ |
528 | 531 |
529 size_t | 532 size_t |
530 PyLong_AsSize_t(PyObject *vv) | 533 PyLong_AsSize_t(PyObject *vv) |
531 { | 534 { |
532 » register PyLongObject *v; | 535 register PyLongObject *v; |
533 » size_t x, prev; | 536 size_t x, prev; |
534 » Py_ssize_t i; | 537 Py_ssize_t i; |
535 | 538 |
536 » if (vv == NULL) { | 539 if (vv == NULL) { |
537 » » PyErr_BadInternalCall(); | 540 PyErr_BadInternalCall(); |
538 » » return (size_t) -1; | 541 return (size_t) -1; |
539 » } | 542 } |
540 » if (!PyLong_Check(vv)) { | 543 if (!PyLong_Check(vv)) { |
541 » » PyErr_SetString(PyExc_TypeError, "an integer is required"); | 544 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
542 » » return (size_t)-1; | 545 return (size_t)-1; |
543 » } | 546 } |
544 | 547 |
545 » v = (PyLongObject *)vv; | 548 v = (PyLongObject *)vv; |
546 » i = Py_SIZE(v); | 549 i = Py_SIZE(v); |
547 » x = 0; | 550 x = 0; |
548 » if (i < 0) { | 551 if (i < 0) { |
549 » » PyErr_SetString(PyExc_OverflowError, | 552 PyErr_SetString(PyExc_OverflowError, |
550 » » » "can't convert negative value to size_t"); | 553 "can't convert negative value to size_t"); |
551 » » return (size_t) -1; | 554 return (size_t) -1; |
552 » } | 555 } |
553 » switch (i) { | 556 switch (i) { |
554 » case 0: return 0; | 557 case 0: return 0; |
555 » case 1: return v->ob_digit[0]; | 558 case 1: return v->ob_digit[0]; |
556 » } | 559 } |
557 » while (--i >= 0) { | 560 while (--i >= 0) { |
558 » » prev = x; | 561 prev = x; |
559 » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 562 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
560 » » if ((x >> PyLong_SHIFT) != prev) { | 563 if ((x >> PyLong_SHIFT) != prev) { |
561 » » » PyErr_SetString(PyExc_OverflowError, | 564 PyErr_SetString(PyExc_OverflowError, |
562 » » » "Python int too large to convert to C size_t"); | 565 "Python int too large to convert to C size_t"); |
563 » » » return (unsigned long) -1; | 566 return (unsigned long) -1; |
564 » » } | 567 } |
565 » } | 568 } |
566 » return x; | 569 return x; |
567 } | 570 } |
568 | 571 |
569 /* Get a C unsigned long int from a long int object, ignoring the high bits. | 572 /* Get a C unsigned long int from a long int object, ignoring the high bits. |
570 Returns -1 and sets an error condition if an error occurs. */ | 573 Returns -1 and sets an error condition if an error occurs. */ |
571 | 574 |
572 static unsigned long | 575 static unsigned long |
573 _PyLong_AsUnsignedLongMask(PyObject *vv) | 576 _PyLong_AsUnsignedLongMask(PyObject *vv) |
574 { | 577 { |
575 » register PyLongObject *v; | 578 register PyLongObject *v; |
576 » unsigned long x; | 579 unsigned long x; |
577 » Py_ssize_t i; | 580 Py_ssize_t i; |
578 » int sign; | 581 int sign; |
579 | 582 |
580 » if (vv == NULL || !PyLong_Check(vv)) { | 583 if (vv == NULL || !PyLong_Check(vv)) { |
581 » » PyErr_BadInternalCall(); | 584 PyErr_BadInternalCall(); |
582 » » return (unsigned long) -1; | 585 return (unsigned long) -1; |
583 » } | 586 } |
584 » v = (PyLongObject *)vv; | 587 v = (PyLongObject *)vv; |
585 » i = Py_SIZE(v); | 588 i = Py_SIZE(v); |
586 » switch (i) { | 589 switch (i) { |
587 » case 0: return 0; | 590 case 0: return 0; |
588 » case 1: return v->ob_digit[0]; | 591 case 1: return v->ob_digit[0]; |
589 » } | 592 } |
590 » sign = 1; | 593 sign = 1; |
591 » x = 0; | 594 x = 0; |
592 » if (i < 0) { | 595 if (i < 0) { |
593 » » sign = -1; | 596 sign = -1; |
594 » » i = -i; | 597 i = -i; |
595 » } | 598 } |
596 » while (--i >= 0) { | 599 while (--i >= 0) { |
597 » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 600 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
598 » } | 601 } |
599 » return x * sign; | 602 return x * sign; |
600 } | 603 } |
601 | 604 |
602 unsigned long | 605 unsigned long |
603 PyLong_AsUnsignedLongMask(register PyObject *op) | 606 PyLong_AsUnsignedLongMask(register PyObject *op) |
604 { | 607 { |
605 » PyNumberMethods *nb; | 608 PyNumberMethods *nb; |
606 » PyLongObject *lo; | 609 PyLongObject *lo; |
607 » unsigned long val; | 610 unsigned long val; |
608 | 611 |
609 » if (op && PyLong_Check(op)) | 612 if (op && PyLong_Check(op)) |
610 » » return _PyLong_AsUnsignedLongMask(op); | 613 return _PyLong_AsUnsignedLongMask(op); |
611 | 614 |
612 » if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || | 615 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
613 » nb->nb_int == NULL) { | 616 nb->nb_int == NULL) { |
614 » » PyErr_SetString(PyExc_TypeError, "an integer is required"); | 617 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
615 » » return (unsigned long)-1; | 618 return (unsigned long)-1; |
616 » } | 619 } |
617 | 620 |
618 » lo = (PyLongObject*) (*nb->nb_int) (op); | 621 lo = (PyLongObject*) (*nb->nb_int) (op); |
619 » if (lo == NULL) | 622 if (lo == NULL) |
620 » » return (unsigned long)-1; | 623 return (unsigned long)-1; |
621 » if (PyLong_Check(lo)) { | 624 if (PyLong_Check(lo)) { |
622 » » val = _PyLong_AsUnsignedLongMask((PyObject *)lo); | 625 val = _PyLong_AsUnsignedLongMask((PyObject *)lo); |
623 » » Py_DECREF(lo); | 626 Py_DECREF(lo); |
624 » » if (PyErr_Occurred()) | 627 if (PyErr_Occurred()) |
625 » » » return (unsigned long)-1; | 628 return (unsigned long)-1; |
626 » » return val; | 629 return val; |
627 » } | 630 } |
628 » else | 631 else |
629 » { | 632 { |
630 » » Py_DECREF(lo); | 633 Py_DECREF(lo); |
631 » » PyErr_SetString(PyExc_TypeError, | 634 PyErr_SetString(PyExc_TypeError, |
632 » » » » "nb_int should return int object"); | 635 "nb_int should return int object"); |
633 » » return (unsigned long)-1; | 636 return (unsigned long)-1; |
634 » } | 637 } |
635 } | 638 } |
636 | 639 |
637 int | 640 int |
638 _PyLong_Sign(PyObject *vv) | 641 _PyLong_Sign(PyObject *vv) |
639 { | 642 { |
640 » PyLongObject *v = (PyLongObject *)vv; | 643 PyLongObject *v = (PyLongObject *)vv; |
641 | 644 |
642 » assert(v != NULL); | 645 assert(v != NULL); |
643 » assert(PyLong_Check(v)); | 646 assert(PyLong_Check(v)); |
644 | 647 |
645 » return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); | 648 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1); |
646 } | 649 } |
647 | 650 |
648 size_t | 651 size_t |
649 _PyLong_NumBits(PyObject *vv) | 652 _PyLong_NumBits(PyObject *vv) |
650 { | 653 { |
651 » PyLongObject *v = (PyLongObject *)vv; | 654 PyLongObject *v = (PyLongObject *)vv; |
652 » size_t result = 0; | 655 size_t result = 0; |
653 » Py_ssize_t ndigits; | 656 Py_ssize_t ndigits; |
654 | 657 |
655 » assert(v != NULL); | 658 assert(v != NULL); |
656 » assert(PyLong_Check(v)); | 659 assert(PyLong_Check(v)); |
657 » ndigits = ABS(Py_SIZE(v)); | 660 ndigits = ABS(Py_SIZE(v)); |
658 » assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); | 661 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
659 » if (ndigits > 0) { | 662 if (ndigits > 0) { |
660 » » digit msd = v->ob_digit[ndigits - 1]; | 663 digit msd = v->ob_digit[ndigits - 1]; |
661 | 664 |
662 » » result = (ndigits - 1) * PyLong_SHIFT; | 665 result = (ndigits - 1) * PyLong_SHIFT; |
663 » » if (result / PyLong_SHIFT != (size_t)(ndigits - 1)) | 666 if (result / PyLong_SHIFT != (size_t)(ndigits - 1)) |
664 » » » goto Overflow; | 667 goto Overflow; |
665 » » do { | 668 do { |
666 » » » ++result; | 669 ++result; |
667 » » » if (result == 0) | 670 if (result == 0) |
668 » » » » goto Overflow; | 671 goto Overflow; |
669 » » » msd >>= 1; | 672 msd >>= 1; |
670 » » } while (msd); | 673 } while (msd); |
671 » } | 674 } |
672 » return result; | 675 return result; |
673 | 676 |
674 Overflow: | 677 Overflow: |
675 » PyErr_SetString(PyExc_OverflowError, "int has too many bits " | 678 PyErr_SetString(PyExc_OverflowError, "int has too many bits " |
676 » » » "to express in a platform size_t"); | 679 "to express in a platform size_t"); |
677 » return (size_t)-1; | 680 return (size_t)-1; |
678 } | 681 } |
679 | 682 |
680 PyObject * | 683 PyObject * |
681 _PyLong_FromByteArray(const unsigned char* bytes, size_t n, | 684 _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
682 » » int little_endian, int is_signed) | 685 int little_endian, int is_signed) |
683 { | 686 { |
684 » const unsigned char* pstartbyte;/* LSB of bytes */ | 687 const unsigned char* pstartbyte; /* LSB of bytes */ |
685 » int incr;» » » /* direction to move pstartbyte */ | 688 int incr; /* direction to move pstartbyte */ |
686 » const unsigned char* pendbyte;» /* MSB of bytes */ | 689 const unsigned char* pendbyte; /* MSB of bytes */ |
687 » size_t numsignificantbytes;» /* number of bytes that matter */ | 690 size_t numsignificantbytes; /* number of bytes that matter */ |
688 » Py_ssize_t ndigits;» » /* number of Python long digits */ | 691 Py_ssize_t ndigits; /* number of Python long digits */ |
689 » PyLongObject* v;» » /* result */ | 692 PyLongObject* v; /* result */ |
690 » Py_ssize_t idigit = 0;» » /* next free index in v->ob_digit */ | 693 Py_ssize_t idigit = 0; /* next free index in v->ob_digit */ |
691 | 694 |
692 » if (n == 0) | 695 if (n == 0) |
693 » » return PyLong_FromLong(0L); | 696 return PyLong_FromLong(0L); |
694 | 697 |
695 » if (little_endian) { | 698 if (little_endian) { |
696 » » pstartbyte = bytes; | 699 pstartbyte = bytes; |
697 » » pendbyte = bytes + n - 1; | 700 pendbyte = bytes + n - 1; |
698 » » incr = 1; | 701 incr = 1; |
699 » } | 702 } |
700 » else { | 703 else { |
701 » » pstartbyte = bytes + n - 1; | 704 pstartbyte = bytes + n - 1; |
702 » » pendbyte = bytes; | 705 pendbyte = bytes; |
703 » » incr = -1; | 706 incr = -1; |
704 » } | 707 } |
705 | 708 |
706 » if (is_signed) | 709 if (is_signed) |
707 » » is_signed = *pendbyte >= 0x80; | 710 is_signed = *pendbyte >= 0x80; |
708 | 711 |
709 » /* Compute numsignificantbytes. This consists of finding the most | 712 /* Compute numsignificantbytes. This consists of finding the most |
710 » significant byte. Leading 0 bytes are insignficant if the number | 713 significant byte. Leading 0 bytes are insignficant if the number |
711 » is positive, and leading 0xff bytes if negative. */ | 714 is positive, and leading 0xff bytes if negative. */ |
712 » { | 715 { |
713 » » size_t i; | 716 size_t i; |
714 » » const unsigned char* p = pendbyte; | 717 const unsigned char* p = pendbyte; |
715 » » const int pincr = -incr; /* search MSB to LSB */ | 718 const int pincr = -incr; /* search MSB to LSB */ |
716 » » const unsigned char insignficant = is_signed ? 0xff : 0x00; | 719 const unsigned char insignficant = is_signed ? 0xff : 0x00; |
717 | 720 |
718 » » for (i = 0; i < n; ++i, p += pincr) { | 721 for (i = 0; i < n; ++i, p += pincr) { |
719 » » » if (*p != insignficant) | 722 if (*p != insignficant) |
720 » » » » break; | 723 break; |
721 » » } | 724 } |
722 » » numsignificantbytes = n - i; | 725 numsignificantbytes = n - i; |
723 » » /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so | 726 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
724 » » actually has 2 significant bytes. OTOH, 0xff0001 == | 727 actually has 2 significant bytes. OTOH, 0xff0001 == |
725 » » -0x00ffff, so we wouldn't *need* to bump it there; but we | 728 -0x00ffff, so we wouldn't *need* to bump it there; but we |
726 » » do for 0xffff = -0x0001. To be safe without bothering to | 729 do for 0xffff = -0x0001. To be safe without bothering to |
727 » » check every case, bump it regardless. */ | 730 check every case, bump it regardless. */ |
728 » » if (is_signed && numsignificantbytes < n) | 731 if (is_signed && numsignificantbytes < n) |
729 » » » ++numsignificantbytes; | 732 ++numsignificantbytes; |
730 » } | 733 } |
731 | 734 |
732 » /* How many Python long digits do we need? We have | 735 /* How many Python long digits do we need? We have |
733 » 8*numsignificantbytes bits, and each Python long digit has | 736 8*numsignificantbytes bits, and each Python long digit has |
734 » PyLong_SHIFT bits, so it's the ceiling of the quotient. */ | 737 PyLong_SHIFT bits, so it's the ceiling of the quotient. */ |
735 » /* catch overflow before it happens */ | 738 /* catch overflow before it happens */ |
736 » if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { | 739 if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) { |
737 » » PyErr_SetString(PyExc_OverflowError, | 740 PyErr_SetString(PyExc_OverflowError, |
738 » » » » "byte array too long to convert to int"); | 741 "byte array too long to convert to int"); |
739 » » return NULL; | 742 return NULL; |
740 » } | 743 } |
741 » ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; | 744 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT; |
742 » v = _PyLong_New(ndigits); | 745 v = _PyLong_New(ndigits); |
743 » if (v == NULL) | 746 if (v == NULL) |
744 » » return NULL; | 747 return NULL; |
745 | 748 |
746 » /* Copy the bits over. The tricky parts are computing 2's-comp on | 749 /* Copy the bits over. The tricky parts are computing 2's-comp on |
747 » the fly for signed numbers, and dealing with the mismatch between | 750 the fly for signed numbers, and dealing with the mismatch between |
748 » 8-bit bytes and (probably) 15-bit Python digits.*/ | 751 8-bit bytes and (probably) 15-bit Python digits.*/ |
749 » { | 752 { |
750 » » size_t i; | 753 size_t i; |
751 » » twodigits carry = 1;» » /* for 2's-comp calculation */ | 754 twodigits carry = 1; /* for 2's-comp calculation */ |
752 » » twodigits accum = 0;» » /* sliding register */ | 755 twodigits accum = 0; /* sliding register */ |
753 » » unsigned int accumbits = 0; » /* number of bits in accum */ | 756 unsigned int accumbits = 0; /* number of bits in accum */ |
754 » » const unsigned char* p = pstartbyte; | 757 const unsigned char* p = pstartbyte; |
755 | 758 |
756 » » for (i = 0; i < numsignificantbytes; ++i, p += incr) { | 759 for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
757 » » » twodigits thisbyte = *p; | 760 twodigits thisbyte = *p; |
758 » » » /* Compute correction for 2's comp, if needed. */ | 761 /* Compute correction for 2's comp, if needed. */ |
759 » » » if (is_signed) { | 762 if (is_signed) { |
760 » » » » thisbyte = (0xff ^ thisbyte) + carry; | 763 thisbyte = (0xff ^ thisbyte) + carry; |
761 » » » » carry = thisbyte >> 8; | 764 carry = thisbyte >> 8; |
762 » » » » thisbyte &= 0xff; | 765 thisbyte &= 0xff; |
763 » » » } | 766 } |
764 » » » /* Because we're going LSB to MSB, thisbyte is | 767 /* Because we're going LSB to MSB, thisbyte is |
765 » » » more significant than what's already in accum, | 768 more significant than what's already in accum, |
766 » » » so needs to be prepended to accum. */ | 769 so needs to be prepended to accum. */ |
767 » » » accum |= (twodigits)thisbyte << accumbits; | 770 accum |= (twodigits)thisbyte << accumbits; |
768 » » » accumbits += 8; | 771 accumbits += 8; |
769 » » » if (accumbits >= PyLong_SHIFT) { | 772 if (accumbits >= PyLong_SHIFT) { |
770 » » » » /* There's enough to fill a Python digit. */ | 773 /* There's enough to fill a Python digit. */ |
771 » » » » assert(idigit < ndigits); | 774 assert(idigit < ndigits); |
772 » » » » v->ob_digit[idigit] = (digit)(accum & | 775 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK); |
773 » » » » » » » PyLong_MASK); | 776 ++idigit; |
774 » » » » ++idigit; | 777 accum >>= PyLong_SHIFT; |
775 » » » » accum >>= PyLong_SHIFT; | 778 accumbits -= PyLong_SHIFT; |
776 » » » » accumbits -= PyLong_SHIFT; | 779 assert(accumbits < PyLong_SHIFT); |
777 » » » » assert(accumbits < PyLong_SHIFT); | 780 } |
778 » » » } | 781 } |
779 » » } | 782 assert(accumbits < PyLong_SHIFT); |
780 » » assert(accumbits < PyLong_SHIFT); | 783 if (accumbits) { |
781 » » if (accumbits) { | 784 assert(idigit < ndigits); |
782 » » » assert(idigit < ndigits); | 785 v->ob_digit[idigit] = (digit)accum; |
783 » » » v->ob_digit[idigit] = (digit)accum; | 786 ++idigit; |
784 » » » ++idigit; | 787 } |
785 » » } | 788 } |
786 » } | 789 |
787 | 790 Py_SIZE(v) = is_signed ? -idigit : idigit; |
788 » Py_SIZE(v) = is_signed ? -idigit : idigit; | 791 return (PyObject *)long_normalize(v); |
789 » return (PyObject *)long_normalize(v); | |
790 } | 792 } |
791 | 793 |
792 int | 794 int |
793 _PyLong_AsByteArray(PyLongObject* v, | 795 _PyLong_AsByteArray(PyLongObject* v, |
794 » » unsigned char* bytes, size_t n, | 796 unsigned char* bytes, size_t n, |
795 » » int little_endian, int is_signed) | 797 int little_endian, int is_signed) |
796 { | 798 { |
797 » Py_ssize_t i;» » /* index into v->ob_digit */ | 799 Py_ssize_t i; /* index into v->ob_digit */ |
798 » Py_ssize_t ndigits;» » /* |v->ob_size| */ | 800 Py_ssize_t ndigits; /* |v->ob_size| */ |
799 » twodigits accum;» /* sliding register */ | 801 twodigits accum; /* sliding register */ |
800 » unsigned int accumbits; /* # bits in accum */ | 802 unsigned int accumbits; /* # bits in accum */ |
801 » int do_twos_comp;» /* store 2's-comp? is_signed and v < 0 */ | 803 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
802 » digit carry;» » /* for computing 2's-comp */ | 804 digit carry; /* for computing 2's-comp */ |
803 » size_t j;» » /* # bytes filled */ | 805 size_t j; /* # bytes filled */ |
804 » unsigned char* p;» /* pointer to next byte in bytes */ | 806 unsigned char* p; /* pointer to next byte in bytes */ |
805 » int pincr;» » /* direction to move p */ | 807 int pincr; /* direction to move p */ |
806 | 808 |
807 » assert(v != NULL && PyLong_Check(v)); | 809 assert(v != NULL && PyLong_Check(v)); |
808 | 810 |
809 » if (Py_SIZE(v) < 0) { | 811 if (Py_SIZE(v) < 0) { |
810 » » ndigits = -(Py_SIZE(v)); | 812 ndigits = -(Py_SIZE(v)); |
811 » » if (!is_signed) { | 813 if (!is_signed) { |
812 » » » PyErr_SetString(PyExc_OverflowError, | 814 PyErr_SetString(PyExc_OverflowError, |
813 » » » » "can't convert negative int to unsigned"); | 815 "can't convert negative int to unsigned"); |
814 » » » return -1; | 816 return -1; |
815 » » } | 817 } |
816 » » do_twos_comp = 1; | 818 do_twos_comp = 1; |
817 » } | 819 } |
818 » else { | 820 else { |
819 » » ndigits = Py_SIZE(v); | 821 ndigits = Py_SIZE(v); |
820 » » do_twos_comp = 0; | 822 do_twos_comp = 0; |
821 » } | 823 } |
822 | 824 |
823 » if (little_endian) { | 825 if (little_endian) { |
824 » » p = bytes; | 826 p = bytes; |
825 » » pincr = 1; | 827 pincr = 1; |
826 » } | 828 } |
827 » else { | 829 else { |
828 » » p = bytes + n - 1; | 830 p = bytes + n - 1; |
829 » » pincr = -1; | 831 pincr = -1; |
830 » } | 832 } |
831 | 833 |
832 » /* Copy over all the Python digits. | 834 /* Copy over all the Python digits. |
833 » It's crucial that every Python digit except for the MSD contribute | 835 It's crucial that every Python digit except for the MSD contribute |
834 » exactly PyLong_SHIFT bits to the total, so first assert that the long
is | 836 exactly PyLong_SHIFT bits to the total, so first assert that the long is |
835 » normalized. */ | 837 normalized. */ |
836 » assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); | 838 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
837 » j = 0; | 839 j = 0; |
838 » accum = 0; | 840 accum = 0; |
839 » accumbits = 0; | 841 accumbits = 0; |
840 » carry = do_twos_comp ? 1 : 0; | 842 carry = do_twos_comp ? 1 : 0; |
841 » for (i = 0; i < ndigits; ++i) { | 843 for (i = 0; i < ndigits; ++i) { |
842 » » digit thisdigit = v->ob_digit[i]; | 844 digit thisdigit = v->ob_digit[i]; |
843 » » if (do_twos_comp) { | 845 if (do_twos_comp) { |
844 » » » thisdigit = (thisdigit ^ PyLong_MASK) + carry; | 846 thisdigit = (thisdigit ^ PyLong_MASK) + carry; |
845 » » » carry = thisdigit >> PyLong_SHIFT; | 847 carry = thisdigit >> PyLong_SHIFT; |
846 » » » thisdigit &= PyLong_MASK; | 848 thisdigit &= PyLong_MASK; |
847 » » } | 849 } |
848 » » /* Because we're going LSB to MSB, thisdigit is more | 850 /* Because we're going LSB to MSB, thisdigit is more |
849 » » significant than what's already in accum, so needs to be | 851 significant than what's already in accum, so needs to be |
850 » » prepended to accum. */ | 852 prepended to accum. */ |
851 » » accum |= (twodigits)thisdigit << accumbits; | 853 accum |= (twodigits)thisdigit << accumbits; |
852 | 854 |
853 » » /* The most-significant digit may be (probably is) at least | 855 /* The most-significant digit may be (probably is) at least |
854 » » partly empty. */ | 856 partly empty. */ |
855 » » if (i == ndigits - 1) { | 857 if (i == ndigits - 1) { |
856 » » » /* Count # of sign bits -- they needn't be stored, | 858 /* Count # of sign bits -- they needn't be stored, |
857 » » » * although for signed conversion we need later to | 859 * although for signed conversion we need later to |
858 » » » * make sure at least one sign bit gets stored. */ | 860 * make sure at least one sign bit gets stored. */ |
859 » » » digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : | 861 digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit; |
860 » » » » thisdigit; | 862 while (s != 0) { |
861 » » » while (s != 0) { | 863 s >>= 1; |
862 » » » » s >>= 1; | 864 accumbits++; |
863 » » » » accumbits++; | 865 } |
864 » » » } | 866 } |
865 » » } | 867 else |
866 » » else | 868 accumbits += PyLong_SHIFT; |
867 » » » accumbits += PyLong_SHIFT; | 869 |
868 | 870 /* Store as many bytes as possible. */ |
869 » » /* Store as many bytes as possible. */ | 871 while (accumbits >= 8) { |
870 » » while (accumbits >= 8) { | 872 if (j >= n) |
871 » » » if (j >= n) | 873 goto Overflow; |
872 » » » » goto Overflow; | 874 ++j; |
873 » » » ++j; | 875 *p = (unsigned char)(accum & 0xff); |
874 » » » *p = (unsigned char)(accum & 0xff); | 876 p += pincr; |
875 » » » p += pincr; | 877 accumbits -= 8; |
876 » » » accumbits -= 8; | 878 accum >>= 8; |
877 » » » accum >>= 8; | 879 } |
878 » » } | 880 } |
879 » } | 881 |
880 | 882 /* Store the straggler (if any). */ |
881 » /* Store the straggler (if any). */ | 883 assert(accumbits < 8); |
882 » assert(accumbits < 8); | 884 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
883 » assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ | 885 if (accumbits > 0) { |
884 » if (accumbits > 0) { | 886 if (j >= n) |
885 » » if (j >= n) | 887 goto Overflow; |
886 » » » goto Overflow; | 888 ++j; |
887 » » ++j; | 889 if (do_twos_comp) { |
888 » » if (do_twos_comp) { | 890 /* Fill leading bits of the byte with sign bits |
889 » » » /* Fill leading bits of the byte with sign bits | 891 (appropriately pretending that the long had an |
890 » » » (appropriately pretending that the long had an | 892 infinite supply of sign bits). */ |
891 » » » infinite supply of sign bits). */ | 893 accum |= (~(twodigits)0) << accumbits; |
892 » » » accum |= (~(twodigits)0) << accumbits; | 894 } |
893 » » } | 895 *p = (unsigned char)(accum & 0xff); |
894 » » *p = (unsigned char)(accum & 0xff); | 896 p += pincr; |
895 » » p += pincr; | 897 } |
896 » } | 898 else if (j == n && n > 0 && is_signed) { |
897 » else if (j == n && n > 0 && is_signed) { | 899 /* The main loop filled the byte array exactly, so the code |
898 » » /* The main loop filled the byte array exactly, so the code | 900 just above didn't get to ensure there's a sign bit, and the |
899 » » just above didn't get to ensure there's a sign bit, and the | 901 loop below wouldn't add one either. Make sure a sign bit |
900 » » loop below wouldn't add one either. Make sure a sign bit | 902 exists. */ |
901 » » exists. */ | 903 unsigned char msb = *(p - pincr); |
902 » » unsigned char msb = *(p - pincr); | 904 int sign_bit_set = msb >= 0x80; |
903 » » int sign_bit_set = msb >= 0x80; | 905 assert(accumbits == 0); |
904 » » assert(accumbits == 0); | 906 if (sign_bit_set == do_twos_comp) |
905 » » if (sign_bit_set == do_twos_comp) | 907 return 0; |
906 » » » return 0; | 908 else |
907 » » else | 909 goto Overflow; |
908 » » » goto Overflow; | 910 } |
909 » } | 911 |
910 | 912 /* Fill remaining bytes with copies of the sign bit. */ |
911 » /* Fill remaining bytes with copies of the sign bit. */ | 913 { |
912 » { | 914 unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
913 » » unsigned char signbyte = do_twos_comp ? 0xffU : 0U; | 915 for ( ; j < n; ++j, p += pincr) |
914 » » for ( ; j < n; ++j, p += pincr) | 916 *p = signbyte; |
915 » » » *p = signbyte; | 917 } |
916 » } | 918 |
917 | 919 return 0; |
918 » return 0; | 920 |
919 | 921 Overflow: |
920 Overflow: | 922 PyErr_SetString(PyExc_OverflowError, "int too big to convert"); |
921 » PyErr_SetString(PyExc_OverflowError, "int too big to convert"); | 923 return -1; |
922 » return -1; | |
923 | 924 |
924 } | 925 } |
925 | 926 |
926 /* Create a new long (or int) object from a C pointer */ | 927 /* Create a new long (or int) object from a C pointer */ |
927 | 928 |
928 PyObject * | 929 PyObject * |
929 PyLong_FromVoidPtr(void *p) | 930 PyLong_FromVoidPtr(void *p) |
930 { | 931 { |
931 #ifndef HAVE_LONG_LONG | 932 #ifndef HAVE_LONG_LONG |
932 # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" | 933 # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
933 #endif | 934 #endif |
934 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P | 935 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
935 # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" | 936 # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
936 #endif | 937 #endif |
937 » /* special-case null pointer */ | 938 /* special-case null pointer */ |
938 » if (!p) | 939 if (!p) |
939 » » return PyLong_FromLong(0); | 940 return PyLong_FromLong(0); |
940 » return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)
p); | 941 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p); |
941 | 942 |
942 } | 943 } |
943 | 944 |
944 /* Get a C pointer from a long object (or an int object in some cases) */ | 945 /* Get a C pointer from a long object (or an int object in some cases) */ |
945 | 946 |
946 void * | 947 void * |
947 PyLong_AsVoidPtr(PyObject *vv) | 948 PyLong_AsVoidPtr(PyObject *vv) |
948 { | 949 { |
949 » /* This function will allow int or long objects. If vv is neither, | 950 /* This function will allow int or long objects. If vv is neither, |
950 » then the PyLong_AsLong*() functions will raise the exception: | 951 then the PyLong_AsLong*() functions will raise the exception: |
951 » PyExc_SystemError, "bad argument to internal function" | 952 PyExc_SystemError, "bad argument to internal function" |
952 » */ | 953 */ |
953 #if SIZEOF_VOID_P <= SIZEOF_LONG | 954 #if SIZEOF_VOID_P <= SIZEOF_LONG |
954 » long x; | 955 long x; |
955 | 956 |
956 » if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) | 957 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
957 » » x = PyLong_AsLong(vv); | 958 x = PyLong_AsLong(vv); |
958 » else | 959 else |
959 » » x = PyLong_AsUnsignedLong(vv); | 960 x = PyLong_AsUnsignedLong(vv); |
960 #else | 961 #else |
961 | 962 |
962 #ifndef HAVE_LONG_LONG | 963 #ifndef HAVE_LONG_LONG |
963 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" | 964 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
964 #endif | 965 #endif |
965 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P | 966 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
966 # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" | 967 # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" |
967 #endif | 968 #endif |
968 » PY_LONG_LONG x; | 969 PY_LONG_LONG x; |
969 | 970 |
970 » if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) | 971 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0) |
971 » » x = PyLong_AsLongLong(vv); | 972 x = PyLong_AsLongLong(vv); |
972 » else | 973 else |
973 » » x = PyLong_AsUnsignedLongLong(vv); | 974 x = PyLong_AsUnsignedLongLong(vv); |
974 | 975 |
975 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ | 976 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
976 | 977 |
977 » if (x == -1 && PyErr_Occurred()) | 978 if (x == -1 && PyErr_Occurred()) |
978 » » return NULL; | 979 return NULL; |
979 » return (void *)x; | 980 return (void *)x; |
980 } | 981 } |
981 | 982 |
982 #ifdef HAVE_LONG_LONG | 983 #ifdef HAVE_LONG_LONG |
983 | 984 |
984 /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later | 985 /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later |
985 * rewritten to use the newer PyLong_{As,From}ByteArray API. | 986 * rewritten to use the newer PyLong_{As,From}ByteArray API. |
986 */ | 987 */ |
987 | 988 |
988 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one | 989 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one |
989 #define PY_ABS_LLONG_MIN» (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN) | 990 #define PY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN) |
990 | 991 |
991 /* Create a new long int object from a C PY_LONG_LONG int. */ | 992 /* Create a new long int object from a C PY_LONG_LONG int. */ |
992 | 993 |
993 PyObject * | 994 PyObject * |
994 PyLong_FromLongLong(PY_LONG_LONG ival) | 995 PyLong_FromLongLong(PY_LONG_LONG ival) |
995 { | 996 { |
996 » PyLongObject *v; | 997 PyLongObject *v; |
997 » unsigned PY_LONG_LONG abs_ival; | 998 unsigned PY_LONG_LONG abs_ival; |
998 » unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit *
/ | 999 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */ |
999 » int ndigits = 0; | 1000 int ndigits = 0; |
1000 » int negative = 0; | 1001 int negative = 0; |
1001 | 1002 |
1002 » CHECK_SMALL_INT(ival); | 1003 CHECK_SMALL_INT(ival); |
1003 » if (ival < 0) { | 1004 if (ival < 0) { |
1004 » » /* avoid signed overflow on negation; see comments | 1005 /* avoid signed overflow on negation; see comments |
1005 » » in PyLong_FromLong above. */ | 1006 in PyLong_FromLong above. */ |
1006 » » abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1; | 1007 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1; |
1007 » » negative = 1; | 1008 negative = 1; |
1008 » } | 1009 } |
1009 » else { | 1010 else { |
1010 » » abs_ival = (unsigned PY_LONG_LONG)ival; | 1011 abs_ival = (unsigned PY_LONG_LONG)ival; |
1011 » } | 1012 } |
1012 | 1013 |
1013 » /* Count the number of Python digits. | 1014 /* Count the number of Python digits. |
1014 » We used to pick 5 ("big enough for anything"), but that's a | 1015 We used to pick 5 ("big enough for anything"), but that's a |
1015 » waste of time and space given that 5*15 = 75 bits are rarely | 1016 waste of time and space given that 5*15 = 75 bits are rarely |
1016 » needed. */ | 1017 needed. */ |
1017 » t = abs_ival; | 1018 t = abs_ival; |
1018 » while (t) { | 1019 while (t) { |
1019 » » ++ndigits; | 1020 ++ndigits; |
1020 » » t >>= PyLong_SHIFT; | 1021 t >>= PyLong_SHIFT; |
1021 » } | 1022 } |
1022 » v = _PyLong_New(ndigits); | 1023 v = _PyLong_New(ndigits); |
1023 » if (v != NULL) { | 1024 if (v != NULL) { |
1024 » » digit *p = v->ob_digit; | 1025 digit *p = v->ob_digit; |
1025 » » Py_SIZE(v) = negative ? -ndigits : ndigits; | 1026 Py_SIZE(v) = negative ? -ndigits : ndigits; |
1026 » » t = abs_ival; | 1027 t = abs_ival; |
1027 » » while (t) { | 1028 while (t) { |
1028 » » » *p++ = (digit)(t & PyLong_MASK); | 1029 *p++ = (digit)(t & PyLong_MASK); |
1029 » » » t >>= PyLong_SHIFT; | 1030 t >>= PyLong_SHIFT; |
1030 » » } | 1031 } |
1031 » } | 1032 } |
1032 » return (PyObject *)v; | 1033 return (PyObject *)v; |
1033 } | 1034 } |
1034 | 1035 |
1035 /* Create a new long int object from a C unsigned PY_LONG_LONG int. */ | 1036 /* Create a new long int object from a C unsigned PY_LONG_LONG int. */ |
1036 | 1037 |
1037 PyObject * | 1038 PyObject * |
1038 PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) | 1039 PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival) |
1039 { | 1040 { |
1040 » PyLongObject *v; | 1041 PyLongObject *v; |
1041 » unsigned PY_LONG_LONG t; | 1042 unsigned PY_LONG_LONG t; |
1042 » int ndigits = 0; | 1043 int ndigits = 0; |
1043 | 1044 |
1044 » if (ival < PyLong_BASE) | 1045 if (ival < PyLong_BASE) |
1045 » » return PyLong_FromLong((long)ival); | 1046 return PyLong_FromLong((long)ival); |
1046 » /* Count the number of Python digits. */ | 1047 /* Count the number of Python digits. */ |
1047 » t = (unsigned PY_LONG_LONG)ival; | 1048 t = (unsigned PY_LONG_LONG)ival; |
1048 » while (t) { | 1049 while (t) { |
1049 » » ++ndigits; | 1050 ++ndigits; |
1050 » » t >>= PyLong_SHIFT; | 1051 t >>= PyLong_SHIFT; |
1051 » } | 1052 } |
1052 » v = _PyLong_New(ndigits); | 1053 v = _PyLong_New(ndigits); |
1053 » if (v != NULL) { | 1054 if (v != NULL) { |
1054 » » digit *p = v->ob_digit; | 1055 digit *p = v->ob_digit; |
1055 » » Py_SIZE(v) = ndigits; | 1056 Py_SIZE(v) = ndigits; |
1056 » » while (ival) { | 1057 while (ival) { |
1057 » » » *p++ = (digit)(ival & PyLong_MASK); | 1058 *p++ = (digit)(ival & PyLong_MASK); |
1058 » » » ival >>= PyLong_SHIFT; | 1059 ival >>= PyLong_SHIFT; |
1059 » » } | 1060 } |
1060 » } | 1061 } |
1061 » return (PyObject *)v; | 1062 return (PyObject *)v; |
1062 } | 1063 } |
1063 | 1064 |
1064 /* Create a new long int object from a C Py_ssize_t. */ | 1065 /* Create a new long int object from a C Py_ssize_t. */ |
1065 | 1066 |
1066 PyObject * | 1067 PyObject * |
1067 PyLong_FromSsize_t(Py_ssize_t ival) | 1068 PyLong_FromSsize_t(Py_ssize_t ival) |
1068 { | 1069 { |
1069 » PyLongObject *v; | 1070 PyLongObject *v; |
1070 » size_t abs_ival; | 1071 size_t abs_ival; |
1071 » size_t t; /* unsigned so >> doesn't propagate sign bit */ | 1072 size_t t; /* unsigned so >> doesn't propagate sign bit */ |
1072 » int ndigits = 0; | 1073 int ndigits = 0; |
1073 » int negative = 0; | 1074 int negative = 0; |
1074 | 1075 |
1075 » CHECK_SMALL_INT(ival); | 1076 CHECK_SMALL_INT(ival); |
1076 » if (ival < 0) { | 1077 if (ival < 0) { |
1077 » » /* avoid signed overflow when ival = SIZE_T_MIN */ | 1078 /* avoid signed overflow when ival = SIZE_T_MIN */ |
1078 » » abs_ival = (size_t)(-1-ival)+1; | 1079 abs_ival = (size_t)(-1-ival)+1; |
1079 » » negative = 1; | 1080 negative = 1; |
1080 » } | 1081 } |
1081 » else { | 1082 else { |
1082 » » abs_ival = (size_t)ival; | 1083 abs_ival = (size_t)ival; |
1083 » } | 1084 } |
1084 | 1085 |
1085 » /* Count the number of Python digits. */ | 1086 /* Count the number of Python digits. */ |
1086 » t = abs_ival; | 1087 t = abs_ival; |
1087 » while (t) { | 1088 while (t) { |
1088 » » ++ndigits; | 1089 ++ndigits; |
1089 » » t >>= PyLong_SHIFT; | 1090 t >>= PyLong_SHIFT; |
1090 » } | 1091 } |
1091 » v = _PyLong_New(ndigits); | 1092 v = _PyLong_New(ndigits); |
1092 » if (v != NULL) { | 1093 if (v != NULL) { |
1093 » » digit *p = v->ob_digit; | 1094 digit *p = v->ob_digit; |
1094 » » Py_SIZE(v) = negative ? -ndigits : ndigits; | 1095 Py_SIZE(v) = negative ? -ndigits : ndigits; |
1095 » » t = abs_ival; | 1096 t = abs_ival; |
1096 » » while (t) { | 1097 while (t) { |
1097 » » » *p++ = (digit)(t & PyLong_MASK); | 1098 *p++ = (digit)(t & PyLong_MASK); |
1098 » » » t >>= PyLong_SHIFT; | 1099 t >>= PyLong_SHIFT; |
1099 » » } | 1100 } |
1100 » } | 1101 } |
1101 » return (PyObject *)v; | 1102 return (PyObject *)v; |
1102 } | 1103 } |
1103 | 1104 |
1104 /* Create a new long int object from a C size_t. */ | 1105 /* Create a new long int object from a C size_t. */ |
1105 | 1106 |
1106 PyObject * | 1107 PyObject * |
1107 PyLong_FromSize_t(size_t ival) | 1108 PyLong_FromSize_t(size_t ival) |
1108 { | 1109 { |
1109 » PyLongObject *v; | 1110 PyLongObject *v; |
1110 » size_t t; | 1111 size_t t; |
1111 » int ndigits = 0; | 1112 int ndigits = 0; |
1112 | 1113 |
1113 » if (ival < PyLong_BASE) | 1114 if (ival < PyLong_BASE) |
1114 » » return PyLong_FromLong(ival); | 1115 return PyLong_FromLong((long)ival); |
1115 » /* Count the number of Python digits. */ | 1116 /* Count the number of Python digits. */ |
1116 » t = ival; | 1117 t = ival; |
1117 » while (t) { | 1118 while (t) { |
1118 » » ++ndigits; | 1119 ++ndigits; |
1119 » » t >>= PyLong_SHIFT; | 1120 t >>= PyLong_SHIFT; |
1120 » } | 1121 } |
1121 » v = _PyLong_New(ndigits); | 1122 v = _PyLong_New(ndigits); |
1122 » if (v != NULL) { | 1123 if (v != NULL) { |
1123 » » digit *p = v->ob_digit; | 1124 digit *p = v->ob_digit; |
1124 » » Py_SIZE(v) = ndigits; | 1125 Py_SIZE(v) = ndigits; |
1125 » » while (ival) { | 1126 while (ival) { |
1126 » » » *p++ = (digit)(ival & PyLong_MASK); | 1127 *p++ = (digit)(ival & PyLong_MASK); |
1127 » » » ival >>= PyLong_SHIFT; | 1128 ival >>= PyLong_SHIFT; |
1128 » » } | 1129 } |
1129 » } | 1130 } |
1130 » return (PyObject *)v; | 1131 return (PyObject *)v; |
1131 } | 1132 } |
1132 | 1133 |
1133 /* Get a C PY_LONG_LONG int from a long int object. | 1134 /* Get a C PY_LONG_LONG int from a long int object. |
1134 Return -1 and set an error if overflow occurs. */ | 1135 Return -1 and set an error if overflow occurs. */ |
1135 | 1136 |
1136 PY_LONG_LONG | 1137 PY_LONG_LONG |
1137 PyLong_AsLongLong(PyObject *vv) | 1138 PyLong_AsLongLong(PyObject *vv) |
1138 { | 1139 { |
1139 » PyLongObject *v; | 1140 PyLongObject *v; |
1140 » PY_LONG_LONG bytes; | 1141 PY_LONG_LONG bytes; |
1141 » int one = 1; | 1142 int one = 1; |
1142 » int res; | 1143 int res; |
1143 | 1144 |
1144 » if (vv == NULL) { | 1145 if (vv == NULL) { |
1145 » » PyErr_BadInternalCall(); | 1146 PyErr_BadInternalCall(); |
1146 » » return -1; | 1147 return -1; |
1147 » } | 1148 } |
1148 » if (!PyLong_Check(vv)) { | 1149 if (!PyLong_Check(vv)) { |
1149 » » PyNumberMethods *nb; | 1150 PyNumberMethods *nb; |
1150 » » PyObject *io; | 1151 PyObject *io; |
1151 » » if ((nb = vv->ob_type->tp_as_number) == NULL || | 1152 if ((nb = vv->ob_type->tp_as_number) == NULL || |
1152 » » nb->nb_int == NULL) { | 1153 nb->nb_int == NULL) { |
1153 » » » PyErr_SetString(PyExc_TypeError, "an integer is required
"); | 1154 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
1154 » » » return -1; | 1155 return -1; |
1155 » » } | 1156 } |
1156 » » io = (*nb->nb_int) (vv); | 1157 io = (*nb->nb_int) (vv); |
1157 » » if (io == NULL) | 1158 if (io == NULL) |
1158 » » » return -1; | 1159 return -1; |
1159 » » if (PyLong_Check(io)) { | 1160 if (PyLong_Check(io)) { |
1160 » » » bytes = PyLong_AsLongLong(io); | 1161 bytes = PyLong_AsLongLong(io); |
1161 » » » Py_DECREF(io); | 1162 Py_DECREF(io); |
1162 » » » return bytes; | 1163 return bytes; |
1163 » » } | 1164 } |
1164 » » Py_DECREF(io); | 1165 Py_DECREF(io); |
1165 » » PyErr_SetString(PyExc_TypeError, "integer conversion failed"); | 1166 PyErr_SetString(PyExc_TypeError, "integer conversion failed"); |
1166 » » return -1; | 1167 return -1; |
1167 » } | 1168 } |
1168 | 1169 |
1169 » v = (PyLongObject*)vv; | 1170 v = (PyLongObject*)vv; |
1170 » switch(Py_SIZE(v)) { | 1171 switch(Py_SIZE(v)) { |
1171 » case -1: return -(sdigit)v->ob_digit[0]; | 1172 case -1: return -(sdigit)v->ob_digit[0]; |
1172 » case 0: return 0; | 1173 case 0: return 0; |
1173 » case 1: return v->ob_digit[0]; | 1174 case 1: return v->ob_digit[0]; |
1174 » } | 1175 } |
1175 » res = _PyLong_AsByteArray( | 1176 res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
1176 » » » (PyLongObject *)vv, (unsigned char *)&bytes, | 1177 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); |
1177 » » » SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); | 1178 |
1178 | 1179 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
1179 » /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ | 1180 if (res < 0) |
1180 » if (res < 0) | 1181 return (PY_LONG_LONG)-1; |
1181 » » return (PY_LONG_LONG)-1; | 1182 else |
1182 » else | 1183 return bytes; |
1183 » » return bytes; | |
1184 } | 1184 } |
1185 | 1185 |
1186 /* Get a C unsigned PY_LONG_LONG int from a long int object. | 1186 /* Get a C unsigned PY_LONG_LONG int from a long int object. |
1187 Return -1 and set an error if overflow occurs. */ | 1187 Return -1 and set an error if overflow occurs. */ |
1188 | 1188 |
1189 unsigned PY_LONG_LONG | 1189 unsigned PY_LONG_LONG |
1190 PyLong_AsUnsignedLongLong(PyObject *vv) | 1190 PyLong_AsUnsignedLongLong(PyObject *vv) |
1191 { | 1191 { |
1192 » PyLongObject *v; | 1192 PyLongObject *v; |
1193 » unsigned PY_LONG_LONG bytes; | 1193 unsigned PY_LONG_LONG bytes; |
1194 » int one = 1; | 1194 int one = 1; |
1195 » int res; | 1195 int res; |
1196 | 1196 |
1197 » if (vv == NULL || !PyLong_Check(vv)) { | 1197 if (vv == NULL || !PyLong_Check(vv)) { |
1198 » » PyErr_BadInternalCall(); | 1198 PyErr_BadInternalCall(); |
1199 » » return (unsigned PY_LONG_LONG)-1; | 1199 return (unsigned PY_LONG_LONG)-1; |
1200 » } | 1200 } |
1201 | 1201 |
1202 » v = (PyLongObject*)vv; | 1202 v = (PyLongObject*)vv; |
1203 » switch(Py_SIZE(v)) { | 1203 switch(Py_SIZE(v)) { |
1204 » case 0: return 0; | 1204 case 0: return 0; |
1205 » case 1: return v->ob_digit[0]; | 1205 case 1: return v->ob_digit[0]; |
1206 » } | 1206 } |
1207 | 1207 |
1208 » res = _PyLong_AsByteArray( | 1208 res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes, |
1209 » » » (PyLongObject *)vv, (unsigned char *)&bytes, | 1209 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); |
1210 » » » SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); | 1210 |
1211 | 1211 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ |
1212 » /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */ | 1212 if (res < 0) |
1213 » if (res < 0) | 1213 return (unsigned PY_LONG_LONG)res; |
1214 » » return (unsigned PY_LONG_LONG)res; | 1214 else |
1215 » else | 1215 return bytes; |
1216 » » return bytes; | |
1217 } | 1216 } |
1218 | 1217 |
1219 /* Get a C unsigned long int from a long int object, ignoring the high bits. | 1218 /* Get a C unsigned long int from a long int object, ignoring the high bits. |
1220 Returns -1 and sets an error condition if an error occurs. */ | 1219 Returns -1 and sets an error condition if an error occurs. */ |
1221 | 1220 |
1222 static unsigned PY_LONG_LONG | 1221 static unsigned PY_LONG_LONG |
1223 _PyLong_AsUnsignedLongLongMask(PyObject *vv) | 1222 _PyLong_AsUnsignedLongLongMask(PyObject *vv) |
1224 { | 1223 { |
1225 » register PyLongObject *v; | 1224 register PyLongObject *v; |
1226 » unsigned PY_LONG_LONG x; | 1225 unsigned PY_LONG_LONG x; |
1227 » Py_ssize_t i; | 1226 Py_ssize_t i; |
1228 » int sign; | 1227 int sign; |
1229 | 1228 |
1230 » if (vv == NULL || !PyLong_Check(vv)) { | 1229 if (vv == NULL || !PyLong_Check(vv)) { |
1231 » » PyErr_BadInternalCall(); | 1230 PyErr_BadInternalCall(); |
1232 » » return (unsigned long) -1; | 1231 return (unsigned long) -1; |
1233 » } | 1232 } |
1234 » v = (PyLongObject *)vv; | 1233 v = (PyLongObject *)vv; |
1235 » switch(Py_SIZE(v)) { | 1234 switch(Py_SIZE(v)) { |
1236 » case 0: return 0; | 1235 case 0: return 0; |
1237 » case 1: return v->ob_digit[0]; | 1236 case 1: return v->ob_digit[0]; |
1238 » } | 1237 } |
1239 » i = Py_SIZE(v); | 1238 i = Py_SIZE(v); |
1240 » sign = 1; | 1239 sign = 1; |
1241 » x = 0; | 1240 x = 0; |
1242 » if (i < 0) { | 1241 if (i < 0) { |
1243 » » sign = -1; | 1242 sign = -1; |
1244 » » i = -i; | 1243 i = -i; |
1245 » } | 1244 } |
1246 » while (--i >= 0) { | 1245 while (--i >= 0) { |
1247 » » x = (x << PyLong_SHIFT) | v->ob_digit[i]; | 1246 x = (x << PyLong_SHIFT) | v->ob_digit[i]; |
1248 » } | 1247 } |
1249 » return x * sign; | 1248 return x * sign; |
1250 } | 1249 } |
1251 | 1250 |
1252 unsigned PY_LONG_LONG | 1251 unsigned PY_LONG_LONG |
1253 PyLong_AsUnsignedLongLongMask(register PyObject *op) | 1252 PyLong_AsUnsignedLongLongMask(register PyObject *op) |
1254 { | 1253 { |
1255 » PyNumberMethods *nb; | 1254 PyNumberMethods *nb; |
1256 » PyLongObject *lo; | 1255 PyLongObject *lo; |
1257 » unsigned PY_LONG_LONG val; | 1256 unsigned PY_LONG_LONG val; |
1258 | 1257 |
1259 » if (op && PyLong_Check(op)) | 1258 if (op && PyLong_Check(op)) |
1260 » » return _PyLong_AsUnsignedLongLongMask(op); | 1259 return _PyLong_AsUnsignedLongLongMask(op); |
1261 | 1260 |
1262 » if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || | 1261 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
1263 » nb->nb_int == NULL) { | 1262 nb->nb_int == NULL) { |
1264 » » PyErr_SetString(PyExc_TypeError, "an integer is required"); | 1263 PyErr_SetString(PyExc_TypeError, "an integer is required"); |
1265 » » return (unsigned PY_LONG_LONG)-1; | 1264 return (unsigned PY_LONG_LONG)-1; |
1266 » } | 1265 } |
1267 | 1266 |
1268 » lo = (PyLongObject*) (*nb->nb_int) (op); | 1267 lo = (PyLongObject*) (*nb->nb_int) (op); |
1269 » if (lo == NULL) | 1268 if (lo == NULL) |
1270 » » return (unsigned PY_LONG_LONG)-1; | 1269 return (unsigned PY_LONG_LONG)-1; |
1271 » if (PyLong_Check(lo)) { | 1270 if (PyLong_Check(lo)) { |
1272 » » val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); | 1271 val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo); |
1273 » » Py_DECREF(lo); | 1272 Py_DECREF(lo); |
1274 » » if (PyErr_Occurred()) | 1273 if (PyErr_Occurred()) |
1275 » » » return (unsigned PY_LONG_LONG)-1; | 1274 return (unsigned PY_LONG_LONG)-1; |
1276 » » return val; | 1275 return val; |
1277 » } | 1276 } |
1278 » else | 1277 else |
1279 » { | 1278 { |
1280 » » Py_DECREF(lo); | 1279 Py_DECREF(lo); |
1281 » » PyErr_SetString(PyExc_TypeError, | 1280 PyErr_SetString(PyExc_TypeError, |
1282 » » » » "nb_int should return int object"); | 1281 "nb_int should return int object"); |
1283 » » return (unsigned PY_LONG_LONG)-1; | 1282 return (unsigned PY_LONG_LONG)-1; |
1284 » } | 1283 } |
1285 } | 1284 } |
1286 #undef IS_LITTLE_ENDIAN | 1285 #undef IS_LITTLE_ENDIAN |
1287 | 1286 |
1288 /* Get a C long long int from a Python long or Python int object. | 1287 /* Get a C long long int from a Python long or Python int object. |
1289 On overflow, returns -1 and sets *overflow to 1 or -1 depending | 1288 On overflow, returns -1 and sets *overflow to 1 or -1 depending |
1290 on the sign of the result. Otherwise *overflow is 0. | 1289 on the sign of the result. Otherwise *overflow is 0. |
1291 | 1290 |
1292 For other errors (e.g., type error), returns -1 and sets an error | 1291 For other errors (e.g., type error), returns -1 and sets an error |
1293 condition. | 1292 condition. |
1294 */ | 1293 */ |
1295 | 1294 |
1296 PY_LONG_LONG | 1295 PY_LONG_LONG |
1297 PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) | 1296 PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow) |
1298 { | 1297 { |
1299 » /* This version by Tim Peters */ | 1298 /* This version by Tim Peters */ |
1300 » register PyLongObject *v; | 1299 register PyLongObject *v; |
1301 » unsigned PY_LONG_LONG x, prev; | 1300 unsigned PY_LONG_LONG x, prev; |
1302 » PY_LONG_LONG res; | 1301 PY_LONG_LONG res; |
1303 » Py_ssize_t i; | 1302 Py_ssize_t i; |
1304 » int sign; | 1303 int sign; |
1305 » int do_decref = 0; /* if nb_int was called */ | 1304 int do_decref = 0; /* if nb_int was called */ |
1306 | 1305 |
1307 » *overflow = 0; | 1306 *overflow = 0; |
1308 » if (vv == NULL) { | 1307 if (vv == NULL) { |
1309 » » PyErr_BadInternalCall(); | 1308 PyErr_BadInternalCall(); |
1310 » » return -1; | 1309 return -1; |
1311 » } | 1310 } |
1312 | 1311 |
1313 » if (!PyLong_Check(vv)) { | 1312 if (!PyLong_Check(vv)) { |
1314 » » PyNumberMethods *nb; | 1313 PyNumberMethods *nb; |
1315 » » nb = vv->ob_type->tp_as_number; | 1314 nb = vv->ob_type->tp_as_number; |
1316 » » if (nb == NULL || nb->nb_int == NULL) { | 1315 if (nb == NULL || nb->nb_int == NULL) { |
1317 » » » PyErr_SetString(PyExc_TypeError, | 1316 PyErr_SetString(PyExc_TypeError, |
1318 » » » » » "an integer is required"); | 1317 "an integer is required"); |
1319 » » » return -1; | 1318 return -1; |
1320 » » } | 1319 } |
1321 » » vv = (*nb->nb_int) (vv); | 1320 vv = (*nb->nb_int) (vv); |
1322 » » if (vv == NULL) | 1321 if (vv == NULL) |
1323 » » » return -1; | 1322 return -1; |
1324 » » do_decref = 1; | 1323 do_decref = 1; |
1325 » » if (!PyLong_Check(vv)) { | 1324 if (!PyLong_Check(vv)) { |
1326 » » » Py_DECREF(vv); | 1325 Py_DECREF(vv); |
1327 » » » PyErr_SetString(PyExc_TypeError, | 1326 PyErr_SetString(PyExc_TypeError, |
1328 » » » » » "nb_int should return int object"); | 1327 "nb_int should return int object"); |
1329 » » » return -1; | 1328 return -1; |
1330 » » } | 1329 } |
1331 » } | 1330 } |
1332 | 1331 |
1333 » res = -1; | 1332 res = -1; |
1334 » v = (PyLongObject *)vv; | 1333 v = (PyLongObject *)vv; |
1335 » i = Py_SIZE(v); | 1334 i = Py_SIZE(v); |
1336 | 1335 |
1337 » switch (i) { | 1336 switch (i) { |
1338 » case -1: | 1337 case -1: |
1339 » » res = -(sdigit)v->ob_digit[0]; | 1338 res = -(sdigit)v->ob_digit[0]; |
1340 » » break; | 1339 break; |
1341 » case 0: | 1340 case 0: |
1342 » » res = 0; | 1341 res = 0; |
1343 » » break; | 1342 break; |
1344 » case 1: | 1343 case 1: |
1345 » » res = v->ob_digit[0]; | 1344 res = v->ob_digit[0]; |
1346 » » break; | 1345 break; |
1347 » default: | 1346 default: |
1348 » » sign = 1; | 1347 sign = 1; |
1349 » » x = 0; | 1348 x = 0; |
1350 » » if (i < 0) { | 1349 if (i < 0) { |
1351 » » » sign = -1; | 1350 sign = -1; |
1352 » » » i = -(i); | 1351 i = -(i); |
1353 » » } | 1352 } |
1354 » » while (--i >= 0) { | 1353 while (--i >= 0) { |
1355 » » » prev = x; | 1354 prev = x; |
1356 » » » x = (x << PyLong_SHIFT) + v->ob_digit[i]; | 1355 x = (x << PyLong_SHIFT) + v->ob_digit[i]; |
1357 » » » if ((x >> PyLong_SHIFT) != prev) { | 1356 if ((x >> PyLong_SHIFT) != prev) { |
1358 » » » » *overflow = sign; | 1357 *overflow = sign; |
1359 » » » » goto exit; | 1358 goto exit; |
1360 » » » } | 1359 } |
1361 » » } | 1360 } |
1362 » » /* Haven't lost any bits, but casting to long requires extra | 1361 /* Haven't lost any bits, but casting to long requires extra |
1363 » » * care (see comment above). | 1362 * care (see comment above). |
1364 » » */ | 1363 */ |
1365 » » if (x <= (unsigned PY_LONG_LONG)PY_LLONG_MAX) { | 1364 if (x <= (unsigned PY_LONG_LONG)PY_LLONG_MAX) { |
1366 » » » res = (PY_LONG_LONG)x * sign; | 1365 res = (PY_LONG_LONG)x * sign; |
1367 » » } | 1366 } |
1368 » » else if (sign < 0 && x == PY_ABS_LLONG_MIN) { | 1367 else if (sign < 0 && x == PY_ABS_LLONG_MIN) { |
1369 » » » res = PY_LLONG_MIN; | 1368 res = PY_LLONG_MIN; |
1370 » » } | 1369 } |
1371 » » else { | 1370 else { |
1372 » » » *overflow = sign; | 1371 *overflow = sign; |
1373 » » » /* res is already set to -1 */ | 1372 /* res is already set to -1 */ |
1374 » » } | 1373 } |
1375 » } | 1374 } |
1376 exit: | 1375 exit: |
1377 » if (do_decref) { | 1376 if (do_decref) { |
1378 » » Py_DECREF(vv); | 1377 Py_DECREF(vv); |
1379 » } | 1378 } |
1380 » return res; | 1379 return res; |
1381 } | 1380 } |
1382 | 1381 |
1383 #endif /* HAVE_LONG_LONG */ | 1382 #endif /* HAVE_LONG_LONG */ |
1384 | 1383 |
1385 #define CHECK_BINOP(v,w) \ | 1384 #define CHECK_BINOP(v,w) \ |
1386 » if (!PyLong_Check(v) || !PyLong_Check(w)) { \ | 1385 do { \ |
1387 » » Py_INCREF(Py_NotImplemented); \ | 1386 if (!PyLong_Check(v) || !PyLong_Check(w)) { \ |
1388 » » return Py_NotImplemented; \ | 1387 Py_INCREF(Py_NotImplemented); \ |
1389 » } | 1388 return Py_NotImplemented; \ |
| 1389 } \ |
| 1390 } while(0) |
1390 | 1391 |
1391 /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < | 1392 /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d < |
1392 2**k if d is nonzero, else 0. */ | 1393 2**k if d is nonzero, else 0. */ |
1393 | 1394 |
1394 static const unsigned char BitLengthTable[32] = { | 1395 static const unsigned char BitLengthTable[32] = { |
1395 » 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, | 1396 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
1396 » 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 | 1397 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
1397 }; | 1398 }; |
1398 | 1399 |
1399 static int | 1400 static int |
1400 bits_in_digit(digit d) | 1401 bits_in_digit(digit d) |
1401 { | 1402 { |
1402 » int d_bits = 0; | 1403 int d_bits = 0; |
1403 » while (d >= 32) { | 1404 while (d >= 32) { |
1404 » » d_bits += 6; | 1405 d_bits += 6; |
1405 » » d >>= 6; | 1406 d >>= 6; |
1406 » } | 1407 } |
1407 » d_bits += (int)BitLengthTable[d]; | 1408 d_bits += (int)BitLengthTable[d]; |
1408 » return d_bits; | 1409 return d_bits; |
1409 } | 1410 } |
1410 | 1411 |
1411 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] | 1412 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
1412 * is modified in place, by adding y to it. Carries are propagated as far as | 1413 * is modified in place, by adding y to it. Carries are propagated as far as |
1413 * x[m-1], and the remaining carry (0 or 1) is returned. | 1414 * x[m-1], and the remaining carry (0 or 1) is returned. |
1414 */ | 1415 */ |
1415 static digit | 1416 static digit |
1416 v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) | 1417 v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) |
1417 { | 1418 { |
1418 » Py_ssize_t i; | 1419 Py_ssize_t i; |
1419 » digit carry = 0; | 1420 digit carry = 0; |
1420 | 1421 |
1421 » assert(m >= n); | 1422 assert(m >= n); |
1422 » for (i = 0; i < n; ++i) { | 1423 for (i = 0; i < n; ++i) { |
1423 » » carry += x[i] + y[i]; | 1424 carry += x[i] + y[i]; |
1424 » » x[i] = carry & PyLong_MASK; | 1425 x[i] = carry & PyLong_MASK; |
1425 » » carry >>= PyLong_SHIFT; | 1426 carry >>= PyLong_SHIFT; |
1426 » » assert((carry & 1) == carry); | 1427 assert((carry & 1) == carry); |
1427 » } | 1428 } |
1428 » for (; carry && i < m; ++i) { | 1429 for (; carry && i < m; ++i) { |
1429 » » carry += x[i]; | 1430 carry += x[i]; |
1430 » » x[i] = carry & PyLong_MASK; | 1431 x[i] = carry & PyLong_MASK; |
1431 » » carry >>= PyLong_SHIFT; | 1432 carry >>= PyLong_SHIFT; |
1432 » » assert((carry & 1) == carry); | 1433 assert((carry & 1) == carry); |
1433 » } | 1434 } |
1434 » return carry; | 1435 return carry; |
1435 } | 1436 } |
1436 | 1437 |
1437 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] | 1438 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n] |
1438 * is modified in place, by subtracting y from it. Borrows are propagated as | 1439 * is modified in place, by subtracting y from it. Borrows are propagated as |
1439 * far as x[m-1], and the remaining borrow (0 or 1) is returned. | 1440 * far as x[m-1], and the remaining borrow (0 or 1) is returned. |
1440 */ | 1441 */ |
1441 static digit | 1442 static digit |
1442 v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) | 1443 v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n) |
1443 { | 1444 { |
1444 » Py_ssize_t i; | 1445 Py_ssize_t i; |
1445 » digit borrow = 0; | 1446 digit borrow = 0; |
1446 | 1447 |
1447 » assert(m >= n); | 1448 assert(m >= n); |
1448 » for (i = 0; i < n; ++i) { | 1449 for (i = 0; i < n; ++i) { |
1449 » » borrow = x[i] - y[i] - borrow; | 1450 borrow = x[i] - y[i] - borrow; |
1450 » » x[i] = borrow & PyLong_MASK; | 1451 x[i] = borrow & PyLong_MASK; |
1451 » » borrow >>= PyLong_SHIFT; | 1452 borrow >>= PyLong_SHIFT; |
1452 » » borrow &= 1;» /* keep only 1 sign bit */ | 1453 borrow &= 1; /* keep only 1 sign bit */ |
1453 » } | 1454 } |
1454 » for (; borrow && i < m; ++i) { | 1455 for (; borrow && i < m; ++i) { |
1455 » » borrow = x[i] - borrow; | 1456 borrow = x[i] - borrow; |
1456 » » x[i] = borrow & PyLong_MASK; | 1457 x[i] = borrow & PyLong_MASK; |
1457 » » borrow >>= PyLong_SHIFT; | 1458 borrow >>= PyLong_SHIFT; |
1458 » » borrow &= 1; | 1459 borrow &= 1; |
1459 » } | 1460 } |
1460 » return borrow; | 1461 return borrow; |
1461 } | 1462 } |
1462 | 1463 |
1463 /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put | 1464 /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put |
1464 * result in z[0:m], and return the d bits shifted out of the top. | 1465 * result in z[0:m], and return the d bits shifted out of the top. |
1465 */ | 1466 */ |
1466 static digit | 1467 static digit |
1467 v_lshift(digit *z, digit *a, Py_ssize_t m, int d) | 1468 v_lshift(digit *z, digit *a, Py_ssize_t m, int d) |
1468 { | 1469 { |
1469 » Py_ssize_t i; | 1470 Py_ssize_t i; |
1470 » digit carry = 0; | 1471 digit carry = 0; |
1471 | 1472 |
1472 » assert(0 <= d && d < PyLong_SHIFT); | 1473 assert(0 <= d && d < PyLong_SHIFT); |
1473 » for (i=0; i < m; i++) { | 1474 for (i=0; i < m; i++) { |
1474 » » twodigits acc = (twodigits)a[i] << d | carry; | 1475 twodigits acc = (twodigits)a[i] << d | carry; |
1475 » » z[i] = (digit)acc & PyLong_MASK; | 1476 z[i] = (digit)acc & PyLong_MASK; |
1476 » » carry = (digit)(acc >> PyLong_SHIFT); | 1477 carry = (digit)(acc >> PyLong_SHIFT); |
1477 » } | 1478 } |
1478 » return carry; | 1479 return carry; |
1479 } | 1480 } |
1480 | 1481 |
1481 /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put | 1482 /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put |
1482 * result in z[0:m], and return the d bits shifted out of the bottom. | 1483 * result in z[0:m], and return the d bits shifted out of the bottom. |
1483 */ | 1484 */ |
1484 static digit | 1485 static digit |
1485 v_rshift(digit *z, digit *a, Py_ssize_t m, int d) | 1486 v_rshift(digit *z, digit *a, Py_ssize_t m, int d) |
1486 { | 1487 { |
1487 » Py_ssize_t i; | 1488 Py_ssize_t i; |
1488 » digit carry = 0; | 1489 digit carry = 0; |
1489 » digit mask = ((digit)1 << d) - 1U; | 1490 digit mask = ((digit)1 << d) - 1U; |
1490 | 1491 |
1491 » assert(0 <= d && d < PyLong_SHIFT); | 1492 assert(0 <= d && d < PyLong_SHIFT); |
1492 » for (i=m; i-- > 0;) { | 1493 for (i=m; i-- > 0;) { |
1493 » » twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; | 1494 twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i]; |
1494 » » carry = (digit)acc & mask; | 1495 carry = (digit)acc & mask; |
1495 » » z[i] = (digit)(acc >> d); | 1496 z[i] = (digit)(acc >> d); |
1496 » } | 1497 } |
1497 » return carry; | 1498 return carry; |
1498 } | 1499 } |
1499 | 1500 |
1500 /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient | 1501 /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
1501 in pout, and returning the remainder. pin and pout point at the LSD. | 1502 in pout, and returning the remainder. pin and pout point at the LSD. |
1502 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in | 1503 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in |
1503 _PyLong_Format, but that should be done with great care since longs are | 1504 _PyLong_Format, but that should be done with great care since longs are |
1504 immutable. */ | 1505 immutable. */ |
1505 | 1506 |
1506 static digit | 1507 static digit |
1507 inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) | 1508 inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n) |
1508 { | 1509 { |
1509 » twodigits rem = 0; | 1510 twodigits rem = 0; |
1510 | 1511 |
1511 » assert(n > 0 && n <= PyLong_MASK); | 1512 assert(n > 0 && n <= PyLong_MASK); |
1512 » pin += size; | 1513 pin += size; |
1513 » pout += size; | 1514 pout += size; |
1514 » while (--size >= 0) { | 1515 while (--size >= 0) { |
1515 » » digit hi; | 1516 digit hi; |
1516 » » rem = (rem << PyLong_SHIFT) | *--pin; | 1517 rem = (rem << PyLong_SHIFT) | *--pin; |
1517 » » *--pout = hi = (digit)(rem / n); | 1518 *--pout = hi = (digit)(rem / n); |
1518 » » rem -= (twodigits)hi * n; | 1519 rem -= (twodigits)hi * n; |
1519 » } | 1520 } |
1520 » return (digit)rem; | 1521 return (digit)rem; |
1521 } | 1522 } |
1522 | 1523 |
1523 /* Divide a long integer by a digit, returning both the quotient | 1524 /* Divide a long integer by a digit, returning both the quotient |
1524 (as function result) and the remainder (through *prem). | 1525 (as function result) and the remainder (through *prem). |
1525 The sign of a is ignored; n should not be zero. */ | 1526 The sign of a is ignored; n should not be zero. */ |
1526 | 1527 |
1527 static PyLongObject * | 1528 static PyLongObject * |
1528 divrem1(PyLongObject *a, digit n, digit *prem) | 1529 divrem1(PyLongObject *a, digit n, digit *prem) |
1529 { | 1530 { |
1530 » const Py_ssize_t size = ABS(Py_SIZE(a)); | 1531 const Py_ssize_t size = ABS(Py_SIZE(a)); |
1531 » PyLongObject *z; | 1532 PyLongObject *z; |
1532 | 1533 |
1533 » assert(n > 0 && n <= PyLong_MASK); | 1534 assert(n > 0 && n <= PyLong_MASK); |
1534 » z = _PyLong_New(size); | 1535 z = _PyLong_New(size); |
1535 » if (z == NULL) | 1536 if (z == NULL) |
1536 » » return NULL; | 1537 return NULL; |
1537 » *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); | 1538 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
1538 » return long_normalize(z); | 1539 return long_normalize(z); |
1539 } | 1540 } |
1540 | 1541 |
1541 /* Convert a long integer to a base 10 string. Returns a new non-shared | 1542 /* Convert a long integer to a base 10 string. Returns a new non-shared |
1542 string. (Return value is non-shared so that callers can modify the | 1543 string. (Return value is non-shared so that callers can modify the |
1543 returned value if necessary.) */ | 1544 returned value if necessary.) */ |
1544 | 1545 |
1545 static PyObject * | 1546 static PyObject * |
1546 long_to_decimal_string(PyObject *aa) | 1547 long_to_decimal_string(PyObject *aa) |
1547 { | 1548 { |
1548 » PyLongObject *scratch, *a; | 1549 PyLongObject *scratch, *a; |
1549 » PyObject *str; | 1550 PyObject *str; |
1550 » Py_ssize_t size, strlen, size_a, i, j; | 1551 Py_ssize_t size, strlen, size_a, i, j; |
1551 » digit *pout, *pin, rem, tenpow; | 1552 digit *pout, *pin, rem, tenpow; |
1552 » Py_UNICODE *p; | 1553 Py_UNICODE *p; |
1553 » int negative; | 1554 int negative; |
1554 | 1555 |
1555 » a = (PyLongObject *)aa; | 1556 a = (PyLongObject *)aa; |
1556 » if (a == NULL || !PyLong_Check(a)) { | 1557 if (a == NULL || !PyLong_Check(a)) { |
1557 » » PyErr_BadInternalCall(); | 1558 PyErr_BadInternalCall(); |
1558 » » return NULL; | 1559 return NULL; |
1559 » } | 1560 } |
1560 » size_a = ABS(Py_SIZE(a)); | 1561 size_a = ABS(Py_SIZE(a)); |
1561 » negative = Py_SIZE(a) < 0; | 1562 negative = Py_SIZE(a) < 0; |
1562 | 1563 |
1563 » /* quick and dirty upper bound for the number of digits | 1564 /* quick and dirty upper bound for the number of digits |
1564 » required to express a in base _PyLong_DECIMAL_BASE: | 1565 required to express a in base _PyLong_DECIMAL_BASE: |
1565 | 1566 |
1566 » #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) | 1567 #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE)) |
1567 | 1568 |
1568 » But log2(a) < size_a * PyLong_SHIFT, and | 1569 But log2(a) < size_a * PyLong_SHIFT, and |
1569 » log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT | 1570 log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT |
1570 » » » » > 3 * _PyLong_DECIMAL_SHIFT | 1571 > 3 * _PyLong_DECIMAL_SHIFT |
1571 » */ | 1572 */ |
1572 » if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { | 1573 if (size_a > PY_SSIZE_T_MAX / PyLong_SHIFT) { |
1573 » » PyErr_SetString(PyExc_OverflowError, | 1574 PyErr_SetString(PyExc_OverflowError, |
1574 » » » » "long is too large to format"); | 1575 "long is too large to format"); |
1575 » » return NULL; | 1576 return NULL; |
1576 » } | 1577 } |
1577 » /* the expression size_a * PyLong_SHIFT is now safe from overflow */ | 1578 /* the expression size_a * PyLong_SHIFT is now safe from overflow */ |
1578 » size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); | 1579 size = 1 + size_a * PyLong_SHIFT / (3 * _PyLong_DECIMAL_SHIFT); |
1579 » scratch = _PyLong_New(size); | 1580 scratch = _PyLong_New(size); |
1580 » if (scratch == NULL) | 1581 if (scratch == NULL) |
1581 » » return NULL; | 1582 return NULL; |
1582 | 1583 |
1583 » /* convert array of base _PyLong_BASE digits in pin to an array of | 1584 /* convert array of base _PyLong_BASE digits in pin to an array of |
1584 » base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, | 1585 base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP, |
1585 » Volume 2 (3rd edn), section 4.4, Method 1b). */ | 1586 Volume 2 (3rd edn), section 4.4, Method 1b). */ |
1586 » pin = a->ob_digit; | 1587 pin = a->ob_digit; |
1587 » pout = scratch->ob_digit; | 1588 pout = scratch->ob_digit; |
1588 » size = 0; | 1589 size = 0; |
1589 » for (i = size_a; --i >= 0; ) { | 1590 for (i = size_a; --i >= 0; ) { |
1590 » » digit hi = pin[i]; | 1591 digit hi = pin[i]; |
1591 » » for (j = 0; j < size; j++) { | 1592 for (j = 0; j < size; j++) { |
1592 » » » twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; | 1593 twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi; |
1593 » » » hi = (digit)(z / _PyLong_DECIMAL_BASE); | 1594 hi = (digit)(z / _PyLong_DECIMAL_BASE); |
1594 » » » pout[j] = (digit)(z - (twodigits)hi * | 1595 pout[j] = (digit)(z - (twodigits)hi * |
1595 » » » » » _PyLong_DECIMAL_BASE); | 1596 _PyLong_DECIMAL_BASE); |
1596 » » } | 1597 } |
1597 » » while (hi) { | 1598 while (hi) { |
1598 » » » pout[size++] = hi % _PyLong_DECIMAL_BASE; | 1599 pout[size++] = hi % _PyLong_DECIMAL_BASE; |
1599 » » » hi /= _PyLong_DECIMAL_BASE; | 1600 hi /= _PyLong_DECIMAL_BASE; |
1600 » » } | 1601 } |
1601 » » /* check for keyboard interrupt */ | 1602 /* check for keyboard interrupt */ |
1602 » » SIGCHECK({ | 1603 SIGCHECK({ |
1603 » » » Py_DECREF(scratch); | 1604 Py_DECREF(scratch); |
1604 » » » return NULL; | 1605 return NULL; |
1605 » » }) | 1606 }); |
1606 » } | 1607 } |
1607 » /* pout should have at least one digit, so that the case when a = 0 | 1608 /* pout should have at least one digit, so that the case when a = 0 |
1608 » works correctly */ | 1609 works correctly */ |
1609 » if (size == 0) | 1610 if (size == 0) |
1610 » » pout[size++] = 0; | 1611 pout[size++] = 0; |
1611 | 1612 |
1612 » /* calculate exact length of output string, and allocate */ | 1613 /* calculate exact length of output string, and allocate */ |
1613 » strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; | 1614 strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT; |
1614 » tenpow = 10; | 1615 tenpow = 10; |
1615 » rem = pout[size-1]; | 1616 rem = pout[size-1]; |
1616 » while (rem >= tenpow) { | 1617 while (rem >= tenpow) { |
1617 » » tenpow *= 10; | 1618 tenpow *= 10; |
1618 » » strlen++; | 1619 strlen++; |
1619 » } | 1620 } |
1620 » str = PyUnicode_FromUnicode(NULL, strlen); | 1621 str = PyUnicode_FromUnicode(NULL, strlen); |
1621 » if (str == NULL) { | 1622 if (str == NULL) { |
1622 » » Py_DECREF(scratch); | 1623 Py_DECREF(scratch); |
1623 » » return NULL; | 1624 return NULL; |
1624 » } | 1625 } |
1625 | 1626 |
1626 » /* fill the string right-to-left */ | 1627 /* fill the string right-to-left */ |
1627 » p = PyUnicode_AS_UNICODE(str) + strlen; | 1628 p = PyUnicode_AS_UNICODE(str) + strlen; |
1628 » *p = '\0'; | 1629 *p = '\0'; |
1629 » /* pout[0] through pout[size-2] contribute exactly | 1630 /* pout[0] through pout[size-2] contribute exactly |
1630 » _PyLong_DECIMAL_SHIFT digits each */ | 1631 _PyLong_DECIMAL_SHIFT digits each */ |
1631 » for (i=0; i < size - 1; i++) { | 1632 for (i=0; i < size - 1; i++) { |
1632 » » rem = pout[i]; | 1633 rem = pout[i]; |
1633 » » for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { | 1634 for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { |
1634 » » » *--p = '0' + rem % 10; | 1635 *--p = '0' + rem % 10; |
1635 » » » rem /= 10; | 1636 rem /= 10; |
1636 » » } | 1637 } |
1637 » } | 1638 } |
1638 » /* pout[size-1]: always produce at least one decimal digit */ | 1639 /* pout[size-1]: always produce at least one decimal digit */ |
1639 » rem = pout[i]; | 1640 rem = pout[i]; |
1640 » do { | 1641 do { |
1641 » » *--p = '0' + rem % 10; | 1642 *--p = '0' + rem % 10; |
1642 » » rem /= 10; | 1643 rem /= 10; |
1643 » } while (rem != 0); | 1644 } while (rem != 0); |
1644 | 1645 |
1645 » /* and sign */ | 1646 /* and sign */ |
1646 » if (negative) | 1647 if (negative) |
1647 » » *--p = '-'; | 1648 *--p = '-'; |
1648 | 1649 |
1649 » /* check we've counted correctly */ | 1650 /* check we've counted correctly */ |
1650 » assert(p == PyUnicode_AS_UNICODE(str)); | 1651 assert(p == PyUnicode_AS_UNICODE(str)); |
1651 » Py_DECREF(scratch); | 1652 Py_DECREF(scratch); |
1652 » return (PyObject *)str; | 1653 return (PyObject *)str; |
1653 } | 1654 } |
1654 | 1655 |
1655 /* Convert a long int object to a string, using a given conversion base, | 1656 /* Convert a long int object to a string, using a given conversion base, |
1656 which should be one of 2, 8, 10 or 16. Return a string object. | 1657 which should be one of 2, 8, 10 or 16. Return a string object. |
1657 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ | 1658 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */ |
1658 | 1659 |
1659 PyObject * | 1660 PyObject * |
1660 _PyLong_Format(PyObject *aa, int base) | 1661 _PyLong_Format(PyObject *aa, int base) |
1661 { | 1662 { |
1662 » register PyLongObject *a = (PyLongObject *)aa; | 1663 register PyLongObject *a = (PyLongObject *)aa; |
1663 » PyObject *str; | 1664 PyObject *str; |
1664 » Py_ssize_t i, sz; | 1665 Py_ssize_t i, sz; |
1665 » Py_ssize_t size_a; | 1666 Py_ssize_t size_a; |
1666 » Py_UNICODE *p, sign = '\0'; | 1667 Py_UNICODE *p, sign = '\0'; |
1667 » int bits; | 1668 int bits; |
1668 | 1669 |
1669 » assert(base == 2 || base == 8 || base == 10 || base == 16); | 1670 assert(base == 2 || base == 8 || base == 10 || base == 16); |
1670 » if (base == 10) | 1671 if (base == 10) |
1671 » » return long_to_decimal_string((PyObject *)a); | 1672 return long_to_decimal_string((PyObject *)a); |
1672 | 1673 |
1673 » if (a == NULL || !PyLong_Check(a)) { | 1674 if (a == NULL || !PyLong_Check(a)) { |
1674 » » PyErr_BadInternalCall(); | 1675 PyErr_BadInternalCall(); |
1675 » » return NULL; | 1676 return NULL; |
1676 » } | 1677 } |
1677 » size_a = ABS(Py_SIZE(a)); | 1678 size_a = ABS(Py_SIZE(a)); |
1678 | 1679 |
1679 » /* Compute a rough upper bound for the length of the string */ | 1680 /* Compute a rough upper bound for the length of the string */ |
1680 » switch (base) { | 1681 switch (base) { |
1681 » case 16: | 1682 case 16: |
1682 » » bits = 4; | 1683 bits = 4; |
1683 » » break; | 1684 break; |
1684 » case 8: | 1685 case 8: |
1685 » » bits = 3; | 1686 bits = 3; |
1686 » » break; | 1687 break; |
1687 » case 2: | 1688 case 2: |
1688 » » bits = 1; | 1689 bits = 1; |
1689 » » break; | 1690 break; |
1690 » default: | 1691 default: |
1691 » » assert(0); /* shouldn't ever get here */ | 1692 assert(0); /* shouldn't ever get here */ |
1692 » » bits = 0; /* to silence gcc warning */ | 1693 bits = 0; /* to silence gcc warning */ |
1693 » } | 1694 } |
1694 » /* compute length of output string: allow 2 characters for prefix and | 1695 /* compute length of output string: allow 2 characters for prefix and |
1695 » 1 for possible '-' sign. */ | 1696 1 for possible '-' sign. */ |
1696 » if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { | 1697 if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) { |
1697 » » PyErr_SetString(PyExc_OverflowError, | 1698 PyErr_SetString(PyExc_OverflowError, |
1698 » » » » "int is too large to format"); | 1699 "int is too large to format"); |
1699 » » return NULL; | 1700 return NULL; |
1700 » } | 1701 } |
1701 » /* now size_a * PyLong_SHIFT + 3 <= PY_SSIZE_T_MAX, so the RHS below | 1702 /* now size_a * PyLong_SHIFT + 3 <= PY_SSIZE_T_MAX, so the RHS below |
1702 » is safe from overflow */ | 1703 is safe from overflow */ |
1703 » sz = 3 + (size_a * PyLong_SHIFT + (bits - 1)) / bits; | 1704 sz = 3 + (size_a * PyLong_SHIFT + (bits - 1)) / bits; |
1704 » assert(sz >= 0); | 1705 assert(sz >= 0); |
1705 » str = PyUnicode_FromUnicode(NULL, sz); | 1706 str = PyUnicode_FromUnicode(NULL, sz); |
1706 » if (str == NULL) | 1707 if (str == NULL) |
1707 » » return NULL; | 1708 return NULL; |
1708 » p = PyUnicode_AS_UNICODE(str) + sz; | 1709 p = PyUnicode_AS_UNICODE(str) + sz; |
1709 » *p = '\0'; | 1710 *p = '\0'; |
1710 » if (Py_SIZE(a) < 0) | 1711 if (Py_SIZE(a) < 0) |
1711 » » sign = '-'; | 1712 sign = '-'; |
1712 | 1713 |
1713 » if (Py_SIZE(a) == 0) { | 1714 if (Py_SIZE(a) == 0) { |
1714 » » *--p = '0'; | 1715 *--p = '0'; |
1715 » } | 1716 } |
1716 » else { | 1717 else { |
1717 » » /* JRH: special case for power-of-2 bases */ | 1718 /* JRH: special case for power-of-2 bases */ |
1718 » » twodigits accum = 0; | 1719 twodigits accum = 0; |
1719 » » int accumbits = 0;» /* # of bits in accum */ | 1720 int accumbits = 0; /* # of bits in accum */ |
1720 » » for (i = 0; i < size_a; ++i) { | 1721 for (i = 0; i < size_a; ++i) { |
1721 » » » accum |= (twodigits)a->ob_digit[i] << accumbits; | 1722 accum |= (twodigits)a->ob_digit[i] << accumbits; |
1722 » » » accumbits += PyLong_SHIFT; | 1723 accumbits += PyLong_SHIFT; |
1723 » » » assert(accumbits >= bits); | 1724 assert(accumbits >= bits); |
1724 » » » do { | 1725 do { |
1725 » » » » Py_UNICODE cdigit; | 1726 Py_UNICODE cdigit; |
1726 » » » » cdigit = (Py_UNICODE)(accum & (base - 1)); | 1727 cdigit = (Py_UNICODE)(accum & (base - 1)); |
1727 » » » » cdigit += (cdigit < 10) ? '0' : 'a'-10; | 1728 cdigit += (cdigit < 10) ? '0' : 'a'-10; |
1728 » » » » assert(p > PyUnicode_AS_UNICODE(str)); | 1729 assert(p > PyUnicode_AS_UNICODE(str)); |
1729 » » » » *--p = cdigit; | 1730 *--p = cdigit; |
1730 » » » » accumbits -= bits; | 1731 accumbits -= bits; |
1731 » » » » accum >>= bits; | 1732 accum >>= bits; |
1732 » » » } while (i < size_a-1 ? accumbits >= bits : accum > 0); | 1733 } while (i < size_a-1 ? accumbits >= bits : accum > 0); |
1733 » » } | 1734 } |
1734 » } | 1735 } |
1735 | 1736 |
1736 » if (base == 16) | 1737 if (base == 16) |
1737 » » *--p = 'x'; | 1738 *--p = 'x'; |
1738 » else if (base == 8) | 1739 else if (base == 8) |
1739 » » *--p = 'o'; | 1740 *--p = 'o'; |
1740 » else /* (base == 2) */ | 1741 else /* (base == 2) */ |
1741 » » *--p = 'b'; | 1742 *--p = 'b'; |
1742 » *--p = '0'; | 1743 *--p = '0'; |
1743 » if (sign) | 1744 if (sign) |
1744 » » *--p = sign; | 1745 *--p = sign; |
1745 » if (p != PyUnicode_AS_UNICODE(str)) { | 1746 if (p != PyUnicode_AS_UNICODE(str)) { |
1746 » » Py_UNICODE *q = PyUnicode_AS_UNICODE(str); | 1747 Py_UNICODE *q = PyUnicode_AS_UNICODE(str); |
1747 » » assert(p > q); | 1748 assert(p > q); |
1748 » » do { | 1749 do { |
1749 » » } while ((*q++ = *p++) != '\0'); | 1750 } while ((*q++ = *p++) != '\0'); |
1750 » » q--; | 1751 q--; |
1751 » » if (PyUnicode_Resize(&str,(Py_ssize_t) (q - | 1752 if (PyUnicode_Resize(&str,(Py_ssize_t) (q - |
1752 » » » » » » PyUnicode_AS_UNICODE(str)))) { | 1753 PyUnicode_AS_UNICODE(str)))) { |
1753 » » » Py_DECREF(str); | 1754 Py_DECREF(str); |
1754 » » » return NULL; | 1755 return NULL; |
1755 » » } | 1756 } |
1756 » } | 1757 } |
1757 » return (PyObject *)str; | 1758 return (PyObject *)str; |
1758 } | 1759 } |
1759 | 1760 |
1760 /* Table of digit values for 8-bit string -> integer conversion. | 1761 /* Table of digit values for 8-bit string -> integer conversion. |
1761 * '0' maps to 0, ..., '9' maps to 9. | 1762 * '0' maps to 0, ..., '9' maps to 9. |
1762 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. | 1763 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35. |
1763 * All other indices map to 37. | 1764 * All other indices map to 37. |
1764 * Note that when converting a base B string, a char c is a legitimate | 1765 * Note that when converting a base B string, a char c is a legitimate |
1765 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. | 1766 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B. |
1766 */ | 1767 */ |
1767 unsigned char _PyLong_DigitValue[256] = { | 1768 unsigned char _PyLong_DigitValue[256] = { |
1768 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1769 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1769 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1770 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1770 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1771 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1771 » 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, | 1772 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37, |
1772 » 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | 1773 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
1773 » 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, | 1774 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
1774 » 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | 1775 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
1775 » 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, | 1776 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37, |
1776 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1777 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1777 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1778 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1778 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1779 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1779 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1780 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1780 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1781 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1781 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1782 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1782 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1783 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1783 » 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, | 1784 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, |
1784 }; | 1785 }; |
1785 | 1786 |
1786 /* *str points to the first digit in a string of base `base` digits. base | 1787 /* *str points to the first digit in a string of base `base` digits. base |
1787 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first | 1788 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first |
1788 * non-digit (which may be *str!). A normalized long is returned. | 1789 * non-digit (which may be *str!). A normalized long is returned. |
1789 * The point to this routine is that it takes time linear in the number of | 1790 * The point to this routine is that it takes time linear in the number of |
1790 * string characters. | 1791 * string characters. |
1791 */ | 1792 */ |
1792 static PyLongObject * | 1793 static PyLongObject * |
1793 long_from_binary_base(char **str, int base) | 1794 long_from_binary_base(char **str, int base) |
1794 { | 1795 { |
1795 » char *p = *str; | 1796 char *p = *str; |
1796 » char *start = p; | 1797 char *start = p; |
1797 » int bits_per_char; | 1798 int bits_per_char; |
1798 » Py_ssize_t n; | 1799 Py_ssize_t n; |
1799 » PyLongObject *z; | 1800 PyLongObject *z; |
1800 » twodigits accum; | 1801 twodigits accum; |
1801 » int bits_in_accum; | 1802 int bits_in_accum; |
1802 » digit *pdigit; | 1803 digit *pdigit; |
1803 | 1804 |
1804 » assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); | 1805 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0); |
1805 » n = base; | 1806 n = base; |
1806 » for (bits_per_char = -1; n; ++bits_per_char) | 1807 for (bits_per_char = -1; n; ++bits_per_char) |
1807 » » n >>= 1; | 1808 n >>= 1; |
1808 » /* n <- total # of bits needed, while setting p to end-of-string */ | 1809 /* n <- total # of bits needed, while setting p to end-of-string */ |
1809 » while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) | 1810 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base) |
1810 » » ++p; | 1811 ++p; |
1811 » *str = p; | 1812 *str = p; |
1812 » /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ | 1813 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */ |
1813 » n = (p - start) * bits_per_char + PyLong_SHIFT - 1; | 1814 n = (p - start) * bits_per_char + PyLong_SHIFT - 1; |
1814 » if (n / bits_per_char < p - start) { | 1815 if (n / bits_per_char < p - start) { |
1815 » » PyErr_SetString(PyExc_ValueError, | 1816 PyErr_SetString(PyExc_ValueError, |
1816 » » » » "int string too large to convert"); | 1817 "int string too large to convert"); |
1817 » » return NULL; | 1818 return NULL; |
1818 » } | 1819 } |
1819 » n = n / PyLong_SHIFT; | 1820 n = n / PyLong_SHIFT; |
1820 » z = _PyLong_New(n); | 1821 z = _PyLong_New(n); |
1821 » if (z == NULL) | 1822 if (z == NULL) |
1822 » » return NULL; | 1823 return NULL; |
1823 » /* Read string from right, and fill in long from left; i.e., | 1824 /* Read string from right, and fill in long from left; i.e., |
1824 » * from least to most significant in both. | 1825 * from least to most significant in both. |
1825 » */ | 1826 */ |
1826 » accum = 0; | 1827 accum = 0; |
1827 » bits_in_accum = 0; | 1828 bits_in_accum = 0; |
1828 » pdigit = z->ob_digit; | 1829 pdigit = z->ob_digit; |
1829 » while (--p >= start) { | 1830 while (--p >= start) { |
1830 » » int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; | 1831 int k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)]; |
1831 » » assert(k >= 0 && k < base); | 1832 assert(k >= 0 && k < base); |
1832 » » accum |= (twodigits)k << bits_in_accum; | 1833 accum |= (twodigits)k << bits_in_accum; |
1833 » » bits_in_accum += bits_per_char; | 1834 bits_in_accum += bits_per_char; |
1834 » » if (bits_in_accum >= PyLong_SHIFT) { | 1835 if (bits_in_accum >= PyLong_SHIFT) { |
1835 » » » *pdigit++ = (digit)(accum & PyLong_MASK); | 1836 *pdigit++ = (digit)(accum & PyLong_MASK); |
1836 » » » assert(pdigit - z->ob_digit <= n); | 1837 assert(pdigit - z->ob_digit <= n); |
1837 » » » accum >>= PyLong_SHIFT; | 1838 accum >>= PyLong_SHIFT; |
1838 » » » bits_in_accum -= PyLong_SHIFT; | 1839 bits_in_accum -= PyLong_SHIFT; |
1839 » » » assert(bits_in_accum < PyLong_SHIFT); | 1840 assert(bits_in_accum < PyLong_SHIFT); |
1840 » » } | 1841 } |
1841 » } | 1842 } |
1842 » if (bits_in_accum) { | 1843 if (bits_in_accum) { |
1843 » » assert(bits_in_accum <= PyLong_SHIFT); | 1844 assert(bits_in_accum <= PyLong_SHIFT); |
1844 » » *pdigit++ = (digit)accum; | 1845 *pdigit++ = (digit)accum; |
1845 » » assert(pdigit - z->ob_digit <= n); | 1846 assert(pdigit - z->ob_digit <= n); |
1846 » } | 1847 } |
1847 » while (pdigit - z->ob_digit < n) | 1848 while (pdigit - z->ob_digit < n) |
1848 » » *pdigit++ = 0; | 1849 *pdigit++ = 0; |
1849 » return long_normalize(z); | 1850 return long_normalize(z); |
1850 } | 1851 } |
1851 | 1852 |
1852 PyObject * | 1853 PyObject * |
1853 PyLong_FromString(char *str, char **pend, int base) | 1854 PyLong_FromString(char *str, char **pend, int base) |
1854 { | 1855 { |
1855 » int sign = 1, error_if_nonzero = 0; | 1856 int sign = 1, error_if_nonzero = 0; |
1856 » char *start, *orig_str = str; | 1857 char *start, *orig_str = str; |
1857 » PyLongObject *z = NULL; | 1858 PyLongObject *z = NULL; |
1858 » PyObject *strobj; | 1859 PyObject *strobj; |
1859 » Py_ssize_t slen; | 1860 Py_ssize_t slen; |
1860 | 1861 |
1861 » if ((base != 0 && base < 2) || base > 36) { | 1862 if ((base != 0 && base < 2) || base > 36) { |
1862 » » PyErr_SetString(PyExc_ValueError, | 1863 PyErr_SetString(PyExc_ValueError, |
1863 » » » » "int() arg 2 must be >= 2 and <= 36"); | 1864 "int() arg 2 must be >= 2 and <= 36"); |
1864 » » return NULL; | 1865 return NULL; |
1865 » } | 1866 } |
1866 » while (*str != '\0' && isspace(Py_CHARMASK(*str))) | 1867 while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
1867 » » str++; | 1868 str++; |
1868 » if (*str == '+') | 1869 if (*str == '+') |
1869 » » ++str; | 1870 ++str; |
1870 » else if (*str == '-') { | 1871 else if (*str == '-') { |
1871 » » ++str; | 1872 ++str; |
1872 » » sign = -1; | 1873 sign = -1; |
1873 » } | 1874 } |
1874 » if (base == 0) { | 1875 if (base == 0) { |
1875 » » if (str[0] != '0') | 1876 if (str[0] != '0') |
1876 » » » base = 10; | 1877 base = 10; |
1877 » » else if (str[1] == 'x' || str[1] == 'X') | 1878 else if (str[1] == 'x' || str[1] == 'X') |
1878 » » » base = 16; | 1879 base = 16; |
1879 » » else if (str[1] == 'o' || str[1] == 'O') | 1880 else if (str[1] == 'o' || str[1] == 'O') |
1880 » » » base = 8; | 1881 base = 8; |
1881 » » else if (str[1] == 'b' || str[1] == 'B') | 1882 else if (str[1] == 'b' || str[1] == 'B') |
1882 » » » base = 2; | 1883 base = 2; |
1883 » » else { | 1884 else { |
1884 » » » /* "old" (C-style) octal literal, now invalid. | 1885 /* "old" (C-style) octal literal, now invalid. |
1885 » » » it might still be zero though */ | 1886 it might still be zero though */ |
1886 » » » error_if_nonzero = 1; | 1887 error_if_nonzero = 1; |
1887 » » » base = 10; | 1888 base = 10; |
1888 » » } | 1889 } |
1889 » } | 1890 } |
1890 » if (str[0] == '0' && | 1891 if (str[0] == '0' && |
1891 » ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || | 1892 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) || |
1892 » (base == 8 && (str[1] == 'o' || str[1] == 'O')) || | 1893 (base == 8 && (str[1] == 'o' || str[1] == 'O')) || |
1893 » (base == 2 && (str[1] == 'b' || str[1] == 'B')))) | 1894 (base == 2 && (str[1] == 'b' || str[1] == 'B')))) |
1894 » » str += 2; | 1895 str += 2; |
1895 | 1896 |
1896 » start = str; | 1897 start = str; |
1897 » if ((base & (base - 1)) == 0) | 1898 if ((base & (base - 1)) == 0) |
1898 » » z = long_from_binary_base(&str, base); | 1899 z = long_from_binary_base(&str, base); |
1899 » else { | 1900 else { |
1900 /*** | 1901 /*** |
1901 Binary bases can be converted in time linear in the number of digits, because | 1902 Binary bases can be converted in time linear in the number of digits, because |
1902 Python's representation base is binary. Other bases (including decimal!) use | 1903 Python's representation base is binary. Other bases (including decimal!) use |
1903 the simple quadratic-time algorithm below, complicated by some speed tricks. | 1904 the simple quadratic-time algorithm below, complicated by some speed tricks. |
1904 | 1905 |
1905 First some math: the largest integer that can be expressed in N base-B digits | 1906 First some math: the largest integer that can be expressed in N base-B digits |
1906 is B**N-1. Consequently, if we have an N-digit input in base B, the worst- | 1907 is B**N-1. Consequently, if we have an N-digit input in base B, the worst- |
1907 case number of Python digits needed to hold it is the smallest integer n s.t. | 1908 case number of Python digits needed to hold it is the smallest integer n s.t. |
1908 | 1909 |
1909 BASE**n-1 >= B**N-1 [or, adding 1 to both sides] | 1910 BASE**n-1 >= B**N-1 [or, adding 1 to both sides] |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1975 is very close to an integer. If we were working with IEEE single-precision, | 1976 is very close to an integer. If we were working with IEEE single-precision, |
1976 rounding errors could kill us. Finding worst cases in IEEE double-precision | 1977 rounding errors could kill us. Finding worst cases in IEEE double-precision |
1977 requires better-than-double-precision log() functions, and Tim didn't bother. | 1978 requires better-than-double-precision log() functions, and Tim didn't bother. |
1978 Instead the code checks to see whether the allocated space is enough as each | 1979 Instead the code checks to see whether the allocated space is enough as each |
1979 new Python digit is added, and copies the whole thing to a larger long if not. | 1980 new Python digit is added, and copies the whole thing to a larger long if not. |
1980 This should happen extremely rarely, and in fact I don't have a test case | 1981 This should happen extremely rarely, and in fact I don't have a test case |
1981 that triggers it(!). Instead the code was tested by artificially allocating | 1982 that triggers it(!). Instead the code was tested by artificially allocating |
1982 just 1 digit at the start, so that the copying code was exercised for every | 1983 just 1 digit at the start, so that the copying code was exercised for every |
1983 digit beyond the first. | 1984 digit beyond the first. |
1984 ***/ | 1985 ***/ |
1985 » » register twodigits c;» /* current input character */ | 1986 register twodigits c; /* current input character */ |
1986 » » Py_ssize_t size_z; | 1987 Py_ssize_t size_z; |
1987 » » int i; | 1988 int i; |
1988 » » int convwidth; | 1989 int convwidth; |
1989 » » twodigits convmultmax, convmult; | 1990 twodigits convmultmax, convmult; |
1990 » » digit *pz, *pzstop; | 1991 digit *pz, *pzstop; |
1991 » » char* scan; | 1992 char* scan; |
1992 | 1993 |
1993 » » static double log_base_BASE[37] = {0.0e0,}; | 1994 static double log_base_BASE[37] = {0.0e0,}; |
1994 » » static int convwidth_base[37] = {0,}; | 1995 static int convwidth_base[37] = {0,}; |
1995 » » static twodigits convmultmax_base[37] = {0,}; | 1996 static twodigits convmultmax_base[37] = {0,}; |
1996 | 1997 |
1997 » » if (log_base_BASE[base] == 0.0) { | 1998 if (log_base_BASE[base] == 0.0) { |
1998 » » » twodigits convmax = base; | 1999 twodigits convmax = base; |
1999 » » » int i = 1; | 2000 int i = 1; |
2000 | 2001 |
2001 » » » log_base_BASE[base] = log((double)base) / | 2002 log_base_BASE[base] = (log((double)base) / |
2002 » » » » » » log((double)PyLong_BASE); | 2003 log((double)PyLong_BASE)); |
2003 » » » for (;;) { | 2004 for (;;) { |
2004 » » » » twodigits next = convmax * base; | 2005 twodigits next = convmax * base; |
2005 » » » » if (next > PyLong_BASE) | 2006 if (next > PyLong_BASE) |
2006 » » » » » break; | 2007 break; |
2007 » » » » convmax = next; | 2008 convmax = next; |
2008 » » » » ++i; | 2009 ++i; |
2009 » » » } | 2010 } |
2010 » » » convmultmax_base[base] = convmax; | 2011 convmultmax_base[base] = convmax; |
2011 » » » assert(i > 0); | 2012 assert(i > 0); |
2012 » » » convwidth_base[base] = i; | 2013 convwidth_base[base] = i; |
2013 » » } | 2014 } |
2014 | 2015 |
2015 » » /* Find length of the string of numeric characters. */ | 2016 /* Find length of the string of numeric characters. */ |
2016 » » scan = str; | 2017 scan = str; |
2017 » » while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) | 2018 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base) |
2018 » » » ++scan; | 2019 ++scan; |
2019 | 2020 |
2020 » » /* Create a long object that can contain the largest possible | 2021 /* Create a long object that can contain the largest possible |
2021 » » * integer with this base and length. Note that there's no | 2022 * integer with this base and length. Note that there's no |
2022 » » * need to initialize z->ob_digit -- no slot is read up before | 2023 * need to initialize z->ob_digit -- no slot is read up before |
2023 » » * being stored into. | 2024 * being stored into. |
2024 » » */ | 2025 */ |
2025 » » size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; | 2026 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1; |
2026 » » /* Uncomment next line to test exceedingly rare copy code */ | 2027 /* Uncomment next line to test exceedingly rare copy code */ |
2027 » » /* size_z = 1; */ | 2028 /* size_z = 1; */ |
2028 » » assert(size_z > 0); | 2029 assert(size_z > 0); |
2029 » » z = _PyLong_New(size_z); | 2030 z = _PyLong_New(size_z); |
2030 » » if (z == NULL) | 2031 if (z == NULL) |
2031 » » » return NULL; | 2032 return NULL; |
2032 » » Py_SIZE(z) = 0; | 2033 Py_SIZE(z) = 0; |
2033 | 2034 |
2034 » » /* `convwidth` consecutive input digits are treated as a single | 2035 /* `convwidth` consecutive input digits are treated as a single |
2035 » » * digit in base `convmultmax`. | 2036 * digit in base `convmultmax`. |
2036 » » */ | 2037 */ |
2037 » » convwidth = convwidth_base[base]; | 2038 convwidth = convwidth_base[base]; |
2038 » » convmultmax = convmultmax_base[base]; | 2039 convmultmax = convmultmax_base[base]; |
2039 | 2040 |
2040 » » /* Work ;-) */ | 2041 /* Work ;-) */ |
2041 » » while (str < scan) { | 2042 while (str < scan) { |
2042 » » » /* grab up to convwidth digits from the input string */ | 2043 /* grab up to convwidth digits from the input string */ |
2043 » » » c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; | 2044 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; |
2044 » » » for (i = 1; i < convwidth && str != scan; ++i, ++str) { | 2045 for (i = 1; i < convwidth && str != scan; ++i, ++str) { |
2045 » » » » c = (twodigits)(c * base + | 2046 c = (twodigits)(c * base + |
2046 » » » » » (int)_PyLong_DigitValue[Py_CHARMASK(*str
)]); | 2047 (int)_PyLong_DigitValue[Py_CHARMASK(*str)]); |
2047 » » » » assert(c < PyLong_BASE); | 2048 assert(c < PyLong_BASE); |
2048 » » » } | 2049 } |
2049 | 2050 |
2050 » » » convmult = convmultmax; | 2051 convmult = convmultmax; |
2051 » » » /* Calculate the shift only if we couldn't get | 2052 /* Calculate the shift only if we couldn't get |
2052 » » » * convwidth digits. | 2053 * convwidth digits. |
2053 » » » */ | 2054 */ |
2054 » » » if (i != convwidth) { | 2055 if (i != convwidth) { |
2055 » » » » convmult = base; | 2056 convmult = base; |
2056 » » » » for ( ; i > 1; --i) | 2057 for ( ; i > 1; --i) |
2057 » » » » » convmult *= base; | 2058 convmult *= base; |
2058 » » » } | 2059 } |
2059 | 2060 |
2060 » » » /* Multiply z by convmult, and add c. */ | 2061 /* Multiply z by convmult, and add c. */ |
2061 » » » pz = z->ob_digit; | 2062 pz = z->ob_digit; |
2062 » » » pzstop = pz + Py_SIZE(z); | 2063 pzstop = pz + Py_SIZE(z); |
2063 » » » for (; pz < pzstop; ++pz) { | 2064 for (; pz < pzstop; ++pz) { |
2064 » » » » c += (twodigits)*pz * convmult; | 2065 c += (twodigits)*pz * convmult; |
2065 » » » » *pz = (digit)(c & PyLong_MASK); | 2066 *pz = (digit)(c & PyLong_MASK); |
2066 » » » » c >>= PyLong_SHIFT; | 2067 c >>= PyLong_SHIFT; |
2067 » » » } | 2068 } |
2068 » » » /* carry off the current end? */ | 2069 /* carry off the current end? */ |
2069 » » » if (c) { | 2070 if (c) { |
2070 » » » » assert(c < PyLong_BASE); | 2071 assert(c < PyLong_BASE); |
2071 » » » » if (Py_SIZE(z) < size_z) { | 2072 if (Py_SIZE(z) < size_z) { |
2072 » » » » » *pz = (digit)c; | 2073 *pz = (digit)c; |
2073 » » » » » ++Py_SIZE(z); | 2074 ++Py_SIZE(z); |
2074 » » » » } | 2075 } |
2075 » » » » else { | 2076 else { |
2076 » » » » » PyLongObject *tmp; | 2077 PyLongObject *tmp; |
2077 » » » » » /* Extremely rare. Get more space. */ | 2078 /* Extremely rare. Get more space. */ |
2078 » » » » » assert(Py_SIZE(z) == size_z); | 2079 assert(Py_SIZE(z) == size_z); |
2079 » » » » » tmp = _PyLong_New(size_z + 1); | 2080 tmp = _PyLong_New(size_z + 1); |
2080 » » » » » if (tmp == NULL) { | 2081 if (tmp == NULL) { |
2081 » » » » » » Py_DECREF(z); | 2082 Py_DECREF(z); |
2082 » » » » » » return NULL; | 2083 return NULL; |
2083 » » » » » } | 2084 } |
2084 » » » » » memcpy(tmp->ob_digit, | 2085 memcpy(tmp->ob_digit, |
2085 » » » » » z->ob_digit, | 2086 z->ob_digit, |
2086 » » » » » sizeof(digit) * size_z); | 2087 sizeof(digit) * size_z); |
2087 » » » » » Py_DECREF(z); | 2088 Py_DECREF(z); |
2088 » » » » » z = tmp; | 2089 z = tmp; |
2089 » » » » » z->ob_digit[size_z] = (digit)c; | 2090 z->ob_digit[size_z] = (digit)c; |
2090 » » » » » ++size_z; | 2091 ++size_z; |
2091 » » » » } | 2092 } |
2092 » » » } | 2093 } |
2093 » » } | 2094 } |
2094 » } | 2095 } |
2095 » if (z == NULL) | 2096 if (z == NULL) |
2096 » » return NULL; | 2097 return NULL; |
2097 » if (error_if_nonzero) { | 2098 if (error_if_nonzero) { |
2098 » » /* reset the base to 0, else the exception message | 2099 /* reset the base to 0, else the exception message |
2099 » » doesn't make too much sense */ | 2100 doesn't make too much sense */ |
2100 » » base = 0; | 2101 base = 0; |
2101 » » if (Py_SIZE(z) != 0) | 2102 if (Py_SIZE(z) != 0) |
2102 » » » goto onError; | 2103 goto onError; |
2103 » » /* there might still be other problems, therefore base | 2104 /* there might still be other problems, therefore base |
2104 » » remains zero here for the same reason */ | 2105 remains zero here for the same reason */ |
2105 » } | 2106 } |
2106 » if (str == start) | 2107 if (str == start) |
2107 » » goto onError; | 2108 goto onError; |
2108 » if (sign < 0) | 2109 if (sign < 0) |
2109 » » Py_SIZE(z) = -(Py_SIZE(z)); | 2110 Py_SIZE(z) = -(Py_SIZE(z)); |
2110 » while (*str && isspace(Py_CHARMASK(*str))) | 2111 while (*str && isspace(Py_CHARMASK(*str))) |
2111 » » str++; | 2112 str++; |
2112 » if (*str != '\0') | 2113 if (*str != '\0') |
2113 » » goto onError; | 2114 goto onError; |
2114 » if (pend) | 2115 if (pend) |
2115 » » *pend = str; | 2116 *pend = str; |
2116 » long_normalize(z); | 2117 long_normalize(z); |
2117 » return (PyObject *) maybe_small_long(z); | 2118 return (PyObject *) maybe_small_long(z); |
2118 | 2119 |
2119 onError: | 2120 onError: |
2120 » Py_XDECREF(z); | 2121 Py_XDECREF(z); |
2121 » slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; | 2122 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200; |
2122 » strobj = PyUnicode_FromStringAndSize(orig_str, slen); | 2123 strobj = PyUnicode_FromStringAndSize(orig_str, slen); |
2123 » if (strobj == NULL) | 2124 if (strobj == NULL) |
2124 » » return NULL; | 2125 return NULL; |
2125 » PyErr_Format(PyExc_ValueError, | 2126 PyErr_Format(PyExc_ValueError, |
2126 » » "invalid literal for int() with base %d: %R", | 2127 "invalid literal for int() with base %d: %R", |
2127 » » base, strobj); | 2128 base, strobj); |
2128 » Py_DECREF(strobj); | 2129 Py_DECREF(strobj); |
2129 » return NULL; | 2130 return NULL; |
2130 } | 2131 } |
2131 | 2132 |
2132 PyObject * | 2133 PyObject * |
2133 PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) | 2134 PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base) |
2134 { | 2135 { |
2135 » PyObject *result; | 2136 PyObject *result; |
2136 » char *buffer = (char *)PyMem_MALLOC(length+1); | 2137 char *buffer = (char *)PyMem_MALLOC(length+1); |
2137 | 2138 |
2138 » if (buffer == NULL) | 2139 if (buffer == NULL) |
2139 » » return NULL; | 2140 return NULL; |
2140 | 2141 |
2141 » if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) { | 2142 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) { |
2142 » » PyMem_FREE(buffer); | 2143 PyMem_FREE(buffer); |
2143 » » return NULL; | 2144 return NULL; |
2144 » } | 2145 } |
2145 » result = PyLong_FromString(buffer, NULL, base); | 2146 result = PyLong_FromString(buffer, NULL, base); |
2146 » PyMem_FREE(buffer); | 2147 PyMem_FREE(buffer); |
2147 » return result; | 2148 return result; |
2148 } | 2149 } |
2149 | 2150 |
2150 /* forward */ | 2151 /* forward */ |
2151 static PyLongObject *x_divrem | 2152 static PyLongObject *x_divrem |
2152 » (PyLongObject *, PyLongObject *, PyLongObject **); | 2153 (PyLongObject *, PyLongObject *, PyLongObject **); |
2153 static PyObject *long_long(PyObject *v); | 2154 static PyObject *long_long(PyObject *v); |
2154 | 2155 |
2155 /* Long division with remainder, top-level routine */ | 2156 /* Long division with remainder, top-level routine */ |
2156 | 2157 |
2157 static int | 2158 static int |
2158 long_divrem(PyLongObject *a, PyLongObject *b, | 2159 long_divrem(PyLongObject *a, PyLongObject *b, |
2159 » PyLongObject **pdiv, PyLongObject **prem) | 2160 PyLongObject **pdiv, PyLongObject **prem) |
2160 { | 2161 { |
2161 » Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); | 2162 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
2162 » PyLongObject *z; | 2163 PyLongObject *z; |
2163 | 2164 |
2164 » if (size_b == 0) { | 2165 if (size_b == 0) { |
2165 » » PyErr_SetString(PyExc_ZeroDivisionError, | 2166 PyErr_SetString(PyExc_ZeroDivisionError, |
2166 » » » » "integer division or modulo by zero"); | 2167 "integer division or modulo by zero"); |
2167 » » return -1; | 2168 return -1; |
2168 » } | 2169 } |
2169 » if (size_a < size_b || | 2170 if (size_a < size_b || |
2170 » (size_a == size_b && | 2171 (size_a == size_b && |
2171 » a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { | 2172 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
2172 » » /* |a| < |b|. */ | 2173 /* |a| < |b|. */ |
2173 » » *pdiv = (PyLongObject*)PyLong_FromLong(0); | 2174 *pdiv = (PyLongObject*)PyLong_FromLong(0); |
2174 » » if (*pdiv == NULL) | 2175 if (*pdiv == NULL) |
2175 » » » return -1; | 2176 return -1; |
2176 » » Py_INCREF(a); | 2177 Py_INCREF(a); |
2177 » » *prem = (PyLongObject *) a; | 2178 *prem = (PyLongObject *) a; |
2178 » » return 0; | 2179 return 0; |
2179 » } | 2180 } |
2180 » if (size_b == 1) { | 2181 if (size_b == 1) { |
2181 » » digit rem = 0; | 2182 digit rem = 0; |
2182 » » z = divrem1(a, b->ob_digit[0], &rem); | 2183 z = divrem1(a, b->ob_digit[0], &rem); |
2183 » » if (z == NULL) | 2184 if (z == NULL) |
2184 » » » return -1; | 2185 return -1; |
2185 » » *prem = (PyLongObject *) PyLong_FromLong((long)rem); | 2186 *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
2186 » » if (*prem == NULL) { | 2187 if (*prem == NULL) { |
2187 » » » Py_DECREF(z); | 2188 Py_DECREF(z); |
2188 » » » return -1; | 2189 return -1; |
2189 » » } | 2190 } |
2190 » } | 2191 } |
2191 » else { | 2192 else { |
2192 » » z = x_divrem(a, b, prem); | 2193 z = x_divrem(a, b, prem); |
2193 » » if (z == NULL) | 2194 if (z == NULL) |
2194 » » » return -1; | 2195 return -1; |
2195 » } | 2196 } |
2196 » /* Set the signs. | 2197 /* Set the signs. |
2197 » The quotient z has the sign of a*b; | 2198 The quotient z has the sign of a*b; |
2198 » the remainder r has the sign of a, | 2199 the remainder r has the sign of a, |
2199 » so a = b*z + r. */ | 2200 so a = b*z + r. */ |
2200 » if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) | 2201 if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) |
2201 » » NEGATE(z); | 2202 NEGATE(z); |
2202 » if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) | 2203 if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) |
2203 » » NEGATE(*prem); | 2204 NEGATE(*prem); |
2204 » *pdiv = maybe_small_long(z); | 2205 *pdiv = maybe_small_long(z); |
2205 » return 0; | 2206 return 0; |
2206 } | 2207 } |
2207 | 2208 |
2208 /* Unsigned long division with remainder -- the algorithm. The arguments v1 | 2209 /* Unsigned long division with remainder -- the algorithm. The arguments v1 |
2209 and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */ | 2210 and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */ |
2210 | 2211 |
2211 static PyLongObject * | 2212 static PyLongObject * |
2212 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) | 2213 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
2213 { | 2214 { |
2214 » PyLongObject *v, *w, *a; | 2215 PyLongObject *v, *w, *a; |
2215 » Py_ssize_t i, k, size_v, size_w; | 2216 Py_ssize_t i, k, size_v, size_w; |
2216 » int d; | 2217 int d; |
2217 » digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; | 2218 digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak; |
2218 » twodigits vv; | 2219 twodigits vv; |
2219 » sdigit zhi; | 2220 sdigit zhi; |
2220 » stwodigits z; | 2221 stwodigits z; |
2221 | 2222 |
2222 » /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd | 2223 /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd |
2223 » edn.), section 4.3.1, Algorithm D], except that we don't explicitly | 2224 edn.), section 4.3.1, Algorithm D], except that we don't explicitly |
2224 » handle the special case when the initial estimate q for a quotient | 2225 handle the special case when the initial estimate q for a quotient |
2225 » digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and | 2226 digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and |
2226 » that won't overflow a digit. */ | 2227 that won't overflow a digit. */ |
2227 | 2228 |
2228 » /* allocate space; w will also be used to hold the final remainder */ | 2229 /* allocate space; w will also be used to hold the final remainder */ |
2229 » size_v = ABS(Py_SIZE(v1)); | 2230 size_v = ABS(Py_SIZE(v1)); |
2230 » size_w = ABS(Py_SIZE(w1)); | 2231 size_w = ABS(Py_SIZE(w1)); |
2231 » assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ | 2232 assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */ |
2232 » v = _PyLong_New(size_v+1); | 2233 v = _PyLong_New(size_v+1); |
2233 » if (v == NULL) { | 2234 if (v == NULL) { |
2234 » » *prem = NULL; | 2235 *prem = NULL; |
2235 » » return NULL; | 2236 return NULL; |
2236 » } | 2237 } |
2237 » w = _PyLong_New(size_w); | 2238 w = _PyLong_New(size_w); |
2238 » if (w == NULL) { | 2239 if (w == NULL) { |
2239 » » Py_DECREF(v); | 2240 Py_DECREF(v); |
2240 » » *prem = NULL; | 2241 *prem = NULL; |
2241 » » return NULL; | 2242 return NULL; |
2242 » } | 2243 } |
2243 | 2244 |
2244 » /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. | 2245 /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2. |
2245 » shift v1 left by the same amount. Results go into w and v. */ | 2246 shift v1 left by the same amount. Results go into w and v. */ |
2246 » d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]); | 2247 d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]); |
2247 » carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); | 2248 carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d); |
2248 » assert(carry == 0); | 2249 assert(carry == 0); |
2249 » carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); | 2250 carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d); |
2250 » if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { | 2251 if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) { |
2251 » » v->ob_digit[size_v] = carry; | 2252 v->ob_digit[size_v] = carry; |
2252 » » size_v++; | 2253 size_v++; |
2253 » } | 2254 } |
2254 | 2255 |
2255 » /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has | 2256 /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has |
2256 » at most (and usually exactly) k = size_v - size_w digits. */ | 2257 at most (and usually exactly) k = size_v - size_w digits. */ |
2257 » k = size_v - size_w; | 2258 k = size_v - size_w; |
2258 » assert(k >= 0); | 2259 assert(k >= 0); |
2259 » a = _PyLong_New(k); | 2260 a = _PyLong_New(k); |
2260 » if (a == NULL) { | 2261 if (a == NULL) { |
2261 » » Py_DECREF(w); | 2262 Py_DECREF(w); |
2262 » » Py_DECREF(v); | 2263 Py_DECREF(v); |
2263 » » *prem = NULL; | 2264 *prem = NULL; |
2264 » » return NULL; | 2265 return NULL; |
2265 » } | 2266 } |
2266 » v0 = v->ob_digit; | 2267 v0 = v->ob_digit; |
2267 » w0 = w->ob_digit; | 2268 w0 = w->ob_digit; |
2268 » wm1 = w0[size_w-1]; | 2269 wm1 = w0[size_w-1]; |
2269 » wm2 = w0[size_w-2]; | 2270 wm2 = w0[size_w-2]; |
2270 » for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { | 2271 for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) { |
2271 » » /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving | 2272 /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving |
2272 » » single-digit quotient q, remainder in vk[0:size_w]. */ | 2273 single-digit quotient q, remainder in vk[0:size_w]. */ |
2273 | 2274 |
2274 » » SIGCHECK({ | 2275 SIGCHECK({ |
2275 » » » Py_DECREF(a); | 2276 Py_DECREF(a); |
2276 » » » Py_DECREF(w); | 2277 Py_DECREF(w); |
2277 » » » Py_DECREF(v); | 2278 Py_DECREF(v); |
2278 » » » *prem = NULL; | 2279 *prem = NULL; |
2279 » » » return NULL; | 2280 return NULL; |
2280 » » }) | 2281 }); |
2281 | 2282 |
2282 » » /* estimate quotient digit q; may overestimate by 1 (rare) */ | 2283 /* estimate quotient digit q; may overestimate by 1 (rare) */ |
2283 » » vtop = vk[size_w]; | 2284 vtop = vk[size_w]; |
2284 » » assert(vtop <= wm1); | 2285 assert(vtop <= wm1); |
2285 » » vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; | 2286 vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1]; |
2286 » » q = (digit)(vv / wm1); | 2287 q = (digit)(vv / wm1); |
2287 » » r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ | 2288 r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */ |
2288 » » while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) | 2289 while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT) |
2289 » » » » » | vk[size_w-2])) { | 2290 | vk[size_w-2])) { |
2290 » » » --q; | 2291 --q; |
2291 » » » r += wm1; | 2292 r += wm1; |
2292 » » » if (r >= PyLong_BASE) | 2293 if (r >= PyLong_BASE) |
2293 » » » » break; | 2294 break; |
2294 » » } | 2295 } |
2295 » » assert(q <= PyLong_BASE); | 2296 assert(q <= PyLong_BASE); |
2296 | 2297 |
2297 » » /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ | 2298 /* subtract q*w0[0:size_w] from vk[0:size_w+1] */ |
2298 » » zhi = 0; | 2299 zhi = 0; |
2299 » » for (i = 0; i < size_w; ++i) { | 2300 for (i = 0; i < size_w; ++i) { |
2300 » » » /* invariants: -PyLong_BASE <= -q <= zhi <= 0; | 2301 /* invariants: -PyLong_BASE <= -q <= zhi <= 0; |
2301 » » » -PyLong_BASE * q <= z < PyLong_BASE */ | 2302 -PyLong_BASE * q <= z < PyLong_BASE */ |
2302 » » » z = (sdigit)vk[i] + zhi - | 2303 z = (sdigit)vk[i] + zhi - |
2303 » » » » (stwodigits)q * (stwodigits)w0[i]; | 2304 (stwodigits)q * (stwodigits)w0[i]; |
2304 » » » vk[i] = (digit)z & PyLong_MASK; | 2305 vk[i] = (digit)z & PyLong_MASK; |
2305 » » » zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, | 2306 zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits, |
2306 » » » » » » » z, PyLong_SHIFT); | 2307 z, PyLong_SHIFT); |
2307 » » } | 2308 } |
2308 | 2309 |
2309 » » /* add w back if q was too large (this branch taken rarely) */ | 2310 /* add w back if q was too large (this branch taken rarely) */ |
2310 » » assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); | 2311 assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0); |
2311 » » if ((sdigit)vtop + zhi < 0) { | 2312 if ((sdigit)vtop + zhi < 0) { |
2312 » » » carry = 0; | 2313 carry = 0; |
2313 » » » for (i = 0; i < size_w; ++i) { | 2314 for (i = 0; i < size_w; ++i) { |
2314 » » » » carry += vk[i] + w0[i]; | 2315 carry += vk[i] + w0[i]; |
2315 » » » » vk[i] = carry & PyLong_MASK; | 2316 vk[i] = carry & PyLong_MASK; |
2316 » » » » carry >>= PyLong_SHIFT; | 2317 carry >>= PyLong_SHIFT; |
2317 » » » } | 2318 } |
2318 » » » --q; | 2319 --q; |
2319 » » } | 2320 } |
2320 | 2321 |
2321 » » /* store quotient digit */ | 2322 /* store quotient digit */ |
2322 » » assert(q < PyLong_BASE); | 2323 assert(q < PyLong_BASE); |
2323 » » *--ak = q; | 2324 *--ak = q; |
2324 » } | 2325 } |
2325 | 2326 |
2326 » /* unshift remainder; we reuse w to store the result */ | 2327 /* unshift remainder; we reuse w to store the result */ |
2327 » carry = v_rshift(w0, v0, size_w, d); | 2328 carry = v_rshift(w0, v0, size_w, d); |
2328 » assert(carry==0); | 2329 assert(carry==0); |
2329 » Py_DECREF(v); | 2330 Py_DECREF(v); |
2330 | 2331 |
2331 » *prem = long_normalize(w); | 2332 *prem = long_normalize(w); |
2332 » return long_normalize(a); | 2333 return long_normalize(a); |
2333 } | 2334 } |
2334 | 2335 |
2335 /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= | 2336 /* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <= |
2336 abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is | 2337 abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is |
2337 rounded to DBL_MANT_DIG significant bits using round-half-to-even. | 2338 rounded to DBL_MANT_DIG significant bits using round-half-to-even. |
2338 If a == 0, return 0.0 and set *e = 0. If the resulting exponent | 2339 If a == 0, return 0.0 and set *e = 0. If the resulting exponent |
2339 e is larger than PY_SSIZE_T_MAX, raise OverflowError and return | 2340 e is larger than PY_SSIZE_T_MAX, raise OverflowError and return |
2340 -1.0. */ | 2341 -1.0. */ |
2341 | 2342 |
2342 /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ | 2343 /* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */ |
2343 #if DBL_MANT_DIG == 53 | 2344 #if DBL_MANT_DIG == 53 |
2344 #define EXP2_DBL_MANT_DIG 9007199254740992.0 | 2345 #define EXP2_DBL_MANT_DIG 9007199254740992.0 |
2345 #else | 2346 #else |
2346 #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) | 2347 #define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG)) |
2347 #endif | 2348 #endif |
2348 | 2349 |
2349 double | 2350 double |
2350 _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) | 2351 _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e) |
2351 { | 2352 { |
2352 » Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; | 2353 Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size; |
2353 » /* See below for why x_digits is always large enough. */ | 2354 /* See below for why x_digits is always large enough. */ |
2354 » digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; | 2355 digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT]; |
2355 » double dx; | 2356 double dx; |
2356 » /* Correction term for round-half-to-even rounding. For a digit x, | 2357 /* Correction term for round-half-to-even rounding. For a digit x, |
2357 » "x + half_even_correction[x & 7]" gives x rounded to the nearest | 2358 "x + half_even_correction[x & 7]" gives x rounded to the nearest |
2358 » multiple of 4, rounding ties to a multiple of 8. */ | 2359 multiple of 4, rounding ties to a multiple of 8. */ |
2359 » static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1}; | 2360 static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1}; |
2360 | 2361 |
2361 » a_size = ABS(Py_SIZE(a)); | 2362 a_size = ABS(Py_SIZE(a)); |
2362 » if (a_size == 0) { | 2363 if (a_size == 0) { |
2363 » » /* Special case for 0: significand 0.0, exponent 0. */ | 2364 /* Special case for 0: significand 0.0, exponent 0. */ |
2364 » » *e = 0; | 2365 *e = 0; |
2365 » » return 0.0; | 2366 return 0.0; |
2366 » } | 2367 } |
2367 » a_bits = bits_in_digit(a->ob_digit[a_size-1]); | 2368 a_bits = bits_in_digit(a->ob_digit[a_size-1]); |
2368 » /* The following is an overflow-free version of the check | 2369 /* The following is an overflow-free version of the check |
2369 » "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ | 2370 "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */ |
2370 » if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && | 2371 if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && |
2371 » (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || | 2372 (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || |
2372 » a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) | 2373 a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1)) |
2373 » » goto overflow; | 2374 goto overflow; |
2374 » a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; | 2375 a_bits = (a_size - 1) * PyLong_SHIFT + a_bits; |
2375 | 2376 |
2376 » /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] | 2377 /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size] |
2377 » (shifting left if a_bits <= DBL_MANT_DIG + 2). | 2378 (shifting left if a_bits <= DBL_MANT_DIG + 2). |
2378 | 2379 |
2379 » Number of digits needed for result: write // for floor division. | 2380 Number of digits needed for result: write // for floor division. |
2380 » Then if shifting left, we end up using | 2381 Then if shifting left, we end up using |
2381 | 2382 |
2382 » 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT | 2383 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT |
2383 | 2384 |
2384 » digits. If shifting right, we use | 2385 digits. If shifting right, we use |
2385 | 2386 |
2386 » a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT | 2387 a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT |
2387 | 2388 |
2388 » digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with | 2389 digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with |
2389 » the inequalities | 2390 the inequalities |
2390 | 2391 |
2391 » m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT | 2392 m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT |
2392 » m // PyLong_SHIFT - n // PyLong_SHIFT <= | 2393 m // PyLong_SHIFT - n // PyLong_SHIFT <= |
2393 » 1 + (m - n - 1) // PyLong_SHIFT, | 2394 1 + (m - n - 1) // PyLong_SHIFT, |
2394 | 2395 |
2395 » valid for any integers m and n, we find that x_size satisfies | 2396 valid for any integers m and n, we find that x_size satisfies |
2396 | 2397 |
2397 » x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT | 2398 x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT |
2398 | 2399 |
2399 » in both cases. | 2400 in both cases. |
2400 » */ | 2401 */ |
2401 » if (a_bits <= DBL_MANT_DIG + 2) { | 2402 if (a_bits <= DBL_MANT_DIG + 2) { |
2402 » » shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; | 2403 shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT; |
2403 » » shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; | 2404 shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT; |
2404 » » x_size = 0; | 2405 x_size = 0; |
2405 » » while (x_size < shift_digits) | 2406 while (x_size < shift_digits) |
2406 » » » x_digits[x_size++] = 0; | 2407 x_digits[x_size++] = 0; |
2407 » » rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, | 2408 rem = v_lshift(x_digits + x_size, a->ob_digit, a_size, |
2408 » » » shift_bits); | 2409 (int)shift_bits); |
2409 » » x_size += a_size; | 2410 x_size += a_size; |
2410 » » x_digits[x_size++] = rem; | 2411 x_digits[x_size++] = rem; |
2411 » } | 2412 } |
2412 » else { | 2413 else { |
2413 » » shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; | 2414 shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT; |
2414 » » shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; | 2415 shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT; |
2415 » » rem = v_rshift(x_digits, a->ob_digit + shift_digits, | 2416 rem = v_rshift(x_digits, a->ob_digit + shift_digits, |
2416 » » » a_size - shift_digits, shift_bits); | 2417 a_size - shift_digits, (int)shift_bits); |
2417 » » x_size = a_size - shift_digits; | 2418 x_size = a_size - shift_digits; |
2418 » » /* For correct rounding below, we need the least significant | 2419 /* For correct rounding below, we need the least significant |
2419 » » bit of x to be 'sticky' for this shift: if any of the bits | 2420 bit of x to be 'sticky' for this shift: if any of the bits |
2420 » » shifted out was nonzero, we set the least significant bit | 2421 shifted out was nonzero, we set the least significant bit |
2421 » » of x. */ | 2422 of x. */ |
2422 » » if (rem) | 2423 if (rem) |
2423 » » » x_digits[0] |= 1; | 2424 x_digits[0] |= 1; |
2424 » » else | 2425 else |
2425 » » » while (shift_digits > 0) | 2426 while (shift_digits > 0) |
2426 » » » » if (a->ob_digit[--shift_digits]) { | 2427 if (a->ob_digit[--shift_digits]) { |
2427 » » » » » x_digits[0] |= 1; | 2428 x_digits[0] |= 1; |
2428 » » » » » break; | 2429 break; |
2429 » » » » } | 2430 } |
2430 » } | 2431 } |
2431 » assert(1 <= x_size && x_size <= sizeof(x_digits)/sizeof(digit)); | 2432 assert(1 <= x_size && |
2432 | 2433 x_size <= (Py_ssize_t)(sizeof(x_digits)/sizeof(digit))); |
2433 » /* Round, and convert to double. */ | 2434 |
2434 » x_digits[0] += half_even_correction[x_digits[0] & 7]; | 2435 /* Round, and convert to double. */ |
2435 » dx = x_digits[--x_size]; | 2436 x_digits[0] += half_even_correction[x_digits[0] & 7]; |
2436 » while (x_size > 0) | 2437 dx = x_digits[--x_size]; |
2437 » » dx = dx * PyLong_BASE + x_digits[--x_size]; | 2438 while (x_size > 0) |
2438 | 2439 dx = dx * PyLong_BASE + x_digits[--x_size]; |
2439 » /* Rescale; make correction if result is 1.0. */ | 2440 |
2440 » dx /= 4.0 * EXP2_DBL_MANT_DIG; | 2441 /* Rescale; make correction if result is 1.0. */ |
2441 » if (dx == 1.0) { | 2442 dx /= 4.0 * EXP2_DBL_MANT_DIG; |
2442 » » if (a_bits == PY_SSIZE_T_MAX) | 2443 if (dx == 1.0) { |
2443 » » » goto overflow; | 2444 if (a_bits == PY_SSIZE_T_MAX) |
2444 » » dx = 0.5; | 2445 goto overflow; |
2445 » » a_bits += 1; | 2446 dx = 0.5; |
2446 » } | 2447 a_bits += 1; |
2447 | 2448 } |
2448 » *e = a_bits; | 2449 |
2449 » return Py_SIZE(a) < 0 ? -dx : dx; | 2450 *e = a_bits; |
| 2451 return Py_SIZE(a) < 0 ? -dx : dx; |
2450 | 2452 |
2451 overflow: | 2453 overflow: |
2452 » /* exponent > PY_SSIZE_T_MAX */ | 2454 /* exponent > PY_SSIZE_T_MAX */ |
2453 » PyErr_SetString(PyExc_OverflowError, | 2455 PyErr_SetString(PyExc_OverflowError, |
2454 » » » "huge integer: number of bits overflows a Py_ssize_t"); | 2456 "huge integer: number of bits overflows a Py_ssize_t"); |
2455 » *e = 0; | 2457 *e = 0; |
2456 » return -1.0; | 2458 return -1.0; |
2457 } | 2459 } |
2458 | 2460 |
2459 /* Get a C double from a long int object. Rounds to the nearest double, | 2461 /* Get a C double from a long int object. Rounds to the nearest double, |
2460 using the round-half-to-even rule in the case of a tie. */ | 2462 using the round-half-to-even rule in the case of a tie. */ |
2461 | 2463 |
2462 double | 2464 double |
2463 PyLong_AsDouble(PyObject *v) | 2465 PyLong_AsDouble(PyObject *v) |
2464 { | 2466 { |
2465 » Py_ssize_t exponent; | 2467 Py_ssize_t exponent; |
2466 » double x; | 2468 double x; |
2467 | 2469 |
2468 » if (v == NULL || !PyLong_Check(v)) { | 2470 if (v == NULL || !PyLong_Check(v)) { |
2469 » » PyErr_BadInternalCall(); | 2471 PyErr_BadInternalCall(); |
2470 » » return -1.0; | 2472 return -1.0; |
2471 » } | 2473 } |
2472 » x = _PyLong_Frexp((PyLongObject *)v, &exponent); | 2474 x = _PyLong_Frexp((PyLongObject *)v, &exponent); |
2473 » if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { | 2475 if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) { |
2474 » » PyErr_SetString(PyExc_OverflowError, | 2476 PyErr_SetString(PyExc_OverflowError, |
2475 » » » » "long int too large to convert to float"); | 2477 "long int too large to convert to float"); |
2476 » » return -1.0; | 2478 return -1.0; |
2477 » } | 2479 } |
2478 » return ldexp(x, exponent); | 2480 return ldexp(x, (int)exponent); |
2479 } | 2481 } |
2480 | 2482 |
2481 /* Methods */ | 2483 /* Methods */ |
2482 | 2484 |
2483 static void | 2485 static void |
2484 long_dealloc(PyObject *v) | 2486 long_dealloc(PyObject *v) |
2485 { | 2487 { |
2486 » Py_TYPE(v)->tp_free(v); | 2488 Py_TYPE(v)->tp_free(v); |
2487 } | 2489 } |
2488 | 2490 |
2489 static int | 2491 static int |
2490 long_compare(PyLongObject *a, PyLongObject *b) | 2492 long_compare(PyLongObject *a, PyLongObject *b) |
2491 { | 2493 { |
2492 » Py_ssize_t sign; | 2494 Py_ssize_t sign; |
2493 | 2495 |
2494 » if (Py_SIZE(a) != Py_SIZE(b)) { | 2496 if (Py_SIZE(a) != Py_SIZE(b)) { |
2495 » » if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0) | 2497 sign = Py_SIZE(a) - Py_SIZE(b); |
2496 » » » sign = 0; | 2498 } |
2497 » » else | 2499 else { |
2498 » » » sign = Py_SIZE(a) - Py_SIZE(b); | 2500 Py_ssize_t i = ABS(Py_SIZE(a)); |
2499 » } | 2501 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
2500 » else { | 2502 ; |
2501 » » Py_ssize_t i = ABS(Py_SIZE(a)); | 2503 if (i < 0) |
2502 » » while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | 2504 sign = 0; |
2503 » » » ; | 2505 else { |
2504 » » if (i < 0) | 2506 sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i]; |
2505 » » » sign = 0; | 2507 if (Py_SIZE(a) < 0) |
2506 » » else { | 2508 sign = -sign; |
2507 » » » sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i]; | 2509 } |
2508 » » » if (Py_SIZE(a) < 0) | 2510 } |
2509 » » » » sign = -sign; | 2511 return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
2510 » » } | |
2511 » } | |
2512 » return sign < 0 ? -1 : sign > 0 ? 1 : 0; | |
2513 } | 2512 } |
2514 | 2513 |
2515 #define TEST_COND(cond) \ | 2514 #define TEST_COND(cond) \ |
2516 » ((cond) ? Py_True : Py_False) | 2515 ((cond) ? Py_True : Py_False) |
2517 | 2516 |
2518 static PyObject * | 2517 static PyObject * |
2519 long_richcompare(PyObject *self, PyObject *other, int op) | 2518 long_richcompare(PyObject *self, PyObject *other, int op) |
2520 { | 2519 { |
2521 » int result; | 2520 int result; |
2522 » PyObject *v; | 2521 PyObject *v; |
2523 » CHECK_BINOP(self, other); | 2522 CHECK_BINOP(self, other); |
2524 » if (self == other) | 2523 if (self == other) |
2525 » » result = 0; | 2524 result = 0; |
2526 » else | 2525 else |
2527 » » result = long_compare((PyLongObject*)self, (PyLongObject*)other)
; | 2526 result = long_compare((PyLongObject*)self, (PyLongObject*)other); |
2528 » /* Convert the return value to a Boolean */ | 2527 /* Convert the return value to a Boolean */ |
2529 » switch (op) { | 2528 switch (op) { |
2530 » case Py_EQ: | 2529 case Py_EQ: |
2531 » » v = TEST_COND(result == 0); | 2530 v = TEST_COND(result == 0); |
2532 » » break; | 2531 break; |
2533 » case Py_NE: | 2532 case Py_NE: |
2534 » » v = TEST_COND(result != 0); | 2533 v = TEST_COND(result != 0); |
2535 » » break; | 2534 break; |
2536 » case Py_LE: | 2535 case Py_LE: |
2537 » » v = TEST_COND(result <= 0); | 2536 v = TEST_COND(result <= 0); |
2538 » » break; | 2537 break; |
2539 » case Py_GE: | 2538 case Py_GE: |
2540 » » v = TEST_COND(result >= 0); | 2539 v = TEST_COND(result >= 0); |
2541 » » break; | 2540 break; |
2542 » case Py_LT: | 2541 case Py_LT: |
2543 » » v = TEST_COND(result == -1); | 2542 v = TEST_COND(result == -1); |
2544 » » break; | 2543 break; |
2545 » case Py_GT: | 2544 case Py_GT: |
2546 » » v = TEST_COND(result == 1); | 2545 v = TEST_COND(result == 1); |
2547 » » break; | 2546 break; |
2548 » default: | 2547 default: |
2549 » » PyErr_BadArgument(); | 2548 PyErr_BadArgument(); |
2550 » » return NULL; | 2549 return NULL; |
2551 » } | 2550 } |
2552 » Py_INCREF(v); | 2551 Py_INCREF(v); |
2553 » return v; | 2552 return v; |
2554 } | 2553 } |
2555 | 2554 |
2556 static long | 2555 static long |
2557 long_hash(PyLongObject *v) | 2556 long_hash(PyLongObject *v) |
2558 { | 2557 { |
2559 » unsigned long x; | 2558 unsigned long x; |
2560 » Py_ssize_t i; | 2559 Py_ssize_t i; |
2561 » int sign; | 2560 int sign; |
2562 | 2561 |
2563 » i = Py_SIZE(v); | 2562 i = Py_SIZE(v); |
2564 » switch(i) { | 2563 switch(i) { |
2565 » case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; | 2564 case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0]; |
2566 » case 0: return 0; | 2565 case 0: return 0; |
2567 » case 1: return v->ob_digit[0]; | 2566 case 1: return v->ob_digit[0]; |
2568 » } | 2567 } |
2569 » sign = 1; | 2568 sign = 1; |
2570 » x = 0; | 2569 x = 0; |
2571 » if (i < 0) { | 2570 if (i < 0) { |
2572 » » sign = -1; | 2571 sign = -1; |
2573 » » i = -(i); | 2572 i = -(i); |
2574 » } | 2573 } |
2575 » while (--i >= 0) { | 2574 while (--i >= 0) { |
2576 » » /* Here x is a quantity in the range [0, _PyHASH_MASK); we | 2575 /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we |
2577 » » want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo | 2576 want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo |
2578 » » _PyHASH_MASK. | 2577 _PyHASH_MODULUS. |
2579 | 2578 |
2580 » » The computation of x * 2**PyLong_SHIFT % _PyHASH_MASK | 2579 The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS |
2581 » » amounts to a rotation of the bits of x. To see this, write | 2580 amounts to a rotation of the bits of x. To see this, write |
2582 | 2581 |
2583 » » x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z | 2582 x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z |
2584 | 2583 |
2585 » » where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top | 2584 where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top |
2586 » » PyLong_SHIFT bits of x (those that are shifted out of the | 2585 PyLong_SHIFT bits of x (those that are shifted out of the |
2587 » » original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & | 2586 original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) & |
2588 » » _PyHASH_MASK gives the bottom _PyHASH_BITS - PyLong_SHIFT | 2587 _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT |
2589 » » bits of x, shifted up. Then since 2**_PyHASH_BITS is | 2588 bits of x, shifted up. Then since 2**_PyHASH_BITS is |
2590 » » congruent to 1 modulo _PyHASH_MASK, y*2**_PyHASH_BITS is | 2589 congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is |
2591 » » congruent to y modulo _PyHASH_MASK. So | 2590 congruent to y modulo _PyHASH_MODULUS. So |
2592 | 2591 |
2593 » » x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MASK). | 2592 x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS). |
2594 | 2593 |
2595 » » The right-hand side is just the result of rotating the | 2594 The right-hand side is just the result of rotating the |
2596 » » _PyHASH_BITS bits of x left by PyLong_SHIFT places; since | 2595 _PyHASH_BITS bits of x left by PyLong_SHIFT places; since |
2597 » » not all _PyHASH_BITS bits of x are 1s, the same is true | 2596 not all _PyHASH_BITS bits of x are 1s, the same is true |
2598 » » after rotation, so 0 <= y+z < _PyHASH_MASK and y + z is the | 2597 after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is |
2599 » » reduction of x*2**PyLong_SHIFT modulo _PyHASH_MASK. */ | 2598 the reduction of x*2**PyLong_SHIFT modulo |
2600 » » x = ((x << PyLong_SHIFT) & _PyHASH_MASK) | | 2599 _PyHASH_MODULUS. */ |
2601 » » » (x >> (_PyHASH_BITS - PyLong_SHIFT)); | 2600 x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) | |
2602 » » x += v->ob_digit[i]; | 2601 (x >> (_PyHASH_BITS - PyLong_SHIFT)); |
2603 » » if (x >= _PyHASH_MASK) | 2602 x += v->ob_digit[i]; |
2604 » » » x -= _PyHASH_MASK; | 2603 if (x >= _PyHASH_MODULUS) |
2605 » } | 2604 x -= _PyHASH_MODULUS; |
2606 » x = x * sign; | 2605 } |
2607 » if (x == (unsigned long)-1) | 2606 x = x * sign; |
2608 » » x = (unsigned long)-2; | 2607 if (x == (unsigned long)-1) |
2609 » return (long)x; | 2608 x = (unsigned long)-2; |
| 2609 return (long)x; |
2610 } | 2610 } |
2611 | 2611 |
2612 | 2612 |
2613 /* Add the absolute values of two long integers. */ | 2613 /* Add the absolute values of two long integers. */ |
2614 | 2614 |
2615 static PyLongObject * | 2615 static PyLongObject * |
2616 x_add(PyLongObject *a, PyLongObject *b) | 2616 x_add(PyLongObject *a, PyLongObject *b) |
2617 { | 2617 { |
2618 » Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); | 2618 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
2619 » PyLongObject *z; | 2619 PyLongObject *z; |
2620 » Py_ssize_t i; | 2620 Py_ssize_t i; |
2621 » digit carry = 0; | 2621 digit carry = 0; |
2622 | 2622 |
2623 » /* Ensure a is the larger of the two: */ | 2623 /* Ensure a is the larger of the two: */ |
2624 » if (size_a < size_b) { | 2624 if (size_a < size_b) { |
2625 » » { PyLongObject *temp = a; a = b; b = temp; } | 2625 { PyLongObject *temp = a; a = b; b = temp; } |
2626 » » { Py_ssize_t size_temp = size_a; | 2626 { Py_ssize_t size_temp = size_a; |
2627 » » size_a = size_b; | 2627 size_a = size_b; |
2628 » » size_b = size_temp; } | 2628 size_b = size_temp; } |
2629 » } | 2629 } |
2630 » z = _PyLong_New(size_a+1); | 2630 z = _PyLong_New(size_a+1); |
2631 » if (z == NULL) | 2631 if (z == NULL) |
2632 » » return NULL; | 2632 return NULL; |
2633 » for (i = 0; i < size_b; ++i) { | 2633 for (i = 0; i < size_b; ++i) { |
2634 » » carry += a->ob_digit[i] + b->ob_digit[i]; | 2634 carry += a->ob_digit[i] + b->ob_digit[i]; |
2635 » » z->ob_digit[i] = carry & PyLong_MASK; | 2635 z->ob_digit[i] = carry & PyLong_MASK; |
2636 » » carry >>= PyLong_SHIFT; | 2636 carry >>= PyLong_SHIFT; |
2637 » } | 2637 } |
2638 » for (; i < size_a; ++i) { | 2638 for (; i < size_a; ++i) { |
2639 » » carry += a->ob_digit[i]; | 2639 carry += a->ob_digit[i]; |
2640 » » z->ob_digit[i] = carry & PyLong_MASK; | 2640 z->ob_digit[i] = carry & PyLong_MASK; |
2641 » » carry >>= PyLong_SHIFT; | 2641 carry >>= PyLong_SHIFT; |
2642 » } | 2642 } |
2643 » z->ob_digit[i] = carry; | 2643 z->ob_digit[i] = carry; |
2644 » return long_normalize(z); | 2644 return long_normalize(z); |
2645 } | 2645 } |
2646 | 2646 |
2647 /* Subtract the absolute values of two integers. */ | 2647 /* Subtract the absolute values of two integers. */ |
2648 | 2648 |
2649 static PyLongObject * | 2649 static PyLongObject * |
2650 x_sub(PyLongObject *a, PyLongObject *b) | 2650 x_sub(PyLongObject *a, PyLongObject *b) |
2651 { | 2651 { |
2652 » Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); | 2652 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b)); |
2653 » PyLongObject *z; | 2653 PyLongObject *z; |
2654 » Py_ssize_t i; | 2654 Py_ssize_t i; |
2655 » int sign = 1; | 2655 int sign = 1; |
2656 » digit borrow = 0; | 2656 digit borrow = 0; |
2657 | 2657 |
2658 » /* Ensure a is the larger of the two: */ | 2658 /* Ensure a is the larger of the two: */ |
2659 » if (size_a < size_b) { | 2659 if (size_a < size_b) { |
2660 » » sign = -1; | 2660 sign = -1; |
2661 » » { PyLongObject *temp = a; a = b; b = temp; } | 2661 { PyLongObject *temp = a; a = b; b = temp; } |
2662 » » { Py_ssize_t size_temp = size_a; | 2662 { Py_ssize_t size_temp = size_a; |
2663 » » size_a = size_b; | 2663 size_a = size_b; |
2664 » » size_b = size_temp; } | 2664 size_b = size_temp; } |
2665 » } | 2665 } |
2666 » else if (size_a == size_b) { | 2666 else if (size_a == size_b) { |
2667 » » /* Find highest digit where a and b differ: */ | 2667 /* Find highest digit where a and b differ: */ |
2668 » » i = size_a; | 2668 i = size_a; |
2669 » » while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | 2669 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
2670 » » » ; | 2670 ; |
2671 » » if (i < 0) | 2671 if (i < 0) |
2672 » » » return (PyLongObject *)PyLong_FromLong(0); | 2672 return (PyLongObject *)PyLong_FromLong(0); |
2673 » » if (a->ob_digit[i] < b->ob_digit[i]) { | 2673 if (a->ob_digit[i] < b->ob_digit[i]) { |
2674 » » » sign = -1; | 2674 sign = -1; |
2675 » » » { PyLongObject *temp = a; a = b; b = temp; } | 2675 { PyLongObject *temp = a; a = b; b = temp; } |
2676 » » } | 2676 } |
2677 » » size_a = size_b = i+1; | 2677 size_a = size_b = i+1; |
2678 » } | 2678 } |
2679 » z = _PyLong_New(size_a); | 2679 z = _PyLong_New(size_a); |
2680 » if (z == NULL) | 2680 if (z == NULL) |
2681 » » return NULL; | 2681 return NULL; |
2682 » for (i = 0; i < size_b; ++i) { | 2682 for (i = 0; i < size_b; ++i) { |
2683 » » /* The following assumes unsigned arithmetic | 2683 /* The following assumes unsigned arithmetic |
2684 » » works module 2**N for some N>PyLong_SHIFT. */ | 2684 works module 2**N for some N>PyLong_SHIFT. */ |
2685 » » borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; | 2685 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
2686 » » z->ob_digit[i] = borrow & PyLong_MASK; | 2686 z->ob_digit[i] = borrow & PyLong_MASK; |
2687 » » borrow >>= PyLong_SHIFT; | 2687 borrow >>= PyLong_SHIFT; |
2688 » » borrow &= 1; /* Keep only one sign bit */ | 2688 borrow &= 1; /* Keep only one sign bit */ |
2689 » } | 2689 } |
2690 » for (; i < size_a; ++i) { | 2690 for (; i < size_a; ++i) { |
2691 » » borrow = a->ob_digit[i] - borrow; | 2691 borrow = a->ob_digit[i] - borrow; |
2692 » » z->ob_digit[i] = borrow & PyLong_MASK; | 2692 z->ob_digit[i] = borrow & PyLong_MASK; |
2693 » » borrow >>= PyLong_SHIFT; | 2693 borrow >>= PyLong_SHIFT; |
2694 » » borrow &= 1; /* Keep only one sign bit */ | 2694 borrow &= 1; /* Keep only one sign bit */ |
2695 » } | 2695 } |
2696 » assert(borrow == 0); | 2696 assert(borrow == 0); |
2697 » if (sign < 0) | 2697 if (sign < 0) |
2698 » » NEGATE(z); | 2698 NEGATE(z); |
2699 » return long_normalize(z); | 2699 return long_normalize(z); |
2700 } | 2700 } |
2701 | 2701 |
2702 static PyObject * | 2702 static PyObject * |
2703 long_add(PyLongObject *a, PyLongObject *b) | 2703 long_add(PyLongObject *a, PyLongObject *b) |
2704 { | 2704 { |
2705 » PyLongObject *z; | 2705 PyLongObject *z; |
2706 | 2706 |
2707 » CHECK_BINOP(a, b); | 2707 CHECK_BINOP(a, b); |
2708 | 2708 |
2709 » if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { | 2709 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
2710 » » PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) + | 2710 PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) + |
2711 » » » » » » MEDIUM_VALUE(b)); | 2711 MEDIUM_VALUE(b)); |
2712 » » return result; | 2712 return result; |
2713 » } | 2713 } |
2714 » if (Py_SIZE(a) < 0) { | 2714 if (Py_SIZE(a) < 0) { |
2715 » » if (Py_SIZE(b) < 0) { | 2715 if (Py_SIZE(b) < 0) { |
2716 » » » z = x_add(a, b); | 2716 z = x_add(a, b); |
2717 » » » if (z != NULL && Py_SIZE(z) != 0) | 2717 if (z != NULL && Py_SIZE(z) != 0) |
2718 » » » » Py_SIZE(z) = -(Py_SIZE(z)); | 2718 Py_SIZE(z) = -(Py_SIZE(z)); |
2719 » » } | 2719 } |
2720 » » else | 2720 else |
2721 » » » z = x_sub(b, a); | 2721 z = x_sub(b, a); |
2722 » } | 2722 } |
2723 » else { | 2723 else { |
2724 » » if (Py_SIZE(b) < 0) | 2724 if (Py_SIZE(b) < 0) |
2725 » » » z = x_sub(a, b); | 2725 z = x_sub(a, b); |
2726 » » else | 2726 else |
2727 » » » z = x_add(a, b); | 2727 z = x_add(a, b); |
2728 » } | 2728 } |
2729 » return (PyObject *)z; | 2729 return (PyObject *)z; |
2730 } | 2730 } |
2731 | 2731 |
2732 static PyObject * | 2732 static PyObject * |
2733 long_sub(PyLongObject *a, PyLongObject *b) | 2733 long_sub(PyLongObject *a, PyLongObject *b) |
2734 { | 2734 { |
2735 » PyLongObject *z; | 2735 PyLongObject *z; |
2736 | 2736 |
2737 » CHECK_BINOP(a, b); | 2737 CHECK_BINOP(a, b); |
2738 | 2738 |
2739 » if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { | 2739 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
2740 » » PyObject* r; | 2740 PyObject* r; |
2741 » » r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); | 2741 r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b)); |
2742 » » return r; | 2742 return r; |
2743 » } | 2743 } |
2744 » if (Py_SIZE(a) < 0) { | 2744 if (Py_SIZE(a) < 0) { |
2745 » » if (Py_SIZE(b) < 0) | 2745 if (Py_SIZE(b) < 0) |
2746 » » » z = x_sub(a, b); | 2746 z = x_sub(a, b); |
2747 » » else | 2747 else |
2748 » » » z = x_add(a, b); | 2748 z = x_add(a, b); |
2749 » » if (z != NULL && Py_SIZE(z) != 0) | 2749 if (z != NULL && Py_SIZE(z) != 0) |
2750 » » » Py_SIZE(z) = -(Py_SIZE(z)); | 2750 Py_SIZE(z) = -(Py_SIZE(z)); |
2751 » } | 2751 } |
2752 » else { | 2752 else { |
2753 » » if (Py_SIZE(b) < 0) | 2753 if (Py_SIZE(b) < 0) |
2754 » » » z = x_add(a, b); | 2754 z = x_add(a, b); |
2755 » » else | 2755 else |
2756 » » » z = x_sub(a, b); | 2756 z = x_sub(a, b); |
2757 » } | 2757 } |
2758 » return (PyObject *)z; | 2758 return (PyObject *)z; |
2759 } | 2759 } |
2760 | 2760 |
2761 /* Grade school multiplication, ignoring the signs. | 2761 /* Grade school multiplication, ignoring the signs. |
2762 * Returns the absolute value of the product, or NULL if error. | 2762 * Returns the absolute value of the product, or NULL if error. |
2763 */ | 2763 */ |
2764 static PyLongObject * | 2764 static PyLongObject * |
2765 x_mul(PyLongObject *a, PyLongObject *b) | 2765 x_mul(PyLongObject *a, PyLongObject *b) |
2766 { | 2766 { |
2767 » PyLongObject *z; | 2767 PyLongObject *z; |
2768 » Py_ssize_t size_a = ABS(Py_SIZE(a)); | 2768 Py_ssize_t size_a = ABS(Py_SIZE(a)); |
2769 » Py_ssize_t size_b = ABS(Py_SIZE(b)); | 2769 Py_ssize_t size_b = ABS(Py_SIZE(b)); |
2770 » Py_ssize_t i; | 2770 Py_ssize_t i; |
2771 | 2771 |
2772 » z = _PyLong_New(size_a + size_b); | 2772 z = _PyLong_New(size_a + size_b); |
2773 » if (z == NULL) | 2773 if (z == NULL) |
2774 » » return NULL; | 2774 return NULL; |
2775 | 2775 |
2776 » memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); | 2776 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit)); |
2777 » if (a == b) { | 2777 if (a == b) { |
2778 » » /* Efficient squaring per HAC, Algorithm 14.16: | 2778 /* Efficient squaring per HAC, Algorithm 14.16: |
2779 » » * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf | 2779 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf |
2780 » » * Gives slightly less than a 2x speedup when a == b, | 2780 * Gives slightly less than a 2x speedup when a == b, |
2781 » » * via exploiting that each entry in the multiplication | 2781 * via exploiting that each entry in the multiplication |
2782 » » * pyramid appears twice (except for the size_a squares). | 2782 * pyramid appears twice (except for the size_a squares). |
2783 » » */ | 2783 */ |
2784 » » for (i = 0; i < size_a; ++i) { | 2784 for (i = 0; i < size_a; ++i) { |
2785 » » » twodigits carry; | 2785 twodigits carry; |
2786 » » » twodigits f = a->ob_digit[i]; | 2786 twodigits f = a->ob_digit[i]; |
2787 » » » digit *pz = z->ob_digit + (i << 1); | 2787 digit *pz = z->ob_digit + (i << 1); |
2788 » » » digit *pa = a->ob_digit + i + 1; | 2788 digit *pa = a->ob_digit + i + 1; |
2789 » » » digit *paend = a->ob_digit + size_a; | 2789 digit *paend = a->ob_digit + size_a; |
2790 | 2790 |
2791 » » » SIGCHECK({ | 2791 SIGCHECK({ |
2792 » » » » Py_DECREF(z); | 2792 Py_DECREF(z); |
2793 » » » » return NULL; | 2793 return NULL; |
2794 » » » }) | 2794 }); |
2795 | 2795 |
2796 » » » carry = *pz + f * f; | 2796 carry = *pz + f * f; |
2797 » » » *pz++ = (digit)(carry & PyLong_MASK); | 2797 *pz++ = (digit)(carry & PyLong_MASK); |
2798 » » » carry >>= PyLong_SHIFT; | 2798 carry >>= PyLong_SHIFT; |
2799 » » » assert(carry <= PyLong_MASK); | 2799 assert(carry <= PyLong_MASK); |
2800 | 2800 |
2801 » » » /* Now f is added in twice in each column of the | 2801 /* Now f is added in twice in each column of the |
2802 » » » * pyramid it appears. Same as adding f<<1 once. | 2802 * pyramid it appears. Same as adding f<<1 once. |
2803 » » » */ | 2803 */ |
2804 » » » f <<= 1; | 2804 f <<= 1; |
2805 » » » while (pa < paend) { | 2805 while (pa < paend) { |
2806 » » » » carry += *pz + *pa++ * f; | 2806 carry += *pz + *pa++ * f; |
2807 » » » » *pz++ = (digit)(carry & PyLong_MASK); | 2807 *pz++ = (digit)(carry & PyLong_MASK); |
2808 » » » » carry >>= PyLong_SHIFT; | 2808 carry >>= PyLong_SHIFT; |
2809 » » » » assert(carry <= (PyLong_MASK << 1)); | 2809 assert(carry <= (PyLong_MASK << 1)); |
2810 » » » } | 2810 } |
2811 » » » if (carry) { | 2811 if (carry) { |
2812 » » » » carry += *pz; | 2812 carry += *pz; |
2813 » » » » *pz++ = (digit)(carry & PyLong_MASK); | 2813 *pz++ = (digit)(carry & PyLong_MASK); |
2814 » » » » carry >>= PyLong_SHIFT; | 2814 carry >>= PyLong_SHIFT; |
2815 » » » } | 2815 } |
2816 » » » if (carry) | 2816 if (carry) |
2817 » » » » *pz += (digit)(carry & PyLong_MASK); | 2817 *pz += (digit)(carry & PyLong_MASK); |
2818 » » » assert((carry >> PyLong_SHIFT) == 0); | 2818 assert((carry >> PyLong_SHIFT) == 0); |
2819 » » } | 2819 } |
2820 » } | 2820 } |
2821 » else {» /* a is not the same as b -- gradeschool long mult */ | 2821 else { /* a is not the same as b -- gradeschool long mult */ |
2822 » » for (i = 0; i < size_a; ++i) { | 2822 for (i = 0; i < size_a; ++i) { |
2823 » » » twodigits carry = 0; | 2823 twodigits carry = 0; |
2824 » » » twodigits f = a->ob_digit[i]; | 2824 twodigits f = a->ob_digit[i]; |
2825 » » » digit *pz = z->ob_digit + i; | 2825 digit *pz = z->ob_digit + i; |
2826 » » » digit *pb = b->ob_digit; | 2826 digit *pb = b->ob_digit; |
2827 » » » digit *pbend = b->ob_digit + size_b; | 2827 digit *pbend = b->ob_digit + size_b; |
2828 | 2828 |
2829 » » » SIGCHECK({ | 2829 SIGCHECK({ |
2830 » » » » Py_DECREF(z); | 2830 Py_DECREF(z); |
2831 » » » » return NULL; | 2831 return NULL; |
2832 » » » }) | 2832 }); |
2833 | 2833 |
2834 » » » while (pb < pbend) { | 2834 while (pb < pbend) { |
2835 » » » » carry += *pz + *pb++ * f; | 2835 carry += *pz + *pb++ * f; |
2836 » » » » *pz++ = (digit)(carry & PyLong_MASK); | 2836 *pz++ = (digit)(carry & PyLong_MASK); |
2837 » » » » carry >>= PyLong_SHIFT; | 2837 carry >>= PyLong_SHIFT; |
2838 » » » » assert(carry <= PyLong_MASK); | 2838 assert(carry <= PyLong_MASK); |
2839 » » » } | 2839 } |
2840 » » » if (carry) | 2840 if (carry) |
2841 » » » » *pz += (digit)(carry & PyLong_MASK); | 2841 *pz += (digit)(carry & PyLong_MASK); |
2842 » » » assert((carry >> PyLong_SHIFT) == 0); | 2842 assert((carry >> PyLong_SHIFT) == 0); |
2843 » » } | 2843 } |
2844 » } | 2844 } |
2845 » return long_normalize(z); | 2845 return long_normalize(z); |
2846 } | 2846 } |
2847 | 2847 |
2848 /* A helper for Karatsuba multiplication (k_mul). | 2848 /* A helper for Karatsuba multiplication (k_mul). |
2849 Takes a long "n" and an integer "size" representing the place to | 2849 Takes a long "n" and an integer "size" representing the place to |
2850 split, and sets low and high such that abs(n) == (high << size) + low, | 2850 split, and sets low and high such that abs(n) == (high << size) + low, |
2851 viewing the shift as being by digits. The sign bit is ignored, and | 2851 viewing the shift as being by digits. The sign bit is ignored, and |
2852 the return values are >= 0. | 2852 the return values are >= 0. |
2853 Returns 0 on success, -1 on failure. | 2853 Returns 0 on success, -1 on failure. |
2854 */ | 2854 */ |
2855 static int | 2855 static int |
2856 kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject *
*low) | 2856 kmul_split(PyLongObject *n, |
2857 { | 2857 Py_ssize_t size, |
2858 » PyLongObject *hi, *lo; | 2858 PyLongObject **high, |
2859 » Py_ssize_t size_lo, size_hi; | 2859 PyLongObject **low) |
2860 » const Py_ssize_t size_n = ABS(Py_SIZE(n)); | 2860 { |
2861 | 2861 PyLongObject *hi, *lo; |
2862 » size_lo = MIN(size_n, size); | 2862 Py_ssize_t size_lo, size_hi; |
2863 » size_hi = size_n - size_lo; | 2863 const Py_ssize_t size_n = ABS(Py_SIZE(n)); |
2864 | 2864 |
2865 » if ((hi = _PyLong_New(size_hi)) == NULL) | 2865 size_lo = MIN(size_n, size); |
2866 » » return -1; | 2866 size_hi = size_n - size_lo; |
2867 » if ((lo = _PyLong_New(size_lo)) == NULL) { | 2867 |
2868 » » Py_DECREF(hi); | 2868 if ((hi = _PyLong_New(size_hi)) == NULL) |
2869 » » return -1; | 2869 return -1; |
2870 » } | 2870 if ((lo = _PyLong_New(size_lo)) == NULL) { |
2871 | 2871 Py_DECREF(hi); |
2872 » memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); | 2872 return -1; |
2873 » memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)); | 2873 } |
2874 | 2874 |
2875 » *high = long_normalize(hi); | 2875 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit)); |
2876 » *low = long_normalize(lo); | 2876 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit)); |
2877 » return 0; | 2877 |
| 2878 *high = long_normalize(hi); |
| 2879 *low = long_normalize(lo); |
| 2880 return 0; |
2878 } | 2881 } |
2879 | 2882 |
2880 static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); | 2883 static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b); |
2881 | 2884 |
2882 /* Karatsuba multiplication. Ignores the input signs, and returns the | 2885 /* Karatsuba multiplication. Ignores the input signs, and returns the |
2883 * absolute value of the product (or NULL if error). | 2886 * absolute value of the product (or NULL if error). |
2884 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). | 2887 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295). |
2885 */ | 2888 */ |
2886 static PyLongObject * | 2889 static PyLongObject * |
2887 k_mul(PyLongObject *a, PyLongObject *b) | 2890 k_mul(PyLongObject *a, PyLongObject *b) |
2888 { | 2891 { |
2889 » Py_ssize_t asize = ABS(Py_SIZE(a)); | 2892 Py_ssize_t asize = ABS(Py_SIZE(a)); |
2890 » Py_ssize_t bsize = ABS(Py_SIZE(b)); | 2893 Py_ssize_t bsize = ABS(Py_SIZE(b)); |
2891 » PyLongObject *ah = NULL; | 2894 PyLongObject *ah = NULL; |
2892 » PyLongObject *al = NULL; | 2895 PyLongObject *al = NULL; |
2893 » PyLongObject *bh = NULL; | 2896 PyLongObject *bh = NULL; |
2894 » PyLongObject *bl = NULL; | 2897 PyLongObject *bl = NULL; |
2895 » PyLongObject *ret = NULL; | 2898 PyLongObject *ret = NULL; |
2896 » PyLongObject *t1, *t2, *t3; | 2899 PyLongObject *t1, *t2, *t3; |
2897 » Py_ssize_t shift;» /* the number of digits we split off */ | 2900 Py_ssize_t shift; /* the number of digits we split off */ |
2898 » Py_ssize_t i; | 2901 Py_ssize_t i; |
2899 | 2902 |
2900 » /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl | 2903 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl |
2901 » * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl | 2904 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl |
2902 » * Then the original product is | 2905 * Then the original product is |
2903 » * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl | 2906 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl |
2904 » * By picking X to be a power of 2, "*X" is just shifting, and it's | 2907 * By picking X to be a power of 2, "*X" is just shifting, and it's |
2905 » * been reduced to 3 multiplies on numbers half the size. | 2908 * been reduced to 3 multiplies on numbers half the size. |
2906 » */ | 2909 */ |
2907 | 2910 |
2908 » /* We want to split based on the larger number; fiddle so that b | 2911 /* We want to split based on the larger number; fiddle so that b |
2909 » * is largest. | 2912 * is largest. |
2910 » */ | 2913 */ |
2911 » if (asize > bsize) { | 2914 if (asize > bsize) { |
2912 » » t1 = a; | 2915 t1 = a; |
2913 » » a = b; | 2916 a = b; |
2914 » » b = t1; | 2917 b = t1; |
2915 | 2918 |
2916 » » i = asize; | 2919 i = asize; |
2917 » » asize = bsize; | 2920 asize = bsize; |
2918 » » bsize = i; | 2921 bsize = i; |
2919 » } | 2922 } |
2920 | 2923 |
2921 » /* Use gradeschool math when either number is too small. */ | 2924 /* Use gradeschool math when either number is too small. */ |
2922 » i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; | 2925 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; |
2923 » if (asize <= i) { | 2926 if (asize <= i) { |
2924 » » if (asize == 0) | 2927 if (asize == 0) |
2925 » » » return (PyLongObject *)PyLong_FromLong(0); | 2928 return (PyLongObject *)PyLong_FromLong(0); |
2926 » » else | 2929 else |
2927 » » » return x_mul(a, b); | 2930 return x_mul(a, b); |
2928 » } | 2931 } |
2929 | 2932 |
2930 » /* If a is small compared to b, splitting on b gives a degenerate | 2933 /* If a is small compared to b, splitting on b gives a degenerate |
2931 » * case with ah==0, and Karatsuba may be (even much) less efficient | 2934 * case with ah==0, and Karatsuba may be (even much) less efficient |
2932 » * than "grade school" then. However, we can still win, by viewing | 2935 * than "grade school" then. However, we can still win, by viewing |
2933 » * b as a string of "big digits", each of width a->ob_size. That | 2936 * b as a string of "big digits", each of width a->ob_size. That |
2934 » * leads to a sequence of balanced calls to k_mul. | 2937 * leads to a sequence of balanced calls to k_mul. |
2935 » */ | 2938 */ |
2936 » if (2 * asize <= bsize) | 2939 if (2 * asize <= bsize) |
2937 » » return k_lopsided_mul(a, b); | 2940 return k_lopsided_mul(a, b); |
2938 | 2941 |
2939 » /* Split a & b into hi & lo pieces. */ | 2942 /* Split a & b into hi & lo pieces. */ |
2940 » shift = bsize >> 1; | 2943 shift = bsize >> 1; |
2941 » if (kmul_split(a, shift, &ah, &al) < 0) goto fail; | 2944 if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
2942 » assert(Py_SIZE(ah) > 0);» /* the split isn't degenerate */ | 2945 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */ |
2943 | 2946 |
2944 » if (a == b) { | 2947 if (a == b) { |
2945 » » bh = ah; | 2948 bh = ah; |
2946 » » bl = al; | 2949 bl = al; |
2947 » » Py_INCREF(bh); | 2950 Py_INCREF(bh); |
2948 » » Py_INCREF(bl); | 2951 Py_INCREF(bl); |
2949 » } | 2952 } |
2950 » else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; | 2953 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
2951 | 2954 |
2952 » /* The plan: | 2955 /* The plan: |
2953 » * 1. Allocate result space (asize + bsize digits: that's always | 2956 * 1. Allocate result space (asize + bsize digits: that's always |
2954 » * enough). | 2957 * enough). |
2955 » * 2. Compute ah*bh, and copy into result at 2*shift. | 2958 * 2. Compute ah*bh, and copy into result at 2*shift. |
2956 » * 3. Compute al*bl, and copy into result at 0. Note that this | 2959 * 3. Compute al*bl, and copy into result at 0. Note that this |
2957 » * can't overlap with #2. | 2960 * can't overlap with #2. |
2958 » * 4. Subtract al*bl from the result, starting at shift. This may | 2961 * 4. Subtract al*bl from the result, starting at shift. This may |
2959 » * underflow (borrow out of the high digit), but we don't care: | 2962 * underflow (borrow out of the high digit), but we don't care: |
2960 » * we're effectively doing unsigned arithmetic mod | 2963 * we're effectively doing unsigned arithmetic mod |
2961 » * BASE**(sizea + sizeb), and so long as the *final* result fits, | 2964 * BASE**(sizea + sizeb), and so long as the *final* result fits, |
2962 » * borrows and carries out of the high digit can be ignored. | 2965 * borrows and carries out of the high digit can be ignored. |
2963 » * 5. Subtract ah*bh from the result, starting at shift. | 2966 * 5. Subtract ah*bh from the result, starting at shift. |
2964 » * 6. Compute (ah+al)*(bh+bl), and add it into the result starting | 2967 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting |
2965 » * at shift. | 2968 * at shift. |
2966 » */ | 2969 */ |
2967 | 2970 |
2968 » /* 1. Allocate result space. */ | 2971 /* 1. Allocate result space. */ |
2969 » ret = _PyLong_New(asize + bsize); | 2972 ret = _PyLong_New(asize + bsize); |
2970 » if (ret == NULL) goto fail; | 2973 if (ret == NULL) goto fail; |
2971 #ifdef Py_DEBUG | 2974 #ifdef Py_DEBUG |
2972 » /* Fill with trash, to catch reference to uninitialized digits. */ | 2975 /* Fill with trash, to catch reference to uninitialized digits. */ |
2973 » memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); | 2976 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); |
2974 #endif | 2977 #endif |
2975 | 2978 |
2976 » /* 2. t1 <- ah*bh, and copy into high digits of result. */ | 2979 /* 2. t1 <- ah*bh, and copy into high digits of result. */ |
2977 » if ((t1 = k_mul(ah, bh)) == NULL) goto fail; | 2980 if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
2978 » assert(Py_SIZE(t1) >= 0); | 2981 assert(Py_SIZE(t1) >= 0); |
2979 » assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); | 2982 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); |
2980 » memcpy(ret->ob_digit + 2*shift, t1->ob_digit, | 2983 memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
2981 » Py_SIZE(t1) * sizeof(digit)); | 2984 Py_SIZE(t1) * sizeof(digit)); |
2982 | 2985 |
2983 » /* Zero-out the digits higher than the ah*bh copy. */ | 2986 /* Zero-out the digits higher than the ah*bh copy. */ |
2984 » i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); | 2987 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); |
2985 » if (i) | 2988 if (i) |
2986 » » memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, | 2989 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, |
2987 » » i * sizeof(digit)); | 2990 i * sizeof(digit)); |
2988 | 2991 |
2989 » /* 3. t2 <- al*bl, and copy into the low digits. */ | 2992 /* 3. t2 <- al*bl, and copy into the low digits. */ |
2990 » if ((t2 = k_mul(al, bl)) == NULL) { | 2993 if ((t2 = k_mul(al, bl)) == NULL) { |
2991 » » Py_DECREF(t1); | 2994 Py_DECREF(t1); |
2992 » » goto fail; | 2995 goto fail; |
2993 » } | 2996 } |
2994 » assert(Py_SIZE(t2) >= 0); | 2997 assert(Py_SIZE(t2) >= 0); |
2995 » assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ | 2998 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ |
2996 » memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); | 2999 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); |
2997 | 3000 |
2998 » /* Zero out remaining digits. */ | 3001 /* Zero out remaining digits. */ |
2999 » i = 2*shift - Py_SIZE(t2);» /* number of uninitialized digits */ | 3002 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */ |
3000 » if (i) | 3003 if (i) |
3001 » » memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); | 3004 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); |
3002 | 3005 |
3003 » /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first | 3006 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first |
3004 » * because it's fresher in cache. | 3007 * because it's fresher in cache. |
3005 » */ | 3008 */ |
3006 » i = Py_SIZE(ret) - shift; /* # digits after shift */ | 3009 i = Py_SIZE(ret) - shift; /* # digits after shift */ |
3007 » (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); | 3010 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); |
3008 » Py_DECREF(t2); | 3011 Py_DECREF(t2); |
3009 | 3012 |
3010 » (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); | 3013 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); |
3011 » Py_DECREF(t1); | 3014 Py_DECREF(t1); |
3012 | 3015 |
3013 » /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ | 3016 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ |
3014 » if ((t1 = x_add(ah, al)) == NULL) goto fail; | 3017 if ((t1 = x_add(ah, al)) == NULL) goto fail; |
3015 » Py_DECREF(ah); | 3018 Py_DECREF(ah); |
3016 » Py_DECREF(al); | 3019 Py_DECREF(al); |
3017 » ah = al = NULL; | 3020 ah = al = NULL; |
3018 | 3021 |
3019 » if (a == b) { | 3022 if (a == b) { |
3020 » » t2 = t1; | 3023 t2 = t1; |
3021 » » Py_INCREF(t2); | 3024 Py_INCREF(t2); |
3022 » } | 3025 } |
3023 » else if ((t2 = x_add(bh, bl)) == NULL) { | 3026 else if ((t2 = x_add(bh, bl)) == NULL) { |
3024 » » Py_DECREF(t1); | 3027 Py_DECREF(t1); |
3025 » » goto fail; | 3028 goto fail; |
3026 » } | 3029 } |
3027 » Py_DECREF(bh); | 3030 Py_DECREF(bh); |
3028 » Py_DECREF(bl); | 3031 Py_DECREF(bl); |
3029 » bh = bl = NULL; | 3032 bh = bl = NULL; |
3030 | 3033 |
3031 » t3 = k_mul(t1, t2); | 3034 t3 = k_mul(t1, t2); |
3032 » Py_DECREF(t1); | 3035 Py_DECREF(t1); |
3033 » Py_DECREF(t2); | 3036 Py_DECREF(t2); |
3034 » if (t3 == NULL) goto fail; | 3037 if (t3 == NULL) goto fail; |
3035 » assert(Py_SIZE(t3) >= 0); | 3038 assert(Py_SIZE(t3) >= 0); |
3036 | 3039 |
3037 » /* Add t3. It's not obvious why we can't run out of room here. | 3040 /* Add t3. It's not obvious why we can't run out of room here. |
3038 » * See the (*) comment after this function. | 3041 * See the (*) comment after this function. |
3039 » */ | 3042 */ |
3040 » (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); | 3043 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); |
3041 » Py_DECREF(t3); | 3044 Py_DECREF(t3); |
3042 | 3045 |
3043 » return long_normalize(ret); | 3046 return long_normalize(ret); |
3044 | 3047 |
3045 fail: | 3048 fail: |
3046 » Py_XDECREF(ret); | 3049 Py_XDECREF(ret); |
3047 » Py_XDECREF(ah); | 3050 Py_XDECREF(ah); |
3048 » Py_XDECREF(al); | 3051 Py_XDECREF(al); |
3049 » Py_XDECREF(bh); | 3052 Py_XDECREF(bh); |
3050 » Py_XDECREF(bl); | 3053 Py_XDECREF(bl); |
3051 » return NULL; | 3054 return NULL; |
3052 } | 3055 } |
3053 | 3056 |
3054 /* (*) Why adding t3 can't "run out of room" above. | 3057 /* (*) Why adding t3 can't "run out of room" above. |
3055 | 3058 |
3056 Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts | 3059 Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts |
3057 to start with: | 3060 to start with: |
3058 | 3061 |
3059 1. For any integer i, i = c(i/2) + f(i/2). In particular, | 3062 1. For any integer i, i = c(i/2) + f(i/2). In particular, |
3060 bsize = c(bsize/2) + f(bsize/2). | 3063 bsize = c(bsize/2) + f(bsize/2). |
3061 2. shift = f(bsize/2) | 3064 2. shift = f(bsize/2) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3100 * would pay off *if* the inputs had balanced sizes. View b as a sequence | 3103 * would pay off *if* the inputs had balanced sizes. View b as a sequence |
3101 * of slices, each with a->ob_size digits, and multiply the slices by a, | 3104 * of slices, each with a->ob_size digits, and multiply the slices by a, |
3102 * one at a time. This gives k_mul balanced inputs to work with, and is | 3105 * one at a time. This gives k_mul balanced inputs to work with, and is |
3103 * also cache-friendly (we compute one double-width slice of the result | 3106 * also cache-friendly (we compute one double-width slice of the result |
3104 * at a time, then move on, never bactracking except for the helpful | 3107 * at a time, then move on, never bactracking except for the helpful |
3105 * single-width slice overlap between successive partial sums). | 3108 * single-width slice overlap between successive partial sums). |
3106 */ | 3109 */ |
3107 static PyLongObject * | 3110 static PyLongObject * |
3108 k_lopsided_mul(PyLongObject *a, PyLongObject *b) | 3111 k_lopsided_mul(PyLongObject *a, PyLongObject *b) |
3109 { | 3112 { |
3110 » const Py_ssize_t asize = ABS(Py_SIZE(a)); | 3113 const Py_ssize_t asize = ABS(Py_SIZE(a)); |
3111 » Py_ssize_t bsize = ABS(Py_SIZE(b)); | 3114 Py_ssize_t bsize = ABS(Py_SIZE(b)); |
3112 » Py_ssize_t nbdone;» /* # of b digits already multiplied */ | 3115 Py_ssize_t nbdone; /* # of b digits already multiplied */ |
3113 » PyLongObject *ret; | 3116 PyLongObject *ret; |
3114 » PyLongObject *bslice = NULL; | 3117 PyLongObject *bslice = NULL; |
3115 | 3118 |
3116 » assert(asize > KARATSUBA_CUTOFF); | 3119 assert(asize > KARATSUBA_CUTOFF); |
3117 » assert(2 * asize <= bsize); | 3120 assert(2 * asize <= bsize); |
3118 | 3121 |
3119 » /* Allocate result space, and zero it out. */ | 3122 /* Allocate result space, and zero it out. */ |
3120 » ret = _PyLong_New(asize + bsize); | 3123 ret = _PyLong_New(asize + bsize); |
3121 » if (ret == NULL) | 3124 if (ret == NULL) |
3122 » » return NULL; | 3125 return NULL; |
3123 » memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); | 3126 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit)); |
3124 | 3127 |
3125 » /* Successive slices of b are copied into bslice. */ | 3128 /* Successive slices of b are copied into bslice. */ |
3126 » bslice = _PyLong_New(asize); | 3129 bslice = _PyLong_New(asize); |
3127 » if (bslice == NULL) | 3130 if (bslice == NULL) |
3128 » » goto fail; | 3131 goto fail; |
3129 | 3132 |
3130 » nbdone = 0; | 3133 nbdone = 0; |
3131 » while (bsize > 0) { | 3134 while (bsize > 0) { |
3132 » » PyLongObject *product; | 3135 PyLongObject *product; |
3133 » » const Py_ssize_t nbtouse = MIN(bsize, asize); | 3136 const Py_ssize_t nbtouse = MIN(bsize, asize); |
3134 | 3137 |
3135 » » /* Multiply the next slice of b by a. */ | 3138 /* Multiply the next slice of b by a. */ |
3136 » » memcpy(bslice->ob_digit, b->ob_digit + nbdone, | 3139 memcpy(bslice->ob_digit, b->ob_digit + nbdone, |
3137 » » nbtouse * sizeof(digit)); | 3140 nbtouse * sizeof(digit)); |
3138 » » Py_SIZE(bslice) = nbtouse; | 3141 Py_SIZE(bslice) = nbtouse; |
3139 » » product = k_mul(a, bslice); | 3142 product = k_mul(a, bslice); |
3140 » » if (product == NULL) | 3143 if (product == NULL) |
3141 » » » goto fail; | 3144 goto fail; |
3142 | 3145 |
3143 » » /* Add into result. */ | 3146 /* Add into result. */ |
3144 » » (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, | 3147 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone, |
3145 » » » product->ob_digit, Py_SIZE(product)); | 3148 product->ob_digit, Py_SIZE(product)); |
3146 » » Py_DECREF(product); | 3149 Py_DECREF(product); |
3147 | 3150 |
3148 » » bsize -= nbtouse; | 3151 bsize -= nbtouse; |
3149 » » nbdone += nbtouse; | 3152 nbdone += nbtouse; |
3150 » } | 3153 } |
3151 | 3154 |
3152 » Py_DECREF(bslice); | 3155 Py_DECREF(bslice); |
3153 » return long_normalize(ret); | 3156 return long_normalize(ret); |
3154 | 3157 |
3155 fail: | 3158 fail: |
3156 » Py_DECREF(ret); | 3159 Py_DECREF(ret); |
3157 » Py_XDECREF(bslice); | 3160 Py_XDECREF(bslice); |
3158 » return NULL; | 3161 return NULL; |
3159 } | 3162 } |
3160 | 3163 |
3161 static PyObject * | 3164 static PyObject * |
3162 long_mul(PyLongObject *a, PyLongObject *b) | 3165 long_mul(PyLongObject *a, PyLongObject *b) |
3163 { | 3166 { |
3164 » PyLongObject *z; | 3167 PyLongObject *z; |
3165 | 3168 |
3166 » CHECK_BINOP(a, b); | 3169 CHECK_BINOP(a, b); |
3167 | 3170 |
3168 » /* fast path for single-digit multiplication */ | 3171 /* fast path for single-digit multiplication */ |
3169 » if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { | 3172 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) { |
3170 » » stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); | 3173 stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); |
3171 #ifdef HAVE_LONG_LONG | 3174 #ifdef HAVE_LONG_LONG |
3172 » » return PyLong_FromLongLong((PY_LONG_LONG)v); | 3175 return PyLong_FromLongLong((PY_LONG_LONG)v); |
3173 #else | 3176 #else |
3174 » » /* if we don't have long long then we're almost certainly | 3177 /* if we don't have long long then we're almost certainly |
3175 » » using 15-bit digits, so v will fit in a long. In the | 3178 using 15-bit digits, so v will fit in a long. In the |
3176 » » unlikely event that we're using 30-bit digits on a platform | 3179 unlikely event that we're using 30-bit digits on a platform |
3177 » » without long long, a large v will just cause us to fall | 3180 without long long, a large v will just cause us to fall |
3178 » » through to the general multiplication code below. */ | 3181 through to the general multiplication code below. */ |
3179 » » if (v >= LONG_MIN && v <= LONG_MAX) | 3182 if (v >= LONG_MIN && v <= LONG_MAX) |
3180 » » » return PyLong_FromLong((long)v); | 3183 return PyLong_FromLong((long)v); |
3181 #endif | 3184 #endif |
3182 » } | 3185 } |
3183 | 3186 |
3184 » z = k_mul(a, b); | 3187 z = k_mul(a, b); |
3185 » /* Negate if exactly one of the inputs is negative. */ | 3188 /* Negate if exactly one of the inputs is negative. */ |
3186 » if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) | 3189 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) |
3187 » » NEGATE(z); | 3190 NEGATE(z); |
3188 » return (PyObject *)z; | 3191 return (PyObject *)z; |
3189 } | 3192 } |
3190 | 3193 |
3191 /* The / and % operators are now defined in terms of divmod(). | 3194 /* The / and % operators are now defined in terms of divmod(). |
3192 The expression a mod b has the value a - b*floor(a/b). | 3195 The expression a mod b has the value a - b*floor(a/b). |
3193 The long_divrem function gives the remainder after division of | 3196 The long_divrem function gives the remainder after division of |
3194 |a| by |b|, with the sign of a. This is also expressed | 3197 |a| by |b|, with the sign of a. This is also expressed |
3195 as a - b*trunc(a/b), if trunc truncates towards zero. | 3198 as a - b*trunc(a/b), if trunc truncates towards zero. |
3196 Some examples: | 3199 Some examples: |
3197 » a» b» a rem b»» a mod b | 3200 a b a rem b a mod b |
3198 » 13» 10» 3» » 3 | 3201 13 10 3 3 |
3199 » -13» 10» -3» » 7 | 3202 -13 10 -3 7 |
3200 » 13» -10» 3» » -7 | 3203 13 -10 3 -7 |
3201 » -13» -10» -3» » -3 | 3204 -13 -10 -3 -3 |
3202 So, to get from rem to mod, we have to add b if a and b | 3205 So, to get from rem to mod, we have to add b if a and b |
3203 have different signs. We then subtract one from the 'div' | 3206 have different signs. We then subtract one from the 'div' |
3204 part of the outcome to keep the invariant intact. */ | 3207 part of the outcome to keep the invariant intact. */ |
3205 | 3208 |
3206 /* Compute | 3209 /* Compute |
3207 * *pdiv, *pmod = divmod(v, w) | 3210 * *pdiv, *pmod = divmod(v, w) |
3208 * NULL can be passed for pdiv or pmod, in which case that part of | 3211 * NULL can be passed for pdiv or pmod, in which case that part of |
3209 * the result is simply thrown away. The caller owns a reference to | 3212 * the result is simply thrown away. The caller owns a reference to |
3210 * each of these it requests (does not pass NULL for). | 3213 * each of these it requests (does not pass NULL for). |
3211 */ | 3214 */ |
3212 static int | 3215 static int |
3213 l_divmod(PyLongObject *v, PyLongObject *w, | 3216 l_divmod(PyLongObject *v, PyLongObject *w, |
3214 » PyLongObject **pdiv, PyLongObject **pmod) | 3217 PyLongObject **pdiv, PyLongObject **pmod) |
3215 { | 3218 { |
3216 » PyLongObject *div, *mod; | 3219 PyLongObject *div, *mod; |
3217 | 3220 |
3218 » if (long_divrem(v, w, &div, &mod) < 0) | 3221 if (long_divrem(v, w, &div, &mod) < 0) |
3219 » » return -1; | 3222 return -1; |
3220 » if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || | 3223 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) || |
3221 » (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { | 3224 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) { |
3222 » » PyLongObject *temp; | 3225 PyLongObject *temp; |
3223 » » PyLongObject *one; | 3226 PyLongObject *one; |
3224 » » temp = (PyLongObject *) long_add(mod, w); | 3227 temp = (PyLongObject *) long_add(mod, w); |
3225 » » Py_DECREF(mod); | 3228 Py_DECREF(mod); |
3226 » » mod = temp; | 3229 mod = temp; |
3227 » » if (mod == NULL) { | 3230 if (mod == NULL) { |
3228 » » » Py_DECREF(div); | 3231 Py_DECREF(div); |
3229 » » » return -1; | 3232 return -1; |
3230 » » } | 3233 } |
3231 » » one = (PyLongObject *) PyLong_FromLong(1L); | 3234 one = (PyLongObject *) PyLong_FromLong(1L); |
3232 » » if (one == NULL || | 3235 if (one == NULL || |
3233 » » (temp = (PyLongObject *) long_sub(div, one)) == NULL) { | 3236 (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
3234 » » » Py_DECREF(mod); | 3237 Py_DECREF(mod); |
3235 » » » Py_DECREF(div); | 3238 Py_DECREF(div); |
3236 » » » Py_XDECREF(one); | 3239 Py_XDECREF(one); |
3237 » » » return -1; | 3240 return -1; |
3238 » » } | 3241 } |
3239 » » Py_DECREF(one); | 3242 Py_DECREF(one); |
3240 » » Py_DECREF(div); | 3243 Py_DECREF(div); |
3241 » » div = temp; | 3244 div = temp; |
3242 » } | 3245 } |
3243 » if (pdiv != NULL) | 3246 if (pdiv != NULL) |
3244 » » *pdiv = div; | 3247 *pdiv = div; |
3245 » else | 3248 else |
3246 » » Py_DECREF(div); | 3249 Py_DECREF(div); |
3247 | 3250 |
3248 » if (pmod != NULL) | 3251 if (pmod != NULL) |
3249 » » *pmod = mod; | 3252 *pmod = mod; |
3250 » else | 3253 else |
3251 » » Py_DECREF(mod); | 3254 Py_DECREF(mod); |
3252 | 3255 |
3253 » return 0; | 3256 return 0; |
3254 } | 3257 } |
3255 | 3258 |
3256 static PyObject * | 3259 static PyObject * |
3257 long_div(PyObject *a, PyObject *b) | 3260 long_div(PyObject *a, PyObject *b) |
3258 { | 3261 { |
3259 » PyLongObject *div; | 3262 PyLongObject *div; |
3260 | 3263 |
3261 » CHECK_BINOP(a, b); | 3264 CHECK_BINOP(a, b); |
3262 » if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) | 3265 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0) |
3263 » » div = NULL; | 3266 div = NULL; |
3264 » return (PyObject *)div; | 3267 return (PyObject *)div; |
3265 } | 3268 } |
3266 | 3269 |
3267 /* PyLong/PyLong -> float, with correctly rounded result. */ | 3270 /* PyLong/PyLong -> float, with correctly rounded result. */ |
3268 | 3271 |
3269 #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) | 3272 #define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT) |
3270 #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) | 3273 #define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT) |
3271 | 3274 |
3272 static PyObject * | 3275 static PyObject * |
3273 long_true_divide(PyObject *v, PyObject *w) | 3276 long_true_divide(PyObject *v, PyObject *w) |
3274 { | 3277 { |
3275 PyLongObject *a, *b, *x; | 3278 PyLongObject *a, *b, *x; |
3276 Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; | 3279 Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits; |
3277 digit mask, low; | 3280 digit mask, low; |
3278 int inexact, negate, a_is_small, b_is_small; | 3281 int inexact, negate, a_is_small, b_is_small; |
3279 double dx, result; | 3282 double dx, result; |
3280 | 3283 |
3281 CHECK_BINOP(v, w); | 3284 CHECK_BINOP(v, w); |
3282 a = (PyLongObject *)v; | 3285 a = (PyLongObject *)v; |
3283 b = (PyLongObject *)w; | 3286 b = (PyLongObject *)w; |
3284 | 3287 |
3285 /* | 3288 /* |
3286 Method in a nutshell: | 3289 Method in a nutshell: |
3287 | 3290 |
3288 0. reduce to case a, b > 0; filter out obvious underflow/overflow | 3291 0. reduce to case a, b > 0; filter out obvious underflow/overflow |
3289 1. choose a suitable integer 'shift' | 3292 1. choose a suitable integer 'shift' |
3290 2. use integer arithmetic to compute x = floor(2**-shift*a/b) | 3293 2. use integer arithmetic to compute x = floor(2**-shift*a/b) |
3291 3. adjust x for correct rounding | 3294 3. adjust x for correct rounding |
3292 4. convert x to a double dx with the same value | 3295 4. convert x to a double dx with the same value |
3293 5. return ldexp(dx, shift). | 3296 5. return ldexp(dx, shift). |
3294 | 3297 |
3295 In more detail: | 3298 In more detail: |
3296 | 3299 |
3297 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b | 3300 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b |
3298 returns either 0.0 or -0.0, depending on the sign of b. For a and | 3301 returns either 0.0 or -0.0, depending on the sign of b. For a and |
3299 b both nonzero, ignore signs of a and b, and add the sign back in | 3302 b both nonzero, ignore signs of a and b, and add the sign back in |
3300 at the end. Now write a_bits and b_bits for the bit lengths of a | 3303 at the end. Now write a_bits and b_bits for the bit lengths of a |
3301 and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise | 3304 and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise |
3302 for b). Then | 3305 for b). Then |
3303 | 3306 |
3304 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1). | 3307 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1). |
3305 | 3308 |
3306 So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and | 3309 So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and |
3307 so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - | 3310 so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP - |
3308 DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of | 3311 DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of |
3309 the way, we can assume that | 3312 the way, we can assume that |
3310 | 3313 |
3311 DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP. | 3314 DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP. |
3312 | 3315 |
3313 1. The integer 'shift' is chosen so that x has the right number of | 3316 1. The integer 'shift' is chosen so that x has the right number of |
3314 bits for a double, plus two or three extra bits that will be used | 3317 bits for a double, plus two or three extra bits that will be used |
3315 in the rounding decisions. Writing a_bits and b_bits for the | 3318 in the rounding decisions. Writing a_bits and b_bits for the |
3316 number of significant bits in a and b respectively, a | 3319 number of significant bits in a and b respectively, a |
3317 straightforward formula for shift is: | 3320 straightforward formula for shift is: |
3318 | 3321 |
3319 shift = a_bits - b_bits - DBL_MANT_DIG - 2 | 3322 shift = a_bits - b_bits - DBL_MANT_DIG - 2 |
3320 | 3323 |
3321 This is fine in the usual case, but if a/b is smaller than the | 3324 This is fine in the usual case, but if a/b is smaller than the |
3322 smallest normal float then it can lead to double rounding on an | 3325 smallest normal float then it can lead to double rounding on an |
3323 IEEE 754 platform, giving incorrectly rounded results. So we | 3326 IEEE 754 platform, giving incorrectly rounded results. So we |
3324 adjust the formula slightly. The actual formula used is: | 3327 adjust the formula slightly. The actual formula used is: |
3325 | 3328 |
3326 shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2 | 3329 shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2 |
3327 | 3330 |
3328 2. The quantity x is computed by first shifting a (left -shift bits | 3331 2. The quantity x is computed by first shifting a (left -shift bits |
3329 if shift <= 0, right shift bits if shift > 0) and then dividing by | 3332 if shift <= 0, right shift bits if shift > 0) and then dividing by |
3330 b. For both the shift and the division, we keep track of whether | 3333 b. For both the shift and the division, we keep track of whether |
3331 the result is inexact, in a flag 'inexact'; this information is | 3334 the result is inexact, in a flag 'inexact'; this information is |
3332 needed at the rounding stage. | 3335 needed at the rounding stage. |
3333 | 3336 |
3334 With the choice of shift above, together with our assumption that | 3337 With the choice of shift above, together with our assumption that |
3335 a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows | 3338 a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows |
3336 that x >= 1. | 3339 that x >= 1. |
3337 | 3340 |
3338 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace | 3341 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace |
3339 this with an exactly representable float of the form | 3342 this with an exactly representable float of the form |
3340 | 3343 |
3341 round(x/2**extra_bits) * 2**(extra_bits+shift). | 3344 round(x/2**extra_bits) * 2**(extra_bits+shift). |
3342 | 3345 |
3343 For float representability, we need x/2**extra_bits < | 3346 For float representability, we need x/2**extra_bits < |
3344 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - | 3347 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP - |
3345 DBL_MANT_DIG. This translates to the condition: | 3348 DBL_MANT_DIG. This translates to the condition: |
3346 | 3349 |
3347 extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG | 3350 extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG |
3348 | 3351 |
3349 To round, we just modify the bottom digit of x in-place; this can | 3352 To round, we just modify the bottom digit of x in-place; this can |
3350 end up giving a digit with value > PyLONG_MASK, but that's not a | 3353 end up giving a digit with value > PyLONG_MASK, but that's not a |
3351 problem since digits can hold values up to 2*PyLONG_MASK+1. | 3354 problem since digits can hold values up to 2*PyLONG_MASK+1. |
3352 | 3355 |
3353 With the original choices for shift above, extra_bits will always | 3356 With the original choices for shift above, extra_bits will always |
3354 be 2 or 3. Then rounding under the round-half-to-even rule, we | 3357 be 2 or 3. Then rounding under the round-half-to-even rule, we |
3355 round up iff the most significant of the extra bits is 1, and | 3358 round up iff the most significant of the extra bits is 1, and |
3356 either: (a) the computation of x in step 2 had an inexact result, | 3359 either: (a) the computation of x in step 2 had an inexact result, |
3357 or (b) at least one other of the extra bits is 1, or (c) the least | 3360 or (b) at least one other of the extra bits is 1, or (c) the least |
3358 significant bit of x (above those to be rounded) is 1. | 3361 significant bit of x (above those to be rounded) is 1. |
3359 | 3362 |
3360 4. Conversion to a double is straightforward; all floating-point | 3363 4. Conversion to a double is straightforward; all floating-point |
3361 operations involved in the conversion are exact, so there's no | 3364 operations involved in the conversion are exact, so there's no |
3362 danger of rounding errors. | 3365 danger of rounding errors. |
3363 | 3366 |
3364 5. Use ldexp(x, shift) to compute x*2**shift, the final result. | 3367 5. Use ldexp(x, shift) to compute x*2**shift, the final result. |
3365 The result will always be exactly representable as a double, except | 3368 The result will always be exactly representable as a double, except |
3366 in the case that it overflows. To avoid dependence on the exact | 3369 in the case that it overflows. To avoid dependence on the exact |
3367 behaviour of ldexp on overflow, we check for overflow before | 3370 behaviour of ldexp on overflow, we check for overflow before |
3368 applying ldexp. The result of ldexp is adjusted for sign before | 3371 applying ldexp. The result of ldexp is adjusted for sign before |
3369 returning. | 3372 returning. |
3370 */ | 3373 */ |
3371 | 3374 |
3372 /* Reduce to case where a and b are both positive. */ | 3375 /* Reduce to case where a and b are both positive. */ |
3373 a_size = ABS(Py_SIZE(a)); | 3376 a_size = ABS(Py_SIZE(a)); |
3374 b_size = ABS(Py_SIZE(b)); | 3377 b_size = ABS(Py_SIZE(b)); |
3375 negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); | 3378 negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0); |
3376 if (b_size == 0) { | 3379 if (b_size == 0) { |
3377 PyErr_SetString(PyExc_ZeroDivisionError, | 3380 PyErr_SetString(PyExc_ZeroDivisionError, |
3378 "division by zero"); | 3381 "division by zero"); |
3379 goto error; | 3382 goto error; |
3380 } | 3383 } |
3381 if (a_size == 0) | 3384 if (a_size == 0) |
3382 goto underflow_or_zero; | 3385 goto underflow_or_zero; |
3383 | 3386 |
3384 /* Fast path for a and b small (exactly representable in a double). | 3387 /* Fast path for a and b small (exactly representable in a double). |
3385 Relies on floating-point division being correctly rounded; results | 3388 Relies on floating-point division being correctly rounded; results |
3386 may be subject to double rounding on x86 machines that operate with | 3389 may be subject to double rounding on x86 machines that operate with |
3387 the x87 FPU set to 64-bit precision. */ | 3390 the x87 FPU set to 64-bit precision. */ |
3388 a_is_small = a_size <= MANT_DIG_DIGITS || | 3391 a_is_small = a_size <= MANT_DIG_DIGITS || |
3389 (a_size == MANT_DIG_DIGITS+1 && | 3392 (a_size == MANT_DIG_DIGITS+1 && |
3390 a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); | 3393 a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
3391 b_is_small = b_size <= MANT_DIG_DIGITS || | 3394 b_is_small = b_size <= MANT_DIG_DIGITS || |
3392 (b_size == MANT_DIG_DIGITS+1 && | 3395 (b_size == MANT_DIG_DIGITS+1 && |
3393 b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); | 3396 b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0); |
3394 if (a_is_small && b_is_small) { | 3397 if (a_is_small && b_is_small) { |
3395 double da, db; | 3398 double da, db; |
3396 da = a->ob_digit[--a_size]; | 3399 da = a->ob_digit[--a_size]; |
3397 while (a_size > 0) | 3400 while (a_size > 0) |
3398 da = da * PyLong_BASE + a->ob_digit[--a_size]; | 3401 da = da * PyLong_BASE + a->ob_digit[--a_size]; |
3399 db = b->ob_digit[--b_size]; | 3402 db = b->ob_digit[--b_size]; |
3400 while (b_size > 0) | 3403 while (b_size > 0) |
3401 db = db * PyLong_BASE + b->ob_digit[--b_size]; | 3404 db = db * PyLong_BASE + b->ob_digit[--b_size]; |
3402 result = da / db; | 3405 result = da / db; |
3403 goto success; | 3406 goto success; |
3404 } | 3407 } |
3405 | 3408 |
3406 /* Catch obvious cases of underflow and overflow */ | 3409 /* Catch obvious cases of underflow and overflow */ |
3407 diff = a_size - b_size; | 3410 diff = a_size - b_size; |
3408 if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) | 3411 if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1) |
3409 /* Extreme overflow */ | 3412 /* Extreme overflow */ |
3410 goto overflow; | 3413 goto overflow; |
3411 else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) | 3414 else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT) |
3412 /* Extreme underflow */ | 3415 /* Extreme underflow */ |
3413 goto underflow_or_zero; | 3416 goto underflow_or_zero; |
3414 /* Next line is now safe from overflowing a Py_ssize_t */ | 3417 /* Next line is now safe from overflowing a Py_ssize_t */ |
3415 diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) - | 3418 diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) - |
3416 bits_in_digit(b->ob_digit[b_size - 1]); | 3419 bits_in_digit(b->ob_digit[b_size - 1]); |
3417 /* Now diff = a_bits - b_bits. */ | 3420 /* Now diff = a_bits - b_bits. */ |
3418 if (diff > DBL_MAX_EXP) | 3421 if (diff > DBL_MAX_EXP) |
3419 goto overflow; | 3422 goto overflow; |
3420 else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) | 3423 else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1) |
3421 goto underflow_or_zero; | 3424 goto underflow_or_zero; |
3422 | 3425 |
3423 /* Choose value for shift; see comments for step 1 above. */ | 3426 /* Choose value for shift; see comments for step 1 above. */ |
3424 shift = MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; | 3427 shift = MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2; |
3425 | 3428 |
3426 inexact = 0; | 3429 inexact = 0; |
3427 | 3430 |
3428 /* x = abs(a * 2**-shift) */ | 3431 /* x = abs(a * 2**-shift) */ |
3429 if (shift <= 0) { | 3432 if (shift <= 0) { |
3430 Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; | 3433 Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT; |
3431 digit rem; | 3434 digit rem; |
3432 /* x = a << -shift */ | 3435 /* x = a << -shift */ |
3433 if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { | 3436 if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) { |
3434 /* In practice, it's probably impossible to end up | 3437 /* In practice, it's probably impossible to end up |
3435 here. Both a and b would have to be enormous, | 3438 here. Both a and b would have to be enormous, |
3436 using close to SIZE_T_MAX bytes of memory each. */ | 3439 using close to SIZE_T_MAX bytes of memory each. */ |
3437 PyErr_SetString(PyExc_OverflowError, | 3440 PyErr_SetString(PyExc_OverflowError, |
3438 "intermediate overflow during division"); | 3441 "intermediate overflow during division"); |
3439 goto error; | 3442 goto error; |
3440 } | 3443 } |
3441 x = _PyLong_New(a_size + shift_digits + 1); | 3444 x = _PyLong_New(a_size + shift_digits + 1); |
3442 if (x == NULL) | 3445 if (x == NULL) |
3443 goto error; | 3446 goto error; |
3444 for (i = 0; i < shift_digits; i++) | 3447 for (i = 0; i < shift_digits; i++) |
3445 x->ob_digit[i] = 0; | 3448 x->ob_digit[i] = 0; |
3446 rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, | 3449 rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit, |
3447 a_size, -shift % PyLong_SHIFT); | 3450 a_size, -shift % PyLong_SHIFT); |
3448 x->ob_digit[a_size + shift_digits] = rem; | 3451 x->ob_digit[a_size + shift_digits] = rem; |
3449 } | 3452 } |
3450 else { | 3453 else { |
3451 Py_ssize_t shift_digits = shift / PyLong_SHIFT; | 3454 Py_ssize_t shift_digits = shift / PyLong_SHIFT; |
3452 digit rem; | 3455 digit rem; |
3453 /* x = a >> shift */ | 3456 /* x = a >> shift */ |
3454 assert(a_size >= shift_digits); | 3457 assert(a_size >= shift_digits); |
3455 x = _PyLong_New(a_size - shift_digits); | 3458 x = _PyLong_New(a_size - shift_digits); |
3456 if (x == NULL) | 3459 if (x == NULL) |
3457 goto error; | 3460 goto error; |
3458 rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, | 3461 rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits, |
3459 a_size - shift_digits, shift % PyLong_SHIFT); | 3462 a_size - shift_digits, shift % PyLong_SHIFT); |
3460 /* set inexact if any of the bits shifted out is nonzero */ | 3463 /* set inexact if any of the bits shifted out is nonzero */ |
3461 if (rem) | 3464 if (rem) |
3462 inexact = 1; | 3465 inexact = 1; |
3463 while (!inexact && shift_digits > 0) | 3466 while (!inexact && shift_digits > 0) |
3464 if (a->ob_digit[--shift_digits]) | 3467 if (a->ob_digit[--shift_digits]) |
3465 inexact = 1; | 3468 inexact = 1; |
3466 } | 3469 } |
3467 long_normalize(x); | 3470 long_normalize(x); |
3468 x_size = Py_SIZE(x); | 3471 x_size = Py_SIZE(x); |
3469 | 3472 |
3470 /* x //= b. If the remainder is nonzero, set inexact. We own the only | 3473 /* x //= b. If the remainder is nonzero, set inexact. We own the only |
3471 reference to x, so it's safe to modify it in-place. */ | 3474 reference to x, so it's safe to modify it in-place. */ |
3472 if (b_size == 1) { | 3475 if (b_size == 1) { |
3473 digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, | 3476 digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size, |
3474 b->ob_digit[0]); | 3477 b->ob_digit[0]); |
3475 long_normalize(x); | 3478 long_normalize(x); |
3476 if (rem) | 3479 if (rem) |
3477 inexact = 1; | 3480 inexact = 1; |
3478 } | 3481 } |
3479 else { | 3482 else { |
3480 PyLongObject *div, *rem; | 3483 PyLongObject *div, *rem; |
3481 div = x_divrem(x, b, &rem); | 3484 div = x_divrem(x, b, &rem); |
3482 Py_DECREF(x); | 3485 Py_DECREF(x); |
3483 x = div; | 3486 x = div; |
3484 if (x == NULL) | 3487 if (x == NULL) |
3485 goto error; | 3488 goto error; |
3486 if (Py_SIZE(rem)) | 3489 if (Py_SIZE(rem)) |
3487 inexact = 1; | 3490 inexact = 1; |
3488 Py_DECREF(rem); | 3491 Py_DECREF(rem); |
3489 } | 3492 } |
3490 x_size = ABS(Py_SIZE(x)); | 3493 x_size = ABS(Py_SIZE(x)); |
3491 assert(x_size > 0); /* result of division is never zero */ | 3494 assert(x_size > 0); /* result of division is never zero */ |
3492 x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]); | 3495 x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]); |
3493 | 3496 |
3494 /* The number of extra bits that have to be rounded away. */ | 3497 /* The number of extra bits that have to be rounded away. */ |
3495 extra_bits = MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG; | 3498 extra_bits = MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG; |
3496 assert(extra_bits == 2 || extra_bits == 3); | 3499 assert(extra_bits == 2 || extra_bits == 3); |
3497 | 3500 |
3498 /* Round by directly modifying the low digit of x. */ | 3501 /* Round by directly modifying the low digit of x. */ |
3499 mask = (digit)1 << (extra_bits - 1); | 3502 mask = (digit)1 << (extra_bits - 1); |
3500 low = x->ob_digit[0] | inexact; | 3503 low = x->ob_digit[0] | inexact; |
3501 if (low & mask && low & (3*mask-1)) | 3504 if (low & mask && low & (3*mask-1)) |
3502 low += mask; | 3505 low += mask; |
3503 x->ob_digit[0] = low & ~(mask-1U); | 3506 x->ob_digit[0] = low & ~(mask-1U); |
3504 | 3507 |
3505 /* Convert x to a double dx; the conversion is exact. */ | 3508 /* Convert x to a double dx; the conversion is exact. */ |
3506 dx = x->ob_digit[--x_size]; | 3509 dx = x->ob_digit[--x_size]; |
3507 while (x_size > 0) | 3510 while (x_size > 0) |
3508 dx = dx * PyLong_BASE + x->ob_digit[--x_size]; | 3511 dx = dx * PyLong_BASE + x->ob_digit[--x_size]; |
3509 Py_DECREF(x); | 3512 Py_DECREF(x); |
3510 | 3513 |
3511 /* Check whether ldexp result will overflow a double. */ | 3514 /* Check whether ldexp result will overflow a double. */ |
3512 if (shift + x_bits >= DBL_MAX_EXP && | 3515 if (shift + x_bits >= DBL_MAX_EXP && |
3513 (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, x_bits))) | 3516 (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits))) |
3514 goto overflow; | 3517 goto overflow; |
3515 result = ldexp(dx, shift); | 3518 result = ldexp(dx, (int)shift); |
3516 | 3519 |
3517 success: | 3520 success: |
3518 » return PyFloat_FromDouble(negate ? -result : result); | 3521 return PyFloat_FromDouble(negate ? -result : result); |
3519 | 3522 |
3520 underflow_or_zero: | 3523 underflow_or_zero: |
3521 » return PyFloat_FromDouble(negate ? -0.0 : 0.0); | 3524 return PyFloat_FromDouble(negate ? -0.0 : 0.0); |
3522 | 3525 |
3523 overflow: | 3526 overflow: |
3524 » PyErr_SetString(PyExc_OverflowError, | 3527 PyErr_SetString(PyExc_OverflowError, |
3525 » » » "integer division result too large for a float"); | 3528 "integer division result too large for a float"); |
3526 error: | 3529 error: |
3527 » return NULL; | 3530 return NULL; |
3528 } | 3531 } |
3529 | 3532 |
3530 static PyObject * | 3533 static PyObject * |
3531 long_mod(PyObject *a, PyObject *b) | 3534 long_mod(PyObject *a, PyObject *b) |
3532 { | 3535 { |
3533 » PyLongObject *mod; | 3536 PyLongObject *mod; |
3534 » | 3537 |
3535 » CHECK_BINOP(a, b); | 3538 CHECK_BINOP(a, b); |
3536 | 3539 |
3537 » if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) | 3540 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0) |
3538 » » mod = NULL; | 3541 mod = NULL; |
3539 » return (PyObject *)mod; | 3542 return (PyObject *)mod; |
3540 } | 3543 } |
3541 | 3544 |
3542 static PyObject * | 3545 static PyObject * |
3543 long_divmod(PyObject *a, PyObject *b) | 3546 long_divmod(PyObject *a, PyObject *b) |
3544 { | 3547 { |
3545 » PyLongObject *div, *mod; | 3548 PyLongObject *div, *mod; |
3546 » PyObject *z; | 3549 PyObject *z; |
3547 | 3550 |
3548 » CHECK_BINOP(a, b); | 3551 CHECK_BINOP(a, b); |
3549 | 3552 |
3550 » if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { | 3553 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) { |
3551 » » return NULL; | 3554 return NULL; |
3552 » } | 3555 } |
3553 » z = PyTuple_New(2); | 3556 z = PyTuple_New(2); |
3554 » if (z != NULL) { | 3557 if (z != NULL) { |
3555 » » PyTuple_SetItem(z, 0, (PyObject *) div); | 3558 PyTuple_SetItem(z, 0, (PyObject *) div); |
3556 » » PyTuple_SetItem(z, 1, (PyObject *) mod); | 3559 PyTuple_SetItem(z, 1, (PyObject *) mod); |
3557 » } | 3560 } |
3558 » else { | 3561 else { |
3559 » » Py_DECREF(div); | 3562 Py_DECREF(div); |
3560 » » Py_DECREF(mod); | 3563 Py_DECREF(mod); |
3561 » } | 3564 } |
3562 » return z; | 3565 return z; |
3563 } | 3566 } |
3564 | 3567 |
3565 /* pow(v, w, x) */ | 3568 /* pow(v, w, x) */ |
3566 static PyObject * | 3569 static PyObject * |
3567 long_pow(PyObject *v, PyObject *w, PyObject *x) | 3570 long_pow(PyObject *v, PyObject *w, PyObject *x) |
3568 { | 3571 { |
3569 » PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ | 3572 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */ |
3570 » int negativeOutput = 0; /* if x<0 return negative output */ | 3573 int negativeOutput = 0; /* if x<0 return negative output */ |
3571 | 3574 |
3572 » PyLongObject *z = NULL; /* accumulated result */ | 3575 PyLongObject *z = NULL; /* accumulated result */ |
3573 » Py_ssize_t i, j, k; /* counters */ | 3576 Py_ssize_t i, j, k; /* counters */ |
3574 » PyLongObject *temp = NULL; | 3577 PyLongObject *temp = NULL; |
3575 | 3578 |
3576 » /* 5-ary values. If the exponent is large enough, table is | 3579 /* 5-ary values. If the exponent is large enough, table is |
3577 » * precomputed so that table[i] == a**i % c for i in range(32). | 3580 * precomputed so that table[i] == a**i % c for i in range(32). |
3578 » */ | 3581 */ |
3579 » PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | 3582 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
3580 » » » » 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | 3583 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
3581 | 3584 |
3582 » /* a, b, c = v, w, x */ | 3585 /* a, b, c = v, w, x */ |
3583 » CHECK_BINOP(v, w); | 3586 CHECK_BINOP(v, w); |
3584 » a = (PyLongObject*)v; Py_INCREF(a); | 3587 a = (PyLongObject*)v; Py_INCREF(a); |
3585 » b = (PyLongObject*)w; Py_INCREF(b); | 3588 b = (PyLongObject*)w; Py_INCREF(b); |
3586 » if (PyLong_Check(x)) { | 3589 if (PyLong_Check(x)) { |
3587 » » c = (PyLongObject *)x; | 3590 c = (PyLongObject *)x; |
3588 » » Py_INCREF(x); | 3591 Py_INCREF(x); |
3589 » } | 3592 } |
3590 » else if (x == Py_None) | 3593 else if (x == Py_None) |
3591 » » c = NULL; | 3594 c = NULL; |
3592 » else { | 3595 else { |
3593 » » Py_DECREF(a); | 3596 Py_DECREF(a); |
3594 » » Py_DECREF(b); | 3597 Py_DECREF(b); |
3595 » » Py_INCREF(Py_NotImplemented); | 3598 Py_INCREF(Py_NotImplemented); |
3596 » » return Py_NotImplemented; | 3599 return Py_NotImplemented; |
3597 » } | 3600 } |
3598 | 3601 |
3599 » if (Py_SIZE(b) < 0) { /* if exponent is negative */ | 3602 if (Py_SIZE(b) < 0) { /* if exponent is negative */ |
3600 » » if (c) { | 3603 if (c) { |
3601 » » » PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " | 3604 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
3602 » » » "cannot be negative when 3rd argument specified"); | 3605 "cannot be negative when 3rd argument specified"); |
3603 » » » goto Error; | 3606 goto Error; |
3604 » » } | 3607 } |
3605 » » else { | 3608 else { |
3606 » » » /* else return a float. This works because we know | 3609 /* else return a float. This works because we know |
3607 » » » that this calls float_pow() which converts its | 3610 that this calls float_pow() which converts its |
3608 » » » arguments to double. */ | 3611 arguments to double. */ |
3609 » » » Py_DECREF(a); | 3612 Py_DECREF(a); |
3610 » » » Py_DECREF(b); | 3613 Py_DECREF(b); |
3611 » » » return PyFloat_Type.tp_as_number->nb_power(v, w, x); | 3614 return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
3612 » » } | 3615 } |
3613 » } | 3616 } |
3614 | 3617 |
3615 » if (c) { | 3618 if (c) { |
3616 » » /* if modulus == 0: | 3619 /* if modulus == 0: |
3617 » » raise ValueError() */ | 3620 raise ValueError() */ |
3618 » » if (Py_SIZE(c) == 0) { | 3621 if (Py_SIZE(c) == 0) { |
3619 » » » PyErr_SetString(PyExc_ValueError, | 3622 PyErr_SetString(PyExc_ValueError, |
3620 » » » » » "pow() 3rd argument cannot be 0"); | 3623 "pow() 3rd argument cannot be 0"); |
3621 » » » goto Error; | 3624 goto Error; |
3622 » » } | 3625 } |
3623 | 3626 |
3624 » » /* if modulus < 0: | 3627 /* if modulus < 0: |
3625 » » negativeOutput = True | 3628 negativeOutput = True |
3626 » » modulus = -modulus */ | 3629 modulus = -modulus */ |
3627 » » if (Py_SIZE(c) < 0) { | 3630 if (Py_SIZE(c) < 0) { |
3628 » » » negativeOutput = 1; | 3631 negativeOutput = 1; |
3629 » » » temp = (PyLongObject *)_PyLong_Copy(c); | 3632 temp = (PyLongObject *)_PyLong_Copy(c); |
3630 » » » if (temp == NULL) | 3633 if (temp == NULL) |
3631 » » » » goto Error; | 3634 goto Error; |
3632 » » » Py_DECREF(c); | 3635 Py_DECREF(c); |
3633 » » » c = temp; | 3636 c = temp; |
3634 » » » temp = NULL; | 3637 temp = NULL; |
3635 » » » NEGATE(c); | 3638 NEGATE(c); |
3636 » » } | 3639 } |
3637 | 3640 |
3638 » » /* if modulus == 1: | 3641 /* if modulus == 1: |
3639 » » return 0 */ | 3642 return 0 */ |
3640 » » if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { | 3643 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) { |
3641 » » » z = (PyLongObject *)PyLong_FromLong(0L); | 3644 z = (PyLongObject *)PyLong_FromLong(0L); |
3642 » » » goto Done; | 3645 goto Done; |
3643 » » } | 3646 } |
3644 | 3647 |
3645 » » /* if base < 0: | 3648 /* if base < 0: |
3646 » » base = base % modulus | 3649 base = base % modulus |
3647 » » Having the base positive just makes things easier. */ | 3650 Having the base positive just makes things easier. */ |
3648 » » if (Py_SIZE(a) < 0) { | 3651 if (Py_SIZE(a) < 0) { |
3649 » » » if (l_divmod(a, c, NULL, &temp) < 0) | 3652 if (l_divmod(a, c, NULL, &temp) < 0) |
3650 » » » » goto Error; | 3653 goto Error; |
3651 » » » Py_DECREF(a); | 3654 Py_DECREF(a); |
3652 » » » a = temp; | 3655 a = temp; |
3653 » » » temp = NULL; | 3656 temp = NULL; |
3654 » » } | 3657 } |
3655 » } | 3658 } |
3656 | 3659 |
3657 » /* At this point a, b, and c are guaranteed non-negative UNLESS | 3660 /* At this point a, b, and c are guaranteed non-negative UNLESS |
3658 » c is NULL, in which case a may be negative. */ | 3661 c is NULL, in which case a may be negative. */ |
3659 | 3662 |
3660 » z = (PyLongObject *)PyLong_FromLong(1L); | 3663 z = (PyLongObject *)PyLong_FromLong(1L); |
3661 » if (z == NULL) | 3664 if (z == NULL) |
3662 » » goto Error; | 3665 goto Error; |
3663 | 3666 |
3664 » /* Perform a modular reduction, X = X % c, but leave X alone if c | 3667 /* Perform a modular reduction, X = X % c, but leave X alone if c |
3665 » * is NULL. | 3668 * is NULL. |
3666 » */ | 3669 */ |
3667 #define REDUCE(X)» » » » » \ | 3670 #define REDUCE(X) \ |
3668 » if (c != NULL) {» » » » \ | 3671 do { \ |
3669 » » if (l_divmod(X, c, NULL, &temp) < 0)» \ | 3672 if (c != NULL) { \ |
3670 » » » goto Error;» » » \ | 3673 if (l_divmod(X, c, NULL, &temp) < 0) \ |
3671 » » Py_XDECREF(X);» » » » \ | 3674 goto Error; \ |
3672 » » X = temp;» » » » \ | 3675 Py_XDECREF(X); \ |
3673 » » temp = NULL;» » » » \ | 3676 X = temp; \ |
3674 » } | 3677 temp = NULL; \ |
3675 | 3678 } \ |
3676 » /* Multiply two values, then reduce the result: | 3679 } while(0) |
3677 » result = X*Y % c. If c is NULL, skip the mod. */ | 3680 |
3678 #define MULT(X, Y, result)» » » » \ | 3681 /* Multiply two values, then reduce the result: |
3679 {» » » » » » » \ | 3682 result = X*Y % c. If c is NULL, skip the mod. */ |
3680 » temp = (PyLongObject *)long_mul(X, Y);» » \ | 3683 #define MULT(X, Y, result) \ |
3681 » if (temp == NULL)» » » » \ | 3684 do { \ |
3682 » » goto Error;» » » » \ | 3685 temp = (PyLongObject *)long_mul(X, Y); \ |
3683 » Py_XDECREF(result);» » » » \ | 3686 if (temp == NULL) \ |
3684 » result = temp;» » » » » \ | 3687 goto Error; \ |
3685 » temp = NULL;» » » » » \ | 3688 Py_XDECREF(result); \ |
3686 » REDUCE(result)» » » » » \ | 3689 result = temp; \ |
3687 } | 3690 temp = NULL; \ |
3688 | 3691 REDUCE(result); \ |
3689 » if (Py_SIZE(b) <= FIVEARY_CUTOFF) { | 3692 } while(0) |
3690 » » /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ | 3693 |
3691 » » /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ | 3694 if (Py_SIZE(b) <= FIVEARY_CUTOFF) { |
3692 » » for (i = Py_SIZE(b) - 1; i >= 0; --i) { | 3695 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */ |
3693 » » » digit bi = b->ob_digit[i]; | 3696 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */ |
3694 | 3697 for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
3695 » » » for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1)
{ | 3698 digit bi = b->ob_digit[i]; |
3696 » » » » MULT(z, z, z) | 3699 |
3697 » » » » if (bi & j) | 3700 for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) { |
3698 » » » » » MULT(z, a, z) | 3701 MULT(z, z, z); |
3699 » » » } | 3702 if (bi & j) |
3700 » » } | 3703 MULT(z, a, z); |
3701 » } | 3704 } |
3702 » else { | 3705 } |
3703 » » /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ | 3706 } |
3704 » » Py_INCREF(z);» /* still holds 1L */ | 3707 else { |
3705 » » table[0] = z; | 3708 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */ |
3706 » » for (i = 1; i < 32; ++i) | 3709 Py_INCREF(z); /* still holds 1L */ |
3707 » » » MULT(table[i-1], a, table[i]) | 3710 table[0] = z; |
3708 | 3711 for (i = 1; i < 32; ++i) |
3709 » » for (i = Py_SIZE(b) - 1; i >= 0; --i) { | 3712 MULT(table[i-1], a, table[i]); |
3710 » » » const digit bi = b->ob_digit[i]; | 3713 |
3711 | 3714 for (i = Py_SIZE(b) - 1; i >= 0; --i) { |
3712 » » » for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { | 3715 const digit bi = b->ob_digit[i]; |
3713 » » » » const int index = (bi >> j) & 0x1f; | 3716 |
3714 » » » » for (k = 0; k < 5; ++k) | 3717 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) { |
3715 » » » » » MULT(z, z, z) | 3718 const int index = (bi >> j) & 0x1f; |
3716 » » » » if (index) | 3719 for (k = 0; k < 5; ++k) |
3717 » » » » » MULT(z, table[index], z) | 3720 MULT(z, z, z); |
3718 » » » } | 3721 if (index) |
3719 » » } | 3722 MULT(z, table[index], z); |
3720 » } | 3723 } |
3721 | 3724 } |
3722 » if (negativeOutput && (Py_SIZE(z) != 0)) { | 3725 } |
3723 » » temp = (PyLongObject *)long_sub(z, c); | 3726 |
3724 » » if (temp == NULL) | 3727 if (negativeOutput && (Py_SIZE(z) != 0)) { |
3725 » » » goto Error; | 3728 temp = (PyLongObject *)long_sub(z, c); |
3726 » » Py_DECREF(z); | 3729 if (temp == NULL) |
3727 » » z = temp; | 3730 goto Error; |
3728 » » temp = NULL; | 3731 Py_DECREF(z); |
3729 » } | 3732 z = temp; |
3730 » goto Done; | 3733 temp = NULL; |
3731 | 3734 } |
3732 Error: | 3735 goto Done; |
3733 » if (z != NULL) { | 3736 |
3734 » » Py_DECREF(z); | 3737 Error: |
3735 » » z = NULL; | 3738 if (z != NULL) { |
3736 » } | 3739 Py_DECREF(z); |
3737 » /* fall through */ | 3740 z = NULL; |
3738 Done: | 3741 } |
3739 » if (Py_SIZE(b) > FIVEARY_CUTOFF) { | 3742 /* fall through */ |
3740 » » for (i = 0; i < 32; ++i) | 3743 Done: |
3741 » » » Py_XDECREF(table[i]); | 3744 if (Py_SIZE(b) > FIVEARY_CUTOFF) { |
3742 » } | 3745 for (i = 0; i < 32; ++i) |
3743 » Py_DECREF(a); | 3746 Py_XDECREF(table[i]); |
3744 » Py_DECREF(b); | 3747 } |
3745 » Py_XDECREF(c); | 3748 Py_DECREF(a); |
3746 » Py_XDECREF(temp); | 3749 Py_DECREF(b); |
3747 » return (PyObject *)z; | 3750 Py_XDECREF(c); |
| 3751 Py_XDECREF(temp); |
| 3752 return (PyObject *)z; |
3748 } | 3753 } |
3749 | 3754 |
3750 static PyObject * | 3755 static PyObject * |
3751 long_invert(PyLongObject *v) | 3756 long_invert(PyLongObject *v) |
3752 { | 3757 { |
3753 » /* Implement ~x as -(x+1) */ | 3758 /* Implement ~x as -(x+1) */ |
3754 » PyLongObject *x; | 3759 PyLongObject *x; |
3755 » PyLongObject *w; | 3760 PyLongObject *w; |
3756 » if (ABS(Py_SIZE(v)) <=1) | 3761 if (ABS(Py_SIZE(v)) <=1) |
3757 » » return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); | 3762 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1)); |
3758 » w = (PyLongObject *)PyLong_FromLong(1L); | 3763 w = (PyLongObject *)PyLong_FromLong(1L); |
3759 » if (w == NULL) | 3764 if (w == NULL) |
3760 » » return NULL; | 3765 return NULL; |
3761 » x = (PyLongObject *) long_add(v, w); | 3766 x = (PyLongObject *) long_add(v, w); |
3762 » Py_DECREF(w); | 3767 Py_DECREF(w); |
3763 » if (x == NULL) | 3768 if (x == NULL) |
3764 » » return NULL; | 3769 return NULL; |
3765 » Py_SIZE(x) = -(Py_SIZE(x)); | 3770 Py_SIZE(x) = -(Py_SIZE(x)); |
3766 » return (PyObject *)maybe_small_long(x); | 3771 return (PyObject *)maybe_small_long(x); |
3767 } | 3772 } |
3768 | 3773 |
3769 static PyObject * | 3774 static PyObject * |
3770 long_neg(PyLongObject *v) | 3775 long_neg(PyLongObject *v) |
3771 { | 3776 { |
3772 » PyLongObject *z; | 3777 PyLongObject *z; |
3773 » if (ABS(Py_SIZE(v)) <= 1) | 3778 if (ABS(Py_SIZE(v)) <= 1) |
3774 » » return PyLong_FromLong(-MEDIUM_VALUE(v)); | 3779 return PyLong_FromLong(-MEDIUM_VALUE(v)); |
3775 » z = (PyLongObject *)_PyLong_Copy(v); | 3780 z = (PyLongObject *)_PyLong_Copy(v); |
3776 » if (z != NULL) | 3781 if (z != NULL) |
3777 » » Py_SIZE(z) = -(Py_SIZE(v)); | 3782 Py_SIZE(z) = -(Py_SIZE(v)); |
3778 » return (PyObject *)z; | 3783 return (PyObject *)z; |
3779 } | 3784 } |
3780 | 3785 |
3781 static PyObject * | 3786 static PyObject * |
3782 long_abs(PyLongObject *v) | 3787 long_abs(PyLongObject *v) |
3783 { | 3788 { |
3784 » if (Py_SIZE(v) < 0) | 3789 if (Py_SIZE(v) < 0) |
3785 » » return long_neg(v); | 3790 return long_neg(v); |
3786 » else | 3791 else |
3787 » » return long_long((PyObject *)v); | 3792 return long_long((PyObject *)v); |
3788 } | 3793 } |
3789 | 3794 |
3790 static int | 3795 static int |
3791 long_bool(PyLongObject *v) | 3796 long_bool(PyLongObject *v) |
3792 { | 3797 { |
3793 » return ABS(Py_SIZE(v)) != 0; | 3798 return Py_SIZE(v) != 0; |
3794 } | 3799 } |
3795 | 3800 |
3796 static PyObject * | 3801 static PyObject * |
3797 long_rshift(PyLongObject *a, PyLongObject *b) | 3802 long_rshift(PyLongObject *a, PyLongObject *b) |
3798 { | 3803 { |
3799 » PyLongObject *z = NULL; | 3804 PyLongObject *z = NULL; |
3800 » long shiftby; | 3805 Py_ssize_t shiftby, newsize, wordshift, loshift, hishift, i, j; |
3801 » Py_ssize_t newsize, wordshift, loshift, hishift, i, j; | 3806 digit lomask, himask; |
3802 » digit lomask, himask; | 3807 |
3803 | 3808 CHECK_BINOP(a, b); |
3804 » CHECK_BINOP(a, b); | 3809 |
3805 | 3810 if (Py_SIZE(a) < 0) { |
3806 » if (Py_SIZE(a) < 0) { | 3811 /* Right shifting negative numbers is harder */ |
3807 » » /* Right shifting negative numbers is harder */ | 3812 PyLongObject *a1, *a2; |
3808 » » PyLongObject *a1, *a2; | 3813 a1 = (PyLongObject *) long_invert(a); |
3809 » » a1 = (PyLongObject *) long_invert(a); | 3814 if (a1 == NULL) |
3810 » » if (a1 == NULL) | 3815 goto rshift_error; |
3811 » » » goto rshift_error; | 3816 a2 = (PyLongObject *) long_rshift(a1, b); |
3812 » » a2 = (PyLongObject *) long_rshift(a1, b); | 3817 Py_DECREF(a1); |
3813 » » Py_DECREF(a1); | 3818 if (a2 == NULL) |
3814 » » if (a2 == NULL) | 3819 goto rshift_error; |
3815 » » » goto rshift_error; | 3820 z = (PyLongObject *) long_invert(a2); |
3816 » » z = (PyLongObject *) long_invert(a2); | 3821 Py_DECREF(a2); |
3817 » » Py_DECREF(a2); | 3822 } |
3818 » } | 3823 else { |
3819 » else { | 3824 shiftby = PyLong_AsSsize_t((PyObject *)b); |
3820 | 3825 if (shiftby == -1L && PyErr_Occurred()) |
3821 » » shiftby = PyLong_AsLong((PyObject *)b); | 3826 goto rshift_error; |
3822 » » if (shiftby == -1L && PyErr_Occurred()) | 3827 if (shiftby < 0) { |
3823 » » » goto rshift_error; | 3828 PyErr_SetString(PyExc_ValueError, |
3824 » » if (shiftby < 0) { | 3829 "negative shift count"); |
3825 » » » PyErr_SetString(PyExc_ValueError, | 3830 goto rshift_error; |
3826 » » » » » "negative shift count"); | 3831 } |
3827 » » » goto rshift_error; | 3832 wordshift = shiftby / PyLong_SHIFT; |
3828 » » } | 3833 newsize = ABS(Py_SIZE(a)) - wordshift; |
3829 » » wordshift = shiftby / PyLong_SHIFT; | 3834 if (newsize <= 0) |
3830 » » newsize = ABS(Py_SIZE(a)) - wordshift; | 3835 return PyLong_FromLong(0); |
3831 » » if (newsize <= 0) | 3836 loshift = shiftby % PyLong_SHIFT; |
3832 » » return PyLong_FromLong(0); | 3837 hishift = PyLong_SHIFT - loshift; |
3833 » » loshift = shiftby % PyLong_SHIFT; | 3838 lomask = ((digit)1 << hishift) - 1; |
3834 » » hishift = PyLong_SHIFT - loshift; | 3839 himask = PyLong_MASK ^ lomask; |
3835 » » lomask = ((digit)1 << hishift) - 1; | 3840 z = _PyLong_New(newsize); |
3836 » » himask = PyLong_MASK ^ lomask; | 3841 if (z == NULL) |
3837 » » z = _PyLong_New(newsize); | 3842 goto rshift_error; |
3838 » » if (z == NULL) | 3843 if (Py_SIZE(a) < 0) |
3839 » » » goto rshift_error; | 3844 Py_SIZE(z) = -(Py_SIZE(z)); |
3840 » » if (Py_SIZE(a) < 0) | 3845 for (i = 0, j = wordshift; i < newsize; i++, j++) { |
3841 » » » Py_SIZE(z) = -(Py_SIZE(z)); | 3846 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
3842 » » for (i = 0, j = wordshift; i < newsize; i++, j++) { | 3847 if (i+1 < newsize) |
3843 » » » z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; | 3848 z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; |
3844 » » » if (i+1 < newsize) | 3849 } |
3845 » » » » z->ob_digit[i] |= | 3850 z = long_normalize(z); |
3846 » » » » (a->ob_digit[j+1] << hishift) & himask; | 3851 } |
3847 » » } | 3852 rshift_error: |
3848 » » z = long_normalize(z); | 3853 return (PyObject *) maybe_small_long(z); |
3849 » } | |
3850 rshift_error: | |
3851 » return (PyObject *) maybe_small_long(z); | |
3852 | 3854 |
3853 } | 3855 } |
3854 | 3856 |
3855 static PyObject * | 3857 static PyObject * |
3856 long_lshift(PyObject *v, PyObject *w) | 3858 long_lshift(PyObject *v, PyObject *w) |
3857 { | 3859 { |
3858 » /* This version due to Tim Peters */ | 3860 /* This version due to Tim Peters */ |
3859 » PyLongObject *a = (PyLongObject*)v; | 3861 PyLongObject *a = (PyLongObject*)v; |
3860 » PyLongObject *b = (PyLongObject*)w; | 3862 PyLongObject *b = (PyLongObject*)w; |
3861 » PyLongObject *z = NULL; | 3863 PyLongObject *z = NULL; |
3862 » long shiftby; | 3864 Py_ssize_t shiftby, oldsize, newsize, wordshift, remshift, i, j; |
3863 » Py_ssize_t oldsize, newsize, wordshift, remshift, i, j; | 3865 twodigits accum; |
3864 » twodigits accum; | 3866 |
3865 | 3867 CHECK_BINOP(a, b); |
3866 » CHECK_BINOP(a, b); | 3868 |
3867 | 3869 shiftby = PyLong_AsSsize_t((PyObject *)b); |
3868 » shiftby = PyLong_AsLong((PyObject *)b); | 3870 if (shiftby == -1L && PyErr_Occurred()) |
3869 » if (shiftby == -1L && PyErr_Occurred()) | 3871 goto lshift_error; |
3870 » » goto lshift_error; | 3872 if (shiftby < 0) { |
3871 » if (shiftby < 0) { | 3873 PyErr_SetString(PyExc_ValueError, "negative shift count"); |
3872 » » PyErr_SetString(PyExc_ValueError, "negative shift count"); | 3874 goto lshift_error; |
3873 » » goto lshift_error; | 3875 } |
3874 » } | 3876 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ |
3875 » if ((long)(int)shiftby != shiftby) { | 3877 wordshift = shiftby / PyLong_SHIFT; |
3876 » » PyErr_SetString(PyExc_ValueError, | 3878 remshift = shiftby - wordshift * PyLong_SHIFT; |
3877 » » » » "outrageous left shift count"); | 3879 |
3878 » » goto lshift_error; | 3880 oldsize = ABS(Py_SIZE(a)); |
3879 » } | 3881 newsize = oldsize + wordshift; |
3880 » /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */ | 3882 if (remshift) |
3881 » wordshift = (int)shiftby / PyLong_SHIFT; | 3883 ++newsize; |
3882 » remshift = (int)shiftby - wordshift * PyLong_SHIFT; | 3884 z = _PyLong_New(newsize); |
3883 | 3885 if (z == NULL) |
3884 » oldsize = ABS(Py_SIZE(a)); | 3886 goto lshift_error; |
3885 » newsize = oldsize + wordshift; | 3887 if (Py_SIZE(a) < 0) |
3886 » if (remshift) | 3888 NEGATE(z); |
3887 » » ++newsize; | 3889 for (i = 0; i < wordshift; i++) |
3888 » z = _PyLong_New(newsize); | 3890 z->ob_digit[i] = 0; |
3889 » if (z == NULL) | 3891 accum = 0; |
3890 » » goto lshift_error; | 3892 for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
3891 » if (Py_SIZE(a) < 0) | 3893 accum |= (twodigits)a->ob_digit[j] << remshift; |
3892 » » NEGATE(z); | 3894 z->ob_digit[i] = (digit)(accum & PyLong_MASK); |
3893 » for (i = 0; i < wordshift; i++) | 3895 accum >>= PyLong_SHIFT; |
3894 » » z->ob_digit[i] = 0; | 3896 } |
3895 » accum = 0; | 3897 if (remshift) |
3896 » for (i = wordshift, j = 0; j < oldsize; i++, j++) { | 3898 z->ob_digit[newsize-1] = (digit)accum; |
3897 » » accum |= (twodigits)a->ob_digit[j] << remshift; | 3899 else |
3898 » » z->ob_digit[i] = (digit)(accum & PyLong_MASK); | 3900 assert(!accum); |
3899 » » accum >>= PyLong_SHIFT; | 3901 z = long_normalize(z); |
3900 » } | 3902 lshift_error: |
3901 » if (remshift) | 3903 return (PyObject *) maybe_small_long(z); |
3902 » » z->ob_digit[newsize-1] = (digit)accum; | |
3903 » else | |
3904 » » assert(!accum); | |
3905 » z = long_normalize(z); | |
3906 lshift_error: | |
3907 » return (PyObject *) maybe_small_long(z); | |
3908 } | 3904 } |
3909 | 3905 |
3910 /* Compute two's complement of digit vector a[0:m], writing result to | 3906 /* Compute two's complement of digit vector a[0:m], writing result to |
3911 z[0:m]. The digit vector a need not be normalized, but should not | 3907 z[0:m]. The digit vector a need not be normalized, but should not |
3912 be entirely zero. a and z may point to the same digit vector. */ | 3908 be entirely zero. a and z may point to the same digit vector. */ |
3913 | 3909 |
3914 static void | 3910 static void |
3915 v_complement(digit *z, digit *a, Py_ssize_t m) | 3911 v_complement(digit *z, digit *a, Py_ssize_t m) |
3916 { | 3912 { |
3917 » Py_ssize_t i; | 3913 Py_ssize_t i; |
3918 » digit carry = 1; | 3914 digit carry = 1; |
3919 » for (i = 0; i < m; ++i) { | 3915 for (i = 0; i < m; ++i) { |
3920 » » carry += a[i] ^ PyLong_MASK; | 3916 carry += a[i] ^ PyLong_MASK; |
3921 » » z[i] = carry & PyLong_MASK; | 3917 z[i] = carry & PyLong_MASK; |
3922 » » carry >>= PyLong_SHIFT; | 3918 carry >>= PyLong_SHIFT; |
3923 » } | 3919 } |
3924 » assert(carry == 0); | 3920 assert(carry == 0); |
3925 } | 3921 } |
3926 | 3922 |
3927 /* Bitwise and/xor/or operations */ | 3923 /* Bitwise and/xor/or operations */ |
3928 | 3924 |
3929 static PyObject * | 3925 static PyObject * |
3930 long_bitwise(PyLongObject *a, | 3926 long_bitwise(PyLongObject *a, |
3931 » int op, /* '&', '|', '^' */ | 3927 int op, /* '&', '|', '^' */ |
3932 » PyLongObject *b) | 3928 PyLongObject *b) |
3933 { | 3929 { |
3934 » int nega, negb, negz; | 3930 int nega, negb, negz; |
3935 » Py_ssize_t size_a, size_b, size_z, i; | 3931 Py_ssize_t size_a, size_b, size_z, i; |
3936 » PyLongObject *z; | 3932 PyLongObject *z; |
3937 | 3933 |
3938 » /* Bitwise operations for negative numbers operate as though | 3934 /* Bitwise operations for negative numbers operate as though |
3939 » on a two's complement representation. So convert arguments | 3935 on a two's complement representation. So convert arguments |
3940 » from sign-magnitude to two's complement, and convert the | 3936 from sign-magnitude to two's complement, and convert the |
3941 » result back to sign-magnitude at the end. */ | 3937 result back to sign-magnitude at the end. */ |
3942 | 3938 |
3943 » /* If a is negative, replace it by its two's complement. */ | 3939 /* If a is negative, replace it by its two's complement. */ |
3944 » size_a = ABS(Py_SIZE(a)); | 3940 size_a = ABS(Py_SIZE(a)); |
3945 » nega = Py_SIZE(a) < 0; | 3941 nega = Py_SIZE(a) < 0; |
3946 » if (nega) { | 3942 if (nega) { |
3947 » » z = _PyLong_New(size_a); | 3943 z = _PyLong_New(size_a); |
3948 » » if (z == NULL) | 3944 if (z == NULL) |
3949 » » » return NULL; | 3945 return NULL; |
3950 » » v_complement(z->ob_digit, a->ob_digit, size_a); | 3946 v_complement(z->ob_digit, a->ob_digit, size_a); |
3951 » » a = z; | 3947 a = z; |
3952 » } | 3948 } |
3953 » else | 3949 else |
3954 » » /* Keep reference count consistent. */ | 3950 /* Keep reference count consistent. */ |
3955 » » Py_INCREF(a); | 3951 Py_INCREF(a); |
3956 | 3952 |
3957 » /* Same for b. */ | 3953 /* Same for b. */ |
3958 » size_b = ABS(Py_SIZE(b)); | 3954 size_b = ABS(Py_SIZE(b)); |
3959 » negb = Py_SIZE(b) < 0; | 3955 negb = Py_SIZE(b) < 0; |
3960 » if (negb) { | 3956 if (negb) { |
3961 » » z = _PyLong_New(size_b); | 3957 z = _PyLong_New(size_b); |
3962 » » if (z == NULL) { | 3958 if (z == NULL) { |
3963 » » » Py_DECREF(a); | 3959 Py_DECREF(a); |
3964 » » » return NULL; | 3960 return NULL; |
3965 » » } | 3961 } |
3966 » » v_complement(z->ob_digit, b->ob_digit, size_b); | 3962 v_complement(z->ob_digit, b->ob_digit, size_b); |
3967 » » b = z; | 3963 b = z; |
3968 » } | 3964 } |
3969 » else | 3965 else |
3970 » » Py_INCREF(b); | 3966 Py_INCREF(b); |
3971 | 3967 |
3972 » /* Swap a and b if necessary to ensure size_a >= size_b. */ | 3968 /* Swap a and b if necessary to ensure size_a >= size_b. */ |
3973 » if (size_a < size_b) { | 3969 if (size_a < size_b) { |
3974 » » z = a; a = b; b = z; | 3970 z = a; a = b; b = z; |
3975 » » size_z = size_a; size_a = size_b; size_b = size_z; | 3971 size_z = size_a; size_a = size_b; size_b = size_z; |
3976 » » negz = nega; nega = negb; negb = negz; | 3972 negz = nega; nega = negb; negb = negz; |
3977 » } | 3973 } |
3978 | 3974 |
3979 » /* JRH: The original logic here was to allocate the result value (z) | 3975 /* JRH: The original logic here was to allocate the result value (z) |
3980 » as the longer of the two operands. However, there are some cases | 3976 as the longer of the two operands. However, there are some cases |
3981 » where the result is guaranteed to be shorter than that: AND of two | 3977 where the result is guaranteed to be shorter than that: AND of two |
3982 » positives, OR of two negatives: use the shorter number. AND with | 3978 positives, OR of two negatives: use the shorter number. AND with |
3983 » mixed signs: use the positive number. OR with mixed signs: use the | 3979 mixed signs: use the positive number. OR with mixed signs: use the |
3984 » negative number. | 3980 negative number. |
3985 » */ | 3981 */ |
3986 » switch (op) { | 3982 switch (op) { |
3987 » case '^': | 3983 case '^': |
3988 » » negz = nega ^ negb; | 3984 negz = nega ^ negb; |
3989 » » size_z = size_a; | 3985 size_z = size_a; |
3990 » » break; | 3986 break; |
3991 » case '&': | 3987 case '&': |
3992 » » negz = nega & negb; | 3988 negz = nega & negb; |
3993 » » size_z = negb ? size_a : size_b; | 3989 size_z = negb ? size_a : size_b; |
3994 » » break; | 3990 break; |
3995 » case '|': | 3991 case '|': |
3996 » » negz = nega | negb; | 3992 negz = nega | negb; |
3997 » » size_z = negb ? size_b : size_a; | 3993 size_z = negb ? size_b : size_a; |
3998 » » break; | 3994 break; |
3999 » default: | 3995 default: |
4000 » » PyErr_BadArgument(); | 3996 PyErr_BadArgument(); |
4001 » » return NULL; | 3997 return NULL; |
4002 » } | 3998 } |
4003 | 3999 |
4004 » /* We allow an extra digit if z is negative, to make sure that | 4000 /* We allow an extra digit if z is negative, to make sure that |
4005 » the final two's complement of z doesn't overflow. */ | 4001 the final two's complement of z doesn't overflow. */ |
4006 » z = _PyLong_New(size_z + negz); | 4002 z = _PyLong_New(size_z + negz); |
4007 » if (z == NULL) { | 4003 if (z == NULL) { |
4008 » » Py_DECREF(a); | 4004 Py_DECREF(a); |
4009 » » Py_DECREF(b); | 4005 Py_DECREF(b); |
4010 » » return NULL; | 4006 return NULL; |
4011 » } | 4007 } |
4012 | 4008 |
4013 » /* Compute digits for overlap of a and b. */ | 4009 /* Compute digits for overlap of a and b. */ |
4014 » switch(op) { | 4010 switch(op) { |
4015 » case '&': | 4011 case '&': |
4016 » » for (i = 0; i < size_b; ++i) | 4012 for (i = 0; i < size_b; ++i) |
4017 » » » z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; | 4013 z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i]; |
4018 » » break; | 4014 break; |
4019 » case '|': | 4015 case '|': |
4020 » » for (i = 0; i < size_b; ++i) | 4016 for (i = 0; i < size_b; ++i) |
4021 » » » z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; | 4017 z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i]; |
4022 » » break; | 4018 break; |
4023 » case '^': | 4019 case '^': |
4024 » » for (i = 0; i < size_b; ++i) | 4020 for (i = 0; i < size_b; ++i) |
4025 » » » z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; | 4021 z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i]; |
4026 » » break; | 4022 break; |
4027 » default: | 4023 default: |
4028 » » PyErr_BadArgument(); | 4024 PyErr_BadArgument(); |
4029 » » return NULL; | 4025 return NULL; |
4030 » } | 4026 } |
4031 | 4027 |
4032 » /* Copy any remaining digits of a, inverting if necessary. */ | 4028 /* Copy any remaining digits of a, inverting if necessary. */ |
4033 » if (op == '^' && negb) | 4029 if (op == '^' && negb) |
4034 » » for (; i < size_z; ++i) | 4030 for (; i < size_z; ++i) |
4035 » » » z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; | 4031 z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK; |
4036 » else if (i < size_z) | 4032 else if (i < size_z) |
4037 » » memcpy(&z->ob_digit[i], &a->ob_digit[i], | 4033 memcpy(&z->ob_digit[i], &a->ob_digit[i], |
4038 » » (size_z-i)*sizeof(digit)); | 4034 (size_z-i)*sizeof(digit)); |
4039 | 4035 |
4040 » /* Complement result if negative. */ | 4036 /* Complement result if negative. */ |
4041 » if (negz) { | 4037 if (negz) { |
4042 » » Py_SIZE(z) = -(Py_SIZE(z)); | 4038 Py_SIZE(z) = -(Py_SIZE(z)); |
4043 » » z->ob_digit[size_z] = PyLong_MASK; | 4039 z->ob_digit[size_z] = PyLong_MASK; |
4044 » » v_complement(z->ob_digit, z->ob_digit, size_z+1); | 4040 v_complement(z->ob_digit, z->ob_digit, size_z+1); |
4045 » } | 4041 } |
4046 | 4042 |
4047 » Py_DECREF(a); | 4043 Py_DECREF(a); |
4048 » Py_DECREF(b); | 4044 Py_DECREF(b); |
4049 » return (PyObject *)maybe_small_long(long_normalize(z)); | 4045 return (PyObject *)maybe_small_long(long_normalize(z)); |
4050 } | 4046 } |
4051 | 4047 |
4052 static PyObject * | 4048 static PyObject * |
4053 long_and(PyObject *a, PyObject *b) | 4049 long_and(PyObject *a, PyObject *b) |
4054 { | 4050 { |
4055 » PyObject *c; | 4051 PyObject *c; |
4056 » CHECK_BINOP(a, b); | 4052 CHECK_BINOP(a, b); |
4057 » c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); | 4053 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b); |
4058 » return c; | 4054 return c; |
4059 } | 4055 } |
4060 | 4056 |
4061 static PyObject * | 4057 static PyObject * |
4062 long_xor(PyObject *a, PyObject *b) | 4058 long_xor(PyObject *a, PyObject *b) |
4063 { | 4059 { |
4064 » PyObject *c; | 4060 PyObject *c; |
4065 » CHECK_BINOP(a, b); | 4061 CHECK_BINOP(a, b); |
4066 » c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); | 4062 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b); |
4067 » return c; | 4063 return c; |
4068 } | 4064 } |
4069 | 4065 |
4070 static PyObject * | 4066 static PyObject * |
4071 long_or(PyObject *a, PyObject *b) | 4067 long_or(PyObject *a, PyObject *b) |
4072 { | 4068 { |
4073 » PyObject *c; | 4069 PyObject *c; |
4074 » CHECK_BINOP(a, b); | 4070 CHECK_BINOP(a, b); |
4075 » c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); | 4071 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b); |
4076 » return c; | 4072 return c; |
4077 } | 4073 } |
4078 | 4074 |
4079 static PyObject * | 4075 static PyObject * |
4080 long_long(PyObject *v) | 4076 long_long(PyObject *v) |
4081 { | 4077 { |
4082 » if (PyLong_CheckExact(v)) | 4078 if (PyLong_CheckExact(v)) |
4083 » » Py_INCREF(v); | 4079 Py_INCREF(v); |
4084 » else | 4080 else |
4085 » » v = _PyLong_Copy((PyLongObject *)v); | 4081 v = _PyLong_Copy((PyLongObject *)v); |
4086 » return v; | 4082 return v; |
4087 } | 4083 } |
4088 | 4084 |
4089 static PyObject * | 4085 static PyObject * |
4090 long_float(PyObject *v) | 4086 long_float(PyObject *v) |
4091 { | 4087 { |
4092 » double result; | 4088 double result; |
4093 » result = PyLong_AsDouble(v); | 4089 result = PyLong_AsDouble(v); |
4094 » if (result == -1.0 && PyErr_Occurred()) | 4090 if (result == -1.0 && PyErr_Occurred()) |
4095 » » return NULL; | 4091 return NULL; |
4096 » return PyFloat_FromDouble(result); | 4092 return PyFloat_FromDouble(result); |
4097 } | 4093 } |
4098 | 4094 |
4099 static PyObject * | 4095 static PyObject * |
4100 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); | 4096 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
4101 | 4097 |
4102 static PyObject * | 4098 static PyObject * |
4103 long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 4099 long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
4104 { | 4100 { |
4105 » PyObject *x = NULL; | 4101 PyObject *x = NULL; |
4106 » int base = -909;» » /* unlikely! */ | 4102 int base = -909; /* unlikely! */ |
4107 » static char *kwlist[] = {"x", "base", 0}; | 4103 static char *kwlist[] = {"x", "base", 0}; |
4108 | 4104 |
4109 » if (type != &PyLong_Type) | 4105 if (type != &PyLong_Type) |
4110 » » return long_subtype_new(type, args, kwds); /* Wimp out */ | 4106 return long_subtype_new(type, args, kwds); /* Wimp out */ |
4111 » if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, | 4107 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
4112 » » » » » &x, &base)) | 4108 &x, &base)) |
4113 » » return NULL; | 4109 return NULL; |
4114 » if (x == NULL) | 4110 if (x == NULL) |
4115 » » return PyLong_FromLong(0L); | 4111 return PyLong_FromLong(0L); |
4116 » if (base == -909) | 4112 if (base == -909) |
4117 » » return PyNumber_Long(x); | 4113 return PyNumber_Long(x); |
4118 » else if (PyUnicode_Check(x)) | 4114 else if (PyUnicode_Check(x)) |
4119 » » return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), | 4115 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), |
4120 » » » » » PyUnicode_GET_SIZE(x), | 4116 PyUnicode_GET_SIZE(x), |
4121 » » » » » base); | 4117 base); |
4122 » else if (PyByteArray_Check(x) || PyBytes_Check(x)) { | 4118 else if (PyByteArray_Check(x) || PyBytes_Check(x)) { |
4123 » » /* Since PyLong_FromString doesn't have a length parameter, | 4119 /* Since PyLong_FromString doesn't have a length parameter, |
4124 » » * check here for possible NULs in the string. */ | 4120 * check here for possible NULs in the string. */ |
4125 » » char *string; | 4121 char *string; |
4126 » » Py_ssize_t size = Py_SIZE(x); | 4122 Py_ssize_t size = Py_SIZE(x); |
4127 » » if (PyByteArray_Check(x)) | 4123 if (PyByteArray_Check(x)) |
4128 » » » string = PyByteArray_AS_STRING(x); | 4124 string = PyByteArray_AS_STRING(x); |
4129 » » else | 4125 else |
4130 » » » string = PyBytes_AS_STRING(x); | 4126 string = PyBytes_AS_STRING(x); |
4131 » » if (strlen(string) != (size_t)size) { | 4127 if (strlen(string) != (size_t)size) { |
4132 » » » /* We only see this if there's a null byte in x, | 4128 /* We only see this if there's a null byte in x, |
4133 » » » x is a bytes or buffer, *and* a base is given. */ | 4129 x is a bytes or buffer, *and* a base is given. */ |
4134 » » » PyErr_Format(PyExc_ValueError, | 4130 PyErr_Format(PyExc_ValueError, |
4135 » » » "invalid literal for int() with base %d: %R", | 4131 "invalid literal for int() with base %d: %R", |
4136 » » » base, x); | 4132 base, x); |
4137 » » » return NULL; | 4133 return NULL; |
4138 » » } | 4134 } |
4139 » » return PyLong_FromString(string, NULL, base); | 4135 return PyLong_FromString(string, NULL, base); |
4140 » } | 4136 } |
4141 » else { | 4137 else { |
4142 » » PyErr_SetString(PyExc_TypeError, | 4138 PyErr_SetString(PyExc_TypeError, |
4143 » » » "int() can't convert non-string with explicit base"); | 4139 "int() can't convert non-string with explicit base"); |
4144 » » return NULL; | 4140 return NULL; |
4145 » } | 4141 } |
4146 } | 4142 } |
4147 | 4143 |
4148 /* Wimpy, slow approach to tp_new calls for subtypes of long: | 4144 /* Wimpy, slow approach to tp_new calls for subtypes of long: |
4149 first create a regular long from whatever arguments we got, | 4145 first create a regular long from whatever arguments we got, |
4150 then allocate a subtype instance and initialize it from | 4146 then allocate a subtype instance and initialize it from |
4151 the regular long. The regular long is then thrown away. | 4147 the regular long. The regular long is then thrown away. |
4152 */ | 4148 */ |
4153 static PyObject * | 4149 static PyObject * |
4154 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 4150 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
4155 { | 4151 { |
4156 » PyLongObject *tmp, *newobj; | 4152 PyLongObject *tmp, *newobj; |
4157 » Py_ssize_t i, n; | 4153 Py_ssize_t i, n; |
4158 | 4154 |
4159 » assert(PyType_IsSubtype(type, &PyLong_Type)); | 4155 assert(PyType_IsSubtype(type, &PyLong_Type)); |
4160 » tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); | 4156 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); |
4161 » if (tmp == NULL) | 4157 if (tmp == NULL) |
4162 » » return NULL; | 4158 return NULL; |
4163 » assert(PyLong_CheckExact(tmp)); | 4159 assert(PyLong_CheckExact(tmp)); |
4164 » n = Py_SIZE(tmp); | 4160 n = Py_SIZE(tmp); |
4165 » if (n < 0) | 4161 if (n < 0) |
4166 » » n = -n; | 4162 n = -n; |
4167 » newobj = (PyLongObject *)type->tp_alloc(type, n); | 4163 newobj = (PyLongObject *)type->tp_alloc(type, n); |
4168 » if (newobj == NULL) { | 4164 if (newobj == NULL) { |
4169 » » Py_DECREF(tmp); | 4165 Py_DECREF(tmp); |
4170 » » return NULL; | 4166 return NULL; |
4171 » } | 4167 } |
4172 » assert(PyLong_Check(newobj)); | 4168 assert(PyLong_Check(newobj)); |
4173 » Py_SIZE(newobj) = Py_SIZE(tmp); | 4169 Py_SIZE(newobj) = Py_SIZE(tmp); |
4174 » for (i = 0; i < n; i++) | 4170 for (i = 0; i < n; i++) |
4175 » » newobj->ob_digit[i] = tmp->ob_digit[i]; | 4171 newobj->ob_digit[i] = tmp->ob_digit[i]; |
4176 » Py_DECREF(tmp); | 4172 Py_DECREF(tmp); |
4177 » return (PyObject *)newobj; | 4173 return (PyObject *)newobj; |
4178 } | 4174 } |
4179 | 4175 |
4180 static PyObject * | 4176 static PyObject * |
4181 long_getnewargs(PyLongObject *v) | 4177 long_getnewargs(PyLongObject *v) |
4182 { | 4178 { |
4183 » return Py_BuildValue("(N)", _PyLong_Copy(v)); | 4179 return Py_BuildValue("(N)", _PyLong_Copy(v)); |
4184 } | 4180 } |
4185 | 4181 |
4186 static PyObject * | 4182 static PyObject * |
4187 long_get0(PyLongObject *v, void *context) { | 4183 long_get0(PyLongObject *v, void *context) { |
4188 » return PyLong_FromLong(0L); | 4184 return PyLong_FromLong(0L); |
4189 } | 4185 } |
4190 | 4186 |
4191 static PyObject * | 4187 static PyObject * |
4192 long_get1(PyLongObject *v, void *context) { | 4188 long_get1(PyLongObject *v, void *context) { |
4193 » return PyLong_FromLong(1L); | 4189 return PyLong_FromLong(1L); |
4194 } | 4190 } |
4195 | 4191 |
4196 static PyObject * | 4192 static PyObject * |
4197 long__format__(PyObject *self, PyObject *args) | 4193 long__format__(PyObject *self, PyObject *args) |
4198 { | 4194 { |
4199 » PyObject *format_spec; | 4195 PyObject *format_spec; |
4200 | 4196 |
4201 » if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) | 4197 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) |
4202 » » return NULL; | 4198 return NULL; |
4203 » return _PyLong_FormatAdvanced(self, | 4199 return _PyLong_FormatAdvanced(self, |
4204 » » » » PyUnicode_AS_UNICODE(format_spec), | 4200 PyUnicode_AS_UNICODE(format_spec), |
4205 » » » » PyUnicode_GET_SIZE(format_spec)); | 4201 PyUnicode_GET_SIZE(format_spec)); |
4206 } | 4202 } |
4207 | 4203 |
4208 static PyObject * | 4204 static PyObject * |
4209 long_round(PyObject *self, PyObject *args) | 4205 long_round(PyObject *self, PyObject *args) |
4210 { | 4206 { |
4211 » PyObject *o_ndigits=NULL, *temp; | 4207 PyObject *o_ndigits=NULL, *temp; |
4212 » PyLongObject *pow=NULL, *q=NULL, *r=NULL, *ndigits=NULL, *one; | 4208 PyLongObject *pow=NULL, *q=NULL, *r=NULL, *ndigits=NULL, *one; |
4213 » int errcode; | 4209 int errcode; |
4214 » digit q_mod_4; | 4210 digit q_mod_4; |
4215 | 4211 |
4216 » /* Notes on the algorithm: to round to the nearest 10**n (n positive), | 4212 /* Notes on the algorithm: to round to the nearest 10**n (n positive), |
4217 » the straightforward method is: | 4213 the straightforward method is: |
4218 | 4214 |
4219 » (1) divide by 10**n | 4215 (1) divide by 10**n |
4220 » (2) round to nearest integer (round to even in case of tie) | 4216 (2) round to nearest integer (round to even in case of tie) |
4221 » (3) multiply result by 10**n. | 4217 (3) multiply result by 10**n. |
4222 | 4218 |
4223 » But the rounding step involves examining the fractional part of the | 4219 But the rounding step involves examining the fractional part of the |
4224 » quotient to see whether it's greater than 0.5 or not. Since we | 4220 quotient to see whether it's greater than 0.5 or not. Since we |
4225 » want to do the whole calculation in integer arithmetic, it's | 4221 want to do the whole calculation in integer arithmetic, it's |
4226 » simpler to do: | 4222 simpler to do: |
4227 | 4223 |
4228 » (1) divide by (10**n)/2 | 4224 (1) divide by (10**n)/2 |
4229 » (2) round to nearest multiple of 2 (multiple of 4 in case of tie) | 4225 (2) round to nearest multiple of 2 (multiple of 4 in case of tie) |
4230 » (3) multiply result by (10**n)/2. | 4226 (3) multiply result by (10**n)/2. |
4231 | 4227 |
4232 » Then all we need to know about the fractional part of the quotient | 4228 Then all we need to know about the fractional part of the quotient |
4233 » arising in step (2) is whether it's zero or not. | 4229 arising in step (2) is whether it's zero or not. |
4234 | 4230 |
4235 » Doing both a multiplication and division is wasteful, and is easily | 4231 Doing both a multiplication and division is wasteful, and is easily |
4236 » avoided if we just figure out how much to adjust the original input | 4232 avoided if we just figure out how much to adjust the original input |
4237 » by to do the rounding. | 4233 by to do the rounding. |
4238 | 4234 |
4239 » Here's the whole algorithm expressed in Python. | 4235 Here's the whole algorithm expressed in Python. |
4240 | 4236 |
4241 » def round(self, ndigits = None): | 4237 def round(self, ndigits = None): |
4242 » """round(int, int) -> int""" | 4238 """round(int, int) -> int""" |
4243 » if ndigits is None or ndigits >= 0: | 4239 if ndigits is None or ndigits >= 0: |
4244 » return self | 4240 return self |
4245 » pow = 10**-ndigits >> 1 | 4241 pow = 10**-ndigits >> 1 |
4246 » q, r = divmod(self, pow) | 4242 q, r = divmod(self, pow) |
4247 » self -= r | 4243 self -= r |
4248 » if (q & 1 != 0): | 4244 if (q & 1 != 0): |
4249 » if (q & 2 == r == 0): | 4245 if (q & 2 == r == 0): |
4250 » self -= pow | 4246 self -= pow |
4251 » else: | 4247 else: |
4252 » self += pow | 4248 self += pow |
4253 » return self | 4249 return self |
4254 | 4250 |
4255 » */ | 4251 */ |
4256 » if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) | 4252 if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) |
4257 » » return NULL; | 4253 return NULL; |
4258 » if (o_ndigits == NULL) | 4254 if (o_ndigits == NULL) |
4259 » » return long_long(self); | 4255 return long_long(self); |
4260 | 4256 |
4261 » ndigits = (PyLongObject *)PyNumber_Index(o_ndigits); | 4257 ndigits = (PyLongObject *)PyNumber_Index(o_ndigits); |
4262 » if (ndigits == NULL) | 4258 if (ndigits == NULL) |
4263 » » return NULL; | 4259 return NULL; |
4264 | 4260 |
4265 » if (Py_SIZE(ndigits) >= 0) { | 4261 if (Py_SIZE(ndigits) >= 0) { |
4266 » » Py_DECREF(ndigits); | 4262 Py_DECREF(ndigits); |
4267 » » return long_long(self); | 4263 return long_long(self); |
4268 » } | 4264 } |
4269 | 4265 |
4270 » Py_INCREF(self); /* to keep refcounting simple */ | 4266 Py_INCREF(self); /* to keep refcounting simple */ |
4271 » /* we now own references to self, ndigits */ | 4267 /* we now own references to self, ndigits */ |
4272 | 4268 |
4273 » /* pow = 10 ** -ndigits >> 1 */ | 4269 /* pow = 10 ** -ndigits >> 1 */ |
4274 » pow = (PyLongObject *)PyLong_FromLong(10L); | 4270 pow = (PyLongObject *)PyLong_FromLong(10L); |
4275 » if (pow == NULL) | 4271 if (pow == NULL) |
4276 » » goto error; | 4272 goto error; |
4277 » temp = long_neg(ndigits); | 4273 temp = long_neg(ndigits); |
4278 » Py_DECREF(ndigits); | 4274 Py_DECREF(ndigits); |
4279 » ndigits = (PyLongObject *)temp; | 4275 ndigits = (PyLongObject *)temp; |
4280 » if (ndigits == NULL) | 4276 if (ndigits == NULL) |
4281 » » goto error; | 4277 goto error; |
4282 » temp = long_pow((PyObject *)pow, (PyObject *)ndigits, Py_None); | 4278 temp = long_pow((PyObject *)pow, (PyObject *)ndigits, Py_None); |
4283 » Py_DECREF(pow); | 4279 Py_DECREF(pow); |
4284 » pow = (PyLongObject *)temp; | 4280 pow = (PyLongObject *)temp; |
4285 » if (pow == NULL) | 4281 if (pow == NULL) |
4286 » » goto error; | 4282 goto error; |
4287 » assert(PyLong_Check(pow)); /* check long_pow returned a long */ | 4283 assert(PyLong_Check(pow)); /* check long_pow returned a long */ |
4288 » one = (PyLongObject *)PyLong_FromLong(1L); | 4284 one = (PyLongObject *)PyLong_FromLong(1L); |
4289 » if (one == NULL) | 4285 if (one == NULL) |
4290 » » goto error; | 4286 goto error; |
4291 » temp = long_rshift(pow, one); | 4287 temp = long_rshift(pow, one); |
4292 » Py_DECREF(one); | 4288 Py_DECREF(one); |
4293 » Py_DECREF(pow); | 4289 Py_DECREF(pow); |
4294 » pow = (PyLongObject *)temp; | 4290 pow = (PyLongObject *)temp; |
4295 » if (pow == NULL) | 4291 if (pow == NULL) |
4296 » » goto error; | 4292 goto error; |
4297 | 4293 |
4298 » /* q, r = divmod(self, pow) */ | 4294 /* q, r = divmod(self, pow) */ |
4299 » errcode = l_divmod((PyLongObject *)self, pow, &q, &r); | 4295 errcode = l_divmod((PyLongObject *)self, pow, &q, &r); |
4300 » if (errcode == -1) | 4296 if (errcode == -1) |
4301 » » goto error; | 4297 goto error; |
4302 | 4298 |
4303 » /* self -= r */ | 4299 /* self -= r */ |
4304 » temp = long_sub((PyLongObject *)self, r); | 4300 temp = long_sub((PyLongObject *)self, r); |
4305 » Py_DECREF(self); | 4301 Py_DECREF(self); |
4306 » self = temp; | 4302 self = temp; |
4307 » if (self == NULL) | 4303 if (self == NULL) |
4308 » » goto error; | 4304 goto error; |
4309 | 4305 |
4310 » /* get value of quotient modulo 4 */ | 4306 /* get value of quotient modulo 4 */ |
4311 » if (Py_SIZE(q) == 0) | 4307 if (Py_SIZE(q) == 0) |
4312 » » q_mod_4 = 0; | 4308 q_mod_4 = 0; |
4313 » else if (Py_SIZE(q) > 0) | 4309 else if (Py_SIZE(q) > 0) |
4314 » » q_mod_4 = q->ob_digit[0] & 3; | 4310 q_mod_4 = q->ob_digit[0] & 3; |
4315 » else | 4311 else |
4316 » » q_mod_4 = (PyLong_BASE-q->ob_digit[0]) & 3; | 4312 q_mod_4 = (PyLong_BASE-q->ob_digit[0]) & 3; |
4317 | 4313 |
4318 » if ((q_mod_4 & 1) == 1) { | 4314 if ((q_mod_4 & 1) == 1) { |
4319 » » /* q is odd; round self up or down by adding or subtracting pow
*/ | 4315 /* q is odd; round self up or down by adding or subtracting pow */ |
4320 » » if (q_mod_4 == 1 && Py_SIZE(r) == 0) | 4316 if (q_mod_4 == 1 && Py_SIZE(r) == 0) |
4321 » » » temp = (PyObject *)long_sub((PyLongObject *)self, pow); | 4317 temp = (PyObject *)long_sub((PyLongObject *)self, pow); |
4322 » » else | 4318 else |
4323 » » » temp = (PyObject *)long_add((PyLongObject *)self, pow); | 4319 temp = (PyObject *)long_add((PyLongObject *)self, pow); |
4324 » » Py_DECREF(self); | 4320 Py_DECREF(self); |
4325 » » self = temp; | 4321 self = temp; |
4326 » » if (self == NULL) | 4322 if (self == NULL) |
4327 » » » goto error; | 4323 goto error; |
4328 » } | 4324 } |
4329 » Py_DECREF(q); | 4325 Py_DECREF(q); |
4330 » Py_DECREF(r); | 4326 Py_DECREF(r); |
4331 » Py_DECREF(pow); | 4327 Py_DECREF(pow); |
4332 » Py_DECREF(ndigits); | 4328 Py_DECREF(ndigits); |
4333 » return self; | 4329 return self; |
4334 | 4330 |
4335 error: | 4331 error: |
4336 » Py_XDECREF(q); | 4332 Py_XDECREF(q); |
4337 » Py_XDECREF(r); | 4333 Py_XDECREF(r); |
4338 » Py_XDECREF(pow); | 4334 Py_XDECREF(pow); |
4339 » Py_XDECREF(self); | 4335 Py_XDECREF(self); |
4340 » Py_XDECREF(ndigits); | 4336 Py_XDECREF(ndigits); |
4341 » return NULL; | 4337 return NULL; |
4342 } | 4338 } |
4343 | 4339 |
4344 static PyObject * | 4340 static PyObject * |
4345 long_sizeof(PyLongObject *v) | 4341 long_sizeof(PyLongObject *v) |
4346 { | 4342 { |
4347 » Py_ssize_t res; | 4343 Py_ssize_t res; |
4348 | 4344 |
4349 » res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit); | 4345 res = offsetof(PyLongObject, ob_digit) + ABS(Py_SIZE(v))*sizeof(digit); |
4350 » return PyLong_FromSsize_t(res); | 4346 return PyLong_FromSsize_t(res); |
4351 } | 4347 } |
4352 | 4348 |
4353 static PyObject * | 4349 static PyObject * |
4354 long_bit_length(PyLongObject *v) | 4350 long_bit_length(PyLongObject *v) |
4355 { | 4351 { |
4356 » PyLongObject *result, *x, *y; | 4352 PyLongObject *result, *x, *y; |
4357 » Py_ssize_t ndigits, msd_bits = 0; | 4353 Py_ssize_t ndigits, msd_bits = 0; |
4358 » digit msd; | 4354 digit msd; |
4359 | 4355 |
4360 » assert(v != NULL); | 4356 assert(v != NULL); |
4361 » assert(PyLong_Check(v)); | 4357 assert(PyLong_Check(v)); |
4362 | 4358 |
4363 » ndigits = ABS(Py_SIZE(v)); | 4359 ndigits = ABS(Py_SIZE(v)); |
4364 » if (ndigits == 0) | 4360 if (ndigits == 0) |
4365 » » return PyLong_FromLong(0); | 4361 return PyLong_FromLong(0); |
4366 | 4362 |
4367 » msd = v->ob_digit[ndigits-1]; | 4363 msd = v->ob_digit[ndigits-1]; |
4368 » while (msd >= 32) { | 4364 while (msd >= 32) { |
4369 » » msd_bits += 6; | 4365 msd_bits += 6; |
4370 » » msd >>= 6; | 4366 msd >>= 6; |
4371 » } | 4367 } |
4372 » msd_bits += (long)(BitLengthTable[msd]); | 4368 msd_bits += (long)(BitLengthTable[msd]); |
4373 | 4369 |
4374 » if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) | 4370 if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT) |
4375 » » return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); | 4371 return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits); |
4376 | 4372 |
4377 » /* expression above may overflow; use Python integers instead */ | 4373 /* expression above may overflow; use Python integers instead */ |
4378 » result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); | 4374 result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1); |
4379 » if (result == NULL) | 4375 if (result == NULL) |
4380 » » return NULL; | 4376 return NULL; |
4381 » x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); | 4377 x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT); |
4382 » if (x == NULL) | 4378 if (x == NULL) |
4383 » » goto error; | 4379 goto error; |
4384 » y = (PyLongObject *)long_mul(result, x); | 4380 y = (PyLongObject *)long_mul(result, x); |
4385 » Py_DECREF(x); | 4381 Py_DECREF(x); |
4386 » if (y == NULL) | 4382 if (y == NULL) |
4387 » » goto error; | 4383 goto error; |
4388 » Py_DECREF(result); | 4384 Py_DECREF(result); |
4389 » result = y; | 4385 result = y; |
4390 | 4386 |
4391 » x = (PyLongObject *)PyLong_FromLong(msd_bits); | 4387 x = (PyLongObject *)PyLong_FromLong((long)msd_bits); |
4392 » if (x == NULL) | 4388 if (x == NULL) |
4393 » » goto error; | 4389 goto error; |
4394 » y = (PyLongObject *)long_add(result, x); | 4390 y = (PyLongObject *)long_add(result, x); |
4395 » Py_DECREF(x); | 4391 Py_DECREF(x); |
4396 » if (y == NULL) | 4392 if (y == NULL) |
4397 » » goto error; | 4393 goto error; |
4398 » Py_DECREF(result); | 4394 Py_DECREF(result); |
4399 » result = y; | 4395 result = y; |
4400 | 4396 |
4401 » return (PyObject *)result; | 4397 return (PyObject *)result; |
4402 | 4398 |
4403 error: | 4399 error: |
4404 » Py_DECREF(result); | 4400 Py_DECREF(result); |
4405 » return NULL; | 4401 return NULL; |
4406 } | 4402 } |
4407 | 4403 |
4408 PyDoc_STRVAR(long_bit_length_doc, | 4404 PyDoc_STRVAR(long_bit_length_doc, |
4409 "int.bit_length() -> int\n\ | 4405 "int.bit_length() -> int\n\ |
4410 \n\ | 4406 \n\ |
4411 Number of bits necessary to represent self in binary.\n\ | 4407 Number of bits necessary to represent self in binary.\n\ |
4412 >>> bin(37)\n\ | 4408 >>> bin(37)\n\ |
4413 '0b100101'\n\ | 4409 '0b100101'\n\ |
4414 >>> (37).bit_length()\n\ | 4410 >>> (37).bit_length()\n\ |
4415 6"); | 4411 6"); |
4416 | 4412 |
4417 #if 0 | 4413 #if 0 |
4418 static PyObject * | 4414 static PyObject * |
4419 long_is_finite(PyObject *v) | 4415 long_is_finite(PyObject *v) |
4420 { | 4416 { |
4421 » Py_RETURN_TRUE; | 4417 Py_RETURN_TRUE; |
4422 } | 4418 } |
4423 #endif | 4419 #endif |
4424 | 4420 |
4425 | 4421 |
4426 static PyObject * | 4422 static PyObject * |
4427 long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) | 4423 long_to_bytes(PyLongObject *v, PyObject *args, PyObject *kwds) |
4428 { | 4424 { |
4429 » PyObject *byteorder_str; | 4425 PyObject *byteorder_str; |
4430 » PyObject *is_signed_obj = NULL; | 4426 PyObject *is_signed_obj = NULL; |
4431 » Py_ssize_t length; | 4427 Py_ssize_t length; |
4432 » int little_endian; | 4428 int little_endian; |
4433 » int is_signed; | 4429 int is_signed; |
4434 » PyObject *bytes; | 4430 PyObject *bytes; |
4435 » static char *kwlist[] = {"length", "byteorder", "signed", NULL}; | 4431 static char *kwlist[] = {"length", "byteorder", "signed", NULL}; |
4436 | 4432 |
4437 » if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist, | 4433 if (!PyArg_ParseTupleAndKeywords(args, kwds, "nU|O:to_bytes", kwlist, |
4438 » » » » » &length, &byteorder_str, | 4434 &length, &byteorder_str, |
4439 » » » » » &is_signed_obj)) | 4435 &is_signed_obj)) |
4440 » » return NULL; | 4436 return NULL; |
4441 | 4437 |
4442 » if (args != NULL && Py_SIZE(args) > 2) { | 4438 if (args != NULL && Py_SIZE(args) > 2) { |
4443 » » PyErr_SetString(PyExc_TypeError, | 4439 PyErr_SetString(PyExc_TypeError, |
4444 » » » "'signed' is a keyword-only argument"); | 4440 "'signed' is a keyword-only argument"); |
4445 » » return NULL; | 4441 return NULL; |
4446 » } | 4442 } |
4447 | 4443 |
4448 » if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) | 4444 if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
4449 » » little_endian = 1; | 4445 little_endian = 1; |
4450 » else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) | 4446 else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
4451 » » little_endian = 0; | 4447 little_endian = 0; |
4452 » else { | 4448 else { |
4453 » » PyErr_SetString(PyExc_ValueError, | 4449 PyErr_SetString(PyExc_ValueError, |
4454 » » » "byteorder must be either 'little' or 'big'"); | 4450 "byteorder must be either 'little' or 'big'"); |
4455 » » return NULL; | 4451 return NULL; |
4456 » } | 4452 } |
4457 | 4453 |
4458 » if (is_signed_obj != NULL) { | 4454 if (is_signed_obj != NULL) { |
4459 » » int cmp = PyObject_IsTrue(is_signed_obj); | 4455 int cmp = PyObject_IsTrue(is_signed_obj); |
4460 » » if (cmp < 0) | 4456 if (cmp < 0) |
4461 » » » return NULL; | 4457 return NULL; |
4462 » » is_signed = cmp ? 1 : 0; | 4458 is_signed = cmp ? 1 : 0; |
4463 » } | 4459 } |
4464 » else { | 4460 else { |
4465 » » /* If the signed argument was omitted, use False as the | 4461 /* If the signed argument was omitted, use False as the |
4466 » » default. */ | 4462 default. */ |
4467 » » is_signed = 0; | 4463 is_signed = 0; |
4468 » } | 4464 } |
4469 | 4465 |
4470 » if (length < 0) { | 4466 if (length < 0) { |
4471 » » PyErr_SetString(PyExc_ValueError,· | 4467 PyErr_SetString(PyExc_ValueError, |
4472 » » » » "length argument must be non-negative"); | 4468 "length argument must be non-negative"); |
4473 » » return NULL; | 4469 return NULL; |
4474 » } | 4470 } |
4475 | 4471 |
4476 » bytes = PyBytes_FromStringAndSize(NULL, length); | 4472 bytes = PyBytes_FromStringAndSize(NULL, length); |
4477 » if (bytes == NULL) | 4473 if (bytes == NULL) |
4478 » » return NULL; | 4474 return NULL; |
4479 | 4475 |
4480 » if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes), | 4476 if (_PyLong_AsByteArray(v, (unsigned char *)PyBytes_AS_STRING(bytes), |
4481 » » » » length, little_endian, is_signed) < 0) { | 4477 length, little_endian, is_signed) < 0) { |
4482 » » Py_DECREF(bytes); | 4478 Py_DECREF(bytes); |
4483 » » return NULL; | 4479 return NULL; |
4484 » } | 4480 } |
4485 | 4481 |
4486 » return bytes; | 4482 return bytes; |
4487 } | 4483 } |
4488 | 4484 |
4489 PyDoc_STRVAR(long_to_bytes_doc, | 4485 PyDoc_STRVAR(long_to_bytes_doc, |
4490 "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\ | 4486 "int.to_bytes(length, byteorder, *, signed=False) -> bytes\n\ |
4491 \n\ | 4487 \n\ |
4492 Return an array of bytes representing an integer.\n\ | 4488 Return an array of bytes representing an integer.\n\ |
4493 \n\ | 4489 \n\ |
4494 The integer is represented using length bytes.» An OverflowError is\n\ | 4490 The integer is represented using length bytes. An OverflowError is\n\ |
4495 raised if the integer is not representable with the given number of\n\ | 4491 raised if the integer is not representable with the given number of\n\ |
4496 bytes.\n\ | 4492 bytes.\n\ |
4497 \n\ | 4493 \n\ |
4498 The byteorder argument determines the byte order used to represent the\n\ | 4494 The byteorder argument determines the byte order used to represent the\n\ |
4499 integer. If byteorder is 'big', the most significant byte is at the\n\ | 4495 integer. If byteorder is 'big', the most significant byte is at the\n\ |
4500 beginning of the byte array. If byteorder is 'little', the most\n\ | 4496 beginning of the byte array. If byteorder is 'little', the most\n\ |
4501 significant byte is at the end of the byte array. To request the native\n\ | 4497 significant byte is at the end of the byte array. To request the native\n\ |
4502 byte order of the host system, use `sys.byteorder' as the byte order value.\n\ | 4498 byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
4503 \n\ | 4499 \n\ |
4504 The signed keyword-only argument determines whether two's complement is\n\ | 4500 The signed keyword-only argument determines whether two's complement is\n\ |
4505 used to represent the integer.» If signed is False and a negative integer\n\ | 4501 used to represent the integer. If signed is False and a negative integer\n\ |
4506 is given, an OverflowError is raised."); | 4502 is given, an OverflowError is raised."); |
4507 | 4503 |
4508 static PyObject * | 4504 static PyObject * |
4509 long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) | 4505 long_from_bytes(PyTypeObject *type, PyObject *args, PyObject *kwds) |
4510 { | 4506 { |
4511 » PyObject *byteorder_str; | 4507 PyObject *byteorder_str; |
4512 » PyObject *is_signed_obj = NULL; | 4508 PyObject *is_signed_obj = NULL; |
4513 » int little_endian; | 4509 int little_endian; |
4514 » int is_signed; | 4510 int is_signed; |
4515 » PyObject *obj; | 4511 PyObject *obj; |
4516 » PyObject *bytes; | 4512 PyObject *bytes; |
4517 » PyObject *long_obj; | 4513 PyObject *long_obj; |
4518 » static char *kwlist[] = {"bytes", "byteorder", "signed", NULL}; | 4514 static char *kwlist[] = {"bytes", "byteorder", "signed", NULL}; |
4519 | 4515 |
4520 » if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist, | 4516 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OU|O:from_bytes", kwlist, |
4521 » » » » » &obj, &byteorder_str, | 4517 &obj, &byteorder_str, |
4522 » » » » » &is_signed_obj)) | 4518 &is_signed_obj)) |
4523 » » return NULL; | 4519 return NULL; |
4524 | 4520 |
4525 » if (args != NULL && Py_SIZE(args) > 2) { | 4521 if (args != NULL && Py_SIZE(args) > 2) { |
4526 » » PyErr_SetString(PyExc_TypeError, | 4522 PyErr_SetString(PyExc_TypeError, |
4527 » » » "'signed' is a keyword-only argument"); | 4523 "'signed' is a keyword-only argument"); |
4528 » » return NULL; | 4524 return NULL; |
4529 » } | 4525 } |
4530 | 4526 |
4531 » if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) | 4527 if (!PyUnicode_CompareWithASCIIString(byteorder_str, "little")) |
4532 » » little_endian = 1; | 4528 little_endian = 1; |
4533 » else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) | 4529 else if (!PyUnicode_CompareWithASCIIString(byteorder_str, "big")) |
4534 » » little_endian = 0; | 4530 little_endian = 0; |
4535 » else { | 4531 else { |
4536 » » PyErr_SetString(PyExc_ValueError, | 4532 PyErr_SetString(PyExc_ValueError, |
4537 » » » "byteorder must be either 'little' or 'big'"); | 4533 "byteorder must be either 'little' or 'big'"); |
4538 » » return NULL; | 4534 return NULL; |
4539 » } | 4535 } |
4540 | 4536 |
4541 » if (is_signed_obj != NULL) { | 4537 if (is_signed_obj != NULL) { |
4542 » » int cmp = PyObject_IsTrue(is_signed_obj); | 4538 int cmp = PyObject_IsTrue(is_signed_obj); |
4543 » » if (cmp < 0) | 4539 if (cmp < 0) |
4544 » » » return NULL; | 4540 return NULL; |
4545 » » is_signed = cmp ? 1 : 0; | 4541 is_signed = cmp ? 1 : 0; |
4546 » } | 4542 } |
4547 » else { | 4543 else { |
4548 » » /* If the signed argument was omitted, use False as the | 4544 /* If the signed argument was omitted, use False as the |
4549 » » default. */ | 4545 default. */ |
4550 » » is_signed = 0; | 4546 is_signed = 0; |
4551 » } | 4547 } |
4552 | 4548 |
4553 » bytes = PyObject_Bytes(obj); | 4549 bytes = PyObject_Bytes(obj); |
4554 » if (bytes == NULL) | 4550 if (bytes == NULL) |
4555 » » return NULL; | 4551 return NULL; |
4556 | 4552 |
4557 » long_obj = _PyLong_FromByteArray( | 4553 long_obj = _PyLong_FromByteArray( |
4558 » » (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), | 4554 (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes), |
4559 » » little_endian, is_signed); | 4555 little_endian, is_signed); |
4560 » Py_DECREF(bytes); | 4556 Py_DECREF(bytes); |
4561 | 4557 |
4562 » /* If from_bytes() was used on subclass, allocate new subclass | 4558 /* If from_bytes() was used on subclass, allocate new subclass |
4563 » * instance, initialize it with decoded long value and return it. | 4559 * instance, initialize it with decoded long value and return it. |
4564 » */ | 4560 */ |
4565 » if (type != &PyLong_Type && PyType_IsSubtype(type, &PyLong_Type)) { | 4561 if (type != &PyLong_Type && PyType_IsSubtype(type, &PyLong_Type)) { |
4566 » » PyLongObject *newobj; | 4562 PyLongObject *newobj; |
4567 » » int i; | 4563 int i; |
4568 » » Py_ssize_t n = ABS(Py_SIZE(long_obj)); | 4564 Py_ssize_t n = ABS(Py_SIZE(long_obj)); |
4569 | 4565 |
4570 » » newobj = (PyLongObject *)type->tp_alloc(type, n); | 4566 newobj = (PyLongObject *)type->tp_alloc(type, n); |
4571 » » if (newobj == NULL) { | 4567 if (newobj == NULL) { |
4572 » » » Py_DECREF(long_obj); | 4568 Py_DECREF(long_obj); |
4573 » » » return NULL; | 4569 return NULL; |
4574 » » } | 4570 } |
4575 » » assert(PyLong_Check(newobj)); | 4571 assert(PyLong_Check(newobj)); |
4576 » » Py_SIZE(newobj) = Py_SIZE(long_obj); | 4572 Py_SIZE(newobj) = Py_SIZE(long_obj); |
4577 » » for (i = 0; i < n; i++) { | 4573 for (i = 0; i < n; i++) { |
4578 » » » newobj->ob_digit[i] =· | 4574 newobj->ob_digit[i] = |
4579 » » » » ((PyLongObject *)long_obj)->ob_digit[i]; | 4575 ((PyLongObject *)long_obj)->ob_digit[i]; |
4580 » » } | 4576 } |
4581 » » Py_DECREF(long_obj); | 4577 Py_DECREF(long_obj); |
4582 » » return (PyObject *)newobj; | 4578 return (PyObject *)newobj; |
4583 » } | 4579 } |
4584 | 4580 |
4585 » return long_obj; | 4581 return long_obj; |
4586 } | 4582 } |
4587 | 4583 |
4588 PyDoc_STRVAR(long_from_bytes_doc, | 4584 PyDoc_STRVAR(long_from_bytes_doc, |
4589 "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\ | 4585 "int.from_bytes(bytes, byteorder, *, signed=False) -> int\n\ |
4590 \n\ | 4586 \n\ |
4591 Return the integer represented by the given array of bytes.\n\ | 4587 Return the integer represented by the given array of bytes.\n\ |
4592 \n\ | 4588 \n\ |
4593 The bytes argument must either support the buffer protocol or be an\n\ | 4589 The bytes argument must either support the buffer protocol or be an\n\ |
4594 iterable object producing bytes. Bytes and bytearray are examples of\n\ | 4590 iterable object producing bytes. Bytes and bytearray are examples of\n\ |
4595 built-in objects that support the buffer protocol.\n\ | 4591 built-in objects that support the buffer protocol.\n\ |
4596 \n\ | 4592 \n\ |
4597 The byteorder argument determines the byte order used to represent the\n\ | 4593 The byteorder argument determines the byte order used to represent the\n\ |
4598 integer. If byteorder is 'big', the most significant byte is at the\n\ | 4594 integer. If byteorder is 'big', the most significant byte is at the\n\ |
4599 beginning of the byte array. If byteorder is 'little', the most\n\ | 4595 beginning of the byte array. If byteorder is 'little', the most\n\ |
4600 significant byte is at the end of the byte array. To request the native\n\ | 4596 significant byte is at the end of the byte array. To request the native\n\ |
4601 byte order of the host system, use `sys.byteorder' as the byte order value.\n\ | 4597 byte order of the host system, use `sys.byteorder' as the byte order value.\n\ |
4602 \n\ | 4598 \n\ |
4603 The signed keyword-only argument indicates whether two's complement is\n\ | 4599 The signed keyword-only argument indicates whether two's complement is\n\ |
4604 used to represent the integer."); | 4600 used to represent the integer."); |
4605 | 4601 |
4606 static PyMethodDef long_methods[] = { | 4602 static PyMethodDef long_methods[] = { |
4607 » {"conjugate",» (PyCFunction)long_long,»METH_NOARGS, | 4603 {"conjugate", (PyCFunction)long_long, METH_NOARGS, |
4608 » "Returns self, the complex conjugate of any int."}, | 4604 "Returns self, the complex conjugate of any int."}, |
4609 » {"bit_length",» (PyCFunction)long_bit_length, METH_NOARGS, | 4605 {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS, |
4610 » long_bit_length_doc}, | 4606 long_bit_length_doc}, |
4611 #if 0 | 4607 #if 0 |
4612 » {"is_finite",» (PyCFunction)long_is_finite,» METH_NOARGS, | 4608 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS, |
4613 » "Returns always True."}, | 4609 "Returns always True."}, |
4614 #endif | 4610 #endif |
4615 » {"to_bytes",» (PyCFunction)long_to_bytes, | 4611 {"to_bytes", (PyCFunction)long_to_bytes, |
4616 » METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, | 4612 METH_VARARGS|METH_KEYWORDS, long_to_bytes_doc}, |
4617 » {"from_bytes",» (PyCFunction)long_from_bytes, | 4613 {"from_bytes", (PyCFunction)long_from_bytes, |
4618 » METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes_doc}, | 4614 METH_VARARGS|METH_KEYWORDS|METH_CLASS, long_from_bytes_doc}, |
4619 » {"__trunc__",» (PyCFunction)long_long,»METH_NOARGS, | 4615 {"__trunc__", (PyCFunction)long_long, METH_NOARGS, |
4620 "Truncating an Integral returns itself."}, | 4616 "Truncating an Integral returns itself."}, |
4621 » {"__floor__",» (PyCFunction)long_long,»METH_NOARGS, | 4617 {"__floor__", (PyCFunction)long_long, METH_NOARGS, |
4622 "Flooring an Integral returns itself."}, | 4618 "Flooring an Integral returns itself."}, |
4623 » {"__ceil__",» (PyCFunction)long_long,»METH_NOARGS, | 4619 {"__ceil__", (PyCFunction)long_long, METH_NOARGS, |
4624 "Ceiling of an Integral returns itself."}, | 4620 "Ceiling of an Integral returns itself."}, |
4625 » {"__round__",» (PyCFunction)long_round, METH_VARARGS, | 4621 {"__round__", (PyCFunction)long_round, METH_VARARGS, |
4626 » "Rounding an Integral returns itself.\n" | 4622 "Rounding an Integral returns itself.\n" |
4627 » "Rounding with an ndigits argument also returns an integer."}, | 4623 "Rounding with an ndigits argument also returns an integer."}, |
4628 » {"__getnewargs__",» (PyCFunction)long_getnewargs,» METH_NOARGS}, | 4624 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, |
4629 {"__format__", (PyCFunction)long__format__, METH_VARARGS}, | 4625 {"__format__", (PyCFunction)long__format__, METH_VARARGS}, |
4630 » {"__sizeof__",» (PyCFunction)long_sizeof, METH_NOARGS, | 4626 {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS, |
4631 » "Returns size in memory, in bytes"}, | 4627 "Returns size in memory, in bytes"}, |
4632 » {NULL,» » NULL}» » /* sentinel */ | 4628 {NULL, NULL} /* sentinel */ |
4633 }; | 4629 }; |
4634 | 4630 |
4635 static PyGetSetDef long_getset[] = { | 4631 static PyGetSetDef long_getset[] = { |
4636 {"real", | 4632 {"real", |
4637 (getter)long_long, (setter)NULL, | 4633 (getter)long_long, (setter)NULL, |
4638 "the real part of a complex number", | 4634 "the real part of a complex number", |
4639 NULL}, | 4635 NULL}, |
4640 {"imag", | 4636 {"imag", |
4641 (getter)long_get0, (setter)NULL, | 4637 (getter)long_get0, (setter)NULL, |
4642 "the imaginary part of a complex number", | 4638 "the imaginary part of a complex number", |
(...skipping 12 matching lines...) Expand all Loading... |
4655 PyDoc_STRVAR(long_doc, | 4651 PyDoc_STRVAR(long_doc, |
4656 "int(x[, base]) -> integer\n\ | 4652 "int(x[, base]) -> integer\n\ |
4657 \n\ | 4653 \n\ |
4658 Convert a string or number to an integer, if possible. A floating\n\ | 4654 Convert a string or number to an integer, if possible. A floating\n\ |
4659 point argument will be truncated towards zero (this does not include a\n\ | 4655 point argument will be truncated towards zero (this does not include a\n\ |
4660 string representation of a floating point number!) When converting a\n\ | 4656 string representation of a floating point number!) When converting a\n\ |
4661 string, use the optional base. It is an error to supply a base when\n\ | 4657 string, use the optional base. It is an error to supply a base when\n\ |
4662 converting a non-string."); | 4658 converting a non-string."); |
4663 | 4659 |
4664 static PyNumberMethods long_as_number = { | 4660 static PyNumberMethods long_as_number = { |
4665 » (binaryfunc)» long_add,» /*nb_add*/ | 4661 (binaryfunc)long_add, /*nb_add*/ |
4666 » (binaryfunc)» long_sub,» /*nb_subtract*/ | 4662 (binaryfunc)long_sub, /*nb_subtract*/ |
4667 » (binaryfunc)» long_mul,» /*nb_multiply*/ | 4663 (binaryfunc)long_mul, /*nb_multiply*/ |
4668 » » » long_mod,» /*nb_remainder*/ | 4664 long_mod, /*nb_remainder*/ |
4669 » » » long_divmod,» /*nb_divmod*/ | 4665 long_divmod, /*nb_divmod*/ |
4670 » » » long_pow,» /*nb_power*/ | 4666 long_pow, /*nb_power*/ |
4671 » (unaryfunc) » long_neg,» /*nb_negative*/ | 4667 (unaryfunc)long_neg, /*nb_negative*/ |
4672 » (unaryfunc) » long_long,» /*tp_positive*/ | 4668 (unaryfunc)long_long, /*tp_positive*/ |
4673 » (unaryfunc) » long_abs,» /*tp_absolute*/ | 4669 (unaryfunc)long_abs, /*tp_absolute*/ |
4674 » (inquiry)» long_bool,» /*tp_bool*/ | 4670 (inquiry)long_bool, /*tp_bool*/ |
4675 » (unaryfunc)» long_invert,» /*nb_invert*/ | 4671 (unaryfunc)long_invert, /*nb_invert*/ |
4676 » » » long_lshift,» /*nb_lshift*/ | 4672 long_lshift, /*nb_lshift*/ |
4677 » (binaryfunc)» long_rshift,» /*nb_rshift*/ | 4673 (binaryfunc)long_rshift, /*nb_rshift*/ |
4678 » » » long_and,» /*nb_and*/ | 4674 long_and, /*nb_and*/ |
4679 » » » long_xor,» /*nb_xor*/ | 4675 long_xor, /*nb_xor*/ |
4680 » » » long_or,» /*nb_or*/ | 4676 long_or, /*nb_or*/ |
4681 » » » long_long,» /*nb_int*/ | 4677 long_long, /*nb_int*/ |
4682 » 0,» » » » /*nb_reserved*/ | 4678 0, /*nb_reserved*/ |
4683 » » » long_float,» /*nb_float*/ | 4679 long_float, /*nb_float*/ |
4684 » 0,» » » » /* nb_inplace_add */ | 4680 0, /* nb_inplace_add */ |
4685 » 0,» » » » /* nb_inplace_subtract */ | 4681 0, /* nb_inplace_subtract */ |
4686 » 0,» » » » /* nb_inplace_multiply */ | 4682 0, /* nb_inplace_multiply */ |
4687 » 0,» » » » /* nb_inplace_remainder */ | 4683 0, /* nb_inplace_remainder */ |
4688 » 0,» » » » /* nb_inplace_power */ | 4684 0, /* nb_inplace_power */ |
4689 » 0,» » » » /* nb_inplace_lshift */ | 4685 0, /* nb_inplace_lshift */ |
4690 » 0,» » » » /* nb_inplace_rshift */ | 4686 0, /* nb_inplace_rshift */ |
4691 » 0,» » » » /* nb_inplace_and */ | 4687 0, /* nb_inplace_and */ |
4692 » 0,» » » » /* nb_inplace_xor */ | 4688 0, /* nb_inplace_xor */ |
4693 » 0,» » » » /* nb_inplace_or */ | 4689 0, /* nb_inplace_or */ |
4694 » long_div,» » » /* nb_floor_divide */ | 4690 long_div, /* nb_floor_divide */ |
4695 » long_true_divide,» » /* nb_true_divide */ | 4691 long_true_divide, /* nb_true_divide */ |
4696 » 0,» » » » /* nb_inplace_floor_divide */ | 4692 0, /* nb_inplace_floor_divide */ |
4697 » 0,» » » » /* nb_inplace_true_divide */ | 4693 0, /* nb_inplace_true_divide */ |
4698 » long_long,» » » /* nb_index */ | 4694 long_long, /* nb_index */ |
4699 }; | 4695 }; |
4700 | 4696 |
4701 PyTypeObject PyLong_Type = { | 4697 PyTypeObject PyLong_Type = { |
4702 » PyVarObject_HEAD_INIT(&PyType_Type, 0) | 4698 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
4703 » "int",» » » » » /* tp_name */ | 4699 "int", /* tp_name */ |
4704 » offsetof(PyLongObject, ob_digit),» /* tp_basicsize */ | 4700 offsetof(PyLongObject, ob_digit), /* tp_basicsize */ |
4705 » sizeof(digit),» » » » /* tp_itemsize */ | 4701 sizeof(digit), /* tp_itemsize */ |
4706 » long_dealloc,» » » » /* tp_dealloc */ | 4702 long_dealloc, /* tp_dealloc */ |
4707 » 0,» » » » » /* tp_print */ | 4703 0, /* tp_print */ |
4708 » 0,» » » » » /* tp_getattr */ | 4704 0, /* tp_getattr */ |
4709 » 0,» » » » » /* tp_setattr */ | 4705 0, /* tp_setattr */ |
4710 » 0,» » » » » /* tp_reserved */ | 4706 0, /* tp_reserved */ |
4711 » long_to_decimal_string,»» » /* tp_repr */ | 4707 long_to_decimal_string, /* tp_repr */ |
4712 » &long_as_number,» » » /* tp_as_number */ | 4708 &long_as_number, /* tp_as_number */ |
4713 » 0,» » » » » /* tp_as_sequence */ | 4709 0, /* tp_as_sequence */ |
4714 » 0,» » » » » /* tp_as_mapping */ | 4710 0, /* tp_as_mapping */ |
4715 » (hashfunc)long_hash,» » » /* tp_hash */ | 4711 (hashfunc)long_hash, /* tp_hash */ |
4716 » 0,» » » » » /* tp_call */ | 4712 0, /* tp_call */ |
4717 » long_to_decimal_string,»» » /* tp_str */ | 4713 long_to_decimal_string, /* tp_str */ |
4718 » PyObject_GenericGetAttr,» » /* tp_getattro */ | 4714 PyObject_GenericGetAttr, /* tp_getattro */ |
4719 » 0,» » » » » /* tp_setattro */ | 4715 0, /* tp_setattro */ |
4720 » 0,» » » » » /* tp_as_buffer */ | 4716 0, /* tp_as_buffer */ |
4721 » Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | | 4717 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
4722 » » Py_TPFLAGS_LONG_SUBCLASS,» /* tp_flags */ | 4718 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */ |
4723 » long_doc,» » » » /* tp_doc */ | 4719 long_doc, /* tp_doc */ |
4724 » 0,» » » » » /* tp_traverse */ | 4720 0, /* tp_traverse */ |
4725 » 0,» » » » » /* tp_clear */ | 4721 0, /* tp_clear */ |
4726 » long_richcompare,» » » /* tp_richcompare */ | 4722 long_richcompare, /* tp_richcompare */ |
4727 » 0,» » » » » /* tp_weaklistoffset */ | 4723 0, /* tp_weaklistoffset */ |
4728 » 0,» » » » » /* tp_iter */ | 4724 0, /* tp_iter */ |
4729 » 0,» » » » » /* tp_iternext */ | 4725 0, /* tp_iternext */ |
4730 » long_methods,» » » » /* tp_methods */ | 4726 long_methods, /* tp_methods */ |
4731 » 0,» » » » » /* tp_members */ | 4727 0, /* tp_members */ |
4732 » long_getset,» » » » /* tp_getset */ | 4728 long_getset, /* tp_getset */ |
4733 » 0,» » » » » /* tp_base */ | 4729 0, /* tp_base */ |
4734 » 0,» » » » » /* tp_dict */ | 4730 0, /* tp_dict */ |
4735 » 0,» » » » » /* tp_descr_get */ | 4731 0, /* tp_descr_get */ |
4736 » 0,» » » » » /* tp_descr_set */ | 4732 0, /* tp_descr_set */ |
4737 » 0,» » » » » /* tp_dictoffset */ | 4733 0, /* tp_dictoffset */ |
4738 » 0,» » » » » /* tp_init */ | 4734 0, /* tp_init */ |
4739 » 0,» » » » » /* tp_alloc */ | 4735 0, /* tp_alloc */ |
4740 » long_new,» » » » /* tp_new */ | 4736 long_new, /* tp_new */ |
4741 » PyObject_Del,» » » » /* tp_free */ | 4737 PyObject_Del, /* tp_free */ |
4742 }; | 4738 }; |
4743 | 4739 |
4744 static PyTypeObject Int_InfoType; | 4740 static PyTypeObject Int_InfoType; |
4745 | 4741 |
4746 PyDoc_STRVAR(int_info__doc__, | 4742 PyDoc_STRVAR(int_info__doc__, |
4747 "sys.int_info\n\ | 4743 "sys.int_info\n\ |
4748 \n\ | 4744 \n\ |
4749 A struct sequence that holds information about Python's\n\ | 4745 A struct sequence that holds information about Python's\n\ |
4750 internal representation of integers. The attributes are read only."); | 4746 internal representation of integers. The attributes are read only."); |
4751 | 4747 |
4752 static PyStructSequence_Field int_info_fields[] = { | 4748 static PyStructSequence_Field int_info_fields[] = { |
4753 » {"bits_per_digit", "size of a digit in bits"}, | 4749 {"bits_per_digit", "size of a digit in bits"}, |
4754 » {"sizeof_digit", "size in bytes of the C type used to " | 4750 {"sizeof_digit", "size in bytes of the C type used to represent a digit"}, |
4755 » "represent a digit"}, | 4751 {NULL, NULL} |
4756 » {NULL, NULL} | |
4757 }; | 4752 }; |
4758 | 4753 |
4759 static PyStructSequence_Desc int_info_desc = { | 4754 static PyStructSequence_Desc int_info_desc = { |
4760 » "sys.int_info", /* name */ | 4755 "sys.int_info", /* name */ |
4761 » int_info__doc__, /* doc */ | 4756 int_info__doc__, /* doc */ |
4762 » int_info_fields, /* fields */ | 4757 int_info_fields, /* fields */ |
4763 » 2 /* number of fields */ | 4758 2 /* number of fields */ |
4764 }; | 4759 }; |
4765 | 4760 |
4766 PyObject * | 4761 PyObject * |
4767 PyLong_GetInfo(void) | 4762 PyLong_GetInfo(void) |
4768 { | 4763 { |
4769 » PyObject* int_info; | 4764 PyObject* int_info; |
4770 » int field = 0; | 4765 int field = 0; |
4771 » int_info = PyStructSequence_New(&Int_InfoType); | 4766 int_info = PyStructSequence_New(&Int_InfoType); |
4772 » if (int_info == NULL) | 4767 if (int_info == NULL) |
4773 » » return NULL; | 4768 return NULL; |
4774 » PyStructSequence_SET_ITEM(int_info, field++, | 4769 PyStructSequence_SET_ITEM(int_info, field++, |
4775 » » » » PyLong_FromLong(PyLong_SHIFT)); | 4770 PyLong_FromLong(PyLong_SHIFT)); |
4776 » PyStructSequence_SET_ITEM(int_info, field++, | 4771 PyStructSequence_SET_ITEM(int_info, field++, |
4777 » » » » PyLong_FromLong(sizeof(digit))); | 4772 PyLong_FromLong(sizeof(digit))); |
4778 » if (PyErr_Occurred()) { | 4773 if (PyErr_Occurred()) { |
4779 » » Py_CLEAR(int_info); | 4774 Py_CLEAR(int_info); |
4780 » » return NULL; | 4775 return NULL; |
4781 » } | 4776 } |
4782 » return int_info; | 4777 return int_info; |
4783 } | 4778 } |
4784 | 4779 |
4785 int | 4780 int |
4786 _PyLong_Init(void) | 4781 _PyLong_Init(void) |
4787 { | 4782 { |
4788 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 4783 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
4789 » int ival, size; | 4784 int ival, size; |
4790 » PyLongObject *v = small_ints; | 4785 PyLongObject *v = small_ints; |
4791 | 4786 |
4792 » for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { | 4787 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) { |
4793 » » size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); | 4788 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1); |
4794 » » if (Py_TYPE(v) == &PyLong_Type) { | 4789 if (Py_TYPE(v) == &PyLong_Type) { |
4795 » » » /* The element is already initialized, most likely | 4790 /* The element is already initialized, most likely |
4796 » » » * the Python interpreter was initialized before. | 4791 * the Python interpreter was initialized before. |
4797 » » » */ | 4792 */ |
4798 » » » Py_ssize_t refcnt; | 4793 Py_ssize_t refcnt; |
4799 » » » PyObject* op = (PyObject*)v; | 4794 PyObject* op = (PyObject*)v; |
4800 | 4795 |
4801 » » » refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); | 4796 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op); |
4802 » » » _Py_NewReference(op); | 4797 _Py_NewReference(op); |
4803 » » » /* _Py_NewReference sets the ref count to 1 but | 4798 /* _Py_NewReference sets the ref count to 1 but |
4804 » » » * the ref count might be larger. Set the refcnt | 4799 * the ref count might be larger. Set the refcnt |
4805 » » » * to the original refcnt + 1 */»········ | 4800 * to the original refcnt + 1 */ |
4806 » » » Py_REFCNT(op) = refcnt + 1; | 4801 Py_REFCNT(op) = refcnt + 1; |
4807 » » » assert(Py_SIZE(op) == size); | 4802 assert(Py_SIZE(op) == size); |
4808 » » » assert(v->ob_digit[0] == abs(ival)); | 4803 assert(v->ob_digit[0] == abs(ival)); |
4809 » » } | 4804 } |
4810 » » else { | 4805 else { |
4811 » » » PyObject_INIT(v, &PyLong_Type); | 4806 PyObject_INIT(v, &PyLong_Type); |
4812 » » } | 4807 } |
4813 » » Py_SIZE(v) = size; | 4808 Py_SIZE(v) = size; |
4814 » » v->ob_digit[0] = abs(ival); | 4809 v->ob_digit[0] = abs(ival); |
4815 » } | 4810 } |
4816 #endif | 4811 #endif |
4817 » /* initialize int_info */ | 4812 /* initialize int_info */ |
4818 » if (Int_InfoType.tp_name == 0) | 4813 if (Int_InfoType.tp_name == 0) |
4819 » » PyStructSequence_InitType(&Int_InfoType, &int_info_desc); | 4814 PyStructSequence_InitType(&Int_InfoType, &int_info_desc); |
4820 | 4815 |
4821 » return 1; | 4816 return 1; |
4822 } | 4817 } |
4823 | 4818 |
4824 void | 4819 void |
4825 PyLong_Fini(void) | 4820 PyLong_Fini(void) |
4826 { | 4821 { |
4827 » /* Integers are currently statically allocated. Py_DECREF is not | 4822 /* Integers are currently statically allocated. Py_DECREF is not |
4828 » needed, but Python must forget about the reference or multiple | 4823 needed, but Python must forget about the reference or multiple |
4829 » reinitializations will fail. */ | 4824 reinitializations will fail. */ |
4830 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 | 4825 #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
4831 » int i; | 4826 int i; |
4832 » PyLongObject *v = small_ints; | 4827 PyLongObject *v = small_ints; |
4833 » for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { | 4828 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) { |
4834 » » _Py_DEC_REFTOTAL; | 4829 _Py_DEC_REFTOTAL; |
4835 » » _Py_ForgetReference((PyObject*)v); | 4830 _Py_ForgetReference((PyObject*)v); |
4836 » } | 4831 } |
4837 #endif | 4832 #endif |
4838 } | 4833 } |
LEFT | RIGHT |