blob: 708934c51fe25089c231973006b2f2a53e35593c [file] [log] [blame]
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001/* Long (arbitrary precision) integer object implementation */
2
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003/* XXX The functional organization of this file is terrible */
4
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005#include "Python.h"
Guido van Rossumedcc38a1991-05-05 20:09:44 +00006#include "longintrepr.h"
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007
Mark Dickinsonc6300392009-04-20 21:38:00 +00008#include <float.h>
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00009#include <ctype.h>
Mark Dickinson5a74bf62009-02-15 11:04:38 +000010#include <stddef.h>
Guido van Rossumedcc38a1991-05-05 20:09:44 +000011
Serhiy Storchaka495e8802017-02-01 23:12:20 +020012#include "clinic/longobject.c.h"
13/*[clinic input]
14class int "PyObject *" "&PyLong_Type"
15[clinic start generated code]*/
16/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ec0275e3422a36e3]*/
17
Guido van Rossumddefaf32007-01-14 03:31:43 +000018#ifndef NSMALLPOSINTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019#define NSMALLPOSINTS 257
Guido van Rossumddefaf32007-01-14 03:31:43 +000020#endif
21#ifndef NSMALLNEGINTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022#define NSMALLNEGINTS 5
Guido van Rossumddefaf32007-01-14 03:31:43 +000023#endif
Facundo Batista6e6f59b2008-07-24 18:57:11 +000024
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +020025_Py_IDENTIFIER(little);
26_Py_IDENTIFIER(big);
27
Mark Dickinsone4416742009-02-15 15:14:57 +000028/* convert a PyLong of size 1, 0 or -1 to an sdigit */
Victor Stinner08a80b12013-07-17 22:33:42 +020029#define MEDIUM_VALUE(x) (assert(-1 <= Py_SIZE(x) && Py_SIZE(x) <= 1), \
30 Py_SIZE(x) < 0 ? -(sdigit)(x)->ob_digit[0] : \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 (Py_SIZE(x) == 0 ? (sdigit)0 : \
32 (sdigit)(x)->ob_digit[0]))
Facundo Batista6e6f59b2008-07-24 18:57:11 +000033
Serhiy Storchakaba85d692017-03-30 09:09:41 +030034PyObject *_PyLong_Zero = NULL;
35PyObject *_PyLong_One = NULL;
36
Guido van Rossumddefaf32007-01-14 03:31:43 +000037#if NSMALLNEGINTS + NSMALLPOSINTS > 0
38/* Small integers are preallocated in this array so that they
39 can be shared.
40 The integers that are preallocated are those in the range
41 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
42*/
43static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
44#ifdef COUNT_ALLOCS
Pablo Galindo49c75a82018-10-28 15:02:17 +000045Py_ssize_t _Py_quick_int_allocs, _Py_quick_neg_int_allocs;
Guido van Rossumddefaf32007-01-14 03:31:43 +000046#endif
47
Guido van Rossum7eaf8222007-06-18 17:58:50 +000048static PyObject *
Mark Dickinsone4416742009-02-15 15:14:57 +000049get_small_int(sdigit ival)
Guido van Rossumddefaf32007-01-14 03:31:43 +000050{
Benjamin Peterson45c9dce2014-03-14 21:53:51 -050051 PyObject *v;
Benjamin Peterson041c38a2014-03-14 21:47:23 -050052 assert(-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS);
Benjamin Peterson45c9dce2014-03-14 21:53:51 -050053 v = (PyObject *)&small_ints[ival + NSMALLNEGINTS];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 Py_INCREF(v);
Guido van Rossumddefaf32007-01-14 03:31:43 +000055#ifdef COUNT_ALLOCS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 if (ival >= 0)
Pablo Galindo49c75a82018-10-28 15:02:17 +000057 _Py_quick_int_allocs++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 else
Pablo Galindo49c75a82018-10-28 15:02:17 +000059 _Py_quick_neg_int_allocs++;
Guido van Rossumddefaf32007-01-14 03:31:43 +000060#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 return v;
Guido van Rossumddefaf32007-01-14 03:31:43 +000062}
63#define CHECK_SMALL_INT(ival) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
65 return get_small_int((sdigit)ival); \
66 } while(0)
Guido van Rossumddefaf32007-01-14 03:31:43 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068static PyLongObject *
Facundo Batista6e6f59b2008-07-24 18:57:11 +000069maybe_small_long(PyLongObject *v)
70{
Victor Stinner45e8e2f2014-05-14 17:24:35 +020071 if (v && Py_ABS(Py_SIZE(v)) <= 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 sdigit ival = MEDIUM_VALUE(v);
73 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
74 Py_DECREF(v);
75 return (PyLongObject *)get_small_int(ival);
76 }
77 }
78 return v;
Facundo Batista6e6f59b2008-07-24 18:57:11 +000079}
Guido van Rossumddefaf32007-01-14 03:31:43 +000080#else
81#define CHECK_SMALL_INT(ival)
Facundo Batista6e6f59b2008-07-24 18:57:11 +000082#define maybe_small_long(val) (val)
Guido van Rossumddefaf32007-01-14 03:31:43 +000083#endif
84
Serhiy Storchaka95949422013-08-27 19:40:23 +030085/* If a freshly-allocated int is already shared, it must
Guido van Rossumddefaf32007-01-14 03:31:43 +000086 be a small integer, so negating it must go to PyLong_FromLong */
Victor Stinner8aed6f12013-07-17 22:31:17 +020087Py_LOCAL_INLINE(void)
88_PyLong_Negate(PyLongObject **x_p)
89{
90 PyLongObject *x;
91
92 x = (PyLongObject *)*x_p;
93 if (Py_REFCNT(x) == 1) {
94 Py_SIZE(x) = -Py_SIZE(x);
95 return;
96 }
97
98 *x_p = (PyLongObject *)PyLong_FromLong(-MEDIUM_VALUE(x));
99 Py_DECREF(x);
100}
101
Serhiy Storchaka95949422013-08-27 19:40:23 +0300102/* For int multiplication, use the O(N**2) school algorithm unless
Tim Peters5af4e6c2002-08-12 02:31:19 +0000103 * both operands contain more than KARATSUBA_CUTOFF digits (this
Serhiy Storchaka95949422013-08-27 19:40:23 +0300104 * being an internal Python int digit, in base BASE).
Tim Peters5af4e6c2002-08-12 02:31:19 +0000105 */
Tim Peters0973b992004-08-29 22:16:50 +0000106#define KARATSUBA_CUTOFF 70
107#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
Tim Peters5af4e6c2002-08-12 02:31:19 +0000108
Tim Peters47e52ee2004-08-30 02:44:38 +0000109/* For exponentiation, use the binary left-to-right algorithm
110 * unless the exponent contains more than FIVEARY_CUTOFF digits.
111 * In that case, do 5 bits at a time. The potential drawback is that
112 * a table of 2**5 intermediate results is computed.
113 */
114#define FIVEARY_CUTOFF 8
115
Mark Dickinsoncdd01d22010-05-10 21:37:34 +0000116#define SIGCHECK(PyTryBlock) \
117 do { \
118 if (PyErr_CheckSignals()) PyTryBlock \
119 } while(0)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +0000120
Serhiy Storchaka95949422013-08-27 19:40:23 +0300121/* Normalize (remove leading zeros from) an int object.
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000122 Doesn't attempt to free the storage--in most cases, due to the nature
123 of the algorithms used, this could save at most be one word anyway. */
124
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125static PyLongObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200126long_normalize(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000127{
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200128 Py_ssize_t j = Py_ABS(Py_SIZE(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 Py_ssize_t i = j;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 while (i > 0 && v->ob_digit[i-1] == 0)
132 --i;
133 if (i != j)
134 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
135 return v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000136}
137
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200138/* _PyLong_FromNbInt: Convert the given object to a PyLongObject
139 using the nb_int slot, if available. Raise TypeError if either the
140 nb_int slot is not available or the result of the call to nb_int
141 returns something not of type int.
142*/
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200143PyObject *
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200144_PyLong_FromNbInt(PyObject *integral)
145{
146 PyNumberMethods *nb;
147 PyObject *result;
148
149 /* Fast path for the case that we already have an int. */
150 if (PyLong_CheckExact(integral)) {
151 Py_INCREF(integral);
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200152 return integral;
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200153 }
154
155 nb = Py_TYPE(integral)->tp_as_number;
156 if (nb == NULL || nb->nb_int == NULL) {
157 PyErr_Format(PyExc_TypeError,
158 "an integer is required (got type %.200s)",
159 Py_TYPE(integral)->tp_name);
160 return NULL;
161 }
162
163 /* Convert using the nb_int slot, which should return something
164 of exact type int. */
165 result = nb->nb_int(integral);
166 if (!result || PyLong_CheckExact(result))
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200167 return result;
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200168 if (!PyLong_Check(result)) {
169 PyErr_Format(PyExc_TypeError,
170 "__int__ returned non-int (type %.200s)",
171 result->ob_type->tp_name);
172 Py_DECREF(result);
173 return NULL;
174 }
175 /* Issue #17576: warn if 'result' not of exact type int. */
176 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
177 "__int__ returned non-int (type %.200s). "
178 "The ability to return an instance of a strict subclass of int "
179 "is deprecated, and may be removed in a future version of Python.",
180 result->ob_type->tp_name)) {
181 Py_DECREF(result);
182 return NULL;
183 }
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200184 return result;
185}
186
187/* Convert the given object to a PyLongObject using the nb_index or
188 nb_int slots, if available (the latter is deprecated).
189 Raise TypeError if either nb_index and nb_int slots are not
190 available or the result of the call to nb_index or nb_int
191 returns something not of type int.
192 Should be replaced with PyNumber_Index after the end of the
193 deprecation period.
194*/
195PyObject *
196_PyLong_FromNbIndexOrNbInt(PyObject *integral)
197{
198 PyNumberMethods *nb;
199 PyObject *result;
200
201 /* Fast path for the case that we already have an int. */
202 if (PyLong_CheckExact(integral)) {
203 Py_INCREF(integral);
204 return integral;
205 }
206
207 nb = Py_TYPE(integral)->tp_as_number;
208 if (nb == NULL || (nb->nb_index == NULL && nb->nb_int == NULL)) {
209 PyErr_Format(PyExc_TypeError,
210 "an integer is required (got type %.200s)",
211 Py_TYPE(integral)->tp_name);
212 return NULL;
213 }
214
215 if (nb->nb_index) {
216 /* Convert using the nb_index slot, which should return something
217 of exact type int. */
218 result = nb->nb_index(integral);
219 if (!result || PyLong_CheckExact(result))
220 return result;
221 if (!PyLong_Check(result)) {
222 PyErr_Format(PyExc_TypeError,
223 "__index__ returned non-int (type %.200s)",
224 result->ob_type->tp_name);
225 Py_DECREF(result);
226 return NULL;
227 }
228 /* Issue #17576: warn if 'result' not of exact type int. */
229 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
230 "__index__ returned non-int (type %.200s). "
231 "The ability to return an instance of a strict subclass of int "
232 "is deprecated, and may be removed in a future version of Python.",
233 result->ob_type->tp_name))
234 {
235 Py_DECREF(result);
236 return NULL;
237 }
238 return result;
239 }
240
241 result = _PyLong_FromNbInt(integral);
242 if (result && PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
243 "an integer is required (got type %.200s). "
244 "Implicit conversion to integers using __int__ is deprecated, "
245 "and may be removed in a future version of Python.",
246 Py_TYPE(integral)->tp_name))
247 {
248 Py_DECREF(result);
249 return NULL;
250 }
251 return result;
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200252}
253
254
Serhiy Storchaka95949422013-08-27 19:40:23 +0300255/* Allocate a new int object with size digits.
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000256 Return NULL and set exception if we run out of memory. */
257
Mark Dickinson5a74bf62009-02-15 11:04:38 +0000258#define MAX_LONG_DIGITS \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
Mark Dickinson5a74bf62009-02-15 11:04:38 +0000260
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261PyLongObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000262_PyLong_New(Py_ssize_t size)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PyLongObject *result;
265 /* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
266 sizeof(digit)*size. Previous incarnations of this code used
267 sizeof(PyVarObject) instead of the offsetof, but this risks being
268 incorrect in the presence of padding between the PyVarObject header
269 and the digits. */
270 if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
271 PyErr_SetString(PyExc_OverflowError,
272 "too many digits in integer");
273 return NULL;
274 }
275 result = PyObject_MALLOC(offsetof(PyLongObject, ob_digit) +
276 size*sizeof(digit));
277 if (!result) {
278 PyErr_NoMemory();
279 return NULL;
280 }
Victor Stinnerb509d522018-11-23 14:27:38 +0100281 return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000282}
283
Tim Peters64b5ce32001-09-10 20:52:51 +0000284PyObject *
285_PyLong_Copy(PyLongObject *src)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 PyLongObject *result;
288 Py_ssize_t i;
Tim Peters64b5ce32001-09-10 20:52:51 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 assert(src != NULL);
291 i = Py_SIZE(src);
292 if (i < 0)
293 i = -(i);
294 if (i < 2) {
Mark Dickinsonbcc17ee2012-04-20 21:42:49 +0100295 sdigit ival = MEDIUM_VALUE(src);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 CHECK_SMALL_INT(ival);
297 }
298 result = _PyLong_New(i);
299 if (result != NULL) {
300 Py_SIZE(result) = Py_SIZE(src);
301 while (--i >= 0)
302 result->ob_digit[i] = src->ob_digit[i];
303 }
304 return (PyObject *)result;
Tim Peters64b5ce32001-09-10 20:52:51 +0000305}
306
Serhiy Storchaka95949422013-08-27 19:40:23 +0300307/* Create a new int object from a C long int */
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000308
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000310PyLong_FromLong(long ival)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyLongObject *v;
313 unsigned long abs_ival;
314 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
315 int ndigits = 0;
Mark Dickinson36820dd2016-09-10 20:17:36 +0100316 int sign;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 CHECK_SMALL_INT(ival);
Tim Petersce9de2f2001-06-14 04:56:19 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (ival < 0) {
321 /* negate: can't write this as abs_ival = -ival since that
322 invokes undefined behaviour when ival is LONG_MIN */
323 abs_ival = 0U-(unsigned long)ival;
324 sign = -1;
325 }
326 else {
327 abs_ival = (unsigned long)ival;
Mark Dickinson36820dd2016-09-10 20:17:36 +0100328 sign = ival == 0 ? 0 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 }
Tim Petersce9de2f2001-06-14 04:56:19 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 /* Fast path for single-digit ints */
332 if (!(abs_ival >> PyLong_SHIFT)) {
333 v = _PyLong_New(1);
334 if (v) {
335 Py_SIZE(v) = sign;
336 v->ob_digit[0] = Py_SAFE_DOWNCAST(
337 abs_ival, unsigned long, digit);
338 }
339 return (PyObject*)v;
340 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000341
Mark Dickinson249b8982009-04-27 19:41:00 +0000342#if PyLong_SHIFT==15
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* 2 digits */
344 if (!(abs_ival >> 2*PyLong_SHIFT)) {
345 v = _PyLong_New(2);
346 if (v) {
347 Py_SIZE(v) = 2*sign;
348 v->ob_digit[0] = Py_SAFE_DOWNCAST(
349 abs_ival & PyLong_MASK, unsigned long, digit);
350 v->ob_digit[1] = Py_SAFE_DOWNCAST(
351 abs_ival >> PyLong_SHIFT, unsigned long, digit);
352 }
353 return (PyObject*)v;
354 }
Mark Dickinsonbd792642009-03-18 20:06:12 +0000355#endif
Guido van Rossumddefaf32007-01-14 03:31:43 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 /* Larger numbers: loop to determine number of digits */
358 t = abs_ival;
359 while (t) {
360 ++ndigits;
361 t >>= PyLong_SHIFT;
362 }
363 v = _PyLong_New(ndigits);
364 if (v != NULL) {
365 digit *p = v->ob_digit;
366 Py_SIZE(v) = ndigits*sign;
367 t = abs_ival;
368 while (t) {
369 *p++ = Py_SAFE_DOWNCAST(
370 t & PyLong_MASK, unsigned long, digit);
371 t >>= PyLong_SHIFT;
372 }
373 }
374 return (PyObject *)v;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000375}
376
Serhiy Storchaka95949422013-08-27 19:40:23 +0300377/* Create a new int object from a C unsigned long int */
Guido van Rossum53756b11997-01-03 17:14:46 +0000378
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000379PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +0000380PyLong_FromUnsignedLong(unsigned long ival)
Guido van Rossum53756b11997-01-03 17:14:46 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyLongObject *v;
383 unsigned long t;
384 int ndigits = 0;
Tim Petersce9de2f2001-06-14 04:56:19 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (ival < PyLong_BASE)
387 return PyLong_FromLong(ival);
388 /* Count the number of Python digits. */
orenmn86aa2692017-03-06 10:42:47 +0200389 t = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 while (t) {
391 ++ndigits;
392 t >>= PyLong_SHIFT;
393 }
394 v = _PyLong_New(ndigits);
395 if (v != NULL) {
396 digit *p = v->ob_digit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 while (ival) {
398 *p++ = (digit)(ival & PyLong_MASK);
399 ival >>= PyLong_SHIFT;
400 }
401 }
402 return (PyObject *)v;
Guido van Rossum53756b11997-01-03 17:14:46 +0000403}
404
Serhiy Storchaka95949422013-08-27 19:40:23 +0300405/* Create a new int object from a C double */
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000406
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000407PyObject *
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408PyLong_FromDouble(double dval)
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyLongObject *v;
411 double frac;
412 int i, ndig, expo, neg;
413 neg = 0;
414 if (Py_IS_INFINITY(dval)) {
415 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson22b20182010-05-10 21:27:53 +0000416 "cannot convert float infinity to integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return NULL;
418 }
419 if (Py_IS_NAN(dval)) {
420 PyErr_SetString(PyExc_ValueError,
Mark Dickinson22b20182010-05-10 21:27:53 +0000421 "cannot convert float NaN to integer");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return NULL;
423 }
424 if (dval < 0.0) {
425 neg = 1;
426 dval = -dval;
427 }
428 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
429 if (expo <= 0)
430 return PyLong_FromLong(0L);
431 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
432 v = _PyLong_New(ndig);
433 if (v == NULL)
434 return NULL;
435 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
436 for (i = ndig; --i >= 0; ) {
437 digit bits = (digit)frac;
438 v->ob_digit[i] = bits;
439 frac = frac - (double)bits;
440 frac = ldexp(frac, PyLong_SHIFT);
441 }
442 if (neg)
443 Py_SIZE(v) = -(Py_SIZE(v));
444 return (PyObject *)v;
Guido van Rossum149e9ea1991-06-03 10:58:24 +0000445}
446
Thomas Wouters89f507f2006-12-13 04:49:30 +0000447/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
448 * anything about what happens when a signed integer operation overflows,
449 * and some compilers think they're doing you a favor by being "clever"
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700450 * then. The bit pattern for the largest positive signed long is
Thomas Wouters89f507f2006-12-13 04:49:30 +0000451 * (unsigned long)LONG_MAX, and for the smallest negative signed long
452 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
453 * However, some other compilers warn about applying unary minus to an
454 * unsigned operand. Hence the weird "0-".
455 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456#define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
457#define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000458
Serhiy Storchaka95949422013-08-27 19:40:23 +0300459/* Get a C long int from an int object or any object that has an __int__
Mark Dickinson8d48b432011-10-23 20:47:14 +0100460 method.
461
462 On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of
463 the result. Otherwise *overflow is 0.
464
465 For other errors (e.g., TypeError), return -1 and set an error condition.
466 In this case *overflow will be 0.
467*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000468
469long
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000470PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* This version by Tim Peters */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200473 PyLongObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 unsigned long x, prev;
475 long res;
476 Py_ssize_t i;
477 int sign;
478 int do_decref = 0; /* if nb_int was called */
Guido van Rossumf7531811998-05-26 14:33:37 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 *overflow = 0;
481 if (vv == NULL) {
482 PyErr_BadInternalCall();
483 return -1;
484 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000485
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200486 if (PyLong_Check(vv)) {
487 v = (PyLongObject *)vv;
488 }
489 else {
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200490 v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200491 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return -1;
493 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 res = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 i = Py_SIZE(v);
Guido van Rossumf7531811998-05-26 14:33:37 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 switch (i) {
500 case -1:
501 res = -(sdigit)v->ob_digit[0];
502 break;
503 case 0:
504 res = 0;
505 break;
506 case 1:
507 res = v->ob_digit[0];
508 break;
509 default:
510 sign = 1;
511 x = 0;
512 if (i < 0) {
513 sign = -1;
514 i = -(i);
515 }
516 while (--i >= 0) {
517 prev = x;
518 x = (x << PyLong_SHIFT) | v->ob_digit[i];
519 if ((x >> PyLong_SHIFT) != prev) {
520 *overflow = sign;
521 goto exit;
522 }
523 }
524 /* Haven't lost any bits, but casting to long requires extra
525 * care (see comment above).
526 */
527 if (x <= (unsigned long)LONG_MAX) {
528 res = (long)x * sign;
529 }
530 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
531 res = LONG_MIN;
532 }
533 else {
534 *overflow = sign;
535 /* res is already set to -1 */
536 }
537 }
Mark Dickinson22b20182010-05-10 21:27:53 +0000538 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (do_decref) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200540 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 }
542 return res;
Guido van Rossumedcc38a1991-05-05 20:09:44 +0000543}
544
Serhiy Storchaka95949422013-08-27 19:40:23 +0300545/* Get a C long int from an int object or any object that has an __int__
Mark Dickinson8d48b432011-10-23 20:47:14 +0100546 method. Return -1 and set an error if overflow occurs. */
547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548long
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000549PyLong_AsLong(PyObject *obj)
550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 int overflow;
552 long result = PyLong_AsLongAndOverflow(obj, &overflow);
553 if (overflow) {
554 /* XXX: could be cute and give a different
555 message for overflow == -1 */
556 PyErr_SetString(PyExc_OverflowError,
557 "Python int too large to convert to C long");
558 }
559 return result;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +0000560}
561
Serhiy Storchaka95949422013-08-27 19:40:23 +0300562/* Get a C int from an int object or any object that has an __int__
Serhiy Storchaka78980432013-01-15 01:12:17 +0200563 method. Return -1 and set an error if overflow occurs. */
564
565int
566_PyLong_AsInt(PyObject *obj)
567{
568 int overflow;
569 long result = PyLong_AsLongAndOverflow(obj, &overflow);
570 if (overflow || result > INT_MAX || result < INT_MIN) {
571 /* XXX: could be cute and give a different
572 message for overflow == -1 */
573 PyErr_SetString(PyExc_OverflowError,
574 "Python int too large to convert to C int");
575 return -1;
576 }
577 return (int)result;
578}
579
Serhiy Storchaka95949422013-08-27 19:40:23 +0300580/* Get a Py_ssize_t from an int object.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000581 Returns -1 and sets an error condition if overflow occurs. */
582
583Py_ssize_t
Guido van Rossumddefaf32007-01-14 03:31:43 +0000584PyLong_AsSsize_t(PyObject *vv) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200585 PyLongObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 size_t x, prev;
587 Py_ssize_t i;
588 int sign;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (vv == NULL) {
591 PyErr_BadInternalCall();
592 return -1;
593 }
594 if (!PyLong_Check(vv)) {
595 PyErr_SetString(PyExc_TypeError, "an integer is required");
596 return -1;
597 }
Mark Dickinsond59b4162010-03-13 11:34:40 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 v = (PyLongObject *)vv;
600 i = Py_SIZE(v);
601 switch (i) {
602 case -1: return -(sdigit)v->ob_digit[0];
603 case 0: return 0;
604 case 1: return v->ob_digit[0];
605 }
606 sign = 1;
607 x = 0;
608 if (i < 0) {
609 sign = -1;
610 i = -(i);
611 }
612 while (--i >= 0) {
613 prev = x;
614 x = (x << PyLong_SHIFT) | v->ob_digit[i];
615 if ((x >> PyLong_SHIFT) != prev)
616 goto overflow;
617 }
618 /* Haven't lost any bits, but casting to a signed type requires
619 * extra care (see comment above).
620 */
621 if (x <= (size_t)PY_SSIZE_T_MAX) {
622 return (Py_ssize_t)x * sign;
623 }
624 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
625 return PY_SSIZE_T_MIN;
626 }
627 /* else overflow */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628
Mark Dickinson22b20182010-05-10 21:27:53 +0000629 overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 PyErr_SetString(PyExc_OverflowError,
631 "Python int too large to convert to C ssize_t");
632 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633}
634
Serhiy Storchaka95949422013-08-27 19:40:23 +0300635/* Get a C unsigned long int from an int object.
Guido van Rossum53756b11997-01-03 17:14:46 +0000636 Returns -1 and sets an error condition if overflow occurs. */
637
638unsigned long
Tim Peters9f688bf2000-07-07 15:53:28 +0000639PyLong_AsUnsignedLong(PyObject *vv)
Guido van Rossum53756b11997-01-03 17:14:46 +0000640{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200641 PyLongObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 unsigned long x, prev;
643 Py_ssize_t i;
Tim Peters5af4e6c2002-08-12 02:31:19 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (vv == NULL) {
646 PyErr_BadInternalCall();
647 return (unsigned long)-1;
648 }
649 if (!PyLong_Check(vv)) {
650 PyErr_SetString(PyExc_TypeError, "an integer is required");
651 return (unsigned long)-1;
652 }
Mark Dickinsond59b4162010-03-13 11:34:40 +0000653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 v = (PyLongObject *)vv;
655 i = Py_SIZE(v);
656 x = 0;
657 if (i < 0) {
658 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson22b20182010-05-10 21:27:53 +0000659 "can't convert negative value to unsigned int");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return (unsigned long) -1;
661 }
662 switch (i) {
663 case 0: return 0;
664 case 1: return v->ob_digit[0];
665 }
666 while (--i >= 0) {
667 prev = x;
668 x = (x << PyLong_SHIFT) | v->ob_digit[i];
669 if ((x >> PyLong_SHIFT) != prev) {
670 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson02515f72013-08-03 12:08:22 +0100671 "Python int too large to convert "
Mark Dickinson22b20182010-05-10 21:27:53 +0000672 "to C unsigned long");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return (unsigned long) -1;
674 }
675 }
676 return x;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000677}
678
Serhiy Storchaka95949422013-08-27 19:40:23 +0300679/* Get a C size_t from an int object. Returns (size_t)-1 and sets
Stefan Krahb77c6c62011-09-12 16:22:47 +0200680 an error condition if overflow occurs. */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000681
682size_t
683PyLong_AsSize_t(PyObject *vv)
684{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200685 PyLongObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 size_t x, prev;
687 Py_ssize_t i;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 if (vv == NULL) {
690 PyErr_BadInternalCall();
691 return (size_t) -1;
692 }
693 if (!PyLong_Check(vv)) {
694 PyErr_SetString(PyExc_TypeError, "an integer is required");
695 return (size_t)-1;
696 }
Mark Dickinsond59b4162010-03-13 11:34:40 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 v = (PyLongObject *)vv;
699 i = Py_SIZE(v);
700 x = 0;
701 if (i < 0) {
702 PyErr_SetString(PyExc_OverflowError,
703 "can't convert negative value to size_t");
704 return (size_t) -1;
705 }
706 switch (i) {
707 case 0: return 0;
708 case 1: return v->ob_digit[0];
709 }
710 while (--i >= 0) {
711 prev = x;
712 x = (x << PyLong_SHIFT) | v->ob_digit[i];
713 if ((x >> PyLong_SHIFT) != prev) {
714 PyErr_SetString(PyExc_OverflowError,
715 "Python int too large to convert to C size_t");
Stefan Krahb77c6c62011-09-12 16:22:47 +0200716 return (size_t) -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 }
718 }
719 return x;
Guido van Rossum53756b11997-01-03 17:14:46 +0000720}
721
Serhiy Storchaka95949422013-08-27 19:40:23 +0300722/* Get a C unsigned long int from an int object, ignoring the high bits.
Thomas Hellera4ea6032003-04-17 18:55:45 +0000723 Returns -1 and sets an error condition if an error occurs. */
724
Guido van Rossumddefaf32007-01-14 03:31:43 +0000725static unsigned long
726_PyLong_AsUnsignedLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000727{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200728 PyLongObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 unsigned long x;
730 Py_ssize_t i;
731 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 if (vv == NULL || !PyLong_Check(vv)) {
734 PyErr_BadInternalCall();
735 return (unsigned long) -1;
736 }
737 v = (PyLongObject *)vv;
738 i = Py_SIZE(v);
739 switch (i) {
740 case 0: return 0;
741 case 1: return v->ob_digit[0];
742 }
743 sign = 1;
744 x = 0;
745 if (i < 0) {
746 sign = -1;
747 i = -i;
748 }
749 while (--i >= 0) {
750 x = (x << PyLong_SHIFT) | v->ob_digit[i];
751 }
752 return x * sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000753}
754
Guido van Rossumddefaf32007-01-14 03:31:43 +0000755unsigned long
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200756PyLong_AsUnsignedLongMask(PyObject *op)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 PyLongObject *lo;
759 unsigned long val;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000760
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200761 if (op == NULL) {
762 PyErr_BadInternalCall();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 return (unsigned long)-1;
764 }
Guido van Rossumddefaf32007-01-14 03:31:43 +0000765
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200766 if (PyLong_Check(op)) {
767 return _PyLong_AsUnsignedLongMask(op);
768 }
769
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200770 lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (lo == NULL)
772 return (unsigned long)-1;
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200773
774 val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
775 Py_DECREF(lo);
776 return val;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000777}
778
Tim Peters5b8132f2003-01-31 15:52:05 +0000779int
780_PyLong_Sign(PyObject *vv)
781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyLongObject *v = (PyLongObject *)vv;
Tim Peters5b8132f2003-01-31 15:52:05 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 assert(v != NULL);
785 assert(PyLong_Check(v));
Tim Peters5b8132f2003-01-31 15:52:05 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
Tim Peters5b8132f2003-01-31 15:52:05 +0000788}
789
Serhiy Storchakab2e64f92016-11-08 20:34:22 +0200790/* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
791 2**k if d is nonzero, else 0. */
792
793static const unsigned char BitLengthTable[32] = {
794 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
795 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
796};
797
798static int
799bits_in_digit(digit d)
800{
801 int d_bits = 0;
802 while (d >= 32) {
803 d_bits += 6;
804 d >>= 6;
805 }
806 d_bits += (int)BitLengthTable[d];
807 return d_bits;
808}
809
Tim Petersbaefd9e2003-01-28 20:37:45 +0000810size_t
811_PyLong_NumBits(PyObject *vv)
812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyLongObject *v = (PyLongObject *)vv;
814 size_t result = 0;
815 Py_ssize_t ndigits;
Serhiy Storchakab2e64f92016-11-08 20:34:22 +0200816 int msd_bits;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 assert(v != NULL);
819 assert(PyLong_Check(v));
Victor Stinner45e8e2f2014-05-14 17:24:35 +0200820 ndigits = Py_ABS(Py_SIZE(v));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
822 if (ndigits > 0) {
823 digit msd = v->ob_digit[ndigits - 1];
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700824 if ((size_t)(ndigits - 1) > SIZE_MAX / (size_t)PyLong_SHIFT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 goto Overflow;
Mark Dickinsonfc9adb62012-10-06 18:50:02 +0100826 result = (size_t)(ndigits - 1) * (size_t)PyLong_SHIFT;
Serhiy Storchakab2e64f92016-11-08 20:34:22 +0200827 msd_bits = bits_in_digit(msd);
828 if (SIZE_MAX - msd_bits < result)
829 goto Overflow;
830 result += msd_bits;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 }
832 return result;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000833
Mark Dickinson22b20182010-05-10 21:27:53 +0000834 Overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyErr_SetString(PyExc_OverflowError, "int has too many bits "
836 "to express in a platform size_t");
837 return (size_t)-1;
Tim Petersbaefd9e2003-01-28 20:37:45 +0000838}
839
Tim Peters2a9b3672001-06-11 21:23:58 +0000840PyObject *
841_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 int little_endian, int is_signed)
Tim Peters2a9b3672001-06-11 21:23:58 +0000843{
Mark Dickinson22b20182010-05-10 21:27:53 +0000844 const unsigned char* pstartbyte; /* LSB of bytes */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 int incr; /* direction to move pstartbyte */
846 const unsigned char* pendbyte; /* MSB of bytes */
847 size_t numsignificantbytes; /* number of bytes that matter */
Serhiy Storchaka95949422013-08-27 19:40:23 +0300848 Py_ssize_t ndigits; /* number of Python int digits */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyLongObject* v; /* result */
850 Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
Tim Peters2a9b3672001-06-11 21:23:58 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (n == 0)
853 return PyLong_FromLong(0L);
Tim Peters2a9b3672001-06-11 21:23:58 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 if (little_endian) {
856 pstartbyte = bytes;
857 pendbyte = bytes + n - 1;
858 incr = 1;
859 }
860 else {
861 pstartbyte = bytes + n - 1;
862 pendbyte = bytes;
863 incr = -1;
864 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (is_signed)
867 is_signed = *pendbyte >= 0x80;
Tim Peters2a9b3672001-06-11 21:23:58 +0000868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 /* Compute numsignificantbytes. This consists of finding the most
Ezio Melotti13925002011-03-16 11:05:33 +0200870 significant byte. Leading 0 bytes are insignificant if the number
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 is positive, and leading 0xff bytes if negative. */
872 {
873 size_t i;
874 const unsigned char* p = pendbyte;
875 const int pincr = -incr; /* search MSB to LSB */
Martin Pantereb995702016-07-28 01:11:04 +0000876 const unsigned char insignificant = is_signed ? 0xff : 0x00;
Tim Peters2a9b3672001-06-11 21:23:58 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 for (i = 0; i < n; ++i, p += pincr) {
Martin Pantereb995702016-07-28 01:11:04 +0000879 if (*p != insignificant)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 break;
881 }
882 numsignificantbytes = n - i;
883 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
884 actually has 2 significant bytes. OTOH, 0xff0001 ==
885 -0x00ffff, so we wouldn't *need* to bump it there; but we
886 do for 0xffff = -0x0001. To be safe without bothering to
887 check every case, bump it regardless. */
888 if (is_signed && numsignificantbytes < n)
889 ++numsignificantbytes;
890 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000891
Serhiy Storchaka95949422013-08-27 19:40:23 +0300892 /* How many Python int digits do we need? We have
893 8*numsignificantbytes bits, and each Python int digit has
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyLong_SHIFT bits, so it's the ceiling of the quotient. */
895 /* catch overflow before it happens */
896 if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
897 PyErr_SetString(PyExc_OverflowError,
898 "byte array too long to convert to int");
899 return NULL;
900 }
901 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
902 v = _PyLong_New(ndigits);
903 if (v == NULL)
904 return NULL;
Tim Peters2a9b3672001-06-11 21:23:58 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 /* Copy the bits over. The tricky parts are computing 2's-comp on
907 the fly for signed numbers, and dealing with the mismatch between
908 8-bit bytes and (probably) 15-bit Python digits.*/
909 {
910 size_t i;
911 twodigits carry = 1; /* for 2's-comp calculation */
912 twodigits accum = 0; /* sliding register */
913 unsigned int accumbits = 0; /* number of bits in accum */
914 const unsigned char* p = pstartbyte;
Tim Peters2a9b3672001-06-11 21:23:58 +0000915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
917 twodigits thisbyte = *p;
918 /* Compute correction for 2's comp, if needed. */
919 if (is_signed) {
920 thisbyte = (0xff ^ thisbyte) + carry;
921 carry = thisbyte >> 8;
922 thisbyte &= 0xff;
923 }
924 /* Because we're going LSB to MSB, thisbyte is
925 more significant than what's already in accum,
926 so needs to be prepended to accum. */
orenmn86aa2692017-03-06 10:42:47 +0200927 accum |= thisbyte << accumbits;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 accumbits += 8;
929 if (accumbits >= PyLong_SHIFT) {
930 /* There's enough to fill a Python digit. */
931 assert(idigit < ndigits);
Mark Dickinson22b20182010-05-10 21:27:53 +0000932 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 ++idigit;
934 accum >>= PyLong_SHIFT;
935 accumbits -= PyLong_SHIFT;
936 assert(accumbits < PyLong_SHIFT);
937 }
938 }
939 assert(accumbits < PyLong_SHIFT);
940 if (accumbits) {
941 assert(idigit < ndigits);
942 v->ob_digit[idigit] = (digit)accum;
943 ++idigit;
944 }
945 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 Py_SIZE(v) = is_signed ? -idigit : idigit;
948 return (PyObject *)long_normalize(v);
Tim Peters2a9b3672001-06-11 21:23:58 +0000949}
950
951int
952_PyLong_AsByteArray(PyLongObject* v,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 unsigned char* bytes, size_t n,
954 int little_endian, int is_signed)
Tim Peters2a9b3672001-06-11 21:23:58 +0000955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Py_ssize_t i; /* index into v->ob_digit */
Mark Dickinson22b20182010-05-10 21:27:53 +0000957 Py_ssize_t ndigits; /* |v->ob_size| */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 twodigits accum; /* sliding register */
Mark Dickinson22b20182010-05-10 21:27:53 +0000959 unsigned int accumbits; /* # bits in accum */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
961 digit carry; /* for computing 2's-comp */
962 size_t j; /* # bytes filled */
963 unsigned char* p; /* pointer to next byte in bytes */
964 int pincr; /* direction to move p */
Tim Peters2a9b3672001-06-11 21:23:58 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 assert(v != NULL && PyLong_Check(v));
Tim Peters2a9b3672001-06-11 21:23:58 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (Py_SIZE(v) < 0) {
969 ndigits = -(Py_SIZE(v));
970 if (!is_signed) {
971 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson22b20182010-05-10 21:27:53 +0000972 "can't convert negative int to unsigned");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return -1;
974 }
975 do_twos_comp = 1;
976 }
977 else {
978 ndigits = Py_SIZE(v);
979 do_twos_comp = 0;
980 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (little_endian) {
983 p = bytes;
984 pincr = 1;
985 }
986 else {
987 p = bytes + n - 1;
988 pincr = -1;
989 }
Tim Peters2a9b3672001-06-11 21:23:58 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 /* Copy over all the Python digits.
992 It's crucial that every Python digit except for the MSD contribute
Serhiy Storchaka95949422013-08-27 19:40:23 +0300993 exactly PyLong_SHIFT bits to the total, so first assert that the int is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 normalized. */
995 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
996 j = 0;
997 accum = 0;
998 accumbits = 0;
999 carry = do_twos_comp ? 1 : 0;
1000 for (i = 0; i < ndigits; ++i) {
1001 digit thisdigit = v->ob_digit[i];
1002 if (do_twos_comp) {
1003 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
1004 carry = thisdigit >> PyLong_SHIFT;
1005 thisdigit &= PyLong_MASK;
1006 }
1007 /* Because we're going LSB to MSB, thisdigit is more
1008 significant than what's already in accum, so needs to be
1009 prepended to accum. */
1010 accum |= (twodigits)thisdigit << accumbits;
Tim Peters8bc84b42001-06-12 19:17:03 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 /* The most-significant digit may be (probably is) at least
1013 partly empty. */
1014 if (i == ndigits - 1) {
1015 /* Count # of sign bits -- they needn't be stored,
1016 * although for signed conversion we need later to
1017 * make sure at least one sign bit gets stored. */
Mark Dickinson22b20182010-05-10 21:27:53 +00001018 digit s = do_twos_comp ? thisdigit ^ PyLong_MASK : thisdigit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 while (s != 0) {
1020 s >>= 1;
1021 accumbits++;
1022 }
1023 }
1024 else
1025 accumbits += PyLong_SHIFT;
Tim Peters8bc84b42001-06-12 19:17:03 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 /* Store as many bytes as possible. */
1028 while (accumbits >= 8) {
1029 if (j >= n)
1030 goto Overflow;
1031 ++j;
1032 *p = (unsigned char)(accum & 0xff);
1033 p += pincr;
1034 accumbits -= 8;
1035 accum >>= 8;
1036 }
1037 }
Tim Peters2a9b3672001-06-11 21:23:58 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 /* Store the straggler (if any). */
1040 assert(accumbits < 8);
1041 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
1042 if (accumbits > 0) {
1043 if (j >= n)
1044 goto Overflow;
1045 ++j;
1046 if (do_twos_comp) {
1047 /* Fill leading bits of the byte with sign bits
Serhiy Storchaka95949422013-08-27 19:40:23 +03001048 (appropriately pretending that the int had an
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 infinite supply of sign bits). */
1050 accum |= (~(twodigits)0) << accumbits;
1051 }
1052 *p = (unsigned char)(accum & 0xff);
1053 p += pincr;
1054 }
1055 else if (j == n && n > 0 && is_signed) {
1056 /* The main loop filled the byte array exactly, so the code
1057 just above didn't get to ensure there's a sign bit, and the
1058 loop below wouldn't add one either. Make sure a sign bit
1059 exists. */
1060 unsigned char msb = *(p - pincr);
1061 int sign_bit_set = msb >= 0x80;
1062 assert(accumbits == 0);
1063 if (sign_bit_set == do_twos_comp)
1064 return 0;
1065 else
1066 goto Overflow;
1067 }
Tim Peters05607ad2001-06-13 21:01:27 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* Fill remaining bytes with copies of the sign bit. */
1070 {
1071 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
1072 for ( ; j < n; ++j, p += pincr)
1073 *p = signbyte;
1074 }
Tim Peters05607ad2001-06-13 21:01:27 +00001075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 return 0;
Tim Peters2a9b3672001-06-11 21:23:58 +00001077
Mark Dickinson22b20182010-05-10 21:27:53 +00001078 Overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 PyErr_SetString(PyExc_OverflowError, "int too big to convert");
1080 return -1;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001081
Tim Peters2a9b3672001-06-11 21:23:58 +00001082}
1083
Serhiy Storchaka95949422013-08-27 19:40:23 +03001084/* Create a new int object from a C pointer */
Guido van Rossum78694d91998-09-18 14:14:13 +00001085
1086PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00001087PyLong_FromVoidPtr(void *p)
Guido van Rossum78694d91998-09-18 14:14:13 +00001088{
Mark Dickinson91044792012-10-18 19:21:43 +01001089#if SIZEOF_VOID_P <= SIZEOF_LONG
Benjamin Petersonca470632016-09-06 13:47:26 -07001090 return PyLong_FromUnsignedLong((unsigned long)(uintptr_t)p);
Mark Dickinson91044792012-10-18 19:21:43 +01001091#else
1092
Tim Peters70128a12001-06-16 08:48:40 +00001093#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001094# error "PyLong_FromVoidPtr: sizeof(long long) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +00001095#endif
Benjamin Petersonca470632016-09-06 13:47:26 -07001096 return PyLong_FromUnsignedLongLong((unsigned long long)(uintptr_t)p);
Mark Dickinson91044792012-10-18 19:21:43 +01001097#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Tim Peters70128a12001-06-16 08:48:40 +00001098
Guido van Rossum78694d91998-09-18 14:14:13 +00001099}
1100
Serhiy Storchaka95949422013-08-27 19:40:23 +03001101/* Get a C pointer from an int object. */
Guido van Rossum78694d91998-09-18 14:14:13 +00001102
1103void *
Tim Peters9f688bf2000-07-07 15:53:28 +00001104PyLong_AsVoidPtr(PyObject *vv)
Guido van Rossum78694d91998-09-18 14:14:13 +00001105{
Tim Peters70128a12001-06-16 08:48:40 +00001106#if SIZEOF_VOID_P <= SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 long x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
1110 x = PyLong_AsLong(vv);
1111 else
1112 x = PyLong_AsUnsignedLong(vv);
Guido van Rossum78694d91998-09-18 14:14:13 +00001113#else
Tim Peters70128a12001-06-16 08:48:40 +00001114
Tim Peters70128a12001-06-16 08:48:40 +00001115#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001116# error "PyLong_AsVoidPtr: sizeof(long long) < sizeof(void*)"
Tim Peters70128a12001-06-16 08:48:40 +00001117#endif
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001118 long long x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
1121 x = PyLong_AsLongLong(vv);
1122 else
1123 x = PyLong_AsUnsignedLongLong(vv);
Tim Peters70128a12001-06-16 08:48:40 +00001124
1125#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
Guido van Rossum78694d91998-09-18 14:14:13 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (x == -1 && PyErr_Occurred())
1128 return NULL;
1129 return (void *)x;
Guido van Rossum78694d91998-09-18 14:14:13 +00001130}
1131
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001132/* Initial long long support by Chris Herborth (chrish@qnx.com), later
Tim Petersd1a7da62001-06-13 00:35:57 +00001133 * rewritten to use the newer PyLong_{As,From}ByteArray API.
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001134 */
1135
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001136#define PY_ABS_LLONG_MIN (0-(unsigned long long)PY_LLONG_MIN)
Tim Petersd1a7da62001-06-13 00:35:57 +00001137
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001138/* Create a new int object from a C long long int. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001139
1140PyObject *
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001141PyLong_FromLongLong(long long ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001144 unsigned long long abs_ival;
1145 unsigned long long t; /* unsigned so >> doesn't propagate sign bit */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 int ndigits = 0;
1147 int negative = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 CHECK_SMALL_INT(ival);
1150 if (ival < 0) {
1151 /* avoid signed overflow on negation; see comments
1152 in PyLong_FromLong above. */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001153 abs_ival = (unsigned long long)(-1-ival) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 negative = 1;
1155 }
1156 else {
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001157 abs_ival = (unsigned long long)ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 /* Count the number of Python digits.
1161 We used to pick 5 ("big enough for anything"), but that's a
1162 waste of time and space given that 5*15 = 75 bits are rarely
1163 needed. */
1164 t = abs_ival;
1165 while (t) {
1166 ++ndigits;
1167 t >>= PyLong_SHIFT;
1168 }
1169 v = _PyLong_New(ndigits);
1170 if (v != NULL) {
1171 digit *p = v->ob_digit;
1172 Py_SIZE(v) = negative ? -ndigits : ndigits;
1173 t = abs_ival;
1174 while (t) {
1175 *p++ = (digit)(t & PyLong_MASK);
1176 t >>= PyLong_SHIFT;
1177 }
1178 }
1179 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001180}
1181
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001182/* Create a new int object from a C unsigned long long int. */
Tim Petersd1a7da62001-06-13 00:35:57 +00001183
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001184PyObject *
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001185PyLong_FromUnsignedLongLong(unsigned long long ival)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001188 unsigned long long t;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 int ndigits = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 if (ival < PyLong_BASE)
1192 return PyLong_FromLong((long)ival);
1193 /* Count the number of Python digits. */
orenmn86aa2692017-03-06 10:42:47 +02001194 t = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 while (t) {
1196 ++ndigits;
1197 t >>= PyLong_SHIFT;
1198 }
1199 v = _PyLong_New(ndigits);
1200 if (v != NULL) {
1201 digit *p = v->ob_digit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 while (ival) {
1203 *p++ = (digit)(ival & PyLong_MASK);
1204 ival >>= PyLong_SHIFT;
1205 }
1206 }
1207 return (PyObject *)v;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001208}
1209
Serhiy Storchaka95949422013-08-27 19:40:23 +03001210/* Create a new int object from a C Py_ssize_t. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001211
1212PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001213PyLong_FromSsize_t(Py_ssize_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 PyLongObject *v;
1216 size_t abs_ival;
1217 size_t t; /* unsigned so >> doesn't propagate sign bit */
1218 int ndigits = 0;
1219 int negative = 0;
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 CHECK_SMALL_INT(ival);
1222 if (ival < 0) {
1223 /* avoid signed overflow when ival = SIZE_T_MIN */
1224 abs_ival = (size_t)(-1-ival)+1;
1225 negative = 1;
1226 }
1227 else {
1228 abs_ival = (size_t)ival;
1229 }
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 /* Count the number of Python digits. */
1232 t = abs_ival;
1233 while (t) {
1234 ++ndigits;
1235 t >>= PyLong_SHIFT;
1236 }
1237 v = _PyLong_New(ndigits);
1238 if (v != NULL) {
1239 digit *p = v->ob_digit;
1240 Py_SIZE(v) = negative ? -ndigits : ndigits;
1241 t = abs_ival;
1242 while (t) {
1243 *p++ = (digit)(t & PyLong_MASK);
1244 t >>= PyLong_SHIFT;
1245 }
1246 }
1247 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001248}
1249
Serhiy Storchaka95949422013-08-27 19:40:23 +03001250/* Create a new int object from a C size_t. */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001251
1252PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +00001253PyLong_FromSize_t(size_t ival)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 PyLongObject *v;
1256 size_t t;
1257 int ndigits = 0;
Mark Dickinson7ab6be22008-04-15 21:42:42 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (ival < PyLong_BASE)
1260 return PyLong_FromLong((long)ival);
1261 /* Count the number of Python digits. */
1262 t = ival;
1263 while (t) {
1264 ++ndigits;
1265 t >>= PyLong_SHIFT;
1266 }
1267 v = _PyLong_New(ndigits);
1268 if (v != NULL) {
1269 digit *p = v->ob_digit;
1270 Py_SIZE(v) = ndigits;
1271 while (ival) {
1272 *p++ = (digit)(ival & PyLong_MASK);
1273 ival >>= PyLong_SHIFT;
1274 }
1275 }
1276 return (PyObject *)v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001277}
1278
Serhiy Storchaka95949422013-08-27 19:40:23 +03001279/* Get a C long long int from an int object or any object that has an
Mark Dickinson8d48b432011-10-23 20:47:14 +01001280 __int__ method. Return -1 and set an error if overflow occurs. */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001281
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001282long long
Tim Peters9f688bf2000-07-07 15:53:28 +00001283PyLong_AsLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001286 long long bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 int res;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001288 int do_decref = 0; /* if nb_int was called */
Tim Petersd1a7da62001-06-13 00:35:57 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (vv == NULL) {
1291 PyErr_BadInternalCall();
1292 return -1;
1293 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001294
1295 if (PyLong_Check(vv)) {
1296 v = (PyLongObject *)vv;
1297 }
1298 else {
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001299 v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001300 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 return -1;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001302 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001304
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001305 res = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 switch(Py_SIZE(v)) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001307 case -1:
1308 bytes = -(sdigit)v->ob_digit[0];
1309 break;
1310 case 0:
1311 bytes = 0;
1312 break;
1313 case 1:
1314 bytes = v->ob_digit[0];
1315 break;
1316 default:
1317 res = _PyLong_AsByteArray((PyLongObject *)v, (unsigned char *)&bytes,
Serhiy Storchakac4f32122013-12-11 21:26:36 +02001318 SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001320 if (do_decref) {
1321 Py_DECREF(v);
1322 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001323
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001324 /* Plan 9 can't handle long long in ? : expressions */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 if (res < 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001326 return (long long)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 else
1328 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001329}
1330
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001331/* Get a C unsigned long long int from an int object.
Tim Petersd1a7da62001-06-13 00:35:57 +00001332 Return -1 and set an error if overflow occurs. */
1333
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001334unsigned long long
Tim Peters9f688bf2000-07-07 15:53:28 +00001335PyLong_AsUnsignedLongLong(PyObject *vv)
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001338 unsigned long long bytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 int res;
Tim Petersd1a7da62001-06-13 00:35:57 +00001340
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02001341 if (vv == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 PyErr_BadInternalCall();
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001343 return (unsigned long long)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 }
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02001345 if (!PyLong_Check(vv)) {
1346 PyErr_SetString(PyExc_TypeError, "an integer is required");
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001347 return (unsigned long long)-1;
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02001348 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 v = (PyLongObject*)vv;
1351 switch(Py_SIZE(v)) {
1352 case 0: return 0;
1353 case 1: return v->ob_digit[0];
1354 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00001355
Mark Dickinson22b20182010-05-10 21:27:53 +00001356 res = _PyLong_AsByteArray((PyLongObject *)vv, (unsigned char *)&bytes,
Christian Heimes743e0cd2012-10-17 23:52:17 +02001357 SIZEOF_LONG_LONG, PY_LITTLE_ENDIAN, 0);
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001358
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001359 /* Plan 9 can't handle long long in ? : expressions */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (res < 0)
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001361 return (unsigned long long)res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 else
1363 return bytes;
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001364}
Tim Petersd1a7da62001-06-13 00:35:57 +00001365
Serhiy Storchaka95949422013-08-27 19:40:23 +03001366/* Get a C unsigned long int from an int object, ignoring the high bits.
Thomas Hellera4ea6032003-04-17 18:55:45 +00001367 Returns -1 and sets an error condition if an error occurs. */
1368
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001369static unsigned long long
Guido van Rossumddefaf32007-01-14 03:31:43 +00001370_PyLong_AsUnsignedLongLongMask(PyObject *vv)
Thomas Hellera4ea6032003-04-17 18:55:45 +00001371{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001372 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001373 unsigned long long x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 Py_ssize_t i;
1375 int sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (vv == NULL || !PyLong_Check(vv)) {
1378 PyErr_BadInternalCall();
Zackery Spytzdd492d92019-06-07 08:22:58 -06001379 return (unsigned long long) -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 }
1381 v = (PyLongObject *)vv;
1382 switch(Py_SIZE(v)) {
1383 case 0: return 0;
1384 case 1: return v->ob_digit[0];
1385 }
1386 i = Py_SIZE(v);
1387 sign = 1;
1388 x = 0;
1389 if (i < 0) {
1390 sign = -1;
1391 i = -i;
1392 }
1393 while (--i >= 0) {
1394 x = (x << PyLong_SHIFT) | v->ob_digit[i];
1395 }
1396 return x * sign;
Thomas Hellera4ea6032003-04-17 18:55:45 +00001397}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001398
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001399unsigned long long
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001400PyLong_AsUnsignedLongLongMask(PyObject *op)
Guido van Rossumddefaf32007-01-14 03:31:43 +00001401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 PyLongObject *lo;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001403 unsigned long long val;
Guido van Rossumddefaf32007-01-14 03:31:43 +00001404
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001405 if (op == NULL) {
1406 PyErr_BadInternalCall();
Zackery Spytzdd492d92019-06-07 08:22:58 -06001407 return (unsigned long long)-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00001409
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001410 if (PyLong_Check(op)) {
1411 return _PyLong_AsUnsignedLongLongMask(op);
1412 }
1413
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001414 lo = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(op);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (lo == NULL)
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001416 return (unsigned long long)-1;
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001417
1418 val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
1419 Py_DECREF(lo);
1420 return val;
Guido van Rossumddefaf32007-01-14 03:31:43 +00001421}
Tim Petersd1a7da62001-06-13 00:35:57 +00001422
Serhiy Storchaka95949422013-08-27 19:40:23 +03001423/* Get a C long long int from an int object or any object that has an
Mark Dickinson8d48b432011-10-23 20:47:14 +01001424 __int__ method.
Mark Dickinson93f562c2010-01-30 10:30:15 +00001425
Mark Dickinson8d48b432011-10-23 20:47:14 +01001426 On overflow, return -1 and set *overflow to 1 or -1 depending on the sign of
1427 the result. Otherwise *overflow is 0.
1428
1429 For other errors (e.g., TypeError), return -1 and set an error condition.
1430 In this case *overflow will be 0.
Mark Dickinson93f562c2010-01-30 10:30:15 +00001431*/
1432
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001433long long
Mark Dickinson93f562c2010-01-30 10:30:15 +00001434PyLong_AsLongLongAndOverflow(PyObject *vv, int *overflow)
1435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* This version by Tim Peters */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001437 PyLongObject *v;
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001438 unsigned long long x, prev;
1439 long long res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 Py_ssize_t i;
1441 int sign;
1442 int do_decref = 0; /* if nb_int was called */
Mark Dickinson93f562c2010-01-30 10:30:15 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 *overflow = 0;
1445 if (vv == NULL) {
1446 PyErr_BadInternalCall();
1447 return -1;
1448 }
Mark Dickinson93f562c2010-01-30 10:30:15 +00001449
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001450 if (PyLong_Check(vv)) {
1451 v = (PyLongObject *)vv;
1452 }
1453 else {
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +02001454 v = (PyLongObject *)_PyLong_FromNbIndexOrNbInt(vv);
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001455 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 return -1;
1457 do_decref = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 }
Mark Dickinson93f562c2010-01-30 10:30:15 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 res = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 i = Py_SIZE(v);
Mark Dickinson93f562c2010-01-30 10:30:15 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 switch (i) {
1464 case -1:
1465 res = -(sdigit)v->ob_digit[0];
1466 break;
1467 case 0:
1468 res = 0;
1469 break;
1470 case 1:
1471 res = v->ob_digit[0];
1472 break;
1473 default:
1474 sign = 1;
1475 x = 0;
1476 if (i < 0) {
1477 sign = -1;
1478 i = -(i);
1479 }
1480 while (--i >= 0) {
1481 prev = x;
1482 x = (x << PyLong_SHIFT) + v->ob_digit[i];
1483 if ((x >> PyLong_SHIFT) != prev) {
1484 *overflow = sign;
1485 goto exit;
1486 }
1487 }
1488 /* Haven't lost any bits, but casting to long requires extra
1489 * care (see comment above).
1490 */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001491 if (x <= (unsigned long long)PY_LLONG_MAX) {
1492 res = (long long)x * sign;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 }
1494 else if (sign < 0 && x == PY_ABS_LLONG_MIN) {
1495 res = PY_LLONG_MIN;
1496 }
1497 else {
1498 *overflow = sign;
1499 /* res is already set to -1 */
1500 }
1501 }
Mark Dickinson22b20182010-05-10 21:27:53 +00001502 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if (do_decref) {
Serhiy Storchaka31a65542013-12-11 21:07:54 +02001504 Py_DECREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 }
1506 return res;
Mark Dickinson93f562c2010-01-30 10:30:15 +00001507}
1508
Serhiy Storchaka7cb7bcf2018-07-26 13:22:16 +03001509int
1510_PyLong_UnsignedShort_Converter(PyObject *obj, void *ptr)
1511{
1512 unsigned long uval;
1513
1514 if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
1515 PyErr_SetString(PyExc_ValueError, "value must be positive");
1516 return 0;
1517 }
1518 uval = PyLong_AsUnsignedLong(obj);
1519 if (uval == (unsigned long)-1 && PyErr_Occurred())
1520 return 0;
1521 if (uval > USHRT_MAX) {
1522 PyErr_SetString(PyExc_OverflowError,
1523 "Python int too large for C unsigned short");
1524 return 0;
1525 }
1526
1527 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
1528 return 1;
1529}
1530
1531int
1532_PyLong_UnsignedInt_Converter(PyObject *obj, void *ptr)
1533{
1534 unsigned long uval;
1535
1536 if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
1537 PyErr_SetString(PyExc_ValueError, "value must be positive");
1538 return 0;
1539 }
1540 uval = PyLong_AsUnsignedLong(obj);
1541 if (uval == (unsigned long)-1 && PyErr_Occurred())
1542 return 0;
1543 if (uval > UINT_MAX) {
1544 PyErr_SetString(PyExc_OverflowError,
1545 "Python int too large for C unsigned int");
1546 return 0;
1547 }
1548
1549 *(unsigned int *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned int);
1550 return 1;
1551}
1552
1553int
1554_PyLong_UnsignedLong_Converter(PyObject *obj, void *ptr)
1555{
1556 unsigned long uval;
1557
1558 if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
1559 PyErr_SetString(PyExc_ValueError, "value must be positive");
1560 return 0;
1561 }
1562 uval = PyLong_AsUnsignedLong(obj);
1563 if (uval == (unsigned long)-1 && PyErr_Occurred())
1564 return 0;
1565
1566 *(unsigned long *)ptr = uval;
1567 return 1;
1568}
1569
1570int
1571_PyLong_UnsignedLongLong_Converter(PyObject *obj, void *ptr)
1572{
1573 unsigned long long uval;
1574
1575 if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
1576 PyErr_SetString(PyExc_ValueError, "value must be positive");
1577 return 0;
1578 }
1579 uval = PyLong_AsUnsignedLongLong(obj);
1580 if (uval == (unsigned long long)-1 && PyErr_Occurred())
1581 return 0;
1582
1583 *(unsigned long long *)ptr = uval;
1584 return 1;
1585}
1586
1587int
1588_PyLong_Size_t_Converter(PyObject *obj, void *ptr)
1589{
1590 size_t uval;
1591
1592 if (PyLong_Check(obj) && _PyLong_Sign(obj) < 0) {
1593 PyErr_SetString(PyExc_ValueError, "value must be positive");
1594 return 0;
1595 }
1596 uval = PyLong_AsSize_t(obj);
1597 if (uval == (size_t)-1 && PyErr_Occurred())
1598 return 0;
1599
1600 *(size_t *)ptr = uval;
1601 return 1;
1602}
1603
1604
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00001605#define CHECK_BINOP(v,w) \
1606 do { \
Brian Curtindfc80e32011-08-10 20:28:54 -05001607 if (!PyLong_Check(v) || !PyLong_Check(w)) \
1608 Py_RETURN_NOTIMPLEMENTED; \
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00001609 } while(0)
Neil Schemenauerba872e22001-01-04 01:46:03 +00001610
Tim Peters877a2122002-08-12 05:09:36 +00001611/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1612 * is modified in place, by adding y to it. Carries are propagated as far as
1613 * x[m-1], and the remaining carry (0 or 1) is returned.
1614 */
1615static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001616v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_ssize_t i;
1619 digit carry = 0;
Tim Peters877a2122002-08-12 05:09:36 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 assert(m >= n);
1622 for (i = 0; i < n; ++i) {
1623 carry += x[i] + y[i];
1624 x[i] = carry & PyLong_MASK;
1625 carry >>= PyLong_SHIFT;
1626 assert((carry & 1) == carry);
1627 }
1628 for (; carry && i < m; ++i) {
1629 carry += x[i];
1630 x[i] = carry & PyLong_MASK;
1631 carry >>= PyLong_SHIFT;
1632 assert((carry & 1) == carry);
1633 }
1634 return carry;
Tim Peters877a2122002-08-12 05:09:36 +00001635}
1636
1637/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
1638 * is modified in place, by subtracting y from it. Borrows are propagated as
1639 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1640 */
1641static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001642v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
Tim Peters877a2122002-08-12 05:09:36 +00001643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 Py_ssize_t i;
1645 digit borrow = 0;
Tim Peters877a2122002-08-12 05:09:36 +00001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 assert(m >= n);
1648 for (i = 0; i < n; ++i) {
1649 borrow = x[i] - y[i] - borrow;
1650 x[i] = borrow & PyLong_MASK;
1651 borrow >>= PyLong_SHIFT;
1652 borrow &= 1; /* keep only 1 sign bit */
1653 }
1654 for (; borrow && i < m; ++i) {
1655 borrow = x[i] - borrow;
1656 x[i] = borrow & PyLong_MASK;
1657 borrow >>= PyLong_SHIFT;
1658 borrow &= 1;
1659 }
1660 return borrow;
Tim Peters877a2122002-08-12 05:09:36 +00001661}
Neil Schemenauerba872e22001-01-04 01:46:03 +00001662
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00001663/* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put
1664 * result in z[0:m], and return the d bits shifted out of the top.
1665 */
1666static digit
1667v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_ssize_t i;
1670 digit carry = 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 assert(0 <= d && d < PyLong_SHIFT);
1673 for (i=0; i < m; i++) {
1674 twodigits acc = (twodigits)a[i] << d | carry;
1675 z[i] = (digit)acc & PyLong_MASK;
1676 carry = (digit)(acc >> PyLong_SHIFT);
1677 }
1678 return carry;
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00001679}
1680
1681/* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put
1682 * result in z[0:m], and return the d bits shifted out of the bottom.
1683 */
1684static digit
1685v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_ssize_t i;
1688 digit carry = 0;
1689 digit mask = ((digit)1 << d) - 1U;
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 assert(0 <= d && d < PyLong_SHIFT);
1692 for (i=m; i-- > 0;) {
1693 twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
1694 carry = (digit)acc & mask;
1695 z[i] = (digit)(acc >> d);
1696 }
1697 return carry;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001698}
1699
Tim Peters212e6142001-07-14 12:23:19 +00001700/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1701 in pout, and returning the remainder. pin and pout point at the LSD.
1702 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
Serhiy Storchaka95949422013-08-27 19:40:23 +03001703 _PyLong_Format, but that should be done with great care since ints are
Tim Peters212e6142001-07-14 12:23:19 +00001704 immutable. */
1705
1706static digit
Martin v. Löwis18e16552006-02-15 17:27:45 +00001707inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
Tim Peters212e6142001-07-14 12:23:19 +00001708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 twodigits rem = 0;
Tim Peters212e6142001-07-14 12:23:19 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 assert(n > 0 && n <= PyLong_MASK);
1712 pin += size;
1713 pout += size;
1714 while (--size >= 0) {
1715 digit hi;
1716 rem = (rem << PyLong_SHIFT) | *--pin;
1717 *--pout = hi = (digit)(rem / n);
1718 rem -= (twodigits)hi * n;
1719 }
1720 return (digit)rem;
Tim Peters212e6142001-07-14 12:23:19 +00001721}
1722
Serhiy Storchaka95949422013-08-27 19:40:23 +03001723/* Divide an integer by a digit, returning both the quotient
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001724 (as function result) and the remainder (through *prem).
1725 The sign of a is ignored; n should not be zero. */
1726
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001727static PyLongObject *
Tim Peters212e6142001-07-14 12:23:19 +00001728divrem1(PyLongObject *a, digit n, digit *prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001729{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02001730 const Py_ssize_t size = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 assert(n > 0 && n <= PyLong_MASK);
1734 z = _PyLong_New(size);
1735 if (z == NULL)
1736 return NULL;
1737 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
1738 return long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001739}
1740
Serhiy Storchaka95949422013-08-27 19:40:23 +03001741/* Convert an integer to a base 10 string. Returns a new non-shared
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001742 string. (Return value is non-shared so that callers can modify the
1743 returned value if necessary.) */
1744
Victor Stinnerd3f08822012-05-29 12:57:52 +02001745static int
1746long_to_decimal_string_internal(PyObject *aa,
1747 PyObject **p_output,
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001748 _PyUnicodeWriter *writer,
1749 _PyBytesWriter *bytes_writer,
1750 char **bytes_str)
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 PyLongObject *scratch, *a;
Victor Stinner1285e5c2015-10-14 12:10:20 +02001753 PyObject *str = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_ssize_t size, strlen, size_a, i, j;
1755 digit *pout, *pin, rem, tenpow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 int negative;
Mark Dickinson4e1de162016-08-29 17:26:43 +01001757 int d;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001758 enum PyUnicode_Kind kind;
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 a = (PyLongObject *)aa;
1761 if (a == NULL || !PyLong_Check(a)) {
1762 PyErr_BadInternalCall();
Victor Stinnerd3f08822012-05-29 12:57:52 +02001763 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 }
Victor Stinner45e8e2f2014-05-14 17:24:35 +02001765 size_a = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 negative = Py_SIZE(a) < 0;
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 /* quick and dirty upper bound for the number of digits
1769 required to express a in base _PyLong_DECIMAL_BASE:
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 #digits = 1 + floor(log2(a) / log2(_PyLong_DECIMAL_BASE))
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 But log2(a) < size_a * PyLong_SHIFT, and
1774 log2(_PyLong_DECIMAL_BASE) = log2(10) * _PyLong_DECIMAL_SHIFT
Mark Dickinson4e1de162016-08-29 17:26:43 +01001775 > 3.3 * _PyLong_DECIMAL_SHIFT
1776
1777 size_a * PyLong_SHIFT / (3.3 * _PyLong_DECIMAL_SHIFT) =
1778 size_a + size_a / d < size_a + size_a / floor(d),
1779 where d = (3.3 * _PyLong_DECIMAL_SHIFT) /
1780 (PyLong_SHIFT - 3.3 * _PyLong_DECIMAL_SHIFT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 */
Mark Dickinson4e1de162016-08-29 17:26:43 +01001782 d = (33 * _PyLong_DECIMAL_SHIFT) /
1783 (10 * PyLong_SHIFT - 33 * _PyLong_DECIMAL_SHIFT);
1784 assert(size_a < PY_SSIZE_T_MAX/2);
1785 size = 1 + size_a + size_a / d;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 scratch = _PyLong_New(size);
1787 if (scratch == NULL)
Victor Stinnerd3f08822012-05-29 12:57:52 +02001788 return -1;
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* convert array of base _PyLong_BASE digits in pin to an array of
1791 base _PyLong_DECIMAL_BASE digits in pout, following Knuth (TAOCP,
1792 Volume 2 (3rd edn), section 4.4, Method 1b). */
1793 pin = a->ob_digit;
1794 pout = scratch->ob_digit;
1795 size = 0;
1796 for (i = size_a; --i >= 0; ) {
1797 digit hi = pin[i];
1798 for (j = 0; j < size; j++) {
1799 twodigits z = (twodigits)pout[j] << PyLong_SHIFT | hi;
1800 hi = (digit)(z / _PyLong_DECIMAL_BASE);
1801 pout[j] = (digit)(z - (twodigits)hi *
1802 _PyLong_DECIMAL_BASE);
1803 }
1804 while (hi) {
1805 pout[size++] = hi % _PyLong_DECIMAL_BASE;
1806 hi /= _PyLong_DECIMAL_BASE;
1807 }
1808 /* check for keyboard interrupt */
1809 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00001810 Py_DECREF(scratch);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001811 return -1;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00001812 });
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 }
1814 /* pout should have at least one digit, so that the case when a = 0
1815 works correctly */
1816 if (size == 0)
1817 pout[size++] = 0;
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* calculate exact length of output string, and allocate */
1820 strlen = negative + 1 + (size - 1) * _PyLong_DECIMAL_SHIFT;
1821 tenpow = 10;
1822 rem = pout[size-1];
1823 while (rem >= tenpow) {
1824 tenpow *= 10;
1825 strlen++;
1826 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001827 if (writer) {
Christian Heimes110ac162012-09-10 02:51:27 +02001828 if (_PyUnicodeWriter_Prepare(writer, strlen, '9') == -1) {
1829 Py_DECREF(scratch);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001830 return -1;
Christian Heimes110ac162012-09-10 02:51:27 +02001831 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001832 kind = writer->kind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 }
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001834 else if (bytes_writer) {
1835 *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, strlen);
1836 if (*bytes_str == NULL) {
1837 Py_DECREF(scratch);
1838 return -1;
1839 }
1840 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001841 else {
1842 str = PyUnicode_New(strlen, '9');
1843 if (str == NULL) {
1844 Py_DECREF(scratch);
1845 return -1;
1846 }
1847 kind = PyUnicode_KIND(str);
1848 }
1849
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001850#define WRITE_DIGITS(p) \
Victor Stinnerd3f08822012-05-29 12:57:52 +02001851 do { \
Victor Stinnerd3f08822012-05-29 12:57:52 +02001852 /* pout[0] through pout[size-2] contribute exactly \
1853 _PyLong_DECIMAL_SHIFT digits each */ \
1854 for (i=0; i < size - 1; i++) { \
1855 rem = pout[i]; \
1856 for (j = 0; j < _PyLong_DECIMAL_SHIFT; j++) { \
1857 *--p = '0' + rem % 10; \
1858 rem /= 10; \
1859 } \
1860 } \
1861 /* pout[size-1]: always produce at least one decimal digit */ \
1862 rem = pout[i]; \
1863 do { \
1864 *--p = '0' + rem % 10; \
1865 rem /= 10; \
1866 } while (rem != 0); \
1867 \
1868 /* and sign */ \
1869 if (negative) \
1870 *--p = '-'; \
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001871 } while (0)
1872
1873#define WRITE_UNICODE_DIGITS(TYPE) \
1874 do { \
1875 if (writer) \
1876 p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + strlen; \
1877 else \
1878 p = (TYPE*)PyUnicode_DATA(str) + strlen; \
1879 \
1880 WRITE_DIGITS(p); \
Victor Stinnerd3f08822012-05-29 12:57:52 +02001881 \
1882 /* check we've counted correctly */ \
1883 if (writer) \
1884 assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
1885 else \
1886 assert(p == (TYPE*)PyUnicode_DATA(str)); \
1887 } while (0)
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 /* fill the string right-to-left */
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001890 if (bytes_writer) {
1891 char *p = *bytes_str + strlen;
1892 WRITE_DIGITS(p);
1893 assert(p == *bytes_str);
1894 }
1895 else if (kind == PyUnicode_1BYTE_KIND) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001896 Py_UCS1 *p;
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001897 WRITE_UNICODE_DIGITS(Py_UCS1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001899 else if (kind == PyUnicode_2BYTE_KIND) {
1900 Py_UCS2 *p;
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001901 WRITE_UNICODE_DIGITS(Py_UCS2);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001902 }
1903 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001904 Py_UCS4 *p;
Victor Stinnere577ab32012-05-29 18:51:10 +02001905 assert (kind == PyUnicode_4BYTE_KIND);
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001906 WRITE_UNICODE_DIGITS(Py_UCS4);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001907 }
1908#undef WRITE_DIGITS
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001909#undef WRITE_UNICODE_DIGITS
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 Py_DECREF(scratch);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001912 if (writer) {
1913 writer->pos += strlen;
1914 }
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001915 else if (bytes_writer) {
1916 (*bytes_str) += strlen;
1917 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02001918 else {
1919 assert(_PyUnicode_CheckConsistency(str, 1));
1920 *p_output = (PyObject *)str;
1921 }
1922 return 0;
1923}
1924
1925static PyObject *
1926long_to_decimal_string(PyObject *aa)
1927{
1928 PyObject *v;
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001929 if (long_to_decimal_string_internal(aa, &v, NULL, NULL, NULL) == -1)
Victor Stinnerd3f08822012-05-29 12:57:52 +02001930 return NULL;
1931 return v;
Mark Dickinson0a1efd02009-09-16 21:23:34 +00001932}
1933
Serhiy Storchaka95949422013-08-27 19:40:23 +03001934/* Convert an int object to a string, using a given conversion base,
Victor Stinnerd3f08822012-05-29 12:57:52 +02001935 which should be one of 2, 8 or 16. Return a string object.
1936 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'
1937 if alternate is nonzero. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001938
Victor Stinnerd3f08822012-05-29 12:57:52 +02001939static int
1940long_format_binary(PyObject *aa, int base, int alternate,
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02001941 PyObject **p_output, _PyUnicodeWriter *writer,
1942 _PyBytesWriter *bytes_writer, char **bytes_str)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00001943{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001944 PyLongObject *a = (PyLongObject *)aa;
Victor Stinner1285e5c2015-10-14 12:10:20 +02001945 PyObject *v = NULL;
Mark Dickinsone2846542012-04-20 21:21:24 +01001946 Py_ssize_t sz;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 Py_ssize_t size_a;
Victor Stinnerd3f08822012-05-29 12:57:52 +02001948 enum PyUnicode_Kind kind;
Mark Dickinsone2846542012-04-20 21:21:24 +01001949 int negative;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 int bits;
Guido van Rossume32e0141992-01-19 16:31:05 +00001951
Victor Stinnerd3f08822012-05-29 12:57:52 +02001952 assert(base == 2 || base == 8 || base == 16);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 if (a == NULL || !PyLong_Check(a)) {
1954 PyErr_BadInternalCall();
Victor Stinnerd3f08822012-05-29 12:57:52 +02001955 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 }
Victor Stinner45e8e2f2014-05-14 17:24:35 +02001957 size_a = Py_ABS(Py_SIZE(a));
Mark Dickinsone2846542012-04-20 21:21:24 +01001958 negative = Py_SIZE(a) < 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 /* Compute a rough upper bound for the length of the string */
1961 switch (base) {
1962 case 16:
1963 bits = 4;
1964 break;
1965 case 8:
1966 bits = 3;
1967 break;
1968 case 2:
1969 bits = 1;
1970 break;
1971 default:
Barry Warsawb2e57942017-09-14 18:13:16 -07001972 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00001974
Mark Dickinsone2846542012-04-20 21:21:24 +01001975 /* Compute exact length 'sz' of output string. */
1976 if (size_a == 0) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02001977 sz = 1;
Mark Dickinsone2846542012-04-20 21:21:24 +01001978 }
1979 else {
1980 Py_ssize_t size_a_in_bits;
1981 /* Ensure overflow doesn't occur during computation of sz. */
1982 if (size_a > (PY_SSIZE_T_MAX - 3) / PyLong_SHIFT) {
1983 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson02515f72013-08-03 12:08:22 +01001984 "int too large to format");
Victor Stinnerd3f08822012-05-29 12:57:52 +02001985 return -1;
Mark Dickinsone2846542012-04-20 21:21:24 +01001986 }
1987 size_a_in_bits = (size_a - 1) * PyLong_SHIFT +
1988 bits_in_digit(a->ob_digit[size_a - 1]);
Victor Stinnerd3f08822012-05-29 12:57:52 +02001989 /* Allow 1 character for a '-' sign. */
1990 sz = negative + (size_a_in_bits + (bits - 1)) / bits;
1991 }
1992 if (alternate) {
1993 /* 2 characters for prefix */
1994 sz += 2;
Mark Dickinsone2846542012-04-20 21:21:24 +01001995 }
1996
Victor Stinnerd3f08822012-05-29 12:57:52 +02001997 if (writer) {
1998 if (_PyUnicodeWriter_Prepare(writer, sz, 'x') == -1)
1999 return -1;
2000 kind = writer->kind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 }
Victor Stinner199c9a62015-10-14 09:47:23 +02002002 else if (bytes_writer) {
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002003 *bytes_str = _PyBytesWriter_Prepare(bytes_writer, *bytes_str, sz);
2004 if (*bytes_str == NULL)
2005 return -1;
2006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02002008 v = PyUnicode_New(sz, 'x');
2009 if (v == NULL)
2010 return -1;
2011 kind = PyUnicode_KIND(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 }
Mark Dickinson8accd6b2009-09-17 19:39:12 +00002013
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002014#define WRITE_DIGITS(p) \
Victor Stinnerd3f08822012-05-29 12:57:52 +02002015 do { \
Victor Stinnerd3f08822012-05-29 12:57:52 +02002016 if (size_a == 0) { \
2017 *--p = '0'; \
2018 } \
2019 else { \
2020 /* JRH: special case for power-of-2 bases */ \
2021 twodigits accum = 0; \
2022 int accumbits = 0; /* # of bits in accum */ \
2023 Py_ssize_t i; \
2024 for (i = 0; i < size_a; ++i) { \
2025 accum |= (twodigits)a->ob_digit[i] << accumbits; \
2026 accumbits += PyLong_SHIFT; \
2027 assert(accumbits >= bits); \
2028 do { \
2029 char cdigit; \
2030 cdigit = (char)(accum & (base - 1)); \
2031 cdigit += (cdigit < 10) ? '0' : 'a'-10; \
2032 *--p = cdigit; \
2033 accumbits -= bits; \
2034 accum >>= bits; \
2035 } while (i < size_a-1 ? accumbits >= bits : accum > 0); \
2036 } \
2037 } \
2038 \
2039 if (alternate) { \
2040 if (base == 16) \
2041 *--p = 'x'; \
2042 else if (base == 8) \
2043 *--p = 'o'; \
2044 else /* (base == 2) */ \
2045 *--p = 'b'; \
2046 *--p = '0'; \
2047 } \
2048 if (negative) \
2049 *--p = '-'; \
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002050 } while (0)
2051
2052#define WRITE_UNICODE_DIGITS(TYPE) \
2053 do { \
2054 if (writer) \
2055 p = (TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos + sz; \
2056 else \
2057 p = (TYPE*)PyUnicode_DATA(v) + sz; \
2058 \
2059 WRITE_DIGITS(p); \
2060 \
Victor Stinnerd3f08822012-05-29 12:57:52 +02002061 if (writer) \
2062 assert(p == ((TYPE*)PyUnicode_DATA(writer->buffer) + writer->pos)); \
2063 else \
2064 assert(p == (TYPE*)PyUnicode_DATA(v)); \
2065 } while (0)
2066
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002067 if (bytes_writer) {
2068 char *p = *bytes_str + sz;
2069 WRITE_DIGITS(p);
2070 assert(p == *bytes_str);
2071 }
2072 else if (kind == PyUnicode_1BYTE_KIND) {
Victor Stinnerd3f08822012-05-29 12:57:52 +02002073 Py_UCS1 *p;
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002074 WRITE_UNICODE_DIGITS(Py_UCS1);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002075 }
2076 else if (kind == PyUnicode_2BYTE_KIND) {
2077 Py_UCS2 *p;
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002078 WRITE_UNICODE_DIGITS(Py_UCS2);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002079 }
2080 else {
Victor Stinnerd3f08822012-05-29 12:57:52 +02002081 Py_UCS4 *p;
Victor Stinnere577ab32012-05-29 18:51:10 +02002082 assert (kind == PyUnicode_4BYTE_KIND);
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002083 WRITE_UNICODE_DIGITS(Py_UCS4);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002084 }
2085#undef WRITE_DIGITS
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002086#undef WRITE_UNICODE_DIGITS
Victor Stinnerd3f08822012-05-29 12:57:52 +02002087
2088 if (writer) {
2089 writer->pos += sz;
2090 }
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002091 else if (bytes_writer) {
2092 (*bytes_str) += sz;
2093 }
Victor Stinnerd3f08822012-05-29 12:57:52 +02002094 else {
2095 assert(_PyUnicode_CheckConsistency(v, 1));
2096 *p_output = v;
2097 }
2098 return 0;
2099}
2100
2101PyObject *
2102_PyLong_Format(PyObject *obj, int base)
2103{
2104 PyObject *str;
2105 int err;
2106 if (base == 10)
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002107 err = long_to_decimal_string_internal(obj, &str, NULL, NULL, NULL);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002108 else
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002109 err = long_format_binary(obj, base, 1, &str, NULL, NULL, NULL);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002110 if (err == -1)
2111 return NULL;
2112 return str;
2113}
2114
2115int
2116_PyLong_FormatWriter(_PyUnicodeWriter *writer,
2117 PyObject *obj,
2118 int base, int alternate)
2119{
2120 if (base == 10)
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002121 return long_to_decimal_string_internal(obj, NULL, writer,
2122 NULL, NULL);
Victor Stinnerd3f08822012-05-29 12:57:52 +02002123 else
Victor Stinnerbe75b8c2015-10-09 22:43:24 +02002124 return long_format_binary(obj, base, alternate, NULL, writer,
2125 NULL, NULL);
2126}
2127
2128char*
2129_PyLong_FormatBytesWriter(_PyBytesWriter *writer, char *str,
2130 PyObject *obj,
2131 int base, int alternate)
2132{
2133 char *str2;
2134 int res;
2135 str2 = str;
2136 if (base == 10)
2137 res = long_to_decimal_string_internal(obj, NULL, NULL,
2138 writer, &str2);
2139 else
2140 res = long_format_binary(obj, base, alternate, NULL, NULL,
2141 writer, &str2);
2142 if (res < 0)
2143 return NULL;
2144 assert(str2 != NULL);
2145 return str2;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002146}
2147
Thomas Wouters477c8d52006-05-27 19:21:47 +00002148/* Table of digit values for 8-bit string -> integer conversion.
2149 * '0' maps to 0, ..., '9' maps to 9.
2150 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
2151 * All other indices map to 37.
2152 * Note that when converting a base B string, a char c is a legitimate
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00002153 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002154 */
Raymond Hettinger35631532009-01-09 03:58:09 +00002155unsigned char _PyLong_DigitValue[256] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2157 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2158 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2159 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
2160 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2161 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
2162 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
2163 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
2164 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2165 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2166 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2167 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2168 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2169 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2170 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
2171 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002172};
2173
2174/* *str points to the first digit in a string of base `base` digits. base
Tim Petersbf2674b2003-02-02 07:51:32 +00002175 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
Serhiy Storchaka95949422013-08-27 19:40:23 +03002176 * non-digit (which may be *str!). A normalized int is returned.
Tim Petersbf2674b2003-02-02 07:51:32 +00002177 * The point to this routine is that it takes time linear in the number of
2178 * string characters.
Brett Cannona721aba2016-09-09 14:57:09 -07002179 *
2180 * Return values:
2181 * -1 on syntax error (exception needs to be set, *res is untouched)
2182 * 0 else (exception may be set, in that case *res is set to NULL)
Tim Petersbf2674b2003-02-02 07:51:32 +00002183 */
Brett Cannona721aba2016-09-09 14:57:09 -07002184static int
2185long_from_binary_base(const char **str, int base, PyLongObject **res)
Tim Petersbf2674b2003-02-02 07:51:32 +00002186{
Serhiy Storchakac6792272013-10-19 21:03:34 +03002187 const char *p = *str;
2188 const char *start = p;
Brett Cannona721aba2016-09-09 14:57:09 -07002189 char prev = 0;
Serhiy Storchaka29ba6882017-12-03 22:16:21 +02002190 Py_ssize_t digits = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 int bits_per_char;
2192 Py_ssize_t n;
2193 PyLongObject *z;
2194 twodigits accum;
2195 int bits_in_accum;
2196 digit *pdigit;
Tim Petersbf2674b2003-02-02 07:51:32 +00002197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
2199 n = base;
Brett Cannona721aba2016-09-09 14:57:09 -07002200 for (bits_per_char = -1; n; ++bits_per_char) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 n >>= 1;
Brett Cannona721aba2016-09-09 14:57:09 -07002202 }
2203 /* count digits and set p to end-of-string */
2204 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base || *p == '_') {
2205 if (*p == '_') {
2206 if (prev == '_') {
2207 *str = p - 1;
2208 return -1;
2209 }
2210 } else {
2211 ++digits;
2212 }
2213 prev = *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 ++p;
Brett Cannona721aba2016-09-09 14:57:09 -07002215 }
2216 if (prev == '_') {
2217 /* Trailing underscore not allowed. */
2218 *str = p - 1;
2219 return -1;
2220 }
2221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 *str = p;
Serhiy Storchaka85c0b892017-10-03 14:13:44 +03002223 /* n <- the number of Python digits needed,
2224 = ceiling((digits * bits_per_char) / PyLong_SHIFT). */
2225 if (digits > (PY_SSIZE_T_MAX - (PyLong_SHIFT - 1)) / bits_per_char) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 PyErr_SetString(PyExc_ValueError,
2227 "int string too large to convert");
Brett Cannona721aba2016-09-09 14:57:09 -07002228 *res = NULL;
2229 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
Serhiy Storchaka85c0b892017-10-03 14:13:44 +03002231 n = (digits * bits_per_char + PyLong_SHIFT - 1) / PyLong_SHIFT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 z = _PyLong_New(n);
Brett Cannona721aba2016-09-09 14:57:09 -07002233 if (z == NULL) {
2234 *res = NULL;
2235 return 0;
2236 }
Serhiy Storchaka95949422013-08-27 19:40:23 +03002237 /* Read string from right, and fill in int from left; i.e.,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 * from least to most significant in both.
2239 */
2240 accum = 0;
2241 bits_in_accum = 0;
2242 pdigit = z->ob_digit;
2243 while (--p >= start) {
Brett Cannona721aba2016-09-09 14:57:09 -07002244 int k;
2245 if (*p == '_') {
2246 continue;
2247 }
2248 k = (int)_PyLong_DigitValue[Py_CHARMASK(*p)];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 assert(k >= 0 && k < base);
2250 accum |= (twodigits)k << bits_in_accum;
2251 bits_in_accum += bits_per_char;
2252 if (bits_in_accum >= PyLong_SHIFT) {
2253 *pdigit++ = (digit)(accum & PyLong_MASK);
2254 assert(pdigit - z->ob_digit <= n);
2255 accum >>= PyLong_SHIFT;
2256 bits_in_accum -= PyLong_SHIFT;
2257 assert(bits_in_accum < PyLong_SHIFT);
2258 }
2259 }
2260 if (bits_in_accum) {
2261 assert(bits_in_accum <= PyLong_SHIFT);
2262 *pdigit++ = (digit)accum;
2263 assert(pdigit - z->ob_digit <= n);
2264 }
2265 while (pdigit - z->ob_digit < n)
2266 *pdigit++ = 0;
Brett Cannona721aba2016-09-09 14:57:09 -07002267 *res = long_normalize(z);
2268 return 0;
Tim Petersbf2674b2003-02-02 07:51:32 +00002269}
2270
Serhiy Storchaka95949422013-08-27 19:40:23 +03002271/* Parses an int from a bytestring. Leading and trailing whitespace will be
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002272 * ignored.
2273 *
2274 * If successful, a PyLong object will be returned and 'pend' will be pointing
2275 * to the first unused byte unless it's NULL.
2276 *
2277 * If unsuccessful, NULL will be returned.
2278 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002279PyObject *
Serhiy Storchakac6792272013-10-19 21:03:34 +03002280PyLong_FromString(const char *str, char **pend, int base)
Guido van Rossumeb1fafc1994-08-29 12:47:19 +00002281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 int sign = 1, error_if_nonzero = 0;
Serhiy Storchakac6792272013-10-19 21:03:34 +03002283 const char *start, *orig_str = str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 PyLongObject *z = NULL;
2285 PyObject *strobj;
2286 Py_ssize_t slen;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 if ((base != 0 && base < 2) || base > 36) {
2289 PyErr_SetString(PyExc_ValueError,
2290 "int() arg 2 must be >= 2 and <= 36");
2291 return NULL;
2292 }
Brett Cannona721aba2016-09-09 14:57:09 -07002293 while (*str != '\0' && Py_ISSPACE(Py_CHARMASK(*str))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 str++;
Brett Cannona721aba2016-09-09 14:57:09 -07002295 }
2296 if (*str == '+') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 ++str;
Brett Cannona721aba2016-09-09 14:57:09 -07002298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 else if (*str == '-') {
2300 ++str;
2301 sign = -1;
2302 }
2303 if (base == 0) {
Brett Cannona721aba2016-09-09 14:57:09 -07002304 if (str[0] != '0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 base = 10;
Brett Cannona721aba2016-09-09 14:57:09 -07002306 }
2307 else if (str[1] == 'x' || str[1] == 'X') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 base = 16;
Brett Cannona721aba2016-09-09 14:57:09 -07002309 }
2310 else if (str[1] == 'o' || str[1] == 'O') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 base = 8;
Brett Cannona721aba2016-09-09 14:57:09 -07002312 }
2313 else if (str[1] == 'b' || str[1] == 'B') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 base = 2;
Brett Cannona721aba2016-09-09 14:57:09 -07002315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 else {
2317 /* "old" (C-style) octal literal, now invalid.
2318 it might still be zero though */
2319 error_if_nonzero = 1;
2320 base = 10;
2321 }
2322 }
2323 if (str[0] == '0' &&
2324 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
2325 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
Brett Cannona721aba2016-09-09 14:57:09 -07002326 (base == 2 && (str[1] == 'b' || str[1] == 'B')))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 str += 2;
Brett Cannona721aba2016-09-09 14:57:09 -07002328 /* One underscore allowed here. */
2329 if (*str == '_') {
2330 ++str;
2331 }
2332 }
2333 if (str[0] == '_') {
Barry Warsawb2e57942017-09-14 18:13:16 -07002334 /* May not start with underscores. */
2335 goto onError;
Brett Cannona721aba2016-09-09 14:57:09 -07002336 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 start = str;
Brett Cannona721aba2016-09-09 14:57:09 -07002339 if ((base & (base - 1)) == 0) {
2340 int res = long_from_binary_base(&str, base, &z);
2341 if (res < 0) {
2342 /* Syntax error. */
2343 goto onError;
2344 }
2345 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002347/***
2348Binary bases can be converted in time linear in the number of digits, because
2349Python's representation base is binary. Other bases (including decimal!) use
2350the simple quadratic-time algorithm below, complicated by some speed tricks.
Tim Peters5af4e6c2002-08-12 02:31:19 +00002351
Thomas Wouters477c8d52006-05-27 19:21:47 +00002352First some math: the largest integer that can be expressed in N base-B digits
2353is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
2354case number of Python digits needed to hold it is the smallest integer n s.t.
2355
2356 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
2357 BASE**n >= B**N [taking logs to base BASE]
2358 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
2359
2360The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
Serhiy Storchaka95949422013-08-27 19:40:23 +03002361this quickly. A Python int with that much space is reserved near the start,
Thomas Wouters477c8d52006-05-27 19:21:47 +00002362and the result is computed into it.
2363
2364The input string is actually treated as being in base base**i (i.e., i digits
2365are processed at a time), where two more static arrays hold:
2366
2367 convwidth_base[base] = the largest integer i such that base**i <= BASE
2368 convmultmax_base[base] = base ** convwidth_base[base]
2369
2370The first of these is the largest i such that i consecutive input digits
2371must fit in a single Python digit. The second is effectively the input
2372base we're really using.
2373
2374Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
2375convmultmax_base[base], the result is "simply"
2376
2377 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
2378
2379where B = convmultmax_base[base].
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002380
2381Error analysis: as above, the number of Python digits `n` needed is worst-
2382case
2383
2384 n >= N * log(B)/log(BASE)
2385
2386where `N` is the number of input digits in base `B`. This is computed via
2387
2388 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
2389
2390below. Two numeric concerns are how much space this can waste, and whether
2391the computed result can be too small. To be concrete, assume BASE = 2**15,
2392which is the default (and it's unlikely anyone changes that).
2393
2394Waste isn't a problem: provided the first input digit isn't 0, the difference
2395between the worst-case input with N digits and the smallest input with N
2396digits is about a factor of B, but B is small compared to BASE so at most
2397one allocated Python digit can remain unused on that count. If
2398N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
2399and adding 1 returns a result 1 larger than necessary. However, that can't
2400happen: whenever B is a power of 2, long_from_binary_base() is called
2401instead, and it's impossible for B**i to be an integer power of 2**15 when
2402B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
2403an exact integer when B is not a power of 2, since B**i has a prime factor
2404other than 2 in that case, but (2**15)**j's only prime factor is 2).
2405
2406The computed result can be too small if the true value of N*log(B)/log(BASE)
2407is a little bit larger than an exact integer, but due to roundoff errors (in
2408computing log(B), log(BASE), their quotient, and/or multiplying that by N)
2409yields a numeric result a little less than that integer. Unfortunately, "how
2410close can a transcendental function get to an integer over some range?"
2411questions are generally theoretically intractable. Computer analysis via
2412continued fractions is practical: expand log(B)/log(BASE) via continued
2413fractions, giving a sequence i/j of "the best" rational approximations. Then
2414j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
2415we can get very close to being in trouble, but very rarely. For example,
241676573 is a denominator in one of the continued-fraction approximations to
2417log(10)/log(2**15), and indeed:
2418
2419 >>> log(10)/log(2**15)*76573
2420 16958.000000654003
2421
2422is very close to an integer. If we were working with IEEE single-precision,
2423rounding errors could kill us. Finding worst cases in IEEE double-precision
2424requires better-than-double-precision log() functions, and Tim didn't bother.
2425Instead the code checks to see whether the allocated space is enough as each
Serhiy Storchaka95949422013-08-27 19:40:23 +03002426new Python digit is added, and copies the whole thing to a larger int if not.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002427This should happen extremely rarely, and in fact I don't have a test case
2428that triggers it(!). Instead the code was tested by artificially allocating
2429just 1 digit at the start, so that the copying code was exercised for every
2430digit beyond the first.
Thomas Wouters477c8d52006-05-27 19:21:47 +00002431***/
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02002432 twodigits c; /* current input character */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 Py_ssize_t size_z;
Serhiy Storchaka29ba6882017-12-03 22:16:21 +02002434 Py_ssize_t digits = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 int i;
2436 int convwidth;
2437 twodigits convmultmax, convmult;
2438 digit *pz, *pzstop;
Brett Cannona721aba2016-09-09 14:57:09 -07002439 const char *scan, *lastdigit;
2440 char prev = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 static double log_base_BASE[37] = {0.0e0,};
2443 static int convwidth_base[37] = {0,};
2444 static twodigits convmultmax_base[37] = {0,};
Thomas Wouters477c8d52006-05-27 19:21:47 +00002445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 if (log_base_BASE[base] == 0.0) {
2447 twodigits convmax = base;
2448 int i = 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002449
Mark Dickinson22b20182010-05-10 21:27:53 +00002450 log_base_BASE[base] = (log((double)base) /
2451 log((double)PyLong_BASE));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 for (;;) {
2453 twodigits next = convmax * base;
Brett Cannona721aba2016-09-09 14:57:09 -07002454 if (next > PyLong_BASE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 break;
Brett Cannona721aba2016-09-09 14:57:09 -07002456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 convmax = next;
2458 ++i;
2459 }
2460 convmultmax_base[base] = convmax;
2461 assert(i > 0);
2462 convwidth_base[base] = i;
2463 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 /* Find length of the string of numeric characters. */
2466 scan = str;
Brett Cannona721aba2016-09-09 14:57:09 -07002467 lastdigit = str;
2468
2469 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base || *scan == '_') {
2470 if (*scan == '_') {
2471 if (prev == '_') {
2472 /* Only one underscore allowed. */
2473 str = lastdigit + 1;
2474 goto onError;
2475 }
2476 }
2477 else {
2478 ++digits;
2479 lastdigit = scan;
2480 }
2481 prev = *scan;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 ++scan;
Brett Cannona721aba2016-09-09 14:57:09 -07002483 }
2484 if (prev == '_') {
2485 /* Trailing underscore not allowed. */
2486 /* Set error pointer to first underscore. */
2487 str = lastdigit + 1;
2488 goto onError;
2489 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002490
Serhiy Storchaka95949422013-08-27 19:40:23 +03002491 /* Create an int object that can contain the largest possible
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 * integer with this base and length. Note that there's no
2493 * need to initialize z->ob_digit -- no slot is read up before
2494 * being stored into.
2495 */
Victor Stinnerdd431b32017-12-08 00:06:55 +01002496 double fsize_z = (double)digits * log_base_BASE[base] + 1.0;
2497 if (fsize_z > (double)MAX_LONG_DIGITS) {
Serhiy Storchaka29ba6882017-12-03 22:16:21 +02002498 /* The same exception as in _PyLong_New(). */
2499 PyErr_SetString(PyExc_OverflowError,
2500 "too many digits in integer");
2501 return NULL;
2502 }
2503 size_z = (Py_ssize_t)fsize_z;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 /* Uncomment next line to test exceedingly rare copy code */
2505 /* size_z = 1; */
2506 assert(size_z > 0);
2507 z = _PyLong_New(size_z);
Brett Cannona721aba2016-09-09 14:57:09 -07002508 if (z == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 return NULL;
Brett Cannona721aba2016-09-09 14:57:09 -07002510 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 Py_SIZE(z) = 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 /* `convwidth` consecutive input digits are treated as a single
2514 * digit in base `convmultmax`.
2515 */
2516 convwidth = convwidth_base[base];
2517 convmultmax = convmultmax_base[base];
Thomas Wouters477c8d52006-05-27 19:21:47 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 /* Work ;-) */
2520 while (str < scan) {
Brett Cannona721aba2016-09-09 14:57:09 -07002521 if (*str == '_') {
2522 str++;
2523 continue;
2524 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 /* grab up to convwidth digits from the input string */
2526 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
Brett Cannona721aba2016-09-09 14:57:09 -07002527 for (i = 1; i < convwidth && str != scan; ++str) {
2528 if (*str == '_') {
2529 continue;
2530 }
2531 i++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 c = (twodigits)(c * base +
Mark Dickinson22b20182010-05-10 21:27:53 +00002533 (int)_PyLong_DigitValue[Py_CHARMASK(*str)]);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 assert(c < PyLong_BASE);
2535 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 convmult = convmultmax;
2538 /* Calculate the shift only if we couldn't get
2539 * convwidth digits.
2540 */
2541 if (i != convwidth) {
2542 convmult = base;
Brett Cannona721aba2016-09-09 14:57:09 -07002543 for ( ; i > 1; --i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 convmult *= base;
Brett Cannona721aba2016-09-09 14:57:09 -07002545 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 /* Multiply z by convmult, and add c. */
2549 pz = z->ob_digit;
2550 pzstop = pz + Py_SIZE(z);
2551 for (; pz < pzstop; ++pz) {
2552 c += (twodigits)*pz * convmult;
2553 *pz = (digit)(c & PyLong_MASK);
2554 c >>= PyLong_SHIFT;
2555 }
2556 /* carry off the current end? */
2557 if (c) {
2558 assert(c < PyLong_BASE);
2559 if (Py_SIZE(z) < size_z) {
2560 *pz = (digit)c;
2561 ++Py_SIZE(z);
2562 }
2563 else {
2564 PyLongObject *tmp;
2565 /* Extremely rare. Get more space. */
2566 assert(Py_SIZE(z) == size_z);
2567 tmp = _PyLong_New(size_z + 1);
2568 if (tmp == NULL) {
2569 Py_DECREF(z);
2570 return NULL;
2571 }
2572 memcpy(tmp->ob_digit,
2573 z->ob_digit,
2574 sizeof(digit) * size_z);
2575 Py_DECREF(z);
2576 z = tmp;
2577 z->ob_digit[size_z] = (digit)c;
2578 ++size_z;
2579 }
2580 }
2581 }
2582 }
Brett Cannona721aba2016-09-09 14:57:09 -07002583 if (z == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 return NULL;
Brett Cannona721aba2016-09-09 14:57:09 -07002585 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 if (error_if_nonzero) {
2587 /* reset the base to 0, else the exception message
2588 doesn't make too much sense */
2589 base = 0;
Brett Cannona721aba2016-09-09 14:57:09 -07002590 if (Py_SIZE(z) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 goto onError;
Brett Cannona721aba2016-09-09 14:57:09 -07002592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 /* there might still be other problems, therefore base
2594 remains zero here for the same reason */
2595 }
Brett Cannona721aba2016-09-09 14:57:09 -07002596 if (str == start) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 goto onError;
Brett Cannona721aba2016-09-09 14:57:09 -07002598 }
2599 if (sign < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 Py_SIZE(z) = -(Py_SIZE(z));
Brett Cannona721aba2016-09-09 14:57:09 -07002601 }
2602 while (*str && Py_ISSPACE(Py_CHARMASK(*str))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 str++;
Brett Cannona721aba2016-09-09 14:57:09 -07002604 }
2605 if (*str != '\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 goto onError;
Brett Cannona721aba2016-09-09 14:57:09 -07002607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 long_normalize(z);
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002609 z = maybe_small_long(z);
Brett Cannona721aba2016-09-09 14:57:09 -07002610 if (z == NULL) {
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002611 return NULL;
Brett Cannona721aba2016-09-09 14:57:09 -07002612 }
2613 if (pend != NULL) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03002614 *pend = (char *)str;
Brett Cannona721aba2016-09-09 14:57:09 -07002615 }
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002616 return (PyObject *) z;
Guido van Rossum9e896b32000-04-05 20:11:21 +00002617
Mark Dickinson22b20182010-05-10 21:27:53 +00002618 onError:
Brett Cannona721aba2016-09-09 14:57:09 -07002619 if (pend != NULL) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03002620 *pend = (char *)str;
Brett Cannona721aba2016-09-09 14:57:09 -07002621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 Py_XDECREF(z);
2623 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
2624 strobj = PyUnicode_FromStringAndSize(orig_str, slen);
Brett Cannona721aba2016-09-09 14:57:09 -07002625 if (strobj == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 return NULL;
Brett Cannona721aba2016-09-09 14:57:09 -07002627 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002628 PyErr_Format(PyExc_ValueError,
Serhiy Storchaka579ddc22013-08-03 21:14:05 +03002629 "invalid literal for int() with base %d: %.200R",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 base, strobj);
2631 Py_DECREF(strobj);
2632 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +00002633}
2634
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002635/* Since PyLong_FromString doesn't have a length parameter,
2636 * check here for possible NULs in the string.
2637 *
2638 * Reports an invalid literal as a bytes object.
2639 */
2640PyObject *
2641_PyLong_FromBytes(const char *s, Py_ssize_t len, int base)
2642{
2643 PyObject *result, *strobj;
2644 char *end = NULL;
2645
Serhiy Storchaka20b39b22014-09-28 11:27:24 +03002646 result = PyLong_FromString(s, &end, base);
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002647 if (end == NULL || (result != NULL && end == s + len))
2648 return result;
2649 Py_XDECREF(result);
2650 strobj = PyBytes_FromStringAndSize(s, Py_MIN(len, 200));
2651 if (strobj != NULL) {
2652 PyErr_Format(PyExc_ValueError,
Serhiy Storchaka579ddc22013-08-03 21:14:05 +03002653 "invalid literal for int() with base %d: %.200R",
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002654 base, strobj);
2655 Py_DECREF(strobj);
2656 }
2657 return NULL;
2658}
2659
Guido van Rossum9e896b32000-04-05 20:11:21 +00002660PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002661PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +00002662{
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02002663 PyObject *v, *unicode = PyUnicode_FromWideChar(u, length);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002664 if (unicode == NULL)
2665 return NULL;
2666 v = PyLong_FromUnicodeObject(unicode, base);
2667 Py_DECREF(unicode);
2668 return v;
2669}
2670
2671PyObject *
2672PyLong_FromUnicodeObject(PyObject *u, int base)
2673{
Serhiy Storchaka579ddc22013-08-03 21:14:05 +03002674 PyObject *result, *asciidig;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02002675 const char *buffer;
2676 char *end = NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002677 Py_ssize_t buflen;
Guido van Rossum9e896b32000-04-05 20:11:21 +00002678
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002679 asciidig = _PyUnicode_TransformDecimalAndSpaceToASCII(u);
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00002680 if (asciidig == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 return NULL;
Serhiy Storchaka9b6c60c2017-11-13 21:23:48 +02002682 assert(PyUnicode_IS_ASCII(asciidig));
2683 /* Simply get a pointer to existing ASCII characters. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002684 buffer = PyUnicode_AsUTF8AndSize(asciidig, &buflen);
Serhiy Storchaka9b6c60c2017-11-13 21:23:48 +02002685 assert(buffer != NULL);
2686
2687 result = PyLong_FromString(buffer, &end, base);
2688 if (end == NULL || (result != NULL && end == buffer + buflen)) {
Alexander Belopolsky942af5a2010-12-04 03:38:46 +00002689 Py_DECREF(asciidig);
Serhiy Storchaka9b6c60c2017-11-13 21:23:48 +02002690 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 }
Serhiy Storchaka9b6c60c2017-11-13 21:23:48 +02002692 Py_DECREF(asciidig);
2693 Py_XDECREF(result);
Serhiy Storchaka579ddc22013-08-03 21:14:05 +03002694 PyErr_Format(PyExc_ValueError,
2695 "invalid literal for int() with base %d: %.200R",
2696 base, u);
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03002697 return NULL;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002698}
2699
Tim Peters9f688bf2000-07-07 15:53:28 +00002700/* forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002701static PyLongObject *x_divrem
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 (PyLongObject *, PyLongObject *, PyLongObject **);
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03002703static PyObject *long_long(PyObject *v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002704
Serhiy Storchaka95949422013-08-27 19:40:23 +03002705/* Int division with remainder, top-level routine */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002706
Guido van Rossume32e0141992-01-19 16:31:05 +00002707static int
Tim Peters9f688bf2000-07-07 15:53:28 +00002708long_divrem(PyLongObject *a, PyLongObject *b,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyLongObject **pdiv, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002710{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02002711 Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 if (size_b == 0) {
2715 PyErr_SetString(PyExc_ZeroDivisionError,
2716 "integer division or modulo by zero");
2717 return -1;
2718 }
2719 if (size_a < size_b ||
2720 (size_a == size_b &&
2721 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
2722 /* |a| < |b|. */
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03002723 *prem = (PyLongObject *)long_long((PyObject *)a);
Mark Dickinsonb820d7f2016-08-22 12:24:46 +01002724 if (*prem == NULL) {
Mark Dickinsonb820d7f2016-08-22 12:24:46 +01002725 return -1;
2726 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +03002727 Py_INCREF(_PyLong_Zero);
2728 *pdiv = (PyLongObject*)_PyLong_Zero;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 return 0;
2730 }
2731 if (size_b == 1) {
2732 digit rem = 0;
2733 z = divrem1(a, b->ob_digit[0], &rem);
2734 if (z == NULL)
2735 return -1;
2736 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
2737 if (*prem == NULL) {
2738 Py_DECREF(z);
2739 return -1;
2740 }
2741 }
2742 else {
2743 z = x_divrem(a, b, prem);
2744 if (z == NULL)
2745 return -1;
2746 }
2747 /* Set the signs.
2748 The quotient z has the sign of a*b;
2749 the remainder r has the sign of a,
2750 so a = b*z + r. */
Victor Stinner8aed6f12013-07-17 22:31:17 +02002751 if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0)) {
2752 _PyLong_Negate(&z);
2753 if (z == NULL) {
2754 Py_CLEAR(*prem);
2755 return -1;
2756 }
2757 }
2758 if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0) {
2759 _PyLong_Negate(prem);
2760 if (*prem == NULL) {
2761 Py_DECREF(z);
2762 Py_CLEAR(*prem);
2763 return -1;
2764 }
2765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 *pdiv = maybe_small_long(z);
2767 return 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002768}
2769
Serhiy Storchaka95949422013-08-27 19:40:23 +03002770/* Unsigned int division with remainder -- the algorithm. The arguments v1
Victor Stinner45e8e2f2014-05-14 17:24:35 +02002771 and w1 should satisfy 2 <= Py_ABS(Py_SIZE(w1)) <= Py_ABS(Py_SIZE(v1)). */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002772
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002773static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00002774x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 PyLongObject *v, *w, *a;
2777 Py_ssize_t i, k, size_v, size_w;
2778 int d;
2779 digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
2780 twodigits vv;
2781 sdigit zhi;
2782 stwodigits z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
2785 edn.), section 4.3.1, Algorithm D], except that we don't explicitly
2786 handle the special case when the initial estimate q for a quotient
2787 digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
2788 that won't overflow a digit. */
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00002789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 /* allocate space; w will also be used to hold the final remainder */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02002791 size_v = Py_ABS(Py_SIZE(v1));
2792 size_w = Py_ABS(Py_SIZE(w1));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
2794 v = _PyLong_New(size_v+1);
2795 if (v == NULL) {
2796 *prem = NULL;
2797 return NULL;
2798 }
2799 w = _PyLong_New(size_w);
2800 if (w == NULL) {
2801 Py_DECREF(v);
2802 *prem = NULL;
2803 return NULL;
2804 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
2807 shift v1 left by the same amount. Results go into w and v. */
2808 d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
2809 carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
2810 assert(carry == 0);
2811 carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
2812 if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
2813 v->ob_digit[size_v] = carry;
2814 size_v++;
2815 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
2818 at most (and usually exactly) k = size_v - size_w digits. */
2819 k = size_v - size_w;
2820 assert(k >= 0);
2821 a = _PyLong_New(k);
2822 if (a == NULL) {
2823 Py_DECREF(w);
2824 Py_DECREF(v);
2825 *prem = NULL;
2826 return NULL;
2827 }
2828 v0 = v->ob_digit;
2829 w0 = w->ob_digit;
2830 wm1 = w0[size_w-1];
2831 wm2 = w0[size_w-2];
2832 for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
2833 /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
2834 single-digit quotient q, remainder in vk[0:size_w]. */
Tim Peters5af4e6c2002-08-12 02:31:19 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00002837 Py_DECREF(a);
2838 Py_DECREF(w);
2839 Py_DECREF(v);
2840 *prem = NULL;
2841 return NULL;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00002842 });
Tim Peters5af4e6c2002-08-12 02:31:19 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 /* estimate quotient digit q; may overestimate by 1 (rare) */
2845 vtop = vk[size_w];
2846 assert(vtop <= wm1);
2847 vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
2848 q = (digit)(vv / wm1);
2849 r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
2850 while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
2851 | vk[size_w-2])) {
2852 --q;
2853 r += wm1;
2854 if (r >= PyLong_BASE)
2855 break;
2856 }
2857 assert(q <= PyLong_BASE);
Tim Peters5af4e6c2002-08-12 02:31:19 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
2860 zhi = 0;
2861 for (i = 0; i < size_w; ++i) {
2862 /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
2863 -PyLong_BASE * q <= z < PyLong_BASE */
2864 z = (sdigit)vk[i] + zhi -
2865 (stwodigits)q * (stwodigits)w0[i];
2866 vk[i] = (digit)z & PyLong_MASK;
2867 zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
Mark Dickinson22b20182010-05-10 21:27:53 +00002868 z, PyLong_SHIFT);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 /* add w back if q was too large (this branch taken rarely) */
2872 assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
2873 if ((sdigit)vtop + zhi < 0) {
2874 carry = 0;
2875 for (i = 0; i < size_w; ++i) {
2876 carry += vk[i] + w0[i];
2877 vk[i] = carry & PyLong_MASK;
2878 carry >>= PyLong_SHIFT;
2879 }
2880 --q;
2881 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 /* store quotient digit */
2884 assert(q < PyLong_BASE);
2885 *--ak = q;
2886 }
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00002887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 /* unshift remainder; we reuse w to store the result */
2889 carry = v_rshift(w0, v0, size_w, d);
2890 assert(carry==0);
2891 Py_DECREF(v);
Mark Dickinson17e4fdd2009-03-23 18:44:57 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 *prem = long_normalize(w);
2894 return long_normalize(a);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00002895}
2896
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002897/* For a nonzero PyLong a, express a in the form x * 2**e, with 0.5 <=
2898 abs(x) < 1.0 and e >= 0; return x and put e in *e. Here x is
2899 rounded to DBL_MANT_DIG significant bits using round-half-to-even.
2900 If a == 0, return 0.0 and set *e = 0. If the resulting exponent
2901 e is larger than PY_SSIZE_T_MAX, raise OverflowError and return
2902 -1.0. */
2903
2904/* attempt to define 2.0**DBL_MANT_DIG as a compile-time constant */
2905#if DBL_MANT_DIG == 53
2906#define EXP2_DBL_MANT_DIG 9007199254740992.0
2907#else
2908#define EXP2_DBL_MANT_DIG (ldexp(1.0, DBL_MANT_DIG))
2909#endif
2910
2911double
2912_PyLong_Frexp(PyLongObject *a, Py_ssize_t *e)
2913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 Py_ssize_t a_size, a_bits, shift_digits, shift_bits, x_size;
2915 /* See below for why x_digits is always large enough. */
2916 digit rem, x_digits[2 + (DBL_MANT_DIG + 1) / PyLong_SHIFT];
2917 double dx;
2918 /* Correction term for round-half-to-even rounding. For a digit x,
2919 "x + half_even_correction[x & 7]" gives x rounded to the nearest
2920 multiple of 4, rounding ties to a multiple of 8. */
2921 static const int half_even_correction[8] = {0, -1, -2, 1, 0, -1, 2, 1};
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002922
Victor Stinner45e8e2f2014-05-14 17:24:35 +02002923 a_size = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (a_size == 0) {
2925 /* Special case for 0: significand 0.0, exponent 0. */
2926 *e = 0;
2927 return 0.0;
2928 }
2929 a_bits = bits_in_digit(a->ob_digit[a_size-1]);
2930 /* The following is an overflow-free version of the check
2931 "if ((a_size - 1) * PyLong_SHIFT + a_bits > PY_SSIZE_T_MAX) ..." */
2932 if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 &&
2933 (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 ||
2934 a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1))
Mark Dickinson22b20182010-05-10 21:27:53 +00002935 goto overflow;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 a_bits = (a_size - 1) * PyLong_SHIFT + a_bits;
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 /* Shift the first DBL_MANT_DIG + 2 bits of a into x_digits[0:x_size]
2939 (shifting left if a_bits <= DBL_MANT_DIG + 2).
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Number of digits needed for result: write // for floor division.
2942 Then if shifting left, we end up using
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 1 + a_size + (DBL_MANT_DIG + 2 - a_bits) // PyLong_SHIFT
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 digits. If shifting right, we use
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 a_size - (a_bits - DBL_MANT_DIG - 2) // PyLong_SHIFT
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 digits. Using a_size = 1 + (a_bits - 1) // PyLong_SHIFT along with
2951 the inequalities
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 m // PyLong_SHIFT + n // PyLong_SHIFT <= (m + n) // PyLong_SHIFT
2954 m // PyLong_SHIFT - n // PyLong_SHIFT <=
2955 1 + (m - n - 1) // PyLong_SHIFT,
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 valid for any integers m and n, we find that x_size satisfies
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 x_size <= 2 + (DBL_MANT_DIG + 1) // PyLong_SHIFT
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 in both cases.
2962 */
2963 if (a_bits <= DBL_MANT_DIG + 2) {
2964 shift_digits = (DBL_MANT_DIG + 2 - a_bits) / PyLong_SHIFT;
2965 shift_bits = (DBL_MANT_DIG + 2 - a_bits) % PyLong_SHIFT;
2966 x_size = 0;
2967 while (x_size < shift_digits)
2968 x_digits[x_size++] = 0;
2969 rem = v_lshift(x_digits + x_size, a->ob_digit, a_size,
2970 (int)shift_bits);
2971 x_size += a_size;
2972 x_digits[x_size++] = rem;
2973 }
2974 else {
2975 shift_digits = (a_bits - DBL_MANT_DIG - 2) / PyLong_SHIFT;
2976 shift_bits = (a_bits - DBL_MANT_DIG - 2) % PyLong_SHIFT;
2977 rem = v_rshift(x_digits, a->ob_digit + shift_digits,
2978 a_size - shift_digits, (int)shift_bits);
2979 x_size = a_size - shift_digits;
2980 /* For correct rounding below, we need the least significant
2981 bit of x to be 'sticky' for this shift: if any of the bits
2982 shifted out was nonzero, we set the least significant bit
2983 of x. */
2984 if (rem)
2985 x_digits[0] |= 1;
2986 else
2987 while (shift_digits > 0)
2988 if (a->ob_digit[--shift_digits]) {
2989 x_digits[0] |= 1;
2990 break;
2991 }
2992 }
Victor Stinner63941882011-09-29 00:42:28 +02002993 assert(1 <= x_size && x_size <= (Py_ssize_t)Py_ARRAY_LENGTH(x_digits));
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00002994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 /* Round, and convert to double. */
2996 x_digits[0] += half_even_correction[x_digits[0] & 7];
2997 dx = x_digits[--x_size];
2998 while (x_size > 0)
2999 dx = dx * PyLong_BASE + x_digits[--x_size];
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 /* Rescale; make correction if result is 1.0. */
3002 dx /= 4.0 * EXP2_DBL_MANT_DIG;
3003 if (dx == 1.0) {
3004 if (a_bits == PY_SSIZE_T_MAX)
3005 goto overflow;
3006 dx = 0.5;
3007 a_bits += 1;
3008 }
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 *e = a_bits;
3011 return Py_SIZE(a) < 0 ? -dx : dx;
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003012
3013 overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 /* exponent > PY_SSIZE_T_MAX */
3015 PyErr_SetString(PyExc_OverflowError,
3016 "huge integer: number of bits overflows a Py_ssize_t");
3017 *e = 0;
3018 return -1.0;
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003019}
3020
Serhiy Storchaka95949422013-08-27 19:40:23 +03003021/* Get a C double from an int object. Rounds to the nearest double,
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003022 using the round-half-to-even rule in the case of a tie. */
3023
3024double
3025PyLong_AsDouble(PyObject *v)
3026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 Py_ssize_t exponent;
3028 double x;
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003029
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02003030 if (v == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyErr_BadInternalCall();
3032 return -1.0;
3033 }
Nadeem Vawda3d5881e2011-09-07 21:40:26 +02003034 if (!PyLong_Check(v)) {
3035 PyErr_SetString(PyExc_TypeError, "an integer is required");
3036 return -1.0;
3037 }
Yury Selivanov186c30b2016-02-05 19:40:01 -05003038 if (Py_ABS(Py_SIZE(v)) <= 1) {
Yury Selivanova0fcaca2016-02-06 12:21:33 -05003039 /* Fast path; single digit long (31 bits) will cast safely
Mark Dickinson1dc3c892016-08-21 10:33:36 +01003040 to double. This improves performance of FP/long operations
3041 by 20%.
Yury Selivanov186c30b2016-02-05 19:40:01 -05003042 */
3043 return (double)MEDIUM_VALUE((PyLongObject *)v);
3044 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 x = _PyLong_Frexp((PyLongObject *)v, &exponent);
3046 if ((x == -1.0 && PyErr_Occurred()) || exponent > DBL_MAX_EXP) {
3047 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson02515f72013-08-03 12:08:22 +01003048 "int too large to convert to float");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 return -1.0;
3050 }
3051 return ldexp(x, (int)exponent);
Mark Dickinson6ecd9e52010-01-02 15:33:56 +00003052}
3053
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003054/* Methods */
3055
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003056static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003057long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 if (Py_SIZE(a) != Py_SIZE(b)) {
3062 sign = Py_SIZE(a) - Py_SIZE(b);
3063 }
3064 else {
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003065 Py_ssize_t i = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
3067 ;
3068 if (i < 0)
3069 sign = 0;
3070 else {
3071 sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
3072 if (Py_SIZE(a) < 0)
3073 sign = -sign;
3074 }
3075 }
3076 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003077}
3078
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003079static PyObject *
3080long_richcompare(PyObject *self, PyObject *other, int op)
3081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 CHECK_BINOP(self, other);
3084 if (self == other)
3085 result = 0;
3086 else
3087 result = long_compare((PyLongObject*)self, (PyLongObject*)other);
stratakise8b19652017-11-02 11:32:54 +01003088 Py_RETURN_RICHCOMPARE(result, 0, op);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003089}
3090
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003091static Py_hash_t
Tim Peters9f688bf2000-07-07 15:53:28 +00003092long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00003093{
Benjamin Peterson8035bc52010-10-23 16:20:50 +00003094 Py_uhash_t x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 Py_ssize_t i;
3096 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 i = Py_SIZE(v);
3099 switch(i) {
3100 case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
3101 case 0: return 0;
3102 case 1: return v->ob_digit[0];
3103 }
3104 sign = 1;
3105 x = 0;
3106 if (i < 0) {
3107 sign = -1;
3108 i = -(i);
3109 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 while (--i >= 0) {
Mark Dickinsondc787d22010-05-23 13:33:13 +00003111 /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
3112 want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo
3113 _PyHASH_MODULUS.
3114
3115 The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS
3116 amounts to a rotation of the bits of x. To see this, write
3117
3118 x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z
3119
3120 where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top
3121 PyLong_SHIFT bits of x (those that are shifted out of the
3122 original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
3123 _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT
3124 bits of x, shifted up. Then since 2**_PyHASH_BITS is
3125 congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is
3126 congruent to y modulo _PyHASH_MODULUS. So
3127
3128 x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS).
3129
3130 The right-hand side is just the result of rotating the
3131 _PyHASH_BITS bits of x left by PyLong_SHIFT places; since
3132 not all _PyHASH_BITS bits of x are 1s, the same is true
3133 after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is
3134 the reduction of x*2**PyLong_SHIFT modulo
3135 _PyHASH_MODULUS. */
3136 x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) |
3137 (x >> (_PyHASH_BITS - PyLong_SHIFT));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 x += v->ob_digit[i];
Mark Dickinsondc787d22010-05-23 13:33:13 +00003139 if (x >= _PyHASH_MODULUS)
3140 x -= _PyHASH_MODULUS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 }
3142 x = x * sign;
Benjamin Peterson8035bc52010-10-23 16:20:50 +00003143 if (x == (Py_uhash_t)-1)
3144 x = (Py_uhash_t)-2;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003145 return (Py_hash_t)x;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003146}
3147
3148
Serhiy Storchaka95949422013-08-27 19:40:23 +03003149/* Add the absolute values of two integers. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003150
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003151static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003152x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003153{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003154 Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 PyLongObject *z;
3156 Py_ssize_t i;
3157 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 /* Ensure a is the larger of the two: */
3160 if (size_a < size_b) {
3161 { PyLongObject *temp = a; a = b; b = temp; }
3162 { Py_ssize_t size_temp = size_a;
Mark Dickinson22b20182010-05-10 21:27:53 +00003163 size_a = size_b;
3164 size_b = size_temp; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 }
3166 z = _PyLong_New(size_a+1);
3167 if (z == NULL)
3168 return NULL;
3169 for (i = 0; i < size_b; ++i) {
3170 carry += a->ob_digit[i] + b->ob_digit[i];
3171 z->ob_digit[i] = carry & PyLong_MASK;
3172 carry >>= PyLong_SHIFT;
3173 }
3174 for (; i < size_a; ++i) {
3175 carry += a->ob_digit[i];
3176 z->ob_digit[i] = carry & PyLong_MASK;
3177 carry >>= PyLong_SHIFT;
3178 }
3179 z->ob_digit[i] = carry;
3180 return long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003181}
3182
3183/* Subtract the absolute values of two integers. */
3184
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003185static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003186x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003187{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003188 Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 PyLongObject *z;
3190 Py_ssize_t i;
3191 int sign = 1;
3192 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00003193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 /* Ensure a is the larger of the two: */
3195 if (size_a < size_b) {
3196 sign = -1;
3197 { PyLongObject *temp = a; a = b; b = temp; }
3198 { Py_ssize_t size_temp = size_a;
Mark Dickinson22b20182010-05-10 21:27:53 +00003199 size_a = size_b;
3200 size_b = size_temp; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
3202 else if (size_a == size_b) {
3203 /* Find highest digit where a and b differ: */
3204 i = size_a;
3205 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
3206 ;
3207 if (i < 0)
3208 return (PyLongObject *)PyLong_FromLong(0);
3209 if (a->ob_digit[i] < b->ob_digit[i]) {
3210 sign = -1;
3211 { PyLongObject *temp = a; a = b; b = temp; }
3212 }
3213 size_a = size_b = i+1;
3214 }
3215 z = _PyLong_New(size_a);
3216 if (z == NULL)
3217 return NULL;
3218 for (i = 0; i < size_b; ++i) {
3219 /* The following assumes unsigned arithmetic
3220 works module 2**N for some N>PyLong_SHIFT. */
3221 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
3222 z->ob_digit[i] = borrow & PyLong_MASK;
3223 borrow >>= PyLong_SHIFT;
3224 borrow &= 1; /* Keep only one sign bit */
3225 }
3226 for (; i < size_a; ++i) {
3227 borrow = a->ob_digit[i] - borrow;
3228 z->ob_digit[i] = borrow & PyLong_MASK;
3229 borrow >>= PyLong_SHIFT;
3230 borrow &= 1; /* Keep only one sign bit */
3231 }
3232 assert(borrow == 0);
Victor Stinner8aed6f12013-07-17 22:31:17 +02003233 if (sign < 0) {
Victor Stinner8bcf3122016-08-17 19:48:33 +02003234 Py_SIZE(z) = -Py_SIZE(z);
Victor Stinner8aed6f12013-07-17 22:31:17 +02003235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 return long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003237}
3238
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003239static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003240long_add(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00003243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003245
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003246 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Mark Dickinsonc1c4a642016-09-17 20:01:56 +01003247 return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 }
3249 if (Py_SIZE(a) < 0) {
3250 if (Py_SIZE(b) < 0) {
3251 z = x_add(a, b);
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003252 if (z != NULL) {
3253 /* x_add received at least one multiple-digit int,
3254 and thus z must be a multiple-digit int.
3255 That also means z is not an element of
3256 small_ints, so negating it in-place is safe. */
3257 assert(Py_REFCNT(z) == 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 Py_SIZE(z) = -(Py_SIZE(z));
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 }
3261 else
3262 z = x_sub(b, a);
3263 }
3264 else {
3265 if (Py_SIZE(b) < 0)
3266 z = x_sub(a, b);
3267 else
3268 z = x_add(a, b);
3269 }
3270 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003271}
3272
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003273static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003274long_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003279
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003280 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Mark Dickinsonc1c4a642016-09-17 20:01:56 +01003281 return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 }
3283 if (Py_SIZE(a) < 0) {
3284 if (Py_SIZE(b) < 0)
3285 z = x_sub(a, b);
3286 else
3287 z = x_add(a, b);
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003288 if (z != NULL) {
3289 assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 Py_SIZE(z) = -(Py_SIZE(z));
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 }
3293 else {
3294 if (Py_SIZE(b) < 0)
3295 z = x_add(a, b);
3296 else
3297 z = x_sub(a, b);
3298 }
3299 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003300}
3301
Tim Peters5af4e6c2002-08-12 02:31:19 +00003302/* Grade school multiplication, ignoring the signs.
3303 * Returns the absolute value of the product, or NULL if error.
3304 */
3305static PyLongObject *
3306x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 PyLongObject *z;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003309 Py_ssize_t size_a = Py_ABS(Py_SIZE(a));
3310 Py_ssize_t size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 z = _PyLong_New(size_a + size_b);
3314 if (z == NULL)
3315 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
3318 if (a == b) {
3319 /* Efficient squaring per HAC, Algorithm 14.16:
3320 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
3321 * Gives slightly less than a 2x speedup when a == b,
3322 * via exploiting that each entry in the multiplication
3323 * pyramid appears twice (except for the size_a squares).
3324 */
3325 for (i = 0; i < size_a; ++i) {
3326 twodigits carry;
3327 twodigits f = a->ob_digit[i];
3328 digit *pz = z->ob_digit + (i << 1);
3329 digit *pa = a->ob_digit + i + 1;
3330 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00003333 Py_DECREF(z);
3334 return NULL;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00003335 });
Tim Peters0973b992004-08-29 22:16:50 +00003336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 carry = *pz + f * f;
3338 *pz++ = (digit)(carry & PyLong_MASK);
3339 carry >>= PyLong_SHIFT;
3340 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00003341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 /* Now f is added in twice in each column of the
3343 * pyramid it appears. Same as adding f<<1 once.
3344 */
3345 f <<= 1;
3346 while (pa < paend) {
3347 carry += *pz + *pa++ * f;
3348 *pz++ = (digit)(carry & PyLong_MASK);
3349 carry >>= PyLong_SHIFT;
3350 assert(carry <= (PyLong_MASK << 1));
3351 }
3352 if (carry) {
3353 carry += *pz;
3354 *pz++ = (digit)(carry & PyLong_MASK);
3355 carry >>= PyLong_SHIFT;
3356 }
3357 if (carry)
3358 *pz += (digit)(carry & PyLong_MASK);
3359 assert((carry >> PyLong_SHIFT) == 0);
3360 }
3361 }
Serhiy Storchaka95949422013-08-27 19:40:23 +03003362 else { /* a is not the same as b -- gradeschool int mult */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 for (i = 0; i < size_a; ++i) {
3364 twodigits carry = 0;
3365 twodigits f = a->ob_digit[i];
3366 digit *pz = z->ob_digit + i;
3367 digit *pb = b->ob_digit;
3368 digit *pbend = b->ob_digit + size_b;
Tim Peters0973b992004-08-29 22:16:50 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00003371 Py_DECREF(z);
3372 return NULL;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00003373 });
Tim Peters0973b992004-08-29 22:16:50 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 while (pb < pbend) {
3376 carry += *pz + *pb++ * f;
3377 *pz++ = (digit)(carry & PyLong_MASK);
3378 carry >>= PyLong_SHIFT;
3379 assert(carry <= PyLong_MASK);
3380 }
3381 if (carry)
3382 *pz += (digit)(carry & PyLong_MASK);
3383 assert((carry >> PyLong_SHIFT) == 0);
3384 }
3385 }
3386 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003387}
3388
3389/* A helper for Karatsuba multiplication (k_mul).
Serhiy Storchaka95949422013-08-27 19:40:23 +03003390 Takes an int "n" and an integer "size" representing the place to
Tim Peters5af4e6c2002-08-12 02:31:19 +00003391 split, and sets low and high such that abs(n) == (high << size) + low,
3392 viewing the shift as being by digits. The sign bit is ignored, and
3393 the return values are >= 0.
3394 Returns 0 on success, -1 on failure.
3395*/
3396static int
Mark Dickinson22b20182010-05-10 21:27:53 +00003397kmul_split(PyLongObject *n,
3398 Py_ssize_t size,
3399 PyLongObject **high,
3400 PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00003401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 PyLongObject *hi, *lo;
3403 Py_ssize_t size_lo, size_hi;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003404 const Py_ssize_t size_n = Py_ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003405
Victor Stinner640c35c2013-06-04 23:14:37 +02003406 size_lo = Py_MIN(size_n, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 size_hi = size_n - size_lo;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if ((hi = _PyLong_New(size_hi)) == NULL)
3410 return -1;
3411 if ((lo = _PyLong_New(size_lo)) == NULL) {
3412 Py_DECREF(hi);
3413 return -1;
3414 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
3417 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 *high = long_normalize(hi);
3420 *low = long_normalize(lo);
3421 return 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003422}
3423
Tim Peters60004642002-08-12 22:01:34 +00003424static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
3425
Tim Peters5af4e6c2002-08-12 02:31:19 +00003426/* Karatsuba multiplication. Ignores the input signs, and returns the
3427 * absolute value of the product (or NULL if error).
3428 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
3429 */
3430static PyLongObject *
3431k_mul(PyLongObject *a, PyLongObject *b)
3432{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003433 Py_ssize_t asize = Py_ABS(Py_SIZE(a));
3434 Py_ssize_t bsize = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 PyLongObject *ah = NULL;
3436 PyLongObject *al = NULL;
3437 PyLongObject *bh = NULL;
3438 PyLongObject *bl = NULL;
3439 PyLongObject *ret = NULL;
3440 PyLongObject *t1, *t2, *t3;
3441 Py_ssize_t shift; /* the number of digits we split off */
3442 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00003443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
3445 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
3446 * Then the original product is
3447 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
3448 * By picking X to be a power of 2, "*X" is just shifting, and it's
3449 * been reduced to 3 multiplies on numbers half the size.
3450 */
Tim Peters5af4e6c2002-08-12 02:31:19 +00003451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 /* We want to split based on the larger number; fiddle so that b
3453 * is largest.
3454 */
3455 if (asize > bsize) {
3456 t1 = a;
3457 a = b;
3458 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00003459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 i = asize;
3461 asize = bsize;
3462 bsize = i;
3463 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 /* Use gradeschool math when either number is too small. */
3466 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
3467 if (asize <= i) {
3468 if (asize == 0)
3469 return (PyLongObject *)PyLong_FromLong(0);
3470 else
3471 return x_mul(a, b);
3472 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 /* If a is small compared to b, splitting on b gives a degenerate
3475 * case with ah==0, and Karatsuba may be (even much) less efficient
3476 * than "grade school" then. However, we can still win, by viewing
3477 * b as a string of "big digits", each of width a->ob_size. That
3478 * leads to a sequence of balanced calls to k_mul.
3479 */
3480 if (2 * asize <= bsize)
3481 return k_lopsided_mul(a, b);
Tim Peters60004642002-08-12 22:01:34 +00003482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 /* Split a & b into hi & lo pieces. */
3484 shift = bsize >> 1;
3485 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
3486 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 if (a == b) {
3489 bh = ah;
3490 bl = al;
3491 Py_INCREF(bh);
3492 Py_INCREF(bl);
3493 }
3494 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 /* The plan:
3497 * 1. Allocate result space (asize + bsize digits: that's always
3498 * enough).
3499 * 2. Compute ah*bh, and copy into result at 2*shift.
3500 * 3. Compute al*bl, and copy into result at 0. Note that this
3501 * can't overlap with #2.
3502 * 4. Subtract al*bl from the result, starting at shift. This may
3503 * underflow (borrow out of the high digit), but we don't care:
3504 * we're effectively doing unsigned arithmetic mod
3505 * BASE**(sizea + sizeb), and so long as the *final* result fits,
3506 * borrows and carries out of the high digit can be ignored.
3507 * 5. Subtract ah*bh from the result, starting at shift.
3508 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
3509 * at shift.
3510 */
Tim Petersd64c1de2002-08-12 17:36:03 +00003511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 /* 1. Allocate result space. */
3513 ret = _PyLong_New(asize + bsize);
3514 if (ret == NULL) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003515#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 /* Fill with trash, to catch reference to uninitialized digits. */
3517 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003518#endif
Tim Peters44121a62002-08-12 06:17:58 +00003519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 /* 2. t1 <- ah*bh, and copy into high digits of result. */
3521 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
3522 assert(Py_SIZE(t1) >= 0);
3523 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
3524 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
3525 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00003526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 /* Zero-out the digits higher than the ah*bh copy. */
3528 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
3529 if (i)
3530 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
3531 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 /* 3. t2 <- al*bl, and copy into the low digits. */
3534 if ((t2 = k_mul(al, bl)) == NULL) {
3535 Py_DECREF(t1);
3536 goto fail;
3537 }
3538 assert(Py_SIZE(t2) >= 0);
3539 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
3540 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 /* Zero out remaining digits. */
3543 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
3544 if (i)
3545 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
3548 * because it's fresher in cache.
3549 */
3550 i = Py_SIZE(ret) - shift; /* # digits after shift */
3551 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
3552 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00003553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
3555 Py_DECREF(t1);
Tim Peters738eda72002-08-12 15:08:20 +00003556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
3558 if ((t1 = x_add(ah, al)) == NULL) goto fail;
3559 Py_DECREF(ah);
3560 Py_DECREF(al);
3561 ah = al = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (a == b) {
3564 t2 = t1;
3565 Py_INCREF(t2);
3566 }
3567 else if ((t2 = x_add(bh, bl)) == NULL) {
3568 Py_DECREF(t1);
3569 goto fail;
3570 }
3571 Py_DECREF(bh);
3572 Py_DECREF(bl);
3573 bh = bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 t3 = k_mul(t1, t2);
3576 Py_DECREF(t1);
3577 Py_DECREF(t2);
3578 if (t3 == NULL) goto fail;
3579 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 /* Add t3. It's not obvious why we can't run out of room here.
3582 * See the (*) comment after this function.
3583 */
3584 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
3585 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 return long_normalize(ret);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003588
Mark Dickinson22b20182010-05-10 21:27:53 +00003589 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 Py_XDECREF(ret);
3591 Py_XDECREF(ah);
3592 Py_XDECREF(al);
3593 Py_XDECREF(bh);
3594 Py_XDECREF(bl);
3595 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003596}
3597
Tim Petersd6974a52002-08-13 20:37:51 +00003598/* (*) Why adding t3 can't "run out of room" above.
3599
Tim Petersab86c2b2002-08-15 20:06:00 +00003600Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
3601to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00003602
Tim Petersab86c2b2002-08-15 20:06:00 +000036031. For any integer i, i = c(i/2) + f(i/2). In particular,
3604 bsize = c(bsize/2) + f(bsize/2).
36052. shift = f(bsize/2)
36063. asize <= bsize
36074. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
3608 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00003609
Tim Petersab86c2b2002-08-15 20:06:00 +00003610We allocated asize + bsize result digits, and add t3 into them at an offset
3611of shift. This leaves asize+bsize-shift allocated digit positions for t3
3612to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
3613asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00003614
Tim Petersab86c2b2002-08-15 20:06:00 +00003615bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
3616at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00003617
Tim Petersab86c2b2002-08-15 20:06:00 +00003618If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
3619digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
3620most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00003621
Tim Petersab86c2b2002-08-15 20:06:00 +00003622The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00003623
Tim Petersab86c2b2002-08-15 20:06:00 +00003624 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00003625
Tim Petersab86c2b2002-08-15 20:06:00 +00003626and we have asize + c(bsize/2) available digit positions. We need to show
3627this is always enough. An instance of c(bsize/2) cancels out in both, so
3628the question reduces to whether asize digits is enough to hold
3629(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
3630then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
3631asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00003632digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00003633asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00003634c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
3635is enough to hold 2 bits. This is so if bsize >= 2, which holds because
3636bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00003637
Tim Peters48d52c02002-08-14 17:07:32 +00003638Note that since there's always enough room for (ah+al)*(bh+bl), and that's
3639clearly >= each of ah*bh and al*bl, there's always enough room to subtract
3640ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00003641*/
3642
Tim Peters60004642002-08-12 22:01:34 +00003643/* b has at least twice the digits of a, and a is big enough that Karatsuba
3644 * would pay off *if* the inputs had balanced sizes. View b as a sequence
3645 * of slices, each with a->ob_size digits, and multiply the slices by a,
3646 * one at a time. This gives k_mul balanced inputs to work with, and is
3647 * also cache-friendly (we compute one double-width slice of the result
Ezio Melotti42da6632011-03-15 05:18:48 +02003648 * at a time, then move on, never backtracking except for the helpful
Tim Peters60004642002-08-12 22:01:34 +00003649 * single-width slice overlap between successive partial sums).
3650 */
3651static PyLongObject *
3652k_lopsided_mul(PyLongObject *a, PyLongObject *b)
3653{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003654 const Py_ssize_t asize = Py_ABS(Py_SIZE(a));
3655 Py_ssize_t bsize = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 Py_ssize_t nbdone; /* # of b digits already multiplied */
3657 PyLongObject *ret;
3658 PyLongObject *bslice = NULL;
Tim Peters60004642002-08-12 22:01:34 +00003659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 assert(asize > KARATSUBA_CUTOFF);
3661 assert(2 * asize <= bsize);
Tim Peters60004642002-08-12 22:01:34 +00003662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 /* Allocate result space, and zero it out. */
3664 ret = _PyLong_New(asize + bsize);
3665 if (ret == NULL)
3666 return NULL;
3667 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 /* Successive slices of b are copied into bslice. */
3670 bslice = _PyLong_New(asize);
3671 if (bslice == NULL)
3672 goto fail;
Tim Peters60004642002-08-12 22:01:34 +00003673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 nbdone = 0;
3675 while (bsize > 0) {
3676 PyLongObject *product;
Victor Stinner640c35c2013-06-04 23:14:37 +02003677 const Py_ssize_t nbtouse = Py_MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00003678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 /* Multiply the next slice of b by a. */
3680 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
3681 nbtouse * sizeof(digit));
3682 Py_SIZE(bslice) = nbtouse;
3683 product = k_mul(a, bslice);
3684 if (product == NULL)
3685 goto fail;
Tim Peters60004642002-08-12 22:01:34 +00003686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 /* Add into result. */
3688 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
3689 product->ob_digit, Py_SIZE(product));
3690 Py_DECREF(product);
Tim Peters60004642002-08-12 22:01:34 +00003691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 bsize -= nbtouse;
3693 nbdone += nbtouse;
3694 }
Tim Peters60004642002-08-12 22:01:34 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 Py_DECREF(bslice);
3697 return long_normalize(ret);
Tim Peters60004642002-08-12 22:01:34 +00003698
Mark Dickinson22b20182010-05-10 21:27:53 +00003699 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 Py_DECREF(ret);
3701 Py_XDECREF(bslice);
3702 return NULL;
Tim Peters60004642002-08-12 22:01:34 +00003703}
Tim Peters5af4e6c2002-08-12 02:31:19 +00003704
3705static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003706long_mul(PyLongObject *a, PyLongObject *b)
Tim Peters5af4e6c2002-08-12 02:31:19 +00003707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 CHECK_BINOP(a, b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 /* fast path for single-digit multiplication */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003713 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b);
Benjamin Petersonaf580df2016-09-06 10:46:49 -07003715 return PyLong_FromLongLong((long long)v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 z = k_mul(a, b);
3719 /* Negate if exactly one of the inputs is negative. */
Victor Stinner8aed6f12013-07-17 22:31:17 +02003720 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) {
3721 _PyLong_Negate(&z);
3722 if (z == NULL)
3723 return NULL;
3724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003726}
3727
Yury Selivanove0b23092016-02-11 10:26:27 -05003728/* Fast modulo division for single-digit longs. */
3729static PyObject *
3730fast_mod(PyLongObject *a, PyLongObject *b)
3731{
3732 sdigit left = a->ob_digit[0];
3733 sdigit right = b->ob_digit[0];
3734 sdigit mod;
3735
3736 assert(Py_ABS(Py_SIZE(a)) == 1);
3737 assert(Py_ABS(Py_SIZE(b)) == 1);
3738
3739 if (Py_SIZE(a) == Py_SIZE(b)) {
3740 /* 'a' and 'b' have the same sign. */
3741 mod = left % right;
3742 }
3743 else {
3744 /* Either 'a' or 'b' is negative. */
3745 mod = right - 1 - (left - 1) % right;
3746 }
3747
Victor Stinnerf963c132016-03-23 18:36:54 +01003748 return PyLong_FromLong(mod * (sdigit)Py_SIZE(b));
Yury Selivanove0b23092016-02-11 10:26:27 -05003749}
3750
3751/* Fast floor division for single-digit longs. */
3752static PyObject *
3753fast_floor_div(PyLongObject *a, PyLongObject *b)
3754{
3755 sdigit left = a->ob_digit[0];
3756 sdigit right = b->ob_digit[0];
3757 sdigit div;
3758
3759 assert(Py_ABS(Py_SIZE(a)) == 1);
3760 assert(Py_ABS(Py_SIZE(b)) == 1);
3761
3762 if (Py_SIZE(a) == Py_SIZE(b)) {
3763 /* 'a' and 'b' have the same sign. */
3764 div = left / right;
3765 }
3766 else {
3767 /* Either 'a' or 'b' is negative. */
3768 div = -1 - (left - 1) / right;
3769 }
3770
3771 return PyLong_FromLong(div);
3772}
3773
Guido van Rossume32e0141992-01-19 16:31:05 +00003774/* The / and % operators are now defined in terms of divmod().
3775 The expression a mod b has the value a - b*floor(a/b).
3776 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003777 |a| by |b|, with the sign of a. This is also expressed
3778 as a - b*trunc(a/b), if trunc truncates towards zero.
3779 Some examples:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 a b a rem b a mod b
3781 13 10 3 3
3782 -13 10 -3 7
3783 13 -10 3 -7
3784 -13 -10 -3 -3
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003785 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003786 have different signs. We then subtract one from the 'div'
3787 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003788
Tim Peters47e52ee2004-08-30 02:44:38 +00003789/* Compute
3790 * *pdiv, *pmod = divmod(v, w)
3791 * NULL can be passed for pdiv or pmod, in which case that part of
3792 * the result is simply thrown away. The caller owns a reference to
3793 * each of these it requests (does not pass NULL for).
3794 */
Guido van Rossume32e0141992-01-19 16:31:05 +00003795static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00003796l_divmod(PyLongObject *v, PyLongObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00003798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003800
Yury Selivanove0b23092016-02-11 10:26:27 -05003801 if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) {
3802 /* Fast path for single-digit longs */
3803 div = NULL;
3804 if (pdiv != NULL) {
3805 div = (PyLongObject *)fast_floor_div(v, w);
3806 if (div == NULL) {
3807 return -1;
3808 }
3809 }
3810 if (pmod != NULL) {
3811 mod = (PyLongObject *)fast_mod(v, w);
3812 if (mod == NULL) {
3813 Py_XDECREF(div);
3814 return -1;
3815 }
3816 *pmod = mod;
3817 }
3818 if (pdiv != NULL) {
3819 /* We only want to set `*pdiv` when `*pmod` is
3820 set successfully. */
3821 *pdiv = div;
3822 }
3823 return 0;
3824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 if (long_divrem(v, w, &div, &mod) < 0)
3826 return -1;
3827 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
3828 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
3829 PyLongObject *temp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 temp = (PyLongObject *) long_add(mod, w);
3831 Py_DECREF(mod);
3832 mod = temp;
3833 if (mod == NULL) {
3834 Py_DECREF(div);
3835 return -1;
3836 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +03003837 temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One);
3838 if (temp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 Py_DECREF(mod);
3840 Py_DECREF(div);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 return -1;
3842 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 Py_DECREF(div);
3844 div = temp;
3845 }
3846 if (pdiv != NULL)
3847 *pdiv = div;
3848 else
3849 Py_DECREF(div);
Tim Peters47e52ee2004-08-30 02:44:38 +00003850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 if (pmod != NULL)
3852 *pmod = mod;
3853 else
3854 Py_DECREF(mod);
Tim Peters47e52ee2004-08-30 02:44:38 +00003855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 return 0;
Guido van Rossume32e0141992-01-19 16:31:05 +00003857}
3858
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003859static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003860long_div(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00003861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 PyLongObject *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 CHECK_BINOP(a, b);
Yury Selivanove0b23092016-02-11 10:26:27 -05003865
3866 if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) {
3867 return fast_floor_div((PyLongObject*)a, (PyLongObject*)b);
3868 }
3869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
3871 div = NULL;
3872 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00003873}
3874
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003875/* PyLong/PyLong -> float, with correctly rounded result. */
3876
3877#define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT)
3878#define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT)
3879
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003880static PyObject *
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003881long_true_divide(PyObject *v, PyObject *w)
Tim Peters20dab9f2001-09-04 05:31:47 +00003882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003883 PyLongObject *a, *b, *x;
3884 Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits;
3885 digit mask, low;
3886 int inexact, negate, a_is_small, b_is_small;
3887 double dx, result;
Tim Peterse2a60002001-09-04 06:17:36 +00003888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 CHECK_BINOP(v, w);
3890 a = (PyLongObject *)v;
3891 b = (PyLongObject *)w;
Tim Peterse2a60002001-09-04 06:17:36 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 /*
3894 Method in a nutshell:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003896 0. reduce to case a, b > 0; filter out obvious underflow/overflow
3897 1. choose a suitable integer 'shift'
3898 2. use integer arithmetic to compute x = floor(2**-shift*a/b)
3899 3. adjust x for correct rounding
3900 4. convert x to a double dx with the same value
3901 5. return ldexp(dx, shift).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 In more detail:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b
3906 returns either 0.0 or -0.0, depending on the sign of b. For a and
3907 b both nonzero, ignore signs of a and b, and add the sign back in
3908 at the end. Now write a_bits and b_bits for the bit lengths of a
3909 and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise
3910 for b). Then
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and
3915 so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP -
3916 DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of
3917 the way, we can assume that
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 1. The integer 'shift' is chosen so that x has the right number of
3922 bits for a double, plus two or three extra bits that will be used
3923 in the rounding decisions. Writing a_bits and b_bits for the
3924 number of significant bits in a and b respectively, a
3925 straightforward formula for shift is:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 shift = a_bits - b_bits - DBL_MANT_DIG - 2
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 This is fine in the usual case, but if a/b is smaller than the
3930 smallest normal float then it can lead to double rounding on an
3931 IEEE 754 platform, giving incorrectly rounded results. So we
3932 adjust the formula slightly. The actual formula used is:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 2. The quantity x is computed by first shifting a (left -shift bits
3937 if shift <= 0, right shift bits if shift > 0) and then dividing by
3938 b. For both the shift and the division, we keep track of whether
3939 the result is inexact, in a flag 'inexact'; this information is
3940 needed at the rounding stage.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 With the choice of shift above, together with our assumption that
3943 a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows
3944 that x >= 1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace
3947 this with an exactly representable float of the form
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 round(x/2**extra_bits) * 2**(extra_bits+shift).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 For float representability, we need x/2**extra_bits <
3952 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP -
3953 DBL_MANT_DIG. This translates to the condition:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 To round, we just modify the bottom digit of x in-place; this can
3958 end up giving a digit with value > PyLONG_MASK, but that's not a
3959 problem since digits can hold values up to 2*PyLONG_MASK+1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 With the original choices for shift above, extra_bits will always
3962 be 2 or 3. Then rounding under the round-half-to-even rule, we
3963 round up iff the most significant of the extra bits is 1, and
3964 either: (a) the computation of x in step 2 had an inexact result,
3965 or (b) at least one other of the extra bits is 1, or (c) the least
3966 significant bit of x (above those to be rounded) is 1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 4. Conversion to a double is straightforward; all floating-point
3969 operations involved in the conversion are exact, so there's no
3970 danger of rounding errors.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 5. Use ldexp(x, shift) to compute x*2**shift, the final result.
3973 The result will always be exactly representable as a double, except
3974 in the case that it overflows. To avoid dependence on the exact
3975 behaviour of ldexp on overflow, we check for overflow before
3976 applying ldexp. The result of ldexp is adjusted for sign before
3977 returning.
3978 */
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 /* Reduce to case where a and b are both positive. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003981 a_size = Py_ABS(Py_SIZE(a));
3982 b_size = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0);
3984 if (b_size == 0) {
3985 PyErr_SetString(PyExc_ZeroDivisionError,
3986 "division by zero");
3987 goto error;
3988 }
3989 if (a_size == 0)
3990 goto underflow_or_zero;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 /* Fast path for a and b small (exactly representable in a double).
3993 Relies on floating-point division being correctly rounded; results
3994 may be subject to double rounding on x86 machines that operate with
3995 the x87 FPU set to 64-bit precision. */
3996 a_is_small = a_size <= MANT_DIG_DIGITS ||
3997 (a_size == MANT_DIG_DIGITS+1 &&
3998 a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
3999 b_is_small = b_size <= MANT_DIG_DIGITS ||
4000 (b_size == MANT_DIG_DIGITS+1 &&
4001 b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4002 if (a_is_small && b_is_small) {
4003 double da, db;
4004 da = a->ob_digit[--a_size];
4005 while (a_size > 0)
4006 da = da * PyLong_BASE + a->ob_digit[--a_size];
4007 db = b->ob_digit[--b_size];
4008 while (b_size > 0)
4009 db = db * PyLong_BASE + b->ob_digit[--b_size];
4010 result = da / db;
4011 goto success;
4012 }
Tim Peterse2a60002001-09-04 06:17:36 +00004013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 /* Catch obvious cases of underflow and overflow */
4015 diff = a_size - b_size;
4016 if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1)
4017 /* Extreme overflow */
4018 goto overflow;
4019 else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT)
4020 /* Extreme underflow */
4021 goto underflow_or_zero;
4022 /* Next line is now safe from overflowing a Py_ssize_t */
4023 diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) -
4024 bits_in_digit(b->ob_digit[b_size - 1]);
4025 /* Now diff = a_bits - b_bits. */
4026 if (diff > DBL_MAX_EXP)
4027 goto overflow;
4028 else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1)
4029 goto underflow_or_zero;
Tim Peterse2a60002001-09-04 06:17:36 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 /* Choose value for shift; see comments for step 1 above. */
Victor Stinner640c35c2013-06-04 23:14:37 +02004032 shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 inexact = 0;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 /* x = abs(a * 2**-shift) */
4037 if (shift <= 0) {
4038 Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT;
4039 digit rem;
4040 /* x = a << -shift */
4041 if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) {
4042 /* In practice, it's probably impossible to end up
4043 here. Both a and b would have to be enormous,
4044 using close to SIZE_T_MAX bytes of memory each. */
4045 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson22b20182010-05-10 21:27:53 +00004046 "intermediate overflow during division");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 goto error;
4048 }
4049 x = _PyLong_New(a_size + shift_digits + 1);
4050 if (x == NULL)
4051 goto error;
4052 for (i = 0; i < shift_digits; i++)
4053 x->ob_digit[i] = 0;
4054 rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit,
4055 a_size, -shift % PyLong_SHIFT);
4056 x->ob_digit[a_size + shift_digits] = rem;
4057 }
4058 else {
4059 Py_ssize_t shift_digits = shift / PyLong_SHIFT;
4060 digit rem;
4061 /* x = a >> shift */
4062 assert(a_size >= shift_digits);
4063 x = _PyLong_New(a_size - shift_digits);
4064 if (x == NULL)
4065 goto error;
4066 rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits,
4067 a_size - shift_digits, shift % PyLong_SHIFT);
4068 /* set inexact if any of the bits shifted out is nonzero */
4069 if (rem)
4070 inexact = 1;
4071 while (!inexact && shift_digits > 0)
4072 if (a->ob_digit[--shift_digits])
4073 inexact = 1;
4074 }
4075 long_normalize(x);
4076 x_size = Py_SIZE(x);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 /* x //= b. If the remainder is nonzero, set inexact. We own the only
4079 reference to x, so it's safe to modify it in-place. */
4080 if (b_size == 1) {
4081 digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size,
4082 b->ob_digit[0]);
4083 long_normalize(x);
4084 if (rem)
4085 inexact = 1;
4086 }
4087 else {
4088 PyLongObject *div, *rem;
4089 div = x_divrem(x, b, &rem);
4090 Py_DECREF(x);
4091 x = div;
4092 if (x == NULL)
4093 goto error;
4094 if (Py_SIZE(rem))
4095 inexact = 1;
4096 Py_DECREF(rem);
4097 }
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004098 x_size = Py_ABS(Py_SIZE(x));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 assert(x_size > 0); /* result of division is never zero */
4100 x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 /* The number of extra bits that have to be rounded away. */
Victor Stinner640c35c2013-06-04 23:14:37 +02004103 extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 assert(extra_bits == 2 || extra_bits == 3);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 /* Round by directly modifying the low digit of x. */
4107 mask = (digit)1 << (extra_bits - 1);
4108 low = x->ob_digit[0] | inexact;
Mark Dickinsondc590a42016-08-21 10:23:23 +01004109 if ((low & mask) && (low & (3U*mask-1U)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 low += mask;
Mark Dickinsondc590a42016-08-21 10:23:23 +01004111 x->ob_digit[0] = low & ~(2U*mask-1U);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 /* Convert x to a double dx; the conversion is exact. */
4114 dx = x->ob_digit[--x_size];
4115 while (x_size > 0)
4116 dx = dx * PyLong_BASE + x->ob_digit[--x_size];
4117 Py_DECREF(x);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 /* Check whether ldexp result will overflow a double. */
4120 if (shift + x_bits >= DBL_MAX_EXP &&
4121 (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits)))
4122 goto overflow;
4123 result = ldexp(dx, (int)shift);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004124
4125 success:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 return PyFloat_FromDouble(negate ? -result : result);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004127
4128 underflow_or_zero:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 return PyFloat_FromDouble(negate ? -0.0 : 0.0);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004130
4131 overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 PyErr_SetString(PyExc_OverflowError,
4133 "integer division result too large for a float");
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004134 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 return NULL;
Tim Peters20dab9f2001-09-04 05:31:47 +00004136}
4137
4138static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004139long_mod(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00004140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 PyLongObject *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00004142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004143 CHECK_BINOP(a, b);
4144
Yury Selivanove0b23092016-02-11 10:26:27 -05004145 if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) {
4146 return fast_mod((PyLongObject*)a, (PyLongObject*)b);
4147 }
4148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
4150 mod = NULL;
4151 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00004152}
4153
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004154static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004155long_divmod(PyObject *a, PyObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 PyLongObject *div, *mod;
4158 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00004159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00004161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
4163 return NULL;
4164 }
4165 z = PyTuple_New(2);
4166 if (z != NULL) {
Sergey Fedoseevea6207d2019-02-21 15:01:11 +05004167 PyTuple_SET_ITEM(z, 0, (PyObject *) div);
4168 PyTuple_SET_ITEM(z, 1, (PyObject *) mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 }
4170 else {
4171 Py_DECREF(div);
4172 Py_DECREF(mod);
4173 }
4174 return z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004175}
4176
Mark Dickinsonc5299672019-06-02 10:24:06 +01004177
4178/* Compute an inverse to a modulo n, or raise ValueError if a is not
4179 invertible modulo n. Assumes n is positive. The inverse returned
4180 is whatever falls out of the extended Euclidean algorithm: it may
4181 be either positive or negative, but will be smaller than n in
4182 absolute value.
4183
4184 Pure Python equivalent for long_invmod:
4185
4186 def invmod(a, n):
4187 b, c = 1, 0
4188 while n:
4189 q, r = divmod(a, n)
4190 a, b, c, n = n, c, b - q*c, r
4191
4192 # at this point a is the gcd of the original inputs
4193 if a == 1:
4194 return b
4195 raise ValueError("Not invertible")
4196*/
4197
4198static PyLongObject *
4199long_invmod(PyLongObject *a, PyLongObject *n)
4200{
4201 PyLongObject *b, *c;
4202
4203 /* Should only ever be called for positive n */
4204 assert(Py_SIZE(n) > 0);
4205
4206 b = (PyLongObject *)PyLong_FromLong(1L);
4207 if (b == NULL) {
4208 return NULL;
4209 }
4210 c = (PyLongObject *)PyLong_FromLong(0L);
4211 if (c == NULL) {
4212 Py_DECREF(b);
4213 return NULL;
4214 }
4215 Py_INCREF(a);
4216 Py_INCREF(n);
4217
4218 /* references now owned: a, b, c, n */
4219 while (Py_SIZE(n) != 0) {
4220 PyLongObject *q, *r, *s, *t;
4221
4222 if (l_divmod(a, n, &q, &r) == -1) {
4223 goto Error;
4224 }
4225 Py_DECREF(a);
4226 a = n;
4227 n = r;
4228 t = (PyLongObject *)long_mul(q, c);
4229 Py_DECREF(q);
4230 if (t == NULL) {
4231 goto Error;
4232 }
4233 s = (PyLongObject *)long_sub(b, t);
4234 Py_DECREF(t);
4235 if (s == NULL) {
4236 goto Error;
4237 }
4238 Py_DECREF(b);
4239 b = c;
4240 c = s;
4241 }
4242 /* references now owned: a, b, c, n */
4243
4244 Py_DECREF(c);
4245 Py_DECREF(n);
Petr Viktorin1e375c62019-06-03 02:28:29 +02004246 if (long_compare(a, (PyLongObject *)_PyLong_One)) {
Mark Dickinsonc5299672019-06-02 10:24:06 +01004247 /* a != 1; we don't have an inverse. */
4248 Py_DECREF(a);
4249 Py_DECREF(b);
4250 PyErr_SetString(PyExc_ValueError,
4251 "base is not invertible for the given modulus");
4252 return NULL;
4253 }
4254 else {
4255 /* a == 1; b gives an inverse modulo n */
4256 Py_DECREF(a);
4257 return b;
4258 }
4259
4260 Error:
4261 Py_DECREF(a);
4262 Py_DECREF(b);
4263 Py_DECREF(c);
4264 Py_DECREF(n);
4265 return NULL;
4266}
4267
4268
Tim Peters47e52ee2004-08-30 02:44:38 +00004269/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004270static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00004271long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
4274 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00004275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 PyLongObject *z = NULL; /* accumulated result */
4277 Py_ssize_t i, j, k; /* counters */
4278 PyLongObject *temp = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00004279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 /* 5-ary values. If the exponent is large enough, table is
4281 * precomputed so that table[i] == a**i % c for i in range(32).
4282 */
4283 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4284 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Tim Peters47e52ee2004-08-30 02:44:38 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 /* a, b, c = v, w, x */
4287 CHECK_BINOP(v, w);
4288 a = (PyLongObject*)v; Py_INCREF(a);
4289 b = (PyLongObject*)w; Py_INCREF(b);
4290 if (PyLong_Check(x)) {
4291 c = (PyLongObject *)x;
4292 Py_INCREF(x);
4293 }
4294 else if (x == Py_None)
4295 c = NULL;
4296 else {
4297 Py_DECREF(a);
4298 Py_DECREF(b);
Brian Curtindfc80e32011-08-10 20:28:54 -05004299 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 }
Tim Peters4c483c42001-09-05 06:24:58 +00004301
Mark Dickinsonc5299672019-06-02 10:24:06 +01004302 if (Py_SIZE(b) < 0 && c == NULL) {
4303 /* if exponent is negative and there's no modulus:
4304 return a float. This works because we know
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 that this calls float_pow() which converts its
4306 arguments to double. */
Mark Dickinsonc5299672019-06-02 10:24:06 +01004307 Py_DECREF(a);
4308 Py_DECREF(b);
4309 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 if (c) {
4313 /* if modulus == 0:
4314 raise ValueError() */
4315 if (Py_SIZE(c) == 0) {
4316 PyErr_SetString(PyExc_ValueError,
4317 "pow() 3rd argument cannot be 0");
4318 goto Error;
4319 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 /* if modulus < 0:
4322 negativeOutput = True
4323 modulus = -modulus */
4324 if (Py_SIZE(c) < 0) {
4325 negativeOutput = 1;
4326 temp = (PyLongObject *)_PyLong_Copy(c);
4327 if (temp == NULL)
4328 goto Error;
4329 Py_DECREF(c);
4330 c = temp;
4331 temp = NULL;
Victor Stinner8aed6f12013-07-17 22:31:17 +02004332 _PyLong_Negate(&c);
4333 if (c == NULL)
4334 goto Error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 /* if modulus == 1:
4338 return 0 */
4339 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
4340 z = (PyLongObject *)PyLong_FromLong(0L);
4341 goto Done;
4342 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004343
Mark Dickinsonc5299672019-06-02 10:24:06 +01004344 /* if exponent is negative, negate the exponent and
4345 replace the base with a modular inverse */
4346 if (Py_SIZE(b) < 0) {
4347 temp = (PyLongObject *)_PyLong_Copy(b);
4348 if (temp == NULL)
4349 goto Error;
4350 Py_DECREF(b);
4351 b = temp;
4352 temp = NULL;
4353 _PyLong_Negate(&b);
4354 if (b == NULL)
4355 goto Error;
4356
4357 temp = long_invmod(a, c);
4358 if (temp == NULL)
4359 goto Error;
4360 Py_DECREF(a);
4361 a = temp;
4362 }
4363
Tim Peters81a93152013-10-05 16:53:52 -05004364 /* Reduce base by modulus in some cases:
4365 1. If base < 0. Forcing the base non-negative makes things easier.
4366 2. If base is obviously larger than the modulus. The "small
4367 exponent" case later can multiply directly by base repeatedly,
4368 while the "large exponent" case multiplies directly by base 31
4369 times. It can be unboundedly faster to multiply by
4370 base % modulus instead.
4371 We could _always_ do this reduction, but l_divmod() isn't cheap,
4372 so we only do it when it buys something. */
4373 if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 if (l_divmod(a, c, NULL, &temp) < 0)
4375 goto Error;
4376 Py_DECREF(a);
4377 a = temp;
4378 temp = NULL;
4379 }
4380 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 /* At this point a, b, and c are guaranteed non-negative UNLESS
4383 c is NULL, in which case a may be negative. */
Tim Peters47e52ee2004-08-30 02:44:38 +00004384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 z = (PyLongObject *)PyLong_FromLong(1L);
4386 if (z == NULL)
4387 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 /* Perform a modular reduction, X = X % c, but leave X alone if c
4390 * is NULL.
4391 */
4392#define REDUCE(X) \
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004393 do { \
4394 if (c != NULL) { \
4395 if (l_divmod(X, c, NULL, &temp) < 0) \
4396 goto Error; \
4397 Py_XDECREF(X); \
4398 X = temp; \
4399 temp = NULL; \
4400 } \
4401 } while(0)
Tim Peters47e52ee2004-08-30 02:44:38 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 /* Multiply two values, then reduce the result:
4404 result = X*Y % c. If c is NULL, skip the mod. */
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004405#define MULT(X, Y, result) \
4406 do { \
4407 temp = (PyLongObject *)long_mul(X, Y); \
4408 if (temp == NULL) \
4409 goto Error; \
4410 Py_XDECREF(result); \
4411 result = temp; \
4412 temp = NULL; \
4413 REDUCE(result); \
4414 } while(0)
Tim Peters47e52ee2004-08-30 02:44:38 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
4417 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
4418 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
4419 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
4420 digit bi = b->ob_digit[i];
Tim Peters47e52ee2004-08-30 02:44:38 +00004421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004423 MULT(z, z, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 if (bi & j)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004425 MULT(z, a, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 }
4427 }
4428 }
4429 else {
4430 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
4431 Py_INCREF(z); /* still holds 1L */
4432 table[0] = z;
4433 for (i = 1; i < 32; ++i)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004434 MULT(table[i-1], a, table[i]);
Tim Peters47e52ee2004-08-30 02:44:38 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
4437 const digit bi = b->ob_digit[i];
Tim Peters47e52ee2004-08-30 02:44:38 +00004438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
4440 const int index = (bi >> j) & 0x1f;
4441 for (k = 0; k < 5; ++k)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004442 MULT(z, z, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004443 if (index)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004444 MULT(z, table[index], z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 }
4446 }
4447 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 if (negativeOutput && (Py_SIZE(z) != 0)) {
4450 temp = (PyLongObject *)long_sub(z, c);
4451 if (temp == NULL)
4452 goto Error;
4453 Py_DECREF(z);
4454 z = temp;
4455 temp = NULL;
4456 }
4457 goto Done;
Tim Peters47e52ee2004-08-30 02:44:38 +00004458
Mark Dickinson22b20182010-05-10 21:27:53 +00004459 Error:
Victor Stinner8aed6f12013-07-17 22:31:17 +02004460 Py_CLEAR(z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 /* fall through */
Mark Dickinson22b20182010-05-10 21:27:53 +00004462 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
4464 for (i = 0; i < 32; ++i)
4465 Py_XDECREF(table[i]);
4466 }
4467 Py_DECREF(a);
4468 Py_DECREF(b);
4469 Py_XDECREF(c);
4470 Py_XDECREF(temp);
4471 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004472}
4473
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004474static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004475long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 /* Implement ~x as -(x+1) */
4478 PyLongObject *x;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004479 if (Py_ABS(Py_SIZE(v)) <=1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
Serhiy Storchakaba85d692017-03-30 09:09:41 +03004481 x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 if (x == NULL)
4483 return NULL;
Mark Dickinson583c6e82016-08-29 16:40:29 +01004484 _PyLong_Negate(&x);
4485 /* No need for maybe_small_long here, since any small
4486 longs will have been caught in the Py_SIZE <= 1 fast path. */
4487 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004488}
4489
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004490static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004491long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 PyLongObject *z;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004494 if (Py_ABS(Py_SIZE(v)) <= 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 return PyLong_FromLong(-MEDIUM_VALUE(v));
4496 z = (PyLongObject *)_PyLong_Copy(v);
4497 if (z != NULL)
4498 Py_SIZE(z) = -(Py_SIZE(v));
4499 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004500}
4501
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004502static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004503long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 if (Py_SIZE(v) < 0)
4506 return long_neg(v);
4507 else
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03004508 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004509}
4510
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004511static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004512long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 return Py_SIZE(v) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004515}
4516
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004517/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
4518static int
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004519divmod_shift(PyObject *shiftby, Py_ssize_t *wordshift, digit *remshift)
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004520{
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004521 assert(PyLong_Check(shiftby));
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004522 assert(Py_SIZE(shiftby) >= 0);
4523 Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby);
4524 if (lshiftby >= 0) {
4525 *wordshift = lshiftby / PyLong_SHIFT;
4526 *remshift = lshiftby % PyLong_SHIFT;
4527 return 0;
4528 }
4529 /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must
4530 be that PyLong_AsSsize_t raised an OverflowError. */
4531 assert(PyErr_ExceptionMatches(PyExc_OverflowError));
4532 PyErr_Clear();
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004533 PyLongObject *wordshift_obj = divrem1((PyLongObject *)shiftby, PyLong_SHIFT, remshift);
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004534 if (wordshift_obj == NULL) {
4535 return -1;
4536 }
4537 *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj);
4538 Py_DECREF(wordshift_obj);
4539 if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) {
4540 return 0;
4541 }
4542 PyErr_Clear();
4543 /* Clip the value. With such large wordshift the right shift
4544 returns 0 and the left shift raises an error in _PyLong_New(). */
4545 *wordshift = PY_SSIZE_T_MAX / sizeof(digit);
4546 *remshift = 0;
4547 return 0;
4548}
4549
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004550static PyObject *
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004551long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyLongObject *z = NULL;
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004554 Py_ssize_t newsize, hishift, i, j;
4555 digit lomask, himask;
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 if (Py_SIZE(a) < 0) {
4558 /* Right shifting negative numbers is harder */
4559 PyLongObject *a1, *a2;
4560 a1 = (PyLongObject *) long_invert(a);
4561 if (a1 == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004562 return NULL;
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004563 a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 Py_DECREF(a1);
4565 if (a2 == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004566 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 z = (PyLongObject *) long_invert(a2);
4568 Py_DECREF(a2);
4569 }
4570 else {
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004571 newsize = Py_SIZE(a) - wordshift;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 if (newsize <= 0)
4573 return PyLong_FromLong(0);
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004574 hishift = PyLong_SHIFT - remshift;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 lomask = ((digit)1 << hishift) - 1;
4576 himask = PyLong_MASK ^ lomask;
4577 z = _PyLong_New(newsize);
4578 if (z == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004579 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 for (i = 0, j = wordshift; i < newsize; i++, j++) {
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004581 z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 if (i+1 < newsize)
Mark Dickinson22b20182010-05-10 21:27:53 +00004583 z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
Mark Dickinson92ca5352016-09-17 17:50:50 +01004585 z = maybe_small_long(long_normalize(z));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 }
Mark Dickinson92ca5352016-09-17 17:50:50 +01004587 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004588}
4589
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004590static PyObject *
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004591long_rshift(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004592{
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004593 Py_ssize_t wordshift;
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004594 digit remshift;
Tim Peters5af4e6c2002-08-12 02:31:19 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00004597
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004598 if (Py_SIZE(b) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 PyErr_SetString(PyExc_ValueError, "negative shift count");
Victor Stinner8aed6f12013-07-17 22:31:17 +02004600 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 }
Mark Dickinson82a95272016-08-29 19:27:06 +01004602 if (Py_SIZE(a) == 0) {
4603 return PyLong_FromLong(0);
4604 }
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004605 if (divmod_shift(b, &wordshift, &remshift) < 0)
4606 return NULL;
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004607 return long_rshift1((PyLongObject *)a, wordshift, remshift);
4608}
4609
4610/* Return a >> shiftby. */
4611PyObject *
4612_PyLong_Rshift(PyObject *a, size_t shiftby)
4613{
4614 Py_ssize_t wordshift;
4615 digit remshift;
4616
4617 assert(PyLong_Check(a));
4618 if (Py_SIZE(a) == 0) {
4619 return PyLong_FromLong(0);
4620 }
4621 wordshift = shiftby / PyLong_SHIFT;
4622 remshift = shiftby % PyLong_SHIFT;
4623 return long_rshift1((PyLongObject *)a, wordshift, remshift);
4624}
4625
4626static PyObject *
4627long_lshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift)
4628{
4629 /* This version due to Tim Peters */
4630 PyLongObject *z = NULL;
4631 Py_ssize_t oldsize, newsize, i, j;
4632 twodigits accum;
4633
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004634 oldsize = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 newsize = oldsize + wordshift;
4636 if (remshift)
4637 ++newsize;
4638 z = _PyLong_New(newsize);
4639 if (z == NULL)
Victor Stinner8aed6f12013-07-17 22:31:17 +02004640 return NULL;
4641 if (Py_SIZE(a) < 0) {
4642 assert(Py_REFCNT(z) == 1);
4643 Py_SIZE(z) = -Py_SIZE(z);
4644 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 for (i = 0; i < wordshift; i++)
4646 z->ob_digit[i] = 0;
4647 accum = 0;
4648 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
4649 accum |= (twodigits)a->ob_digit[j] << remshift;
4650 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
4651 accum >>= PyLong_SHIFT;
4652 }
4653 if (remshift)
4654 z->ob_digit[newsize-1] = (digit)accum;
4655 else
4656 assert(!accum);
4657 z = long_normalize(z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 return (PyObject *) maybe_small_long(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00004659}
4660
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03004661static PyObject *
4662long_lshift(PyObject *a, PyObject *b)
4663{
4664 Py_ssize_t wordshift;
4665 digit remshift;
4666
4667 CHECK_BINOP(a, b);
4668
4669 if (Py_SIZE(b) < 0) {
4670 PyErr_SetString(PyExc_ValueError, "negative shift count");
4671 return NULL;
4672 }
4673 if (Py_SIZE(a) == 0) {
4674 return PyLong_FromLong(0);
4675 }
4676 if (divmod_shift(b, &wordshift, &remshift) < 0)
4677 return NULL;
4678 return long_lshift1((PyLongObject *)a, wordshift, remshift);
4679}
4680
4681/* Return a << shiftby. */
4682PyObject *
4683_PyLong_Lshift(PyObject *a, size_t shiftby)
4684{
4685 Py_ssize_t wordshift;
4686 digit remshift;
4687
4688 assert(PyLong_Check(a));
4689 if (Py_SIZE(a) == 0) {
4690 return PyLong_FromLong(0);
4691 }
4692 wordshift = shiftby / PyLong_SHIFT;
4693 remshift = shiftby % PyLong_SHIFT;
4694 return long_lshift1((PyLongObject *)a, wordshift, remshift);
4695}
4696
Mark Dickinson27a87a22009-10-25 20:43:34 +00004697/* Compute two's complement of digit vector a[0:m], writing result to
4698 z[0:m]. The digit vector a need not be normalized, but should not
4699 be entirely zero. a and z may point to the same digit vector. */
4700
4701static void
4702v_complement(digit *z, digit *a, Py_ssize_t m)
4703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 Py_ssize_t i;
4705 digit carry = 1;
4706 for (i = 0; i < m; ++i) {
4707 carry += a[i] ^ PyLong_MASK;
4708 z[i] = carry & PyLong_MASK;
4709 carry >>= PyLong_SHIFT;
4710 }
4711 assert(carry == 0);
Mark Dickinson27a87a22009-10-25 20:43:34 +00004712}
Guido van Rossum4c260ff1992-01-14 18:36:43 +00004713
4714/* Bitwise and/xor/or operations */
4715
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004716static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004717long_bitwise(PyLongObject *a,
Benjamin Peterson513762f2012-12-26 16:43:33 -06004718 char op, /* '&', '|', '^' */
Mark Dickinson22b20182010-05-10 21:27:53 +00004719 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 int nega, negb, negz;
4722 Py_ssize_t size_a, size_b, size_z, i;
4723 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00004724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 /* Bitwise operations for negative numbers operate as though
4726 on a two's complement representation. So convert arguments
4727 from sign-magnitude to two's complement, and convert the
4728 result back to sign-magnitude at the end. */
Mark Dickinson27a87a22009-10-25 20:43:34 +00004729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 /* If a is negative, replace it by its two's complement. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004731 size_a = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 nega = Py_SIZE(a) < 0;
4733 if (nega) {
4734 z = _PyLong_New(size_a);
4735 if (z == NULL)
4736 return NULL;
4737 v_complement(z->ob_digit, a->ob_digit, size_a);
4738 a = z;
4739 }
4740 else
4741 /* Keep reference count consistent. */
4742 Py_INCREF(a);
Mark Dickinson27a87a22009-10-25 20:43:34 +00004743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 /* Same for b. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004745 size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 negb = Py_SIZE(b) < 0;
4747 if (negb) {
4748 z = _PyLong_New(size_b);
4749 if (z == NULL) {
4750 Py_DECREF(a);
4751 return NULL;
4752 }
4753 v_complement(z->ob_digit, b->ob_digit, size_b);
4754 b = z;
4755 }
4756 else
4757 Py_INCREF(b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00004758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 /* Swap a and b if necessary to ensure size_a >= size_b. */
4760 if (size_a < size_b) {
4761 z = a; a = b; b = z;
4762 size_z = size_a; size_a = size_b; size_b = size_z;
4763 negz = nega; nega = negb; negb = negz;
4764 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 /* JRH: The original logic here was to allocate the result value (z)
4767 as the longer of the two operands. However, there are some cases
4768 where the result is guaranteed to be shorter than that: AND of two
4769 positives, OR of two negatives: use the shorter number. AND with
4770 mixed signs: use the positive number. OR with mixed signs: use the
4771 negative number.
4772 */
4773 switch (op) {
4774 case '^':
4775 negz = nega ^ negb;
4776 size_z = size_a;
4777 break;
4778 case '&':
4779 negz = nega & negb;
4780 size_z = negb ? size_a : size_b;
4781 break;
4782 case '|':
4783 negz = nega | negb;
4784 size_z = negb ? size_b : size_a;
4785 break;
4786 default:
stratakisa10d4262019-03-18 18:59:20 +01004787 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00004789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 /* We allow an extra digit if z is negative, to make sure that
4791 the final two's complement of z doesn't overflow. */
4792 z = _PyLong_New(size_z + negz);
4793 if (z == NULL) {
4794 Py_DECREF(a);
4795 Py_DECREF(b);
4796 return NULL;
4797 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 /* Compute digits for overlap of a and b. */
4800 switch(op) {
4801 case '&':
4802 for (i = 0; i < size_b; ++i)
4803 z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i];
4804 break;
4805 case '|':
4806 for (i = 0; i < size_b; ++i)
4807 z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i];
4808 break;
4809 case '^':
4810 for (i = 0; i < size_b; ++i)
4811 z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i];
4812 break;
4813 default:
stratakisa10d4262019-03-18 18:59:20 +01004814 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 }
Mark Dickinson27a87a22009-10-25 20:43:34 +00004816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 /* Copy any remaining digits of a, inverting if necessary. */
4818 if (op == '^' && negb)
4819 for (; i < size_z; ++i)
4820 z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK;
4821 else if (i < size_z)
4822 memcpy(&z->ob_digit[i], &a->ob_digit[i],
4823 (size_z-i)*sizeof(digit));
Mark Dickinson27a87a22009-10-25 20:43:34 +00004824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 /* Complement result if negative. */
4826 if (negz) {
4827 Py_SIZE(z) = -(Py_SIZE(z));
4828 z->ob_digit[size_z] = PyLong_MASK;
4829 v_complement(z->ob_digit, z->ob_digit, size_z+1);
4830 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 Py_DECREF(a);
4833 Py_DECREF(b);
4834 return (PyObject *)maybe_small_long(long_normalize(z));
Guido van Rossumc6913e71991-11-19 20:26:46 +00004835}
4836
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004837static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004838long_and(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004840 PyObject *c;
4841 CHECK_BINOP(a, b);
4842 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
4843 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004844}
4845
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004846static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004847long_xor(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004849 PyObject *c;
4850 CHECK_BINOP(a, b);
4851 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
4852 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004853}
4854
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004855static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004856long_or(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 PyObject *c;
4859 CHECK_BINOP(a, b);
4860 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
4861 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004862}
4863
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004864static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03004865long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 if (PyLong_CheckExact(v))
4868 Py_INCREF(v);
4869 else
4870 v = _PyLong_Copy((PyLongObject *)v);
4871 return v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004872}
4873
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004874PyObject *
4875_PyLong_GCD(PyObject *aarg, PyObject *barg)
4876{
4877 PyLongObject *a, *b, *c = NULL, *d = NULL, *r;
4878 stwodigits x, y, q, s, t, c_carry, d_carry;
4879 stwodigits A, B, C, D, T;
4880 int nbits, k;
4881 Py_ssize_t size_a, size_b, alloc_a, alloc_b;
4882 digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end;
4883
4884 a = (PyLongObject *)aarg;
4885 b = (PyLongObject *)barg;
4886 size_a = Py_SIZE(a);
4887 size_b = Py_SIZE(b);
4888 if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) {
4889 Py_INCREF(a);
4890 Py_INCREF(b);
4891 goto simple;
4892 }
4893
4894 /* Initial reduction: make sure that 0 <= b <= a. */
4895 a = (PyLongObject *)long_abs(a);
4896 if (a == NULL)
4897 return NULL;
4898 b = (PyLongObject *)long_abs(b);
4899 if (b == NULL) {
4900 Py_DECREF(a);
4901 return NULL;
4902 }
4903 if (long_compare(a, b) < 0) {
4904 r = a;
4905 a = b;
4906 b = r;
4907 }
4908 /* We now own references to a and b */
4909
4910 alloc_a = Py_SIZE(a);
4911 alloc_b = Py_SIZE(b);
4912 /* reduce until a fits into 2 digits */
4913 while ((size_a = Py_SIZE(a)) > 2) {
4914 nbits = bits_in_digit(a->ob_digit[size_a-1]);
4915 /* extract top 2*PyLong_SHIFT bits of a into x, along with
4916 corresponding bits of b into y */
4917 size_b = Py_SIZE(b);
4918 assert(size_b <= size_a);
4919 if (size_b == 0) {
4920 if (size_a < alloc_a) {
4921 r = (PyLongObject *)_PyLong_Copy(a);
4922 Py_DECREF(a);
4923 }
4924 else
4925 r = a;
4926 Py_DECREF(b);
4927 Py_XDECREF(c);
4928 Py_XDECREF(d);
4929 return (PyObject *)r;
4930 }
4931 x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) |
4932 ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) |
4933 (a->ob_digit[size_a-3] >> nbits));
4934
4935 y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) |
4936 (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) |
4937 (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0));
4938
4939 /* inner loop of Lehmer's algorithm; A, B, C, D never grow
4940 larger than PyLong_MASK during the algorithm. */
4941 A = 1; B = 0; C = 0; D = 1;
4942 for (k=0;; k++) {
4943 if (y-C == 0)
4944 break;
4945 q = (x+(A-1))/(y-C);
4946 s = B+q*D;
4947 t = x-q*y;
4948 if (s > t)
4949 break;
4950 x = y; y = t;
4951 t = A+q*C; A = D; B = C; C = s; D = t;
4952 }
4953
4954 if (k == 0) {
4955 /* no progress; do a Euclidean step */
4956 if (l_divmod(a, b, NULL, &r) < 0)
4957 goto error;
4958 Py_DECREF(a);
4959 a = b;
4960 b = r;
4961 alloc_a = alloc_b;
4962 alloc_b = Py_SIZE(b);
4963 continue;
4964 }
4965
4966 /*
4967 a, b = A*b-B*a, D*a-C*b if k is odd
4968 a, b = A*a-B*b, D*b-C*a if k is even
4969 */
4970 if (k&1) {
4971 T = -A; A = -B; B = T;
4972 T = -C; C = -D; D = T;
4973 }
4974 if (c != NULL)
4975 Py_SIZE(c) = size_a;
4976 else if (Py_REFCNT(a) == 1) {
4977 Py_INCREF(a);
4978 c = a;
4979 }
4980 else {
4981 alloc_a = size_a;
4982 c = _PyLong_New(size_a);
4983 if (c == NULL)
4984 goto error;
4985 }
4986
4987 if (d != NULL)
4988 Py_SIZE(d) = size_a;
4989 else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
4990 Py_INCREF(b);
4991 d = b;
4992 Py_SIZE(d) = size_a;
4993 }
4994 else {
4995 alloc_b = size_a;
4996 d = _PyLong_New(size_a);
4997 if (d == NULL)
4998 goto error;
4999 }
5000 a_end = a->ob_digit + size_a;
5001 b_end = b->ob_digit + size_b;
5002
5003 /* compute new a and new b in parallel */
5004 a_digit = a->ob_digit;
5005 b_digit = b->ob_digit;
5006 c_digit = c->ob_digit;
5007 d_digit = d->ob_digit;
5008 c_carry = 0;
5009 d_carry = 0;
5010 while (b_digit < b_end) {
5011 c_carry += (A * *a_digit) - (B * *b_digit);
5012 d_carry += (D * *b_digit++) - (C * *a_digit++);
5013 *c_digit++ = (digit)(c_carry & PyLong_MASK);
5014 *d_digit++ = (digit)(d_carry & PyLong_MASK);
5015 c_carry >>= PyLong_SHIFT;
5016 d_carry >>= PyLong_SHIFT;
5017 }
5018 while (a_digit < a_end) {
5019 c_carry += A * *a_digit;
5020 d_carry -= C * *a_digit++;
5021 *c_digit++ = (digit)(c_carry & PyLong_MASK);
5022 *d_digit++ = (digit)(d_carry & PyLong_MASK);
5023 c_carry >>= PyLong_SHIFT;
5024 d_carry >>= PyLong_SHIFT;
5025 }
5026 assert(c_carry == 0);
5027 assert(d_carry == 0);
5028
5029 Py_INCREF(c);
5030 Py_INCREF(d);
5031 Py_DECREF(a);
5032 Py_DECREF(b);
5033 a = long_normalize(c);
5034 b = long_normalize(d);
5035 }
5036 Py_XDECREF(c);
5037 Py_XDECREF(d);
5038
5039simple:
5040 assert(Py_REFCNT(a) > 0);
5041 assert(Py_REFCNT(b) > 0);
Victor Stinner5783fd22015-09-19 13:39:03 +02005042/* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid
5043 undefined behaviour when LONG_MAX type is smaller than 60 bits */
5044#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03005045 /* a fits into a long, so b must too */
5046 x = PyLong_AsLong((PyObject *)a);
5047 y = PyLong_AsLong((PyObject *)b);
Benjamin Petersond953f8e2016-09-06 10:51:19 -07005048#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03005049 x = PyLong_AsLongLong((PyObject *)a);
5050 y = PyLong_AsLongLong((PyObject *)b);
5051#else
5052# error "_PyLong_GCD"
5053#endif
5054 x = Py_ABS(x);
5055 y = Py_ABS(y);
5056 Py_DECREF(a);
5057 Py_DECREF(b);
5058
5059 /* usual Euclidean algorithm for longs */
5060 while (y != 0) {
5061 t = y;
5062 y = x % y;
5063 x = t;
5064 }
Victor Stinner5783fd22015-09-19 13:39:03 +02005065#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03005066 return PyLong_FromLong(x);
Benjamin Petersond953f8e2016-09-06 10:51:19 -07005067#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03005068 return PyLong_FromLongLong(x);
5069#else
5070# error "_PyLong_GCD"
5071#endif
5072
5073error:
5074 Py_DECREF(a);
5075 Py_DECREF(b);
5076 Py_XDECREF(c);
5077 Py_XDECREF(d);
5078 return NULL;
5079}
5080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005081static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00005082long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00005083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 double result;
5085 result = PyLong_AsDouble(v);
5086 if (result == -1.0 && PyErr_Occurred())
5087 return NULL;
5088 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00005089}
5090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005091static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005092long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase);
5093
5094/*[clinic input]
5095@classmethod
5096int.__new__ as long_new
5097 x: object(c_default="NULL") = 0
5098 /
5099 base as obase: object(c_default="NULL") = 10
5100[clinic start generated code]*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +00005101
Tim Peters6d6c1a32001-08-02 04:15:00 +00005102static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005103long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase)
5104/*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00005105{
Gregory P. Smitha689e522012-12-25 22:38:32 -08005106 Py_ssize_t base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00005107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 if (type != &PyLong_Type)
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005109 return long_subtype_new(type, x, obase); /* Wimp out */
Serhiy Storchaka0b386d52012-12-28 09:42:11 +02005110 if (x == NULL) {
5111 if (obase != NULL) {
5112 PyErr_SetString(PyExc_TypeError,
5113 "int() missing string argument");
5114 return NULL;
5115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 return PyLong_FromLong(0L);
Serhiy Storchaka0b386d52012-12-28 09:42:11 +02005117 }
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00005118 if (obase == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005119 return PyNumber_Long(x);
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00005120
Gregory P. Smitha689e522012-12-25 22:38:32 -08005121 base = PyNumber_AsSsize_t(obase, NULL);
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00005122 if (base == -1 && PyErr_Occurred())
5123 return NULL;
Gregory P. Smitha689e522012-12-25 22:38:32 -08005124 if ((base != 0 && base < 2) || base > 36) {
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00005125 PyErr_SetString(PyExc_ValueError,
Sanyam Khurana28b62482017-11-14 03:19:26 +05305126 "int() base must be >= 2 and <= 36, or 0");
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00005127 return NULL;
5128 }
5129
5130 if (PyUnicode_Check(x))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005131 return PyLong_FromUnicodeObject(x, (int)base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 char *string;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 if (PyByteArray_Check(x))
5135 string = PyByteArray_AS_STRING(x);
5136 else
5137 string = PyBytes_AS_STRING(x);
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03005138 return _PyLong_FromBytes(string, Py_SIZE(x), (int)base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005139 }
5140 else {
5141 PyErr_SetString(PyExc_TypeError,
Mark Dickinson22b20182010-05-10 21:27:53 +00005142 "int() can't convert non-string with explicit base");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 return NULL;
5144 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00005145}
5146
Serhiy Storchaka95949422013-08-27 19:40:23 +03005147/* Wimpy, slow approach to tp_new calls for subtypes of int:
5148 first create a regular int from whatever arguments we got,
Guido van Rossumbef14172001-08-29 15:47:46 +00005149 then allocate a subtype instance and initialize it from
Serhiy Storchaka95949422013-08-27 19:40:23 +03005150 the regular int. The regular int is then thrown away.
Guido van Rossumbef14172001-08-29 15:47:46 +00005151*/
5152static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005153long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase)
Guido van Rossumbef14172001-08-29 15:47:46 +00005154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 PyLongObject *tmp, *newobj;
5156 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00005157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 assert(PyType_IsSubtype(type, &PyLong_Type));
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005159 tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 if (tmp == NULL)
5161 return NULL;
Serhiy Storchaka15095802015-11-25 15:47:01 +02005162 assert(PyLong_Check(tmp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 n = Py_SIZE(tmp);
5164 if (n < 0)
5165 n = -n;
5166 newobj = (PyLongObject *)type->tp_alloc(type, n);
5167 if (newobj == NULL) {
5168 Py_DECREF(tmp);
5169 return NULL;
5170 }
5171 assert(PyLong_Check(newobj));
5172 Py_SIZE(newobj) = Py_SIZE(tmp);
5173 for (i = 0; i < n; i++)
5174 newobj->ob_digit[i] = tmp->ob_digit[i];
5175 Py_DECREF(tmp);
5176 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00005177}
5178
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005179/*[clinic input]
5180int.__getnewargs__
5181[clinic start generated code]*/
5182
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005183static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005184int___getnewargs___impl(PyObject *self)
5185/*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005186{
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005187 return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self));
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005188}
5189
Guido van Rossumb43daf72007-08-01 18:08:08 +00005190static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005191long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context))
5192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005193 return PyLong_FromLong(0L);
Mark Dickinson6bf19002009-05-02 17:57:52 +00005194}
5195
5196static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005197long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored))
5198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 return PyLong_FromLong(1L);
Guido van Rossumb43daf72007-08-01 18:08:08 +00005200}
5201
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005202/*[clinic input]
5203int.__format__
5204
5205 format_spec: unicode
5206 /
5207[clinic start generated code]*/
5208
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005209static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005210int___format___impl(PyObject *self, PyObject *format_spec)
5211/*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/
Eric Smith8c663262007-08-25 02:26:07 +00005212{
Victor Stinnerd3f08822012-05-29 12:57:52 +02005213 _PyUnicodeWriter writer;
5214 int ret;
Eric Smith4a7d76d2008-05-30 18:10:19 +00005215
Victor Stinner8f674cc2013-04-17 23:02:17 +02005216 _PyUnicodeWriter_Init(&writer);
Victor Stinnerd3f08822012-05-29 12:57:52 +02005217 ret = _PyLong_FormatAdvancedWriter(
5218 &writer,
5219 self,
5220 format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
5221 if (ret == -1) {
5222 _PyUnicodeWriter_Dealloc(&writer);
5223 return NULL;
5224 }
5225 return _PyUnicodeWriter_Finish(&writer);
Eric Smith8c663262007-08-25 02:26:07 +00005226}
5227
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005228/* Return a pair (q, r) such that a = b * q + r, and
5229 abs(r) <= abs(b)/2, with equality possible only if q is even.
5230 In other words, q == a / b, rounded to the nearest integer using
5231 round-half-to-even. */
5232
5233PyObject *
Mark Dickinsonfa68a612010-06-07 18:47:09 +00005234_PyLong_DivmodNear(PyObject *a, PyObject *b)
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005235{
5236 PyLongObject *quo = NULL, *rem = NULL;
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005237 PyObject *twice_rem, *result, *temp;
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005238 int cmp, quo_is_odd, quo_is_neg;
5239
5240 /* Equivalent Python code:
5241
5242 def divmod_near(a, b):
5243 q, r = divmod(a, b)
5244 # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
5245 # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
5246 # positive, 2 * r < b if b negative.
5247 greater_than_half = 2*r > b if b > 0 else 2*r < b
5248 exactly_half = 2*r == b
5249 if greater_than_half or exactly_half and q % 2 == 1:
5250 q += 1
5251 r -= b
5252 return q, r
5253
5254 */
5255 if (!PyLong_Check(a) || !PyLong_Check(b)) {
5256 PyErr_SetString(PyExc_TypeError,
5257 "non-integer arguments in division");
5258 return NULL;
5259 }
5260
5261 /* Do a and b have different signs? If so, quotient is negative. */
5262 quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0);
5263
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005264 if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
5265 goto error;
5266
5267 /* compare twice the remainder with the divisor, to see
5268 if we need to adjust the quotient and remainder */
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005269 twice_rem = long_lshift((PyObject *)rem, _PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005270 if (twice_rem == NULL)
5271 goto error;
5272 if (quo_is_neg) {
5273 temp = long_neg((PyLongObject*)twice_rem);
5274 Py_DECREF(twice_rem);
5275 twice_rem = temp;
5276 if (twice_rem == NULL)
5277 goto error;
5278 }
5279 cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b);
5280 Py_DECREF(twice_rem);
5281
5282 quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0);
5283 if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
5284 /* fix up quotient */
5285 if (quo_is_neg)
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005286 temp = long_sub(quo, (PyLongObject *)_PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005287 else
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005288 temp = long_add(quo, (PyLongObject *)_PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005289 Py_DECREF(quo);
5290 quo = (PyLongObject *)temp;
5291 if (quo == NULL)
5292 goto error;
5293 /* and remainder */
5294 if (quo_is_neg)
5295 temp = long_add(rem, (PyLongObject *)b);
5296 else
5297 temp = long_sub(rem, (PyLongObject *)b);
5298 Py_DECREF(rem);
5299 rem = (PyLongObject *)temp;
5300 if (rem == NULL)
5301 goto error;
5302 }
5303
5304 result = PyTuple_New(2);
5305 if (result == NULL)
5306 goto error;
5307
5308 /* PyTuple_SET_ITEM steals references */
5309 PyTuple_SET_ITEM(result, 0, (PyObject *)quo);
5310 PyTuple_SET_ITEM(result, 1, (PyObject *)rem);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005311 return result;
5312
5313 error:
5314 Py_XDECREF(quo);
5315 Py_XDECREF(rem);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005316 return NULL;
5317}
5318
Eric Smith8c663262007-08-25 02:26:07 +00005319static PyObject *
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005320long_round(PyObject *self, PyObject *args)
5321{
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005322 PyObject *o_ndigits=NULL, *temp, *result, *ndigits;
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005323
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005324 /* To round an integer m to the nearest 10**n (n positive), we make use of
5325 * the divmod_near operation, defined by:
5326 *
5327 * divmod_near(a, b) = (q, r)
5328 *
5329 * where q is the nearest integer to the quotient a / b (the
5330 * nearest even integer in the case of a tie) and r == a - q * b.
5331 * Hence q * b = a - r is the nearest multiple of b to a,
5332 * preferring even multiples in the case of a tie.
5333 *
5334 * So the nearest multiple of 10**n to m is:
5335 *
5336 * m - divmod_near(m, 10**n)[1].
5337 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
5339 return NULL;
5340 if (o_ndigits == NULL)
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005341 return long_long(self);
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005342
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005343 ndigits = PyNumber_Index(o_ndigits);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 if (ndigits == NULL)
5345 return NULL;
Mark Dickinson1124e712009-01-28 21:25:58 +00005346
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005347 /* if ndigits >= 0 then no rounding is necessary; return self unchanged */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005348 if (Py_SIZE(ndigits) >= 0) {
5349 Py_DECREF(ndigits);
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005350 return long_long(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 }
Mark Dickinson1124e712009-01-28 21:25:58 +00005352
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005353 /* result = self - divmod_near(self, 10 ** -ndigits)[1] */
5354 temp = long_neg((PyLongObject*)ndigits);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005355 Py_DECREF(ndigits);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005356 ndigits = temp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 if (ndigits == NULL)
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005358 return NULL;
Mark Dickinson1124e712009-01-28 21:25:58 +00005359
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005360 result = PyLong_FromLong(10L);
5361 if (result == NULL) {
5362 Py_DECREF(ndigits);
5363 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 }
Mark Dickinson1124e712009-01-28 21:25:58 +00005365
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005366 temp = long_pow(result, ndigits, Py_None);
5367 Py_DECREF(ndigits);
5368 Py_DECREF(result);
5369 result = temp;
5370 if (result == NULL)
5371 return NULL;
5372
Mark Dickinsonfa68a612010-06-07 18:47:09 +00005373 temp = _PyLong_DivmodNear(self, result);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005374 Py_DECREF(result);
5375 result = temp;
5376 if (result == NULL)
5377 return NULL;
5378
5379 temp = long_sub((PyLongObject *)self,
5380 (PyLongObject *)PyTuple_GET_ITEM(result, 1));
5381 Py_DECREF(result);
5382 result = temp;
5383
5384 return result;
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005385}
5386
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005387/*[clinic input]
5388int.__sizeof__ -> Py_ssize_t
5389
5390Returns size in memory, in bytes.
5391[clinic start generated code]*/
5392
5393static Py_ssize_t
5394int___sizeof___impl(PyObject *self)
5395/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005398
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005399 res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit);
5400 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005401}
5402
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005403/*[clinic input]
5404int.bit_length
5405
5406Number of bits necessary to represent self in binary.
5407
5408>>> bin(37)
5409'0b100101'
5410>>> (37).bit_length()
54116
5412[clinic start generated code]*/
5413
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005414static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005415int_bit_length_impl(PyObject *self)
5416/*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 PyLongObject *result, *x, *y;
Serhiy Storchakab2e64f92016-11-08 20:34:22 +02005419 Py_ssize_t ndigits;
5420 int msd_bits;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005421 digit msd;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005422
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005423 assert(self != NULL);
5424 assert(PyLong_Check(self));
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005425
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005426 ndigits = Py_ABS(Py_SIZE(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 if (ndigits == 0)
5428 return PyLong_FromLong(0);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005429
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005430 msd = ((PyLongObject *)self)->ob_digit[ndigits-1];
Serhiy Storchakab2e64f92016-11-08 20:34:22 +02005431 msd_bits = bits_in_digit(msd);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
5434 return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005436 /* expression above may overflow; use Python integers instead */
5437 result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
5438 if (result == NULL)
5439 return NULL;
5440 x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
5441 if (x == NULL)
5442 goto error;
5443 y = (PyLongObject *)long_mul(result, x);
5444 Py_DECREF(x);
5445 if (y == NULL)
5446 goto error;
5447 Py_DECREF(result);
5448 result = y;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
5451 if (x == NULL)
5452 goto error;
5453 y = (PyLongObject *)long_add(result, x);
5454 Py_DECREF(x);
5455 if (y == NULL)
5456 goto error;
5457 Py_DECREF(result);
5458 result = y;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005460 return (PyObject *)result;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005461
Mark Dickinson22b20182010-05-10 21:27:53 +00005462 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005463 Py_DECREF(result);
5464 return NULL;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005465}
5466
Christian Heimes53876d92008-04-19 00:31:39 +00005467#if 0
5468static PyObject *
5469long_is_finite(PyObject *v)
5470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 Py_RETURN_TRUE;
Christian Heimes53876d92008-04-19 00:31:39 +00005472}
5473#endif
5474
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005475/*[clinic input]
Lisa Roach5ac70432018-09-13 23:56:23 -07005476int.as_integer_ratio
5477
5478Return integer ratio.
5479
5480Return a pair of integers, whose ratio is exactly equal to the original int
5481and with a positive denominator.
5482
5483>>> (10).as_integer_ratio()
5484(10, 1)
5485>>> (-10).as_integer_ratio()
5486(-10, 1)
5487>>> (0).as_integer_ratio()
5488(0, 1)
5489[clinic start generated code]*/
5490
5491static PyObject *
5492int_as_integer_ratio_impl(PyObject *self)
5493/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
5494{
Raymond Hettinger00bc08e2018-09-14 01:00:11 -07005495 PyObject *ratio_tuple;
Serhiy Storchakab2e20252018-10-20 00:46:31 +03005496 PyObject *numerator = long_long(self);
Raymond Hettinger00bc08e2018-09-14 01:00:11 -07005497 if (numerator == NULL) {
5498 return NULL;
5499 }
5500 ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One);
5501 Py_DECREF(numerator);
5502 return ratio_tuple;
Lisa Roach5ac70432018-09-13 23:56:23 -07005503}
5504
5505/*[clinic input]
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005506int.to_bytes
5507
5508 length: Py_ssize_t
5509 Length of bytes object to use. An OverflowError is raised if the
5510 integer is not representable with the given number of bytes.
5511 byteorder: unicode
5512 The byte order used to represent the integer. If byteorder is 'big',
5513 the most significant byte is at the beginning of the byte array. If
5514 byteorder is 'little', the most significant byte is at the end of the
5515 byte array. To request the native byte order of the host system, use
5516 `sys.byteorder' as the byte order value.
5517 *
5518 signed as is_signed: bool = False
5519 Determines whether two's complement is used to represent the integer.
5520 If signed is False and a negative integer is given, an OverflowError
5521 is raised.
5522
5523Return an array of bytes representing an integer.
5524[clinic start generated code]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005525
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005526static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005527int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
5528 int is_signed)
5529/*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005531 int little_endian;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005532 PyObject *bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005533
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005534 if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005535 little_endian = 1;
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005536 else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005537 little_endian = 0;
5538 else {
5539 PyErr_SetString(PyExc_ValueError,
5540 "byteorder must be either 'little' or 'big'");
5541 return NULL;
5542 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005544 if (length < 0) {
5545 PyErr_SetString(PyExc_ValueError,
5546 "length argument must be non-negative");
5547 return NULL;
5548 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005550 bytes = PyBytes_FromStringAndSize(NULL, length);
5551 if (bytes == NULL)
5552 return NULL;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005553
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005554 if (_PyLong_AsByteArray((PyLongObject *)self,
5555 (unsigned char *)PyBytes_AS_STRING(bytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 length, little_endian, is_signed) < 0) {
5557 Py_DECREF(bytes);
5558 return NULL;
5559 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005561 return bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005562}
5563
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005564/*[clinic input]
5565@classmethod
5566int.from_bytes
5567
5568 bytes as bytes_obj: object
5569 Holds the array of bytes to convert. The argument must either
5570 support the buffer protocol or be an iterable object producing bytes.
5571 Bytes and bytearray are examples of built-in objects that support the
5572 buffer protocol.
5573 byteorder: unicode
5574 The byte order used to represent the integer. If byteorder is 'big',
5575 the most significant byte is at the beginning of the byte array. If
5576 byteorder is 'little', the most significant byte is at the end of the
5577 byte array. To request the native byte order of the host system, use
5578 `sys.byteorder' as the byte order value.
5579 *
5580 signed as is_signed: bool = False
5581 Indicates whether two's complement is used to represent the integer.
5582
5583Return the integer represented by the given array of bytes.
5584[clinic start generated code]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005585
5586static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005587int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
5588 PyObject *byteorder, int is_signed)
5589/*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005591 int little_endian;
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005592 PyObject *long_obj, *bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005593
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005594 if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 little_endian = 1;
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005596 else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005597 little_endian = 0;
5598 else {
5599 PyErr_SetString(PyExc_ValueError,
5600 "byteorder must be either 'little' or 'big'");
5601 return NULL;
5602 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005603
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005604 bytes = PyObject_Bytes(bytes_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 if (bytes == NULL)
5606 return NULL;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 long_obj = _PyLong_FromByteArray(
5609 (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
5610 little_endian, is_signed);
5611 Py_DECREF(bytes);
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005612
Zackery Spytz7bb9cd02018-10-05 15:02:23 -06005613 if (long_obj != NULL && type != &PyLong_Type) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01005614 Py_SETREF(long_obj, PyObject_CallFunctionObjArgs((PyObject *)type,
5615 long_obj, NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005616 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005618 return long_obj;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005619}
5620
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005621static PyObject *
5622long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
5623{
5624 return long_long(self);
5625}
5626
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005627static PyMethodDef long_methods[] = {
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005628 {"conjugate", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005629 "Returns self, the complex conjugate of any int."},
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005630 INT_BIT_LENGTH_METHODDEF
Christian Heimes53876d92008-04-19 00:31:39 +00005631#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005632 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
5633 "Returns always True."},
Christian Heimes53876d92008-04-19 00:31:39 +00005634#endif
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005635 INT_TO_BYTES_METHODDEF
5636 INT_FROM_BYTES_METHODDEF
Lisa Roach5ac70432018-09-13 23:56:23 -07005637 INT_AS_INTEGER_RATIO_METHODDEF
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005638 {"__trunc__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005639 "Truncating an Integral returns itself."},
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005640 {"__floor__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005641 "Flooring an Integral returns itself."},
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005642 {"__ceil__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005643 "Ceiling of an Integral returns itself."},
5644 {"__round__", (PyCFunction)long_round, METH_VARARGS,
5645 "Rounding an Integral returns itself.\n"
5646 "Rounding with an ndigits argument also returns an integer."},
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005647 INT___GETNEWARGS___METHODDEF
5648 INT___FORMAT___METHODDEF
5649 INT___SIZEOF___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005650 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005651};
5652
Guido van Rossumb43daf72007-08-01 18:08:08 +00005653static PyGetSetDef long_getset[] = {
Mark Dickinson6bf19002009-05-02 17:57:52 +00005654 {"real",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005655 (getter)long_long_meth, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005656 "the real part of a complex number",
5657 NULL},
Mark Dickinson6bf19002009-05-02 17:57:52 +00005658 {"imag",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005659 long_get0, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005660 "the imaginary part of a complex number",
Mark Dickinson6bf19002009-05-02 17:57:52 +00005661 NULL},
5662 {"numerator",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005663 (getter)long_long_meth, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005664 "the numerator of a rational number in lowest terms",
5665 NULL},
Mark Dickinson6bf19002009-05-02 17:57:52 +00005666 {"denominator",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005667 long_get1, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005668 "the denominator of a rational number in lowest terms",
Mark Dickinson6bf19002009-05-02 17:57:52 +00005669 NULL},
Guido van Rossumb43daf72007-08-01 18:08:08 +00005670 {NULL} /* Sentinel */
5671};
5672
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005673PyDoc_STRVAR(long_doc,
svelankar390a0962017-03-08 19:29:01 -05005674"int([x]) -> integer\n\
Chris Jerdonek83fe2e12012-10-07 14:48:36 -07005675int(x, base=10) -> integer\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00005676\n\
Chris Jerdonek83fe2e12012-10-07 14:48:36 -07005677Convert a number or string to an integer, or return 0 if no arguments\n\
5678are given. If x is a number, return x.__int__(). For floating point\n\
5679numbers, this truncates towards zero.\n\
5680\n\
5681If x is not a number or if base is given, then x must be a string,\n\
5682bytes, or bytearray instance representing an integer literal in the\n\
5683given base. The literal can be preceded by '+' or '-' and be surrounded\n\
5684by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\
5685Base 0 means to interpret the base from the string as an integer literal.\n\
5686>>> int('0b100', base=0)\n\
56874");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005688
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005689static PyNumberMethods long_as_number = {
Mark Dickinson22b20182010-05-10 21:27:53 +00005690 (binaryfunc)long_add, /*nb_add*/
5691 (binaryfunc)long_sub, /*nb_subtract*/
5692 (binaryfunc)long_mul, /*nb_multiply*/
5693 long_mod, /*nb_remainder*/
5694 long_divmod, /*nb_divmod*/
5695 long_pow, /*nb_power*/
5696 (unaryfunc)long_neg, /*nb_negative*/
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005697 long_long, /*tp_positive*/
Mark Dickinson22b20182010-05-10 21:27:53 +00005698 (unaryfunc)long_abs, /*tp_absolute*/
5699 (inquiry)long_bool, /*tp_bool*/
5700 (unaryfunc)long_invert, /*nb_invert*/
5701 long_lshift, /*nb_lshift*/
Serhiy Storchakaa5119e72019-05-19 14:14:38 +03005702 long_rshift, /*nb_rshift*/
Mark Dickinson22b20182010-05-10 21:27:53 +00005703 long_and, /*nb_and*/
5704 long_xor, /*nb_xor*/
5705 long_or, /*nb_or*/
5706 long_long, /*nb_int*/
5707 0, /*nb_reserved*/
5708 long_float, /*nb_float*/
5709 0, /* nb_inplace_add */
5710 0, /* nb_inplace_subtract */
5711 0, /* nb_inplace_multiply */
5712 0, /* nb_inplace_remainder */
5713 0, /* nb_inplace_power */
5714 0, /* nb_inplace_lshift */
5715 0, /* nb_inplace_rshift */
5716 0, /* nb_inplace_and */
5717 0, /* nb_inplace_xor */
5718 0, /* nb_inplace_or */
5719 long_div, /* nb_floor_divide */
5720 long_true_divide, /* nb_true_divide */
5721 0, /* nb_inplace_floor_divide */
5722 0, /* nb_inplace_true_divide */
5723 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00005724};
5725
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005726PyTypeObject PyLong_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005727 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5728 "int", /* tp_name */
5729 offsetof(PyLongObject, ob_digit), /* tp_basicsize */
5730 sizeof(digit), /* tp_itemsize */
Inada Naoki7d408692019-05-29 17:23:27 +09005731 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005732 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005733 0, /* tp_getattr */
5734 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +02005735 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 long_to_decimal_string, /* tp_repr */
5737 &long_as_number, /* tp_as_number */
5738 0, /* tp_as_sequence */
5739 0, /* tp_as_mapping */
5740 (hashfunc)long_hash, /* tp_hash */
5741 0, /* tp_call */
Serhiy Storchaka96aeaec2019-05-06 22:29:40 +03005742 0, /* tp_str */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005743 PyObject_GenericGetAttr, /* tp_getattro */
5744 0, /* tp_setattro */
5745 0, /* tp_as_buffer */
5746 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
5747 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
5748 long_doc, /* tp_doc */
5749 0, /* tp_traverse */
5750 0, /* tp_clear */
5751 long_richcompare, /* tp_richcompare */
5752 0, /* tp_weaklistoffset */
5753 0, /* tp_iter */
5754 0, /* tp_iternext */
5755 long_methods, /* tp_methods */
5756 0, /* tp_members */
5757 long_getset, /* tp_getset */
5758 0, /* tp_base */
5759 0, /* tp_dict */
5760 0, /* tp_descr_get */
5761 0, /* tp_descr_set */
5762 0, /* tp_dictoffset */
5763 0, /* tp_init */
5764 0, /* tp_alloc */
5765 long_new, /* tp_new */
5766 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00005767};
Guido van Rossumddefaf32007-01-14 03:31:43 +00005768
Mark Dickinsonbd792642009-03-18 20:06:12 +00005769static PyTypeObject Int_InfoType;
5770
5771PyDoc_STRVAR(int_info__doc__,
5772"sys.int_info\n\
5773\n\
Paul Ganssle2bb6bf02019-09-12 03:50:29 +01005774A named tuple that holds information about Python's\n\
Mark Dickinsonbd792642009-03-18 20:06:12 +00005775internal representation of integers. The attributes are read only.");
5776
5777static PyStructSequence_Field int_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005778 {"bits_per_digit", "size of a digit in bits"},
Mark Dickinson22b20182010-05-10 21:27:53 +00005779 {"sizeof_digit", "size in bytes of the C type used to represent a digit"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005780 {NULL, NULL}
Mark Dickinsonbd792642009-03-18 20:06:12 +00005781};
5782
5783static PyStructSequence_Desc int_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005784 "sys.int_info", /* name */
5785 int_info__doc__, /* doc */
5786 int_info_fields, /* fields */
5787 2 /* number of fields */
Mark Dickinsonbd792642009-03-18 20:06:12 +00005788};
5789
5790PyObject *
5791PyLong_GetInfo(void)
5792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005793 PyObject* int_info;
5794 int field = 0;
5795 int_info = PyStructSequence_New(&Int_InfoType);
5796 if (int_info == NULL)
5797 return NULL;
5798 PyStructSequence_SET_ITEM(int_info, field++,
5799 PyLong_FromLong(PyLong_SHIFT));
5800 PyStructSequence_SET_ITEM(int_info, field++,
5801 PyLong_FromLong(sizeof(digit)));
5802 if (PyErr_Occurred()) {
5803 Py_CLEAR(int_info);
5804 return NULL;
5805 }
5806 return int_info;
Mark Dickinsonbd792642009-03-18 20:06:12 +00005807}
5808
Guido van Rossumddefaf32007-01-14 03:31:43 +00005809int
5810_PyLong_Init(void)
5811{
5812#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005813 int ival, size;
5814 PyLongObject *v = small_ints;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00005815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005816 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
5817 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
5818 if (Py_TYPE(v) == &PyLong_Type) {
5819 /* The element is already initialized, most likely
5820 * the Python interpreter was initialized before.
5821 */
5822 Py_ssize_t refcnt;
5823 PyObject* op = (PyObject*)v;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00005824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005825 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
5826 _Py_NewReference(op);
5827 /* _Py_NewReference sets the ref count to 1 but
5828 * the ref count might be larger. Set the refcnt
5829 * to the original refcnt + 1 */
5830 Py_REFCNT(op) = refcnt + 1;
5831 assert(Py_SIZE(op) == size);
Victor Stinner12174a52014-08-15 23:17:38 +02005832 assert(v->ob_digit[0] == (digit)abs(ival));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005833 }
5834 else {
Victor Stinnerb509d522018-11-23 14:27:38 +01005835 (void)PyObject_INIT(v, &PyLong_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005836 }
5837 Py_SIZE(v) = size;
Victor Stinner12174a52014-08-15 23:17:38 +02005838 v->ob_digit[0] = (digit)abs(ival);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005839 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00005840#endif
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005841 _PyLong_Zero = PyLong_FromLong(0);
5842 if (_PyLong_Zero == NULL)
5843 return 0;
5844 _PyLong_One = PyLong_FromLong(1);
5845 if (_PyLong_One == NULL)
5846 return 0;
5847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005848 /* initialize int_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02005849 if (Int_InfoType.tp_name == NULL) {
Victor Stinner6d43f6f2019-01-22 21:18:05 +01005850 if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) {
Victor Stinner1c8f0592013-07-22 22:24:54 +02005851 return 0;
Victor Stinner6d43f6f2019-01-22 21:18:05 +01005852 }
Victor Stinner1c8f0592013-07-22 22:24:54 +02005853 }
Mark Dickinsonbd792642009-03-18 20:06:12 +00005854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005855 return 1;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005856}
5857
5858void
5859PyLong_Fini(void)
5860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005861 /* Integers are currently statically allocated. Py_DECREF is not
5862 needed, but Python must forget about the reference or multiple
5863 reinitializations will fail. */
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005864 Py_CLEAR(_PyLong_One);
5865 Py_CLEAR(_PyLong_Zero);
Guido van Rossumddefaf32007-01-14 03:31:43 +00005866#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005867 int i;
5868 PyLongObject *v = small_ints;
5869 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
5870 _Py_DEC_REFTOTAL;
5871 _Py_ForgetReference((PyObject*)v);
5872 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00005873#endif
5874}