annotate Objects/longobject.c @ 48416:fb02fedfe647

Merged revisions 64677 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r64677 | brett.cannon | 2008-07-02 14:52:42 -0700 (Wed, 02 Jul 2008) | 2 lines Revert r64673 and instead just change the file encoding. ........
author Brett Cannon <bcannon@gmail.com>
date Wed, 02 Jul 2008 22:06:12 +0000
parents 25412c6d2776
children c0c06ffaea83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Long (arbitrary precision) integer object implementation */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3 /* XXX The functional organization of this file is terrible */
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
4
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
5 #include "Python.h"
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "longintrepr.h"
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
7
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
8 #include <ctype.h>
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
10 #ifndef NSMALLPOSINTS
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
11 #define NSMALLPOSINTS 257
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
12 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
13 #ifndef NSMALLNEGINTS
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
14 #define NSMALLNEGINTS 5
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
15 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
16 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
17 /* Small integers are preallocated in this array so that they
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
18 can be shared.
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
19 The integers that are preallocated are those in the range
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
20 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
21 */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
22 static PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
23 #ifdef COUNT_ALLOCS
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
24 int quick_int_allocs, quick_neg_int_allocs;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
25 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
26
42110
e4f649c4f457 Merged revisions 55962-56019 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
27 static PyObject *
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
28 get_small_int(int ival)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
29 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
30 PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
31 Py_INCREF(v);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
32 #ifdef COUNT_ALLOCS
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
33 if (ival >= 0)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
34 quick_int_allocs++;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
35 else
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
36 quick_neg_int_allocs++;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
37 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
38 return v;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
39 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
40 #define CHECK_SMALL_INT(ival) \
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
41 do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
42 return get_small_int(ival); \
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
43 } while(0)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
44
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
45 #else
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
46 #define CHECK_SMALL_INT(ival)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
47 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
48
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
49 #define MEDIUM_VALUE(x) (Py_SIZE(x) < 0 ? -(x)->ob_digit[0] : (Py_SIZE(x) == 0 ? 0 : (x)->ob_digit[0]))
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
50 /* If a freshly-allocated long is already shared, it must
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
51 be a small integer, so negating it must go to PyLong_FromLong */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
52 #define NEGATE(x) \
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
53 do if (Py_REFCNT(x) == 1) Py_SIZE(x) = -Py_SIZE(x); \
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
54 else { PyObject* tmp=PyLong_FromLong(-MEDIUM_VALUE(x)); \
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
55 Py_DECREF(x); (x) = (PyLongObject*)tmp; } \
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
56 while(0)
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
57 /* For long multiplication, use the O(N**2) school algorithm unless
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
58 * both operands contain more than KARATSUBA_CUTOFF digits (this
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
59 * being an internal Python long digit, in base BASE).
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
60 */
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
61 #define KARATSUBA_CUTOFF 70
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
62 #define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
63
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
64 /* For exponentiation, use the binary left-to-right algorithm
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
65 * unless the exponent contains more than FIVEARY_CUTOFF digits.
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
66 * In that case, do 5 bits at a time. The potential drawback is that
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
67 * a table of 2**5 intermediate results is computed.
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
68 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
69 #define FIVEARY_CUTOFF 8
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
70
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
71 #define ABS(x) ((x) < 0 ? -(x) : (x))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
72
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
73 #undef MIN
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
74 #undef MAX
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
75 #define MAX(x, y) ((x) < (y) ? (y) : (x))
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
76 #define MIN(x, y) ((x) > (y) ? (y) : (x))
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
77
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
78 /* Forward */
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
79 static PyLongObject *long_normalize(PyLongObject *);
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
80 static PyLongObject *mul1(PyLongObject *, wdigit);
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
81 static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
82 static PyLongObject *divrem1(PyLongObject *, digit, digit *);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
83
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
84 #define SIGCHECK(PyTryBlock) \
25108
3a26e352f0a6 replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents: 24919
diff changeset
85 if (--_Py_Ticker < 0) { \
3a26e352f0a6 replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents: 24919
diff changeset
86 _Py_Ticker = _Py_CheckInterval; \
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
87 if (PyErr_CheckSignals()) PyTryBlock \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
88 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
89
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 /* Normalize (remove leading zeros from) a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 Doesn't attempt to free the storage--in most cases, due to the nature
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 of the algorithms used, this could save at most be one word anyway. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
94 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
95 long_normalize(register PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
97 Py_ssize_t j = ABS(Py_SIZE(v));
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
98 Py_ssize_t i = j;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
99
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 while (i > 0 && v->ob_digit[i-1] == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 --i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 if (i != j)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
103 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 /* Allocate a new long int object with size digits.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 Return NULL and set exception if we run out of memory. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
110 PyLongObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
111 _PyLong_New(Py_ssize_t size)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
113 PyLongObject *result;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
114 /* Can't use sizeof(PyLongObject) here, since the
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
115 compiler takes padding at the end into account.
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
116 As the consequence, this would waste 2 bytes on
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
117 a 32-bit system, and 6 bytes on a 64-bit system.
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
118 This computation would be incorrect on systems
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
119 which have padding before the digits; with 16-bit
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
120 digits this should not happen. */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
121 result = PyObject_MALLOC(sizeof(PyVarObject) +
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
122 size*sizeof(digit));
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
123 if (!result) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
124 PyErr_NoMemory();
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
125 return NULL;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
126 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
127 return (PyLongObject*)PyObject_INIT_VAR(result, &PyLong_Type, size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
130 PyObject *
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
131 _PyLong_Copy(PyLongObject *src)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
132 {
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
133 PyLongObject *result;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
134 Py_ssize_t i;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
135
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
136 assert(src != NULL);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
137 i = Py_SIZE(src);
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
138 if (i < 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
139 i = -(i);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
140 if (i < 2) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
141 int ival = src->ob_digit[0];
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
142 if (Py_SIZE(src) < 0)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
143 ival = -ival;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
144 CHECK_SMALL_INT(ival);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
145 }
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
146 result = _PyLong_New(i);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
147 if (result != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
148 Py_SIZE(result) = Py_SIZE(src);
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
149 while (--i >= 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
150 result->ob_digit[i] = src->ob_digit[i];
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
151 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
152 return (PyObject *)result;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
153 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
154
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 /* Create a new long int object from a C long int */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
157 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
158 PyLong_FromLong(long ival)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
160 PyLongObject *v;
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
161 unsigned long abs_ival;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
162 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
163 int ndigits = 0;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
164 int sign = 1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
165
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
166 CHECK_SMALL_INT(ival);
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
167
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
168 if (ival < 0) {
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
169 /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
170 ANSI C says that the result of -ival is undefined when ival
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
171 == LONG_MIN. Hence the following workaround. */
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
172 abs_ival = (unsigned long)(-1-ival) + 1;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
173 sign = -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
174 }
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
175 else {
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
176 abs_ival = (unsigned long)ival;
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
177 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
178
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
179 /* Fast path for single-digits ints */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
180 if (!(ival>>PyLong_SHIFT)) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
181 v = _PyLong_New(1);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
182 if (v) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
183 Py_SIZE(v) = sign;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
184 v->ob_digit[0] = ival;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
185 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
186 return (PyObject*)v;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
187 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
188
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
189 /* 2 digits */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
190 if (!(ival >> 2*PyLong_SHIFT)) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
191 v = _PyLong_New(2);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
192 if (v) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
193 Py_SIZE(v) = 2*sign;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
194 v->ob_digit[0] = (digit)ival & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
195 v->ob_digit[1] = ival >> PyLong_SHIFT;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
196 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
197 return (PyObject*)v;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
198 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
199
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
200 /* Larger numbers: loop to determine number of digits */
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
201 t = abs_ival;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
202 while (t) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
203 ++ndigits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
204 t >>= PyLong_SHIFT;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
205 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
206 v = _PyLong_New(ndigits);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if (v != NULL) {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
208 digit *p = v->ob_digit;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
209 Py_SIZE(v) = ndigits*sign;
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
210 t = abs_ival;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
211 while (t) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
212 *p++ = (digit)(t & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
213 t >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
216 return (PyObject *)v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
219 /* Create a new long int object from a C unsigned long int */
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
220
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
221 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
222 PyLong_FromUnsignedLong(unsigned long ival)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
223 {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
224 PyLongObject *v;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
225 unsigned long t;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
226 int ndigits = 0;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
227
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
228 if (ival < PyLong_BASE)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
229 return PyLong_FromLong(ival);
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
230 /* Count the number of Python digits. */
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
231 t = (unsigned long)ival;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
232 while (t) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
233 ++ndigits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
234 t >>= PyLong_SHIFT;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
235 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
236 v = _PyLong_New(ndigits);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
237 if (v != NULL) {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
238 digit *p = v->ob_digit;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
239 Py_SIZE(v) = ndigits;
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
240 while (ival) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
241 *p++ = (digit)(ival & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
242 ival >>= PyLong_SHIFT;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
243 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
244 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
245 return (PyObject *)v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
246 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
247
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
248 /* Create a new long int object from a C double */
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
249
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
250 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
251 PyLong_FromDouble(double dval)
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
252 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
253 PyLongObject *v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
254 double frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
255 int i, ndig, expo, neg;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
256 neg = 0;
13976
5ada9fa54801 Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents: 13761
diff changeset
257 if (Py_IS_INFINITY(dval)) {
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
258 PyErr_SetString(PyExc_OverflowError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
259 "cannot convert float infinity to int");
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
260 return NULL;
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
261 }
44460
13aabc23cf2e Merged revisions 59680-59695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44366
diff changeset
262 if (Py_IS_NAN(dval)) {
44683
357b365560f4 long(float('nan')) raises an OverflowError as discussed on the mailing list a week ago
Christian Heimes <christian@cheimes.de>
parents: 44469
diff changeset
263 PyErr_SetString(PyExc_OverflowError,
357b365560f4 long(float('nan')) raises an OverflowError as discussed on the mailing list a week ago
Christian Heimes <christian@cheimes.de>
parents: 44469
diff changeset
264 "cannot convert float NaN to int");
357b365560f4 long(float('nan')) raises an OverflowError as discussed on the mailing list a week ago
Christian Heimes <christian@cheimes.de>
parents: 44469
diff changeset
265 return NULL;
44460
13aabc23cf2e Merged revisions 59680-59695 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44366
diff changeset
266 }
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
267 if (dval < 0.0) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
268 neg = 1;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
269 dval = -dval;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
270 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
271 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
272 if (expo <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
273 return PyLong_FromLong(0L);
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
274 ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
275 v = _PyLong_New(ndig);
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
276 if (v == NULL)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
277 return NULL;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
278 frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
279 for (i = ndig; --i >= 0; ) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
280 long bits = (long)frac;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
281 v->ob_digit[i] = (digit) bits;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
282 frac = frac - (double)bits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
283 frac = ldexp(frac, PyLong_SHIFT);
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
284 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
285 if (neg)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
286 Py_SIZE(v) = -(Py_SIZE(v));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
287 return (PyObject *)v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
288 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
289
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
290 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
291 * anything about what happens when a signed integer operation overflows,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
292 * and some compilers think they're doing you a favor by being "clever"
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
293 * then. The bit pattern for the largest postive signed long is
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
294 * (unsigned long)LONG_MAX, and for the smallest negative signed long
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
295 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
296 * However, some other compilers warn about applying unary minus to an
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
297 * unsigned operand. Hence the weird "0-".
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
298 */
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
299 #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
300 #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
301
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 /* Get a C long int from a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 Returns -1 and sets an error condition if overflow occurs. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 long
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
306 PyLong_AsLongAndOverflow(PyObject *vv, int *overflow)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 {
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
308 /* This version by Tim Peters */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
309 register PyLongObject *v;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
310 unsigned long x, prev;
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
311 long res;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
312 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
313 int sign;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
314 int do_decref = 0; /* if nb_int was called */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
315
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
316 *overflow = 0;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
317 if (vv == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
318 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
321
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
322 if (!PyLong_Check(vv)) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
323 PyNumberMethods *nb;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
324 if ((nb = vv->ob_type->tp_as_number) == NULL ||
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
325 nb->nb_int == NULL) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
326 PyErr_SetString(PyExc_TypeError, "an integer is required");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
327 return -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
328 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
329 vv = (*nb->nb_int) (vv);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
330 if (vv == NULL)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
331 return -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
332 do_decref = 1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
333 if (!PyLong_Check(vv)) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
334 Py_DECREF(vv);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
335 PyErr_SetString(PyExc_TypeError,
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
336 "nb_int should return int object");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
337 return -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
338 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
339 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
340
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
341 res = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
342 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
343 i = Py_SIZE(v);
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
344
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
345 switch (i) {
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
346 case -1:
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
347 res = -v->ob_digit[0];
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
348 break;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
349 case 0:
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
350 res = 0;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
351 break;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
352 case 1:
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
353 res = v->ob_digit[0];
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
354 break;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
355 default:
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
356 sign = 1;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
357 x = 0;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
358 if (i < 0) {
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
359 sign = -1;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
360 i = -(i);
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
361 }
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
362 while (--i >= 0) {
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
363 prev = x;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
364 x = (x << PyLong_SHIFT) + v->ob_digit[i];
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
365 if ((x >> PyLong_SHIFT) != prev) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
366 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
367 goto exit;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
368 }
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
369 }
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
370 /* Haven't lost any bits, but casting to long requires extra care
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
371 * (see comment above).
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
372 */
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
373 if (x <= (unsigned long)LONG_MAX) {
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
374 res = (long)x * sign;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
375 }
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
376 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
377 res = LONG_MIN;
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
378 }
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
379 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
380 *overflow = Py_SIZE(v) > 0 ? 1 : -1;
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
381 /* res is already set to -1 */
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
382 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
383 }
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
384 exit:
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
385 if (do_decref) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
386 Py_DECREF(vv);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
387 }
40929
c3451a77bfd1 Another refleak, this time in PyLong_AsLong. Fixes leaks showing in
Georg Brandl <georg@python.org>
parents: 40909
diff changeset
388 return res;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
44158
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
391 long
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
392 PyLong_AsLong(PyObject *obj)
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
393 {
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
394 int overflow;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
395 long result = PyLong_AsLongAndOverflow(obj, &overflow);
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
396 if (overflow) {
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
397 /* XXX: could be cute and give a different
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
398 message for overflow == -1 */
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
399 PyErr_SetString(PyExc_OverflowError,
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
400 "Python int too large to convert to C long");
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
401 }
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
402 return result;
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
403 }
81e0c0e2a8c1 Remove PyInt_CheckExact. Add PyLong_AsLongAndOverflow.
Martin v. Löwis <martin@v.loewis.de>
parents: 44099
diff changeset
404
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
405 /* Get a Py_ssize_t from a long int object.
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
406 Returns -1 and sets an error condition if overflow occurs. */
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
407
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
408 Py_ssize_t
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
409 PyLong_AsSsize_t(PyObject *vv) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
410 register PyLongObject *v;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
411 size_t x, prev;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
412 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
413 int sign;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
414
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
415 if (vv == NULL || !PyLong_Check(vv)) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
416 PyErr_BadInternalCall();
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
417 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
418 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
419 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
420 i = Py_SIZE(v);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
421 switch (i) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
422 case -1: return -v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
423 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
424 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
425 }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
426 sign = 1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
427 x = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
428 if (i < 0) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
429 sign = -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
430 i = -(i);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
431 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
432 while (--i >= 0) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
433 prev = x;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
434 x = (x << PyLong_SHIFT) + v->ob_digit[i];
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
435 if ((x >> PyLong_SHIFT) != prev)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
436 goto overflow;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
437 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
438 /* Haven't lost any bits, but casting to a signed type requires
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
439 * extra care (see comment above).
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
440 */
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
441 if (x <= (size_t)PY_SSIZE_T_MAX) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
442 return (Py_ssize_t)x * sign;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
443 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
444 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
445 return PY_SSIZE_T_MIN;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
446 }
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
447 /* else overflow */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
448
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
449 overflow:
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
450 PyErr_SetString(PyExc_OverflowError,
40641
342532f4536e Fix the sqlite failure -- it was the usual, PyInt_Check -> PyInt_CheckExact.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
451 "Python int too large to convert to C ssize_t");
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
452 return -1;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
453 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
454
24777
696a284569e6 Fix comment for PyLong_AsUnsignedLong() to say that the return value
Guido van Rossum <guido@python.org>
parents: 24775
diff changeset
455 /* Get a C unsigned long int from a long int object.
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
456 Returns -1 and sets an error condition if overflow occurs. */
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
457
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
458 unsigned long
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
459 PyLong_AsUnsignedLong(PyObject *vv)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
460 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
461 register PyLongObject *v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
462 unsigned long x, prev;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
463 Py_ssize_t i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
464
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
465 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
466 PyErr_BadInternalCall();
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
467 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
468 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
469 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
470 i = Py_SIZE(v);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
471 x = 0;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
472 if (i < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
473 PyErr_SetString(PyExc_OverflowError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
474 "can't convert negative value to unsigned int");
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
475 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
476 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
477 switch (i) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
478 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
479 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
480 }
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
481 while (--i >= 0) {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
482 prev = x;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
483 x = (x << PyLong_SHIFT) + v->ob_digit[i];
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
484 if ((x >> PyLong_SHIFT) != prev) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
485 PyErr_SetString(PyExc_OverflowError,
40641
342532f4536e Fix the sqlite failure -- it was the usual, PyInt_Check -> PyInt_CheckExact.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
486 "python int too large to convert to C unsigned long");
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
487 return (unsigned long) -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
488 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
489 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
490 return x;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
491 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
492
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
493 /* Get a C unsigned long int from a long int object.
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
494 Returns -1 and sets an error condition if overflow occurs. */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
495
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
496 size_t
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
497 PyLong_AsSize_t(PyObject *vv)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
498 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
499 register PyLongObject *v;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
500 size_t x, prev;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
501 Py_ssize_t i;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
502
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
503 if (vv == NULL || !PyLong_Check(vv)) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
504 PyErr_BadInternalCall();
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
505 return (unsigned long) -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
506 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
507 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
508 i = Py_SIZE(v);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
509 x = 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
510 if (i < 0) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
511 PyErr_SetString(PyExc_OverflowError,
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
512 "can't convert negative value to size_t");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
513 return (size_t) -1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
514 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
515 switch (i) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
516 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
517 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
518 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
519 while (--i >= 0) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
520 prev = x;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
521 x = (x << PyLong_SHIFT) + v->ob_digit[i];
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
522 if ((x >> PyLong_SHIFT) != prev) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
523 PyErr_SetString(PyExc_OverflowError,
40641
342532f4536e Fix the sqlite failure -- it was the usual, PyInt_Check -> PyInt_CheckExact.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
524 "Python int too large to convert to C size_t");
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
525 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
526 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
527 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
528 return x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
529 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
530
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
531 /* Get a C unsigned long int from a long int object, ignoring the high bits.
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
532 Returns -1 and sets an error condition if an error occurs. */
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
533
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
534 static unsigned long
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
535 _PyLong_AsUnsignedLongMask(PyObject *vv)
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
536 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
537 register PyLongObject *v;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
538 unsigned long x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
539 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
540 int sign;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
541
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
542 if (vv == NULL || !PyLong_Check(vv)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
543 PyErr_BadInternalCall();
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
544 return (unsigned long) -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
545 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
546 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
547 i = Py_SIZE(v);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
548 switch (i) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
549 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
550 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
551 }
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
552 sign = 1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
553 x = 0;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
554 if (i < 0) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
555 sign = -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
556 i = -i;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
557 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
558 while (--i >= 0) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
559 x = (x << PyLong_SHIFT) + v->ob_digit[i];
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
560 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
561 return x * sign;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
562 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
563
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
564 unsigned long
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
565 PyLong_AsUnsignedLongMask(register PyObject *op)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
566 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
567 PyNumberMethods *nb;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
568 PyLongObject *lo;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
569 unsigned long val;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
570
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
571 if (op && PyLong_Check(op))
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
572 return _PyLong_AsUnsignedLongMask(op);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
573
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
574 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
575 nb->nb_int == NULL) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
576 PyErr_SetString(PyExc_TypeError, "an integer is required");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
577 return (unsigned long)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
578 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
579
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
580 lo = (PyLongObject*) (*nb->nb_int) (op);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
581 if (lo == NULL)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
582 return (unsigned long)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
583 if (PyLong_Check(lo)) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
584 val = _PyLong_AsUnsignedLongMask((PyObject *)lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
585 Py_DECREF(lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
586 if (PyErr_Occurred())
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
587 return (unsigned long)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
588 return val;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
589 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
590 else
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
591 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
592 Py_DECREF(lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
593 PyErr_SetString(PyExc_TypeError,
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
594 "nb_int should return int object");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
595 return (unsigned long)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
596 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
597 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
598
27549
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
599 int
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
600 _PyLong_Sign(PyObject *vv)
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
601 {
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
602 PyLongObject *v = (PyLongObject *)vv;
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
603
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
604 assert(v != NULL);
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
605 assert(PyLong_Check(v));
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
606
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
607 return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
27549
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
608 }
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
609
27487
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
610 size_t
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
611 _PyLong_NumBits(PyObject *vv)
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
612 {
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
613 PyLongObject *v = (PyLongObject *)vv;
27549
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
614 size_t result = 0;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
615 Py_ssize_t ndigits;
27487
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
616
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
617 assert(v != NULL);
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
618 assert(PyLong_Check(v));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
619 ndigits = ABS(Py_SIZE(v));
27487
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
620 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
621 if (ndigits > 0) {
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
622 digit msd = v->ob_digit[ndigits - 1];
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
623
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
624 result = (ndigits - 1) * PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
625 if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
27487
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
626 goto Overflow;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
627 do {
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
628 ++result;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
629 if (result == 0)
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
630 goto Overflow;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
631 msd >>= 1;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
632 } while (msd);
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
633 }
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
634 return result;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
635
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
636 Overflow:
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
637 PyErr_SetString(PyExc_OverflowError, "int has too many bits "
27487
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
638 "to express in a platform size_t");
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
639 return (size_t)-1;
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
640 }
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
641
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
642 PyObject *
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
643 _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
644 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
645 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
646 const unsigned char* pstartbyte;/* LSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
647 int incr; /* direction to move pstartbyte */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
648 const unsigned char* pendbyte; /* MSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
649 size_t numsignificantbytes; /* number of bytes that matter */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
650 size_t ndigits; /* number of Python long digits */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
651 PyLongObject* v; /* result */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
652 int idigit = 0; /* next free index in v->ob_digit */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
653
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
654 if (n == 0)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
655 return PyLong_FromLong(0L);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
656
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
657 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
658 pstartbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
659 pendbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
660 incr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
661 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
662 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
663 pstartbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
664 pendbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
665 incr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
666 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
667
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
668 if (is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
669 is_signed = *pendbyte >= 0x80;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
670
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
671 /* Compute numsignificantbytes. This consists of finding the most
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
672 significant byte. Leading 0 bytes are insignficant if the number
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
673 is positive, and leading 0xff bytes if negative. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
674 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
675 size_t i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
676 const unsigned char* p = pendbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
677 const int pincr = -incr; /* search MSB to LSB */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
678 const unsigned char insignficant = is_signed ? 0xff : 0x00;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
679
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
680 for (i = 0; i < n; ++i, p += pincr) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
681 if (*p != insignficant)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
682 break;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
683 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
684 numsignificantbytes = n - i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
685 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
686 actually has 2 significant bytes. OTOH, 0xff0001 ==
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
687 -0x00ffff, so we wouldn't *need* to bump it there; but we
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
688 do for 0xffff = -0x0001. To be safe without bothering to
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
689 check every case, bump it regardless. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
690 if (is_signed && numsignificantbytes < n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
691 ++numsignificantbytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
692 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
693
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
694 /* How many Python long digits do we need? We have
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
695 8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
696 bits, so it's the ceiling of the quotient. */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
697 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
698 if (ndigits > (size_t)INT_MAX)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
699 return PyErr_NoMemory();
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
700 v = _PyLong_New((int)ndigits);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
701 if (v == NULL)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
702 return NULL;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
703
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
704 /* Copy the bits over. The tricky parts are computing 2's-comp on
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
705 the fly for signed numbers, and dealing with the mismatch between
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
706 8-bit bytes and (probably) 15-bit Python digits.*/
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
707 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
708 size_t i;
18110
e21cd4135f3d _PyLong_FromByteArray: changed decl of "carry" to match "thisbyte". No
Tim Peters <tim.peters@gmail.com>
parents: 18109
diff changeset
709 twodigits carry = 1; /* for 2's-comp calculation */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
710 twodigits accum = 0; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
711 unsigned int accumbits = 0; /* number of bits in accum */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
712 const unsigned char* p = pstartbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
713
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
714 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
715 twodigits thisbyte = *p;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
716 /* Compute correction for 2's comp, if needed. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
717 if (is_signed) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
718 thisbyte = (0xff ^ thisbyte) + carry;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
719 carry = thisbyte >> 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
720 thisbyte &= 0xff;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
721 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
722 /* Because we're going LSB to MSB, thisbyte is
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
723 more significant than what's already in accum,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
724 so needs to be prepended to accum. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
725 accum |= thisbyte << accumbits;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
726 accumbits += 8;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
727 if (accumbits >= PyLong_SHIFT) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
728 /* There's enough to fill a Python digit. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
729 assert(idigit < (int)ndigits);
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
730 v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
731 ++idigit;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
732 accum >>= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
733 accumbits -= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
734 assert(accumbits < PyLong_SHIFT);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
735 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
736 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
737 assert(accumbits < PyLong_SHIFT);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
738 if (accumbits) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
739 assert(idigit < (int)ndigits);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
740 v->ob_digit[idigit] = (digit)accum;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
741 ++idigit;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
742 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
743 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
744
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
745 Py_SIZE(v) = is_signed ? -idigit : idigit;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
746 return (PyObject *)long_normalize(v);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
747 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
748
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
749 int
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
750 _PyLong_AsByteArray(PyLongObject* v,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
751 unsigned char* bytes, size_t n,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
752 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
753 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
754 int i; /* index into v->ob_digit */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
755 Py_ssize_t ndigits; /* |v->ob_size| */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
756 twodigits accum; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
757 unsigned int accumbits; /* # bits in accum */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
758 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
759 twodigits carry; /* for computing 2's-comp */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
760 size_t j; /* # bytes filled */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
761 unsigned char* p; /* pointer to next byte in bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
762 int pincr; /* direction to move p */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
763
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
764 assert(v != NULL && PyLong_Check(v));
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
765
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
766 if (Py_SIZE(v) < 0) {
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
767 ndigits = -(Py_SIZE(v));
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
768 if (!is_signed) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
769 PyErr_SetString(PyExc_TypeError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
770 "can't convert negative int to unsigned");
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
771 return -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
772 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
773 do_twos_comp = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
774 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
775 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
776 ndigits = Py_SIZE(v);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
777 do_twos_comp = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
778 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
779
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
780 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
781 p = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
782 pincr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
783 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
784 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
785 p = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
786 pincr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
787 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
788
18108
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
789 /* Copy over all the Python digits.
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
790 It's crucial that every Python digit except for the MSD contribute
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
791 exactly PyLong_SHIFT bits to the total, so first assert that the long is
18108
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
792 normalized. */
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
793 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
794 j = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
795 accum = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
796 accumbits = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
797 carry = do_twos_comp ? 1 : 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
798 for (i = 0; i < ndigits; ++i) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
799 twodigits thisdigit = v->ob_digit[i];
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
800 if (do_twos_comp) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
801 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
802 carry = thisdigit >> PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
803 thisdigit &= PyLong_MASK;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
804 }
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
805 /* Because we're going LSB to MSB, thisdigit is more
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
806 significant than what's already in accum, so needs to be
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
807 prepended to accum. */
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
808 accum |= thisdigit << accumbits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
809 accumbits += PyLong_SHIFT;
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
810
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
811 /* The most-significant digit may be (probably is) at least
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
812 partly empty. */
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
813 if (i == ndigits - 1) {
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
814 /* Count # of sign bits -- they needn't be stored,
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
815 * although for signed conversion we need later to
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
816 * make sure at least one sign bit gets stored.
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
817 * First shift conceptual sign bit to real sign bit.
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
818 */
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
819 stwodigits s = (stwodigits)(thisdigit <<
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
820 (8*sizeof(stwodigits) - PyLong_SHIFT));
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
821 unsigned int nsignbits = 0;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
822 while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
823 ++nsignbits;
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
824 s <<= 1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
825 }
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
826 accumbits -= nsignbits;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
827 }
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
828
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
829 /* Store as many bytes as possible. */
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
830 while (accumbits >= 8) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
831 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
832 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
833 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
834 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
835 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
836 accumbits -= 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
837 accum >>= 8;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
838 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
839 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
840
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
841 /* Store the straggler (if any). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
842 assert(accumbits < 8);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
843 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
844 if (accumbits > 0) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
845 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
846 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
847 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
848 if (do_twos_comp) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
849 /* Fill leading bits of the byte with sign bits
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
850 (appropriately pretending that the long had an
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
851 infinite supply of sign bits). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
852 accum |= (~(twodigits)0) << accumbits;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
853 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
854 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
855 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
856 }
18109
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
857 else if (j == n && n > 0 && is_signed) {
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
858 /* The main loop filled the byte array exactly, so the code
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
859 just above didn't get to ensure there's a sign bit, and the
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
860 loop below wouldn't add one either. Make sure a sign bit
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
861 exists. */
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
862 unsigned char msb = *(p - pincr);
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
863 int sign_bit_set = msb >= 0x80;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
864 assert(accumbits == 0);
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
865 if (sign_bit_set == do_twos_comp)
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
866 return 0;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
867 else
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
868 goto Overflow;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
869 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
870
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
871 /* Fill remaining bytes with copies of the sign bit. */
18109
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
872 {
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
873 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
874 for ( ; j < n; ++j, p += pincr)
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
875 *p = signbyte;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
876 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
877
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
878 return 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
879
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
880 Overflow:
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
881 PyErr_SetString(PyExc_OverflowError, "int too big to convert");
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
882 return -1;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
883
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
884 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
885
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
886 double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
887 _PyLong_AsScaledDouble(PyObject *vv, int *exponent)
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
888 {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
889 /* NBITS_WANTED should be > the number of bits in a double's precision,
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
890 but small enough so that 2**NBITS_WANTED is within the normal double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
891 range. nbitsneeded is set to 1 less than that because the most-significant
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
892 Python digit contains at least 1 significant bit, but we don't want to
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
893 bother counting them (catering to the worst case cheaply).
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
894
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
895 57 is one more than VAX-D double precision; I (Tim) don't know of a double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
896 format with more precision than that; it's 1 larger so that we add in at
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
897 least one round bit to stand in for the ignored least-significant bits.
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
898 */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
899 #define NBITS_WANTED 57
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
900 PyLongObject *v;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
901 double x;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
902 const double multiplier = (double)(1L << PyLong_SHIFT);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
903 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
904 int sign;
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
905 int nbitsneeded;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
906
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
907 if (vv == NULL || !PyLong_Check(vv)) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
908 PyErr_BadInternalCall();
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
909 return -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
910 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
911 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
912 i = Py_SIZE(v);
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
913 sign = 1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
914 if (i < 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
915 sign = -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
916 i = -(i);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
917 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
918 else if (i == 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
919 *exponent = 0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
920 return 0.0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
921 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
922 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
923 x = (double)v->ob_digit[i];
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
924 nbitsneeded = NBITS_WANTED - 1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
925 /* Invariant: i Python digits remain unaccounted for. */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
926 while (i > 0 && nbitsneeded > 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
927 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
928 x = x * multiplier + (double)v->ob_digit[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
929 nbitsneeded -= PyLong_SHIFT;
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
930 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
931 /* There are i digits we didn't shift in. Pretending they're all
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
932 zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
933 *exponent = i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
934 assert(x > 0.0);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
935 return x * sign;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
936 #undef NBITS_WANTED
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
937 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
938
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
939 /* Get a C double from a long int object. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
940
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 double
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
942 PyLong_AsDouble(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 {
36430
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
944 int e = -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 double x;
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
946
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
947 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
948 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
949 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 }
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
951 x = _PyLong_AsScaledDouble(vv, &e);
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
952 if (x == -1.0 && PyErr_Occurred())
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
953 return -1.0;
36430
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
954 /* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
955 set correctly after a successful _PyLong_AsScaledDouble() call */
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
956 assert(e >= 0);
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
957 if (e > INT_MAX / PyLong_SHIFT)
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
958 goto overflow;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
959 errno = 0;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
960 x = ldexp(x, e * PyLong_SHIFT);
19717
01de0a703069 Try to recover from that glibc's ldexp apparently doesn't set errno on
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
961 if (Py_OVERFLOWED(x))
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
962 goto overflow;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
963 return x;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
964
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
965 overflow:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
966 PyErr_SetString(PyExc_OverflowError,
40641
342532f4536e Fix the sqlite failure -- it was the usual, PyInt_Check -> PyInt_CheckExact.
Guido van Rossum <guido@python.org>
parents: 40626
diff changeset
967 "Python int too large to convert to C double");
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
968 return -1.0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
970
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
971 /* Create a new long (or int) object from a C pointer */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
972
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
973 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
974 PyLong_FromVoidPtr(void *p)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
975 {
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
976 #ifndef HAVE_LONG_LONG
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
977 # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
978 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
979 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
980 # error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
981 #endif
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
982 /* special-case null pointer */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
983 if (!p)
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
984 return PyLong_FromLong(0);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
985 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)(Py_uintptr_t)p);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
986
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
987 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
988
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
989 /* Get a C pointer from a long object (or an int object in some cases) */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
990
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
991 void *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
992 PyLong_AsVoidPtr(PyObject *vv)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
993 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
994 /* This function will allow int or long objects. If vv is neither,
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
995 then the PyLong_AsLong*() functions will raise the exception:
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
996 PyExc_SystemError, "bad argument to internal function"
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
997 */
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
998 #if SIZEOF_VOID_P <= SIZEOF_LONG
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
999 long x;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1000
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1001 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1002 x = PyLong_AsLong(vv);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1003 else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1004 x = PyLong_AsUnsignedLong(vv);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1005 #else
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1006
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1007 #ifndef HAVE_LONG_LONG
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1008 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1009 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1010 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1011 # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1012 #endif
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1013 PY_LONG_LONG x;
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1014
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1015 if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1016 x = PyLong_AsLongLong(vv);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1017 else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1018 x = PyLong_AsUnsignedLongLong(vv);
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1019
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
1020 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1021
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1022 if (x == -1 && PyErr_Occurred())
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1023 return NULL;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1024 return (void *)x;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1025 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
1026
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1027 #ifdef HAVE_LONG_LONG
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1028
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1029 /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1030 * rewritten to use the newer PyLong_{As,From}ByteArray API.
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1031 */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1032
18121
166b7b365851 Change IS_LITTLE_ENDIAN macro -- a little faster now.
Tim Peters <tim.peters@gmail.com>
parents: 18115
diff changeset
1033 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1034
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1035 /* Create a new long int object from a C PY_LONG_LONG int. */
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1036
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1037 PyObject *
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1038 PyLong_FromLongLong(PY_LONG_LONG ival)
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1039 {
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1040 PyLongObject *v;
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1041 unsigned PY_LONG_LONG abs_ival;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1042 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1043 int ndigits = 0;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1044 int negative = 0;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1045
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1046 CHECK_SMALL_INT(ival);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1047 if (ival < 0) {
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1048 /* avoid signed overflow on negation; see comments
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1049 in PyLong_FromLong above. */
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1050 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1051 negative = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1052 }
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1053 else {
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1054 abs_ival = (unsigned PY_LONG_LONG)ival;
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1055 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1056
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1057 /* Count the number of Python digits.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1058 We used to pick 5 ("big enough for anything"), but that's a
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1059 waste of time and space given that 5*15 = 75 bits are rarely
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1060 needed. */
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1061 t = abs_ival;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1062 while (t) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1063 ++ndigits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1064 t >>= PyLong_SHIFT;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1065 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1066 v = _PyLong_New(ndigits);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1067 if (v != NULL) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1068 digit *p = v->ob_digit;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1069 Py_SIZE(v) = negative ? -ndigits : ndigits;
46478
beb7abcd6c01 Merged revisions 62350-62355,62358-62359,62364-62365,62370,62372-62375,62378-62379,62381 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46477
diff changeset
1070 t = abs_ival;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1071 while (t) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1072 *p++ = (digit)(t & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1073 t >>= PyLong_SHIFT;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1074 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1075 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1076 return (PyObject *)v;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1077 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1078
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1079 /* Create a new long int object from a C unsigned PY_LONG_LONG int. */
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1080
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1081 PyObject *
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1082 PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1083 {
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1084 PyLongObject *v;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1085 unsigned PY_LONG_LONG t;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1086 int ndigits = 0;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1087
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1088 if (ival < PyLong_BASE)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1089 return PyLong_FromLong(ival);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1090 /* Count the number of Python digits. */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1091 t = (unsigned PY_LONG_LONG)ival;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1092 while (t) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1093 ++ndigits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1094 t >>= PyLong_SHIFT;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1095 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1096 v = _PyLong_New(ndigits);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1097 if (v != NULL) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1098 digit *p = v->ob_digit;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1099 Py_SIZE(v) = ndigits;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1100 while (ival) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1101 *p++ = (digit)(ival & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1102 ival >>= PyLong_SHIFT;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1103 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1104 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1105 return (PyObject *)v;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1106 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1107
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1108 /* Create a new long int object from a C Py_ssize_t. */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1109
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1110 PyObject *
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1111 PyLong_FromSsize_t(Py_ssize_t ival)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1112 {
46460
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1113 PyLongObject *v;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1114 size_t abs_ival;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1115 size_t t; /* unsigned so >> doesn't propagate sign bit */
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1116 int ndigits = 0;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1117 int negative = 0;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1118
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1119 CHECK_SMALL_INT(ival);
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1120 if (ival < 0) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1121 /* avoid signed overflow when ival = SIZE_T_MIN */
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1122 abs_ival = (size_t)(-1-ival)+1;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1123 negative = 1;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1124 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1125 else {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1126 abs_ival = (size_t)ival;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1127 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1128
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1129 /* Count the number of Python digits. */
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1130 t = abs_ival;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1131 while (t) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1132 ++ndigits;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1133 t >>= PyLong_SHIFT;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1134 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1135 v = _PyLong_New(ndigits);
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1136 if (v != NULL) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1137 digit *p = v->ob_digit;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1138 Py_SIZE(v) = negative ? -ndigits : ndigits;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1139 t = abs_ival;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1140 while (t) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1141 *p++ = (digit)(t & PyLong_MASK);
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1142 t >>= PyLong_SHIFT;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1143 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1144 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1145 return (PyObject *)v;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1146 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1147
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1148 /* Create a new long int object from a C size_t. */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1149
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1150 PyObject *
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1151 PyLong_FromSize_t(size_t ival)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1152 {
46460
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1153 PyLongObject *v;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1154 size_t t;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1155 int ndigits = 0;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1156
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1157 if (ival < PyLong_BASE)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1158 return PyLong_FromLong(ival);
46460
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1159 /* Count the number of Python digits. */
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1160 t = ival;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1161 while (t) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1162 ++ndigits;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1163 t >>= PyLong_SHIFT;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1164 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1165 v = _PyLong_New(ndigits);
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1166 if (v != NULL) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1167 digit *p = v->ob_digit;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1168 Py_SIZE(v) = ndigits;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1169 while (ival) {
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1170 *p++ = (digit)(ival & PyLong_MASK);
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1171 ival >>= PyLong_SHIFT;
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1172 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1173 }
b04f2052e812 PyLong_FromSsize_t was incorrect when sizeof(size_t) > sizeof(long);
Mark Dickinson <dickinsm@gmail.com>
parents: 46161
diff changeset
1174 return (PyObject *)v;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1175 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1176
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1177 /* Get a C PY_LONG_LONG int from a long int object.
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1178 Return -1 and set an error if overflow occurs. */
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1179
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1180 PY_LONG_LONG
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1181 PyLong_AsLongLong(PyObject *vv)
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1182 {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1183 PyLongObject *v;
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1184 PY_LONG_LONG bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1185 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1186 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1187
20182
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1188 if (vv == NULL) {
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1189 PyErr_BadInternalCall();
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1190 return -1;
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1191 }
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1192 if (!PyLong_Check(vv)) {
34666
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1193 PyNumberMethods *nb;
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1194 PyObject *io;
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1195 if ((nb = vv->ob_type->tp_as_number) == NULL ||
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1196 nb->nb_int == NULL) {
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1197 PyErr_SetString(PyExc_TypeError, "an integer is required");
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1198 return -1;
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1199 }
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1200 io = (*nb->nb_int) (vv);
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1201 if (io == NULL)
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1202 return -1;
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1203 if (PyLong_Check(io)) {
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1204 bytes = PyLong_AsLongLong(io);
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1205 Py_DECREF(io);
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1206 return bytes;
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1207 }
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1208 Py_DECREF(io);
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1209 PyErr_SetString(PyExc_TypeError, "integer conversion failed");
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1210 return -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1211 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1212
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1213 v = (PyLongObject*)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1214 switch(Py_SIZE(v)) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1215 case -1: return -v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1216 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1217 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1218 }
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1219 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1220 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1221 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1222
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1223 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
22204
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1224 if (res < 0)
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1225 return (PY_LONG_LONG)-1;
22204
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1226 else
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1227 return bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1228 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1229
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1230 /* Get a C unsigned PY_LONG_LONG int from a long int object.
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1231 Return -1 and set an error if overflow occurs. */
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1232
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1233 unsigned PY_LONG_LONG
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1234 PyLong_AsUnsignedLongLong(PyObject *vv)
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1235 {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1236 PyLongObject *v;
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1237 unsigned PY_LONG_LONG bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1238 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1239 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1240
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1241 if (vv == NULL || !PyLong_Check(vv)) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1242 PyErr_BadInternalCall();
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1243 return (unsigned PY_LONG_LONG)-1;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1244 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1245
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1246 v = (PyLongObject*)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1247 switch(Py_SIZE(v)) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1248 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1249 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1250 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1251
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1252 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1253 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1254 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1255
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1256 /* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
22204
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1257 if (res < 0)
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1258 return (unsigned PY_LONG_LONG)res;
22204
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1259 else
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1260 return bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1261 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1262
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1263 /* Get a C unsigned long int from a long int object, ignoring the high bits.
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1264 Returns -1 and sets an error condition if an error occurs. */
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1265
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1266 static unsigned PY_LONG_LONG
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1267 _PyLong_AsUnsignedLongLongMask(PyObject *vv)
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1268 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1269 register PyLongObject *v;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1270 unsigned PY_LONG_LONG x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1271 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1272 int sign;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1273
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1274 if (vv == NULL || !PyLong_Check(vv)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1275 PyErr_BadInternalCall();
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1276 return (unsigned long) -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1277 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1278 v = (PyLongObject *)vv;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1279 switch(Py_SIZE(v)) {
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1280 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1281 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1282 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1283 i = Py_SIZE(v);
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1284 sign = 1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1285 x = 0;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1286 if (i < 0) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1287 sign = -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1288 i = -i;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1289 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1290 while (--i >= 0) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1291 x = (x << PyLong_SHIFT) + v->ob_digit[i];
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1292 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1293 return x * sign;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1294 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1295
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1296 unsigned PY_LONG_LONG
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1297 PyLong_AsUnsignedLongLongMask(register PyObject *op)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1298 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1299 PyNumberMethods *nb;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1300 PyLongObject *lo;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1301 unsigned PY_LONG_LONG val;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1302
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1303 if (op && PyLong_Check(op))
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1304 return _PyLong_AsUnsignedLongLongMask(op);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1305
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1306 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1307 nb->nb_int == NULL) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1308 PyErr_SetString(PyExc_TypeError, "an integer is required");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1309 return (unsigned PY_LONG_LONG)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1310 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1311
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1312 lo = (PyLongObject*) (*nb->nb_int) (op);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1313 if (lo == NULL)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1314 return (unsigned PY_LONG_LONG)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1315 if (PyLong_Check(lo)) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1316 val = _PyLong_AsUnsignedLongLongMask((PyObject *)lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1317 Py_DECREF(lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1318 if (PyErr_Occurred())
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1319 return (unsigned PY_LONG_LONG)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1320 return val;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1321 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1322 else
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1323 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1324 Py_DECREF(lo);
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1325 PyErr_SetString(PyExc_TypeError,
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1326 "nb_int should return int object");
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1327 return (unsigned PY_LONG_LONG)-1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1328 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1329 }
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1330 #undef IS_LITTLE_ENDIAN
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1331
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1332 #endif /* HAVE_LONG_LONG */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1333
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
1334 #define CHECK_BINOP(v,w) \
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
1335 if (!PyLong_Check(v) || !PyLong_Check(w)) { \
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1336 Py_INCREF(Py_NotImplemented); \
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1337 return Py_NotImplemented; \
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1338 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1339
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1340 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1341 * is modified in place, by adding y to it. Carries are propagated as far as
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1342 * x[m-1], and the remaining carry (0 or 1) is returned.
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1343 */
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1344 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1345 v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1346 {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1347 int i;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1348 digit carry = 0;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1349
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1350 assert(m >= n);
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1351 for (i = 0; i < n; ++i) {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1352 carry += x[i] + y[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1353 x[i] = carry & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1354 carry >>= PyLong_SHIFT;
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1355 assert((carry & 1) == carry);
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1356 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1357 for (; carry && i < m; ++i) {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1358 carry += x[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1359 x[i] = carry & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1360 carry >>= PyLong_SHIFT;
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1361 assert((carry & 1) == carry);
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1362 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1363 return carry;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1364 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1365
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1366 /* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required. x[0:n]
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1367 * is modified in place, by subtracting y from it. Borrows are propagated as
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1368 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1369 */
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1370 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1371 v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1372 {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1373 int i;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1374 digit borrow = 0;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1375
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1376 assert(m >= n);
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1377 for (i = 0; i < n; ++i) {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1378 borrow = x[i] - y[i] - borrow;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1379 x[i] = borrow & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1380 borrow >>= PyLong_SHIFT;
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1381 borrow &= 1; /* keep only 1 sign bit */
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1382 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1383 for (; borrow && i < m; ++i) {
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1384 borrow = x[i] - borrow;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1385 x[i] = borrow & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1386 borrow >>= PyLong_SHIFT;
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1387 borrow &= 1;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1388 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1389 return borrow;
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1390 }
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1391
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1392 /* Multiply by a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1393
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1394 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1395 mul1(PyLongObject *a, wdigit n)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1397 return muladd1(a, n, (digit)0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1398 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 /* Multiply by a single digit and add a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1401
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1402 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1403 muladd1(PyLongObject *a, wdigit n, wdigit extra)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1405 Py_ssize_t size_a = ABS(Py_SIZE(a));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1406 PyLongObject *z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 twodigits carry = extra;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1408 Py_ssize_t i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1409
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1411 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1412 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413 carry += (twodigits)a->ob_digit[i] * n;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1414 z->ob_digit[i] = (digit) (carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1415 carry >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416 }
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
1417 z->ob_digit[i] = (digit) carry;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1418 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1419 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1420
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1421 /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1422 in pout, and returning the remainder. pin and pout point at the LSD.
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1423 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1424 _PyLong_Format, but that should be done with great care since longs are
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1425 immutable. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1426
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1427 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1428 inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1429 {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1430 twodigits rem = 0;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1431
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1432 assert(n > 0 && n <= PyLong_MASK);
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1433 pin += size;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1434 pout += size;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1435 while (--size >= 0) {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1436 digit hi;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1437 rem = (rem << PyLong_SHIFT) + *--pin;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1438 *--pout = hi = (digit)(rem / n);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1439 rem -= hi * n;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1440 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1441 return (digit)rem;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1442 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1443
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 /* Divide a long integer by a digit, returning both the quotient
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1445 (as function result) and the remainder (through *prem).
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1446 The sign of a is ignored; n should not be zero. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1447
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1448 static PyLongObject *
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1449 divrem1(PyLongObject *a, digit n, digit *prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1450 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1451 const Py_ssize_t size = ABS(Py_SIZE(a));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1452 PyLongObject *z;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1453
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1454 assert(n > 0 && n <= PyLong_MASK);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1455 z = _PyLong_New(size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1456 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1457 return NULL;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1458 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1459 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1460 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1461
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1462 /* Convert a long int object to a string, using a given conversion base.
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1463 Return a string object.
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1464 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. */
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1465
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1466 PyObject *
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1467 _PyLong_Format(PyObject *aa, int base)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1468 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1469 register PyLongObject *a = (PyLongObject *)aa;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1470 PyObject *str;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1471 Py_ssize_t i, j, sz;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
1472 Py_ssize_t size_a;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1473 Py_UNICODE *p;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1474 int bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1475 char sign = '\0';
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1476
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1477 if (a == NULL || !PyLong_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1478 PyErr_BadInternalCall();
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1479 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1480 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481 assert(base >= 2 && base <= 36);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1482 size_a = ABS(Py_SIZE(a));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1483
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 /* Compute a rough upper bound for the length of the string */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 i = base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 bits = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 while (i > 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488 ++bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1489 i >>= 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1490 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1491 i = 5;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1492 j = size_a*PyLong_SHIFT + bits-1;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1493 sz = i + j / bits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1494 if (j / PyLong_SHIFT < size_a || sz < i) {
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1495 PyErr_SetString(PyExc_OverflowError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1496 "int is too large to format");
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1497 return NULL;
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1498 }
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1499 str = PyUnicode_FromUnicode(NULL, sz);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1500 if (str == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1501 return NULL;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1502 p = PyUnicode_AS_UNICODE(str) + sz;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1503 *p = '\0';
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1504 if (Py_SIZE(a) < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1505 sign = '-';
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1506
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1507 if (Py_SIZE(a) == 0) {
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1508 *--p = '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1509 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1510 else if ((base & (base - 1)) == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1511 /* JRH: special case for power-of-2 bases */
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1512 twodigits accum = 0;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1513 int accumbits = 0; /* # of bits in accum */
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1514 int basebits = 1; /* # of bits in base-1 */
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1515 i = base;
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1516 while ((i >>= 1) > 1)
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1517 ++basebits;
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1518
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1519 for (i = 0; i < size_a; ++i) {
24919
4cb253c556b1 long_format(), long_lshift(): Someone on c.l.py is trying to boost
Tim Peters <tim.peters@gmail.com>
parents: 24847
diff changeset
1520 accum |= (twodigits)a->ob_digit[i] << accumbits;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1521 accumbits += PyLong_SHIFT;
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1522 assert(accumbits >= basebits);
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1523 do {
21968
62741b25ed65 Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents: 20868
diff changeset
1524 char cdigit = (char)(accum & (base - 1));
35177
27a718221e72 SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents: 34931
diff changeset
1525 cdigit += (cdigit < 10) ? '0' : 'a'-10;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1526 assert(p > PyUnicode_AS_UNICODE(str));
21968
62741b25ed65 Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents: 20868
diff changeset
1527 *--p = cdigit;
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1528 accumbits -= basebits;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1529 accum >>= basebits;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1530 } while (i < size_a-1 ? accumbits >= basebits :
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1531 accum > 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532 }
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1533 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1534 else {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1535 /* Not 0, and base not a power of 2. Divide repeatedly by
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1536 base, but for speed use the highest power of base that
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1537 fits in a digit. */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1538 Py_ssize_t size = size_a;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1539 digit *pin = a->ob_digit;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1540 PyLongObject *scratch;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1541 /* powbasw <- largest power of base that fits in a digit. */
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1542 digit powbase = base; /* powbase == base ** power */
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1543 int power = 1;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1544 for (;;) {
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1545 unsigned long newpow = powbase * (unsigned long)base;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1546 if (newpow >> PyLong_SHIFT) /* doesn't fit in a digit */
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1547 break;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1548 powbase = (digit)newpow;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1549 ++power;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1550 }
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1551
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1552 /* Get a scratch area for repeated division. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1553 scratch = _PyLong_New(size);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1554 if (scratch == NULL) {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1555 Py_DECREF(str);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1556 return NULL;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1557 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1558
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1559 /* Repeatedly divide by powbase. */
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1560 do {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1561 int ntostore = power;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1562 digit rem = inplace_divrem1(scratch->ob_digit,
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1563 pin, size, powbase);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1564 pin = scratch->ob_digit; /* no need to use a again */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1565 if (pin[size - 1] == 0)
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1566 --size;
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1567 SIGCHECK({
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1568 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1569 Py_DECREF(str);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1570 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1571 })
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1572
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1573 /* Break rem into digits. */
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1574 assert(ntostore > 0);
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1575 do {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1576 digit nextrem = (digit)(rem / base);
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1577 char c = (char)(rem - nextrem * base);
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1578 assert(p > PyUnicode_AS_UNICODE(str));
35177
27a718221e72 SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents: 34931
diff changeset
1579 c += (c < 10) ? '0' : 'a'-10;
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1580 *--p = c;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
1581 rem = nextrem;
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1582 --ntostore;
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1583 /* Termination is a bit delicate: must not
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1584 store leading zeroes, so must get out if
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1585 remaining quotient and rem are both 0. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1586 } while (ntostore && (size || rem));
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1587 } while (size != 0);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1588 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1589 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1590
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1591 if (base == 16) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1592 *--p = 'x';
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1593 *--p = '0';
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1594 }
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1595 else if (base == 8) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1596 *--p = 'o';
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1597 *--p = '0';
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1598 }
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1599 else if (base == 2) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1600 *--p = 'b';
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1601 *--p = '0';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1602 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1603 else if (base != 10) {
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1604 *--p = '#';
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1605 *--p = '0' + base%10;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1606 if (base > 10)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1607 *--p = '0' + base/10;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1608 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1609 if (sign)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1610 *--p = sign;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1611 if (p != PyUnicode_AS_UNICODE(str)) {
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1612 Py_UNICODE *q = PyUnicode_AS_UNICODE(str);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1613 assert(p > q);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1614 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1615 } while ((*q++ = *p++) != '\0');
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1616 q--;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1617 if (PyUnicode_Resize(&str, (Py_ssize_t) (q - PyUnicode_AS_UNICODE(str)))) {
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1618 Py_DECREF(str);
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1619 return NULL;
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41587
diff changeset
1620 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1621 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1622 return (PyObject *)str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1623 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1624
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1625 /* Table of digit values for 8-bit string -> integer conversion.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1626 * '0' maps to 0, ..., '9' maps to 9.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1627 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1628 * All other indices map to 37.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1629 * Note that when converting a base B string, a char c is a legitimate
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1630 * base B digit iff _PyLong_DigitValue[Py_CHARPyLong_MASK(c)] < B.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1631 */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1632 int _PyLong_DigitValue[256] = {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1633 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1634 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1635 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1636 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1637 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1638 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1639 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1640 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1641 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1642 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1643 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1644 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1645 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1646 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1647 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1648 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1649 };
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1650
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1651 /* *str points to the first digit in a string of base `base` digits. base
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1652 * is a power of 2 (2, 4, 8, 16, or 32). *str is set to point to the first
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1653 * non-digit (which may be *str!). A normalized long is returned.
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1654 * The point to this routine is that it takes time linear in the number of
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1655 * string characters.
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1656 */
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1657 static PyLongObject *
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1658 long_from_binary_base(char **str, int base)
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1659 {
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1660 char *p = *str;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1661 char *start = p;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1662 int bits_per_char;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1663 Py_ssize_t n;
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1664 PyLongObject *z;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1665 twodigits accum;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1666 int bits_in_accum;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1667 digit *pdigit;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1668
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1669 assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1670 n = base;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1671 for (bits_per_char = -1; n; ++bits_per_char)
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1672 n >>= 1;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1673 /* n <- total # of bits needed, while setting p to end-of-string */
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1674 n = 0;
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
1675 while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1676 ++p;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1677 *str = p;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1678 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1679 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40401
diff changeset
1680 if (n / bits_per_char < p - start) {
27597
8e60ec88ecb8 long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents: 27588
diff changeset
1681 PyErr_SetString(PyExc_ValueError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1682 "int string too large to convert");
27597
8e60ec88ecb8 long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents: 27588
diff changeset
1683 return NULL;
8e60ec88ecb8 long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents: 27588
diff changeset
1684 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1685 n = n / PyLong_SHIFT;
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1686 z = _PyLong_New(n);
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1687 if (z == NULL)
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1688 return NULL;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1689 /* Read string from right, and fill in long from left; i.e.,
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1690 * from least to most significant in both.
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1691 */
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1692 accum = 0;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1693 bits_in_accum = 0;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1694 pdigit = z->ob_digit;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1695 while (--p >= start) {
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
1696 int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
28981
815ff0ab24e7 SF patch 730594: assert from longobject.c, line 1215.
Tim Peters <tim.peters@gmail.com>
parents: 28920
diff changeset
1697 assert(k >= 0 && k < base);
815ff0ab24e7 SF patch 730594: assert from longobject.c, line 1215.
Tim Peters <tim.peters@gmail.com>
parents: 28920
diff changeset
1698 accum |= (twodigits)(k << bits_in_accum);
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1699 bits_in_accum += bits_per_char;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1700 if (bits_in_accum >= PyLong_SHIFT) {
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1701 *pdigit++ = (digit)(accum & PyLong_MASK);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1702 assert(pdigit - z->ob_digit <= (int)n);
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1703 accum >>= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1704 bits_in_accum -= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1705 assert(bits_in_accum < PyLong_SHIFT);
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1706 }
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1707 }
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1708 if (bits_in_accum) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1709 assert(bits_in_accum <= PyLong_SHIFT);
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1710 *pdigit++ = (digit)accum;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1711 assert(pdigit - z->ob_digit <= (int)n);
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1712 }
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1713 while (pdigit - z->ob_digit < n)
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1714 *pdigit++ = 0;
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1715 return long_normalize(z);
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1716 }
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1717
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1718 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1719 PyLong_FromString(char *str, char **pend, int base)
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1720 {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1721 int sign = 1, error_if_nonzero = 0;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1722 char *start, *orig_str = str;
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1723 PyLongObject *z = NULL;
42719
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
1724 PyObject *strobj;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1725 Py_ssize_t slen;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1726
4587
09d94e9eb101 Fix newlongobject so it will work for 64-bit as well as 32-bit hardware
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1727 if ((base != 0 && base < 2) || base > 36) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1728 PyErr_SetString(PyExc_ValueError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
1729 "int() arg 2 must be >= 2 and <= 36");
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1730 return NULL;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1731 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2158
diff changeset
1732 while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1733 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1734 if (*str == '+')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1735 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1736 else if (*str == '-') {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1737 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1738 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1739 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1740 if (base == 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1741 if (str[0] != '0')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1742 base = 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1743 else if (str[1] == 'x' || str[1] == 'X')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1744 base = 16;
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1745 else if (str[1] == 'o' || str[1] == 'O')
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1746 base = 8;
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1747 else if (str[1] == 'b' || str[1] == 'B')
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1748 base = 2;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1749 else {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1750 /* "old" (C-style) octal literal, now invalid.
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1751 it might still be zero though */
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1752 error_if_nonzero = 1;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1753 base = 10;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1754 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1755 }
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1756 if (str[0] == '0' &&
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1757 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1758 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1759 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1760 str += 2;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1761
8533
e5d06a602469 Add check in long-to-int conversion for at least one digit.
Guido van Rossum <guido@python.org>
parents: 8425
diff changeset
1762 start = str;
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1763 if ((base & (base - 1)) == 0)
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1764 z = long_from_binary_base(&str, base);
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1765 else {
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1766 /***
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1767 Binary bases can be converted in time linear in the number of digits, because
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1768 Python's representation base is binary. Other bases (including decimal!) use
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1769 the simple quadratic-time algorithm below, complicated by some speed tricks.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1770
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1771 First some math: the largest integer that can be expressed in N base-B digits
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1772 is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1773 case number of Python digits needed to hold it is the smallest integer n s.t.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1774
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1775 BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1776 BASE**n >= B**N [taking logs to base BASE]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1777 n >= log(B**N)/log(BASE) = N * log(B)/log(BASE)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1778
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1779 The static array log_base_BASE[base] == log(base)/log(BASE) so we can compute
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1780 this quickly. A Python long with that much space is reserved near the start,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1781 and the result is computed into it.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1782
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1783 The input string is actually treated as being in base base**i (i.e., i digits
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1784 are processed at a time), where two more static arrays hold:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1785
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1786 convwidth_base[base] = the largest integer i such that base**i <= BASE
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1787 convmultmax_base[base] = base ** convwidth_base[base]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1788
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1789 The first of these is the largest i such that i consecutive input digits
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1790 must fit in a single Python digit. The second is effectively the input
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1791 base we're really using.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1792
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1793 Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1794 convmultmax_base[base], the result is "simply"
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1795
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1796 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1797
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1798 where B = convmultmax_base[base].
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1799
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1800 Error analysis: as above, the number of Python digits `n` needed is worst-
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1801 case
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1802
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1803 n >= N * log(B)/log(BASE)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1804
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1805 where `N` is the number of input digits in base `B`. This is computed via
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1806
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1807 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1808
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1809 below. Two numeric concerns are how much space this can waste, and whether
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1810 the computed result can be too small. To be concrete, assume BASE = 2**15,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1811 which is the default (and it's unlikely anyone changes that).
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1812
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1813 Waste isn't a problem: provided the first input digit isn't 0, the difference
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1814 between the worst-case input with N digits and the smallest input with N
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1815 digits is about a factor of B, but B is small compared to BASE so at most
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1816 one allocated Python digit can remain unused on that count. If
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1817 N*log(B)/log(BASE) is mathematically an exact integer, then truncating that
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1818 and adding 1 returns a result 1 larger than necessary. However, that can't
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1819 happen: whenever B is a power of 2, long_from_binary_base() is called
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1820 instead, and it's impossible for B**i to be an integer power of 2**15 when
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1821 B is not a power of 2 (i.e., it's impossible for N*log(B)/log(BASE) to be
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1822 an exact integer when B is not a power of 2, since B**i has a prime factor
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1823 other than 2 in that case, but (2**15)**j's only prime factor is 2).
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1824
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1825 The computed result can be too small if the true value of N*log(B)/log(BASE)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1826 is a little bit larger than an exact integer, but due to roundoff errors (in
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1827 computing log(B), log(BASE), their quotient, and/or multiplying that by N)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1828 yields a numeric result a little less than that integer. Unfortunately, "how
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1829 close can a transcendental function get to an integer over some range?"
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1830 questions are generally theoretically intractable. Computer analysis via
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1831 continued fractions is practical: expand log(B)/log(BASE) via continued
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1832 fractions, giving a sequence i/j of "the best" rational approximations. Then
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1833 j*log(B)/log(BASE) is approximately equal to (the integer) i. This shows that
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1834 we can get very close to being in trouble, but very rarely. For example,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1835 76573 is a denominator in one of the continued-fraction approximations to
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1836 log(10)/log(2**15), and indeed:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1837
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1838 >>> log(10)/log(2**15)*76573
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1839 16958.000000654003
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1840
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1841 is very close to an integer. If we were working with IEEE single-precision,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1842 rounding errors could kill us. Finding worst cases in IEEE double-precision
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1843 requires better-than-double-precision log() functions, and Tim didn't bother.
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1844 Instead the code checks to see whether the allocated space is enough as each
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1845 new Python digit is added, and copies the whole thing to a larger long if not.
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1846 This should happen extremely rarely, and in fact I don't have a test case
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1847 that triggers it(!). Instead the code was tested by artificially allocating
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1848 just 1 digit at the start, so that the copying code was exercised for every
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1849 digit beyond the first.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1850 ***/
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1851 register twodigits c; /* current input character */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1852 Py_ssize_t size_z;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1853 int i;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1854 int convwidth;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1855 twodigits convmultmax, convmult;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1856 digit *pz, *pzstop;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1857 char* scan;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1858
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1859 static double log_base_BASE[37] = {0.0e0,};
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1860 static int convwidth_base[37] = {0,};
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1861 static twodigits convmultmax_base[37] = {0,};
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1862
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1863 if (log_base_BASE[base] == 0.0) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1864 twodigits convmax = base;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1865 int i = 1;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1866
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1867 log_base_BASE[base] = log((double)base) /
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1868 log((double)PyLong_BASE);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1869 for (;;) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1870 twodigits next = convmax * base;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1871 if (next > PyLong_BASE)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1872 break;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1873 convmax = next;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1874 ++i;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1875 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1876 convmultmax_base[base] = convmax;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1877 assert(i > 0);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1878 convwidth_base[base] = i;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1879 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1880
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1881 /* Find length of the string of numeric characters. */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1882 scan = str;
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
1883 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1884 ++scan;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1885
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1886 /* Create a long object that can contain the largest possible
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1887 * integer with this base and length. Note that there's no
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1888 * need to initialize z->ob_digit -- no slot is read up before
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1889 * being stored into.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1890 */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1891 size_z = (Py_ssize_t)((scan - str) * log_base_BASE[base]) + 1;
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1892 /* Uncomment next line to test exceedingly rare copy code */
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1893 /* size_z = 1; */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1894 assert(size_z > 0);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1895 z = _PyLong_New(size_z);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1896 if (z == NULL)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1897 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1898 Py_SIZE(z) = 0;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1899
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1900 /* `convwidth` consecutive input digits are treated as a single
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1901 * digit in base `convmultmax`.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1902 */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1903 convwidth = convwidth_base[base];
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1904 convmultmax = convmultmax_base[base];
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1905
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1906 /* Work ;-) */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1907 while (str < scan) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1908 /* grab up to convwidth digits from the input string */
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
1909 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1910 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1911 c = (twodigits)(c * base +
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46145
diff changeset
1912 _PyLong_DigitValue[Py_CHARMASK(*str)]);
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1913 assert(c < PyLong_BASE);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1914 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1915
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1916 convmult = convmultmax;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1917 /* Calculate the shift only if we couldn't get
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1918 * convwidth digits.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1919 */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1920 if (i != convwidth) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1921 convmult = base;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1922 for ( ; i > 1; --i)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1923 convmult *= base;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1924 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1925
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1926 /* Multiply z by convmult, and add c. */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1927 pz = z->ob_digit;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1928 pzstop = pz + Py_SIZE(z);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1929 for (; pz < pzstop; ++pz) {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1930 c += (twodigits)*pz * convmult;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1931 *pz = (digit)(c & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1932 c >>= PyLong_SHIFT;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1933 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1934 /* carry off the current end? */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1935 if (c) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
1936 assert(c < PyLong_BASE);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1937 if (Py_SIZE(z) < size_z) {
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1938 *pz = (digit)c;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1939 ++Py_SIZE(z);
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1940 }
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1941 else {
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1942 PyLongObject *tmp;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1943 /* Extremely rare. Get more space. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1944 assert(Py_SIZE(z) == size_z);
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1945 tmp = _PyLong_New(size_z + 1);
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1946 if (tmp == NULL) {
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1947 Py_DECREF(z);
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1948 return NULL;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1949 }
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1950 memcpy(tmp->ob_digit,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1951 z->ob_digit,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1952 sizeof(digit) * size_z);
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1953 Py_DECREF(z);
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1954 z = tmp;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1955 z->ob_digit[size_z] = (digit)c;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1956 ++size_z;
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1957 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1958 }
27587
7283140d4319 long(string, base) now takes time linear in len(string) when base is a
Tim Peters <tim.peters@gmail.com>
parents: 27585
diff changeset
1959 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1960 }
8808
78785985a2ab Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents: 8533
diff changeset
1961 if (z == NULL)
78785985a2ab Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents: 8533
diff changeset
1962 return NULL;
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1963 if (error_if_nonzero) {
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1964 /* reset the base to 0, else the exception message
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1965 doesn't make too much sense */
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1966 base = 0;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1967 if (Py_SIZE(z) != 0)
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1968 goto onError;
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1969 /* there might still be other problems, therefore base
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1970 remains zero here for the same reason */
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
1971 }
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1972 if (str == start)
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1973 goto onError;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1974 if (sign < 0)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
1975 Py_SIZE(z) = -(Py_SIZE(z));
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1976 if (*str == 'L' || *str == 'l')
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1977 str++;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1978 while (*str && isspace(Py_CHARMASK(*str)))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1979 str++;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1980 if (*str != '\0')
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1981 goto onError;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1982 if (pend)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1983 *pend = str;
48385
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1984 long_normalize(z);
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1985 if (ABS(Py_SIZE(z)) <= 1) {
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1986 long res = MEDIUM_VALUE(z);
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1987 if (-NSMALLPOSINTS <= res && res <= NSMALLPOSINTS) {
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1988 Py_DECREF(z);
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1989 return PyLong_FromLong(res);
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1990 }
25412c6d2776 Issue #3236: Return small longs from PyLong_FromString.
Martin v. Löwis <martin@v.loewis.de>
parents: 47620
diff changeset
1991 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1992 return (PyObject *) z;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1993
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1994 onError:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1995 Py_XDECREF(z);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1996 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
42719
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
1997 strobj = PyUnicode_FromStringAndSize(orig_str, slen);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1998 if (strobj == NULL)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
1999 return NULL;
42719
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
2000 PyErr_Format(PyExc_ValueError,
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
2001 "invalid literal for int() with base %d: %R",
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
2002 base, strobj);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2003 Py_DECREF(strobj);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2004 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2005 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2006
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2007 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2008 PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2009 {
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2010 PyObject *result;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
2011 char *buffer = (char *)PyMem_MALLOC(length+1);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2012
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2013 if (buffer == NULL)
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2014 return NULL;
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2015
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2016 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2017 PyMem_FREE(buffer);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2018 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
2019 }
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2020 result = PyLong_FromString(buffer, NULL, base);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2021 PyMem_FREE(buffer);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
2022 return result;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2023 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2024
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2025 /* forward */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2026 static PyLongObject *x_divrem
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2027 (PyLongObject *, PyLongObject *, PyLongObject **);
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
2028 static PyObject *long_long(PyObject *v);
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2029 static int long_divrem(PyLongObject *, PyLongObject *,
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2030 PyLongObject **, PyLongObject **);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2031
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2032 /* Long division with remainder, top-level routine */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2033
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2034 static int
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2035 long_divrem(PyLongObject *a, PyLongObject *b,
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2036 PyLongObject **pdiv, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2037 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2038 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2039 PyLongObject *z;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2040
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2041 if (size_b == 0) {
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2042 PyErr_SetString(PyExc_ZeroDivisionError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2043 "integer division or modulo by zero");
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2044 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2045 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2046 if (size_a < size_b ||
4587
09d94e9eb101 Fix newlongobject so it will work for 64-bit as well as 32-bit hardware
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2047 (size_a == size_b &&
09d94e9eb101 Fix newlongobject so it will work for 64-bit as well as 32-bit hardware
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2048 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2049 /* |a| < |b|. */
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2050 *pdiv = (PyLongObject*)PyLong_FromLong(0);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2051 if (*pdiv == NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2052 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2053 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2054 *prem = (PyLongObject *) a;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2055 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2056 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2057 if (size_b == 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2058 digit rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2059 z = divrem1(a, b->ob_digit[0], &rem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2060 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2061 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2062 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2063 if (*prem == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2064 Py_DECREF(z);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2065 return -1;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
2066 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2067 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2068 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2069 z = x_divrem(a, b, prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2070 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2071 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2072 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2073 /* Set the signs.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2074 The quotient z has the sign of a*b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2075 the remainder r has the sign of a,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2076 so a = b*z + r. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2077 if ((Py_SIZE(a) < 0) != (Py_SIZE(b) < 0))
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2078 NEGATE(z);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2079 if (Py_SIZE(a) < 0 && Py_SIZE(*prem) != 0)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2080 NEGATE(*prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2081 *pdiv = z;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2082 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2083 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2084
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2085 /* Unsigned long division with remainder -- the algorithm */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2086
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2087 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2088 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2089 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2090 Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2091 digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2092 PyLongObject *v = mul1(v1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2093 PyLongObject *w = mul1(w1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2094 PyLongObject *a;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2095 Py_ssize_t j, k;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2096
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2097 if (v == NULL || w == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2098 Py_XDECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2099 Py_XDECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2100 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2101 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2102
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2103 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2104 assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2105 assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2106
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2107 size_v = ABS(Py_SIZE(v));
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2108 k = size_v - size_w;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2109 a = _PyLong_New(k + 1);
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2110
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
2111 for (j = size_v; a != NULL && k >= 0; --j, --k) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2112 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2113 twodigits q;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2114 stwodigits carry = 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2115 int i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2116
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
2117 SIGCHECK({
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2118 Py_DECREF(a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2119 a = NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2120 break;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2121 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2122 if (vj == w->ob_digit[size_w-1])
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2123 q = PyLong_MASK;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2124 else
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2125 q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2126 w->ob_digit[size_w-1];
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2127
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2128 while (w->ob_digit[size_w-2]*q >
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2129 ((
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2130 ((twodigits)vj << PyLong_SHIFT)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2131 + v->ob_digit[j-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2132 - q*w->ob_digit[size_w-1]
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2133 ) << PyLong_SHIFT)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2134 + v->ob_digit[j-2])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2135 --q;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2136
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2137 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2138 twodigits z = w->ob_digit[i] * q;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2139 digit zz = (digit) (z >> PyLong_SHIFT);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2140 carry += v->ob_digit[i+k] - z
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2141 + ((twodigits)zz << PyLong_SHIFT);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2142 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
2143 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2144 carry, PyLong_SHIFT);
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
2145 carry -= zz;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2146 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2147
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2148 if (i+k < size_v) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2149 carry += v->ob_digit[i+k];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2150 v->ob_digit[i+k] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2151 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2152
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2153 if (carry == 0)
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
2154 a->ob_digit[k] = (digit) q;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2155 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2156 assert(carry == -1);
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
2157 a->ob_digit[k] = (digit) q-1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2158 carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2159 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2160 carry += v->ob_digit[i+k] + w->ob_digit[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2161 v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
2162 carry = Py_ARITHMETIC_RIGHT_SHIFT(
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
2163 BASE_TWODIGITS_TYPE,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2164 carry, PyLong_SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2165 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2166 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2167 } /* for j, k */
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2168
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
2169 if (a == NULL)
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
2170 *prem = NULL;
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
2171 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2172 a = long_normalize(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2173 *prem = divrem1(v, d, &d);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2174 /* d receives the (unused) remainder */
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2175 if (*prem == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2176 Py_DECREF(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2177 a = NULL;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2178 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2179 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2180 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2181 Py_DECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2182 return a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2183 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2184
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2185 /* Methods */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2186
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2187 static void
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2188 long_dealloc(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2189 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2190 Py_TYPE(v)->tp_free(v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2191 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2192
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2193 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2194 long_repr(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2195 {
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41827
diff changeset
2196 return _PyLong_Format(v, 10);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2197 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2198
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2199 static int
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2200 long_compare(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2201 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2202 Py_ssize_t sign;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2203
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2204 if (Py_SIZE(a) != Py_SIZE(b)) {
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2205 if (ABS(Py_SIZE(a)) == 0 && ABS(Py_SIZE(b)) == 0)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2206 sign = 0;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2207 else
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2208 sign = Py_SIZE(a) - Py_SIZE(b);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2209 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2210 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2211 Py_ssize_t i = ABS(Py_SIZE(a));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2212 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2213 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2214 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2215 sign = 0;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
2216 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2217 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2218 if (Py_SIZE(a) < 0)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
2219 sign = -sign;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
2220 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2221 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2222 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2223 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2224
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
2225 static PyObject *
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
2226 long_richcompare(PyObject *self, PyObject *other, int op)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
2227 {
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2228 PyObject *result;
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2229 CHECK_BINOP(self, other);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2230 result = Py_CmpToRich(op, long_compare((PyLongObject*)self,
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2231 (PyLongObject*)other));
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2232 return result;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
2233 }
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
2234
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2235 static long
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2236 long_hash(PyLongObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2237 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2238 long x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2239 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2240 int sign;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2241
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2242 /* This is designed so that Python ints and longs with the
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2243 same value hash to the same value, otherwise comparisons
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2244 of mapping keys will turn out weird */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2245 i = Py_SIZE(v);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2246 switch(i) {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2247 case -1: return v->ob_digit[0]==1 ? -2 : -v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2248 case 0: return 0;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2249 case 1: return v->ob_digit[0];
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2250 }
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2251 sign = 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2252 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2253 if (i < 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2254 sign = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2255 i = -(i);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2256 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2257 #define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
43410
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2258 /* The following loop produces a C long x such that (unsigned long)x
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2259 is congruent to the absolute value of v modulo ULONG_MAX. The
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2260 resulting x is nonzero if and only if v is. */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2261 while (--i >= 0) {
28032
53bef20f84cc Fix SF bug #689659, 64-bit int and long hash keys incompatible
Neal Norwitz <nnorwitz@gmail.com>
parents: 27639
diff changeset
2262 /* Force a native long #-bits (32 or 64) circular shift */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2263 x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2264 x += v->ob_digit[i];
43410
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2265 /* If the addition above overflowed (thinking of x as
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2266 unsigned), we compensate by incrementing. This preserves
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2267 the value modulo ULONG_MAX. */
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2268 if ((unsigned long)x < v->ob_digit[i])
b06b191b7cfa Merged revisions 58203-58210 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 43313
diff changeset
2269 x++;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2270 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2271 #undef LONG_BIT_PyLong_SHIFT
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2272 x = x * sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2273 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2274 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2275 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2276 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2277
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2278
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2279 /* Add the absolute values of two long integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2280
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2281 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2282 x_add(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2283 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2284 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2285 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2286 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2287 digit carry = 0;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
2288
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2289 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2290 if (size_a < size_b) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2291 { PyLongObject *temp = a; a = b; b = temp; }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2292 { Py_ssize_t size_temp = size_a;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2293 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2294 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2295 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2296 z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2297 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2298 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2299 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2300 carry += a->ob_digit[i] + b->ob_digit[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2301 z->ob_digit[i] = carry & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2302 carry >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2303 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2304 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2305 carry += a->ob_digit[i];
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2306 z->ob_digit[i] = carry & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2307 carry >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2308 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2309 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2310 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2311 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2312
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2313 /* Subtract the absolute values of two integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2314
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2315 static PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2316 x_sub(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2317 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2318 Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2319 PyLongObject *z;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2320 Py_ssize_t i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2321 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2322 digit borrow = 0;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
2323
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2324 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2325 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2326 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2327 { PyLongObject *temp = a; a = b; b = temp; }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2328 { Py_ssize_t size_temp = size_a;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2329 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2330 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2331 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2332 else if (size_a == size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2333 /* Find highest digit where a and b differ: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2334 i = size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2335 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2336 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2337 if (i < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2338 return _PyLong_New(0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2339 if (a->ob_digit[i] < b->ob_digit[i]) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2340 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2341 { PyLongObject *temp = a; a = b; b = temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2342 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2343 size_a = size_b = i+1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2344 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2345 z = _PyLong_New(size_a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2346 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2347 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2348 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2349 /* The following assumes unsigned arithmetic
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2350 works module 2**N for some N>PyLong_SHIFT. */
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
2351 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2352 z->ob_digit[i] = borrow & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2353 borrow >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2354 borrow &= 1; /* Keep only one sign bit */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2355 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2356 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2357 borrow = a->ob_digit[i] - borrow;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2358 z->ob_digit[i] = borrow & PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2359 borrow >>= PyLong_SHIFT;
13420
d3f1eefdcb3a The tail end of x_sub implicitly assumed that an unsigned short
Tim Peters <tim.peters@gmail.com>
parents: 13418
diff changeset
2360 borrow &= 1; /* Keep only one sign bit */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2361 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2362 assert(borrow == 0);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2363 if (sign < 0)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2364 NEGATE(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2365 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2366 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2367
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2368 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2369 long_add(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2370 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2371 PyLongObject *z;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2372
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2373 CHECK_BINOP(a, b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2374
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2375 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
2376 PyObject *result = PyLong_FromLong(MEDIUM_VALUE(a) +
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2377 MEDIUM_VALUE(b));
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2378 return result;
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2379 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2380 if (Py_SIZE(a) < 0) {
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2381 if (Py_SIZE(b) < 0) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2382 z = x_add(a, b);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2383 if (z != NULL && Py_SIZE(z) != 0)
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2384 Py_SIZE(z) = -(Py_SIZE(z));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2385 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2386 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2387 z = x_sub(b, a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2388 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2389 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2390 if (Py_SIZE(b) < 0)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2391 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2392 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2393 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2394 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2395 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2396 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2397
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2398 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2399 long_sub(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2400 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2401 PyLongObject *z;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2402
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2403 CHECK_BINOP(a, b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2404
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2405 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2406 PyObject* r;
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2407 r = PyLong_FromLong(MEDIUM_VALUE(a)-MEDIUM_VALUE(b));
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2408 return r;
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2409 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2410 if (Py_SIZE(a) < 0) {
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2411 if (Py_SIZE(b) < 0)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2412 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2413 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2414 z = x_add(a, b);
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2415 if (z != NULL && Py_SIZE(z) != 0)
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2416 Py_SIZE(z) = -(Py_SIZE(z));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2417 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2418 else {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2419 if (Py_SIZE(b) < 0)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2420 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2421 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2422 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2423 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2424 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2425 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2426
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2427 /* Grade school multiplication, ignoring the signs.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2428 * Returns the absolute value of the product, or NULL if error.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2429 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2430 static PyLongObject *
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2431 x_mul(PyLongObject *a, PyLongObject *b)
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2432 {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2433 PyLongObject *z;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2434 Py_ssize_t size_a = ABS(Py_SIZE(a));
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2435 Py_ssize_t size_b = ABS(Py_SIZE(b));
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2436 Py_ssize_t i;
19925
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
2437
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2438 z = _PyLong_New(size_a + size_b);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2439 if (z == NULL)
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2440 return NULL;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2441
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2442 memset(z->ob_digit, 0, Py_SIZE(z) * sizeof(digit));
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2443 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2444 /* Efficient squaring per HAC, Algorithm 14.16:
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2445 * http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2446 * Gives slightly less than a 2x speedup when a == b,
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2447 * via exploiting that each entry in the multiplication
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2448 * pyramid appears twice (except for the size_a squares).
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2449 */
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2450 for (i = 0; i < size_a; ++i) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2451 twodigits carry;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2452 twodigits f = a->ob_digit[i];
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2453 digit *pz = z->ob_digit + (i << 1);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2454 digit *pa = a->ob_digit + i + 1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2455 digit *paend = a->ob_digit + size_a;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2456
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2457 SIGCHECK({
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2458 Py_DECREF(z);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2459 return NULL;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2460 })
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2461
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2462 carry = *pz + f * f;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2463 *pz++ = (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2464 carry >>= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2465 assert(carry <= PyLong_MASK);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2466
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2467 /* Now f is added in twice in each column of the
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2468 * pyramid it appears. Same as adding f<<1 once.
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2469 */
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2470 f <<= 1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2471 while (pa < paend) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2472 carry += *pz + *pa++ * f;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2473 *pz++ = (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2474 carry >>= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2475 assert(carry <= (PyLong_MASK << 1));
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2476 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2477 if (carry) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2478 carry += *pz;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2479 *pz++ = (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2480 carry >>= PyLong_SHIFT;
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2481 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2482 if (carry)
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2483 *pz += (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2484 assert((carry >> PyLong_SHIFT) == 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2485 }
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2486 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2487 else { /* a is not the same as b -- gradeschool long mult */
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2488 for (i = 0; i < size_a; ++i) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2489 twodigits carry = 0;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2490 twodigits f = a->ob_digit[i];
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2491 digit *pz = z->ob_digit + i;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2492 digit *pb = b->ob_digit;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2493 digit *pbend = b->ob_digit + size_b;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2494
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2495 SIGCHECK({
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2496 Py_DECREF(z);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2497 return NULL;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2498 })
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2499
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2500 while (pb < pbend) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2501 carry += *pz + *pb++ * f;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2502 *pz++ = (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2503 carry >>= PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2504 assert(carry <= PyLong_MASK);
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2505 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2506 if (carry)
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2507 *pz += (digit)(carry & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2508 assert((carry >> PyLong_SHIFT) == 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2509 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2510 }
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2511 return long_normalize(z);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2512 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2513
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2514 /* A helper for Karatsuba multiplication (k_mul).
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2515 Takes a long "n" and an integer "size" representing the place to
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2516 split, and sets low and high such that abs(n) == (high << size) + low,
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2517 viewing the shift as being by digits. The sign bit is ignored, and
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2518 the return values are >= 0.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2519 Returns 0 on success, -1 on failure.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2520 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2521 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2522 kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low)
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2523 {
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2524 PyLongObject *hi, *lo;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2525 Py_ssize_t size_lo, size_hi;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2526 const Py_ssize_t size_n = ABS(Py_SIZE(n));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2527
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2528 size_lo = MIN(size_n, size);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2529 size_hi = size_n - size_lo;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2530
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2531 if ((hi = _PyLong_New(size_hi)) == NULL)
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2532 return -1;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2533 if ((lo = _PyLong_New(size_lo)) == NULL) {
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2534 Py_DECREF(hi);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2535 return -1;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2536 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2537
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2538 memcpy(lo->ob_digit, n->ob_digit, size_lo * sizeof(digit));
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2539 memcpy(hi->ob_digit, n->ob_digit + size_lo, size_hi * sizeof(digit));
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2540
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2541 *high = long_normalize(hi);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2542 *low = long_normalize(lo);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2543 return 0;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2544 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2545
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2546 static PyLongObject *k_lopsided_mul(PyLongObject *a, PyLongObject *b);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2547
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2548 /* Karatsuba multiplication. Ignores the input signs, and returns the
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2549 * absolute value of the product (or NULL if error).
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2550 * See Knuth Vol. 2 Chapter 4.3.3 (Pp. 294-295).
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2551 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2552 static PyLongObject *
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2553 k_mul(PyLongObject *a, PyLongObject *b)
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2554 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2555 Py_ssize_t asize = ABS(Py_SIZE(a));
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2556 Py_ssize_t bsize = ABS(Py_SIZE(b));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2557 PyLongObject *ah = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2558 PyLongObject *al = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2559 PyLongObject *bh = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2560 PyLongObject *bl = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2561 PyLongObject *ret = NULL;
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2562 PyLongObject *t1, *t2, *t3;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2563 Py_ssize_t shift; /* the number of digits we split off */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2564 Py_ssize_t i;
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2565
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2566 /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2567 * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh + ah*bh + al*bl
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2568 * Then the original product is
24746
49169d00ab18 k_mul(): Repaired typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 24745
diff changeset
2569 * ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2570 * By picking X to be a power of 2, "*X" is just shifting, and it's
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2571 * been reduced to 3 multiplies on numbers half the size.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2572 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2573
24747
19e010f792a3 k_mul(): Repaired another typo in another comment.
Tim Peters <tim.peters@gmail.com>
parents: 24746
diff changeset
2574 /* We want to split based on the larger number; fiddle so that b
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2575 * is largest.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2576 */
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2577 if (asize > bsize) {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2578 t1 = a;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2579 a = b;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2580 b = t1;
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2581
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2582 i = asize;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2583 asize = bsize;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2584 bsize = i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2585 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2586
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2587 /* Use gradeschool math when either number is too small. */
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2588 i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2589 if (asize <= i) {
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2590 if (asize == 0)
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2591 return _PyLong_New(0);
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2592 else
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2593 return x_mul(a, b);
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2594 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2595
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2596 /* If a is small compared to b, splitting on b gives a degenerate
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2597 * case with ah==0, and Karatsuba may be (even much) less efficient
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2598 * than "grade school" then. However, we can still win, by viewing
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2599 * b as a string of "big digits", each of width a->ob_size. That
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2600 * leads to a sequence of balanced calls to k_mul.
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2601 */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2602 if (2 * asize <= bsize)
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2603 return k_lopsided_mul(a, b);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2604
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2605 /* Split a & b into hi & lo pieces. */
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2606 shift = bsize >> 1;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2607 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2608 assert(Py_SIZE(ah) > 0); /* the split isn't degenerate */
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2609
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2610 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2611 bh = ah;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2612 bl = al;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2613 Py_INCREF(bh);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2614 Py_INCREF(bl);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2615 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2616 else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2617
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2618 /* The plan:
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2619 * 1. Allocate result space (asize + bsize digits: that's always
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2620 * enough).
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2621 * 2. Compute ah*bh, and copy into result at 2*shift.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2622 * 3. Compute al*bl, and copy into result at 0. Note that this
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2623 * can't overlap with #2.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2624 * 4. Subtract al*bl from the result, starting at shift. This may
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2625 * underflow (borrow out of the high digit), but we don't care:
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2626 * we're effectively doing unsigned arithmetic mod
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2627 * BASE**(sizea + sizeb), and so long as the *final* result fits,
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2628 * borrows and carries out of the high digit can be ignored.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2629 * 5. Subtract ah*bh from the result, starting at shift.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2630 * 6. Compute (ah+al)*(bh+bl), and add it into the result starting
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2631 * at shift.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2632 */
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2633
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2634 /* 1. Allocate result space. */
24764
f0bd5cd4b170 k_mul(): Heh -- I checked in two fixes for the last problem. Only keep
Tim Peters <tim.peters@gmail.com>
parents: 24763
diff changeset
2635 ret = _PyLong_New(asize + bsize);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2636 if (ret == NULL) goto fail;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2637 #ifdef Py_DEBUG
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2638 /* Fill with trash, to catch reference to uninitialized digits. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2639 memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2640 #endif
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2641
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2642 /* 2. t1 <- ah*bh, and copy into high digits of result. */
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2643 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2644 assert(Py_SIZE(t1) >= 0);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2645 assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret));
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2646 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2647 Py_SIZE(t1) * sizeof(digit));
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2648
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2649 /* Zero-out the digits higher than the ah*bh copy. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2650 i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1);
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2651 if (i)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2652 memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0,
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2653 i * sizeof(digit));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2654
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2655 /* 3. t2 <- al*bl, and copy into the low digits. */
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2656 if ((t2 = k_mul(al, bl)) == NULL) {
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2657 Py_DECREF(t1);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2658 goto fail;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2659 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2660 assert(Py_SIZE(t2) >= 0);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2661 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2662 memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2663
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2664 /* Zero out remaining digits. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2665 i = 2*shift - Py_SIZE(t2); /* number of uninitialized digits */
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2666 if (i)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2667 memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2668
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2669 /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2). We do al*bl first
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2670 * because it's fresher in cache.
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2671 */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2672 i = Py_SIZE(ret) - shift; /* # digits after shift */
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2673 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2));
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2674 Py_DECREF(t2);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2675
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2676 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1));
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2677 Py_DECREF(t1);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2678
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2679 /* 6. t3 <- (ah+al)(bh+bl), and add into result. */
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2680 if ((t1 = x_add(ah, al)) == NULL) goto fail;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2681 Py_DECREF(ah);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2682 Py_DECREF(al);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2683 ah = al = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2684
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2685 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2686 t2 = t1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2687 Py_INCREF(t2);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2688 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2689 else if ((t2 = x_add(bh, bl)) == NULL) {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2690 Py_DECREF(t1);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2691 goto fail;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2692 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2693 Py_DECREF(bh);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2694 Py_DECREF(bl);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2695 bh = bl = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2696
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2697 t3 = k_mul(t1, t2);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2698 Py_DECREF(t1);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2699 Py_DECREF(t2);
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2700 if (t3 == NULL) goto fail;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2701 assert(Py_SIZE(t3) >= 0);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2702
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2703 /* Add t3. It's not obvious why we can't run out of room here.
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2704 * See the (*) comment after this function.
24763
5ed7c977d2a9 k_mul(): White-box testing turned up that (ah+al)*(bh+bl) can, in rare
Tim Peters <tim.peters@gmail.com>
parents: 24760
diff changeset
2705 */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2706 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3));
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2707 Py_DECREF(t3);
24750
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
2708
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2709 return long_normalize(ret);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2710
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2711 fail:
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2712 Py_XDECREF(ret);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2713 Py_XDECREF(ah);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2714 Py_XDECREF(al);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2715 Py_XDECREF(bh);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2716 Py_XDECREF(bl);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2717 return NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2718 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2719
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2720 /* (*) Why adding t3 can't "run out of room" above.
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2721
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2722 Let f(x) mean the floor of x and c(x) mean the ceiling of x. Some facts
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2723 to start with:
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2724
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2725 1. For any integer i, i = c(i/2) + f(i/2). In particular,
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2726 bsize = c(bsize/2) + f(bsize/2).
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2727 2. shift = f(bsize/2)
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2728 3. asize <= bsize
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2729 4. Since we call k_lopsided_mul if asize*2 <= bsize, asize*2 > bsize in this
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2730 routine, so asize > bsize/2 >= f(bsize/2) in this routine.
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2731
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2732 We allocated asize + bsize result digits, and add t3 into them at an offset
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2733 of shift. This leaves asize+bsize-shift allocated digit positions for t3
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2734 to fit into, = (by #1 and #2) asize + f(bsize/2) + c(bsize/2) - f(bsize/2) =
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2735 asize + c(bsize/2) available digit positions.
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2736
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2737 bh has c(bsize/2) digits, and bl at most f(size/2) digits. So bh+hl has
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2738 at most c(bsize/2) digits + 1 bit.
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2739
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2740 If asize == bsize, ah has c(bsize/2) digits, else ah has at most f(bsize/2)
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2741 digits, and al has at most f(bsize/2) digits in any case. So ah+al has at
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2742 most (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 1 bit.
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2743
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2744 The product (ah+al)*(bh+bl) therefore has at most
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2745
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2746 c(bsize/2) + (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2747
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2748 and we have asize + c(bsize/2) available digit positions. We need to show
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2749 this is always enough. An instance of c(bsize/2) cancels out in both, so
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2750 the question reduces to whether asize digits is enough to hold
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2751 (asize == bsize ? c(bsize/2) : f(bsize/2)) digits + 2 bits. If asize < bsize,
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2752 then we're asking whether asize digits >= f(bsize/2) digits + 2 bits. By #4,
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2753 asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2754 digit is enough to hold 2 bits. This is so since PyLong_SHIFT=15 >= 2. If
24846
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2755 asize == bsize, then we're asking whether bsize digits is enough to hold
24847
873f12f68e3b Illustrating by example one good reason not to trust a proof <wink>.
Tim Peters <tim.peters@gmail.com>
parents: 24846
diff changeset
2756 c(bsize/2) digits + 2 bits, or equivalently (by #1) whether f(bsize/2) digits
873f12f68e3b Illustrating by example one good reason not to trust a proof <wink>.
Tim Peters <tim.peters@gmail.com>
parents: 24846
diff changeset
2757 is enough to hold 2 bits. This is so if bsize >= 2, which holds because
873f12f68e3b Illustrating by example one good reason not to trust a proof <wink>.
Tim Peters <tim.peters@gmail.com>
parents: 24846
diff changeset
2758 bsize >= KARATSUBA_CUTOFF >= 2.
24821
ae9c9f90b4b0 k_mul() comments: Explained why there's always enough room to subtract
Tim Peters <tim.peters@gmail.com>
parents: 24792
diff changeset
2759
24824
7c91d303c563 k_mul() comments: Simplified the simplified explanation of why ah*bh and
Tim Peters <tim.peters@gmail.com>
parents: 24821
diff changeset
2760 Note that since there's always enough room for (ah+al)*(bh+bl), and that's
7c91d303c563 k_mul() comments: Simplified the simplified explanation of why ah*bh and
Tim Peters <tim.peters@gmail.com>
parents: 24821
diff changeset
2761 clearly >= each of ah*bh and al*bl, there's always enough room to subtract
7c91d303c563 k_mul() comments: Simplified the simplified explanation of why ah*bh and
Tim Peters <tim.peters@gmail.com>
parents: 24821
diff changeset
2762 ah*bh and al*bl too.
24791
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2763 */
dbf4cc560898 k_mul(): The fix for (ah+al)*(bh+bl) spilling 1 bit beyond the allocated
Tim Peters <tim.peters@gmail.com>
parents: 24777
diff changeset
2764
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2765 /* b has at least twice the digits of a, and a is big enough that Karatsuba
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2766 * would pay off *if* the inputs had balanced sizes. View b as a sequence
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2767 * of slices, each with a->ob_size digits, and multiply the slices by a,
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2768 * one at a time. This gives k_mul balanced inputs to work with, and is
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2769 * also cache-friendly (we compute one double-width slice of the result
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2770 * at a time, then move on, never bactracking except for the helpful
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2771 * single-width slice overlap between successive partial sums).
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2772 */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2773 static PyLongObject *
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2774 k_lopsided_mul(PyLongObject *a, PyLongObject *b)
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2775 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2776 const Py_ssize_t asize = ABS(Py_SIZE(a));
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2777 Py_ssize_t bsize = ABS(Py_SIZE(b));
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2778 Py_ssize_t nbdone; /* # of b digits already multiplied */
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2779 PyLongObject *ret;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2780 PyLongObject *bslice = NULL;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2781
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2782 assert(asize > KARATSUBA_CUTOFF);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2783 assert(2 * asize <= bsize);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2784
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2785 /* Allocate result space, and zero it out. */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2786 ret = _PyLong_New(asize + bsize);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2787 if (ret == NULL)
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2788 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2789 memset(ret->ob_digit, 0, Py_SIZE(ret) * sizeof(digit));
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2790
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2791 /* Successive slices of b are copied into bslice. */
24775
6c6df69236b1 k_lopsided_mul(): This allocated more space for bslice than necessary.
Tim Peters <tim.peters@gmail.com>
parents: 24773
diff changeset
2792 bslice = _PyLong_New(asize);
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2793 if (bslice == NULL)
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2794 goto fail;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2795
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2796 nbdone = 0;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2797 while (bsize > 0) {
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2798 PyLongObject *product;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2799 const Py_ssize_t nbtouse = MIN(bsize, asize);
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2800
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2801 /* Multiply the next slice of b by a. */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2802 memcpy(bslice->ob_digit, b->ob_digit + nbdone,
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2803 nbtouse * sizeof(digit));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2804 Py_SIZE(bslice) = nbtouse;
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2805 product = k_mul(a, bslice);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2806 if (product == NULL)
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2807 goto fail;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2808
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2809 /* Add into result. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2810 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2811 product->ob_digit, Py_SIZE(product));
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2812 Py_DECREF(product);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2813
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2814 bsize -= nbtouse;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2815 nbdone += nbtouse;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2816 }
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2817
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2818 Py_DECREF(bslice);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2819 return long_normalize(ret);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2820
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2821 fail:
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2822 Py_DECREF(ret);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2823 Py_XDECREF(bslice);
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2824 return NULL;
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2825 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2826
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2827 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2828 long_mul(PyLongObject *a, PyLongObject *b)
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2829 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2830 PyLongObject *z;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2831
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2832 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2833
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2834 if (ABS(Py_SIZE(a)) <= 1 && ABS(Py_SIZE(b)) <= 1) {
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2835 PyObject *r;
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2836 r = PyLong_FromLong(MEDIUM_VALUE(a)*MEDIUM_VALUE(b));
40751
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2837 return r;
d594ec858fe9 Fix refcounting bugs related to CONVERT_BINOP.
Martin v. Löwis <martin@v.loewis.de>
parents: 40641
diff changeset
2838 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2839
24759
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2840 z = k_mul(a, b);
24845
6939d72182e6 long_mul(): Simplified exit code. In particular, k_mul() returns a
Tim Peters <tim.peters@gmail.com>
parents: 24824
diff changeset
2841 /* Negate if exactly one of the inputs is negative. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2842 if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2843 NEGATE(z);
24845
6939d72182e6 long_mul(): Simplified exit code. In particular, k_mul() returns a
Tim Peters <tim.peters@gmail.com>
parents: 24824
diff changeset
2844 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2845 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2846
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2847 /* The / and % operators are now defined in terms of divmod().
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2848 The expression a mod b has the value a - b*floor(a/b).
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2849 The long_divrem function gives the remainder after division of
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2850 |a| by |b|, with the sign of a. This is also expressed
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2851 as a - b*trunc(a/b), if trunc truncates towards zero.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2852 Some examples:
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2853 a b a rem b a mod b
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2854 13 10 3 3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2855 -13 10 -3 7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2856 13 -10 3 -7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2857 -13 -10 -3 -3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2858 So, to get from rem to mod, we have to add b if a and b
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2859 have different signs. We then subtract one from the 'div'
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2860 part of the outcome to keep the invariant intact. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2861
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2862 /* Compute
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2863 * *pdiv, *pmod = divmod(v, w)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2864 * NULL can be passed for pdiv or pmod, in which case that part of
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2865 * the result is simply thrown away. The caller owns a reference to
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2866 * each of these it requests (does not pass NULL for).
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2867 */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2868 static int
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2869 l_divmod(PyLongObject *v, PyLongObject *w,
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2870 PyLongObject **pdiv, PyLongObject **pmod)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2871 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2872 PyLongObject *div, *mod;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2873
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2874 if (long_divrem(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2875 return -1;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2876 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
2877 (Py_SIZE(mod) > 0 && Py_SIZE(w) < 0)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2878 PyLongObject *temp;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2879 PyLongObject *one;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2880 temp = (PyLongObject *) long_add(mod, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2881 Py_DECREF(mod);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2882 mod = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2883 if (mod == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2884 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2885 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2886 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2887 one = (PyLongObject *) PyLong_FromLong(1L);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2888 if (one == NULL ||
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2889 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2890 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2891 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2892 Py_XDECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2893 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2894 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2895 Py_DECREF(one);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2896 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2897 div = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2898 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2899 if (pdiv != NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2900 *pdiv = div;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2901 else
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2902 Py_DECREF(div);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2903
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2904 if (pmod != NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2905 *pmod = mod;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2906 else
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2907 Py_DECREF(mod);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2908
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2909 return 0;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2910 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2911
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2912 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2913 long_div(PyObject *a, PyObject *b)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2914 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2915 PyLongObject *div;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2916
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2917 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2918 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, NULL) < 0)
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2919 div = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2920 return (PyObject *)div;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2921 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2922
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2923 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2924 long_true_divide(PyObject *a, PyObject *b)
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2925 {
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2926 double ad, bd;
36430
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
2927 int failed, aexp = -1, bexp = -1;
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2928
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2929 CHECK_BINOP(a, b);
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2930 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2931 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
20868
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
2932 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
2933 if (failed)
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2934 return NULL;
36430
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
2935 /* 'aexp' and 'bexp' were initialized to -1 to silence gcc-4.0.x,
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
2936 but should really be set correctly after sucessful calls to
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
2937 _PyLong_AsScaledDouble() */
81942fc9a20d As discussed on python-dev, silence three gcc-4.0.x warnings, using assert()
Thomas Wouters <thomas@python.org>
parents: 35999
diff changeset
2938 assert(aexp >= 0 && bexp >= 0);
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2939
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2940 if (bd == 0.0) {
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2941 PyErr_SetString(PyExc_ZeroDivisionError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2942 "int division or modulo by zero");
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2943 return NULL;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2944 }
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2945
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2946 /* True value is very close to ad/bd * 2**(PyLong_SHIFT*(aexp-bexp)) */
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2947 ad /= bd; /* overflow/underflow impossible here */
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2948 aexp -= bexp;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2949 if (aexp > INT_MAX / PyLong_SHIFT)
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2950 goto overflow;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2951 else if (aexp < -(INT_MAX / PyLong_SHIFT))
19787
f814ea6bb667 long_true_divide: reliably force underflow to 0 when the denominator
Tim Peters <tim.peters@gmail.com>
parents: 19719
diff changeset
2952 return PyFloat_FromDouble(0.0); /* underflow to 0 */
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2953 errno = 0;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
2954 ad = ldexp(ad, aexp * PyLong_SHIFT);
19717
01de0a703069 Try to recover from that glibc's ldexp apparently doesn't set errno on
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
2955 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2956 goto overflow;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2957 return PyFloat_FromDouble(ad);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2958
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2959 overflow:
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2960 PyErr_SetString(PyExc_OverflowError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
2961 "int/int too large for a float");
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
2962 return NULL;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2963
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2964 }
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2965
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2966 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2967 long_mod(PyObject *a, PyObject *b)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2968 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2969 PyLongObject *mod;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2970
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2971 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2972
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2973 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, NULL, &mod) < 0)
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2974 mod = NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2975 return (PyObject *)mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2976 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2977
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2978 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2979 long_divmod(PyObject *a, PyObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2980 {
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2981 PyLongObject *div, *mod;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2982 PyObject *z;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2983
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2984 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2985
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
2986 if (l_divmod((PyLongObject*)a, (PyLongObject*)b, &div, &mod) < 0) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2987 return NULL;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2988 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2989 z = PyTuple_New(2);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2990 if (z != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2991 PyTuple_SetItem(z, 0, (PyObject *) div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2992 PyTuple_SetItem(z, 1, (PyObject *) mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2993 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2994 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2995 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2996 Py_DECREF(mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2997 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2998 return z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2999 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3000
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3001 /* pow(v, w, x) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3002 static PyObject *
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3003 long_pow(PyObject *v, PyObject *w, PyObject *x)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3004 {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3005 PyLongObject *a, *b, *c; /* a,b,c = v,w,x */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3006 int negativeOutput = 0; /* if x<0 return negative output */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3007
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3008 PyLongObject *z = NULL; /* accumulated result */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3009 Py_ssize_t i, j, k; /* counters */
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3010 PyLongObject *temp = NULL;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3011
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3012 /* 5-ary values. If the exponent is large enough, table is
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3013 * precomputed so that table[i] == a**i % c for i in range(32).
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3014 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3015 PyLongObject *table[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3016 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3017
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3018 /* a, b, c = v, w, x */
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3019 CHECK_BINOP(v, w);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3020 a = (PyLongObject*)v; Py_INCREF(a);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3021 b = (PyLongObject*)w; Py_INCREF(b);
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3022 if (PyLong_Check(x)) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3023 c = (PyLongObject *)x;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3024 Py_INCREF(x);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3025 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3026 else if (x == Py_None)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3027 c = NULL;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3028 else {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3029 Py_DECREF(a);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3030 Py_DECREF(b);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3031 Py_INCREF(Py_NotImplemented);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3032 return Py_NotImplemented;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3033 }
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
3034
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3035 if (Py_SIZE(b) < 0) { /* if exponent is negative */
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3036 if (c) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
3037 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3038 "cannot be negative when 3rd argument specified");
33328
fee592fb5643 long_pow(): Fix more instances of leaks in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 33327
diff changeset
3039 goto Error;
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
3040 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3041 else {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3042 /* else return a float. This works because we know
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3043 that this calls float_pow() which converts its
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3044 arguments to double. */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3045 Py_DECREF(a);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3046 Py_DECREF(b);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3047 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3048 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3049 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3050
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3051 if (c) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3052 /* if modulus == 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3053 raise ValueError() */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3054 if (Py_SIZE(c) == 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3055 PyErr_SetString(PyExc_ValueError,
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3056 "pow() 3rd argument cannot be 0");
33328
fee592fb5643 long_pow(): Fix more instances of leaks in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 33327
diff changeset
3057 goto Error;
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3058 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3059
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3060 /* if modulus < 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3061 negativeOutput = True
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3062 modulus = -modulus */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3063 if (Py_SIZE(c) < 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3064 negativeOutput = 1;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3065 temp = (PyLongObject *)_PyLong_Copy(c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3066 if (temp == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3067 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3068 Py_DECREF(c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3069 c = temp;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3070 temp = NULL;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3071 NEGATE(c);
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3072 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3073
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3074 /* if modulus == 1:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3075 return 0 */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3076 if ((Py_SIZE(c) == 1) && (c->ob_digit[0] == 1)) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3077 z = (PyLongObject *)PyLong_FromLong(0L);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3078 goto Done;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3079 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3080
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3081 /* if base < 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3082 base = base % modulus
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3083 Having the base positive just makes things easier. */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3084 if (Py_SIZE(a) < 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3085 if (l_divmod(a, c, NULL, &temp) < 0)
33328
fee592fb5643 long_pow(): Fix more instances of leaks in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 33327
diff changeset
3086 goto Error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3087 Py_DECREF(a);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3088 a = temp;
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3089 temp = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3090 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3091 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3092
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3093 /* At this point a, b, and c are guaranteed non-negative UNLESS
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3094 c is NULL, in which case a may be negative. */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3095
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3096 z = (PyLongObject *)PyLong_FromLong(1L);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3097 if (z == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3098 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3099
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3100 /* Perform a modular reduction, X = X % c, but leave X alone if c
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3101 * is NULL.
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3102 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3103 #define REDUCE(X) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3104 if (c != NULL) { \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3105 if (l_divmod(X, c, NULL, &temp) < 0) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3106 goto Error; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3107 Py_XDECREF(X); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3108 X = temp; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3109 temp = NULL; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3110 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3111
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3112 /* Multiply two values, then reduce the result:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3113 result = X*Y % c. If c is NULL, skip the mod. */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3114 #define MULT(X, Y, result) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3115 { \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3116 temp = (PyLongObject *)long_mul(X, Y); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3117 if (temp == NULL) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3118 goto Error; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3119 Py_XDECREF(result); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3120 result = temp; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3121 temp = NULL; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3122 REDUCE(result) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3123 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3124
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3125 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3126 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3127 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3128 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3129 digit bi = b->ob_digit[i];
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3130
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3131 for (j = 1 << (PyLong_SHIFT-1); j != 0; j >>= 1) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3132 MULT(z, z, z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3133 if (bi & j)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3134 MULT(z, a, z)
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3135 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3136 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3137 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3138 else {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3139 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3140 Py_INCREF(z); /* still holds 1L */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3141 table[0] = z;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3142 for (i = 1; i < 32; ++i)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3143 MULT(table[i-1], a, table[i])
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3144
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3145 for (i = Py_SIZE(b) - 1; i >= 0; --i) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3146 const digit bi = b->ob_digit[i];
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3147
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3148 for (j = PyLong_SHIFT - 5; j >= 0; j -= 5) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3149 const int index = (bi >> j) & 0x1f;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3150 for (k = 0; k < 5; ++k)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3151 MULT(z, z, z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3152 if (index)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3153 MULT(z, table[index], z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3154 }
11380
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
3155 }
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
3156 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3157
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3158 if (negativeOutput && (Py_SIZE(z) != 0)) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3159 temp = (PyLongObject *)long_sub(z, c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3160 if (temp == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3161 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3162 Py_DECREF(z);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3163 z = temp;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3164 temp = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3165 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3166 goto Done;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3167
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3168 Error:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3169 if (z != NULL) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3170 Py_DECREF(z);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3171 z = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3172 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3173 /* fall through */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3174 Done:
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3175 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3176 for (i = 0; i < 32; ++i)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3177 Py_XDECREF(table[i]);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3178 }
35254
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
3179 Py_DECREF(a);
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
3180 Py_DECREF(b);
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
3181 Py_XDECREF(c);
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
3182 Py_XDECREF(temp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3183 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3184 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3185
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3186 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3187 long_invert(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3188 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3189 /* Implement ~x as -(x+1) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3190 PyLongObject *x;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3191 PyLongObject *w;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3192 if (ABS(Py_SIZE(v)) <=1)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3193 return PyLong_FromLong(-(MEDIUM_VALUE(v)+1));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3194 w = (PyLongObject *)PyLong_FromLong(1L);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3195 if (w == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3196 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3197 x = (PyLongObject *) long_add(v, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3198 Py_DECREF(w);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3199 if (x == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3200 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3201 Py_SIZE(x) = -(Py_SIZE(x));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3202 return (PyObject *)x;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3203 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3204
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3205 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3206 long_neg(PyLongObject *v)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3207 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3208 PyLongObject *z;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3209 if (ABS(Py_SIZE(v)) <= 1)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3210 return PyLong_FromLong(-MEDIUM_VALUE(v));
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3211 z = (PyLongObject *)_PyLong_Copy(v);
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3212 if (z != NULL)
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3213 Py_SIZE(z) = -(Py_SIZE(v));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3214 return (PyObject *)z;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3215 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3216
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3217 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3218 long_abs(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3219 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3220 if (Py_SIZE(v) < 0)
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3221 return long_neg(v);
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3222 else
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3223 return long_long((PyObject *)v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3224 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3225
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3226 static int
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39703
diff changeset
3227 long_bool(PyLongObject *v)
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3228 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3229 return ABS(Py_SIZE(v)) != 0;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3230 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3231
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3232 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3233 long_rshift(PyLongObject *a, PyLongObject *b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3234 {
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3235 PyLongObject *z = NULL;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3236 long shiftby;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3237 Py_ssize_t newsize, wordshift, loshift, hishift, i, j;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3238 digit lomask, himask;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3239
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3240 CHECK_BINOP(a, b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3241
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3242 if (Py_SIZE(a) < 0) {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3243 /* Right shifting negative numbers is harder */
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3244 PyLongObject *a1, *a2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3245 a1 = (PyLongObject *) long_invert(a);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3246 if (a1 == NULL)
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3247 goto rshift_error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3248 a2 = (PyLongObject *) long_rshift(a1, b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3249 Py_DECREF(a1);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3250 if (a2 == NULL)
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3251 goto rshift_error;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3252 z = (PyLongObject *) long_invert(a2);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3253 Py_DECREF(a2);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3254 }
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3255 else {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3256
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3257 shiftby = PyLong_AsLong((PyObject *)b);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3258 if (shiftby == -1L && PyErr_Occurred())
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3259 goto rshift_error;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3260 if (shiftby < 0) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3261 PyErr_SetString(PyExc_ValueError,
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3262 "negative shift count");
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3263 goto rshift_error;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3264 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3265 wordshift = shiftby / PyLong_SHIFT;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3266 newsize = ABS(Py_SIZE(a)) - wordshift;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3267 if (newsize <= 0) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3268 z = _PyLong_New(0);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3269 return (PyObject *)z;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3270 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3271 loshift = shiftby % PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3272 hishift = PyLong_SHIFT - loshift;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3273 lomask = ((digit)1 << hishift) - 1;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3274 himask = PyLong_MASK ^ lomask;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3275 z = _PyLong_New(newsize);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3276 if (z == NULL)
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3277 goto rshift_error;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3278 if (Py_SIZE(a) < 0)
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3279 Py_SIZE(z) = -(Py_SIZE(z));
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3280 for (i = 0, j = wordshift; i < newsize; i++, j++) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3281 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3282 if (i+1 < newsize)
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3283 z->ob_digit[i] |=
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3284 (a->ob_digit[j+1] << hishift) & himask;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3285 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3286 z = long_normalize(z);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3287 }
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3288 rshift_error:
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3289 return (PyObject *) z;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3290
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3291 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3292
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3293 static PyObject *
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3294 long_lshift(PyObject *v, PyObject *w)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3295 {
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3296 /* This version due to Tim Peters */
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3297 PyLongObject *a = (PyLongObject*)v;
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3298 PyLongObject *b = (PyLongObject*)w;
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3299 PyLongObject *z = NULL;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3300 long shiftby;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3301 Py_ssize_t oldsize, newsize, wordshift, remshift, i, j;
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3302 twodigits accum;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3303
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3304 CHECK_BINOP(a, b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3305
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3306 shiftby = PyLong_AsLong((PyObject *)b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3307 if (shiftby == -1L && PyErr_Occurred())
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3308 goto lshift_error;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3309 if (shiftby < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3310 PyErr_SetString(PyExc_ValueError, "negative shift count");
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3311 goto lshift_error;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3312 }
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3313 if ((long)(int)shiftby != shiftby) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3314 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3315 "outrageous left shift count");
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3316 goto lshift_error;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3317 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3318 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3319 wordshift = (int)shiftby / PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3320 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3321
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3322 oldsize = ABS(Py_SIZE(a));
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3323 newsize = oldsize + wordshift;
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3324 if (remshift)
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3325 ++newsize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3326 z = _PyLong_New(newsize);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3327 if (z == NULL)
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3328 goto lshift_error;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3329 if (Py_SIZE(a) < 0)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3330 NEGATE(z);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3331 for (i = 0; i < wordshift; i++)
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3332 z->ob_digit[i] = 0;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3333 accum = 0;
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3334 for (i = wordshift, j = 0; j < oldsize; i++, j++) {
24919
4cb253c556b1 long_format(), long_lshift(): Someone on c.l.py is trying to boost
Tim Peters <tim.peters@gmail.com>
parents: 24847
diff changeset
3335 accum |= (twodigits)a->ob_digit[j] << remshift;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3336 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3337 accum >>= PyLong_SHIFT;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3338 }
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3339 if (remshift)
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3340 z->ob_digit[newsize-1] = (digit)accum;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3341 else
5199
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3342 assert(!accum);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3343 z = long_normalize(z);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3344 lshift_error:
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3345 return (PyObject *) z;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3346 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3347
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3348
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3349 /* Bitwise and/xor/or operations */
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3350
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3351 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3352 long_bitwise(PyLongObject *a,
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3353 int op, /* '&', '|', '^' */
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3354 PyLongObject *b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3355 {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3356 digit maska, maskb; /* 0 or PyLong_MASK */
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3357 int negz;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3358 Py_ssize_t size_a, size_b, size_z;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3359 PyLongObject *z;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3360 int i;
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
3361 digit diga, digb;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3362 PyObject *v;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3363
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3364 if (Py_SIZE(a) < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3365 a = (PyLongObject *) long_invert(a);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3366 if (a == NULL)
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3367 return NULL;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3368 maska = PyLong_MASK;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3369 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3370 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3371 Py_INCREF(a);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3372 maska = 0;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3373 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3374 if (Py_SIZE(b) < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3375 b = (PyLongObject *) long_invert(b);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3376 if (b == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3377 Py_DECREF(a);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3378 return NULL;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3379 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3380 maskb = PyLong_MASK;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3381 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3382 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3383 Py_INCREF(b);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3384 maskb = 0;
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
3385 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3386
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3387 negz = 0;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3388 switch (op) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3389 case '^':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3390 if (maska != maskb) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3391 maska ^= PyLong_MASK;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3392 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3393 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3394 break;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
3395 case '&':
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3396 if (maska && maskb) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3397 op = '|';
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3398 maska ^= PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3399 maskb ^= PyLong_MASK;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3400 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3401 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3402 break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3403 case '|':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3404 if (maska || maskb) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3405 op = '&';
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3406 maska ^= PyLong_MASK;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3407 maskb ^= PyLong_MASK;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3408 negz = -1;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3409 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3410 break;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3411 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3412
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3413 /* JRH: The original logic here was to allocate the result value (z)
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3414 as the longer of the two operands. However, there are some cases
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3415 where the result is guaranteed to be shorter than that: AND of two
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3416 positives, OR of two negatives: use the shorter number. AND with
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3417 mixed signs: use the positive number. OR with mixed signs: use the
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3418 negative number. After the transformations above, op will be '&'
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3419 iff one of these cases applies, and mask will be non-0 for operands
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3420 whose length should be ignored.
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3421 */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3422
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3423 size_a = Py_SIZE(a);
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3424 size_b = Py_SIZE(b);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3425 size_z = op == '&'
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3426 ? (maska
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3427 ? size_b
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3428 : (maskb ? size_a : MIN(size_a, size_b)))
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3429 : MAX(size_a, size_b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3430 z = _PyLong_New(size_z);
36877
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3431 if (z == NULL) {
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
3432 Py_DECREF(a);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 39222
diff changeset
3433 Py_DECREF(b);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3434 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3435 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3436
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3437 for (i = 0; i < size_z; ++i) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3438 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3439 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3440 switch (op) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3441 case '&': z->ob_digit[i] = diga & digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3442 case '|': z->ob_digit[i] = diga | digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3443 case '^': z->ob_digit[i] = diga ^ digb; break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3444 }
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
3445 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3446
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3447 Py_DECREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3448 Py_DECREF(b);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3449 z = long_normalize(z);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3450 if (negz == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3451 return (PyObject *) z;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3452 v = long_invert(z);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3453 Py_DECREF(z);
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3454 return v;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3455 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3456
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3457 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3458 long_and(PyObject *a, PyObject *b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3459 {
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3460 PyObject *c;
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3461 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3462 c = long_bitwise((PyLongObject*)a, '&', (PyLongObject*)b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3463 return c;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3464 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3465
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3466 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3467 long_xor(PyObject *a, PyObject *b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3468 {
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3469 PyObject *c;
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3470 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3471 c = long_bitwise((PyLongObject*)a, '^', (PyLongObject*)b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3472 return c;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3473 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3474
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3475 static PyObject *
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3476 long_or(PyObject *a, PyObject *b)
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3477 {
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3478 PyObject *c;
43313
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3479 CHECK_BINOP(a, b);
d1552c150e91 Drop convert_binop.
Martin v. Löwis <martin@v.loewis.de>
parents: 43137
diff changeset
3480 c = long_bitwise((PyLongObject*)a, '|', (PyLongObject*)b);
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3481 return c;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3482 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3483
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3484 static PyObject *
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3485 long_long(PyObject *v)
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3486 {
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3487 if (PyLong_CheckExact(v))
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3488 Py_INCREF(v);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3489 else
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3490 v = _PyLong_Copy((PyLongObject *)v);
26291
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3491 return v;
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3492 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3493
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3494 static PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3495 long_float(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3496 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
3497 double result;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3498 result = PyLong_AsDouble(v);
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
3499 if (result == -1.0 && PyErr_Occurred())
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
3500 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3501 return PyFloat_FromDouble(result);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3502 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3503
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3504 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3505 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3506
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3507 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3508 long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3509 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3510 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3511 int base = -909; /* unlikely! */
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
3512 static char *kwlist[] = {"x", "base", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3513
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3514 if (type != &PyLong_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3515 return long_subtype_new(type, args, kwds); /* Wimp out */
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3516 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3517 &x, &base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3518 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3519 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3520 return PyLong_FromLong(0L);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3521 if (base == -909)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3522 return PyNumber_Long(x);
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3523 else if (PyUnicode_Check(x))
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3524 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3525 PyUnicode_GET_SIZE(x),
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3526 base);
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
3527 else if (PyByteArray_Check(x) || PyBytes_Check(x)) {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
3528 /* Since PyLong_FromString doesn't have a length parameter,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
3529 * check here for possible NULs in the string. */
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3530 char *string;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3531 int size = Py_SIZE(x);
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 46478
diff changeset
3532 if (PyByteArray_Check(x))
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 46478
diff changeset
3533 string = PyByteArray_AS_STRING(x);
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3534 else
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
3535 string = PyBytes_AS_STRING(x);
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 41769
diff changeset
3536 if (strlen(string) != size) {
42719
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
3537 /* We only see this if there's a null byte in x,
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43420
diff changeset
3538 x is a bytes or buffer, *and* a base is given. */
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
3539 PyErr_Format(PyExc_ValueError,
42719
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
3540 "invalid literal for int() with base %d: %R",
ae83efb73506 Change the error reporting when an invalid string is encountered to avoid
Guido van Rossum <guido@python.org>
parents: 42480
diff changeset
3541 base, x);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40929
diff changeset
3542 return NULL;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3543 }
41827
eb45f85c4c79 Make test_base64 pass.
Guido van Rossum <guido@python.org>
parents: 41769
diff changeset
3544 return PyLong_FromString(string, NULL, base);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3545 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3546 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3547 PyErr_SetString(PyExc_TypeError,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3548 "int() can't convert non-string with explicit base");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3549 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3550 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3551 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3552
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3553 /* Wimpy, slow approach to tp_new calls for subtypes of long:
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3554 first create a regular long from whatever arguments we got,
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3555 then allocate a subtype instance and initialize it from
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3556 the regular long. The regular long is then thrown away.
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3557 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3558 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3559 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3560 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3561 PyLongObject *tmp, *newobj;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3562 Py_ssize_t i, n;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3563
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3564 assert(PyType_IsSubtype(type, &PyLong_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3565 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3566 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3567 return NULL;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
3568 assert(PyLong_CheckExact(tmp));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3569 n = Py_SIZE(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3570 if (n < 0)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3571 n = -n;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3572 newobj = (PyLongObject *)type->tp_alloc(type, n);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3573 if (newobj == NULL) {
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 28981
diff changeset
3574 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3575 return NULL;
29641
4909aa64405f SF patch 703666: Several objects don't decref tmp on failure in subtype_new
Raymond Hettinger <python@rcn.com>
parents: 28981
diff changeset
3576 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3577 assert(PyLong_Check(newobj));
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44160
diff changeset
3578 Py_SIZE(newobj) = Py_SIZE(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3579 for (i = 0; i < n; i++)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3580 newobj->ob_digit[i] = tmp->ob_digit[i];
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3581 Py_DECREF(tmp);
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3582 return (PyObject *)newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3583 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3584
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3585 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3586 long_getnewargs(PyLongObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3587 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3588 return Py_BuildValue("(N)", _PyLong_Copy(v));
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3589 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3590
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3591 static PyObject *
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3592 long_getN(PyLongObject *v, void *context) {
44469
5d7d2af59afe Some VS 6.0 compatibility fixes from Hirokazu Yamamoto which are also useful for later versions of MSVC. VS6 claims that fortran is a reserved word
Christian Heimes <christian@cheimes.de>
parents: 44460
diff changeset
3593 return PyLong_FromLong((Py_intptr_t)context);
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3594 }
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3595
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3596 static PyObject *
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3597 long__format__(PyObject *self, PyObject *args)
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3598 {
47393
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3599 PyObject *format_spec;
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3600
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3601 if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3602 return NULL;
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3603 return _PyLong_FormatAdvanced(self,
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3604 PyUnicode_AS_UNICODE(format_spec),
0da27fdf0192 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47301
diff changeset
3605 PyUnicode_GET_SIZE(format_spec));
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3606 }
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3607
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3608
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3609 static PyObject *
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3610 long_round(PyObject *self, PyObject *args)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3611 {
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3612 #define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3613 int ndigits = UNDEF_NDIGITS;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3614 double x;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3615 PyObject *res;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3616
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3617 if (!PyArg_ParseTuple(args, "|i", &ndigits))
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3618 return NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3619
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3620 if (ndigits == UNDEF_NDIGITS)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3621 return long_long(self);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3622
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3623 /* If called with two args, defer to float.__round__(). */
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3624 x = PyLong_AsDouble(self);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3625 if (x == -1.0 && PyErr_Occurred())
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3626 return NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3627 self = PyFloat_FromDouble(x);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3628 if (self == NULL)
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3629 return NULL;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3630 res = PyObject_CallMethod(self, "__round__", "i", ndigits);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3631 Py_DECREF(self);
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3632 return res;
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3633 #undef UNDEF_NDIGITS
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3634 }
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3635
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3636 static PyObject *
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3637 long_sizeof(PyLongObject *v)
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3638 {
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3639 Py_ssize_t res;
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3640
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3641 res = sizeof(PyLongObject) + abs(Py_SIZE(v)) * sizeof(digit);
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3642 if (Py_SIZE(v) != 0)
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3643 res -= sizeof(digit);
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3644 return PyLong_FromSsize_t(res);
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3645 }
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3646
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3647 #if 0
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3648 static PyObject *
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3649 long_is_finite(PyObject *v)
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3650 {
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3651 Py_RETURN_TRUE;
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3652 }
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3653 #endif
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3654
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3655 static PyMethodDef long_methods[] = {
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3656 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3657 "Returns self, the complex conjugate of any int."},
46477
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3658 #if 0
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3659 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3660 "Returns always True."},
b69f443ef480 Merged revisions 62380,62382-62383 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46460
diff changeset
3661 #endif
42753
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3662 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3663 "Truncating an Integral returns itself."},
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3664 {"__floor__", (PyCFunction)long_long, METH_NOARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3665 "Flooring an Integral returns itself."},
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3666 {"__ceil__", (PyCFunction)long_long, METH_NOARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3667 "Ceiling of an Integral returns itself."},
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3668 {"__round__", (PyCFunction)long_round, METH_VARARGS,
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3669 "Rounding an Integral returns itself.\n"
077fe7860cc9 Finish the work on __round__ and __trunc__.
Guido van Rossum <guido@python.org>
parents: 42719
diff changeset
3670 "Rounding with an ndigits arguments defers to float.__round__."},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3671 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
42817
5355337709e1 Implementation of PEP 3101, Advanced String Formatting.
Eric Smith <eric@trueblade.com>
parents: 42753
diff changeset
3672 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
47466
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3673 {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
58f4b60773fe Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 47393
diff changeset
3674 "Returns size in memory, in bytes"},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3675 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3676 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3677
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3678 static PyGetSetDef long_getset[] = {
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3679 {"real",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3680 (getter)long_long, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3681 "the real part of a complex number",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3682 NULL},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3683 {"imag",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3684 (getter)long_getN, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3685 "the imaginary part of a complex number",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3686 (void*)0},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3687 {"numerator",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3688 (getter)long_long, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3689 "the numerator of a rational number in lowest terms",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3690 NULL},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3691 {"denominator",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3692 (getter)long_getN, (setter)NULL,
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3693 "the denominator of a rational number in lowest terms",
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3694 (void*)1},
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3695 {NULL} /* Sentinel */
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3696 };
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3697
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23248
diff changeset
3698 PyDoc_STRVAR(long_doc,
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3699 "int(x[, base]) -> integer\n\
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3700 \n\
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3701 Convert a string or number to an integer, if possible. A floating\n\
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3702 point argument will be truncated towards zero (this does not include a\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3703 string representation of a floating point number!) When converting a\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3704 string, use the optional base. It is an error to supply a base when\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23248
diff changeset
3705 converting a non-string.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3706
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3707 static PyNumberMethods long_as_number = {
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3708 (binaryfunc) long_add, /*nb_add*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3709 (binaryfunc) long_sub, /*nb_subtract*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3710 (binaryfunc) long_mul, /*nb_multiply*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3711 long_mod, /*nb_remainder*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3712 long_divmod, /*nb_divmod*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3713 long_pow, /*nb_power*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3714 (unaryfunc) long_neg, /*nb_negative*/
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3715 (unaryfunc) long_long, /*tp_positive*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3716 (unaryfunc) long_abs, /*tp_absolute*/
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39703
diff changeset
3717 (inquiry) long_bool, /*tp_bool*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3718 (unaryfunc) long_invert, /*nb_invert*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3719 long_lshift, /*nb_lshift*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3720 (binaryfunc) long_rshift, /*nb_rshift*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3721 long_and, /*nb_and*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3722 long_xor, /*nb_xor*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3723 long_or, /*nb_or*/
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3724 long_long, /*nb_int*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3725 long_long, /*nb_long*/
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3726 long_float, /*nb_float*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3727 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3728 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3729 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3730 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3731 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3732 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3733 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3734 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3735 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3736 0, /* nb_inplace_or */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3737 long_div, /* nb_floor_divide */
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3738 long_true_divide, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3739 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3740 0, /* nb_inplace_true_divide */
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39574
diff changeset
3741 long_long, /* nb_index */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3742 };
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3743
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3744 PyTypeObject PyLong_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42110
diff changeset
3745 PyVarObject_HEAD_INIT(&PyType_Type, 0)
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3746 "int", /* tp_name */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3747 /* See _PyLong_New for why this isn't
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3748 sizeof(PyLongObject) - sizeof(digit) */
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3749 sizeof(PyVarObject), /* tp_basicsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3750 sizeof(digit), /* tp_itemsize */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3751 long_dealloc, /* tp_dealloc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3752 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3753 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3754 0, /* tp_setattr */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
3755 0, /* tp_compare */
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 37191
diff changeset
3756 long_repr, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3757 &long_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3758 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3759 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3760 (hashfunc)long_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3761 0, /* tp_call */
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3762 long_repr, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3763 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3764 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3765 0, /* tp_as_buffer */
40909
f2f0b41f270d Merged revisions 53875-53911 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40751
diff changeset
3766 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
f2f0b41f270d Merged revisions 53875-53911 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40751
diff changeset
3767 Py_TPFLAGS_LONG_SUBCLASS, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3768 long_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3769 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3770 0, /* tp_clear */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
3771 long_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3772 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3773 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3774 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3775 long_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3776 0, /* tp_members */
42417
d3a5c796c8ba Changes to long and float by Jeffrey Jasskin to conform to PEP 3141.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
3777 long_getset, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3778 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3779 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3780 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3781 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3782 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3783 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3784 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3785 long_new, /* tp_new */
22974
72778ec94dec PyObject_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22204
diff changeset
3786 PyObject_Del, /* tp_free */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3787 };
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3788
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3789 int
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3790 _PyLong_Init(void)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3791 {
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3792 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3793 int ival, size;
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3794 PyLongObject *v = small_ints;
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3795
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3796 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++, v++) {
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3797 size = (ival < 0) ? -1 : ((ival == 0) ? 0 : 1);
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3798 if (Py_TYPE(v) == &PyLong_Type) {
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3799 /* The element is already initialized, most likely
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3800 * the Python interpreter was initialized before.
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3801 */
45088
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3802 Py_ssize_t refcnt;
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3803 PyObject* op = (PyObject*)v;
45088
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3804
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3805 refcnt = Py_REFCNT(op) < 0 ? 0 : Py_REFCNT(op);
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3806 _Py_NewReference(op);
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3807 /* _Py_NewReference sets the ref count to 1 but
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3808 * the ref count might be larger. Set the refcnt
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3809 * to the original refcnt + 1 */
2b63690d5128 Get the ref counting for static allocated longs right.
Christian Heimes <christian@cheimes.de>
parents: 45072
diff changeset
3810 Py_REFCNT(op) = refcnt + 1;
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3811 assert(Py_SIZE(op) == size);
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3812 assert(v->ob_digit[0] == abs(ival));
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3813 }
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3814 else {
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3815 PyObject_INIT(v, &PyLong_Type);
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3816 }
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3817 Py_SIZE(v) = size;
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3818 v->ob_digit[0] = abs(ival);
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3819 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3820 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3821 return 1;
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3822 }
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3823
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3824 void
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3825 PyLong_Fini(void)
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3826 {
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3827 /* Integers are currently statically allocated. Py_DECREF is not
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3828 needed, but Python must forget about the reference or multiple
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3829 reinitializations will fail. */
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3830 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
45072
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3831 int i;
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3832 PyLongObject *v = small_ints;
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3833 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3834 _Py_DEC_REFTOTAL;
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3835 _Py_ForgetReference((PyObject*)v);
f183f1189824 Fixed multiple reinitialization of the Python interpreter. The small int list in longobject.c has caused a seg fault during the third finalization.
Christian Heimes <christian@cheimes.de>
parents: 44778
diff changeset
3836 }
40626
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3837 #endif
4aee3f672f77 Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
3838 }