annotate Objects/longobject.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 5aaf24ec43dc
children c66455a416ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35254
diff changeset
1
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* Long (arbitrary precision) integer object implementation */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
5 /* XXX The functional organization of this file is terrible */
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
6
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
7 #include "Python.h"
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 #include "longintrepr.h"
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
9 #include "structseq.h"
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
10
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
11 #include <float.h>
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
12 #include <ctype.h>
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
13 #include <stddef.h>
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
15 /* 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
16 * both operands contain more than KARATSUBA_CUTOFF digits (this
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
17 * being an internal Python long digit, in base PyLong_BASE).
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
18 */
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
19 #define KARATSUBA_CUTOFF 70
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
20 #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
21
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
22 /* 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
23 * 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
24 * 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
25 * 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
26 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
27 #define FIVEARY_CUTOFF 8
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
28
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
29 #define ABS(x) ((x) < 0 ? -(x) : (x))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
30
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
31 #undef MIN
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
32 #undef MAX
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
33 #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
34 #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
35
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
36 #define SIGCHECK(PyTryBlock) \
25108
3a26e352f0a6 replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents: 24919
diff changeset
37 if (--_Py_Ticker < 0) { \
3a26e352f0a6 replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents: 24919
diff changeset
38 _Py_Ticker = _Py_CheckInterval; \
38331
7a6af4a143c6 Patch #1494387: SVN longobject.c compiler warnings
Tim Peters <tim.peters@gmail.com>
parents: 38307
diff changeset
39 if (PyErr_CheckSignals()) PyTryBlock \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
40 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
41
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
42 /* forward declaration */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
43 static int bits_in_digit(digit d);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
44
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 /* Normalize (remove leading zeros from) a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 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
47 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
48
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
49 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
50 long_normalize(register PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
52 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
53 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
54
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 while (i > 0 && v->ob_digit[i-1] == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 --i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 if (i != j)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
58 Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 return v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 /* Allocate a new long int object with size digits.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 Return NULL and set exception if we run out of memory. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
65 #define MAX_LONG_DIGITS \
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
66 ((PY_SSIZE_T_MAX - offsetof(PyLongObject, ob_digit))/sizeof(digit))
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
67
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
68 PyLongObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
69 _PyLong_New(Py_ssize_t size)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 {
51644
af7d780f7a0b A few more minor fixes in longobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
71 if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
72 PyErr_SetString(PyExc_OverflowError,
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
73 "too many digits in integer");
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
74 return NULL;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
75 }
44749
55ddb166f1d1 Silence Coverity false alerts with CIDs #172, #183, #184
Christian Heimes <christian@cheimes.de>
parents: 44488
diff changeset
76 /* coverity[ampersand_in_size] */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
77 /* XXX(nnorwitz): PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
78 overflow */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
79 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
82 PyObject *
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
83 _PyLong_Copy(PyLongObject *src)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
84 {
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
85 PyLongObject *result;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
86 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
87
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
88 assert(src != NULL);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
89 i = src->ob_size;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
90 if (i < 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
91 i = -(i);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
92 result = _PyLong_New(i);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
93 if (result != NULL) {
22147
1d1a01d956a2 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21968
diff changeset
94 result->ob_size = src->ob_size;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
95 while (--i >= 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
96 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
97 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
98 return (PyObject *)result;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
99 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
100
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 /* Create a new long int object from a C long int */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
103 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
104 PyLong_FromLong(long ival)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 {
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
106 PyLongObject *v;
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
107 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
108 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
109 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
110 int negative = 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
111
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
112 if (ival < 0) {
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
113 /* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
114 ANSI C says that the result of -ival is undefined when ival
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
115 == LONG_MIN. Hence the following workaround. */
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
116 abs_ival = (unsigned long)(-1-ival) + 1;
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
117 negative = 1;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
118 }
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
119 else {
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
120 abs_ival = (unsigned long)ival;
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
121 }
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
122
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
123 /* 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
124 We used to pick 5 ("big enough for anything"), but that's a
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
125 waste of time and space given that 5*15 = 75 bits are rarely
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
126 needed. */
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
127 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
128 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
129 ++ndigits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
130 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
131 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
132 v = _PyLong_New(ndigits);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 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
134 digit *p = v->ob_digit;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
135 v->ob_size = negative ? -ndigits : ndigits;
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
136 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
137 while (t) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
138 *p++ = (digit)(t & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
139 t >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
142 return (PyObject *)v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
145 /* 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
146
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
147 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
148 PyLong_FromUnsignedLong(unsigned long ival)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
149 {
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
150 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
151 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
152 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
153
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
154 /* 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
155 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
156 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
157 ++ndigits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
158 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
159 }
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 v = _PyLong_New(ndigits);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
161 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
162 digit *p = v->ob_digit;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
163 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
164 while (ival) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
165 *p++ = (digit)(ival & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
166 ival >>= PyLong_SHIFT;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
167 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
168 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
169 return (PyObject *)v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
170 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
171
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
172 /* 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
173
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
174 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
175 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
176 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
177 PyLongObject *v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
178 double frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
179 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
180 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
181 if (Py_IS_INFINITY(dval)) {
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
182 PyErr_SetString(PyExc_OverflowError,
48177
6c6a030567dd Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents: 48114
diff changeset
183 "cannot convert float infinity to integer");
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
184 return NULL;
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
185 }
44448
5e0170d3c4da Bug #1481296: Fixed long(float('nan'))!=0L.
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
186 if (Py_IS_NAN(dval)) {
48177
6c6a030567dd Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents: 48114
diff changeset
187 PyErr_SetString(PyExc_ValueError,
6c6a030567dd Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents: 48114
diff changeset
188 "cannot convert float NaN to integer");
6c6a030567dd Issue #1481296: (again!) Make conversion of a float NaN to an int or
Mark Dickinson <dickinsm@gmail.com>
parents: 48114
diff changeset
189 return NULL;
44448
5e0170d3c4da Bug #1481296: Fixed long(float('nan'))!=0L.
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
190 }
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
191 if (dval < 0.0) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
192 neg = 1;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
193 dval = -dval;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
194 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
195 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
196 if (expo <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
197 return PyLong_FromLong(0L);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
198 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
199 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
200 if (v == NULL)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
201 return NULL;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
202 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
203 for (i = ndig; --i >= 0; ) {
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
204 digit bits = (digit)frac;
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
205 v->ob_digit[i] = bits;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
206 frac = frac - (double)bits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
207 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
208 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
209 if (neg)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
210 Py_SIZE(v) = -(Py_SIZE(v));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
211 return (PyObject *)v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
212 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
213
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
214 /* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
215 * anything about what happens when a signed integer operation overflows,
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
216 * and some compilers think they're doing you a favor by being "clever"
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
217 * then. The bit pattern for the largest postive signed long is
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
218 * (unsigned long)LONG_MAX, and for the smallest negative signed long
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
219 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
220 * However, some other compilers warn about applying unary minus to an
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
221 * unsigned operand. Hence the weird "0-".
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
222 */
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
223 #define PY_ABS_LONG_MIN (0-(unsigned long)LONG_MIN)
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
224 #define PY_ABS_SSIZE_T_MIN (0-(size_t)PY_SSIZE_T_MIN)
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
225
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 /* Get a C long int from a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 Returns -1 and sets an error condition if overflow occurs. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 long
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
230 PyLong_AsLong(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 {
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
232 /* This version by Tim Peters */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
233 register PyLongObject *v;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
234 unsigned long x, prev;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
235 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
236 int sign;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
237
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
238 if (vv == NULL || !PyLong_Check(vv)) {
19925
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
239 if (vv != NULL && PyInt_Check(vv))
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
240 return PyInt_AsLong(vv);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
241 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
244 v = (PyLongObject *)vv;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 x = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 sign = -1;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
250 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 while (--i >= 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 prev = x;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
254 x = (x << PyLong_SHIFT) + v->ob_digit[i];
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
255 if ((x >> PyLong_SHIFT) != prev)
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
256 goto overflow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 }
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
258 /* Haven't lost any bits, but casting to long requires extra care
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
259 * (see comment above).
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
260 */
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
261 if (x <= (unsigned long)LONG_MAX) {
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
262 return (long)x * sign;
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
263 }
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
264 else if (sign < 0 && x == PY_ABS_LONG_MIN) {
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
265 return LONG_MIN;
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
266 }
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
267 /* else overflow */
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
268
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
269 overflow:
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
270 PyErr_SetString(PyExc_OverflowError,
19897
84376356ff89 based upon a suggestion in c.l.py, this slight expansion of the
Skip Montanaro <skip@pobox.com>
parents: 19882
diff changeset
271 "long int too large to convert to int");
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
272 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
275 /* Get a Py_ssize_t from a long int object.
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
276 Returns -1 and sets an error condition if overflow occurs. */
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
277
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
278 Py_ssize_t
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
279 PyLong_AsSsize_t(PyObject *vv) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
280 register PyLongObject *v;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
281 size_t x, prev;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
282 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
283 int sign;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
284
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
285 if (vv == NULL || !PyLong_Check(vv)) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
286 PyErr_BadInternalCall();
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
287 return -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
288 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
289 v = (PyLongObject *)vv;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
290 i = v->ob_size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
291 sign = 1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
292 x = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
293 if (i < 0) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
294 sign = -1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
295 i = -(i);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
296 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
297 while (--i >= 0) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
298 prev = x;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
299 x = (x << PyLong_SHIFT) + v->ob_digit[i];
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
300 if ((x >> PyLong_SHIFT) != prev)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
301 goto overflow;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
302 }
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
303 /* Haven't lost any bits, but casting to a signed type requires
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
304 * extra care (see comment above).
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
305 */
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
306 if (x <= (size_t)PY_SSIZE_T_MAX) {
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
307 return (Py_ssize_t)x * sign;
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
308 }
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
309 else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
310 return PY_SSIZE_T_MIN;
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
311 }
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
312 /* else overflow */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
313
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
314 overflow:
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
315 PyErr_SetString(PyExc_OverflowError,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
316 "long int too large to convert to int");
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
317 return -1;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
318 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
319
24777
696a284569e6 Fix comment for PyLong_AsUnsignedLong() to say that the return value
Guido van Rossum <guido@python.org>
parents: 24775
diff changeset
320 /* 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
321 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
322
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
323 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
324 PyLong_AsUnsignedLong(PyObject *vv)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
325 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
326 register PyLongObject *v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
327 unsigned long x, prev;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
328 Py_ssize_t i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
329
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
330 if (vv == NULL || !PyLong_Check(vv)) {
33524
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
331 if (vv != NULL && PyInt_Check(vv)) {
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
332 long val = PyInt_AsLong(vv);
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
333 if (val < 0) {
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
334 PyErr_SetString(PyExc_OverflowError,
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
335 "can't convert negative value to unsigned long");
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
336 return (unsigned long) -1;
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
337 }
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
338 return val;
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
339 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
340 PyErr_BadInternalCall();
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
341 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
342 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
343 v = (PyLongObject *)vv;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
344 i = Py_SIZE(v);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
345 x = 0;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
346 if (i < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
347 PyErr_SetString(PyExc_OverflowError,
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
348 "can't convert negative value to unsigned long");
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
349 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
350 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
351 while (--i >= 0) {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
352 prev = x;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
353 x = (x << PyLong_SHIFT) + v->ob_digit[i];
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
354 if ((x >> PyLong_SHIFT) != prev) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
355 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
356 "long int too large to convert");
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
357 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
358 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
359 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
360 return x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
361 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
362
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
363 /* 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
364 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
365
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
366 unsigned long
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
367 PyLong_AsUnsignedLongMask(PyObject *vv)
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
368 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
369 register PyLongObject *v;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
370 unsigned long x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
371 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
372 int sign;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
373
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
374 if (vv == NULL || !PyLong_Check(vv)) {
33524
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
375 if (vv != NULL && PyInt_Check(vv))
30d428f992f9 Patch #1024670: Support int objects in PyLong_AsUnsignedLong[Mask].
Martin v. Löwis <martin@v.loewis.de>
parents: 33328
diff changeset
376 return PyInt_AsUnsignedLongMask(vv);
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
377 PyErr_BadInternalCall();
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
378 return (unsigned long) -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
379 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
380 v = (PyLongObject *)vv;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
381 i = v->ob_size;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
382 sign = 1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
383 x = 0;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
384 if (i < 0) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
385 sign = -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
386 i = -i;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
387 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
388 while (--i >= 0) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
389 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
390 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
391 return x * sign;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
392 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
393
27549
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
394 int
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
395 _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
396 {
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
397 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
398
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
399 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
400 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
401
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
402 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
403 }
7fcdeeb16ec0 _PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents: 27521
diff changeset
404
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
405 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
406 _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
407 {
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
408 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
409 size_t result = 0;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
410 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
411
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
412 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
413 assert(PyLong_Check(v));
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
414 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
415 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
416 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
417 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
418
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
419 result = (ndigits - 1) * PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
420 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
421 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
422 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
423 ++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
424 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
425 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
426 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
427 } 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
428 }
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
429 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
430
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
431 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
432 PyErr_SetString(PyExc_OverflowError, "long has too many bits "
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
433 "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
434 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
435 }
0904d878b8f1 Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents: 26867
diff changeset
436
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
437 PyObject *
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
438 _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
439 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
440 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
441 const unsigned char* pstartbyte;/* LSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
442 int incr; /* direction to move pstartbyte */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
443 const unsigned char* pendbyte; /* MSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
444 size_t numsignificantbytes; /* number of bytes that matter */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
445 Py_ssize_t ndigits; /* number of Python long digits */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
446 PyLongObject* v; /* result */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
447 Py_ssize_t idigit = 0; /* next free index in v->ob_digit */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
448
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
449 if (n == 0)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
450 return PyLong_FromLong(0L);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
451
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
452 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
453 pstartbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
454 pendbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
455 incr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
456 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
457 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
458 pstartbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
459 pendbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
460 incr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
461 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
462
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
463 if (is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
464 is_signed = *pendbyte >= 0x80;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
465
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
466 /* 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
467 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
468 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
469 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
470 size_t i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
471 const unsigned char* p = pendbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
472 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
473 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
474
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
475 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
476 if (*p != insignficant)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
477 break;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
478 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
479 numsignificantbytes = n - i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
480 /* 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
481 actually has 2 significant bytes. OTOH, 0xff0001 ==
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
482 -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
483 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
484 check every case, bump it regardless. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
485 if (is_signed && numsignificantbytes < n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
486 ++numsignificantbytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
487 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
488
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
489 /* How many Python long digits do we need? We have
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
490 8*numsignificantbytes bits, and each Python long digit has
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
491 PyLong_SHIFT bits, so it's the ceiling of the quotient. */
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
492 /* catch overflow before it happens */
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
493 if (numsignificantbytes > (PY_SSIZE_T_MAX - PyLong_SHIFT) / 8) {
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
494 PyErr_SetString(PyExc_OverflowError,
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
495 "byte array too long to convert to int");
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
496 return NULL;
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
497 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
498 ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
499 v = _PyLong_New(ndigits);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
500 if (v == NULL)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
501 return NULL;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
502
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
503 /* 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
504 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
505 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
506 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
507 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
508 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
509 twodigits accum = 0; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
510 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
511 const unsigned char* p = pstartbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
512
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
513 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
514 twodigits thisbyte = *p;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
515 /* 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
516 if (is_signed) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
517 thisbyte = (0xff ^ thisbyte) + carry;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
518 carry = thisbyte >> 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
519 thisbyte &= 0xff;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
520 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
521 /* 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
522 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
523 so needs to be prepended to accum. */
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
524 accum |= (twodigits)thisbyte << accumbits;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
525 accumbits += 8;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
526 if (accumbits >= PyLong_SHIFT) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
527 /* There's enough to fill a Python digit. */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
528 assert(idigit < ndigits);
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
529 v->ob_digit[idigit] = (digit)(accum &
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
530 PyLong_MASK);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
531 ++idigit;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
532 accum >>= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
533 accumbits -= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
534 assert(accumbits < PyLong_SHIFT);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
535 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
536 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
537 assert(accumbits < PyLong_SHIFT);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
538 if (accumbits) {
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
539 assert(idigit < ndigits);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
540 v->ob_digit[idigit] = (digit)accum;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
541 ++idigit;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
542 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
543 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
544
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
545 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
546 return (PyObject *)long_normalize(v);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
547 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
548
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
549 int
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
550 _PyLong_AsByteArray(PyLongObject* v,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
551 unsigned char* bytes, size_t n,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
552 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
553 {
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
554 Py_ssize_t i; /* index into v->ob_digit */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
555 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
556 twodigits accum; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
557 unsigned int accumbits; /* # bits in accum */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
558 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
51050
c851ed0cf65f No need for carry to be type twodigits in _PyLong_AsByteArray; digit is large enough.
Mark Dickinson <dickinsm@gmail.com>
parents: 51004
diff changeset
559 digit carry; /* for computing 2's-comp */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
560 size_t j; /* # bytes filled */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
561 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
562 int pincr; /* direction to move p */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
563
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
564 assert(v != NULL && PyLong_Check(v));
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
565
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
566 if (Py_SIZE(v) < 0) {
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
567 ndigits = -(Py_SIZE(v));
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
568 if (!is_signed) {
51517
20609fc13a13 Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for
Mark Dickinson <dickinsm@gmail.com>
parents: 51075
diff changeset
569 PyErr_SetString(PyExc_OverflowError,
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
570 "can't convert negative long to unsigned");
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
571 return -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
572 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
573 do_twos_comp = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
574 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
575 else {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
576 ndigits = Py_SIZE(v);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
577 do_twos_comp = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
578 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
579
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
580 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
581 p = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
582 pincr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
583 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
584 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
585 p = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
586 pincr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
587 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
588
18108
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
589 /* 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
590 It's crucial that every Python digit except for the MSD contribute
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
591 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
592 normalized. */
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
593 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
594 j = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
595 accum = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
596 accumbits = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
597 carry = do_twos_comp ? 1 : 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
598 for (i = 0; i < ndigits; ++i) {
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
599 digit thisdigit = v->ob_digit[i];
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
600 if (do_twos_comp) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
601 thisdigit = (thisdigit ^ PyLong_MASK) + carry;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
602 carry = thisdigit >> PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
603 thisdigit &= PyLong_MASK;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
604 }
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
605 /* 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
606 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
607 prepended to accum. */
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
608 accum |= (twodigits)thisdigit << accumbits;
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
609
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
610 /* 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
611 partly empty. */
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
612 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
613 /* 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
614 * although for signed conversion we need later to
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
615 * make sure at least one sign bit gets stored. */
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
616 digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
617 thisdigit;
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
618 while (s != 0) {
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
619 s >>= 1;
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
620 accumbits++;
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
621 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
622 }
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
623 else
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
624 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
625
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
626 /* 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
627 while (accumbits >= 8) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
628 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
629 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
630 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
631 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
632 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
633 accumbits -= 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
634 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
635 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
636 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
637
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
638 /* Store the straggler (if any). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
639 assert(accumbits < 8);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
640 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
641 if (accumbits > 0) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
642 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
643 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
644 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
645 if (do_twos_comp) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
646 /* 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
647 (appropriately pretending that the long had an
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
648 infinite supply of sign bits). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
649 accum |= (~(twodigits)0) << accumbits;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
650 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
651 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
652 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
653 }
18109
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
654 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
655 /* 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
656 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
657 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
658 exists. */
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
659 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
660 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
661 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
662 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
663 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
664 else
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
665 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
666 }
18082
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 /* 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
669 {
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
670 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
671 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
672 *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
673 }
18082
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 return 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
676
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
677 Overflow:
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
678 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
679 return -1;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
680
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
681 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
682
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
683 double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
684 _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
685 {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
686 /* 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
687 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
688 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
689 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
690 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
691
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
692 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
693 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
694 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
695 */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
696 #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
697 PyLongObject *v;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
698 double x;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
699 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
700 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
701 int sign;
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
702 int nbitsneeded;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
703
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
704 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
705 PyErr_BadInternalCall();
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
706 return -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
707 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
708 v = (PyLongObject *)vv;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
709 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
710 sign = 1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
711 if (i < 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
712 sign = -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
713 i = -(i);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
714 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
715 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
716 *exponent = 0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
717 return 0.0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
718 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
719 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
720 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
721 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
722 /* 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
723 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
724 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
725 x = x * multiplier + (double)v->ob_digit[i];
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
726 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
727 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
728 /* There are i digits we didn't shift in. Pretending they're all
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
729 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
730 *exponent = i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
731 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
732 return x * sign;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
733 #undef NBITS_WANTED
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
734 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
735
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
736 /* Get a C double from a long int object. Rounds to the nearest double,
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
737 using the round-half-to-even rule in the case of a tie. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
738
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 double
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
740 PyLong_AsDouble(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 {
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
742 PyLongObject *v = (PyLongObject *)vv;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
743 Py_ssize_t rnd_digit, rnd_bit, m, n;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
744 digit lsb, *d;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
745 int round_up = 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 double x;
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
747
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
748 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
749 PyErr_BadInternalCall();
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
750 return -1.0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 }
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
752
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
753 /* Notes on the method: for simplicity, assume v is positive and >=
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
754 2**DBL_MANT_DIG. (For negative v we just ignore the sign until the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
755 end; for small v no rounding is necessary.) Write n for the number
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
756 of bits in v, so that 2**(n-1) <= v < 2**n, and n > DBL_MANT_DIG.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
757
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
758 Some terminology: the *rounding bit* of v is the 1st bit of v that
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
759 will be rounded away (bit n - DBL_MANT_DIG - 1); the *parity bit*
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
760 is the bit immediately above. The round-half-to-even rule says
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
761 that we round up if the rounding bit is set, unless v is exactly
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
762 halfway between two floats and the parity bit is zero.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
763
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
764 Write d[0] ... d[m] for the digits of v, least to most significant.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
765 Let rnd_bit be the index of the rounding bit, and rnd_digit the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
766 index of the PyLong digit containing the rounding bit. Then the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
767 bits of the digit d[rnd_digit] look something like:
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
768
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
769 rounding bit
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
770 |
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
771 v
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
772 msb -> sssssrttttttttt <- lsb
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
773 ^
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
774 |
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
775 parity bit
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
776
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
777 where 's' represents a 'significant bit' that will be included in
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
778 the mantissa of the result, 'r' is the rounding bit, and 't'
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
779 represents a 'trailing bit' following the rounding bit. Note that
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
780 if the rounding bit is at the top of d[rnd_digit] then the parity
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
781 bit will be the lsb of d[rnd_digit+1]. If we set
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
782
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
783 lsb = 1 << (rnd_bit % PyLong_SHIFT)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
784
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
785 then d[rnd_digit] & (PyLong_BASE - 2*lsb) selects just the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
786 significant bits of d[rnd_digit], d[rnd_digit] & (lsb-1) gets the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
787 trailing bits, and d[rnd_digit] & lsb gives the rounding bit.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
788
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
789 We initialize the double x to the integer given by digits
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
790 d[rnd_digit:m-1], but with the rounding bit and trailing bits of
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
791 d[rnd_digit] masked out. So the value of x comes from the top
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
792 DBL_MANT_DIG bits of v, multiplied by 2*lsb. Note that in the loop
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
793 that produces x, all floating-point operations are exact (assuming
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
794 that FLT_RADIX==2). Now if we're rounding down, the value we want
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
795 to return is simply
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
796
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
797 x * 2**(PyLong_SHIFT * rnd_digit).
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
798
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
799 and if we're rounding up, it's
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
800
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
801 (x + 2*lsb) * 2**(PyLong_SHIFT * rnd_digit).
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
802
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
803 Under the round-half-to-even rule, we round up if, and only
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
804 if, the rounding bit is set *and* at least one of the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
805 following three conditions is satisfied:
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
806
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
807 (1) the parity bit is set, or
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
808 (2) at least one of the trailing bits of d[rnd_digit] is set, or
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
809 (3) at least one of the digits d[i], 0 <= i < rnd_digit
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
810 is nonzero.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
811
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
812 Finally, we have to worry about overflow. If v >= 2**DBL_MAX_EXP,
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
813 or equivalently n > DBL_MAX_EXP, then overflow occurs. If v <
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
814 2**DBL_MAX_EXP then we're usually safe, but there's a corner case
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
815 to consider: if v is very close to 2**DBL_MAX_EXP then it's
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
816 possible that v is rounded up to exactly 2**DBL_MAX_EXP, and then
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
817 again overflow occurs.
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
818 */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
819
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
820 if (Py_SIZE(v) == 0)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
821 return 0.0;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
822 m = ABS(Py_SIZE(v)) - 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
823 d = v->ob_digit;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
824 assert(d[m]); /* v should be normalized */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
825
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
826 /* fast path for case where 0 < abs(v) < 2**DBL_MANT_DIG */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
827 if (m < DBL_MANT_DIG / PyLong_SHIFT ||
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
828 (m == DBL_MANT_DIG / PyLong_SHIFT &&
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
829 d[m] < (digit)1 << DBL_MANT_DIG%PyLong_SHIFT)) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
830 x = d[m];
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
831 while (--m >= 0)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
832 x = x*PyLong_BASE + d[m];
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
833 return Py_SIZE(v) < 0 ? -x : x;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
834 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
835
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
836 /* if m is huge then overflow immediately; otherwise, compute the
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
837 number of bits n in v. The condition below implies n (= #bits) >=
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
838 m * PyLong_SHIFT + 1 > DBL_MAX_EXP, hence v >= 2**DBL_MAX_EXP. */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
839 if (m > (DBL_MAX_EXP-1)/PyLong_SHIFT)
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
840 goto overflow;
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
841 n = m * PyLong_SHIFT + bits_in_digit(d[m]);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
842 if (n > DBL_MAX_EXP)
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
843 goto overflow;
53289
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
844
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
845 /* find location of rounding bit */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
846 assert(n > DBL_MANT_DIG); /* dealt with |v| < 2**DBL_MANT_DIG above */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
847 rnd_bit = n - DBL_MANT_DIG - 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
848 rnd_digit = rnd_bit/PyLong_SHIFT;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
849 lsb = (digit)1 << (rnd_bit%PyLong_SHIFT);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
850
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
851 /* Get top DBL_MANT_DIG bits of v. Assumes PyLong_SHIFT <
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
852 DBL_MANT_DIG, so we'll need bits from at least 2 digits of v. */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
853 x = d[m];
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
854 assert(m > rnd_digit);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
855 while (--m > rnd_digit)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
856 x = x*PyLong_BASE + d[m];
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
857 x = x*PyLong_BASE + (d[m] & (PyLong_BASE-2*lsb));
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
858
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
859 /* decide whether to round up, using round-half-to-even */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
860 assert(m == rnd_digit);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
861 if (d[m] & lsb) { /* if (rounding bit is set) */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
862 digit parity_bit;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
863 if (lsb == PyLong_BASE/2)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
864 parity_bit = d[m+1] & 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
865 else
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
866 parity_bit = d[m] & 2*lsb;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
867 if (parity_bit)
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
868 round_up = 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
869 else if (d[m] & (lsb-1))
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
870 round_up = 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
871 else {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
872 while (--m >= 0) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
873 if (d[m]) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
874 round_up = 1;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
875 break;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
876 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
877 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
878 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
879 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
880
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
881 /* and round up if necessary */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
882 if (round_up) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
883 x += 2*lsb;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
884 if (n == DBL_MAX_EXP &&
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
885 x == ldexp((double)(2*lsb), DBL_MANT_DIG)) {
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
886 /* overflow corner case */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
887 goto overflow;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
888 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
889 }
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
890
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
891 /* shift, adjust for sign, and return */
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
892 x = ldexp(x, rnd_digit*PyLong_SHIFT);
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
893 return Py_SIZE(v) < 0 ? -x : x;
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
894
8f28ce7b8e69 Issue #3166: Make long -> float (and int -> float) conversions
Mark Dickinson <dickinsm@gmail.com>
parents: 52739
diff changeset
895 overflow:
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
896 PyErr_SetString(PyExc_OverflowError,
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
897 "long int too large to convert to float");
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
898 return -1.0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
900
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
901 /* 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
902
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
903 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
904 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
905 {
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
906 #if SIZEOF_VOID_P <= SIZEOF_LONG
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
907 if ((long)p < 0)
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
908 return PyLong_FromUnsignedLong((unsigned long)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
909 return PyInt_FromLong((long)p);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
910 #else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
911
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
912 #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
913 # 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
914 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
915 #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
916 # 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
917 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
918 /* optimize null pointers */
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
919 if (p == NULL)
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
920 return PyInt_FromLong(0);
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
921 return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
922
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
923 #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
924 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
925
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
926 /* 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
927
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
928 void *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
929 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
930 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
931 /* 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
932 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
933 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
934 */
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
935 #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
936 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
937
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
938 if (PyInt_Check(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
939 x = PyInt_AS_LONG(vv);
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
940 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
941 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
942 else
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
943 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
944 #else
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
945
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
946 #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
947 # 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
948 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
949 #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
950 # 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
951 #endif
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
952 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
953
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
954 if (PyInt_Check(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
955 x = PyInt_AS_LONG(vv);
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
956 else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
957 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
958 else
37568
213c4198fe23 Patch #837242: id() for large ptr should return a long.
Martin v. Löwis <martin@v.loewis.de>
parents: 37494
diff changeset
959 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
960
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
961 #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
962
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
963 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
964 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
965 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
966 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
967
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
968 #ifdef HAVE_LONG_LONG
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
969
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
970 /* 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
971 * 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
972 */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
973
18121
166b7b365851 Change IS_LITTLE_ENDIAN macro -- a little faster now.
Tim Peters <tim.peters@gmail.com>
parents: 18115
diff changeset
974 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
975
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
976 /* 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
977
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
978 PyObject *
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
979 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
980 {
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
981 PyLongObject *v;
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
982 unsigned PY_LONG_LONG abs_ival;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
983 unsigned PY_LONG_LONG t; /* unsigned so >> doesn't propagate sign bit */
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
984 int ndigits = 0;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
985 int negative = 0;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
986
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
987 if (ival < 0) {
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
988 /* avoid signed overflow on negation; see comments
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
989 in PyLong_FromLong above. */
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
990 abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
991 negative = 1;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
992 }
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
993 else {
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
994 abs_ival = (unsigned PY_LONG_LONG)ival;
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
995 }
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
996
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
997 /* Count the number of Python digits.
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
998 We used to pick 5 ("big enough for anything"), but that's a
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
999 waste of time and space given that 5*15 = 75 bits are rarely
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1000 needed. */
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
1001 t = abs_ival;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1002 while (t) {
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1003 ++ndigits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1004 t >>= PyLong_SHIFT;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1005 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1006 v = _PyLong_New(ndigits);
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1007 if (v != NULL) {
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1008 digit *p = v->ob_digit;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1009 Py_SIZE(v) = negative ? -ndigits : ndigits;
46459
38c831b47dc5 Fix for possible signed overflow: the behaviour of -LONG_MIN is
Mark Dickinson <dickinsm@gmail.com>
parents: 46149
diff changeset
1010 t = abs_ival;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1011 while (t) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1012 *p++ = (digit)(t & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1013 t >>= PyLong_SHIFT;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1014 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1015 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1016 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
1017 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1018
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1019 /* 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
1020
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1021 PyObject *
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1022 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
1023 {
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1024 PyLongObject *v;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1025 unsigned PY_LONG_LONG t;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1026 int ndigits = 0;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1027
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1028 /* Count the number of Python digits. */
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1029 t = (unsigned PY_LONG_LONG)ival;
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1030 while (t) {
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1031 ++ndigits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1032 t >>= PyLong_SHIFT;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1033 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1034 v = _PyLong_New(ndigits);
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1035 if (v != NULL) {
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1036 digit *p = v->ob_digit;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1037 Py_SIZE(v) = ndigits;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1038 while (ival) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1039 *p++ = (digit)(ival & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1040 ival >>= PyLong_SHIFT;
38307
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1041 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1042 }
5a63369056a7 Faster path for PyLong_FromLongLong, using PyLong_FromLong algorithm
Bob Ippolito <bob@redivi.com>
parents: 38301
diff changeset
1043 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
1044 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1045
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1046 /* 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
1047
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1048 PyObject *
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1049 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
1050 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1051 Py_ssize_t bytes = ival;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1052 int one = 1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1053 return _PyLong_FromByteArray(
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1054 (unsigned char *)&bytes,
41590
1c1705bf36b8 Fix problems in x64 build that were discovered by the testsuite:
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41435
diff changeset
1055 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1056 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1057
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1058 /* 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
1059
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1060 PyObject *
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1061 PyLong_FromSize_t(size_t ival)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1062 {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1063 size_t bytes = ival;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1064 int one = 1;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1065 return _PyLong_FromByteArray(
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1066 (unsigned char *)&bytes,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1067 SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1068 }
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1069
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1070 /* 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
1071 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
1072
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1073 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
1074 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
1075 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1076 PY_LONG_LONG bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1077 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1078 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1079
20182
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1080 if (vv == NULL) {
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1081 PyErr_BadInternalCall();
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1082 return -1;
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1083 }
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1084 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
1085 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
1086 PyObject *io;
20182
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
1087 if (PyInt_Check(vv))
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1088 return (PY_LONG_LONG)PyInt_AsLong(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
1089 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
1090 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
1091 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
1092 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
1093 }
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1094 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
1095 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
1096 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
1097 if (PyInt_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
1098 bytes = PyInt_AsLong(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
1099 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
1100 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
1101 }
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1102 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
1103 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
1104 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
1105 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
1106 }
4812f6a7f735 Revert previous checkin on getargs 'L' code. Try to convert all
Martin v. Löwis <martin@v.loewis.de>
parents: 33524
diff changeset
1107 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
1108 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
1109 return -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1110 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1111
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1112 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1113 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1114 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
1115
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1116 /* 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
1117 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
1118 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
1119 else
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1120 return bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1121 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1122
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1123 /* 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
1124 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
1125
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1126 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
1127 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
1128 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1129 unsigned PY_LONG_LONG bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1130 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1131 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1132
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1133 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
1134 PyErr_BadInternalCall();
37815
20cb13bdf1f0 C++ compiler cleanup: bunch-o-casts, plus use of unsigned loop index var in a couple places
Skip Montanaro <skip@pobox.com>
parents: 37611
diff changeset
1135 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
1136 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1137
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1138 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1139 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1140 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
1141
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28032
diff changeset
1142 /* 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
1143 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
1144 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
1145 else
928c1282e5e2 Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22147
diff changeset
1146 return bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1147 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1148
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1149 /* 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
1150 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
1151
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1152 unsigned PY_LONG_LONG
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1153 PyLong_AsUnsignedLongLongMask(PyObject *vv)
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1154 {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1155 register PyLongObject *v;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1156 unsigned PY_LONG_LONG x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1157 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1158 int sign;
28652
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1159
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1160 if (vv == NULL || !PyLong_Check(vv)) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1161 PyErr_BadInternalCall();
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1162 return (unsigned long) -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1163 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1164 v = (PyLongObject *)vv;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1165 i = v->ob_size;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1166 sign = 1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1167 x = 0;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1168 if (i < 0) {
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1169 sign = -1;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1170 i = -i;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1171 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1172 while (--i >= 0) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1173 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
1174 }
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1175 return x * sign;
28a2975514cd SF # 595026: support for masks in getargs.c.
Thomas Heller <theller@ctypes.org>
parents: 28437
diff changeset
1176 }
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1177 #undef IS_LITTLE_ENDIAN
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
1178
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
1179 #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
1180
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
1181
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1182 static int
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1183 convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1184 if (PyLong_Check(v)) {
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
1185 *a = (PyLongObject *) v;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1186 Py_INCREF(v);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1187 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1188 else if (PyInt_Check(v)) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1189 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1190 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1191 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
1192 return 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
1193 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1194 if (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
1195 *b = (PyLongObject *) w;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1196 Py_INCREF(w);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1197 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1198 else if (PyInt_Check(w)) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1199 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1200 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1201 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
1202 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
1203 return 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
1204 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1205 return 1;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1206 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1207
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1208 #define CONVERT_BINOP(v, w, a, 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
1209 if (!convert_binop(v, w, a, 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
1210 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
1211 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
1212 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1213
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1214 /* bits_in_digit(d) returns the unique integer k such that 2**(k-1) <= d <
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1215 2**k if d is nonzero, else 0. */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1216
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1217 static const unsigned char BitLengthTable[32] = {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1218 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1219 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1220 };
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1221
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1222 static int
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1223 bits_in_digit(digit d)
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1224 {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1225 int d_bits = 0;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1226 while (d >= 32) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1227 d_bits += 6;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1228 d >>= 6;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1229 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1230 d_bits += (int)BitLengthTable[d];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1231 return d_bits;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1232 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1233
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
1234 /* 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
1235 * 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
1236 * 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
1237 */
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1238 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1239 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
1240 {
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1241 Py_ssize_t i;
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
1242 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
1243
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1244 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
1245 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
1246 carry += x[i] + y[i];
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1247 x[i] = carry & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1248 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
1249 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
1250 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1251 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
1252 carry += x[i];
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1253 x[i] = carry & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1254 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
1255 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
1256 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1257 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
1258 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1259
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1260 /* 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
1261 * 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
1262 * 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
1263 */
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1264 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1265 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
1266 {
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1267 Py_ssize_t i;
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
1268 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
1269
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1270 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
1271 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
1272 borrow = x[i] - y[i] - borrow;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1273 x[i] = borrow & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1274 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
1275 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
1276 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1277 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
1278 borrow = x[i] - borrow;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1279 x[i] = borrow & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1280 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
1281 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
1282 }
c248bee52dc6 Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents: 24747
diff changeset
1283 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
1284 }
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
1285
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1286 /* Shift digit vector a[0:m] d bits left, with 0 <= d < PyLong_SHIFT. Put
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1287 * result in z[0:m], and return the d bits shifted out of the top.
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1288 */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1289 static digit
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1290 v_lshift(digit *z, digit *a, Py_ssize_t m, int d)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1292 Py_ssize_t i;
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1293 digit carry = 0;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1294
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1295 assert(0 <= d && d < PyLong_SHIFT);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1296 for (i=0; i < m; i++) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1297 twodigits acc = (twodigits)a[i] << d | carry;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1298 z[i] = (digit)acc & PyLong_MASK;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1299 carry = (digit)(acc >> PyLong_SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300 }
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1301 return carry;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1302 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1303
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1304 /* Shift digit vector a[0:m] d bits right, with 0 <= d < PyLong_SHIFT. Put
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1305 * result in z[0:m], and return the d bits shifted out of the bottom.
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1306 */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1307 static digit
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1308 v_rshift(digit *z, digit *a, Py_ssize_t m, int d)
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1309 {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1310 Py_ssize_t i;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1311 digit carry = 0;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1312 digit mask = ((digit)1 << d) - 1U;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1313
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1314 assert(0 <= d && d < PyLong_SHIFT);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1315 for (i=m; i-- > 0;) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1316 twodigits acc = (twodigits)carry << PyLong_SHIFT | a[i];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1317 carry = (digit)acc & mask;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1318 z[i] = (digit)(acc >> d);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1319 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1320 return carry;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1323 /* 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
1324 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
1325 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1326 _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
1327 immutable. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1328
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1329 static digit
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1330 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
1331 {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1332 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
1333
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1334 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
1335 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
1336 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
1337 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
1338 digit hi;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1339 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
1340 *--pout = hi = (digit)(rem / n);
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1341 rem -= (twodigits)hi * 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
1342 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1343 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
1344 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1345
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 /* Divide a long integer by a digit, returning both the quotient
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 (as function result) and the remainder (through *prem).
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 The sign of a is ignored; n should not be zero. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1350 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
1351 divrem1(PyLongObject *a, digit n, digit *prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1353 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
1354 PyLongObject *z;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1355
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1356 assert(n > 0 && n <= PyLong_MASK);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1357 z = _PyLong_New(size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 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
1360 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1364 /* Convert the long to a string object with given base,
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1365 appending a base prefix of 0[box] if base is 2, 8 or 16.
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1366 Add a trailing "L" if addL is non-zero.
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1367 If newstyle is zero, then use the pre-2.6 behavior of octal having
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1368 a leading "0", instead of the prefix "0o" */
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1369 PyAPI_FUNC(PyObject *)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1370 _PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1372 register PyLongObject *a = (PyLongObject *)aa;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1373 PyStringObject *str;
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1374 Py_ssize_t i, j, sz;
39141
bc5d831f1dda Move the initialization of size_a down below the check for a being NULL.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39095
diff changeset
1375 Py_ssize_t size_a;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 char *p;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 int bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 char sign = '\0';
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1379
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1380 if (a == NULL || !PyLong_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1381 PyErr_BadInternalCall();
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1382 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1383 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1384 assert(base >= 2 && base <= 36);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1385 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
1386
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 /* Compute a rough upper bound for the length of the string */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1388 i = base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1389 bits = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1390 while (i > 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1391 ++bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1392 i >>= 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1393 }
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1394 i = 5 + (addL ? 1 : 0);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1395 j = size_a*PyLong_SHIFT + bits-1;
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1396 sz = i + j / bits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1397 if (j / PyLong_SHIFT < size_a || sz < i) {
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1398 PyErr_SetString(PyExc_OverflowError,
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1399 "long is too large to format");
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1400 return NULL;
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1401 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1402 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 if (str == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1405 p = PyString_AS_STRING(str) + sz;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 *p = '\0';
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1407 if (addL)
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1408 *--p = 'L';
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1409 if (a->ob_size < 0)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1410 sign = '-';
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1411
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1412 if (a->ob_size == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1413 *--p = '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1414 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1415 else if ((base & (base - 1)) == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1416 /* 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
1417 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
1418 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
1419 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
1420 i = base;
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1421 while ((i >>= 1) > 1)
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1422 ++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
1423
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
1424 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
1425 accum |= (twodigits)a->ob_digit[i] << accumbits;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1426 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
1427 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
1428 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
1429 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
1430 cdigit += (cdigit < 10) ? '0' : 'a'-10;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1431 assert(p > PyString_AS_STRING(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
1432 *--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
1433 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
1434 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
1435 } 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
1436 accum > 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437 }
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1438 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1439 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
1440 /* 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
1441 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
1442 fits in a digit. */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1443 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
1444 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
1445 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
1446 /* 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
1447 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
1448 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
1449 for (;;) {
51854
421a0369f239 Replace long with twodigits, to avoid depending
Mark Dickinson <dickinsm@gmail.com>
parents: 51644
diff changeset
1450 twodigits newpow = powbase * (twodigits)base;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1451 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
1452 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
1453 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
1454 ++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
1455 }
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1456
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1457 /* 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
1458 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
1459 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
1460 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
1461 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
1462 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1463
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1464 /* Repeatedly divide by powbase. */
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1465 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
1466 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
1467 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
1468 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
1469 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
1470 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
1471 --size;
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1472 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
1473 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1474 Py_DECREF(str);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1475 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1476 })
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1477
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
1478 /* Break rem into digits. */
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1479 assert(ntostore > 0);
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1480 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
1481 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
1482 char c = (char)(rem - nextrem * base);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1483 assert(p > PyString_AS_STRING(str));
35177
27a718221e72 SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents: 34931
diff changeset
1484 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
1485 *--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
1486 rem = nextrem;
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1487 --ntostore;
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
1488 /* 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
1489 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
1490 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
1491 } 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
1492 } 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
1493 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1494 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1495
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1496 if (base == 2) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1497 *--p = 'b';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1498 *--p = '0';
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1499 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1500 else if (base == 8) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1501 if (newstyle) {
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1502 *--p = 'o';
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1503 *--p = '0';
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1504 }
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1505 else
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1506 if (size_a != 0)
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
1507 *--p = '0';
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
1508 }
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1509 else if (base == 16) {
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1510 *--p = 'x';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1511 *--p = '0';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1512 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1513 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
1514 *--p = '#';
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1515 *--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
1516 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
1517 *--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
1518 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 if (sign)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1520 *--p = sign;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1521 if (p != PyString_AS_STRING(str)) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1522 char *q = PyString_AS_STRING(str);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 assert(p > q);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525 } while ((*q++ = *p++) != '\0');
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1526 q--;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1527 _PyString_Resize((PyObject **)&str,
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1528 (Py_ssize_t) (q - PyString_AS_STRING(str)));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1530 return (PyObject *)str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532
38301
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1533 /* Table of digit values for 8-bit string -> integer conversion.
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1534 * '0' maps to 0, ..., '9' maps to 9.
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1535 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1536 * All other indices map to 37.
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1537 * Note that when converting a base B string, a char c is a legitimate
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1538 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1539 */
dccbcbbd6720 A new table to help string->integer conversion was added yesterday to
Tim Peters <tim.peters@gmail.com>
parents: 38283
diff changeset
1540 int _PyLong_DigitValue[256] = {
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1541 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1542 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1543 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1544 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1545 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1546 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1547 37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1548 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1549 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1550 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1551 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1552 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1553 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1554 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1555 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1556 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1557 };
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1558
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1559 /* *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
1560 * 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
1561 * 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
1562 * 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
1563 * 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
1564 */
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
1565 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
1566 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
1567 {
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
1568 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
1569 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
1570 int bits_per_char;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1571 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
1572 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
1573 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
1574 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
1575 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
1576
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
1577 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
1578 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
1579 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
1580 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
1581 /* 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
1582 n = 0;
46149
7fdff02e6f45 Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46132
diff changeset
1583 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
1584 ++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
1585 *str = p;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1586 /* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1587 n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
39995
6ba402b60041 Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents: 39463
diff changeset
1588 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
1589 PyErr_SetString(PyExc_ValueError,
8e60ec88ecb8 long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents: 27588
diff changeset
1590 "long string too large to convert");
8e60ec88ecb8 long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents: 27588
diff changeset
1591 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
1592 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1593 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
1594 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
1595 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
1596 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
1597 /* 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
1598 * 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
1599 */
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
1600 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
1601 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
1602 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
1603 while (--p >= start) {
46149
7fdff02e6f45 Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46132
diff changeset
1604 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
1605 assert(k >= 0 && k < base);
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1606 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
1607 bits_in_accum += bits_per_char;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1608 if (bits_in_accum >= PyLong_SHIFT) {
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1609 *pdigit++ = (digit)(accum & PyLong_MASK);
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1610 assert(pdigit - z->ob_digit <= n);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1611 accum >>= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1612 bits_in_accum -= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1613 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
1614 }
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
1615 }
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
1616 if (bits_in_accum) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1617 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
1618 *pdigit++ = (digit)accum;
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
1619 assert(pdigit - z->ob_digit <= 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
1620 }
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
1621 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
1622 *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
1623 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
1624 }
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
1625
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1626 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1627 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
1628 {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1629 int sign = 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
1630 char *start, *orig_str = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1631 PyLongObject *z;
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1632 PyObject *strobj, *strrepr;
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1633 Py_ssize_t slen;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1634
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
1635 if ((base != 0 && base < 2) || base > 36) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1636 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
1637 "long() 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
1638 return NULL;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1639 }
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
1640 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
1641 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1642 if (*str == '+')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1643 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1644 else if (*str == '-') {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1645 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1646 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1647 }
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
1648 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
1649 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1650 if (base == 0) {
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1651 /* No base given. Deduce the base from the contents
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1652 of the string */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1653 if (str[0] != '0')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1654 base = 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1655 else if (str[1] == 'x' || str[1] == 'X')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1656 base = 16;
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1657 else if (str[1] == 'o' || str[1] == 'O')
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1658 base = 8;
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1659 else if (str[1] == 'b' || str[1] == 'B')
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1660 base = 2;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1661 else
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1662 /* "old" (C-style) octal literal, still valid in
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1663 2.x, although illegal in 3.x */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1664 base = 8;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1665 }
45791
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1666 /* Whether or not we were deducing the base, skip leading chars
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1667 as needed */
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1668 if (str[0] == '0' &&
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1669 ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1670 (base == 8 && (str[1] == 'o' || str[1] == 'O')) ||
62ef0ea034f4 Finished backporting PEP 3127, Integer Literal Support and Syntax.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
1671 (base == 2 && (str[1] == 'b' || str[1] == 'B'))))
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1672 str += 2;
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1673
8533
e5d06a602469 Add check in long-to-int conversion for at least one digit.
Guido van Rossum <guido@python.org>
parents: 8425
diff changeset
1674 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
1675 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
1676 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
1677 else {
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1678 /***
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1679 Binary bases can be converted in time linear in the number of digits, because
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1680 Python's representation base is binary. Other bases (including decimal!) use
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1681 the simple quadratic-time algorithm below, complicated by some speed tricks.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1682
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1683 First some math: the largest integer that can be expressed in N base-B digits
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1684 is B**N-1. Consequently, if we have an N-digit input in base B, the worst-
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1685 case number of Python digits needed to hold it is the smallest integer n s.t.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1686
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1687 PyLong_BASE**n-1 >= B**N-1 [or, adding 1 to both sides]
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1688 PyLong_BASE**n >= B**N [taking logs to base PyLong_BASE]
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1689 n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1690
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1691 The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1692 this quickly. A Python long with that much space is reserved near the start,
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1693 and the result is computed into it.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1694
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1695 The input string is actually treated as being in base base**i (i.e., i digits
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1696 are processed at a time), where two more static arrays hold:
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1697
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1698 convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1699 convmultmax_base[base] = base ** convwidth_base[base]
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1700
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1701 The first of these is the largest i such that i consecutive input digits
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1702 must fit in a single Python digit. The second is effectively the input
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1703 base we're really using.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1704
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1705 Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1706 convmultmax_base[base], the result is "simply"
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1707
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1708 (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1709
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1710 where B = convmultmax_base[base].
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1711
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1712 Error analysis: as above, the number of Python digits `n` needed is worst-
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1713 case
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1714
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1715 n >= N * log(B)/log(PyLong_BASE)
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1716
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1717 where `N` is the number of input digits in base `B`. This is computed via
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1718
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1719 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1720
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1721 below. Two numeric concerns are how much space this can waste, and whether
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1722 the computed result can be too small. To be concrete, assume PyLong_BASE = 2**15,
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1723 which is the default (and it's unlikely anyone changes that).
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1724
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1725 Waste isn't a problem: provided the first input digit isn't 0, the difference
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1726 between the worst-case input with N digits and the smallest input with N
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1727 digits is about a factor of B, but B is small compared to PyLong_BASE so at most
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1728 one allocated Python digit can remain unused on that count. If
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1729 N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1730 and adding 1 returns a result 1 larger than necessary. However, that can't
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1731 happen: whenever B is a power of 2, long_from_binary_base() is called
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1732 instead, and it's impossible for B**i to be an integer power of 2**15 when
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1733 B is not a power of 2 (i.e., it's impossible for N*log(B)/log(PyLong_BASE) to be
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1734 an exact integer when B is not a power of 2, since B**i has a prime factor
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1735 other than 2 in that case, but (2**15)**j's only prime factor is 2).
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1736
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1737 The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1738 is a little bit larger than an exact integer, but due to roundoff errors (in
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1739 computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1740 yields a numeric result a little less than that integer. Unfortunately, "how
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1741 close can a transcendental function get to an integer over some range?"
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1742 questions are generally theoretically intractable. Computer analysis via
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1743 continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1744 fractions, giving a sequence i/j of "the best" rational approximations. Then
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1745 j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1746 we can get very close to being in trouble, but very rarely. For example,
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1747 76573 is a denominator in one of the continued-fraction approximations to
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1748 log(10)/log(2**15), and indeed:
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1749
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1750 >>> log(10)/log(2**15)*76573
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1751 16958.000000654003
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1752
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1753 is very close to an integer. If we were working with IEEE single-precision,
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1754 rounding errors could kill us. Finding worst cases in IEEE double-precision
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1755 requires better-than-double-precision log() functions, and Tim didn't bother.
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1756 Instead the code checks to see whether the allocated space is enough as each
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1757 new Python digit is added, and copies the whole thing to a larger long if not.
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1758 This should happen extremely rarely, and in fact I don't have a test case
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1759 that triggers it(!). Instead the code was tested by artificially allocating
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1760 just 1 digit at the start, so that the copying code was exercised for every
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1761 digit beyond the first.
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1762 ***/
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1763 register twodigits c; /* current input character */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1764 Py_ssize_t size_z;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1765 int i;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1766 int convwidth;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1767 twodigits convmultmax, convmult;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1768 digit *pz, *pzstop;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1769 char* scan;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1770
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1771 static double log_base_PyLong_BASE[37] = {0.0e0,};
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1772 static int convwidth_base[37] = {0,};
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1773 static twodigits convmultmax_base[37] = {0,};
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1774
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1775 if (log_base_PyLong_BASE[base] == 0.0) {
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1776 twodigits convmax = base;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1777 int i = 1;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1778
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1779 log_base_PyLong_BASE[base] = log((double)base) /
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1780 log((double)PyLong_BASE);
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1781 for (;;) {
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1782 twodigits next = convmax * base;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1783 if (next > PyLong_BASE)
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1784 break;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1785 convmax = next;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1786 ++i;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1787 }
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1788 convmultmax_base[base] = convmax;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1789 assert(i > 0);
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1790 convwidth_base[base] = i;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1791 }
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1792
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1793 /* Find length of the string of numeric characters. */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1794 scan = str;
46149
7fdff02e6f45 Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46132
diff changeset
1795 while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1796 ++scan;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1797
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1798 /* Create a long object that can contain the largest possible
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1799 * integer with this base and length. Note that there's no
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1800 * need to initialize z->ob_digit -- no slot is read up before
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1801 * being stored into.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1802 */
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1803 size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1804 /* Uncomment next line to test exceedingly rare copy code */
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1805 /* size_z = 1; */
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1806 assert(size_z > 0);
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1807 z = _PyLong_New(size_z);
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1808 if (z == NULL)
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1809 return NULL;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1810 Py_SIZE(z) = 0;
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1811
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1812 /* `convwidth` consecutive input digits are treated as a single
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1813 * digit in base `convmultmax`.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1814 */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1815 convwidth = convwidth_base[base];
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1816 convmultmax = convmultmax_base[base];
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1817
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1818 /* Work ;-) */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1819 while (str < scan) {
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1820 /* grab up to convwidth digits from the input string */
46149
7fdff02e6f45 Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46132
diff changeset
1821 c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1822 for (i = 1; i < convwidth && str != scan; ++i, ++str) {
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1823 c = (twodigits)(c * base +
46149
7fdff02e6f45 Revert r61969 which added casts to Py_CHARMASK to avoid compiler warnings.
Neal Norwitz <nnorwitz@gmail.com>
parents: 46132
diff changeset
1824 _PyLong_DigitValue[Py_CHARMASK(*str)]);
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1825 assert(c < PyLong_BASE);
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1826 }
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1827
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1828 convmult = convmultmax;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1829 /* Calculate the shift only if we couldn't get
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1830 * convwidth digits.
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1831 */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1832 if (i != convwidth) {
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1833 convmult = base;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1834 for ( ; i > 1; --i)
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1835 convmult *= base;
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1836 }
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1837
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1838 /* Multiply z by convmult, and add c. */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1839 pz = z->ob_digit;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1840 pzstop = pz + Py_SIZE(z);
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1841 for (; pz < pzstop; ++pz) {
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1842 c += (twodigits)*pz * convmult;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1843 *pz = (digit)(c & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1844 c >>= PyLong_SHIFT;
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1845 }
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1846 /* carry off the current end? */
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1847 if (c) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
1848 assert(c < PyLong_BASE);
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1849 if (Py_SIZE(z) < size_z) {
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1850 *pz = (digit)c;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1851 ++Py_SIZE(z);
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1852 }
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1853 else {
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1854 PyLongObject *tmp;
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1855 /* Extremely rare. Get more space. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1856 assert(Py_SIZE(z) == size_z);
38513
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1857 tmp = _PyLong_New(size_z + 1);
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1858 if (tmp == NULL) {
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1859 Py_DECREF(z);
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1860 return NULL;
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1861 }
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1862 memcpy(tmp->ob_digit,
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1863 z->ob_digit,
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1864 sizeof(digit) * size_z);
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1865 Py_DECREF(z);
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1866 z = tmp;
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1867 z->ob_digit[size_z] = (digit)c;
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1868 ++size_z;
7f1f36b3c9f9 PyLong_FromString(): Continued fraction analysis (explained in
Tim Peters <tim.peters@gmail.com>
parents: 38331
diff changeset
1869 }
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1870 }
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
1871 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1872 }
8808
78785985a2ab Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents: 8533
diff changeset
1873 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
1874 return NULL;
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
1875 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
1876 goto onError;
38283
88cc7e4f01ff Heavily fiddled variant of patch #1442927: PyLong_FromString optimization.
Tim Peters <tim.peters@gmail.com>
parents: 38139
diff changeset
1877 if (sign < 0)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1878 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
1879 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
1880 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
1881 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
1882 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
1883 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
1884 goto onError;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1885 if (pend)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1886 *pend = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1887 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
1888
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
1889 onError:
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1890 Py_XDECREF(z);
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1891 slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1892 strobj = PyString_FromStringAndSize(orig_str, slen);
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1893 if (strobj == NULL)
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1894 return NULL;
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1895 strrepr = PyObject_Repr(strobj);
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1896 Py_DECREF(strobj);
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1897 if (strrepr == NULL)
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1898 return NULL;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1899 PyErr_Format(PyExc_ValueError,
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1900 "invalid literal for long() with base %d: %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
1901 base, PyString_AS_STRING(strrepr));
37611
7afaa752c772 Fix int() and long() to repr() their argument when formatting the exception,
Thomas Wouters <thomas@python.org>
parents: 37589
diff changeset
1902 Py_DECREF(strrepr);
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
1903 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
1904 }
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
1905
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1906 #ifdef Py_USING_UNICODE
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
1907 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
1908 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
1909 {
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1910 PyObject *result;
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
1911 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
1912
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1913 if (buffer == NULL)
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1914 return NULL;
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1915
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1916 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
1917 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
1918 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
1919 }
26081
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1920 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
1921 PyMem_FREE(buffer);
999accfa91c5 Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents: 25108
diff changeset
1922 return result;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1923 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1924 #endif
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1925
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1926 /* forward */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1927 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
1928 (PyLongObject *, PyLongObject *, PyLongObject **);
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
1929 static PyObject *long_long(PyObject *v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1930
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1931 /* Long division with remainder, top-level routine */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1933 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
1934 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
1935 PyLongObject **pdiv, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1936 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
1937 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
1938 PyLongObject *z;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
1939
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1940 if (size_b == 0) {
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1941 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
1942 "long division or modulo by zero");
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1943 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1944 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1945 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
1946 (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
1947 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
1948 /* |a| < |b|. */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1949 *pdiv = _PyLong_New(0);
41435
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1950 if (*pdiv == NULL)
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1951 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1952 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1953 *prem = (PyLongObject *) a;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1954 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1955 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 if (size_b == 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1957 digit rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1958 z = divrem1(a, b->ob_digit[0], &rem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1959 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1960 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1961 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
41435
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1962 if (*prem == NULL) {
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1963 Py_DECREF(z);
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1964 return -1;
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
1965 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1966 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1967 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1968 z = x_divrem(a, b, prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1969 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1970 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1971 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1972 /* Set the signs.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1973 The quotient z has the sign of a*b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1974 the remainder r has the sign of a,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1975 so a = b*z + r. */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1976 if ((a->ob_size < 0) != (b->ob_size < 0))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1977 z->ob_size = -(z->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1978 if (a->ob_size < 0 && (*prem)->ob_size != 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1979 (*prem)->ob_size = -((*prem)->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1980 *pdiv = z;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1981 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1982 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1983
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1984 /* Unsigned long division with remainder -- the algorithm. The arguments v1
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1985 and w1 should satisfy 2 <= ABS(Py_SIZE(w1)) <= ABS(Py_SIZE(v1)). */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1986
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1987 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
1988 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1989 {
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1990 PyLongObject *v, *w, *a;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1991 Py_ssize_t i, k, size_v, size_w;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1992 int d;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1993 digit wm1, wm2, carry, q, r, vtop, *v0, *vk, *w0, *ak;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1994 twodigits vv;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1995 sdigit zhi;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1996 stwodigits z;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1997
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1998 /* We follow Knuth [The Art of Computer Programming, Vol. 2 (3rd
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
1999 edn.), section 4.3.1, Algorithm D], except that we don't explicitly
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2000 handle the special case when the initial estimate q for a quotient
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2001 digit is >= PyLong_BASE: the max value for q is PyLong_BASE+1, and
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2002 that won't overflow a digit. */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2003
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2004 /* allocate space; w will also be used to hold the final remainder */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2005 size_v = ABS(Py_SIZE(v1));
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2006 size_w = ABS(Py_SIZE(w1));
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2007 assert(size_v >= size_w && size_w >= 2); /* Assert checks by div() */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2008 v = _PyLong_New(size_v+1);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2009 if (v == NULL) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2010 *prem = NULL;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2011 return NULL;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2012 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2013 w = _PyLong_New(size_w);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2014 if (w == NULL) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2015 Py_DECREF(v);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2016 *prem = NULL;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2017 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2018 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2019
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2020 /* normalize: shift w1 left so that its top digit is >= PyLong_BASE/2.
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2021 shift v1 left by the same amount. Results go into w and v. */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2022 d = PyLong_SHIFT - bits_in_digit(w1->ob_digit[size_w-1]);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2023 carry = v_lshift(w->ob_digit, w1->ob_digit, size_w, d);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2024 assert(carry == 0);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2025 carry = v_lshift(v->ob_digit, v1->ob_digit, size_v, d);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2026 if (carry != 0 || v->ob_digit[size_v-1] >= w->ob_digit[size_w-1]) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2027 v->ob_digit[size_v] = carry;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2028 size_v++;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2029 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2030
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2031 /* Now v->ob_digit[size_v-1] < w->ob_digit[size_w-1], so quotient has
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2032 at most (and usually exactly) k = size_v - size_w digits. */
38139
f1e24452dbba Fix problems found by Coverity.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37815
diff changeset
2033 k = size_v - size_w;
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2034 assert(k >= 0);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2035 a = _PyLong_New(k);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2036 if (a == NULL) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2037 Py_DECREF(w);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2038 Py_DECREF(v);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2039 *prem = NULL;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2040 return NULL;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2041 }
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2042 v0 = v->ob_digit;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2043 w0 = w->ob_digit;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2044 wm1 = w0[size_w-1];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2045 wm2 = w0[size_w-2];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2046 for (vk = v0+k, ak = a->ob_digit + k; vk-- > v0;) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2047 /* inner loop: divide vk[0:size_w+1] by w0[0:size_w], giving
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2048 single-digit quotient q, remainder in vk[0:size_w]. */
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2049
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
2050 SIGCHECK({
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2051 Py_DECREF(a);
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2052 Py_DECREF(w);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2053 Py_DECREF(v);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2054 *prem = NULL;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2055 return NULL;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2056 })
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2057
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2058 /* estimate quotient digit q; may overestimate by 1 (rare) */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2059 vtop = vk[size_w];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2060 assert(vtop <= wm1);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2061 vv = ((twodigits)vtop << PyLong_SHIFT) | vk[size_w-1];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2062 q = (digit)(vv / wm1);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2063 r = (digit)(vv - (twodigits)wm1 * q); /* r = vv % wm1 */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2064 while ((twodigits)wm2 * q > (((twodigits)r << PyLong_SHIFT)
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2065 | vk[size_w-2])) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2066 --q;
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2067 r += wm1;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2068 if (r >= PyLong_BASE)
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2069 break;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2070 }
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2071 assert(q <= PyLong_BASE);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2072
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2073 /* subtract q*w0[0:size_w] from vk[0:size_w+1] */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2074 zhi = 0;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2075 for (i = 0; i < size_w; ++i) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2076 /* invariants: -PyLong_BASE <= -q <= zhi <= 0;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2077 -PyLong_BASE * q <= z < PyLong_BASE */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2078 z = (sdigit)vk[i] + zhi -
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2079 (stwodigits)q * (stwodigits)w0[i];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2080 vk[i] = (digit)z & PyLong_MASK;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2081 zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2082 z, PyLong_SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2083 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2084
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2085 /* add w back if q was too large (this branch taken rarely) */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2086 assert((sdigit)vtop + zhi == -1 || (sdigit)vtop + zhi == 0);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2087 if ((sdigit)vtop + zhi < 0) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2088 carry = 0;
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2089 for (i = 0; i < size_w; ++i) {
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2090 carry += vk[i] + w0[i];
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2091 vk[i] = carry & PyLong_MASK;
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2092 carry >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2093 }
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2094 --q;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2095 }
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2096
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2097 /* store quotient digit */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2098 assert(q < PyLong_BASE);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2099 *--ak = q;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2100 }
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2101
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2102 /* unshift remainder; we reuse w to store the result */
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2103 carry = v_rshift(w0, v0, size_w, d);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2104 assert(carry==0);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2105 Py_DECREF(v);
52284
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2106
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2107 *prem = long_normalize(w);
a54ef18ab0d8 Issue #5512: speed up the long division algorithm for Python longs.
Mark Dickinson <dickinsm@gmail.com>
parents: 52233
diff changeset
2108 return long_normalize(a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2109 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2110
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2111 /* Methods */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2112
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2113 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
2114 long_dealloc(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2115 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2116 Py_TYPE(v)->tp_free(v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2117 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2118
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2119 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
2120 long_repr(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2121 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
2122 return _PyLong_Format(v, 10, 1, 0);
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2123 }
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2124
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2125 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
2126 long_str(PyObject *v)
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2127 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
2128 return _PyLong_Format(v, 10, 0, 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2129 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2130
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2131 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
2132 long_compare(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2133 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2134 Py_ssize_t sign;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2135
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2136 if (Py_SIZE(a) != Py_SIZE(b)) {
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2137 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
2138 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
2139 else
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2140 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
2141 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2142 else {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2143 Py_ssize_t i = ABS(Py_SIZE(a));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2144 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2145 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2146 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2147 sign = 0;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
2148 else {
51644
af7d780f7a0b A few more minor fixes in longobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
2149 sign = (sdigit)a->ob_digit[i] - (sdigit)b->ob_digit[i];
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2150 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
2151 sign = -sign;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
2152 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2153 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2154 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2155 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2156
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2157 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
2158 long_hash(PyLongObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2159 {
51074
200829255a6a Fix undefined behaviour (left shift of negative value) in long_hash. Also,
Mark Dickinson <dickinsm@gmail.com>
parents: 51050
diff changeset
2160 unsigned long x;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2161 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2162 int sign;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2163
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2164 /* 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
2165 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
2166 of mapping keys will turn out weird */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2167 i = v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2168 sign = 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2169 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2170 if (i < 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2171 sign = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2172 i = -(i);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2173 }
51075
2ea75f60d9e5 Fix comment.
Mark Dickinson <dickinsm@gmail.com>
parents: 51074
diff changeset
2174 /* The following loop produces a C unsigned long x such that x is
2ea75f60d9e5 Fix comment.
Mark Dickinson <dickinsm@gmail.com>
parents: 51074
diff changeset
2175 congruent to the absolute value of v modulo ULONG_MAX. The
2ea75f60d9e5 Fix comment.
Mark Dickinson <dickinsm@gmail.com>
parents: 51074
diff changeset
2176 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
2177 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
2178 /* Force a native long #-bits (32 or 64) circular shift */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
2179 x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2180 x += v->ob_digit[i];
51074
200829255a6a Fix undefined behaviour (left shift of negative value) in long_hash. Also,
Mark Dickinson <dickinsm@gmail.com>
parents: 51050
diff changeset
2181 /* If the addition above overflowed we compensate by
200829255a6a Fix undefined behaviour (left shift of negative value) in long_hash. Also,
Mark Dickinson <dickinsm@gmail.com>
parents: 51050
diff changeset
2182 incrementing. This preserves the value modulo
200829255a6a Fix undefined behaviour (left shift of negative value) in long_hash. Also,
Mark Dickinson <dickinsm@gmail.com>
parents: 51050
diff changeset
2183 ULONG_MAX. */
200829255a6a Fix undefined behaviour (left shift of negative value) in long_hash. Also,
Mark Dickinson <dickinsm@gmail.com>
parents: 51050
diff changeset
2184 if (x < v->ob_digit[i])
43406
357f54015a2d Issue #1772851. Alters long.__hash__ from being *almost* completely
Facundo Batista <facundobatista@gmail.com>
parents: 42333
diff changeset
2185 x++;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2186 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2187 x = x * sign;
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
2188 if (x == (unsigned long)-1)
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
2189 x = (unsigned long)-2;
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
2190 return (long)x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2191 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2192
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
2193
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2194 /* Add the absolute values of two long integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2195
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2196 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
2197 x_add(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2198 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2199 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
2200 PyLongObject *z;
51004
4eac9941538c Issue #4393: fix 3 classes of potential portability problems in longobject.c:
Mark Dickinson <dickinsm@gmail.com>
parents: 50205
diff changeset
2201 Py_ssize_t i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2202 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
2203
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2204 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2205 if (size_a < size_b) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2206 { 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
2207 { Py_ssize_t size_temp = size_a;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2208 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2209 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2210 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2211 z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2212 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2213 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2214 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2215 carry += a->ob_digit[i] + b->ob_digit[i];
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2216 z->ob_digit[i] = carry & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2217 carry >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2218 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2219 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2220 carry += a->ob_digit[i];
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2221 z->ob_digit[i] = carry & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2222 carry >>= PyLong_SHIFT;
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 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2225 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2226 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2227
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2228 /* Subtract the absolute values of two integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2229
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2230 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
2231 x_sub(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2232 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2233 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
2234 PyLongObject *z;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2235 Py_ssize_t i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2236 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2237 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
2238
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2239 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2240 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2241 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2242 { 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
2243 { Py_ssize_t size_temp = size_a;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2244 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2245 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2246 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2247 else if (size_a == size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2248 /* Find highest digit where a and b differ: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2249 i = size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2250 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2251 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2252 if (i < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2253 return _PyLong_New(0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2254 if (a->ob_digit[i] < b->ob_digit[i]) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2255 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2256 { PyLongObject *temp = a; a = b; b = temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2257 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2258 size_a = size_b = i+1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2259 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2260 z = _PyLong_New(size_a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2261 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2262 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2263 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2264 /* The following assumes unsigned arithmetic
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2265 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
2266 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2267 z->ob_digit[i] = borrow & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2268 borrow >>= PyLong_SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2269 borrow &= 1; /* Keep only one sign bit */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2270 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2271 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2272 borrow = a->ob_digit[i] - borrow;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2273 z->ob_digit[i] = borrow & PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2274 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
2275 borrow &= 1; /* Keep only one sign bit */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2276 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2277 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
2278 if (sign < 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
2279 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2280 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2281 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2282
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2283 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
2284 long_add(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2285 {
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
2286 PyLongObject *a, *b, *z;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
2287
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
2288 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &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
2289
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2290 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2291 if (b->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2292 z = x_add(a, 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
2293 if (z != NULL && z->ob_size != 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
2294 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2295 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2296 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2297 z = x_sub(b, a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2298 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2299 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2300 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2301 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2302 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2303 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2304 }
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
2305 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
2306 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2307 return (PyObject *)z;
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2310 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
2311 long_sub(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2312 {
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
2313 PyLongObject *a, *b, *z;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2314
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
2315 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &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
2316
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2317 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2318 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2319 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2320 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2321 z = x_add(a, 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
2322 if (z != NULL && z->ob_size != 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
2323 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2324 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2325 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2326 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2327 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2328 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2329 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2330 }
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
2331 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
2332 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2333 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2334 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2335
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2336 /* 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
2337 * 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
2338 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2339 static PyLongObject *
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2340 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
2341 {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2342 PyLongObject *z;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2343 Py_ssize_t size_a = ABS(Py_SIZE(a));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2344 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
2345 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
2346
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2347 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
2348 if (z == NULL)
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2349 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
2350
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2351 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
2352 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2353 /* Efficient squaring per HAC, Algorithm 14.16:
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2354 * 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
2355 * 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
2356 * 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
2357 * 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
2358 */
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2359 for (i = 0; i < size_a; ++i) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2360 twodigits carry;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2361 twodigits f = a->ob_digit[i];
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2362 digit *pz = z->ob_digit + (i << 1);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2363 digit *pa = a->ob_digit + i + 1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2364 digit *paend = a->ob_digit + size_a;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2365
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2366 SIGCHECK({
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2367 Py_DECREF(z);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2368 return NULL;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2369 })
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2370
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2371 carry = *pz + f * f;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2372 *pz++ = (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2373 carry >>= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2374 assert(carry <= PyLong_MASK);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2375
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2376 /* 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
2377 * 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
2378 */
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2379 f <<= 1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2380 while (pa < paend) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2381 carry += *pz + *pa++ * f;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2382 *pz++ = (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2383 carry >>= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2384 assert(carry <= (PyLong_MASK << 1));
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2385 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2386 if (carry) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2387 carry += *pz;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2388 *pz++ = (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2389 carry >>= PyLong_SHIFT;
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2390 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2391 if (carry)
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2392 *pz += (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2393 assert((carry >> PyLong_SHIFT) == 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2394 }
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2395 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2396 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
2397 for (i = 0; i < size_a; ++i) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2398 twodigits carry = 0;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2399 twodigits f = a->ob_digit[i];
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2400 digit *pz = z->ob_digit + i;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2401 digit *pb = b->ob_digit;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2402 digit *pbend = b->ob_digit + size_b;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2403
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2404 SIGCHECK({
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2405 Py_DECREF(z);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2406 return NULL;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2407 })
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2408
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2409 while (pb < pbend) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2410 carry += *pz + *pb++ * f;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2411 *pz++ = (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2412 carry >>= PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2413 assert(carry <= PyLong_MASK);
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2414 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2415 if (carry)
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2416 *pz += (digit)(carry & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2417 assert((carry >> PyLong_SHIFT) == 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2418 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2419 }
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2420 return long_normalize(z);
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2421 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2422
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2423 /* 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
2424 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
2425 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
2426 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
2427 the return values are >= 0.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2428 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
2429 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2430 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2431 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
2432 {
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2433 PyLongObject *hi, *lo;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2434 Py_ssize_t size_lo, size_hi;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2435 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
2436
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2437 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
2438 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
2439
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2440 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
2441 return -1;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2442 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
2443 Py_DECREF(hi);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2444 return -1;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2445 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2446
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2447 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
2448 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
2449
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2450 *high = long_normalize(hi);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2451 *low = long_normalize(lo);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2452 return 0;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2453 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2454
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2455 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
2456
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2457 /* 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
2458 * 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
2459 * 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
2460 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2461 static PyLongObject *
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2462 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
2463 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2464 Py_ssize_t asize = ABS(Py_SIZE(a));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2465 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
2466 PyLongObject *ah = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2467 PyLongObject *al = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2468 PyLongObject *bh = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2469 PyLongObject *bl = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2470 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
2471 PyLongObject *t1, *t2, *t3;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2472 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
2473 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
2474
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2475 /* (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
2476 * 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
2477 * Then the original product is
24746
49169d00ab18 k_mul(): Repaired typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 24745
diff changeset
2478 * 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
2479 * 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
2480 * 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
2481 */
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2482
24747
19e010f792a3 k_mul(): Repaired another typo in another comment.
Tim Peters <tim.peters@gmail.com>
parents: 24746
diff changeset
2483 /* 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
2484 * is largest.
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2485 */
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2486 if (asize > bsize) {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2487 t1 = a;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2488 a = b;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2489 b = t1;
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2490
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2491 i = asize;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2492 asize = bsize;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2493 bsize = i;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2494 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2495
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2496 /* 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
2497 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
2498 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
2499 if (asize == 0)
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2500 return _PyLong_New(0);
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2501 else
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2502 return x_mul(a, b);
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2503 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2504
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2505 /* 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
2506 * 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
2507 * 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
2508 * 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
2509 * 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
2510 */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2511 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
2512 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
2513
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
2514 /* 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
2515 shift = bsize >> 1;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2516 if (kmul_split(a, shift, &ah, &al) < 0) goto fail;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2517 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
2518
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2519 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2520 bh = ah;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2521 bl = al;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2522 Py_INCREF(bh);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2523 Py_INCREF(bl);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2524 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2525 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
2526
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
2527 /* 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
2528 * 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
2529 * 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
2530 * 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
2531 * 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
2532 * 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
2533 * 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
2534 * 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
2535 * we're effectively doing unsigned arithmetic mod
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2536 * PyLong_BASE**(sizea + sizeb), and so long as the *final* result fits,
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
2537 * 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
2538 * 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
2539 * 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
2540 * 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
2541 */
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2542
3d2335094b88 k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents: 24755
diff changeset
2543 /* 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
2544 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
2545 if (ret == NULL) goto fail;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2546 #ifdef Py_DEBUG
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2547 /* Fill with trash, to catch reference to uninitialized digits. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2548 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
2549 #endif
24751
dbf6e563023b x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents: 24750
diff changeset
2550
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
2551 /* 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
2552 if ((t1 = k_mul(ah, bh)) == NULL) goto fail;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2553 assert(Py_SIZE(t1) >= 0);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2554 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
2555 memcpy(ret->ob_digit + 2*shift, t1->ob_digit,
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2556 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
2557
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2558 /* Zero-out the digits higher than the ah*bh copy. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2559 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
2560 if (i)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2561 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
2562 i * sizeof(digit));
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2563
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
2564 /* 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
2565 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
2566 Py_DECREF(t1);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2567 goto fail;
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2568 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2569 assert(Py_SIZE(t2) >= 0);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2570 assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2571 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
2572
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2573 /* Zero out remaining digits. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2574 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
2575 if (i)
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2576 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
2577
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
2578 /* 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
2579 * 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
2580 */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2581 i = Py_SIZE(ret) - shift; /* # digits after shift */
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2582 (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
2583 Py_DECREF(t2);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2584
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2585 (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
2586 Py_DECREF(t1);
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2587
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
2588 /* 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
2589 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
2590 Py_DECREF(ah);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2591 Py_DECREF(al);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2592 ah = al = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2593
33325
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2594 if (a == b) {
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2595 t2 = t1;
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2596 Py_INCREF(t2);
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2597 }
1768aff92864 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 29641
diff changeset
2598 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
2599 Py_DECREF(t1);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2600 goto fail;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2601 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2602 Py_DECREF(bh);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2603 Py_DECREF(bl);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2604 bh = bl = NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2605
24755
c88d0848c5c9 k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents: 24751
diff changeset
2606 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
2607 Py_DECREF(t1);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2608 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
2609 if (t3 == NULL) goto fail;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2610 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
2611
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
2612 /* 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
2613 * 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
2614 */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2615 (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
2616 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
2617
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2618 return long_normalize(ret);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2619
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2620 fail:
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2621 Py_XDECREF(ret);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2622 Py_XDECREF(ah);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2623 Py_XDECREF(al);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2624 Py_XDECREF(bh);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2625 Py_XDECREF(bl);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2626 return NULL;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2627 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2628
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
2629 /* (*) 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
2630
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
2631 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
2632 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
2633
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
2634 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
2635 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
2636 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
2637 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
2638 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
2639 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
2640
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
2641 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
2642 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
2643 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
2644 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
2645
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
2646 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
2647 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
2648
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
2649 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
2650 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
2651 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
2652
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
2653 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
2654
10a4ec45549d k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents: 24845
diff changeset
2655 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
2656
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
2657 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
2658 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
2659 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
2660 (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
2661 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
2662 asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2663 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
2664 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
2665 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
2666 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
2667 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
2668
24824
7c91d303c563 k_mul() comments: Simplified the simplified explanation of why ah*bh and
Tim Peters <tim.peters@gmail.com>
parents: 24821
diff changeset
2669 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
2670 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
2671 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
2672 */
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
2673
24773
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2674 /* 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
2675 * 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
2676 * 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
2677 * 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
2678 * 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
2679 * 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
2680 * 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
2681 */
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2682 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
2683 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
2684 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2685 const Py_ssize_t asize = ABS(Py_SIZE(a));
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2686 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
2687 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
2688 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
2689 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
2690
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2691 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
2692 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
2693
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2694 /* 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
2695 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
2696 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
2697 return NULL;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2698 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
2699
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2700 /* 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
2701 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
2702 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
2703 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
2704
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2705 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
2706 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
2707 PyLongObject *product;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2708 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
2709
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2710 /* 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
2711 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
2712 nbtouse * sizeof(digit));
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2713 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
2714 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
2715 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
2716 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
2717
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2718 /* Add into result. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2719 (void)v_iadd(ret->ob_digit + nbdone, Py_SIZE(ret) - nbdone,
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2720 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
2721 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
2722
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2723 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
2724 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
2725 }
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2726
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2727 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
2728 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
2729
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2730 fail:
801a44ab2aae Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents: 24765
diff changeset
2731 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
2732 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
2733 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
2734 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2735
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2736 static PyObject *
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2737 long_mul(PyLongObject *v, PyLongObject *w)
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2738 {
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2739 PyLongObject *a, *b, *z;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2740
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2741 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2742 Py_INCREF(Py_NotImplemented);
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2743 return Py_NotImplemented;
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2744 }
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2745
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
2746 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
2747 /* Negate if exactly one of the inputs is negative. */
6939d72182e6 long_mul(): Simplified exit code. In particular, k_mul() returns a
Tim Peters <tim.peters@gmail.com>
parents: 24824
diff changeset
2748 if (((a->ob_size ^ b->ob_size) < 0) && 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
2749 z->ob_size = -(z->ob_size);
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
2750 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
2751 Py_DECREF(b);
24845
6939d72182e6 long_mul(): Simplified exit code. In particular, k_mul() returns a
Tim Peters <tim.peters@gmail.com>
parents: 24824
diff changeset
2752 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2753 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2754
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2755 /* The / and % operators are now defined in terms of divmod().
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2756 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
2757 The long_divrem function gives the remainder after division of
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2758 |a| by |b|, with the sign of a. This is also expressed
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2759 as a - b*trunc(a/b), if trunc truncates towards zero.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2760 Some examples:
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2761 a b a rem b a mod b
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2762 13 10 3 3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2763 -13 10 -3 7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2764 13 -10 3 -7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2765 -13 -10 -3 -3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2766 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
2767 have different signs. We then subtract one from the 'div'
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2768 part of the outcome to keep the invariant intact. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2769
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2770 /* Compute
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2771 * *pdiv, *pmod = divmod(v, w)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2772 * 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
2773 * 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
2774 * 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
2775 */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2776 static int
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2777 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
2778 PyLongObject **pdiv, PyLongObject **pmod)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2779 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2780 PyLongObject *div, *mod;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2781
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2782 if (long_divrem(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2783 return -1;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2784 if ((Py_SIZE(mod) < 0 && Py_SIZE(w) > 0) ||
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2785 (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
2786 PyLongObject *temp;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2787 PyLongObject *one;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2788 temp = (PyLongObject *) long_add(mod, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2789 Py_DECREF(mod);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2790 mod = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2791 if (mod == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2792 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2793 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2794 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2795 one = (PyLongObject *) PyLong_FromLong(1L);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2796 if (one == NULL ||
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2797 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2798 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2799 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2800 Py_XDECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2801 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2802 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2803 Py_DECREF(one);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2804 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2805 div = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2806 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2807 if (pdiv != NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2808 *pdiv = div;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2809 else
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2810 Py_DECREF(div);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2811
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2812 if (pmod != NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2813 *pmod = mod;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2814 else
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2815 Py_DECREF(mod);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2816
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2817 return 0;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2818 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2819
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2820 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
2821 long_div(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2822 {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2823 PyLongObject *a, *b, *div;
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
2824
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2825 CONVERT_BINOP(v, w, &a, &b);
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2826 if (l_divmod(a, b, &div, NULL) < 0)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2827 div = 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
2828 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
2829 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2830 return (PyObject *)div;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2831 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2832
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2833 static PyObject *
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2834 long_classic_div(PyObject *v, PyObject *w)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2835 {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2836 PyLongObject *a, *b, *div;
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2837
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2838 CONVERT_BINOP(v, w, &a, &b);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2839 if (Py_DivisionWarningFlag &&
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2840 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2841 div = NULL;
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2842 else if (l_divmod(a, b, &div, NULL) < 0)
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2843 div = NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2844 Py_DECREF(a);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2845 Py_DECREF(b);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2846 return (PyObject *)div;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2847 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2848
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2849 static PyObject *
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2850 long_true_divide(PyObject *v, PyObject *w)
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2851 {
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
2852 PyLongObject *a, *b;
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
2853 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
2854 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
2855
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
2856 CONVERT_BINOP(v, w, &a, &b);
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
2857 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
2858 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
2859 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
2860 Py_DECREF(a);
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
2861 Py_DECREF(b);
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
2862 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
2863 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
2864 /* '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
2865 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
2866 _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
2867 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
2868
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
2869 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
2870 PyErr_SetString(PyExc_ZeroDivisionError,
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
2871 "long division or modulo by zero");
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
2872 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
2873 }
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
2874
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2875 /* 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
2876 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
2877 aexp -= bexp;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2878 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
2879 goto overflow;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2880 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
2881 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
2882 errno = 0;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
2883 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
2884 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
2885 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
2886 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
2887
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
2888 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
2889 PyErr_SetString(PyExc_OverflowError,
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
2890 "long/long too large for a float");
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
2891 return NULL;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2892
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2893 }
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2894
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
2895 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
2896 long_mod(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2897 {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2898 PyLongObject *a, *b, *mod;
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
2899
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2900 CONVERT_BINOP(v, w, &a, &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
2901
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2902 if (l_divmod(a, b, NULL, &mod) < 0)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2903 mod = 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
2904 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
2905 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2906 return (PyObject *)mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2907 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
2908
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2909 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
2910 long_divmod(PyObject *v, PyObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2911 {
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
2912 PyLongObject *a, *b, *div, *mod;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2913 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
2914
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2915 CONVERT_BINOP(v, w, &a, &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
2916
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
2917 if (l_divmod(a, b, &div, &mod) < 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
2918 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
2919 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2920 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
2921 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2922 z = PyTuple_New(2);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2923 if (z != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2924 PyTuple_SetItem(z, 0, (PyObject *) div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2925 PyTuple_SetItem(z, 1, (PyObject *) mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2926 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2927 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2928 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2929 Py_DECREF(mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2930 }
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
2931 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
2932 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2933 return z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2934 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2935
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2936 /* pow(v, w, x) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2937 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
2938 long_pow(PyObject *v, PyObject *w, PyObject *x)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2939 {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2940 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
2941 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
2942
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2943 PyLongObject *z = NULL; /* accumulated result */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
2944 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
2945 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
2946
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2947 /* 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
2948 * 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
2949 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2950 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
2951 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
2952
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2953 /* a, b, c = v, w, 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
2954 CONVERT_BINOP(v, w, &a, &b);
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2955 if (PyLong_Check(x)) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2956 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
2957 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
2958 }
35254
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
2959 else if (PyInt_Check(x)) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2960 c = (PyLongObject *)PyLong_FromLong(PyInt_AS_LONG(x));
35254
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
2961 if (c == NULL)
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
2962 goto Error;
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
2963 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2964 else if (x == Py_None)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2965 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
2966 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
2967 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
2968 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
2969 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
2970 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
2971 }
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
2972
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2973 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
2974 if (c) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
2975 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
2976 "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
2977 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
2978 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2979 else {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2980 /* 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
2981 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
2982 arguments to double. */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2983 Py_DECREF(a);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2984 Py_DECREF(b);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2985 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
2986 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
2987 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2988
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2989 if (c) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2990 /* if modulus == 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2991 raise ValueError() */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
2992 if (Py_SIZE(c) == 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2993 PyErr_SetString(PyExc_ValueError,
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2994 "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
2995 goto Error;
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2996 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
2997
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2998 /* if modulus < 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
2999 negativeOutput = True
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3000 modulus = -modulus */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3001 if (Py_SIZE(c) < 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3002 negativeOutput = 1;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3003 temp = (PyLongObject *)_PyLong_Copy(c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3004 if (temp == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3005 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3006 Py_DECREF(c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3007 c = temp;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3008 temp = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3009 c->ob_size = - c->ob_size;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3010 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3011
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3012 /* if modulus == 1:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3013 return 0 */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3014 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
3015 z = (PyLongObject *)PyLong_FromLong(0L);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3016 goto Done;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3017 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3018
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3019 /* if base < 0:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3020 base = base % modulus
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3021 Having the base positive just makes things easier. */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3022 if (Py_SIZE(a) < 0) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3023 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
3024 goto Error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3025 Py_DECREF(a);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3026 a = temp;
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3027 temp = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3028 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3029 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3030
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3031 /* 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
3032 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
3033
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3034 z = (PyLongObject *)PyLong_FromLong(1L);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3035 if (z == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3036 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3037
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3038 /* 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
3039 * is NULL.
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3040 */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3041 #define REDUCE(X) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3042 if (c != NULL) { \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3043 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
3044 goto Error; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3045 Py_XDECREF(X); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3046 X = temp; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3047 temp = NULL; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3048 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3049
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3050 /* Multiply two values, then reduce the result:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3051 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
3052 #define MULT(X, Y, result) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3053 { \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3054 temp = (PyLongObject *)long_mul(X, Y); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3055 if (temp == NULL) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3056 goto Error; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3057 Py_XDECREF(result); \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3058 result = temp; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3059 temp = NULL; \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3060 REDUCE(result) \
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3061 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3062
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3063 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3064 /* 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
3065 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3066 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
3067 digit bi = b->ob_digit[i];
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3068
51644
af7d780f7a0b A few more minor fixes in longobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
3069 for (j = (digit)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
3070 MULT(z, z, z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3071 if (bi & j)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3072 MULT(z, a, z)
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3073 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3074 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
3075 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3076 else {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3077 /* 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
3078 Py_INCREF(z); /* still holds 1L */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3079 table[0] = z;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3080 for (i = 1; i < 32; ++i)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3081 MULT(table[i-1], a, table[i])
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3082
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3083 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
3084 const digit bi = b->ob_digit[i];
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3085
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3086 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
3087 const int index = (bi >> j) & 0x1f;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3088 for (k = 0; k < 5; ++k)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3089 MULT(z, z, z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3090 if (index)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3091 MULT(z, table[index], z)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3092 }
11380
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
3093 }
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
3094 }
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3095
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3096 if (negativeOutput && (Py_SIZE(z) != 0)) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3097 temp = (PyLongObject *)long_sub(z, c);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3098 if (temp == NULL)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3099 goto Error;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3100 Py_DECREF(z);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3101 z = temp;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3102 temp = NULL;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3103 }
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3104 goto Done;
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3105
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3106 Error:
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3107 if (z != NULL) {
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3108 Py_DECREF(z);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3109 z = 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 /* fall through */
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3112 Done:
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3113 if (Py_SIZE(b) > FIVEARY_CUTOFF) {
33327
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3114 for (i = 0; i < 32; ++i)
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3115 Py_XDECREF(table[i]);
5452d4ecddd0 SF patch 936813: fast modular exponentiation
Tim Peters <tim.peters@gmail.com>
parents: 33325
diff changeset
3116 }
35254
d61efcd87c8e SF bug #1238681: freed pointer is used in longobject.c:long_pow().
Tim Peters <tim.peters@gmail.com>
parents: 35177
diff changeset
3117 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
3118 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
3119 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
3120 Py_XDECREF(temp);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3121 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3122 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3123
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3124 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
3125 long_invert(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3126 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3127 /* Implement ~x as -(x+1) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3128 PyLongObject *x;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3129 PyLongObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3130 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
3131 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
3132 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3133 x = (PyLongObject *) long_add(v, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3134 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
3135 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
3136 return NULL;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3137 Py_SIZE(x) = -(Py_SIZE(x));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3138 return (PyObject *)x;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3139 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3140
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3141 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
3142 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
3143 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3144 PyLongObject *z;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3145 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3146 /* -0 == 0 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3147 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3148 return (PyObject *) 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
3149 }
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3150 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
3151 if (z != NULL)
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
3152 z->ob_size = -(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3153 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
3154 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3155
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3156 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
3157 long_abs(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3158 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3159 if (v->ob_size < 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
3160 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
3161 else
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3162 return long_long((PyObject *)v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3163 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3164
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3165 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
3166 long_nonzero(PyLongObject *v)
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3167 {
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3168 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
3169 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3170
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3171 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
3172 long_rshift(PyLongObject *v, PyLongObject *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
3173 {
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
3174 PyLongObject *a, *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
3175 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
3176 long shiftby;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3177 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
3178 digit lomask, himask;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3179
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
3180 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &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
3181
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3182 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
3183 /* 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
3184 PyLongObject *a1, *a2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3185 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
3186 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
3187 goto rshift_error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3188 a2 = (PyLongObject *) long_rshift(a1, b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3189 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
3190 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
3191 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
3192 z = (PyLongObject *) long_invert(a2);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3193 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
3194 }
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
3195 else {
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3196
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
3197 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
3198 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
3199 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
3200 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
3201 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
3202 "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
3203 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
3204 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3205 wordshift = shiftby / PyLong_SHIFT;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3206 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
3207 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
3208 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
3209 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
3210 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
3211 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
3212 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3213 loshift = shiftby % PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3214 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
3215 lomask = ((digit)1 << hishift) - 1;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3216 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
3217 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
3218 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
3219 goto rshift_error;
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3220 if (Py_SIZE(a) < 0)
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3221 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
3222 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
3223 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
3224 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
3225 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
3226 (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
3227 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3228 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
3229 }
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
3230 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
3231 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
3232 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
3233 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
3234
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3235 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3236
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3237 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
3238 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
3239 {
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
3240 /* This version due to Tim Peters */
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 PyLongObject *a, *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
3242 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
3243 long shiftby;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3244 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
3245 twodigits accum;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3246
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
3247 CONVERT_BINOP(v, w, &a, &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
3248
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3249 shiftby = PyLong_AsLong((PyObject *)b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3250 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
3251 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
3252 if (shiftby < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3253 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
3254 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
3255 }
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
3256 if ((long)(int)shiftby != shiftby) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3257 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3258 "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
3259 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
3260 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3261 /* wordshift, remshift = divmod(shiftby, PyLong_SHIFT) */
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3262 wordshift = (int)shiftby / PyLong_SHIFT;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3263 remshift = (int)shiftby - wordshift * PyLong_SHIFT;
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
3264
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3265 oldsize = ABS(a->ob_size);
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3266 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
3267 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
3268 ++newsize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3269 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
3270 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
3271 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
3272 if (a->ob_size < 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
3273 z->ob_size = -(z->ob_size);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3274 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
3275 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
3276 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
3277 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
3278 accum |= (twodigits)a->ob_digit[j] << remshift;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3279 z->ob_digit[i] = (digit)(accum & PyLong_MASK);
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3280 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
3281 }
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
3282 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
3283 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
3284 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
3285 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
3286 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
3287 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
3288 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
3289 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
3290 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
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
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3293
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3294 /* 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
3295
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3296 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
3297 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
3298 int op, /* '&', '|', '^' */
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3299 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
3300 {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3301 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
3302 int negz;
51644
af7d780f7a0b A few more minor fixes in longobject.c
Mark Dickinson <dickinsm@gmail.com>
parents: 51641
diff changeset
3303 Py_ssize_t size_a, size_b, size_z, i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3304 PyLongObject *z;
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
3305 digit diga, digb;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3306 PyObject *v;
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3307
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3308 if (Py_SIZE(a) < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3309 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
3310 if (a == NULL)
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3311 return NULL;
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3312 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
3313 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3314 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3315 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
3316 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
3317 }
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3318 if (Py_SIZE(b) < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3319 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
3320 if (b == NULL) {
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3321 Py_DECREF(a);
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3322 return NULL;
e9a7a2784e17 SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 36675
diff changeset
3323 }
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3324 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
3325 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3326 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3327 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
3328 maskb = 0;
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
3329 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3330
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3331 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
3332 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
3333 case '^':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3334 if (maska != maskb) {
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3335 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
3336 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
3337 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3338 break;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
3339 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
3340 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
3341 op = '|';
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3342 maska ^= PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3343 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
3344 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
3345 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3346 break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3347 case '|':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3348 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
3349 op = '&';
44945
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3350 maska ^= PyLong_MASK;
bbc719a8b58b Backport of several functions from Python 3.0 to 2.6 including PyUnicode_FromString, PyUnicode_Format and PyLong_From/AsSsize_t. The functions are partly required for the backport of the bytearray type and _fileio module. They should also make it easier to port C to 3.0.
Christian Heimes <christian@cheimes.de>
parents: 44749
diff changeset
3351 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
3352 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
3353 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3354 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
3355 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3356
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3357 /* 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
3358 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
3359 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
3360 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
3361 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
3362 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
3363 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
3364 whose length should be ignored.
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3365 */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3366
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3367 size_a = Py_SIZE(a);
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3368 size_b = Py_SIZE(b);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3369 size_z = op == '&'
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3370 ? (maska
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3371 ? size_b
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3372 : (maskb ? size_a : MIN(size_a, size_b)))
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3373 : MAX(size_a, size_b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3374 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
3375 if (z == NULL) {
39095
bda93b45ca41 a & b were dereffed above, so they are known to be valid pointers.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38513
diff changeset
3376 Py_DECREF(a);
bda93b45ca41 a & b were dereffed above, so they are known to be valid pointers.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38513
diff changeset
3377 Py_DECREF(b);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3378 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3379 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3380
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
3381 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
3382 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
3383 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
3384 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
3385 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
3386 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
3387 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
3388 }
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
3389 }
24745
ba035f4b47c1 Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
3390
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3391 Py_DECREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3392 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
3393 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
3394 if (negz == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3395 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
3396 v = long_invert(z);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3397 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
3398 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
3399 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3400
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3401 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
3402 long_and(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
3403 {
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
3404 PyLongObject *a, *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
3405 PyObject *c;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3406 CONVERT_BINOP(v, w, &a, &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
3407 c = long_bitwise(a, '&', 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
3408 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
3409 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
3410 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
3411 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3412
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3413 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
3414 long_xor(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
3415 {
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
3416 PyLongObject *a, *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
3417 PyObject *c;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3418 CONVERT_BINOP(v, w, &a, &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
3419 c = long_bitwise(a, '^', 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
3420 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
3421 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
3422 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
3423 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
3424
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3425 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
3426 long_or(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
3427 {
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
3428 PyLongObject *a, *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
3429 PyObject *c;
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
3430 CONVERT_BINOP(v, w, &a, &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
3431 c = long_bitwise(a, '|', 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
3432 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
3433 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
3434 return c;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3435 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
3436
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
3437 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
3438 long_coerce(PyObject **pv, PyObject **pw)
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3439 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3440 if (PyInt_Check(*pw)) {
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
3441 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
41435
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
3442 if (*pw == NULL)
46d0f4057705 Add some missing NULL checks which trigger crashes on low-memory conditions.
Georg Brandl <georg@python.org>
parents: 41032
diff changeset
3443 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3444 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3445 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3446 }
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
3447 else if (PyLong_Check(*pw)) {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
3448 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
3449 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
3450 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
3451 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3452 return 1; /* Can't do it */
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3453 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3454
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3455 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
3456 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
3457 {
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3458 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
3459 Py_INCREF(v);
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3460 else
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 34666
diff changeset
3461 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
3462 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
3463 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3464
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3465 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
3466 long_int(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3467 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3468 long x;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3469 x = PyLong_AsLong(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
3470 if (PyErr_Occurred()) {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3471 if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3472 PyErr_Clear();
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3473 if (PyLong_CheckExact(v)) {
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3474 Py_INCREF(v);
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3475 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
3476 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3477 else
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3478 return _PyLong_Copy((PyLongObject *)v);
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3479 }
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3480 else
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3481 return NULL;
d31ddc66b1b5 Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents: 26081
diff changeset
3482 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3483 return PyInt_FromLong(x);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3484 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3485
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3486 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
3487 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
3488 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
3489 double result;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3490 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
3491 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
3492 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3493 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
3494 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3495
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3496 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
3497 long_oct(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3498 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
3499 return _PyLong_Format(v, 8, 1, 0);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3500 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3501
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3502 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
3503 long_hex(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3504 {
45262
86906a5ceca6 Added PyNumber_ToBase and supporting routines _PyInt_Format and
Eric Smith <eric@trueblade.com>
parents: 44945
diff changeset
3505 return _PyLong_Format(v, 16, 1, 0);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
3506 }
24280
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24231
diff changeset
3507
1598a1e473e2 staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24231
diff changeset
3508 static PyObject *
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3509 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
3510
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3511 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3512 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
3513 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3514 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3515 int base = -909; /* unlikely! */
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
3516 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
3517
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3518 if (type != &PyLong_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3519 return long_subtype_new(type, args, kwds); /* Wimp out */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3520 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3521 &x, &base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3522 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3523 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3524 return PyLong_FromLong(0L);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3525 if (base == -909)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3526 return PyNumber_Long(x);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3527 else if (PyString_Check(x)) {
41032
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3528 /* Since PyLong_FromString doesn't have a length parameter,
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3529 * check here for possible NULs in the string. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3530 char *string = PyString_AS_STRING(x);
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
3531 if (strlen(string) != (size_t)PyString_Size(x)) {
41032
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3532 /* create a repr() of the input string,
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3533 * just like PyLong_FromString does. */
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3534 PyObject *srepr;
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3535 srepr = PyObject_Repr(x);
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3536 if (srepr == NULL)
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3537 return NULL;
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3538 PyErr_Format(PyExc_ValueError,
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3539 "invalid literal for long() with base %d: %s",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3540 base, PyString_AS_STRING(srepr));
41032
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3541 Py_DECREF(srepr);
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3542 return NULL;
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3543 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47421
diff changeset
3544 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
41032
6a599aee7ca4 Patch #1638879: don't accept strings with embedded NUL bytes in long().
Georg Brandl <georg@python.org>
parents: 40908
diff changeset
3545 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
3546 #ifdef Py_USING_UNICODE
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3547 else if (PyUnicode_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3548 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3549 PyUnicode_GET_SIZE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3550 base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
3551 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3552 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3553 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3554 "long() can't convert non-string with explicit base");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3555 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3556 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3557 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3558
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3559 /* 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
3560 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
3561 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
3562 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
3563 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3564 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3565 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
3566 {
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3567 PyLongObject *tmp, *newobj;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36430
diff changeset
3568 Py_ssize_t i, n;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3569
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3570 assert(PyType_IsSubtype(type, &PyLong_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3571 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
3572 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3573 return NULL;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
3574 assert(PyLong_CheckExact(tmp));
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3575 n = Py_SIZE(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3576 if (n < 0)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3577 n = -n;
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3578 newobj = (PyLongObject *)type->tp_alloc(type, n);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3579 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
3580 Py_DECREF(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3581 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
3582 }
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3583 assert(PyLong_Check(newobj));
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43406
diff changeset
3584 Py_SIZE(newobj) = Py_SIZE(tmp);
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3585 for (i = 0; i < n; i++)
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3586 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
3587 Py_DECREF(tmp);
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37568
diff changeset
3588 return (PyObject *)newobj;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3589 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3590
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3591 static PyObject *
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3592 long_getnewargs(PyLongObject *v)
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3593 {
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3594 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
3595 }
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3596
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3597 static PyObject *
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3598 long_get0(PyLongObject *v, void *context) {
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3599 return PyLong_FromLong(0L);
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3600 }
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3601
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3602 static PyObject *
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3603 long_get1(PyLongObject *v, void *context) {
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3604 return PyLong_FromLong(1L);
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3605 }
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3606
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3607 static PyObject *
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3608 long__format__(PyObject *self, PyObject *args)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3609 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3610 PyObject *format_spec;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3611
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3612 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3613 return NULL;
47294
4fe192d48960 Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46473
diff changeset
3614 if (PyBytes_Check(format_spec))
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3615 return _PyLong_FormatAdvanced(self,
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3616 PyBytes_AS_STRING(format_spec),
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3617 PyBytes_GET_SIZE(format_spec));
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3618 if (PyUnicode_Check(format_spec)) {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3619 /* Convert format_spec to a str */
47392
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3620 PyObject *result;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3621 PyObject *str_spec = PyObject_Str(format_spec);
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3622
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3623 if (str_spec == NULL)
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3624 return NULL;
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3625
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3626 result = _PyLong_FormatAdvanced(self,
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3627 PyBytes_AS_STRING(str_spec),
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3628 PyBytes_GET_SIZE(str_spec));
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3629
47ef9a806ce9 Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents: 47294
diff changeset
3630 Py_DECREF(str_spec);
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3631 return result;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3632 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3633 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3634 return NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3635 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3636
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3637 static PyObject *
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3638 long_sizeof(PyLongObject *v)
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3639 {
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3640 Py_ssize_t res;
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3641
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
3642 res = v->ob_type->tp_basicsize + ABS(Py_SIZE(v))*sizeof(digit);
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3643 return PyInt_FromSsize_t(res);
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3644 }
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3645
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3646 static PyObject *
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3647 long_bit_length(PyLongObject *v)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3648 {
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3649 PyLongObject *result, *x, *y;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3650 Py_ssize_t ndigits, msd_bits = 0;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3651 digit msd;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3652
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3653 assert(v != NULL);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3654 assert(PyLong_Check(v));
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3655
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3656 ndigits = ABS(Py_SIZE(v));
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3657 if (ndigits == 0)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3658 return PyInt_FromLong(0);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3659
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3660 msd = v->ob_digit[ndigits-1];
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3661 while (msd >= 32) {
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3662 msd_bits += 6;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3663 msd >>= 6;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3664 }
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3665 msd_bits += (long)(BitLengthTable[msd]);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3666
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3667 if (ndigits <= PY_SSIZE_T_MAX/PyLong_SHIFT)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3668 return PyInt_FromSsize_t((ndigits-1)*PyLong_SHIFT + msd_bits);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3669
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3670 /* expression above may overflow; use Python integers instead */
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3671 result = (PyLongObject *)PyLong_FromSsize_t(ndigits - 1);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3672 if (result == NULL)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3673 return NULL;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3674 x = (PyLongObject *)PyLong_FromLong(PyLong_SHIFT);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3675 if (x == NULL)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3676 goto error;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3677 y = (PyLongObject *)long_mul(result, x);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3678 Py_DECREF(x);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3679 if (y == NULL)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3680 goto error;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3681 Py_DECREF(result);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3682 result = y;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3683
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3684 x = (PyLongObject *)PyLong_FromLong(msd_bits);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3685 if (x == NULL)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3686 goto error;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3687 y = (PyLongObject *)long_add(result, x);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3688 Py_DECREF(x);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3689 if (y == NULL)
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3690 goto error;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3691 Py_DECREF(result);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3692 result = y;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3693
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3694 return (PyObject *)result;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3695
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3696 error:
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3697 Py_DECREF(result);
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3698 return NULL;
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3699 }
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3700
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3701 PyDoc_STRVAR(long_bit_length_doc,
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3702 "long.bit_length() -> int or long\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3703 \n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3704 Number of bits necessary to represent self in binary.\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3705 >>> bin(37L)\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3706 '0b100101'\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3707 >>> (37L).bit_length()\n\
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3708 6");
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3709
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3710 #if 0
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3711 static PyObject *
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3712 long_is_finite(PyObject *v)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3713 {
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3714 Py_RETURN_TRUE;
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3715 }
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3716 #endif
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3717
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3718 static PyMethodDef long_methods[] = {
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3719 {"conjugate", (PyCFunction)long_long, METH_NOARGS,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3720 "Returns self, the complex conjugate of any long."},
50205
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3721 {"bit_length", (PyCFunction)long_bit_length, METH_NOARGS,
f0db06f49a4e Issue #3439: add bit_length method to int and long.
Mark Dickinson <dickinsm@gmail.com>
parents: 48177
diff changeset
3722 long_bit_length_doc},
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3723 #if 0
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3724 {"is_finite", (PyCFunction)long_is_finite, METH_NOARGS,
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3725 "Returns always True."},
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 46459
diff changeset
3726 #endif
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3727 {"__trunc__", (PyCFunction)long_long, METH_NOARGS,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3728 "Truncating an Integral returns itself."},
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3729 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45262
diff changeset
3730 {"__format__", (PyCFunction)long__format__, METH_VARARGS},
47419
88bd07c0a847 Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents: 47392
diff changeset
3731 {"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,
47421
a65f095b27f3 Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents: 47419
diff changeset
3732 "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
3733 {NULL, NULL} /* sentinel */
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3734 };
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3735
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3736 static PyGetSetDef long_getset[] = {
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3737 {"real",
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3738 (getter)long_long, (setter)NULL,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3739 "the real part of a complex number",
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3740 NULL},
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3741 {"imag",
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3742 (getter)long_get0, (setter)NULL,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3743 "the imaginary part of a complex number",
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3744 NULL},
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3745 {"numerator",
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3746 (getter)long_long, (setter)NULL,
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3747 "the numerator of a rational number in lowest terms",
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3748 NULL},
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3749 {"denominator",
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3750 (getter)long_get1, (setter)NULL,
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3751 "the denominator of a rational number in lowest terms",
53675
5aaf24ec43dc Remove unnecessary use of context for long getters.
Mark Dickinson <dickinsm@gmail.com>
parents: 53289
diff changeset
3752 NULL},
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3753 {NULL} /* Sentinel */
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3754 };
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3755
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23248
diff changeset
3756 PyDoc_STRVAR(long_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3757 "long(x[, base]) -> integer\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3758 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3759 Convert a string or number to a long integer, if possible. A floating\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3760 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
3761 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
3762 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
3763 converting a non-string.");
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3764
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3765 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
3766 (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
3767 (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
3768 (binaryfunc) long_mul, /*nb_multiply*/
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3769 long_classic_div, /*nb_divide*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3770 long_mod, /*nb_remainder*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3771 long_divmod, /*nb_divmod*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3772 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
3773 (unaryfunc) long_neg, /*nb_negative*/
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3774 (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
3775 (unaryfunc) long_abs, /*tp_absolute*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3776 (inquiry) long_nonzero, /*tp_nonzero*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
3777 (unaryfunc) long_invert, /*nb_invert*/
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3778 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
3779 (binaryfunc) long_rshift, /*nb_rshift*/
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3780 long_and, /*nb_and*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3781 long_xor, /*nb_xor*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3782 long_or, /*nb_or*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3783 long_coerce, /*nb_coerce*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3784 long_int, /*nb_int*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3785 long_long, /*nb_long*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3786 long_float, /*nb_float*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3787 long_oct, /*nb_oct*/
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3788 long_hex, /*nb_hex*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3789 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3790 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3791 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3792 0, /* nb_inplace_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3793 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3794 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3795 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3796 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3797 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3798 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3799 0, /* nb_inplace_or */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3800 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
3801 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
3802 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
3803 0, /* nb_inplace_true_divide */
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39141
diff changeset
3804 long_long, /* nb_index */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3805 };
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3806
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3807 PyTypeObject PyLong_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
3808 PyObject_HEAD_INIT(&PyType_Type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3809 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3810 "long", /* tp_name */
51641
b55a3b5d7008 Issue #5260: Various portability and standards compliance fixes, optimizations
Mark Dickinson <dickinsm@gmail.com>
parents: 51517
diff changeset
3811 offsetof(PyLongObject, ob_digit), /* tp_basicsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3812 sizeof(digit), /* tp_itemsize */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3813 long_dealloc, /* tp_dealloc */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3814 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3815 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3816 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3817 (cmpfunc)long_compare, /* tp_compare */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3818 long_repr, /* tp_repr */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3819 &long_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3820 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3821 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3822 (hashfunc)long_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3823 0, /* tp_call */
37304
79a0f080f898 Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents: 37288
diff changeset
3824 long_str, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3825 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3826 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3827 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
3828 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
40908
d01bd37bf5ce Variation of patch # 1624059 to speed up checking if an object is a subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 39995
diff changeset
3829 Py_TPFLAGS_BASETYPE | 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
3830 long_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3831 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3832 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3833 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3834 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3835 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3836 0, /* tp_iternext */
27521
17bfd5dbc0f1 Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents: 27487
diff changeset
3837 long_methods, /* tp_methods */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3838 0, /* tp_members */
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44350
diff changeset
3839 long_getset, /* tp_getset */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3840 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3841 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3842 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3843 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3844 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3845 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3846 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
3847 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
3848 PyObject_Del, /* tp_free */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3849 };
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3850
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3851 static PyTypeObject Long_InfoType;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3852
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3853 PyDoc_STRVAR(long_info__doc__,
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3854 "sys.long_info\n\
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3855 \n\
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3856 A struct sequence that holds information about Python's\n\
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3857 internal representation of integers. The attributes are read only.");
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3858
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3859 static PyStructSequence_Field long_info_fields[] = {
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3860 {"bits_per_digit", "size of a digit in bits"},
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3861 {"sizeof_digit", "size in bytes of the C type used to "
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3862 "represent a digit"},
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3863 {NULL, NULL}
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3864 };
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3865
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3866 static PyStructSequence_Desc long_info_desc = {
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3867 "sys.long_info", /* name */
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3868 long_info__doc__, /* doc */
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3869 long_info_fields, /* fields */
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3870 2 /* number of fields */
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3871 };
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3872
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3873 PyObject *
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3874 PyLong_GetInfo(void)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3875 {
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3876 PyObject* long_info;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3877 int field = 0;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3878 long_info = PyStructSequence_New(&Long_InfoType);
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3879 if (long_info == NULL)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3880 return NULL;
52739
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52284
diff changeset
3881 PyStructSequence_SET_ITEM(long_info, field++,
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52284
diff changeset
3882 PyInt_FromLong(PyLong_SHIFT));
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52284
diff changeset
3883 PyStructSequence_SET_ITEM(long_info, field++,
7fa0f81d607e sys.long_info attributes should be ints, not longs
Mark Dickinson <dickinsm@gmail.com>
parents: 52284
diff changeset
3884 PyInt_FromLong(sizeof(digit)));
52233
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3885 if (PyErr_Occurred()) {
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3886 Py_CLEAR(long_info);
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3887 return NULL;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3888 }
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3889 return long_info;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3890 }
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3891
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3892 int
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3893 _PyLong_Init(void)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3894 {
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3895 /* initialize long_info */
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3896 if (Long_InfoType.tp_name == 0)
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3897 PyStructSequence_InitType(&Long_InfoType, &long_info_desc);
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3898 return 1;
9b358eae532a Issue #4258: Use 30-bit digits for Python longs, on 64-bit platforms.
Mark Dickinson <dickinsm@gmail.com>
parents: 51854
diff changeset
3899 }