blob: da697a784faad0be06296c97753029e38074f5d6 [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();
1379 return (unsigned long) -1;
1380 }
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();
1407 return (unsigned 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
3056static void
Tim Peters9f688bf2000-07-07 15:53:28 +00003057long_dealloc(PyObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 Py_TYPE(v)->tp_free(v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003060}
3061
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003062static int
Tim Peters9f688bf2000-07-07 15:53:28 +00003063long_compare(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 Py_ssize_t sign;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 if (Py_SIZE(a) != Py_SIZE(b)) {
3068 sign = Py_SIZE(a) - Py_SIZE(b);
3069 }
3070 else {
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003071 Py_ssize_t i = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
3073 ;
3074 if (i < 0)
3075 sign = 0;
3076 else {
3077 sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
3078 if (Py_SIZE(a) < 0)
3079 sign = -sign;
3080 }
3081 }
3082 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003083}
3084
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003085static PyObject *
3086long_richcompare(PyObject *self, PyObject *other, int op)
3087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 CHECK_BINOP(self, other);
3090 if (self == other)
3091 result = 0;
3092 else
3093 result = long_compare((PyLongObject*)self, (PyLongObject*)other);
stratakise8b19652017-11-02 11:32:54 +01003094 Py_RETURN_RICHCOMPARE(result, 0, op);
Guido van Rossum47b9ff62006-08-24 00:41:19 +00003095}
3096
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003097static Py_hash_t
Tim Peters9f688bf2000-07-07 15:53:28 +00003098long_hash(PyLongObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +00003099{
Benjamin Peterson8035bc52010-10-23 16:20:50 +00003100 Py_uhash_t x;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 Py_ssize_t i;
3102 int sign;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 i = Py_SIZE(v);
3105 switch(i) {
3106 case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
3107 case 0: return 0;
3108 case 1: return v->ob_digit[0];
3109 }
3110 sign = 1;
3111 x = 0;
3112 if (i < 0) {
3113 sign = -1;
3114 i = -(i);
3115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 while (--i >= 0) {
Mark Dickinsondc787d22010-05-23 13:33:13 +00003117 /* Here x is a quantity in the range [0, _PyHASH_MODULUS); we
3118 want to compute x * 2**PyLong_SHIFT + v->ob_digit[i] modulo
3119 _PyHASH_MODULUS.
3120
3121 The computation of x * 2**PyLong_SHIFT % _PyHASH_MODULUS
3122 amounts to a rotation of the bits of x. To see this, write
3123
3124 x * 2**PyLong_SHIFT = y * 2**_PyHASH_BITS + z
3125
3126 where y = x >> (_PyHASH_BITS - PyLong_SHIFT) gives the top
3127 PyLong_SHIFT bits of x (those that are shifted out of the
3128 original _PyHASH_BITS bits, and z = (x << PyLong_SHIFT) &
3129 _PyHASH_MODULUS gives the bottom _PyHASH_BITS - PyLong_SHIFT
3130 bits of x, shifted up. Then since 2**_PyHASH_BITS is
3131 congruent to 1 modulo _PyHASH_MODULUS, y*2**_PyHASH_BITS is
3132 congruent to y modulo _PyHASH_MODULUS. So
3133
3134 x * 2**PyLong_SHIFT = y + z (mod _PyHASH_MODULUS).
3135
3136 The right-hand side is just the result of rotating the
3137 _PyHASH_BITS bits of x left by PyLong_SHIFT places; since
3138 not all _PyHASH_BITS bits of x are 1s, the same is true
3139 after rotation, so 0 <= y+z < _PyHASH_MODULUS and y + z is
3140 the reduction of x*2**PyLong_SHIFT modulo
3141 _PyHASH_MODULUS. */
3142 x = ((x << PyLong_SHIFT) & _PyHASH_MODULUS) |
3143 (x >> (_PyHASH_BITS - PyLong_SHIFT));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 x += v->ob_digit[i];
Mark Dickinsondc787d22010-05-23 13:33:13 +00003145 if (x >= _PyHASH_MODULUS)
3146 x -= _PyHASH_MODULUS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 }
3148 x = x * sign;
Benjamin Peterson8035bc52010-10-23 16:20:50 +00003149 if (x == (Py_uhash_t)-1)
3150 x = (Py_uhash_t)-2;
Benjamin Peterson8f67d082010-10-17 20:54:53 +00003151 return (Py_hash_t)x;
Guido van Rossum9bfef441993-03-29 10:43:31 +00003152}
3153
3154
Serhiy Storchaka95949422013-08-27 19:40:23 +03003155/* Add the absolute values of two integers. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003156
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003157static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003158x_add(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003159{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003160 Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 PyLongObject *z;
3162 Py_ssize_t i;
3163 digit carry = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00003164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 /* Ensure a is the larger of the two: */
3166 if (size_a < size_b) {
3167 { PyLongObject *temp = a; a = b; b = temp; }
3168 { Py_ssize_t size_temp = size_a;
Mark Dickinson22b20182010-05-10 21:27:53 +00003169 size_a = size_b;
3170 size_b = size_temp; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 }
3172 z = _PyLong_New(size_a+1);
3173 if (z == NULL)
3174 return NULL;
3175 for (i = 0; i < size_b; ++i) {
3176 carry += a->ob_digit[i] + b->ob_digit[i];
3177 z->ob_digit[i] = carry & PyLong_MASK;
3178 carry >>= PyLong_SHIFT;
3179 }
3180 for (; i < size_a; ++i) {
3181 carry += a->ob_digit[i];
3182 z->ob_digit[i] = carry & PyLong_MASK;
3183 carry >>= PyLong_SHIFT;
3184 }
3185 z->ob_digit[i] = carry;
3186 return long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003187}
3188
3189/* Subtract the absolute values of two integers. */
3190
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003191static PyLongObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00003192x_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003193{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003194 Py_ssize_t size_a = Py_ABS(Py_SIZE(a)), size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyLongObject *z;
3196 Py_ssize_t i;
3197 int sign = 1;
3198 digit borrow = 0;
Tim Peters69c2de32001-09-11 22:31:33 +00003199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 /* Ensure a is the larger of the two: */
3201 if (size_a < size_b) {
3202 sign = -1;
3203 { PyLongObject *temp = a; a = b; b = temp; }
3204 { Py_ssize_t size_temp = size_a;
Mark Dickinson22b20182010-05-10 21:27:53 +00003205 size_a = size_b;
3206 size_b = size_temp; }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
3208 else if (size_a == size_b) {
3209 /* Find highest digit where a and b differ: */
3210 i = size_a;
3211 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
3212 ;
3213 if (i < 0)
3214 return (PyLongObject *)PyLong_FromLong(0);
3215 if (a->ob_digit[i] < b->ob_digit[i]) {
3216 sign = -1;
3217 { PyLongObject *temp = a; a = b; b = temp; }
3218 }
3219 size_a = size_b = i+1;
3220 }
3221 z = _PyLong_New(size_a);
3222 if (z == NULL)
3223 return NULL;
3224 for (i = 0; i < size_b; ++i) {
3225 /* The following assumes unsigned arithmetic
3226 works module 2**N for some N>PyLong_SHIFT. */
3227 borrow = a->ob_digit[i] - b->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 for (; i < size_a; ++i) {
3233 borrow = a->ob_digit[i] - borrow;
3234 z->ob_digit[i] = borrow & PyLong_MASK;
3235 borrow >>= PyLong_SHIFT;
3236 borrow &= 1; /* Keep only one sign bit */
3237 }
3238 assert(borrow == 0);
Victor Stinner8aed6f12013-07-17 22:31:17 +02003239 if (sign < 0) {
Victor Stinner8bcf3122016-08-17 19:48:33 +02003240 Py_SIZE(z) = -Py_SIZE(z);
Victor Stinner8aed6f12013-07-17 22:31:17 +02003241 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 return long_normalize(z);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003243}
3244
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003245static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003246long_add(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003247{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 PyLongObject *z;
Tim Peters69c2de32001-09-11 22:31:33 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003251
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003252 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Mark Dickinsonc1c4a642016-09-17 20:01:56 +01003253 return PyLong_FromLong(MEDIUM_VALUE(a) + MEDIUM_VALUE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
3255 if (Py_SIZE(a) < 0) {
3256 if (Py_SIZE(b) < 0) {
3257 z = x_add(a, b);
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003258 if (z != NULL) {
3259 /* x_add received at least one multiple-digit int,
3260 and thus z must be a multiple-digit int.
3261 That also means z is not an element of
3262 small_ints, so negating it in-place is safe. */
3263 assert(Py_REFCNT(z) == 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 Py_SIZE(z) = -(Py_SIZE(z));
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003265 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 }
3267 else
3268 z = x_sub(b, a);
3269 }
3270 else {
3271 if (Py_SIZE(b) < 0)
3272 z = x_sub(a, b);
3273 else
3274 z = x_add(a, b);
3275 }
3276 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003277}
3278
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003279static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003280long_sub(PyLongObject *a, PyLongObject *b)
Guido van Rossum4c260ff1992-01-14 18:36:43 +00003281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00003285
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003286 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Mark Dickinsonc1c4a642016-09-17 20:01:56 +01003287 return PyLong_FromLong(MEDIUM_VALUE(a) - MEDIUM_VALUE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 }
3289 if (Py_SIZE(a) < 0) {
3290 if (Py_SIZE(b) < 0)
3291 z = x_sub(a, b);
3292 else
3293 z = x_add(a, b);
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003294 if (z != NULL) {
3295 assert(Py_SIZE(z) == 0 || Py_REFCNT(z) == 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 Py_SIZE(z) = -(Py_SIZE(z));
Serhiy Storchakae63e5d62016-06-04 00:06:45 +03003297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 }
3299 else {
3300 if (Py_SIZE(b) < 0)
3301 z = x_add(a, b);
3302 else
3303 z = x_sub(a, b);
3304 }
3305 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003306}
3307
Tim Peters5af4e6c2002-08-12 02:31:19 +00003308/* Grade school multiplication, ignoring the signs.
3309 * Returns the absolute value of the product, or NULL if error.
3310 */
3311static PyLongObject *
3312x_mul(PyLongObject *a, PyLongObject *b)
Neil Schemenauerba872e22001-01-04 01:46:03 +00003313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 PyLongObject *z;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003315 Py_ssize_t size_a = Py_ABS(Py_SIZE(a));
3316 Py_ssize_t size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 Py_ssize_t i;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 z = _PyLong_New(size_a + size_b);
3320 if (z == NULL)
3321 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
3324 if (a == b) {
3325 /* Efficient squaring per HAC, Algorithm 14.16:
3326 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
3327 * Gives slightly less than a 2x speedup when a == b,
3328 * via exploiting that each entry in the multiplication
3329 * pyramid appears twice (except for the size_a squares).
3330 */
3331 for (i = 0; i < size_a; ++i) {
3332 twodigits carry;
3333 twodigits f = a->ob_digit[i];
3334 digit *pz = z->ob_digit + (i << 1);
3335 digit *pa = a->ob_digit + i + 1;
3336 digit *paend = a->ob_digit + size_a;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00003339 Py_DECREF(z);
3340 return NULL;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00003341 });
Tim Peters0973b992004-08-29 22:16:50 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 carry = *pz + f * f;
3344 *pz++ = (digit)(carry & PyLong_MASK);
3345 carry >>= PyLong_SHIFT;
3346 assert(carry <= PyLong_MASK);
Tim Peters0973b992004-08-29 22:16:50 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 /* Now f is added in twice in each column of the
3349 * pyramid it appears. Same as adding f<<1 once.
3350 */
3351 f <<= 1;
3352 while (pa < paend) {
3353 carry += *pz + *pa++ * f;
3354 *pz++ = (digit)(carry & PyLong_MASK);
3355 carry >>= PyLong_SHIFT;
3356 assert(carry <= (PyLong_MASK << 1));
3357 }
3358 if (carry) {
3359 carry += *pz;
3360 *pz++ = (digit)(carry & PyLong_MASK);
3361 carry >>= PyLong_SHIFT;
3362 }
3363 if (carry)
3364 *pz += (digit)(carry & PyLong_MASK);
3365 assert((carry >> PyLong_SHIFT) == 0);
3366 }
3367 }
Serhiy Storchaka95949422013-08-27 19:40:23 +03003368 else { /* a is not the same as b -- gradeschool int mult */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003369 for (i = 0; i < size_a; ++i) {
3370 twodigits carry = 0;
3371 twodigits f = a->ob_digit[i];
3372 digit *pz = z->ob_digit + i;
3373 digit *pb = b->ob_digit;
3374 digit *pbend = b->ob_digit + size_b;
Tim Peters0973b992004-08-29 22:16:50 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 SIGCHECK({
Mark Dickinson22b20182010-05-10 21:27:53 +00003377 Py_DECREF(z);
3378 return NULL;
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00003379 });
Tim Peters0973b992004-08-29 22:16:50 +00003380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 while (pb < pbend) {
3382 carry += *pz + *pb++ * f;
3383 *pz++ = (digit)(carry & PyLong_MASK);
3384 carry >>= PyLong_SHIFT;
3385 assert(carry <= PyLong_MASK);
3386 }
3387 if (carry)
3388 *pz += (digit)(carry & PyLong_MASK);
3389 assert((carry >> PyLong_SHIFT) == 0);
3390 }
3391 }
3392 return long_normalize(z);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003393}
3394
3395/* A helper for Karatsuba multiplication (k_mul).
Serhiy Storchaka95949422013-08-27 19:40:23 +03003396 Takes an int "n" and an integer "size" representing the place to
Tim Peters5af4e6c2002-08-12 02:31:19 +00003397 split, and sets low and high such that abs(n) == (high << size) + low,
3398 viewing the shift as being by digits. The sign bit is ignored, and
3399 the return values are >= 0.
3400 Returns 0 on success, -1 on failure.
3401*/
3402static int
Mark Dickinson22b20182010-05-10 21:27:53 +00003403kmul_split(PyLongObject *n,
3404 Py_ssize_t size,
3405 PyLongObject **high,
3406 PyLongObject **low)
Tim Peters5af4e6c2002-08-12 02:31:19 +00003407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 PyLongObject *hi, *lo;
3409 Py_ssize_t size_lo, size_hi;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003410 const Py_ssize_t size_n = Py_ABS(Py_SIZE(n));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003411
Victor Stinner640c35c2013-06-04 23:14:37 +02003412 size_lo = Py_MIN(size_n, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 size_hi = size_n - size_lo;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 if ((hi = _PyLong_New(size_hi)) == NULL)
3416 return -1;
3417 if ((lo = _PyLong_New(size_lo)) == NULL) {
3418 Py_DECREF(hi);
3419 return -1;
3420 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
3423 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 *high = long_normalize(hi);
3426 *low = long_normalize(lo);
3427 return 0;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003428}
3429
Tim Peters60004642002-08-12 22:01:34 +00003430static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
3431
Tim Peters5af4e6c2002-08-12 02:31:19 +00003432/* Karatsuba multiplication. Ignores the input signs, and returns the
3433 * absolute value of the product (or NULL if error).
3434 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
3435 */
3436static PyLongObject *
3437k_mul(PyLongObject *a, PyLongObject *b)
3438{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003439 Py_ssize_t asize = Py_ABS(Py_SIZE(a));
3440 Py_ssize_t bsize = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 PyLongObject *ah = NULL;
3442 PyLongObject *al = NULL;
3443 PyLongObject *bh = NULL;
3444 PyLongObject *bl = NULL;
3445 PyLongObject *ret = NULL;
3446 PyLongObject *t1, *t2, *t3;
3447 Py_ssize_t shift; /* the number of digits we split off */
3448 Py_ssize_t i;
Tim Peters738eda72002-08-12 15:08:20 +00003449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
3451 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
3452 * Then the original product is
3453 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
3454 * By picking X to be a power of 2, "*X" is just shifting, and it's
3455 * been reduced to 3 multiplies on numbers half the size.
3456 */
Tim Peters5af4e6c2002-08-12 02:31:19 +00003457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 /* We want to split based on the larger number; fiddle so that b
3459 * is largest.
3460 */
3461 if (asize > bsize) {
3462 t1 = a;
3463 a = b;
3464 b = t1;
Tim Peters738eda72002-08-12 15:08:20 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 i = asize;
3467 asize = bsize;
3468 bsize = i;
3469 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 /* Use gradeschool math when either number is too small. */
3472 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
3473 if (asize <= i) {
3474 if (asize == 0)
3475 return (PyLongObject *)PyLong_FromLong(0);
3476 else
3477 return x_mul(a, b);
3478 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00003479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 /* If a is small compared to b, splitting on b gives a degenerate
3481 * case with ah==0, and Karatsuba may be (even much) less efficient
3482 * than "grade school" then. However, we can still win, by viewing
3483 * b as a string of "big digits", each of width a->ob_size. That
3484 * leads to a sequence of balanced calls to k_mul.
3485 */
3486 if (2 * asize <= bsize)
3487 return k_lopsided_mul(a, b);
Tim Peters60004642002-08-12 22:01:34 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 /* Split a & b into hi & lo pieces. */
3490 shift = bsize >> 1;
3491 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
3492 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
Tim Petersd6974a52002-08-13 20:37:51 +00003493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 if (a == b) {
3495 bh = ah;
3496 bl = al;
3497 Py_INCREF(bh);
3498 Py_INCREF(bl);
3499 }
3500 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 /* The plan:
3503 * 1. Allocate result space (asize + bsize digits: that's always
3504 * enough).
3505 * 2. Compute ah*bh, and copy into result at 2*shift.
3506 * 3. Compute al*bl, and copy into result at 0. Note that this
3507 * can't overlap with #2.
3508 * 4. Subtract al*bl from the result, starting at shift. This may
3509 * underflow (borrow out of the high digit), but we don't care:
3510 * we're effectively doing unsigned arithmetic mod
3511 * BASE**(sizea + sizeb), and so long as the *final* result fits,
3512 * borrows and carries out of the high digit can be ignored.
3513 * 5. Subtract ah*bh from the result, starting at shift.
3514 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
3515 * at shift.
3516 */
Tim Petersd64c1de2002-08-12 17:36:03 +00003517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 /* 1. Allocate result space. */
3519 ret = _PyLong_New(asize + bsize);
3520 if (ret == NULL) goto fail;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003521#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 /* Fill with trash, to catch reference to uninitialized digits. */
3523 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003524#endif
Tim Peters44121a62002-08-12 06:17:58 +00003525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 /* 2. t1 <- ah*bh, and copy into high digits of result. */
3527 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
3528 assert(Py_SIZE(t1) >= 0);
3529 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
3530 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
3531 Py_SIZE(t1) * sizeof(digit));
Tim Peters738eda72002-08-12 15:08:20 +00003532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 /* Zero-out the digits higher than the ah*bh copy. */
3534 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
3535 if (i)
3536 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
3537 i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539 /* 3. t2 <- al*bl, and copy into the low digits. */
3540 if ((t2 = k_mul(al, bl)) == NULL) {
3541 Py_DECREF(t1);
3542 goto fail;
3543 }
3544 assert(Py_SIZE(t2) >= 0);
3545 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
3546 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 /* Zero out remaining digits. */
3549 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
3550 if (i)
3551 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
Tim Peters5af4e6c2002-08-12 02:31:19 +00003552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
3554 * because it's fresher in cache.
3555 */
3556 i = Py_SIZE(ret) - shift; /* # digits after shift */
3557 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
3558 Py_DECREF(t2);
Tim Peters738eda72002-08-12 15:08:20 +00003559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
3561 Py_DECREF(t1);
Tim Peters738eda72002-08-12 15:08:20 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
3564 if ((t1 = x_add(ah, al)) == NULL) goto fail;
3565 Py_DECREF(ah);
3566 Py_DECREF(al);
3567 ah = al = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 if (a == b) {
3570 t2 = t1;
3571 Py_INCREF(t2);
3572 }
3573 else if ((t2 = x_add(bh, bl)) == NULL) {
3574 Py_DECREF(t1);
3575 goto fail;
3576 }
3577 Py_DECREF(bh);
3578 Py_DECREF(bl);
3579 bh = bl = NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 t3 = k_mul(t1, t2);
3582 Py_DECREF(t1);
3583 Py_DECREF(t2);
3584 if (t3 == NULL) goto fail;
3585 assert(Py_SIZE(t3) >= 0);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 /* Add t3. It's not obvious why we can't run out of room here.
3588 * See the (*) comment after this function.
3589 */
3590 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
3591 Py_DECREF(t3);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 return long_normalize(ret);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003594
Mark Dickinson22b20182010-05-10 21:27:53 +00003595 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 Py_XDECREF(ret);
3597 Py_XDECREF(ah);
3598 Py_XDECREF(al);
3599 Py_XDECREF(bh);
3600 Py_XDECREF(bl);
3601 return NULL;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003602}
3603
Tim Petersd6974a52002-08-13 20:37:51 +00003604/* (*) Why adding t3 can't "run out of room" above.
3605
Tim Petersab86c2b2002-08-15 20:06:00 +00003606Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
3607to start with:
Tim Petersd6974a52002-08-13 20:37:51 +00003608
Tim Petersab86c2b2002-08-15 20:06:00 +000036091. For any integer i, i = c(i/2) + f(i/2). In particular,
3610 bsize = c(bsize/2) + f(bsize/2).
36112. shift = f(bsize/2)
36123. asize <= bsize
36134. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
3614 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
Tim Petersd6974a52002-08-13 20:37:51 +00003615
Tim Petersab86c2b2002-08-15 20:06:00 +00003616We allocated asize + bsize result digits, and add t3 into them at an offset
3617of shift. This leaves asize+bsize-shift allocated digit positions for t3
3618to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
3619asize + c(bsize/2) available digit positions.
Tim Petersd6974a52002-08-13 20:37:51 +00003620
Tim Petersab86c2b2002-08-15 20:06:00 +00003621bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
3622at most c(bsize/2) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00003623
Tim Petersab86c2b2002-08-15 20:06:00 +00003624If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
3625digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
3626most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
Tim Petersd6974a52002-08-13 20:37:51 +00003627
Tim Petersab86c2b2002-08-15 20:06:00 +00003628The product (ah+al)*(bh+bl) therefore has at most
Tim Petersd6974a52002-08-13 20:37:51 +00003629
Tim Petersab86c2b2002-08-15 20:06:00 +00003630 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
Tim Petersd6974a52002-08-13 20:37:51 +00003631
Tim Petersab86c2b2002-08-15 20:06:00 +00003632and we have asize + c(bsize/2) available digit positions. We need to show
3633this is always enough. An instance of c(bsize/2) cancels out in both, so
3634the question reduces to whether asize digits is enough to hold
3635(asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
3636then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
3637asize 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 +00003638digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
Tim Petersab86c2b2002-08-15 20:06:00 +00003639asize == bsize, then we're asking whether bsize digits is enough to hold
Tim Peterse417de02002-08-15 20:10:45 +00003640c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
3641is enough to hold 2 bits. This is so if bsize >= 2, which holds because
3642bsize >= KARATSUBA_CUTOFF >= 2.
Tim Peters8e966ee2002-08-14 16:36:23 +00003643
Tim Peters48d52c02002-08-14 17:07:32 +00003644Note that since there's always enough room for (ah+al)*(bh+bl), and that's
3645clearly >= each of ah*bh and al*bl, there's always enough room to subtract
3646ah*bh and al*bl too.
Tim Petersd6974a52002-08-13 20:37:51 +00003647*/
3648
Tim Peters60004642002-08-12 22:01:34 +00003649/* b has at least twice the digits of a, and a is big enough that Karatsuba
3650 * would pay off *if* the inputs had balanced sizes. View b as a sequence
3651 * of slices, each with a->ob_size digits, and multiply the slices by a,
3652 * one at a time. This gives k_mul balanced inputs to work with, and is
3653 * also cache-friendly (we compute one double-width slice of the result
Ezio Melotti42da6632011-03-15 05:18:48 +02003654 * at a time, then move on, never backtracking except for the helpful
Tim Peters60004642002-08-12 22:01:34 +00003655 * single-width slice overlap between successive partial sums).
3656 */
3657static PyLongObject *
3658k_lopsided_mul(PyLongObject *a, PyLongObject *b)
3659{
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003660 const Py_ssize_t asize = Py_ABS(Py_SIZE(a));
3661 Py_ssize_t bsize = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 Py_ssize_t nbdone; /* # of b digits already multiplied */
3663 PyLongObject *ret;
3664 PyLongObject *bslice = NULL;
Tim Peters60004642002-08-12 22:01:34 +00003665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 assert(asize > KARATSUBA_CUTOFF);
3667 assert(2 * asize <= bsize);
Tim Peters60004642002-08-12 22:01:34 +00003668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003669 /* Allocate result space, and zero it out. */
3670 ret = _PyLong_New(asize + bsize);
3671 if (ret == NULL)
3672 return NULL;
3673 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
Tim Peters60004642002-08-12 22:01:34 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 /* Successive slices of b are copied into bslice. */
3676 bslice = _PyLong_New(asize);
3677 if (bslice == NULL)
3678 goto fail;
Tim Peters60004642002-08-12 22:01:34 +00003679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 nbdone = 0;
3681 while (bsize > 0) {
3682 PyLongObject *product;
Victor Stinner640c35c2013-06-04 23:14:37 +02003683 const Py_ssize_t nbtouse = Py_MIN(bsize, asize);
Tim Peters60004642002-08-12 22:01:34 +00003684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 /* Multiply the next slice of b by a. */
3686 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
3687 nbtouse * sizeof(digit));
3688 Py_SIZE(bslice) = nbtouse;
3689 product = k_mul(a, bslice);
3690 if (product == NULL)
3691 goto fail;
Tim Peters60004642002-08-12 22:01:34 +00003692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 /* Add into result. */
3694 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
3695 product->ob_digit, Py_SIZE(product));
3696 Py_DECREF(product);
Tim Peters60004642002-08-12 22:01:34 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 bsize -= nbtouse;
3699 nbdone += nbtouse;
3700 }
Tim Peters60004642002-08-12 22:01:34 +00003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 Py_DECREF(bslice);
3703 return long_normalize(ret);
Tim Peters60004642002-08-12 22:01:34 +00003704
Mark Dickinson22b20182010-05-10 21:27:53 +00003705 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 Py_DECREF(ret);
3707 Py_XDECREF(bslice);
3708 return NULL;
Tim Peters60004642002-08-12 22:01:34 +00003709}
Tim Peters5af4e6c2002-08-12 02:31:19 +00003710
3711static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003712long_mul(PyLongObject *a, PyLongObject *b)
Tim Peters5af4e6c2002-08-12 02:31:19 +00003713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 CHECK_BINOP(a, b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 /* fast path for single-digit multiplication */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003719 if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b);
Benjamin Petersonaf580df2016-09-06 10:46:49 -07003721 return PyLong_FromLongLong((long long)v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 z = k_mul(a, b);
3725 /* Negate if exactly one of the inputs is negative. */
Victor Stinner8aed6f12013-07-17 22:31:17 +02003726 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) {
3727 _PyLong_Negate(&z);
3728 if (z == NULL)
3729 return NULL;
3730 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003732}
3733
Yury Selivanove0b23092016-02-11 10:26:27 -05003734/* Fast modulo division for single-digit longs. */
3735static PyObject *
3736fast_mod(PyLongObject *a, PyLongObject *b)
3737{
3738 sdigit left = a->ob_digit[0];
3739 sdigit right = b->ob_digit[0];
3740 sdigit mod;
3741
3742 assert(Py_ABS(Py_SIZE(a)) == 1);
3743 assert(Py_ABS(Py_SIZE(b)) == 1);
3744
3745 if (Py_SIZE(a) == Py_SIZE(b)) {
3746 /* 'a' and 'b' have the same sign. */
3747 mod = left % right;
3748 }
3749 else {
3750 /* Either 'a' or 'b' is negative. */
3751 mod = right - 1 - (left - 1) % right;
3752 }
3753
Victor Stinnerf963c132016-03-23 18:36:54 +01003754 return PyLong_FromLong(mod * (sdigit)Py_SIZE(b));
Yury Selivanove0b23092016-02-11 10:26:27 -05003755}
3756
3757/* Fast floor division for single-digit longs. */
3758static PyObject *
3759fast_floor_div(PyLongObject *a, PyLongObject *b)
3760{
3761 sdigit left = a->ob_digit[0];
3762 sdigit right = b->ob_digit[0];
3763 sdigit div;
3764
3765 assert(Py_ABS(Py_SIZE(a)) == 1);
3766 assert(Py_ABS(Py_SIZE(b)) == 1);
3767
3768 if (Py_SIZE(a) == Py_SIZE(b)) {
3769 /* 'a' and 'b' have the same sign. */
3770 div = left / right;
3771 }
3772 else {
3773 /* Either 'a' or 'b' is negative. */
3774 div = -1 - (left - 1) / right;
3775 }
3776
3777 return PyLong_FromLong(div);
3778}
3779
Guido van Rossume32e0141992-01-19 16:31:05 +00003780/* The / and % operators are now defined in terms of divmod().
3781 The expression a mod b has the value a - b*floor(a/b).
3782 The long_divrem function gives the remainder after division of
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003783 |a| by |b|, with the sign of a. This is also expressed
3784 as a - b*trunc(a/b), if trunc truncates towards zero.
3785 Some examples:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 a b a rem b a mod b
3787 13 10 3 3
3788 -13 10 -3 7
3789 13 -10 3 -7
3790 -13 -10 -3 -3
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003791 So, to get from rem to mod, we have to add b if a and b
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00003792 have different signs. We then subtract one from the 'div'
3793 part of the outcome to keep the invariant intact. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00003794
Tim Peters47e52ee2004-08-30 02:44:38 +00003795/* Compute
3796 * *pdiv, *pmod = divmod(v, w)
3797 * NULL can be passed for pdiv or pmod, in which case that part of
3798 * the result is simply thrown away. The caller owns a reference to
3799 * each of these it requests (does not pass NULL for).
3800 */
Guido van Rossume32e0141992-01-19 16:31:05 +00003801static int
Tim Peters5af4e6c2002-08-12 02:31:19 +00003802l_divmod(PyLongObject *v, PyLongObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 PyLongObject **pdiv, PyLongObject **pmod)
Guido van Rossume32e0141992-01-19 16:31:05 +00003804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 PyLongObject *div, *mod;
Tim Peters5af4e6c2002-08-12 02:31:19 +00003806
Yury Selivanove0b23092016-02-11 10:26:27 -05003807 if (Py_ABS(Py_SIZE(v)) == 1 && Py_ABS(Py_SIZE(w)) == 1) {
3808 /* Fast path for single-digit longs */
3809 div = NULL;
3810 if (pdiv != NULL) {
3811 div = (PyLongObject *)fast_floor_div(v, w);
3812 if (div == NULL) {
3813 return -1;
3814 }
3815 }
3816 if (pmod != NULL) {
3817 mod = (PyLongObject *)fast_mod(v, w);
3818 if (mod == NULL) {
3819 Py_XDECREF(div);
3820 return -1;
3821 }
3822 *pmod = mod;
3823 }
3824 if (pdiv != NULL) {
3825 /* We only want to set `*pdiv` when `*pmod` is
3826 set successfully. */
3827 *pdiv = div;
3828 }
3829 return 0;
3830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 if (long_divrem(v, w, &div, &mod) < 0)
3832 return -1;
3833 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
3834 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
3835 PyLongObject *temp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 temp = (PyLongObject *) long_add(mod, w);
3837 Py_DECREF(mod);
3838 mod = temp;
3839 if (mod == NULL) {
3840 Py_DECREF(div);
3841 return -1;
3842 }
Serhiy Storchakaba85d692017-03-30 09:09:41 +03003843 temp = (PyLongObject *) long_sub(div, (PyLongObject *)_PyLong_One);
3844 if (temp == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 Py_DECREF(mod);
3846 Py_DECREF(div);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 return -1;
3848 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 Py_DECREF(div);
3850 div = temp;
3851 }
3852 if (pdiv != NULL)
3853 *pdiv = div;
3854 else
3855 Py_DECREF(div);
Tim Peters47e52ee2004-08-30 02:44:38 +00003856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 if (pmod != NULL)
3858 *pmod = mod;
3859 else
3860 Py_DECREF(mod);
Tim Peters47e52ee2004-08-30 02:44:38 +00003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 return 0;
Guido van Rossume32e0141992-01-19 16:31:05 +00003863}
3864
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003865static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00003866long_div(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00003867{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 PyLongObject *div;
Neil Schemenauerba872e22001-01-04 01:46:03 +00003869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 CHECK_BINOP(a, b);
Yury Selivanove0b23092016-02-11 10:26:27 -05003871
3872 if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) {
3873 return fast_floor_div((PyLongObject*)a, (PyLongObject*)b);
3874 }
3875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
3877 div = NULL;
3878 return (PyObject *)div;
Guido van Rossume32e0141992-01-19 16:31:05 +00003879}
3880
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003881/* PyLong/PyLong -> float, with correctly rounded result. */
3882
3883#define MANT_DIG_DIGITS (DBL_MANT_DIG / PyLong_SHIFT)
3884#define MANT_DIG_BITS (DBL_MANT_DIG % PyLong_SHIFT)
3885
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003886static PyObject *
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003887long_true_divide(PyObject *v, PyObject *w)
Tim Peters20dab9f2001-09-04 05:31:47 +00003888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003889 PyLongObject *a, *b, *x;
3890 Py_ssize_t a_size, b_size, shift, extra_bits, diff, x_size, x_bits;
3891 digit mask, low;
3892 int inexact, negate, a_is_small, b_is_small;
3893 double dx, result;
Tim Peterse2a60002001-09-04 06:17:36 +00003894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 CHECK_BINOP(v, w);
3896 a = (PyLongObject *)v;
3897 b = (PyLongObject *)w;
Tim Peterse2a60002001-09-04 06:17:36 +00003898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 /*
3900 Method in a nutshell:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 0. reduce to case a, b > 0; filter out obvious underflow/overflow
3903 1. choose a suitable integer 'shift'
3904 2. use integer arithmetic to compute x = floor(2**-shift*a/b)
3905 3. adjust x for correct rounding
3906 4. convert x to a double dx with the same value
3907 5. return ldexp(dx, shift).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 In more detail:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 0. For any a, a/0 raises ZeroDivisionError; for nonzero b, 0/b
3912 returns either 0.0 or -0.0, depending on the sign of b. For a and
3913 b both nonzero, ignore signs of a and b, and add the sign back in
3914 at the end. Now write a_bits and b_bits for the bit lengths of a
3915 and b respectively (that is, a_bits = 1 + floor(log_2(a)); likewise
3916 for b). Then
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 2**(a_bits - b_bits - 1) < a/b < 2**(a_bits - b_bits + 1).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 So if a_bits - b_bits > DBL_MAX_EXP then a/b > 2**DBL_MAX_EXP and
3921 so overflows. Similarly, if a_bits - b_bits < DBL_MIN_EXP -
3922 DBL_MANT_DIG - 1 then a/b underflows to 0. With these cases out of
3923 the way, we can assume that
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 DBL_MIN_EXP - DBL_MANT_DIG - 1 <= a_bits - b_bits <= DBL_MAX_EXP.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 1. The integer 'shift' is chosen so that x has the right number of
3928 bits for a double, plus two or three extra bits that will be used
3929 in the rounding decisions. Writing a_bits and b_bits for the
3930 number of significant bits in a and b respectively, a
3931 straightforward formula for shift is:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 shift = a_bits - b_bits - DBL_MANT_DIG - 2
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 This is fine in the usual case, but if a/b is smaller than the
3936 smallest normal float then it can lead to double rounding on an
3937 IEEE 754 platform, giving incorrectly rounded results. So we
3938 adjust the formula slightly. The actual formula used is:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 shift = MAX(a_bits - b_bits, DBL_MIN_EXP) - DBL_MANT_DIG - 2
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 2. The quantity x is computed by first shifting a (left -shift bits
3943 if shift <= 0, right shift bits if shift > 0) and then dividing by
3944 b. For both the shift and the division, we keep track of whether
3945 the result is inexact, in a flag 'inexact'; this information is
3946 needed at the rounding stage.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 With the choice of shift above, together with our assumption that
3949 a_bits - b_bits >= DBL_MIN_EXP - DBL_MANT_DIG - 1, it follows
3950 that x >= 1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 3. Now x * 2**shift <= a/b < (x+1) * 2**shift. We want to replace
3953 this with an exactly representable float of the form
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 round(x/2**extra_bits) * 2**(extra_bits+shift).
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 For float representability, we need x/2**extra_bits <
3958 2**DBL_MANT_DIG and extra_bits + shift >= DBL_MIN_EXP -
3959 DBL_MANT_DIG. This translates to the condition:
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 extra_bits >= MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 To round, we just modify the bottom digit of x in-place; this can
3964 end up giving a digit with value > PyLONG_MASK, but that's not a
3965 problem since digits can hold values up to 2*PyLONG_MASK+1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 With the original choices for shift above, extra_bits will always
3968 be 2 or 3. Then rounding under the round-half-to-even rule, we
3969 round up iff the most significant of the extra bits is 1, and
3970 either: (a) the computation of x in step 2 had an inexact result,
3971 or (b) at least one other of the extra bits is 1, or (c) the least
3972 significant bit of x (above those to be rounded) is 1.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 4. Conversion to a double is straightforward; all floating-point
3975 operations involved in the conversion are exact, so there's no
3976 danger of rounding errors.
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 5. Use ldexp(x, shift) to compute x*2**shift, the final result.
3979 The result will always be exactly representable as a double, except
3980 in the case that it overflows. To avoid dependence on the exact
3981 behaviour of ldexp on overflow, we check for overflow before
3982 applying ldexp. The result of ldexp is adjusted for sign before
3983 returning.
3984 */
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 /* Reduce to case where a and b are both positive. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02003987 a_size = Py_ABS(Py_SIZE(a));
3988 b_size = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 negate = (Py_SIZE(a) < 0) ^ (Py_SIZE(b) < 0);
3990 if (b_size == 0) {
3991 PyErr_SetString(PyExc_ZeroDivisionError,
3992 "division by zero");
3993 goto error;
3994 }
3995 if (a_size == 0)
3996 goto underflow_or_zero;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00003997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003998 /* Fast path for a and b small (exactly representable in a double).
3999 Relies on floating-point division being correctly rounded; results
4000 may be subject to double rounding on x86 machines that operate with
4001 the x87 FPU set to 64-bit precision. */
4002 a_is_small = a_size <= MANT_DIG_DIGITS ||
4003 (a_size == MANT_DIG_DIGITS+1 &&
4004 a->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4005 b_is_small = b_size <= MANT_DIG_DIGITS ||
4006 (b_size == MANT_DIG_DIGITS+1 &&
4007 b->ob_digit[MANT_DIG_DIGITS] >> MANT_DIG_BITS == 0);
4008 if (a_is_small && b_is_small) {
4009 double da, db;
4010 da = a->ob_digit[--a_size];
4011 while (a_size > 0)
4012 da = da * PyLong_BASE + a->ob_digit[--a_size];
4013 db = b->ob_digit[--b_size];
4014 while (b_size > 0)
4015 db = db * PyLong_BASE + b->ob_digit[--b_size];
4016 result = da / db;
4017 goto success;
4018 }
Tim Peterse2a60002001-09-04 06:17:36 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 /* Catch obvious cases of underflow and overflow */
4021 diff = a_size - b_size;
4022 if (diff > PY_SSIZE_T_MAX/PyLong_SHIFT - 1)
4023 /* Extreme overflow */
4024 goto overflow;
4025 else if (diff < 1 - PY_SSIZE_T_MAX/PyLong_SHIFT)
4026 /* Extreme underflow */
4027 goto underflow_or_zero;
4028 /* Next line is now safe from overflowing a Py_ssize_t */
4029 diff = diff * PyLong_SHIFT + bits_in_digit(a->ob_digit[a_size - 1]) -
4030 bits_in_digit(b->ob_digit[b_size - 1]);
4031 /* Now diff = a_bits - b_bits. */
4032 if (diff > DBL_MAX_EXP)
4033 goto overflow;
4034 else if (diff < DBL_MIN_EXP - DBL_MANT_DIG - 1)
4035 goto underflow_or_zero;
Tim Peterse2a60002001-09-04 06:17:36 +00004036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 /* Choose value for shift; see comments for step 1 above. */
Victor Stinner640c35c2013-06-04 23:14:37 +02004038 shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 inexact = 0;
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 /* x = abs(a * 2**-shift) */
4043 if (shift <= 0) {
4044 Py_ssize_t i, shift_digits = -shift / PyLong_SHIFT;
4045 digit rem;
4046 /* x = a << -shift */
4047 if (a_size >= PY_SSIZE_T_MAX - 1 - shift_digits) {
4048 /* In practice, it's probably impossible to end up
4049 here. Both a and b would have to be enormous,
4050 using close to SIZE_T_MAX bytes of memory each. */
4051 PyErr_SetString(PyExc_OverflowError,
Mark Dickinson22b20182010-05-10 21:27:53 +00004052 "intermediate overflow during division");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 goto error;
4054 }
4055 x = _PyLong_New(a_size + shift_digits + 1);
4056 if (x == NULL)
4057 goto error;
4058 for (i = 0; i < shift_digits; i++)
4059 x->ob_digit[i] = 0;
4060 rem = v_lshift(x->ob_digit + shift_digits, a->ob_digit,
4061 a_size, -shift % PyLong_SHIFT);
4062 x->ob_digit[a_size + shift_digits] = rem;
4063 }
4064 else {
4065 Py_ssize_t shift_digits = shift / PyLong_SHIFT;
4066 digit rem;
4067 /* x = a >> shift */
4068 assert(a_size >= shift_digits);
4069 x = _PyLong_New(a_size - shift_digits);
4070 if (x == NULL)
4071 goto error;
4072 rem = v_rshift(x->ob_digit, a->ob_digit + shift_digits,
4073 a_size - shift_digits, shift % PyLong_SHIFT);
4074 /* set inexact if any of the bits shifted out is nonzero */
4075 if (rem)
4076 inexact = 1;
4077 while (!inexact && shift_digits > 0)
4078 if (a->ob_digit[--shift_digits])
4079 inexact = 1;
4080 }
4081 long_normalize(x);
4082 x_size = Py_SIZE(x);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 /* x //= b. If the remainder is nonzero, set inexact. We own the only
4085 reference to x, so it's safe to modify it in-place. */
4086 if (b_size == 1) {
4087 digit rem = inplace_divrem1(x->ob_digit, x->ob_digit, x_size,
4088 b->ob_digit[0]);
4089 long_normalize(x);
4090 if (rem)
4091 inexact = 1;
4092 }
4093 else {
4094 PyLongObject *div, *rem;
4095 div = x_divrem(x, b, &rem);
4096 Py_DECREF(x);
4097 x = div;
4098 if (x == NULL)
4099 goto error;
4100 if (Py_SIZE(rem))
4101 inexact = 1;
4102 Py_DECREF(rem);
4103 }
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004104 x_size = Py_ABS(Py_SIZE(x));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 assert(x_size > 0); /* result of division is never zero */
4106 x_bits = (x_size-1)*PyLong_SHIFT+bits_in_digit(x->ob_digit[x_size-1]);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 /* The number of extra bits that have to be rounded away. */
Victor Stinner640c35c2013-06-04 23:14:37 +02004109 extra_bits = Py_MAX(x_bits, DBL_MIN_EXP - shift) - DBL_MANT_DIG;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 assert(extra_bits == 2 || extra_bits == 3);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 /* Round by directly modifying the low digit of x. */
4113 mask = (digit)1 << (extra_bits - 1);
4114 low = x->ob_digit[0] | inexact;
Mark Dickinsondc590a42016-08-21 10:23:23 +01004115 if ((low & mask) && (low & (3U*mask-1U)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 low += mask;
Mark Dickinsondc590a42016-08-21 10:23:23 +01004117 x->ob_digit[0] = low & ~(2U*mask-1U);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 /* Convert x to a double dx; the conversion is exact. */
4120 dx = x->ob_digit[--x_size];
4121 while (x_size > 0)
4122 dx = dx * PyLong_BASE + x->ob_digit[--x_size];
4123 Py_DECREF(x);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 /* Check whether ldexp result will overflow a double. */
4126 if (shift + x_bits >= DBL_MAX_EXP &&
4127 (shift + x_bits > DBL_MAX_EXP || dx == ldexp(1.0, (int)x_bits)))
4128 goto overflow;
4129 result = ldexp(dx, (int)shift);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004130
4131 success:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 return PyFloat_FromDouble(negate ? -result : result);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004133
4134 underflow_or_zero:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 return PyFloat_FromDouble(negate ? -0.0 : 0.0);
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004136
4137 overflow:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 PyErr_SetString(PyExc_OverflowError,
4139 "integer division result too large for a float");
Mark Dickinsoncbb62742009-12-27 15:09:50 +00004140 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 return NULL;
Tim Peters20dab9f2001-09-04 05:31:47 +00004142}
4143
4144static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004145long_mod(PyObject *a, PyObject *b)
Guido van Rossume32e0141992-01-19 16:31:05 +00004146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 PyLongObject *mod;
Neil Schemenauerba872e22001-01-04 01:46:03 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 CHECK_BINOP(a, b);
4150
Yury Selivanove0b23092016-02-11 10:26:27 -05004151 if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) {
4152 return fast_mod((PyLongObject*)a, (PyLongObject*)b);
4153 }
4154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004155 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
4156 mod = NULL;
4157 return (PyObject *)mod;
Guido van Rossume32e0141992-01-19 16:31:05 +00004158}
4159
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004160static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004161long_divmod(PyObject *a, PyObject *b)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 PyLongObject *div, *mod;
4164 PyObject *z;
Neil Schemenauerba872e22001-01-04 01:46:03 +00004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00004167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
4169 return NULL;
4170 }
4171 z = PyTuple_New(2);
4172 if (z != NULL) {
Sergey Fedoseevea6207d2019-02-21 15:01:11 +05004173 PyTuple_SET_ITEM(z, 0, (PyObject *) div);
4174 PyTuple_SET_ITEM(z, 1, (PyObject *) mod);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 }
4176 else {
4177 Py_DECREF(div);
4178 Py_DECREF(mod);
4179 }
4180 return z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004181}
4182
Tim Peters47e52ee2004-08-30 02:44:38 +00004183/* pow(v, w, x) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004184static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00004185long_pow(PyObject *v, PyObject *w, PyObject *x)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
4188 int negativeOutput = 0; /* if x<0 return negative output */
Neil Schemenauerba872e22001-01-04 01:46:03 +00004189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 PyLongObject *z = NULL; /* accumulated result */
4191 Py_ssize_t i, j, k; /* counters */
4192 PyLongObject *temp = NULL;
Tim Peters47e52ee2004-08-30 02:44:38 +00004193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 /* 5-ary values. If the exponent is large enough, table is
4195 * precomputed so that table[i] == a**i % c for i in range(32).
4196 */
4197 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4198 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
Tim Peters47e52ee2004-08-30 02:44:38 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 /* a, b, c = v, w, x */
4201 CHECK_BINOP(v, w);
4202 a = (PyLongObject*)v; Py_INCREF(a);
4203 b = (PyLongObject*)w; Py_INCREF(b);
4204 if (PyLong_Check(x)) {
4205 c = (PyLongObject *)x;
4206 Py_INCREF(x);
4207 }
4208 else if (x == Py_None)
4209 c = NULL;
4210 else {
4211 Py_DECREF(a);
4212 Py_DECREF(b);
Brian Curtindfc80e32011-08-10 20:28:54 -05004213 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 }
Tim Peters4c483c42001-09-05 06:24:58 +00004215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (Py_SIZE(b) < 0) { /* if exponent is negative */
4217 if (c) {
Mark Dickinson0c346d82014-04-11 14:34:40 -04004218 PyErr_SetString(PyExc_ValueError, "pow() 2nd argument "
Mark Dickinson22b20182010-05-10 21:27:53 +00004219 "cannot be negative when 3rd argument specified");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 goto Error;
4221 }
4222 else {
4223 /* else return a float. This works because we know
4224 that this calls float_pow() which converts its
4225 arguments to double. */
4226 Py_DECREF(a);
4227 Py_DECREF(b);
4228 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
4229 }
4230 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 if (c) {
4233 /* if modulus == 0:
4234 raise ValueError() */
4235 if (Py_SIZE(c) == 0) {
4236 PyErr_SetString(PyExc_ValueError,
4237 "pow() 3rd argument cannot be 0");
4238 goto Error;
4239 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 /* if modulus < 0:
4242 negativeOutput = True
4243 modulus = -modulus */
4244 if (Py_SIZE(c) < 0) {
4245 negativeOutput = 1;
4246 temp = (PyLongObject *)_PyLong_Copy(c);
4247 if (temp == NULL)
4248 goto Error;
4249 Py_DECREF(c);
4250 c = temp;
4251 temp = NULL;
Victor Stinner8aed6f12013-07-17 22:31:17 +02004252 _PyLong_Negate(&c);
4253 if (c == NULL)
4254 goto Error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 /* if modulus == 1:
4258 return 0 */
4259 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
4260 z = (PyLongObject *)PyLong_FromLong(0L);
4261 goto Done;
4262 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004263
Tim Peters81a93152013-10-05 16:53:52 -05004264 /* Reduce base by modulus in some cases:
4265 1. If base < 0. Forcing the base non-negative makes things easier.
4266 2. If base is obviously larger than the modulus. The "small
4267 exponent" case later can multiply directly by base repeatedly,
4268 while the "large exponent" case multiplies directly by base 31
4269 times. It can be unboundedly faster to multiply by
4270 base % modulus instead.
4271 We could _always_ do this reduction, but l_divmod() isn't cheap,
4272 so we only do it when it buys something. */
4273 if (Py_SIZE(a) < 0 || Py_SIZE(a) > Py_SIZE(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 if (l_divmod(a, c, NULL, &temp) < 0)
4275 goto Error;
4276 Py_DECREF(a);
4277 a = temp;
4278 temp = NULL;
4279 }
4280 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 /* At this point a, b, and c are guaranteed non-negative UNLESS
4283 c is NULL, in which case a may be negative. */
Tim Peters47e52ee2004-08-30 02:44:38 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 z = (PyLongObject *)PyLong_FromLong(1L);
4286 if (z == NULL)
4287 goto Error;
Tim Peters47e52ee2004-08-30 02:44:38 +00004288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 /* Perform a modular reduction, X = X % c, but leave X alone if c
4290 * is NULL.
4291 */
4292#define REDUCE(X) \
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004293 do { \
4294 if (c != NULL) { \
4295 if (l_divmod(X, c, NULL, &temp) < 0) \
4296 goto Error; \
4297 Py_XDECREF(X); \
4298 X = temp; \
4299 temp = NULL; \
4300 } \
4301 } while(0)
Tim Peters47e52ee2004-08-30 02:44:38 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 /* Multiply two values, then reduce the result:
4304 result = X*Y % c. If c is NULL, skip the mod. */
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004305#define MULT(X, Y, result) \
4306 do { \
4307 temp = (PyLongObject *)long_mul(X, Y); \
4308 if (temp == NULL) \
4309 goto Error; \
4310 Py_XDECREF(result); \
4311 result = temp; \
4312 temp = NULL; \
4313 REDUCE(result); \
4314 } while(0)
Tim Peters47e52ee2004-08-30 02:44:38 +00004315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
4317 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
4318 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
4319 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
4320 digit bi = b->ob_digit[i];
Tim Peters47e52ee2004-08-30 02:44:38 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 for (j = (digit)1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004323 MULT(z, z, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 if (bi & j)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004325 MULT(z, a, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 }
4327 }
4328 }
4329 else {
4330 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
4331 Py_INCREF(z); /* still holds 1L */
4332 table[0] = z;
4333 for (i = 1; i < 32; ++i)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004334 MULT(table[i-1], a, table[i]);
Tim Peters47e52ee2004-08-30 02:44:38 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
4337 const digit bi = b->ob_digit[i];
Tim Peters47e52ee2004-08-30 02:44:38 +00004338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
4340 const int index = (bi >> j) & 0x1f;
4341 for (k = 0; k < 5; ++k)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004342 MULT(z, z, z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 if (index)
Mark Dickinsoncdd01d22010-05-10 21:37:34 +00004344 MULT(z, table[index], z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 }
4346 }
4347 }
Tim Peters47e52ee2004-08-30 02:44:38 +00004348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 if (negativeOutput && (Py_SIZE(z) != 0)) {
4350 temp = (PyLongObject *)long_sub(z, c);
4351 if (temp == NULL)
4352 goto Error;
4353 Py_DECREF(z);
4354 z = temp;
4355 temp = NULL;
4356 }
4357 goto Done;
Tim Peters47e52ee2004-08-30 02:44:38 +00004358
Mark Dickinson22b20182010-05-10 21:27:53 +00004359 Error:
Victor Stinner8aed6f12013-07-17 22:31:17 +02004360 Py_CLEAR(z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 /* fall through */
Mark Dickinson22b20182010-05-10 21:27:53 +00004362 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
4364 for (i = 0; i < 32; ++i)
4365 Py_XDECREF(table[i]);
4366 }
4367 Py_DECREF(a);
4368 Py_DECREF(b);
4369 Py_XDECREF(c);
4370 Py_XDECREF(temp);
4371 return (PyObject *)z;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004372}
4373
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004374static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004375long_invert(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 /* Implement ~x as -(x+1) */
4378 PyLongObject *x;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004379 if (Py_ABS(Py_SIZE(v)) <=1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
Serhiy Storchakaba85d692017-03-30 09:09:41 +03004381 x = (PyLongObject *) long_add(v, (PyLongObject *)_PyLong_One);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004382 if (x == NULL)
4383 return NULL;
Mark Dickinson583c6e82016-08-29 16:40:29 +01004384 _PyLong_Negate(&x);
4385 /* No need for maybe_small_long here, since any small
4386 longs will have been caught in the Py_SIZE <= 1 fast path. */
4387 return (PyObject *)x;
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004388}
4389
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004390static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004391long_neg(PyLongObject *v)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyLongObject *z;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004394 if (Py_ABS(Py_SIZE(v)) <= 1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 return PyLong_FromLong(-MEDIUM_VALUE(v));
4396 z = (PyLongObject *)_PyLong_Copy(v);
4397 if (z != NULL)
4398 Py_SIZE(z) = -(Py_SIZE(v));
4399 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004400}
4401
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004402static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004403long_abs(PyLongObject *v)
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 if (Py_SIZE(v) < 0)
4406 return long_neg(v);
4407 else
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03004408 return long_long((PyObject *)v);
Guido van Rossumedcc38a1991-05-05 20:09:44 +00004409}
4410
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004411static int
Jack Diederich4dafcc42006-11-28 19:15:13 +00004412long_bool(PyLongObject *v)
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 return Py_SIZE(v) != 0;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004415}
4416
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004417/* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
4418static int
4419divmod_shift(PyLongObject *shiftby, Py_ssize_t *wordshift, digit *remshift)
4420{
4421 assert(PyLong_Check((PyObject *)shiftby));
4422 assert(Py_SIZE(shiftby) >= 0);
4423 Py_ssize_t lshiftby = PyLong_AsSsize_t((PyObject *)shiftby);
4424 if (lshiftby >= 0) {
4425 *wordshift = lshiftby / PyLong_SHIFT;
4426 *remshift = lshiftby % PyLong_SHIFT;
4427 return 0;
4428 }
4429 /* PyLong_Check(shiftby) is true and Py_SIZE(shiftby) >= 0, so it must
4430 be that PyLong_AsSsize_t raised an OverflowError. */
4431 assert(PyErr_ExceptionMatches(PyExc_OverflowError));
4432 PyErr_Clear();
4433 PyLongObject *wordshift_obj = divrem1(shiftby, PyLong_SHIFT, remshift);
4434 if (wordshift_obj == NULL) {
4435 return -1;
4436 }
4437 *wordshift = PyLong_AsSsize_t((PyObject *)wordshift_obj);
4438 Py_DECREF(wordshift_obj);
4439 if (*wordshift >= 0 && *wordshift < PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(digit)) {
4440 return 0;
4441 }
4442 PyErr_Clear();
4443 /* Clip the value. With such large wordshift the right shift
4444 returns 0 and the left shift raises an error in _PyLong_New(). */
4445 *wordshift = PY_SSIZE_T_MAX / sizeof(digit);
4446 *remshift = 0;
4447 return 0;
4448}
4449
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004450static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004451long_rshift(PyLongObject *a, PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 PyLongObject *z = NULL;
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004454 Py_ssize_t newsize, wordshift, hishift, i, j;
4455 digit loshift, lomask, himask;
Tim Peters5af4e6c2002-08-12 02:31:19 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00004458
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004459 if (Py_SIZE(b) < 0) {
4460 PyErr_SetString(PyExc_ValueError,
4461 "negative shift count");
4462 return NULL;
4463 }
4464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 if (Py_SIZE(a) < 0) {
4466 /* Right shifting negative numbers is harder */
4467 PyLongObject *a1, *a2;
4468 a1 = (PyLongObject *) long_invert(a);
4469 if (a1 == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004470 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 a2 = (PyLongObject *) long_rshift(a1, b);
4472 Py_DECREF(a1);
4473 if (a2 == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004474 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 z = (PyLongObject *) long_invert(a2);
4476 Py_DECREF(a2);
4477 }
4478 else {
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004479 if (divmod_shift(b, &wordshift, &loshift) < 0)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004480 return NULL;
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004481 newsize = Py_SIZE(a) - wordshift;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 if (newsize <= 0)
4483 return PyLong_FromLong(0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 hishift = PyLong_SHIFT - loshift;
4485 lomask = ((digit)1 << hishift) - 1;
4486 himask = PyLong_MASK ^ lomask;
4487 z = _PyLong_New(newsize);
4488 if (z == NULL)
Mark Dickinson92ca5352016-09-17 17:50:50 +01004489 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 for (i = 0, j = wordshift; i < newsize; i++, j++) {
4491 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
4492 if (i+1 < newsize)
Mark Dickinson22b20182010-05-10 21:27:53 +00004493 z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
Mark Dickinson92ca5352016-09-17 17:50:50 +01004495 z = maybe_small_long(long_normalize(z));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 }
Mark Dickinson92ca5352016-09-17 17:50:50 +01004497 return (PyObject *)z;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004498}
4499
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004500static PyObject *
Neil Schemenauerba872e22001-01-04 01:46:03 +00004501long_lshift(PyObject *v, PyObject *w)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 /* This version due to Tim Peters */
4504 PyLongObject *a = (PyLongObject*)v;
4505 PyLongObject *b = (PyLongObject*)w;
4506 PyLongObject *z = NULL;
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004507 Py_ssize_t oldsize, newsize, wordshift, i, j;
4508 digit remshift;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 twodigits accum;
Tim Peters5af4e6c2002-08-12 02:31:19 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 CHECK_BINOP(a, b);
Neil Schemenauerba872e22001-01-04 01:46:03 +00004512
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004513 if (Py_SIZE(b) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 PyErr_SetString(PyExc_ValueError, "negative shift count");
Victor Stinner8aed6f12013-07-17 22:31:17 +02004515 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 }
Mark Dickinson82a95272016-08-29 19:27:06 +01004517 if (Py_SIZE(a) == 0) {
4518 return PyLong_FromLong(0);
4519 }
4520
Serhiy Storchaka918403c2017-03-30 09:47:07 +03004521 if (divmod_shift(b, &wordshift, &remshift) < 0)
4522 return NULL;
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004523 oldsize = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 newsize = oldsize + wordshift;
4525 if (remshift)
4526 ++newsize;
4527 z = _PyLong_New(newsize);
4528 if (z == NULL)
Victor Stinner8aed6f12013-07-17 22:31:17 +02004529 return NULL;
4530 if (Py_SIZE(a) < 0) {
4531 assert(Py_REFCNT(z) == 1);
4532 Py_SIZE(z) = -Py_SIZE(z);
4533 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 for (i = 0; i < wordshift; i++)
4535 z->ob_digit[i] = 0;
4536 accum = 0;
4537 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
4538 accum |= (twodigits)a->ob_digit[j] << remshift;
4539 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
4540 accum >>= PyLong_SHIFT;
4541 }
4542 if (remshift)
4543 z->ob_digit[newsize-1] = (digit)accum;
4544 else
4545 assert(!accum);
4546 z = long_normalize(z);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 return (PyObject *) maybe_small_long(z);
Guido van Rossumc6913e71991-11-19 20:26:46 +00004548}
4549
Mark Dickinson27a87a22009-10-25 20:43:34 +00004550/* Compute two's complement of digit vector a[0:m], writing result to
4551 z[0:m]. The digit vector a need not be normalized, but should not
4552 be entirely zero. a and z may point to the same digit vector. */
4553
4554static void
4555v_complement(digit *z, digit *a, Py_ssize_t m)
4556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 Py_ssize_t i;
4558 digit carry = 1;
4559 for (i = 0; i < m; ++i) {
4560 carry += a[i] ^ PyLong_MASK;
4561 z[i] = carry & PyLong_MASK;
4562 carry >>= PyLong_SHIFT;
4563 }
4564 assert(carry == 0);
Mark Dickinson27a87a22009-10-25 20:43:34 +00004565}
Guido van Rossum4c260ff1992-01-14 18:36:43 +00004566
4567/* Bitwise and/xor/or operations */
4568
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004569static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004570long_bitwise(PyLongObject *a,
Benjamin Peterson513762f2012-12-26 16:43:33 -06004571 char op, /* '&', '|', '^' */
Mark Dickinson22b20182010-05-10 21:27:53 +00004572 PyLongObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 int nega, negb, negz;
4575 Py_ssize_t size_a, size_b, size_z, i;
4576 PyLongObject *z;
Tim Peters5af4e6c2002-08-12 02:31:19 +00004577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 /* Bitwise operations for negative numbers operate as though
4579 on a two's complement representation. So convert arguments
4580 from sign-magnitude to two's complement, and convert the
4581 result back to sign-magnitude at the end. */
Mark Dickinson27a87a22009-10-25 20:43:34 +00004582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 /* If a is negative, replace it by its two's complement. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004584 size_a = Py_ABS(Py_SIZE(a));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 nega = Py_SIZE(a) < 0;
4586 if (nega) {
4587 z = _PyLong_New(size_a);
4588 if (z == NULL)
4589 return NULL;
4590 v_complement(z->ob_digit, a->ob_digit, size_a);
4591 a = z;
4592 }
4593 else
4594 /* Keep reference count consistent. */
4595 Py_INCREF(a);
Mark Dickinson27a87a22009-10-25 20:43:34 +00004596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 /* Same for b. */
Victor Stinner45e8e2f2014-05-14 17:24:35 +02004598 size_b = Py_ABS(Py_SIZE(b));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 negb = Py_SIZE(b) < 0;
4600 if (negb) {
4601 z = _PyLong_New(size_b);
4602 if (z == NULL) {
4603 Py_DECREF(a);
4604 return NULL;
4605 }
4606 v_complement(z->ob_digit, b->ob_digit, size_b);
4607 b = z;
4608 }
4609 else
4610 Py_INCREF(b);
Tim Peters5af4e6c2002-08-12 02:31:19 +00004611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 /* Swap a and b if necessary to ensure size_a >= size_b. */
4613 if (size_a < size_b) {
4614 z = a; a = b; b = z;
4615 size_z = size_a; size_a = size_b; size_b = size_z;
4616 negz = nega; nega = negb; negb = negz;
4617 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 /* JRH: The original logic here was to allocate the result value (z)
4620 as the longer of the two operands. However, there are some cases
4621 where the result is guaranteed to be shorter than that: AND of two
4622 positives, OR of two negatives: use the shorter number. AND with
4623 mixed signs: use the positive number. OR with mixed signs: use the
4624 negative number.
4625 */
4626 switch (op) {
4627 case '^':
4628 negz = nega ^ negb;
4629 size_z = size_a;
4630 break;
4631 case '&':
4632 negz = nega & negb;
4633 size_z = negb ? size_a : size_b;
4634 break;
4635 case '|':
4636 negz = nega | negb;
4637 size_z = negb ? size_b : size_a;
4638 break;
4639 default:
stratakisa10d4262019-03-18 18:59:20 +01004640 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 }
Guido van Rossumbd3a5271998-08-11 15:04:47 +00004642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 /* We allow an extra digit if z is negative, to make sure that
4644 the final two's complement of z doesn't overflow. */
4645 z = _PyLong_New(size_z + negz);
4646 if (z == NULL) {
4647 Py_DECREF(a);
4648 Py_DECREF(b);
4649 return NULL;
4650 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 /* Compute digits for overlap of a and b. */
4653 switch(op) {
4654 case '&':
4655 for (i = 0; i < size_b; ++i)
4656 z->ob_digit[i] = a->ob_digit[i] & b->ob_digit[i];
4657 break;
4658 case '|':
4659 for (i = 0; i < size_b; ++i)
4660 z->ob_digit[i] = a->ob_digit[i] | b->ob_digit[i];
4661 break;
4662 case '^':
4663 for (i = 0; i < size_b; ++i)
4664 z->ob_digit[i] = a->ob_digit[i] ^ b->ob_digit[i];
4665 break;
4666 default:
stratakisa10d4262019-03-18 18:59:20 +01004667 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 }
Mark Dickinson27a87a22009-10-25 20:43:34 +00004669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004670 /* Copy any remaining digits of a, inverting if necessary. */
4671 if (op == '^' && negb)
4672 for (; i < size_z; ++i)
4673 z->ob_digit[i] = a->ob_digit[i] ^ PyLong_MASK;
4674 else if (i < size_z)
4675 memcpy(&z->ob_digit[i], &a->ob_digit[i],
4676 (size_z-i)*sizeof(digit));
Mark Dickinson27a87a22009-10-25 20:43:34 +00004677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 /* Complement result if negative. */
4679 if (negz) {
4680 Py_SIZE(z) = -(Py_SIZE(z));
4681 z->ob_digit[size_z] = PyLong_MASK;
4682 v_complement(z->ob_digit, z->ob_digit, size_z+1);
4683 }
Tim Peters5af4e6c2002-08-12 02:31:19 +00004684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 Py_DECREF(a);
4686 Py_DECREF(b);
4687 return (PyObject *)maybe_small_long(long_normalize(z));
Guido van Rossumc6913e71991-11-19 20:26:46 +00004688}
4689
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004690static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004691long_and(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 PyObject *c;
4694 CHECK_BINOP(a, b);
4695 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
4696 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004697}
4698
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004699static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004700long_xor(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004702 PyObject *c;
4703 CHECK_BINOP(a, b);
4704 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
4705 return c;
Guido van Rossumc6913e71991-11-19 20:26:46 +00004706}
4707
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004708static PyObject *
Martin v. Löwisda86fcc2007-09-10 07:59:54 +00004709long_or(PyObject *a, PyObject *b)
Guido van Rossumc6913e71991-11-19 20:26:46 +00004710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 PyObject *c;
4712 CHECK_BINOP(a, b);
4713 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
4714 return c;
Guido van Rossum23d6f0e1991-05-14 12:06:49 +00004715}
4716
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004717static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03004718long_long(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 if (PyLong_CheckExact(v))
4721 Py_INCREF(v);
4722 else
4723 v = _PyLong_Copy((PyLongObject *)v);
4724 return v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004725}
4726
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004727PyObject *
4728_PyLong_GCD(PyObject *aarg, PyObject *barg)
4729{
4730 PyLongObject *a, *b, *c = NULL, *d = NULL, *r;
4731 stwodigits x, y, q, s, t, c_carry, d_carry;
4732 stwodigits A, B, C, D, T;
4733 int nbits, k;
4734 Py_ssize_t size_a, size_b, alloc_a, alloc_b;
4735 digit *a_digit, *b_digit, *c_digit, *d_digit, *a_end, *b_end;
4736
4737 a = (PyLongObject *)aarg;
4738 b = (PyLongObject *)barg;
4739 size_a = Py_SIZE(a);
4740 size_b = Py_SIZE(b);
4741 if (-2 <= size_a && size_a <= 2 && -2 <= size_b && size_b <= 2) {
4742 Py_INCREF(a);
4743 Py_INCREF(b);
4744 goto simple;
4745 }
4746
4747 /* Initial reduction: make sure that 0 <= b <= a. */
4748 a = (PyLongObject *)long_abs(a);
4749 if (a == NULL)
4750 return NULL;
4751 b = (PyLongObject *)long_abs(b);
4752 if (b == NULL) {
4753 Py_DECREF(a);
4754 return NULL;
4755 }
4756 if (long_compare(a, b) < 0) {
4757 r = a;
4758 a = b;
4759 b = r;
4760 }
4761 /* We now own references to a and b */
4762
4763 alloc_a = Py_SIZE(a);
4764 alloc_b = Py_SIZE(b);
4765 /* reduce until a fits into 2 digits */
4766 while ((size_a = Py_SIZE(a)) > 2) {
4767 nbits = bits_in_digit(a->ob_digit[size_a-1]);
4768 /* extract top 2*PyLong_SHIFT bits of a into x, along with
4769 corresponding bits of b into y */
4770 size_b = Py_SIZE(b);
4771 assert(size_b <= size_a);
4772 if (size_b == 0) {
4773 if (size_a < alloc_a) {
4774 r = (PyLongObject *)_PyLong_Copy(a);
4775 Py_DECREF(a);
4776 }
4777 else
4778 r = a;
4779 Py_DECREF(b);
4780 Py_XDECREF(c);
4781 Py_XDECREF(d);
4782 return (PyObject *)r;
4783 }
4784 x = (((twodigits)a->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits)) |
4785 ((twodigits)a->ob_digit[size_a-2] << (PyLong_SHIFT-nbits)) |
4786 (a->ob_digit[size_a-3] >> nbits));
4787
4788 y = ((size_b >= size_a - 2 ? b->ob_digit[size_a-3] >> nbits : 0) |
4789 (size_b >= size_a - 1 ? (twodigits)b->ob_digit[size_a-2] << (PyLong_SHIFT-nbits) : 0) |
4790 (size_b >= size_a ? (twodigits)b->ob_digit[size_a-1] << (2*PyLong_SHIFT-nbits) : 0));
4791
4792 /* inner loop of Lehmer's algorithm; A, B, C, D never grow
4793 larger than PyLong_MASK during the algorithm. */
4794 A = 1; B = 0; C = 0; D = 1;
4795 for (k=0;; k++) {
4796 if (y-C == 0)
4797 break;
4798 q = (x+(A-1))/(y-C);
4799 s = B+q*D;
4800 t = x-q*y;
4801 if (s > t)
4802 break;
4803 x = y; y = t;
4804 t = A+q*C; A = D; B = C; C = s; D = t;
4805 }
4806
4807 if (k == 0) {
4808 /* no progress; do a Euclidean step */
4809 if (l_divmod(a, b, NULL, &r) < 0)
4810 goto error;
4811 Py_DECREF(a);
4812 a = b;
4813 b = r;
4814 alloc_a = alloc_b;
4815 alloc_b = Py_SIZE(b);
4816 continue;
4817 }
4818
4819 /*
4820 a, b = A*b-B*a, D*a-C*b if k is odd
4821 a, b = A*a-B*b, D*b-C*a if k is even
4822 */
4823 if (k&1) {
4824 T = -A; A = -B; B = T;
4825 T = -C; C = -D; D = T;
4826 }
4827 if (c != NULL)
4828 Py_SIZE(c) = size_a;
4829 else if (Py_REFCNT(a) == 1) {
4830 Py_INCREF(a);
4831 c = a;
4832 }
4833 else {
4834 alloc_a = size_a;
4835 c = _PyLong_New(size_a);
4836 if (c == NULL)
4837 goto error;
4838 }
4839
4840 if (d != NULL)
4841 Py_SIZE(d) = size_a;
4842 else if (Py_REFCNT(b) == 1 && size_a <= alloc_b) {
4843 Py_INCREF(b);
4844 d = b;
4845 Py_SIZE(d) = size_a;
4846 }
4847 else {
4848 alloc_b = size_a;
4849 d = _PyLong_New(size_a);
4850 if (d == NULL)
4851 goto error;
4852 }
4853 a_end = a->ob_digit + size_a;
4854 b_end = b->ob_digit + size_b;
4855
4856 /* compute new a and new b in parallel */
4857 a_digit = a->ob_digit;
4858 b_digit = b->ob_digit;
4859 c_digit = c->ob_digit;
4860 d_digit = d->ob_digit;
4861 c_carry = 0;
4862 d_carry = 0;
4863 while (b_digit < b_end) {
4864 c_carry += (A * *a_digit) - (B * *b_digit);
4865 d_carry += (D * *b_digit++) - (C * *a_digit++);
4866 *c_digit++ = (digit)(c_carry & PyLong_MASK);
4867 *d_digit++ = (digit)(d_carry & PyLong_MASK);
4868 c_carry >>= PyLong_SHIFT;
4869 d_carry >>= PyLong_SHIFT;
4870 }
4871 while (a_digit < a_end) {
4872 c_carry += A * *a_digit;
4873 d_carry -= C * *a_digit++;
4874 *c_digit++ = (digit)(c_carry & PyLong_MASK);
4875 *d_digit++ = (digit)(d_carry & PyLong_MASK);
4876 c_carry >>= PyLong_SHIFT;
4877 d_carry >>= PyLong_SHIFT;
4878 }
4879 assert(c_carry == 0);
4880 assert(d_carry == 0);
4881
4882 Py_INCREF(c);
4883 Py_INCREF(d);
4884 Py_DECREF(a);
4885 Py_DECREF(b);
4886 a = long_normalize(c);
4887 b = long_normalize(d);
4888 }
4889 Py_XDECREF(c);
4890 Py_XDECREF(d);
4891
4892simple:
4893 assert(Py_REFCNT(a) > 0);
4894 assert(Py_REFCNT(b) > 0);
Victor Stinner5783fd22015-09-19 13:39:03 +02004895/* Issue #24999: use two shifts instead of ">> 2*PyLong_SHIFT" to avoid
4896 undefined behaviour when LONG_MAX type is smaller than 60 bits */
4897#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004898 /* a fits into a long, so b must too */
4899 x = PyLong_AsLong((PyObject *)a);
4900 y = PyLong_AsLong((PyObject *)b);
Benjamin Petersond953f8e2016-09-06 10:51:19 -07004901#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004902 x = PyLong_AsLongLong((PyObject *)a);
4903 y = PyLong_AsLongLong((PyObject *)b);
4904#else
4905# error "_PyLong_GCD"
4906#endif
4907 x = Py_ABS(x);
4908 y = Py_ABS(y);
4909 Py_DECREF(a);
4910 Py_DECREF(b);
4911
4912 /* usual Euclidean algorithm for longs */
4913 while (y != 0) {
4914 t = y;
4915 y = x % y;
4916 x = t;
4917 }
Victor Stinner5783fd22015-09-19 13:39:03 +02004918#if LONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004919 return PyLong_FromLong(x);
Benjamin Petersond953f8e2016-09-06 10:51:19 -07004920#elif PY_LLONG_MAX >> PyLong_SHIFT >> PyLong_SHIFT
Serhiy Storchaka48e47aa2015-05-13 00:19:51 +03004921 return PyLong_FromLongLong(x);
4922#else
4923# error "_PyLong_GCD"
4924#endif
4925
4926error:
4927 Py_DECREF(a);
4928 Py_DECREF(b);
4929 Py_XDECREF(c);
4930 Py_XDECREF(d);
4931 return NULL;
4932}
4933
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004934static PyObject *
Tim Peters9f688bf2000-07-07 15:53:28 +00004935long_float(PyObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004937 double result;
4938 result = PyLong_AsDouble(v);
4939 if (result == -1.0 && PyErr_Occurred())
4940 return NULL;
4941 return PyFloat_FromDouble(result);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004942}
4943
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004944static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02004945long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase);
4946
4947/*[clinic input]
4948@classmethod
4949int.__new__ as long_new
4950 x: object(c_default="NULL") = 0
4951 /
4952 base as obase: object(c_default="NULL") = 10
4953[clinic start generated code]*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +00004954
Tim Peters6d6c1a32001-08-02 04:15:00 +00004955static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02004956long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase)
4957/*[clinic end generated code: output=e47cfe777ab0f24c input=81c98f418af9eb6f]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +00004958{
Gregory P. Smitha689e522012-12-25 22:38:32 -08004959 Py_ssize_t base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 if (type != &PyLong_Type)
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02004962 return long_subtype_new(type, x, obase); /* Wimp out */
Serhiy Storchaka0b386d52012-12-28 09:42:11 +02004963 if (x == NULL) {
4964 if (obase != NULL) {
4965 PyErr_SetString(PyExc_TypeError,
4966 "int() missing string argument");
4967 return NULL;
4968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 return PyLong_FromLong(0L);
Serhiy Storchaka0b386d52012-12-28 09:42:11 +02004970 }
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00004971 if (obase == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004972 return PyNumber_Long(x);
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00004973
Gregory P. Smitha689e522012-12-25 22:38:32 -08004974 base = PyNumber_AsSsize_t(obase, NULL);
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00004975 if (base == -1 && PyErr_Occurred())
4976 return NULL;
Gregory P. Smitha689e522012-12-25 22:38:32 -08004977 if ((base != 0 && base < 2) || base > 36) {
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00004978 PyErr_SetString(PyExc_ValueError,
Sanyam Khurana28b62482017-11-14 03:19:26 +05304979 "int() base must be >= 2 and <= 36, or 0");
Mark Dickinsonf9a5a8e2010-05-26 20:07:58 +00004980 return NULL;
4981 }
4982
4983 if (PyUnicode_Check(x))
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004984 return PyLong_FromUnicodeObject(x, (int)base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004985 else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004986 char *string;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 if (PyByteArray_Check(x))
4988 string = PyByteArray_AS_STRING(x);
4989 else
4990 string = PyBytes_AS_STRING(x);
Serhiy Storchakaf6d0aee2013-08-03 20:55:06 +03004991 return _PyLong_FromBytes(string, Py_SIZE(x), (int)base);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004992 }
4993 else {
4994 PyErr_SetString(PyExc_TypeError,
Mark Dickinson22b20182010-05-10 21:27:53 +00004995 "int() can't convert non-string with explicit base");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 return NULL;
4997 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00004998}
4999
Serhiy Storchaka95949422013-08-27 19:40:23 +03005000/* Wimpy, slow approach to tp_new calls for subtypes of int:
5001 first create a regular int from whatever arguments we got,
Guido van Rossumbef14172001-08-29 15:47:46 +00005002 then allocate a subtype instance and initialize it from
Serhiy Storchaka95949422013-08-27 19:40:23 +03005003 the regular int. The regular int is then thrown away.
Guido van Rossumbef14172001-08-29 15:47:46 +00005004*/
5005static PyObject *
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005006long_subtype_new(PyTypeObject *type, PyObject *x, PyObject *obase)
Guido van Rossumbef14172001-08-29 15:47:46 +00005007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005008 PyLongObject *tmp, *newobj;
5009 Py_ssize_t i, n;
Guido van Rossumbef14172001-08-29 15:47:46 +00005010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005011 assert(PyType_IsSubtype(type, &PyLong_Type));
Serhiy Storchaka18b250f2017-03-19 08:51:07 +02005012 tmp = (PyLongObject *)long_new_impl(&PyLong_Type, x, obase);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 if (tmp == NULL)
5014 return NULL;
Serhiy Storchaka15095802015-11-25 15:47:01 +02005015 assert(PyLong_Check(tmp));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 n = Py_SIZE(tmp);
5017 if (n < 0)
5018 n = -n;
5019 newobj = (PyLongObject *)type->tp_alloc(type, n);
5020 if (newobj == NULL) {
5021 Py_DECREF(tmp);
5022 return NULL;
5023 }
5024 assert(PyLong_Check(newobj));
5025 Py_SIZE(newobj) = Py_SIZE(tmp);
5026 for (i = 0; i < n; i++)
5027 newobj->ob_digit[i] = tmp->ob_digit[i];
5028 Py_DECREF(tmp);
5029 return (PyObject *)newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00005030}
5031
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005032/*[clinic input]
5033int.__getnewargs__
5034[clinic start generated code]*/
5035
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005036static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005037int___getnewargs___impl(PyObject *self)
5038/*[clinic end generated code: output=839a49de3f00b61b input=5904770ab1fb8c75]*/
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005039{
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005040 return Py_BuildValue("(N)", _PyLong_Copy((PyLongObject *)self));
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005041}
5042
Guido van Rossumb43daf72007-08-01 18:08:08 +00005043static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005044long_get0(PyObject *Py_UNUSED(self), void *Py_UNUSED(context))
5045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005046 return PyLong_FromLong(0L);
Mark Dickinson6bf19002009-05-02 17:57:52 +00005047}
5048
5049static PyObject *
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005050long_get1(PyObject *Py_UNUSED(self), void *Py_UNUSED(ignored))
5051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005052 return PyLong_FromLong(1L);
Guido van Rossumb43daf72007-08-01 18:08:08 +00005053}
5054
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005055/*[clinic input]
5056int.__format__
5057
5058 format_spec: unicode
5059 /
5060[clinic start generated code]*/
5061
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005062static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005063int___format___impl(PyObject *self, PyObject *format_spec)
5064/*[clinic end generated code: output=b4929dee9ae18689 input=e31944a9b3e428b7]*/
Eric Smith8c663262007-08-25 02:26:07 +00005065{
Victor Stinnerd3f08822012-05-29 12:57:52 +02005066 _PyUnicodeWriter writer;
5067 int ret;
Eric Smith4a7d76d2008-05-30 18:10:19 +00005068
Victor Stinner8f674cc2013-04-17 23:02:17 +02005069 _PyUnicodeWriter_Init(&writer);
Victor Stinnerd3f08822012-05-29 12:57:52 +02005070 ret = _PyLong_FormatAdvancedWriter(
5071 &writer,
5072 self,
5073 format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
5074 if (ret == -1) {
5075 _PyUnicodeWriter_Dealloc(&writer);
5076 return NULL;
5077 }
5078 return _PyUnicodeWriter_Finish(&writer);
Eric Smith8c663262007-08-25 02:26:07 +00005079}
5080
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005081/* Return a pair (q, r) such that a = b * q + r, and
5082 abs(r) <= abs(b)/2, with equality possible only if q is even.
5083 In other words, q == a / b, rounded to the nearest integer using
5084 round-half-to-even. */
5085
5086PyObject *
Mark Dickinsonfa68a612010-06-07 18:47:09 +00005087_PyLong_DivmodNear(PyObject *a, PyObject *b)
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005088{
5089 PyLongObject *quo = NULL, *rem = NULL;
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005090 PyObject *twice_rem, *result, *temp;
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005091 int cmp, quo_is_odd, quo_is_neg;
5092
5093 /* Equivalent Python code:
5094
5095 def divmod_near(a, b):
5096 q, r = divmod(a, b)
5097 # round up if either r / b > 0.5, or r / b == 0.5 and q is odd.
5098 # The expression r / b > 0.5 is equivalent to 2 * r > b if b is
5099 # positive, 2 * r < b if b negative.
5100 greater_than_half = 2*r > b if b > 0 else 2*r < b
5101 exactly_half = 2*r == b
5102 if greater_than_half or exactly_half and q % 2 == 1:
5103 q += 1
5104 r -= b
5105 return q, r
5106
5107 */
5108 if (!PyLong_Check(a) || !PyLong_Check(b)) {
5109 PyErr_SetString(PyExc_TypeError,
5110 "non-integer arguments in division");
5111 return NULL;
5112 }
5113
5114 /* Do a and b have different signs? If so, quotient is negative. */
5115 quo_is_neg = (Py_SIZE(a) < 0) != (Py_SIZE(b) < 0);
5116
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005117 if (long_divrem((PyLongObject*)a, (PyLongObject*)b, &quo, &rem) < 0)
5118 goto error;
5119
5120 /* compare twice the remainder with the divisor, to see
5121 if we need to adjust the quotient and remainder */
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005122 twice_rem = long_lshift((PyObject *)rem, _PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005123 if (twice_rem == NULL)
5124 goto error;
5125 if (quo_is_neg) {
5126 temp = long_neg((PyLongObject*)twice_rem);
5127 Py_DECREF(twice_rem);
5128 twice_rem = temp;
5129 if (twice_rem == NULL)
5130 goto error;
5131 }
5132 cmp = long_compare((PyLongObject *)twice_rem, (PyLongObject *)b);
5133 Py_DECREF(twice_rem);
5134
5135 quo_is_odd = Py_SIZE(quo) != 0 && ((quo->ob_digit[0] & 1) != 0);
5136 if ((Py_SIZE(b) < 0 ? cmp < 0 : cmp > 0) || (cmp == 0 && quo_is_odd)) {
5137 /* fix up quotient */
5138 if (quo_is_neg)
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005139 temp = long_sub(quo, (PyLongObject *)_PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005140 else
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005141 temp = long_add(quo, (PyLongObject *)_PyLong_One);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005142 Py_DECREF(quo);
5143 quo = (PyLongObject *)temp;
5144 if (quo == NULL)
5145 goto error;
5146 /* and remainder */
5147 if (quo_is_neg)
5148 temp = long_add(rem, (PyLongObject *)b);
5149 else
5150 temp = long_sub(rem, (PyLongObject *)b);
5151 Py_DECREF(rem);
5152 rem = (PyLongObject *)temp;
5153 if (rem == NULL)
5154 goto error;
5155 }
5156
5157 result = PyTuple_New(2);
5158 if (result == NULL)
5159 goto error;
5160
5161 /* PyTuple_SET_ITEM steals references */
5162 PyTuple_SET_ITEM(result, 0, (PyObject *)quo);
5163 PyTuple_SET_ITEM(result, 1, (PyObject *)rem);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005164 return result;
5165
5166 error:
5167 Py_XDECREF(quo);
5168 Py_XDECREF(rem);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005169 return NULL;
5170}
5171
Eric Smith8c663262007-08-25 02:26:07 +00005172static PyObject *
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005173long_round(PyObject *self, PyObject *args)
5174{
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005175 PyObject *o_ndigits=NULL, *temp, *result, *ndigits;
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005176
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005177 /* To round an integer m to the nearest 10**n (n positive), we make use of
5178 * the divmod_near operation, defined by:
5179 *
5180 * divmod_near(a, b) = (q, r)
5181 *
5182 * where q is the nearest integer to the quotient a / b (the
5183 * nearest even integer in the case of a tie) and r == a - q * b.
5184 * Hence q * b = a - r is the nearest multiple of b to a,
5185 * preferring even multiples in the case of a tie.
5186 *
5187 * So the nearest multiple of 10**n to m is:
5188 *
5189 * m - divmod_near(m, 10**n)[1].
5190 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005191 if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
5192 return NULL;
5193 if (o_ndigits == NULL)
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005194 return long_long(self);
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005195
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005196 ndigits = PyNumber_Index(o_ndigits);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005197 if (ndigits == NULL)
5198 return NULL;
Mark Dickinson1124e712009-01-28 21:25:58 +00005199
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005200 /* if ndigits >= 0 then no rounding is necessary; return self unchanged */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005201 if (Py_SIZE(ndigits) >= 0) {
5202 Py_DECREF(ndigits);
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005203 return long_long(self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005204 }
Mark Dickinson1124e712009-01-28 21:25:58 +00005205
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005206 /* result = self - divmod_near(self, 10 ** -ndigits)[1] */
5207 temp = long_neg((PyLongObject*)ndigits);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 Py_DECREF(ndigits);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005209 ndigits = temp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005210 if (ndigits == NULL)
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005211 return NULL;
Mark Dickinson1124e712009-01-28 21:25:58 +00005212
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005213 result = PyLong_FromLong(10L);
5214 if (result == NULL) {
5215 Py_DECREF(ndigits);
5216 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 }
Mark Dickinson1124e712009-01-28 21:25:58 +00005218
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005219 temp = long_pow(result, ndigits, Py_None);
5220 Py_DECREF(ndigits);
5221 Py_DECREF(result);
5222 result = temp;
5223 if (result == NULL)
5224 return NULL;
5225
Mark Dickinsonfa68a612010-06-07 18:47:09 +00005226 temp = _PyLong_DivmodNear(self, result);
Mark Dickinson7f1bf802010-05-26 16:02:59 +00005227 Py_DECREF(result);
5228 result = temp;
5229 if (result == NULL)
5230 return NULL;
5231
5232 temp = long_sub((PyLongObject *)self,
5233 (PyLongObject *)PyTuple_GET_ITEM(result, 1));
5234 Py_DECREF(result);
5235 result = temp;
5236
5237 return result;
Guido van Rossum2fa33db2007-08-23 22:07:24 +00005238}
5239
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005240/*[clinic input]
5241int.__sizeof__ -> Py_ssize_t
5242
5243Returns size in memory, in bytes.
5244[clinic start generated code]*/
5245
5246static Py_ssize_t
5247int___sizeof___impl(PyObject *self)
5248/*[clinic end generated code: output=3303f008eaa6a0a5 input=9b51620c76fc4507]*/
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 Py_ssize_t res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005251
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005252 res = offsetof(PyLongObject, ob_digit) + Py_ABS(Py_SIZE(self))*sizeof(digit);
5253 return res;
Martin v. Löwis00709aa2008-06-04 14:18:43 +00005254}
5255
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005256/*[clinic input]
5257int.bit_length
5258
5259Number of bits necessary to represent self in binary.
5260
5261>>> bin(37)
5262'0b100101'
5263>>> (37).bit_length()
52646
5265[clinic start generated code]*/
5266
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005267static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005268int_bit_length_impl(PyObject *self)
5269/*[clinic end generated code: output=fc1977c9353d6a59 input=e4eb7a587e849a32]*/
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005271 PyLongObject *result, *x, *y;
Serhiy Storchakab2e64f92016-11-08 20:34:22 +02005272 Py_ssize_t ndigits;
5273 int msd_bits;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005274 digit msd;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005275
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005276 assert(self != NULL);
5277 assert(PyLong_Check(self));
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005278
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005279 ndigits = Py_ABS(Py_SIZE(self));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 if (ndigits == 0)
5281 return PyLong_FromLong(0);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005282
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005283 msd = ((PyLongObject *)self)->ob_digit[ndigits-1];
Serhiy Storchakab2e64f92016-11-08 20:34:22 +02005284 msd_bits = bits_in_digit(msd);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
5287 return PyLong_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 /* expression above may overflow; use Python integers instead */
5290 result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
5291 if (result == NULL)
5292 return NULL;
5293 x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
5294 if (x == NULL)
5295 goto error;
5296 y = (PyLongObject *)long_mul(result, x);
5297 Py_DECREF(x);
5298 if (y == NULL)
5299 goto error;
5300 Py_DECREF(result);
5301 result = y;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 x = (PyLongObject *)PyLong_FromLong((long)msd_bits);
5304 if (x == NULL)
5305 goto error;
5306 y = (PyLongObject *)long_add(result, x);
5307 Py_DECREF(x);
5308 if (y == NULL)
5309 goto error;
5310 Py_DECREF(result);
5311 result = y;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005313 return (PyObject *)result;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005314
Mark Dickinson22b20182010-05-10 21:27:53 +00005315 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 Py_DECREF(result);
5317 return NULL;
Mark Dickinson54bc1ec2008-12-17 16:19:07 +00005318}
5319
Christian Heimes53876d92008-04-19 00:31:39 +00005320#if 0
5321static PyObject *
5322long_is_finite(PyObject *v)
5323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005324 Py_RETURN_TRUE;
Christian Heimes53876d92008-04-19 00:31:39 +00005325}
5326#endif
5327
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005328/*[clinic input]
Lisa Roach5ac70432018-09-13 23:56:23 -07005329int.as_integer_ratio
5330
5331Return integer ratio.
5332
5333Return a pair of integers, whose ratio is exactly equal to the original int
5334and with a positive denominator.
5335
5336>>> (10).as_integer_ratio()
5337(10, 1)
5338>>> (-10).as_integer_ratio()
5339(-10, 1)
5340>>> (0).as_integer_ratio()
5341(0, 1)
5342[clinic start generated code]*/
5343
5344static PyObject *
5345int_as_integer_ratio_impl(PyObject *self)
5346/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
5347{
Raymond Hettinger00bc08e2018-09-14 01:00:11 -07005348 PyObject *ratio_tuple;
Serhiy Storchakab2e20252018-10-20 00:46:31 +03005349 PyObject *numerator = long_long(self);
Raymond Hettinger00bc08e2018-09-14 01:00:11 -07005350 if (numerator == NULL) {
5351 return NULL;
5352 }
5353 ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One);
5354 Py_DECREF(numerator);
5355 return ratio_tuple;
Lisa Roach5ac70432018-09-13 23:56:23 -07005356}
5357
5358/*[clinic input]
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005359int.to_bytes
5360
5361 length: Py_ssize_t
5362 Length of bytes object to use. An OverflowError is raised if the
5363 integer is not representable with the given number of bytes.
5364 byteorder: unicode
5365 The byte order used to represent the integer. If byteorder is 'big',
5366 the most significant byte is at the beginning of the byte array. If
5367 byteorder is 'little', the most significant byte is at the end of the
5368 byte array. To request the native byte order of the host system, use
5369 `sys.byteorder' as the byte order value.
5370 *
5371 signed as is_signed: bool = False
5372 Determines whether two's complement is used to represent the integer.
5373 If signed is False and a negative integer is given, an OverflowError
5374 is raised.
5375
5376Return an array of bytes representing an integer.
5377[clinic start generated code]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005378
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005379static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005380int_to_bytes_impl(PyObject *self, Py_ssize_t length, PyObject *byteorder,
5381 int is_signed)
5382/*[clinic end generated code: output=89c801df114050a3 input=ddac63f4c7bf414c]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 int little_endian;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 PyObject *bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005386
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005387 if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 little_endian = 1;
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005389 else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 little_endian = 0;
5391 else {
5392 PyErr_SetString(PyExc_ValueError,
5393 "byteorder must be either 'little' or 'big'");
5394 return NULL;
5395 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 if (length < 0) {
5398 PyErr_SetString(PyExc_ValueError,
5399 "length argument must be non-negative");
5400 return NULL;
5401 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005403 bytes = PyBytes_FromStringAndSize(NULL, length);
5404 if (bytes == NULL)
5405 return NULL;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005406
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005407 if (_PyLong_AsByteArray((PyLongObject *)self,
5408 (unsigned char *)PyBytes_AS_STRING(bytes),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 length, little_endian, is_signed) < 0) {
5410 Py_DECREF(bytes);
5411 return NULL;
5412 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 return bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005415}
5416
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005417/*[clinic input]
5418@classmethod
5419int.from_bytes
5420
5421 bytes as bytes_obj: object
5422 Holds the array of bytes to convert. The argument must either
5423 support the buffer protocol or be an iterable object producing bytes.
5424 Bytes and bytearray are examples of built-in objects that support the
5425 buffer protocol.
5426 byteorder: unicode
5427 The byte order used to represent the integer. If byteorder is 'big',
5428 the most significant byte is at the beginning of the byte array. If
5429 byteorder is 'little', the most significant byte is at the end of the
5430 byte array. To request the native byte order of the host system, use
5431 `sys.byteorder' as the byte order value.
5432 *
5433 signed as is_signed: bool = False
5434 Indicates whether two's complement is used to represent the integer.
5435
5436Return the integer represented by the given array of bytes.
5437[clinic start generated code]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005438
5439static PyObject *
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005440int_from_bytes_impl(PyTypeObject *type, PyObject *bytes_obj,
5441 PyObject *byteorder, int is_signed)
5442/*[clinic end generated code: output=efc5d68e31f9314f input=cdf98332b6a821b0]*/
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005444 int little_endian;
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005445 PyObject *long_obj, *bytes;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005446
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005447 if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_little))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005448 little_endian = 1;
Serhiy Storchaka196a7bc2017-02-02 16:54:45 +02005449 else if (_PyUnicode_EqualToASCIIId(byteorder, &PyId_big))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005450 little_endian = 0;
5451 else {
5452 PyErr_SetString(PyExc_ValueError,
5453 "byteorder must be either 'little' or 'big'");
5454 return NULL;
5455 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005456
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005457 bytes = PyObject_Bytes(bytes_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 if (bytes == NULL)
5459 return NULL;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 long_obj = _PyLong_FromByteArray(
5462 (unsigned char *)PyBytes_AS_STRING(bytes), Py_SIZE(bytes),
5463 little_endian, is_signed);
5464 Py_DECREF(bytes);
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005465
Zackery Spytz7bb9cd02018-10-05 15:02:23 -06005466 if (long_obj != NULL && type != &PyLong_Type) {
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01005467 Py_SETREF(long_obj, PyObject_CallFunctionObjArgs((PyObject *)type,
5468 long_obj, NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 }
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005471 return long_obj;
Alexandre Vassalottic36c3782010-01-09 20:35:09 +00005472}
5473
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005474static PyObject *
5475long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
5476{
5477 return long_long(self);
5478}
5479
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005480static PyMethodDef long_methods[] = {
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005481 {"conjugate", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005482 "Returns self, the complex conjugate of any int."},
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005483 INT_BIT_LENGTH_METHODDEF
Christian Heimes53876d92008-04-19 00:31:39 +00005484#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005485 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
5486 "Returns always True."},
Christian Heimes53876d92008-04-19 00:31:39 +00005487#endif
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005488 INT_TO_BYTES_METHODDEF
5489 INT_FROM_BYTES_METHODDEF
Lisa Roach5ac70432018-09-13 23:56:23 -07005490 INT_AS_INTEGER_RATIO_METHODDEF
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005491 {"__trunc__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005492 "Truncating an Integral returns itself."},
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005493 {"__floor__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005494 "Flooring an Integral returns itself."},
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005495 {"__ceil__", long_long_meth, METH_NOARGS,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 "Ceiling of an Integral returns itself."},
5497 {"__round__", (PyCFunction)long_round, METH_VARARGS,
5498 "Rounding an Integral returns itself.\n"
5499 "Rounding with an ndigits argument also returns an integer."},
Serhiy Storchaka495e8802017-02-01 23:12:20 +02005500 INT___GETNEWARGS___METHODDEF
5501 INT___FORMAT___METHODDEF
5502 INT___SIZEOF___METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005503 {NULL, NULL} /* sentinel */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00005504};
5505
Guido van Rossumb43daf72007-08-01 18:08:08 +00005506static PyGetSetDef long_getset[] = {
Mark Dickinson6bf19002009-05-02 17:57:52 +00005507 {"real",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005508 (getter)long_long_meth, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005509 "the real part of a complex number",
5510 NULL},
Mark Dickinson6bf19002009-05-02 17:57:52 +00005511 {"imag",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005512 long_get0, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005513 "the imaginary part of a complex number",
Mark Dickinson6bf19002009-05-02 17:57:52 +00005514 NULL},
5515 {"numerator",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005516 (getter)long_long_meth, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005517 "the numerator of a rational number in lowest terms",
5518 NULL},
Mark Dickinson6bf19002009-05-02 17:57:52 +00005519 {"denominator",
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005520 long_get1, (setter)NULL,
Guido van Rossumb43daf72007-08-01 18:08:08 +00005521 "the denominator of a rational number in lowest terms",
Mark Dickinson6bf19002009-05-02 17:57:52 +00005522 NULL},
Guido van Rossumb43daf72007-08-01 18:08:08 +00005523 {NULL} /* Sentinel */
5524};
5525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005526PyDoc_STRVAR(long_doc,
svelankar390a0962017-03-08 19:29:01 -05005527"int([x]) -> integer\n\
Chris Jerdonek83fe2e12012-10-07 14:48:36 -07005528int(x, base=10) -> integer\n\
Tim Peters6d6c1a32001-08-02 04:15:00 +00005529\n\
Chris Jerdonek83fe2e12012-10-07 14:48:36 -07005530Convert a number or string to an integer, or return 0 if no arguments\n\
5531are given. If x is a number, return x.__int__(). For floating point\n\
5532numbers, this truncates towards zero.\n\
5533\n\
5534If x is not a number or if base is given, then x must be a string,\n\
5535bytes, or bytearray instance representing an integer literal in the\n\
5536given base. The literal can be preceded by '+' or '-' and be surrounded\n\
5537by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.\n\
5538Base 0 means to interpret the base from the string as an integer literal.\n\
5539>>> int('0b100', base=0)\n\
55404");
Tim Peters6d6c1a32001-08-02 04:15:00 +00005541
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005542static PyNumberMethods long_as_number = {
Mark Dickinson22b20182010-05-10 21:27:53 +00005543 (binaryfunc)long_add, /*nb_add*/
5544 (binaryfunc)long_sub, /*nb_subtract*/
5545 (binaryfunc)long_mul, /*nb_multiply*/
5546 long_mod, /*nb_remainder*/
5547 long_divmod, /*nb_divmod*/
5548 long_pow, /*nb_power*/
5549 (unaryfunc)long_neg, /*nb_negative*/
Serhiy Storchaka6405fee2018-04-30 15:35:08 +03005550 long_long, /*tp_positive*/
Mark Dickinson22b20182010-05-10 21:27:53 +00005551 (unaryfunc)long_abs, /*tp_absolute*/
5552 (inquiry)long_bool, /*tp_bool*/
5553 (unaryfunc)long_invert, /*nb_invert*/
5554 long_lshift, /*nb_lshift*/
5555 (binaryfunc)long_rshift, /*nb_rshift*/
5556 long_and, /*nb_and*/
5557 long_xor, /*nb_xor*/
5558 long_or, /*nb_or*/
5559 long_long, /*nb_int*/
5560 0, /*nb_reserved*/
5561 long_float, /*nb_float*/
5562 0, /* nb_inplace_add */
5563 0, /* nb_inplace_subtract */
5564 0, /* nb_inplace_multiply */
5565 0, /* nb_inplace_remainder */
5566 0, /* nb_inplace_power */
5567 0, /* nb_inplace_lshift */
5568 0, /* nb_inplace_rshift */
5569 0, /* nb_inplace_and */
5570 0, /* nb_inplace_xor */
5571 0, /* nb_inplace_or */
5572 long_div, /* nb_floor_divide */
5573 long_true_divide, /* nb_true_divide */
5574 0, /* nb_inplace_floor_divide */
5575 0, /* nb_inplace_true_divide */
5576 long_long, /* nb_index */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00005577};
5578
Guido van Rossumc0b618a1997-05-02 03:12:38 +00005579PyTypeObject PyLong_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005580 PyVarObject_HEAD_INIT(&PyType_Type, 0)
5581 "int", /* tp_name */
5582 offsetof(PyLongObject, ob_digit), /* tp_basicsize */
5583 sizeof(digit), /* tp_itemsize */
5584 long_dealloc, /* tp_dealloc */
5585 0, /* tp_print */
5586 0, /* tp_getattr */
5587 0, /* tp_setattr */
5588 0, /* tp_reserved */
5589 long_to_decimal_string, /* tp_repr */
5590 &long_as_number, /* tp_as_number */
5591 0, /* tp_as_sequence */
5592 0, /* tp_as_mapping */
5593 (hashfunc)long_hash, /* tp_hash */
5594 0, /* tp_call */
5595 long_to_decimal_string, /* tp_str */
5596 PyObject_GenericGetAttr, /* tp_getattro */
5597 0, /* tp_setattro */
5598 0, /* tp_as_buffer */
5599 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
5600 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
5601 long_doc, /* tp_doc */
5602 0, /* tp_traverse */
5603 0, /* tp_clear */
5604 long_richcompare, /* tp_richcompare */
5605 0, /* tp_weaklistoffset */
5606 0, /* tp_iter */
5607 0, /* tp_iternext */
5608 long_methods, /* tp_methods */
5609 0, /* tp_members */
5610 long_getset, /* tp_getset */
5611 0, /* tp_base */
5612 0, /* tp_dict */
5613 0, /* tp_descr_get */
5614 0, /* tp_descr_set */
5615 0, /* tp_dictoffset */
5616 0, /* tp_init */
5617 0, /* tp_alloc */
5618 long_new, /* tp_new */
5619 PyObject_Del, /* tp_free */
Guido van Rossumedcc38a1991-05-05 20:09:44 +00005620};
Guido van Rossumddefaf32007-01-14 03:31:43 +00005621
Mark Dickinsonbd792642009-03-18 20:06:12 +00005622static PyTypeObject Int_InfoType;
5623
5624PyDoc_STRVAR(int_info__doc__,
5625"sys.int_info\n\
5626\n\
5627A struct sequence that holds information about Python's\n\
5628internal representation of integers. The attributes are read only.");
5629
5630static PyStructSequence_Field int_info_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005631 {"bits_per_digit", "size of a digit in bits"},
Mark Dickinson22b20182010-05-10 21:27:53 +00005632 {"sizeof_digit", "size in bytes of the C type used to represent a digit"},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005633 {NULL, NULL}
Mark Dickinsonbd792642009-03-18 20:06:12 +00005634};
5635
5636static PyStructSequence_Desc int_info_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005637 "sys.int_info", /* name */
5638 int_info__doc__, /* doc */
5639 int_info_fields, /* fields */
5640 2 /* number of fields */
Mark Dickinsonbd792642009-03-18 20:06:12 +00005641};
5642
5643PyObject *
5644PyLong_GetInfo(void)
5645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005646 PyObject* int_info;
5647 int field = 0;
5648 int_info = PyStructSequence_New(&Int_InfoType);
5649 if (int_info == NULL)
5650 return NULL;
5651 PyStructSequence_SET_ITEM(int_info, field++,
5652 PyLong_FromLong(PyLong_SHIFT));
5653 PyStructSequence_SET_ITEM(int_info, field++,
5654 PyLong_FromLong(sizeof(digit)));
5655 if (PyErr_Occurred()) {
5656 Py_CLEAR(int_info);
5657 return NULL;
5658 }
5659 return int_info;
Mark Dickinsonbd792642009-03-18 20:06:12 +00005660}
5661
Guido van Rossumddefaf32007-01-14 03:31:43 +00005662int
5663_PyLong_Init(void)
5664{
5665#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005666 int ival, size;
5667 PyLongObject *v = small_ints;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00005668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005669 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
5670 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
5671 if (Py_TYPE(v) == &PyLong_Type) {
5672 /* The element is already initialized, most likely
5673 * the Python interpreter was initialized before.
5674 */
5675 Py_ssize_t refcnt;
5676 PyObject* op = (PyObject*)v;
Christian Heimesdfc12ed2008-01-31 15:16:38 +00005677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005678 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
5679 _Py_NewReference(op);
5680 /* _Py_NewReference sets the ref count to 1 but
5681 * the ref count might be larger. Set the refcnt
5682 * to the original refcnt + 1 */
5683 Py_REFCNT(op) = refcnt + 1;
5684 assert(Py_SIZE(op) == size);
Victor Stinner12174a52014-08-15 23:17:38 +02005685 assert(v->ob_digit[0] == (digit)abs(ival));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005686 }
5687 else {
Victor Stinnerb509d522018-11-23 14:27:38 +01005688 (void)PyObject_INIT(v, &PyLong_Type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005689 }
5690 Py_SIZE(v) = size;
Victor Stinner12174a52014-08-15 23:17:38 +02005691 v->ob_digit[0] = (digit)abs(ival);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005692 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00005693#endif
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005694 _PyLong_Zero = PyLong_FromLong(0);
5695 if (_PyLong_Zero == NULL)
5696 return 0;
5697 _PyLong_One = PyLong_FromLong(1);
5698 if (_PyLong_One == NULL)
5699 return 0;
5700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005701 /* initialize int_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +02005702 if (Int_InfoType.tp_name == NULL) {
Victor Stinner6d43f6f2019-01-22 21:18:05 +01005703 if (PyStructSequence_InitType2(&Int_InfoType, &int_info_desc) < 0) {
Victor Stinner1c8f0592013-07-22 22:24:54 +02005704 return 0;
Victor Stinner6d43f6f2019-01-22 21:18:05 +01005705 }
Victor Stinner1c8f0592013-07-22 22:24:54 +02005706 }
Mark Dickinsonbd792642009-03-18 20:06:12 +00005707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005708 return 1;
Guido van Rossumddefaf32007-01-14 03:31:43 +00005709}
5710
5711void
5712PyLong_Fini(void)
5713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005714 /* Integers are currently statically allocated. Py_DECREF is not
5715 needed, but Python must forget about the reference or multiple
5716 reinitializations will fail. */
Serhiy Storchakaba85d692017-03-30 09:09:41 +03005717 Py_CLEAR(_PyLong_One);
5718 Py_CLEAR(_PyLong_Zero);
Guido van Rossumddefaf32007-01-14 03:31:43 +00005719#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005720 int i;
5721 PyLongObject *v = small_ints;
5722 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
5723 _Py_DEC_REFTOTAL;
5724 _Py_ForgetReference((PyObject*)v);
5725 }
Guido van Rossumddefaf32007-01-14 03:31:43 +00005726#endif
5727}