Mercurial > cpython-lru_cache-weakref
annotate Objects/longobject.c @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 6205da383e4c |
| children | 53bef20f84cc |
| rev | line source |
|---|---|
| 230 | 1 |
| 2 /* Long (arbitrary precision) integer object implementation */ | |
| 3 | |
| 242 | 4 /* XXX The functional organization of this file is terrible */ |
| 5 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
6 #include "Python.h" |
| 230 | 7 #include "longintrepr.h" |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
8 |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
9 #include <ctype.h> |
| 230 | 10 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
11 /* 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
|
12 * both operands contain more than KARATSUBA_CUTOFF digits (this |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
13 * being an internal Python long digit, in base BASE). |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
14 */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
15 #define KARATSUBA_CUTOFF 35 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
16 |
| 613 | 17 #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 18 | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
19 #undef MIN |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
20 #undef MAX |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
21 #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
|
22 #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
|
23 |
| 613 | 24 /* Forward */ |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
25 static PyLongObject *long_normalize(PyLongObject *); |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
26 static PyLongObject *mul1(PyLongObject *, wdigit); |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
27 static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
28 static PyLongObject *divrem1(PyLongObject *, digit, digit *); |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
29 static PyObject *long_format(PyObject *aa, int base, int addL); |
| 613 | 30 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
31 #define SIGCHECK(PyTryBlock) \ |
|
25108
3a26e352f0a6
replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents:
24919
diff
changeset
|
32 if (--_Py_Ticker < 0) { \ |
|
3a26e352f0a6
replace thread state objects' ticker and checkinterval fields with two
Skip Montanaro <skip@pobox.com>
parents:
24919
diff
changeset
|
33 _Py_Ticker = _Py_CheckInterval; \ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
34 if (PyErr_CheckSignals()) { PyTryBlock; } \ |
| 242 | 35 } |
| 36 | |
| 230 | 37 /* Normalize (remove leading zeros from) a long int object. |
| 38 Doesn't attempt to free the storage--in most cases, due to the nature | |
| 39 of the algorithms used, this could save at most be one word anyway. */ | |
| 40 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
41 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
|
42 long_normalize(register PyLongObject *v) |
| 230 | 43 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
44 int j = ABS(v->ob_size); |
| 230 | 45 register int i = j; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
46 |
| 230 | 47 while (i > 0 && v->ob_digit[i-1] == 0) |
| 48 --i; | |
| 49 if (i != j) | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
50 v->ob_size = (v->ob_size < 0) ? -(i) : i; |
| 230 | 51 return v; |
| 52 } | |
| 53 | |
| 54 /* Allocate a new long int object with size digits. | |
| 55 Return NULL and set exception if we run out of memory. */ | |
| 56 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
57 PyLongObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
58 _PyLong_New(int size) |
| 230 | 59 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
60 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); |
| 230 | 61 } |
| 62 | |
|
19837
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
63 PyObject * |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
64 _PyLong_Copy(PyLongObject *src) |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
65 { |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
66 PyLongObject *result; |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
67 int i; |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
68 |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
69 assert(src != NULL); |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
70 i = src->ob_size; |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
71 if (i < 0) |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
72 i = -(i); |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
73 result = _PyLong_New(i); |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
74 if (result != NULL) { |
|
22147
1d1a01d956a2
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21968
diff
changeset
|
75 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
|
76 while (--i >= 0) |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
77 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
|
78 } |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
79 return (PyObject *)result; |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
80 } |
|
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
81 |
| 230 | 82 /* Create a new long int object from a C long int */ |
| 83 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
84 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
85 PyLong_FromLong(long ival) |
| 230 | 86 { |
|
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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
92 if (ival < 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
|
93 ival = -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
|
94 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
|
95 } |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
96 |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
97 /* 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
|
98 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
|
99 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
|
100 needed. */ |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
101 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
|
102 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
|
103 ++ndigits; |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
104 t >>= SHIFT; |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
105 } |
|
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 v = _PyLong_New(ndigits); |
| 230 | 107 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
|
108 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
|
109 v->ob_size = negative ? -ndigits : ndigits; |
|
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 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
|
111 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
|
112 *p++ = (digit)(t & MASK); |
|
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
|
113 t >>= SHIFT; |
| 230 | 114 } |
| 115 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
116 return (PyObject *)v; |
| 230 | 117 } |
| 118 | |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
119 /* 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
|
120 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
121 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
122 PyLong_FromUnsignedLong(unsigned long ival) |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
123 { |
|
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
|
124 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
|
125 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
|
126 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
|
127 |
|
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 /* 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
|
129 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
|
130 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
|
131 ++ndigits; |
|
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 t >>= SHIFT; |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
133 } |
|
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 v = _PyLong_New(ndigits); |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
135 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
|
136 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
|
137 v->ob_size = ndigits; |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
138 while (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
|
139 *p++ = (digit)(ival & MASK); |
|
31cd0db0f09f
PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents:
18110
diff
changeset
|
140 ival >>= SHIFT; |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
141 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
142 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
143 return (PyObject *)v; |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
144 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
145 |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
146 /* 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
|
147 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
148 PyObject * |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
149 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
|
150 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
151 PyLongObject *v; |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
152 double frac; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
153 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
|
154 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
|
155 if (Py_IS_INFINITY(dval)) { |
|
11331
4fdc9c1ecaf6
Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents:
9969
diff
changeset
|
156 PyErr_SetString(PyExc_OverflowError, |
|
4fdc9c1ecaf6
Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents:
9969
diff
changeset
|
157 "cannot convert float infinity to long"); |
|
4fdc9c1ecaf6
Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents:
9969
diff
changeset
|
158 return NULL; |
|
4fdc9c1ecaf6
Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents:
9969
diff
changeset
|
159 } |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
160 if (dval < 0.0) { |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
161 neg = 1; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
162 dval = -dval; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
163 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
164 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
|
165 if (expo <= 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
166 return PyLong_FromLong(0L); |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
167 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
168 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
|
169 if (v == NULL) |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
170 return NULL; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
171 frac = ldexp(frac, (expo-1) % SHIFT + 1); |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
172 for (i = ndig; --i >= 0; ) { |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
173 long bits = (long)frac; |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
174 v->ob_digit[i] = (digit) bits; |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
175 frac = frac - (double)bits; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
176 frac = ldexp(frac, SHIFT); |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
177 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
178 if (neg) |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
179 v->ob_size = -(v->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
180 return (PyObject *)v; |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
181 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
182 |
| 230 | 183 /* Get a C long int from a long int object. |
| 184 Returns -1 and sets an error condition if overflow occurs. */ | |
| 185 | |
| 186 long | |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
187 PyLong_AsLong(PyObject *vv) |
| 230 | 188 { |
|
8425
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
189 /* This version by Tim Peters */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
190 register PyLongObject *v; |
|
8425
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
191 unsigned long x, prev; |
| 230 | 192 int i, sign; |
|
8425
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
193 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
194 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
|
195 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
|
196 return PyInt_AsLong(vv); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
197 PyErr_BadInternalCall(); |
| 230 | 198 return -1; |
| 199 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
200 v = (PyLongObject *)vv; |
| 230 | 201 i = v->ob_size; |
| 202 sign = 1; | |
| 203 x = 0; | |
| 204 if (i < 0) { | |
| 205 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
|
206 i = -(i); |
| 230 | 207 } |
| 208 while (--i >= 0) { | |
| 209 prev = x; | |
| 210 x = (x << SHIFT) + v->ob_digit[i]; | |
|
8425
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
211 if ((x >> SHIFT) != prev) |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
212 goto overflow; |
| 230 | 213 } |
|
8425
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
214 /* Haven't lost any bits, but if the sign bit is set we're in |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
215 * trouble *unless* this is the min negative number. So, |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
216 * trouble iff sign bit set && (positive || some bit set other |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
217 * than the sign bit). |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
218 */ |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
219 if ((long)x < 0 && (sign > 0 || (x << 1) != 0)) |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
220 goto overflow; |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
221 return (long)x * sign; |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
222 |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
223 overflow: |
|
e2d41bf1a0f0
Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
224 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
|
225 "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
|
226 return -1; |
| 230 | 227 } |
| 228 | |
|
24777
696a284569e6
Fix comment for PyLong_AsUnsignedLong() to say that the return value
Guido van Rossum <guido@python.org>
parents:
24775
diff
changeset
|
229 /* 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
|
230 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
|
231 |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
232 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
|
233 PyLong_AsUnsignedLong(PyObject *vv) |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
234 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
235 register PyLongObject *v; |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
236 unsigned long x, prev; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
237 int i; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
238 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
239 if (vv == NULL || !PyLong_Check(vv)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
240 PyErr_BadInternalCall(); |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
241 return (unsigned long) -1; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
242 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
243 v = (PyLongObject *)vv; |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
244 i = v->ob_size; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
245 x = 0; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
246 if (i < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
247 PyErr_SetString(PyExc_OverflowError, |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
248 "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
|
249 return (unsigned long) -1; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
250 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
251 while (--i >= 0) { |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
252 prev = x; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
253 x = (x << SHIFT) + v->ob_digit[i]; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
254 if ((x >> SHIFT) != prev) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
255 PyErr_SetString(PyExc_OverflowError, |
| 15318 | 256 "long int too large to convert"); |
|
4870
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
257 return (unsigned long) -1; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
258 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
259 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
260 return x; |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
261 } |
|
68f4134869db
Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents:
4587
diff
changeset
|
262 |
|
27549
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
263 int |
|
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
264 _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
|
265 { |
|
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
266 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
|
267 |
|
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
268 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
|
269 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
|
270 |
|
27585
a05814184e8c
cPickle.c: Full support for the new LONG1 and LONG4. Added comments.
Tim Peters <tim.peters@gmail.com>
parents:
27567
diff
changeset
|
271 return v->ob_size == 0 ? 0 : (v->ob_size < 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
|
272 } |
|
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
273 |
|
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
|
274 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
|
275 _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
|
276 { |
|
0904d878b8f1
Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents:
26867
diff
changeset
|
277 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
|
278 size_t result = 0; |
|
27639
6205da383e4c
_PyLong_Sign(): remove an assert that needed a variable ndigits that
Guido van Rossum <guido@python.org>
parents:
27597
diff
changeset
|
279 int 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
|
280 |
|
0904d878b8f1
Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents:
26867
diff
changeset
|
281 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
|
282 assert(PyLong_Check(v)); |
|
27639
6205da383e4c
_PyLong_Sign(): remove an assert that needed a variable ndigits that
Guido van Rossum <guido@python.org>
parents:
27597
diff
changeset
|
283 ndigits = ABS(v->ob_size); |
|
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
|
284 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
|
285 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
|
286 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
|
287 |
|
27549
7fcdeeb16ec0
_PyLong_NumBits(): The definition of this was too specific to the quirky
Tim Peters <tim.peters@gmail.com>
parents:
27521
diff
changeset
|
288 result = (ndigits - 1) * SHIFT; |
|
27567
c075ba2abf7a
Squash compiler wng about signed/unsigned comparison mismatch.
Tim Peters <tim.peters@gmail.com>
parents:
27549
diff
changeset
|
289 if (result / 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
|
290 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
|
291 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
|
292 ++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
|
293 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
|
294 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
|
295 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
|
296 } 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
|
297 } |
|
0904d878b8f1
Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents:
26867
diff
changeset
|
298 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
|
299 |
|
0904d878b8f1
Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents:
26867
diff
changeset
|
300 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
|
301 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
|
302 "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
|
303 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
|
304 } |
|
0904d878b8f1
Added new private API function _PyLong_NumBits. This will be used at the
Tim Peters <tim.peters@gmail.com>
parents:
26867
diff
changeset
|
305 |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
306 PyObject * |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
307 _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
|
308 int little_endian, int is_signed) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
309 { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
310 const unsigned char* pstartbyte;/* LSB of bytes */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
311 int incr; /* direction to move pstartbyte */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
312 const unsigned char* pendbyte; /* MSB of bytes */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
313 size_t numsignificantbytes; /* number of bytes that matter */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
314 size_t ndigits; /* number of Python long digits */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
315 PyLongObject* v; /* result */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
316 int idigit = 0; /* next free index in v->ob_digit */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
317 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
318 if (n == 0) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
319 return PyLong_FromLong(0L); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
320 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
321 if (little_endian) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
322 pstartbyte = bytes; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
323 pendbyte = bytes + n - 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
324 incr = 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
325 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
326 else { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
327 pstartbyte = bytes + n - 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
328 pendbyte = bytes; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
329 incr = -1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
330 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
331 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
332 if (is_signed) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
333 is_signed = *pendbyte >= 0x80; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
334 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
335 /* 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
|
336 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
|
337 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
|
338 { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
339 size_t i; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
340 const unsigned char* p = pendbyte; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
341 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
|
342 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
|
343 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
344 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
|
345 if (*p != insignficant) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
346 break; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
347 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
348 numsignificantbytes = n - i; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
349 /* 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
|
350 actually has 2 significant bytes. OTOH, 0xff0001 == |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
351 -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
|
352 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
|
353 check every case, bump it regardless. */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
354 if (is_signed && numsignificantbytes < n) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
355 ++numsignificantbytes; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
356 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
357 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
358 /* How many Python long digits do we need? We have |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
359 8*numsignificantbytes bits, and each Python long digit has SHIFT |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
360 bits, so it's the ceiling of the quotient. */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
361 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
362 if (ndigits > (size_t)INT_MAX) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
363 return PyErr_NoMemory(); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
364 v = _PyLong_New((int)ndigits); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
365 if (v == NULL) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
366 return NULL; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
367 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
368 /* 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
|
369 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
|
370 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
|
371 { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
372 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
|
373 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
|
374 twodigits accum = 0; /* sliding register */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
375 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
|
376 const unsigned char* p = pstartbyte; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
377 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
378 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
|
379 twodigits thisbyte = *p; |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
380 /* 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
|
381 if (is_signed) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
382 thisbyte = (0xff ^ thisbyte) + carry; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
383 carry = thisbyte >> 8; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
384 thisbyte &= 0xff; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
385 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
386 /* 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
|
387 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
|
388 so needs to be prepended to accum. */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
389 accum |= thisbyte << accumbits; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
390 accumbits += 8; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
391 if (accumbits >= SHIFT) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
392 /* There's enough to fill a Python digit. */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
393 assert(idigit < (int)ndigits); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
394 v->ob_digit[idigit] = (digit)(accum & MASK); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
395 ++idigit; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
396 accum >>= SHIFT; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
397 accumbits -= SHIFT; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
398 assert(accumbits < SHIFT); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
399 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
400 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
401 assert(accumbits < SHIFT); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
402 if (accumbits) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
403 assert(idigit < (int)ndigits); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
404 v->ob_digit[idigit] = (digit)accum; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
405 ++idigit; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
406 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
407 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
408 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
409 v->ob_size = is_signed ? -idigit : idigit; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
410 return (PyObject *)long_normalize(v); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
411 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
412 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
413 int |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
414 _PyLong_AsByteArray(PyLongObject* v, |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
415 unsigned char* bytes, size_t n, |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
416 int little_endian, int is_signed) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
417 { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
418 int i; /* index into v->ob_digit */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
419 int ndigits; /* |v->ob_size| */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
420 twodigits accum; /* sliding register */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
421 unsigned int accumbits; /* # bits in accum */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
422 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
423 twodigits carry; /* for computing 2's-comp */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
424 size_t j; /* # bytes filled */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
425 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
|
426 int pincr; /* direction to move p */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
427 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
428 assert(v != NULL && PyLong_Check(v)); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
429 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
430 if (v->ob_size < 0) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
431 ndigits = -(v->ob_size); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
432 if (!is_signed) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
433 PyErr_SetString(PyExc_TypeError, |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
434 "can't convert negative long to unsigned"); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
435 return -1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
436 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
437 do_twos_comp = 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
438 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
439 else { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
440 ndigits = v->ob_size; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
441 do_twos_comp = 0; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
442 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
443 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
444 if (little_endian) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
445 p = bytes; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
446 pincr = 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
447 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
448 else { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
449 p = bytes + n - 1; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
450 pincr = -1; |
|
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 |
|
18108
8cf72b187b3a
_PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents:
18107
diff
changeset
|
453 /* 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
|
454 It's crucial that every Python digit except for the MSD contribute |
|
8cf72b187b3a
_PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents:
18107
diff
changeset
|
455 exactly SHIFT bits to the total, so first assert that the long is |
|
8cf72b187b3a
_PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents:
18107
diff
changeset
|
456 normalized. */ |
|
8cf72b187b3a
_PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents:
18107
diff
changeset
|
457 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
|
458 j = 0; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
459 accum = 0; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
460 accumbits = 0; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
461 carry = do_twos_comp ? 1 : 0; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
462 for (i = 0; i < ndigits; ++i) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
463 twodigits thisdigit = v->ob_digit[i]; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
464 if (do_twos_comp) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
465 thisdigit = (thisdigit ^ MASK) + carry; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
466 carry = thisdigit >> SHIFT; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
467 thisdigit &= MASK; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
468 } |
|
18091
a001fcb76177
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
469 /* 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
|
470 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
|
471 prepended to accum. */ |
|
a001fcb76177
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
472 accum |= thisdigit << accumbits; |
|
18115
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
473 accumbits += SHIFT; |
|
18091
a001fcb76177
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
474 |
|
18115
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
475 /* 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
|
476 partly empty. */ |
|
18091
a001fcb76177
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
477 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
|
478 /* 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
|
479 * although for signed conversion we need later to |
|
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
480 * make sure at least one sign bit gets stored. |
|
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
481 * First shift conceptual sign bit to real sign bit. |
|
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
482 */ |
|
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
483 stwodigits s = (stwodigits)(thisdigit << |
|
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
484 (8*sizeof(stwodigits) - SHIFT)); |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18082
diff
changeset
|
485 unsigned int nsignbits = 0; |
|
18115
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
486 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) { |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18082
diff
changeset
|
487 ++nsignbits; |
|
18115
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
488 s <<= 1; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18082
diff
changeset
|
489 } |
|
18115
2ecaf9e21a04
_PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents:
18114
diff
changeset
|
490 accumbits -= nsignbits; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18082
diff
changeset
|
491 } |
|
18091
a001fcb76177
_PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
492 |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
493 /* 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
|
494 while (accumbits >= 8) { |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
495 if (j >= n) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
496 goto Overflow; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
497 ++j; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
498 *p = (unsigned char)(accum & 0xff); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
499 p += pincr; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
500 accumbits -= 8; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
501 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
|
502 } |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
503 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
504 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
505 /* Store the straggler (if any). */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
506 assert(accumbits < 8); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
507 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
|
508 if (accumbits > 0) { |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
509 if (j >= n) |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
510 goto Overflow; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
511 ++j; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
512 if (do_twos_comp) { |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
513 /* 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
|
514 (appropriately pretending that the long had an |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
515 infinite supply of sign bits). */ |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
516 accum |= (~(twodigits)0) << accumbits; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
517 } |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
518 *p = (unsigned char)(accum & 0xff); |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
519 p += pincr; |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
520 } |
|
18109
be2ddb25b698
_PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents:
18108
diff
changeset
|
521 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
|
522 /* 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
|
523 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
|
524 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
|
525 exists. */ |
|
be2ddb25b698
_PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents:
18108
diff
changeset
|
526 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
|
527 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
|
528 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
|
529 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
|
530 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
|
531 else |
|
be2ddb25b698
_PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents:
18108
diff
changeset
|
532 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
|
533 } |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
534 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
535 /* 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
|
536 { |
|
be2ddb25b698
_PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents:
18108
diff
changeset
|
537 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
|
538 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
|
539 *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
|
540 } |
|
18082
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
541 |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
542 return 0; |
|
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 Overflow: |
|
dd09f11fa40d
Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents:
15946
diff
changeset
|
545 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
|
546 return -1; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
547 |
|
18082
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 |
|
19671
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
550 double |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
551 _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
|
552 { |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
553 /* 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
|
554 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
|
555 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
|
556 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
|
557 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
|
558 |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
559 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
|
560 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
|
561 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
|
562 */ |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
563 #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
|
564 PyLongObject *v; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
565 double x; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
566 const double multiplier = (double)(1L << SHIFT); |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
567 int i, sign; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
568 int nbitsneeded; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
569 |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
570 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
|
571 PyErr_BadInternalCall(); |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
572 return -1; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
573 } |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
574 v = (PyLongObject *)vv; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
575 i = v->ob_size; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
576 sign = 1; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
577 if (i < 0) { |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
578 sign = -1; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
579 i = -(i); |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
580 } |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
581 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
|
582 *exponent = 0; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
583 return 0.0; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
584 } |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
585 --i; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
586 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
|
587 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
|
588 /* 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
|
589 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
|
590 --i; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
591 x = x * multiplier + (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
|
592 nbitsneeded -= SHIFT; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
593 } |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
594 /* There are i digits we didn't shift in. Pretending they're all |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
595 zeroes, the true value is x * 2**(i*SHIFT). */ |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
596 *exponent = i; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
597 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
|
598 return x * sign; |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
599 #undef NBITS_WANTED |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
600 } |
|
7df300d284c6
Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents:
19666
diff
changeset
|
601 |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4870
diff
changeset
|
602 /* Get a C double from a long int object. */ |
| 230 | 603 |
| 604 double | |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
605 PyLong_AsDouble(PyObject *vv) |
| 230 | 606 { |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
607 int e; |
| 230 | 608 double x; |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
609 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
610 if (vv == NULL || !PyLong_Check(vv)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
611 PyErr_BadInternalCall(); |
| 230 | 612 return -1; |
| 613 } | |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
614 x = _PyLong_AsScaledDouble(vv, &e); |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
615 if (x == -1.0 && PyErr_Occurred()) |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
616 return -1.0; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
617 if (e > INT_MAX / SHIFT) |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
618 goto overflow; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
619 errno = 0; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
620 x = ldexp(x, e * 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
|
621 if (Py_OVERFLOWED(x)) |
|
19674
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
622 goto overflow; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
623 return x; |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
624 |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
625 overflow: |
|
715244108faa
Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents:
19671
diff
changeset
|
626 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
|
627 "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
|
628 return -1.0; |
| 230 | 629 } |
| 630 | |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
631 /* 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
|
632 |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
633 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
634 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
|
635 { |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
636 #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
|
637 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
|
638 #else |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
639 |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
640 #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
|
641 # 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
|
642 #endif |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
643 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
644 # error "PyLong_FromVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
645 #endif |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
646 /* optimize null pointers */ |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
647 if (p == NULL) |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
648 return PyInt_FromLong(0); |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
649 return PyLong_FromLongLong((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
|
650 |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
651 #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
|
652 } |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
653 |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
654 /* 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
|
655 |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
656 void * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
657 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
|
658 { |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
659 /* 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
|
660 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
|
661 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
|
662 */ |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
663 #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
|
664 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
|
665 |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
666 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
|
667 x = PyInt_AS_LONG(vv); |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
668 else |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
669 x = PyLong_AsLong(vv); |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
670 #else |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
671 |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
672 #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
|
673 # 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
|
674 #endif |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
675 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
676 # error "PyLong_AsVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
677 #endif |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
678 LONG_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
|
679 |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
680 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
|
681 x = PyInt_AS_LONG(vv); |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
682 else |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
683 x = PyLong_AsLongLong(vv); |
|
18134
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
684 |
|
eff8da7c3dcb
PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents:
18121
diff
changeset
|
685 #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
|
686 |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
687 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
|
688 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
|
689 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
|
690 } |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
9094
diff
changeset
|
691 |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
692 #ifdef HAVE_LONG_LONG |
| 18093 | 693 |
| 694 /* Initial LONG_LONG support by Chris Herborth (chrish@qnx.com), later | |
| 695 * 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
|
696 */ |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
697 |
|
18121
166b7b365851
Change IS_LITTLE_ENDIAN macro -- a little faster now.
Tim Peters <tim.peters@gmail.com>
parents:
18115
diff
changeset
|
698 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one |
| 18093 | 699 |
| 700 /* Create a new long int object from a C 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
|
701 |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
702 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
703 PyLong_FromLongLong(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
|
704 { |
| 18093 | 705 LONG_LONG bytes = ival; |
| 706 int one = 1; | |
| 707 return _PyLong_FromByteArray( | |
| 708 (unsigned char *)&bytes, | |
| 709 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
|
710 } |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
711 |
| 18093 | 712 /* Create a new long int object from a C unsigned LONG_LONG int. */ |
| 713 | |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
714 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
715 PyLong_FromUnsignedLongLong(unsigned 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
|
716 { |
| 18093 | 717 unsigned LONG_LONG bytes = ival; |
| 718 int one = 1; | |
| 719 return _PyLong_FromByteArray( | |
| 720 (unsigned char *)&bytes, | |
| 721 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
|
722 } |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
723 |
|
9044
d437b6683e24
Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents:
8933
diff
changeset
|
724 /* Get a C LONG_LONG int from a long int object. |
| 18093 | 725 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
|
726 |
|
9044
d437b6683e24
Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents:
8933
diff
changeset
|
727 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
|
728 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
|
729 { |
| 18093 | 730 LONG_LONG bytes; |
| 731 int one = 1; | |
| 732 int res; | |
| 733 | |
|
20182
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
734 if (vv == NULL) { |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
735 PyErr_BadInternalCall(); |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
736 return -1; |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
737 } |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
738 if (!PyLong_Check(vv)) { |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
739 if (PyInt_Check(vv)) |
|
6d73b54d9205
SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents:
19976
diff
changeset
|
740 return (LONG_LONG)PyInt_AsLong(vv); |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
741 PyErr_BadInternalCall(); |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
742 return -1; |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
743 } |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
744 |
| 18093 | 745 res = _PyLong_AsByteArray( |
| 746 (PyLongObject *)vv, (unsigned char *)&bytes, | |
| 747 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
|
748 |
|
22204
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
749 /* Plan 9 can't handle LONG_LONG in ? : expressions */ |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
750 if (res < 0) |
|
23248
97650edf58bb
Clarify return value of PyLong_AsLongLong().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22974
diff
changeset
|
751 return (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
|
752 else |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
753 return bytes; |
| 18093 | 754 } |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
755 |
| 18093 | 756 /* Get a C unsigned LONG_LONG int from a long int object. |
| 757 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
|
758 |
|
9044
d437b6683e24
Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents:
8933
diff
changeset
|
759 unsigned 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
|
760 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
|
761 { |
| 18093 | 762 unsigned LONG_LONG bytes; |
| 763 int one = 1; | |
| 764 int res; | |
| 765 | |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
766 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
|
767 PyErr_BadInternalCall(); |
| 18093 | 768 return -1; |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
769 } |
|
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
770 |
| 18093 | 771 res = _PyLong_AsByteArray( |
| 772 (PyLongObject *)vv, (unsigned char *)&bytes, | |
| 773 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
|
774 |
|
22204
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
775 /* Plan 9 can't handle LONG_LONG in ? : expressions */ |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
776 if (res < 0) |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
777 return (unsigned LONG_LONG)res; |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
778 else |
|
928c1282e5e2
Patch #494047: removes 64-bit ?: to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents:
22147
diff
changeset
|
779 return bytes; |
| 18093 | 780 } |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
781 |
| 18093 | 782 #undef IS_LITTLE_ENDIAN |
| 783 | |
|
8822
bd47ee068b83
Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents:
8808
diff
changeset
|
784 #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
|
785 |
|
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
|
786 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
787 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
|
788 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
|
789 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
|
790 *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
|
791 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
|
792 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
793 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
|
794 *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
|
795 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
796 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
|
797 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
|
798 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
799 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
|
800 *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
|
801 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
|
802 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
803 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
|
804 *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
|
805 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
806 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
|
807 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
|
808 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
|
809 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
810 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
|
811 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
812 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
813 #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
|
814 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
|
815 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
|
816 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
|
817 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
818 |
|
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
|
819 /* 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
|
820 * 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
|
821 * 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
|
822 */ |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
823 static digit |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
824 v_iadd(digit *x, int m, digit *y, int 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
|
825 { |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
826 int i; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
827 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
|
828 |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
829 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
|
830 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
|
831 carry += x[i] + y[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
|
832 x[i] = carry & MASK; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
833 carry >>= SHIFT; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
834 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
|
835 } |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
836 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
|
837 carry += x[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
|
838 x[i] = carry & MASK; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
839 carry >>= SHIFT; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
840 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
|
841 } |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
842 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
|
843 } |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
844 |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
845 /* 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
|
846 * 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
|
847 * 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
|
848 */ |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
849 static digit |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
850 v_isub(digit *x, int m, digit *y, int 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
|
851 { |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
852 int i; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
853 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
|
854 |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
855 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
|
856 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
|
857 borrow = x[i] - y[i] - 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
|
858 x[i] = borrow & MASK; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
859 borrow >>= SHIFT; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
860 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
|
861 } |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
862 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
|
863 borrow = x[i] - 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
|
864 x[i] = borrow & MASK; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
865 borrow >>= SHIFT; |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
866 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
|
867 } |
|
c248bee52dc6
Introduced helper functions v_iadd and v_isub, for in-place digit-vector
Tim Peters <tim.peters@gmail.com>
parents:
24747
diff
changeset
|
868 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
|
869 } |
|
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
|
870 |
| 230 | 871 /* Multiply by a single digit, ignoring the sign. */ |
| 872 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
873 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
|
874 mul1(PyLongObject *a, wdigit n) |
| 230 | 875 { |
| 876 return muladd1(a, n, (digit)0); | |
| 877 } | |
| 878 | |
| 879 /* Multiply by a single digit and add a single digit, ignoring the sign. */ | |
| 880 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
881 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
|
882 muladd1(PyLongObject *a, wdigit n, wdigit extra) |
| 230 | 883 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
884 int size_a = ABS(a->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
885 PyLongObject *z = _PyLong_New(size_a+1); |
| 230 | 886 twodigits carry = extra; |
| 887 int i; | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
888 |
| 230 | 889 if (z == NULL) |
| 890 return NULL; | |
| 891 for (i = 0; i < size_a; ++i) { | |
| 892 carry += (twodigits)a->ob_digit[i] * n; | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
893 z->ob_digit[i] = (digit) (carry & MASK); |
| 230 | 894 carry >>= SHIFT; |
| 895 } | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
896 z->ob_digit[i] = (digit) carry; |
| 230 | 897 return long_normalize(z); |
| 898 } | |
| 899 | |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
900 /* 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
|
901 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
|
902 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
903 long_format, but that should be done with great care since longs are |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
904 immutable. */ |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
905 |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
906 static digit |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
907 inplace_divrem1(digit *pout, digit *pin, int size, digit n) |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
908 { |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
909 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
|
910 |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
911 assert(n > 0 && n <= MASK); |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
912 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
|
913 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
|
914 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
|
915 digit hi; |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
916 rem = (rem << SHIFT) + *--pin; |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
917 *--pout = hi = (digit)(rem / n); |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
918 rem -= hi * n; |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
919 } |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
920 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
|
921 } |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
922 |
| 230 | 923 /* Divide a long integer by a digit, returning both the quotient |
| 924 (as function result) and the remainder (through *prem). | |
| 925 The sign of a is ignored; n should not be zero. */ | |
| 926 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
927 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
|
928 divrem1(PyLongObject *a, digit n, digit *prem) |
| 230 | 929 { |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
930 const int size = ABS(a->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
931 PyLongObject *z; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
932 |
| 230 | 933 assert(n > 0 && n <= MASK); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
934 z = _PyLong_New(size); |
| 230 | 935 if (z == NULL) |
| 936 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
|
937 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
| 230 | 938 return long_normalize(z); |
| 939 } | |
| 940 | |
| 941 /* Convert a long int object to a string, using a given conversion base. | |
|
406
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
942 Return a string object. |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
943 If base is 8 or 16, add the proper prefix '0' or '0x'. */ |
| 230 | 944 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
945 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
|
946 long_format(PyObject *aa, int base, int addL) |
| 230 | 947 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
948 register PyLongObject *a = (PyLongObject *)aa; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
949 PyStringObject *str; |
| 230 | 950 int i; |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
951 const int size_a = ABS(a->ob_size); |
| 230 | 952 char *p; |
| 953 int bits; | |
| 954 char sign = '\0'; | |
| 613 | 955 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
956 if (a == NULL || !PyLong_Check(a)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
957 PyErr_BadInternalCall(); |
| 613 | 958 return NULL; |
| 959 } | |
| 230 | 960 assert(base >= 2 && base <= 36); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
961 |
| 230 | 962 /* Compute a rough upper bound for the length of the string */ |
| 963 i = base; | |
| 964 bits = 0; | |
| 965 while (i > 1) { | |
| 966 ++bits; | |
| 967 i >>= 1; | |
| 968 } | |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
969 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
970 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); |
| 230 | 971 if (str == NULL) |
| 972 return NULL; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
973 p = PyString_AS_STRING(str) + i; |
| 230 | 974 *p = '\0'; |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
975 if (addL) |
|
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
976 *--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
|
977 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
|
978 sign = '-'; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
979 |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
980 if (a->ob_size == 0) { |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
981 *--p = '0'; |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
982 } |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
983 else if ((base & (base - 1)) == 0) { |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
984 /* 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
|
985 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
|
986 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
|
987 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
|
988 i = base; |
|
13423
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
989 while ((i >>= 1) > 1) |
|
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
990 ++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
|
991 |
|
0857fb489fa3
long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents:
18594
diff
changeset
|
992 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
|
993 accum |= (twodigits)a->ob_digit[i] << accumbits; |
|
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
|
994 accumbits += SHIFT; |
|
0857fb489fa3
long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents:
18594
diff
changeset
|
995 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
|
996 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
|
997 char cdigit = (char)(accum & (base - 1)); |
|
62741b25ed65
Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents:
20868
diff
changeset
|
998 cdigit += (cdigit < 10) ? '0' : 'A'-10; |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
999 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
|
1000 *--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
|
1001 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
|
1002 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
|
1003 } 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
|
1004 accum > 0); |
| 230 | 1005 } |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1006 } |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1007 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
|
1008 /* 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
|
1009 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
|
1010 fits in a digit. */ |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1011 int size = size_a; |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1012 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
|
1013 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
|
1014 /* 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
|
1015 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
|
1016 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
|
1017 for (;;) { |
|
b287f2927db3
long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents:
18500
diff
changeset
|
1018 unsigned long newpow = powbase * (unsigned long)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
|
1019 if (newpow >> SHIFT) /* doesn't fit in a digit */ |
|
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
|
1020 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
|
1021 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
|
1022 ++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
|
1023 } |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1024 |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1025 /* 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
|
1026 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
|
1027 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
|
1028 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
|
1029 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
|
1030 } |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1031 |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1032 /* Repeatedly divide by powbase. */ |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1033 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
|
1034 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
|
1035 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
|
1036 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
|
1037 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
|
1038 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
|
1039 --size; |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1040 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
|
1041 Py_DECREF(scratch); |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1042 Py_DECREF(str); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1043 return NULL; |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1044 }) |
|
18594
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1045 |
|
19250f9181dc
divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents:
18593
diff
changeset
|
1046 /* Break rem into digits. */ |
|
18593
9a373a143a13
long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents:
18536
diff
changeset
|
1047 assert(ntostore > 0); |
|
9a373a143a13
long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents:
18536
diff
changeset
|
1048 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
|
1049 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
|
1050 char c = (char)(rem - nextrem * 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
|
1051 assert(p > PyString_AS_STRING(str)); |
|
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
|
1052 c += (c < 10) ? '0' : 'A'-10; |
|
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
|
1053 *--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
|
1054 rem = nextrem; |
|
18593
9a373a143a13
long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents:
18536
diff
changeset
|
1055 --ntostore; |
|
9a373a143a13
long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents:
18536
diff
changeset
|
1056 /* 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
|
1057 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
|
1058 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
|
1059 } 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
|
1060 } 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
|
1061 Py_DECREF(scratch); |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1062 } |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1063 |
|
958
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1064 if (base == 8) { |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1065 if (size_a != 0) |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1066 *--p = '0'; |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
1067 } |
|
406
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
1068 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
|
1069 *--p = 'x'; |
|
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
1070 *--p = '0'; |
|
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
1071 } |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1072 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
|
1073 *--p = '#'; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1074 *--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
|
1075 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
|
1076 *--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
|
1077 } |
| 230 | 1078 if (sign) |
| 1079 *--p = sign; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1080 if (p != PyString_AS_STRING(str)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1081 char *q = PyString_AS_STRING(str); |
| 230 | 1082 assert(p > q); |
| 1083 do { | |
| 1084 } while ((*q++ = *p++) != '\0'); | |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1085 q--; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1086 _PyString_Resize((PyObject **)&str, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1087 (int) (q - PyString_AS_STRING(str))); |
| 230 | 1088 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1089 return (PyObject *)str; |
| 230 | 1090 } |
| 1091 | |
|
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
|
1092 /* *str points to the first digit in a string of base base digits. 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
|
1093 * 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
|
1094 * 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
|
1095 * 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
|
1096 * 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
|
1097 */ |
|
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
|
1098 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
|
1099 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
|
1100 { |
|
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
|
1101 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
|
1102 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
|
1103 int 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
|
1104 int 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
|
1105 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
|
1106 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
|
1107 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
|
1108 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
|
1109 |
|
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
|
1110 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
|
1111 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
|
1112 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
|
1113 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
|
1114 /* 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
|
1115 n = 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
|
1116 for (;;) { |
|
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
|
1117 int k = -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
|
1118 char ch = *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
|
1119 |
|
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
|
1120 if (ch <= '9') |
|
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
|
1121 k = ch - '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
|
1122 else if (ch >= 'a') |
|
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
|
1123 k = ch - 'a' + 10; |
|
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
|
1124 else if (ch >= 'A') |
|
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
|
1125 k = ch - 'A' + 10; |
|
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
|
1126 if (k < 0 || k >= 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
|
1127 break; |
|
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
|
1128 ++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
|
1129 } |
|
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
|
1130 *str = p; |
|
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
|
1131 n = (p - start) * bits_per_char; |
|
8e60ec88ecb8
long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents:
27588
diff
changeset
|
1132 if (n / bits_per_char != p - start) { |
|
8e60ec88ecb8
long_from_binary_base(): Sped this a little by computing the # of bits
Tim Peters <tim.peters@gmail.com>
parents:
27588
diff
changeset
|
1133 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
|
1134 "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
|
1135 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
|
1136 } |
|
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
|
1137 /* n <- # of Python digits needed, = ceiling(n/SHIFT). */ |
|
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
|
1138 n = (n + SHIFT - 1) / SHIFT; |
|
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
|
1139 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
|
1140 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
|
1141 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
|
1142 /* 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
|
1143 * 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
|
1144 */ |
|
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
|
1145 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
|
1146 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
|
1147 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
|
1148 while (--p >= start) { |
|
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
|
1149 unsigned char ch = (unsigned char)*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
|
1150 digit k; |
|
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
|
1151 |
|
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
|
1152 if (ch <= '9') |
|
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
|
1153 k = ch - '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
|
1154 else if (ch >= 'a') |
|
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
|
1155 k = ch - 'a' + 10; |
|
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
|
1156 else { |
|
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
|
1157 assert(ch >= 'A'); |
|
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
|
1158 k = ch - 'A' + 10; |
|
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
|
1159 } |
|
27588
3eb10a14ba00
Tightened a too-generous assert.
Tim Peters <tim.peters@gmail.com>
parents:
27587
diff
changeset
|
1160 assert(k < 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
|
1161 accum |= k << 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
|
1162 bits_in_accum += 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
|
1163 if (bits_in_accum >= SHIFT) { |
|
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
|
1164 *pdigit++ = (digit)(accum & MASK); |
|
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
|
1165 assert(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
|
1166 accum >>= SHIFT; |
|
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
|
1167 bits_in_accum -= SHIFT; |
|
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
|
1168 assert(bits_in_accum < SHIFT); |
|
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
|
1169 } |
|
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
|
1170 } |
|
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
|
1171 if (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
|
1172 assert(bits_in_accum <= SHIFT); |
|
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
|
1173 *pdigit++ = (digit)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
|
1174 assert(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
|
1175 } |
|
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
|
1176 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
|
1177 *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
|
1178 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
|
1179 } |
|
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
|
1180 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1181 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1182 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
|
1183 { |
| 230 | 1184 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
|
1185 char *start, *orig_str = str; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1186 PyLongObject *z; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1187 |
|
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
|
1188 if ((base != 0 && base < 2) || base > 36) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1189 PyErr_SetString(PyExc_ValueError, |
| 15318 | 1190 "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
|
1191 return NULL; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1192 } |
|
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
|
1193 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
|
1194 str++; |
| 230 | 1195 if (*str == '+') |
| 1196 ++str; | |
| 1197 else if (*str == '-') { | |
| 1198 ++str; | |
| 1199 sign = -1; | |
| 1200 } | |
|
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
|
1201 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
|
1202 str++; |
| 230 | 1203 if (base == 0) { |
| 1204 if (str[0] != '0') | |
| 1205 base = 10; | |
| 1206 else if (str[1] == 'x' || str[1] == 'X') | |
| 1207 base = 16; | |
| 1208 else | |
| 1209 base = 8; | |
| 1210 } | |
| 1211 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) | |
| 1212 str += 2; | |
|
8533
e5d06a602469
Add check in long-to-int conversion for at least one digit.
Guido van Rossum <guido@python.org>
parents:
8425
diff
changeset
|
1213 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
|
1214 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
|
1215 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
|
1216 else { |
|
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
|
1217 z = _PyLong_New(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
|
1218 for ( ; z != NULL; ++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
|
1219 int k = -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
|
1220 PyLongObject *temp; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1221 |
|
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
|
1222 if (*str <= '9') |
|
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
|
1223 k = *str - '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
|
1224 else if (*str >= 'a') |
|
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
|
1225 k = *str - 'a' + 10; |
|
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
|
1226 else if (*str >= 'A') |
|
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
|
1227 k = *str - 'A' + 10; |
|
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
|
1228 if (k < 0 || k >= 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
|
1229 break; |
|
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
|
1230 temp = muladd1(z, (digit)base, (digit)k); |
|
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
|
1231 Py_DECREF(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
|
1232 z = temp; |
|
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
|
1233 } |
| 230 | 1234 } |
|
8808
78785985a2ab
Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents:
8533
diff
changeset
|
1235 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
|
1236 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
|
1237 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
|
1238 goto onError; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1239 if (sign < 0 && 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
|
1240 z->ob_size = -(z->ob_size); |
|
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
|
1241 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
|
1242 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
|
1243 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
|
1244 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
|
1245 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
|
1246 goto onError; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1247 if (pend) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1248 *pend = str; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1249 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
|
1250 |
|
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
|
1251 onError: |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1252 PyErr_Format(PyExc_ValueError, |
|
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
|
1253 "invalid literal for long(): %.200s", orig_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
|
1254 Py_XDECREF(z); |
|
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
|
1255 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
|
1256 } |
|
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
|
1257 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
1258 #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
|
1259 PyObject * |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1260 PyLong_FromUnicode(Py_UNICODE *u, int 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
|
1261 { |
|
26081
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1262 PyObject *result; |
|
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1263 char *buffer = 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
|
1264 |
|
26081
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1265 if (buffer == NULL) |
|
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1266 return NULL; |
|
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1267 |
|
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1268 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
|
1269 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
|
1270 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
|
1271 } |
|
26081
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1272 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
|
1273 PyMem_FREE(buffer); |
|
999accfa91c5
Make int("...") return a long if an int would overflow.
Walter Dörwald <walter@livinglogic.de>
parents:
25108
diff
changeset
|
1274 return result; |
| 230 | 1275 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
1276 #endif |
| 230 | 1277 |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1278 /* forward */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1279 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
|
1280 (PyLongObject *, PyLongObject *, PyLongObject **); |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1281 static PyObject *long_pos(PyLongObject *); |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1282 static int long_divrem(PyLongObject *, PyLongObject *, |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
1283 PyLongObject **, PyLongObject **); |
| 230 | 1284 |
| 1285 /* Long division with remainder, top-level routine */ | |
| 1286 | |
| 613 | 1287 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
|
1288 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
|
1289 PyLongObject **pdiv, PyLongObject **prem) |
| 230 | 1290 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1291 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1292 PyLongObject *z; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1293 |
| 230 | 1294 if (size_b == 0) { |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
1295 PyErr_SetString(PyExc_ZeroDivisionError, |
| 15318 | 1296 "long division or modulo by zero"); |
| 613 | 1297 return -1; |
| 230 | 1298 } |
| 1299 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
|
1300 (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
|
1301 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { |
| 230 | 1302 /* |a| < |b|. */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1303 *pdiv = _PyLong_New(0); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1304 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1305 *prem = (PyLongObject *) a; |
| 613 | 1306 return 0; |
| 230 | 1307 } |
| 1308 if (size_b == 1) { | |
| 1309 digit rem = 0; | |
| 1310 z = divrem1(a, b->ob_digit[0], &rem); | |
| 613 | 1311 if (z == NULL) |
| 1312 return -1; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1313 *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
| 230 | 1314 } |
| 613 | 1315 else { |
| 230 | 1316 z = x_divrem(a, b, prem); |
| 613 | 1317 if (z == NULL) |
| 1318 return -1; | |
| 1319 } | |
| 230 | 1320 /* Set the signs. |
| 1321 The quotient z has the sign of a*b; | |
| 1322 the remainder r has the sign of a, | |
| 1323 so a = b*z + r. */ | |
| 613 | 1324 if ((a->ob_size < 0) != (b->ob_size < 0)) |
| 1325 z->ob_size = -(z->ob_size); | |
| 1326 if (a->ob_size < 0 && (*prem)->ob_size != 0) | |
| 1327 (*prem)->ob_size = -((*prem)->ob_size); | |
| 1328 *pdiv = z; | |
| 1329 return 0; | |
| 230 | 1330 } |
| 1331 | |
| 242 | 1332 /* Unsigned long division with remainder -- the algorithm */ |
| 230 | 1333 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1334 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
|
1335 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
| 230 | 1336 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1337 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
1338 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1339 PyLongObject *v = mul1(v1, d); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1340 PyLongObject *w = mul1(w1, d); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1341 PyLongObject *a; |
| 230 | 1342 int j, k; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1343 |
| 230 | 1344 if (v == NULL || w == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1345 Py_XDECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1346 Py_XDECREF(w); |
| 230 | 1347 return NULL; |
| 1348 } | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1349 |
| 230 | 1350 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ |
| 242 | 1351 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */ |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1352 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1353 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1354 size_v = ABS(v->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1355 a = _PyLong_New(size_v - size_w + 1); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1356 |
| 230 | 1357 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { |
| 1358 digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; | |
| 1359 twodigits q; | |
| 242 | 1360 stwodigits carry = 0; |
| 230 | 1361 int i; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1362 |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1363 SIGCHECK({ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1364 Py_DECREF(a); |
| 230 | 1365 a = NULL; |
| 1366 break; | |
| 242 | 1367 }) |
| 230 | 1368 if (vj == w->ob_digit[size_w-1]) |
| 1369 q = MASK; | |
| 1370 else | |
| 1371 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) / | |
| 1372 w->ob_digit[size_w-1]; | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1373 |
| 230 | 1374 while (w->ob_digit[size_w-2]*q > |
| 1375 (( | |
| 1376 ((twodigits)vj << SHIFT) | |
| 1377 + v->ob_digit[j-1] | |
| 1378 - q*w->ob_digit[size_w-1] | |
| 1379 ) << SHIFT) | |
| 1380 + v->ob_digit[j-2]) | |
| 1381 --q; | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1382 |
| 230 | 1383 for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 1384 twodigits z = w->ob_digit[i] * q; | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
1385 digit zz = (digit) (z >> SHIFT); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1386 carry += v->ob_digit[i+k] - z |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1387 + ((twodigits)zz << SHIFT); |
| 230 | 1388 v->ob_digit[i+k] = carry & MASK; |
|
13423
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1389 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, |
|
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1390 carry, SHIFT); |
|
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1391 carry -= zz; |
| 230 | 1392 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1393 |
| 230 | 1394 if (i+k < size_v) { |
| 1395 carry += v->ob_digit[i+k]; | |
| 1396 v->ob_digit[i+k] = 0; | |
| 1397 } | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1398 |
| 230 | 1399 if (carry == 0) |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
1400 a->ob_digit[k] = (digit) q; |
| 230 | 1401 else { |
| 1402 assert(carry == -1); | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5199
diff
changeset
|
1403 a->ob_digit[k] = (digit) q-1; |
| 230 | 1404 carry = 0; |
| 1405 for (i = 0; i < size_w && i+k < size_v; ++i) { | |
| 1406 carry += v->ob_digit[i+k] + w->ob_digit[i]; | |
| 1407 v->ob_digit[i+k] = carry & MASK; | |
|
13423
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1408 carry = Py_ARITHMETIC_RIGHT_SHIFT( |
|
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1409 BASE_TWODIGITS_TYPE, |
|
a87803eb9dfb
Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents:
13420
diff
changeset
|
1410 carry, SHIFT); |
| 230 | 1411 } |
| 1412 } | |
| 1413 } /* for j, k */ | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1414 |
|
2111
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1415 if (a == NULL) |
|
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1416 *prem = NULL; |
|
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1417 else { |
| 230 | 1418 a = long_normalize(a); |
| 613 | 1419 *prem = divrem1(v, d, &d); |
| 1420 /* d receives the (unused) remainder */ | |
| 1421 if (*prem == NULL) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1422 Py_DECREF(a); |
| 613 | 1423 a = NULL; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1424 } |
| 230 | 1425 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1426 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1427 Py_DECREF(w); |
| 230 | 1428 return a; |
| 1429 } | |
| 1430 | |
| 1431 /* Methods */ | |
| 1432 | |
| 1433 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
|
1434 long_dealloc(PyObject *v) |
| 230 | 1435 { |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20182
diff
changeset
|
1436 v->ob_type->tp_free(v); |
| 230 | 1437 } |
| 1438 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1439 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
|
1440 long_repr(PyObject *v) |
| 230 | 1441 { |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
1442 return long_format(v, 10, 1); |
|
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
1443 } |
|
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
1444 |
|
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
1445 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
|
1446 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
|
1447 { |
|
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
1448 return long_format(v, 10, 0); |
| 230 | 1449 } |
| 1450 | |
| 1451 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
|
1452 long_compare(PyLongObject *a, PyLongObject *b) |
| 230 | 1453 { |
| 1454 int sign; | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1455 |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1456 if (a->ob_size != b->ob_size) { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1457 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 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
|
1458 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
|
1459 else |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1460 sign = a->ob_size - b->ob_size; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1461 } |
| 230 | 1462 else { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1463 int i = ABS(a->ob_size); |
| 230 | 1464 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 1465 ; | |
| 1466 if (i < 0) | |
| 1467 sign = 0; | |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1468 else { |
| 230 | 1469 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1470 if (a->ob_size < 0) |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1471 sign = -sign; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1472 } |
| 230 | 1473 } |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1474 return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
| 230 | 1475 } |
| 1476 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1477 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
|
1478 long_hash(PyLongObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1479 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1480 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1481 int i, sign; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1482 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1483 /* 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
|
1484 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
|
1485 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
|
1486 i = v->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1487 sign = 1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1488 x = 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1489 if (i < 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1490 sign = -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1491 i = -(i); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1492 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1493 while (--i >= 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1494 /* Force a 32-bit circular shift */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1495 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1496 x += v->ob_digit[i]; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1497 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1498 x = x * sign; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1499 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1500 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1501 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1502 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1503 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1504 |
| 230 | 1505 /* Add the absolute values of two long integers. */ |
| 1506 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1507 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
|
1508 x_add(PyLongObject *a, PyLongObject *b) |
| 230 | 1509 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1510 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1511 PyLongObject *z; |
| 230 | 1512 int i; |
| 1513 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
|
1514 |
| 230 | 1515 /* Ensure a is the larger of the two: */ |
| 1516 if (size_a < size_b) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1517 { PyLongObject *temp = a; a = b; b = temp; } |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1518 { int size_temp = size_a; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1519 size_a = size_b; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1520 size_b = size_temp; } |
| 230 | 1521 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1522 z = _PyLong_New(size_a+1); |
| 230 | 1523 if (z == NULL) |
| 1524 return NULL; | |
| 1525 for (i = 0; i < size_b; ++i) { | |
| 1526 carry += a->ob_digit[i] + b->ob_digit[i]; | |
| 1527 z->ob_digit[i] = carry & MASK; | |
| 1528 carry >>= SHIFT; | |
| 1529 } | |
| 1530 for (; i < size_a; ++i) { | |
| 1531 carry += a->ob_digit[i]; | |
| 1532 z->ob_digit[i] = carry & MASK; | |
| 1533 carry >>= SHIFT; | |
| 1534 } | |
| 1535 z->ob_digit[i] = carry; | |
| 1536 return long_normalize(z); | |
| 1537 } | |
| 1538 | |
| 1539 /* Subtract the absolute values of two integers. */ | |
| 1540 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1541 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
|
1542 x_sub(PyLongObject *a, PyLongObject *b) |
| 230 | 1543 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1544 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1545 PyLongObject *z; |
| 230 | 1546 int i; |
| 1547 int sign = 1; | |
| 1548 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
|
1549 |
| 230 | 1550 /* Ensure a is the larger of the two: */ |
| 1551 if (size_a < size_b) { | |
| 1552 sign = -1; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1553 { PyLongObject *temp = a; a = b; b = temp; } |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1554 { int size_temp = size_a; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1555 size_a = size_b; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1556 size_b = size_temp; } |
| 230 | 1557 } |
| 1558 else if (size_a == size_b) { | |
| 1559 /* Find highest digit where a and b differ: */ | |
| 1560 i = size_a; | |
| 1561 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | |
| 1562 ; | |
| 1563 if (i < 0) | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1564 return _PyLong_New(0); |
| 230 | 1565 if (a->ob_digit[i] < b->ob_digit[i]) { |
| 1566 sign = -1; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1567 { PyLongObject *temp = a; a = b; b = temp; } |
| 230 | 1568 } |
| 1569 size_a = size_b = i+1; | |
| 1570 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1571 z = _PyLong_New(size_a); |
| 230 | 1572 if (z == NULL) |
| 1573 return NULL; | |
| 1574 for (i = 0; i < size_b; ++i) { | |
| 1575 /* The following assumes unsigned arithmetic | |
| 1576 works module 2**N for some N>SHIFT. */ | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1577 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 230 | 1578 z->ob_digit[i] = borrow & MASK; |
| 1579 borrow >>= SHIFT; | |
| 1580 borrow &= 1; /* Keep only one sign bit */ | |
| 1581 } | |
| 1582 for (; i < size_a; ++i) { | |
| 1583 borrow = a->ob_digit[i] - borrow; | |
| 1584 z->ob_digit[i] = borrow & MASK; | |
| 1585 borrow >>= 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
|
1586 borrow &= 1; /* Keep only one sign bit */ |
| 230 | 1587 } |
| 1588 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
|
1589 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
|
1590 z->ob_size = -(z->ob_size); |
| 230 | 1591 return long_normalize(z); |
| 1592 } | |
| 1593 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1594 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
|
1595 long_add(PyLongObject *v, PyLongObject *w) |
| 230 | 1596 { |
|
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
|
1597 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
|
1598 |
|
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
|
1599 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
|
1600 |
| 230 | 1601 if (a->ob_size < 0) { |
| 1602 if (b->ob_size < 0) { | |
| 1603 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
|
1604 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
|
1605 z->ob_size = -(z->ob_size); |
| 230 | 1606 } |
| 1607 else | |
| 1608 z = x_sub(b, a); | |
| 1609 } | |
| 1610 else { | |
| 1611 if (b->ob_size < 0) | |
| 1612 z = x_sub(a, b); | |
| 1613 else | |
| 1614 z = x_add(a, b); | |
| 1615 } | |
|
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
|
1616 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
|
1617 Py_DECREF(b); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1618 return (PyObject *)z; |
| 230 | 1619 } |
| 1620 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1621 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
|
1622 long_sub(PyLongObject *v, PyLongObject *w) |
| 230 | 1623 { |
|
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
|
1624 PyLongObject *a, *b, *z; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1625 |
|
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
|
1626 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
|
1627 |
| 230 | 1628 if (a->ob_size < 0) { |
| 1629 if (b->ob_size < 0) | |
| 1630 z = x_sub(a, b); | |
| 1631 else | |
| 1632 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
|
1633 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
|
1634 z->ob_size = -(z->ob_size); |
| 230 | 1635 } |
| 1636 else { | |
| 1637 if (b->ob_size < 0) | |
| 1638 z = x_add(a, b); | |
| 1639 else | |
| 1640 z = x_sub(a, b); | |
| 1641 } | |
|
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
|
1642 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
|
1643 Py_DECREF(b); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1644 return (PyObject *)z; |
| 230 | 1645 } |
| 1646 | |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1647 /* 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
|
1648 * 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
|
1649 */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1650 static PyLongObject * |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1651 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
|
1652 { |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1653 PyLongObject *z; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1654 int size_a = ABS(a->ob_size); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1655 int size_b = ABS(b->ob_size); |
| 230 | 1656 int i; |
|
19925
df409844b659
A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents:
19897
diff
changeset
|
1657 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1658 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
|
1659 if (z == NULL) |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1660 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
|
1661 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1662 memset(z->ob_digit, 0, z->ob_size * sizeof(digit)); |
| 230 | 1663 for (i = 0; i < size_a; ++i) { |
| 1664 twodigits carry = 0; | |
| 1665 twodigits f = a->ob_digit[i]; | |
| 1666 int j; | |
|
24760
dcfabe65c22e
x_mul(): Made life easier for C optimizers in the "grade school"
Tim Peters <tim.peters@gmail.com>
parents:
24759
diff
changeset
|
1667 digit *pz = z->ob_digit + i; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1668 |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1669 SIGCHECK({ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1670 Py_DECREF(z); |
| 230 | 1671 return NULL; |
| 242 | 1672 }) |
| 230 | 1673 for (j = 0; j < size_b; ++j) { |
|
24760
dcfabe65c22e
x_mul(): Made life easier for C optimizers in the "grade school"
Tim Peters <tim.peters@gmail.com>
parents:
24759
diff
changeset
|
1674 carry += *pz + b->ob_digit[j] * f; |
|
dcfabe65c22e
x_mul(): Made life easier for C optimizers in the "grade school"
Tim Peters <tim.peters@gmail.com>
parents:
24759
diff
changeset
|
1675 *pz++ = (digit) (carry & MASK); |
| 230 | 1676 carry >>= SHIFT; |
| 1677 } | |
| 1678 for (; carry != 0; ++j) { | |
| 1679 assert(i+j < z->ob_size); | |
|
24760
dcfabe65c22e
x_mul(): Made life easier for C optimizers in the "grade school"
Tim Peters <tim.peters@gmail.com>
parents:
24759
diff
changeset
|
1680 carry += *pz; |
|
dcfabe65c22e
x_mul(): Made life easier for C optimizers in the "grade school"
Tim Peters <tim.peters@gmail.com>
parents:
24759
diff
changeset
|
1681 *pz++ = (digit) (carry & MASK); |
| 230 | 1682 carry >>= SHIFT; |
| 1683 } | |
| 1684 } | |
|
24751
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1685 return long_normalize(z); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1686 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1687 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1688 /* 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
|
1689 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
|
1690 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
|
1691 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
|
1692 the return values are >= 0. |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1693 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
|
1694 */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1695 static int |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1696 kmul_split(PyLongObject *n, int size, PyLongObject **high, PyLongObject **low) |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1697 { |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1698 PyLongObject *hi, *lo; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1699 int size_lo, size_hi; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1700 const int size_n = ABS(n->ob_size); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1701 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1702 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
|
1703 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
|
1704 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1705 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
|
1706 return -1; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1707 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
|
1708 Py_DECREF(hi); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1709 return -1; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1710 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1711 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1712 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
|
1713 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
|
1714 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1715 *high = long_normalize(hi); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1716 *low = long_normalize(lo); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1717 return 0; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1718 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1719 |
|
24773
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1720 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
|
1721 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1722 /* 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
|
1723 * 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
|
1724 * 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
|
1725 */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1726 static PyLongObject * |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1727 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
|
1728 { |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1729 int asize = ABS(a->ob_size); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1730 int bsize = ABS(b->ob_size); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1731 PyLongObject *ah = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1732 PyLongObject *al = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1733 PyLongObject *bh = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1734 PyLongObject *bl = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1735 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
|
1736 PyLongObject *t1, *t2, *t3; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1737 int shift; /* the number of digits we split off */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1738 int i; |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1739 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1740 /* (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
|
1741 * 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
|
1742 * Then the original product is |
|
24746
49169d00ab18
k_mul(): Repaired typo in comment.
Tim Peters <tim.peters@gmail.com>
parents:
24745
diff
changeset
|
1743 * 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
|
1744 * 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
|
1745 * 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
|
1746 */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1747 |
|
24747
19e010f792a3
k_mul(): Repaired another typo in another comment.
Tim Peters <tim.peters@gmail.com>
parents:
24746
diff
changeset
|
1748 /* 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
|
1749 * is largest. |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1750 */ |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1751 if (asize > bsize) { |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1752 t1 = a; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1753 a = b; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1754 b = t1; |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1755 |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1756 i = asize; |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1757 asize = bsize; |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1758 bsize = i; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1759 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1760 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1761 /* Use gradeschool math when either number is too small. */ |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1762 if (asize <= KARATSUBA_CUTOFF) { |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1763 if (asize == 0) |
|
24751
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1764 return _PyLong_New(0); |
|
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1765 else |
|
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1766 return x_mul(a, b); |
|
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1767 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1768 |
|
24773
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1769 /* 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
|
1770 * 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
|
1771 * 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
|
1772 * 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
|
1773 * 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
|
1774 */ |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1775 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
|
1776 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
|
1777 |
|
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
|
1778 /* 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
|
1779 shift = bsize >> 1; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1780 if (kmul_split(a, shift, &ah, &al) < 0) goto fail; |
|
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
|
1781 assert(ah->ob_size > 0); /* the split isn't degenerate */ |
|
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
|
1782 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1783 if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1784 |
|
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
|
1785 /* 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
|
1786 * 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
|
1787 * 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
|
1788 * 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
|
1789 * 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
|
1790 * 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
|
1791 * 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
|
1792 * 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
|
1793 * we're effectively doing unsigned arithmetic mod |
|
3d2335094b88
k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents:
24755
diff
changeset
|
1794 * BASE**(sizea + sizeb), and so long as the *final* result fits, |
|
3d2335094b88
k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents:
24755
diff
changeset
|
1795 * 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
|
1796 * 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
|
1797 * 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
|
1798 * 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
|
1799 */ |
|
3d2335094b88
k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents:
24755
diff
changeset
|
1800 |
|
3d2335094b88
k_mul() and long_mul(): I'm confident that the Karatsuba algorithm is
Tim Peters <tim.peters@gmail.com>
parents:
24755
diff
changeset
|
1801 /* 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
|
1802 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
|
1803 if (ret == NULL) goto fail; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1804 #ifdef Py_DEBUG |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1805 /* Fill with trash, to catch reference to uninitialized digits. */ |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1806 memset(ret->ob_digit, 0xDF, ret->ob_size * sizeof(digit)); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1807 #endif |
|
24751
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1808 |
|
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
|
1809 /* 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
|
1810 if ((t1 = k_mul(ah, bh)) == NULL) goto fail; |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1811 assert(t1->ob_size >= 0); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1812 assert(2*shift + t1->ob_size <= ret->ob_size); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1813 memcpy(ret->ob_digit + 2*shift, t1->ob_digit, |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1814 t1->ob_size * sizeof(digit)); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1815 |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1816 /* Zero-out the digits higher than the ah*bh copy. */ |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1817 i = ret->ob_size - 2*shift - t1->ob_size; |
|
24751
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1818 if (i) |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1819 memset(ret->ob_digit + 2*shift + t1->ob_size, 0, |
|
24751
dbf6e563023b
x_mul(): This failed to normalize its result.
Tim Peters <tim.peters@gmail.com>
parents:
24750
diff
changeset
|
1820 i * sizeof(digit)); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1821 |
|
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
|
1822 /* 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
|
1823 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
|
1824 Py_DECREF(t1); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1825 goto fail; |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1826 } |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1827 assert(t2->ob_size >= 0); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1828 assert(t2->ob_size <= 2*shift); /* no overlap with high digits */ |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1829 memcpy(ret->ob_digit, t2->ob_digit, t2->ob_size * sizeof(digit)); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1830 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1831 /* Zero out remaining digits. */ |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1832 i = 2*shift - t2->ob_size; /* number of uninitialized digits */ |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1833 if (i) |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1834 memset(ret->ob_digit + t2->ob_size, 0, i * sizeof(digit)); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1835 |
|
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
|
1836 /* 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
|
1837 * 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
|
1838 */ |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1839 i = ret->ob_size - shift; /* # digits after shift */ |
|
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
|
1840 (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, t2->ob_size); |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1841 Py_DECREF(t2); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1842 |
|
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
|
1843 (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, t1->ob_size); |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1844 Py_DECREF(t1); |
|
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1845 |
|
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
|
1846 /* 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
|
1847 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
|
1848 Py_DECREF(ah); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1849 Py_DECREF(al); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1850 ah = al = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1851 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1852 if ((t2 = x_add(bh, bl)) == NULL) { |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1853 Py_DECREF(t1); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1854 goto fail; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1855 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1856 Py_DECREF(bh); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1857 Py_DECREF(bl); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1858 bh = bl = NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1859 |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1860 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
|
1861 Py_DECREF(t1); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1862 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
|
1863 if (t3 == NULL) goto fail; |
|
24765
4cafd9dbc8b4
k_mul(): Moved an assert down. In a debug build, interrupting a
Tim Peters <tim.peters@gmail.com>
parents:
24764
diff
changeset
|
1864 assert(t3->ob_size >= 0); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1865 |
|
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
|
1866 /* 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
|
1867 * 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
|
1868 */ |
|
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
|
1869 (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, t3->ob_size); |
|
24755
c88d0848c5c9
k_mul: Rearranged computation for better cache use. Ignored overflow
Tim Peters <tim.peters@gmail.com>
parents:
24751
diff
changeset
|
1870 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
|
1871 |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1872 return long_normalize(ret); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1873 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1874 fail: |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1875 Py_XDECREF(ret); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1876 Py_XDECREF(ah); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1877 Py_XDECREF(al); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1878 Py_XDECREF(bh); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1879 Py_XDECREF(bl); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1880 return NULL; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1881 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1882 |
|
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
|
1883 /* (*) 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
|
1884 |
|
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
|
1885 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
|
1886 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
|
1887 |
|
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
|
1888 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
|
1889 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
|
1890 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
|
1891 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
|
1892 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
|
1893 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
|
1894 |
|
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
|
1895 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
|
1896 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
|
1897 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
|
1898 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
|
1899 |
|
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
|
1900 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
|
1901 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
|
1902 |
|
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
|
1903 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
|
1904 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
|
1905 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
|
1906 |
|
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
|
1907 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
|
1908 |
|
10a4ec45549d
k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents:
24845
diff
changeset
|
1909 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
|
1910 |
|
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
|
1911 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
|
1912 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
|
1913 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
|
1914 (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
|
1915 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
|
1916 asize is at least f(bsize/2)+1 digits, so this in turn reduces to whether 1 |
|
10a4ec45549d
k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents:
24845
diff
changeset
|
1917 digit is enough to hold 2 bits. This is so since SHIFT=15 >= 2. If |
|
10a4ec45549d
k_mul() comments: In honor of Dijkstra, made the proof that "t3 fits"
Tim Peters <tim.peters@gmail.com>
parents:
24845
diff
changeset
|
1918 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
|
1919 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
|
1920 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
|
1921 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
|
1922 |
|
24824
7c91d303c563
k_mul() comments: Simplified the simplified explanation of why ah*bh and
Tim Peters <tim.peters@gmail.com>
parents:
24821
diff
changeset
|
1923 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
|
1924 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
|
1925 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
|
1926 */ |
|
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
|
1927 |
|
24773
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1928 /* 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
|
1929 * 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
|
1930 * 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
|
1931 * 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
|
1932 * 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
|
1933 * 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
|
1934 * 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
|
1935 */ |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1936 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
|
1937 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
|
1938 { |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1939 const int asize = ABS(a->ob_size); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1940 int bsize = ABS(b->ob_size); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1941 int nbdone; /* # of b digits already multiplied */ |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1942 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
|
1943 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
|
1944 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1945 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
|
1946 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
|
1947 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1948 /* 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
|
1949 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
|
1950 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
|
1951 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
|
1952 memset(ret->ob_digit, 0, ret->ob_size * sizeof(digit)); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1953 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1954 /* 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
|
1955 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
|
1956 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
|
1957 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
|
1958 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1959 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
|
1960 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
|
1961 PyLongObject *product; |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1962 const int nbtouse = MIN(bsize, asize); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1963 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1964 /* 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
|
1965 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
|
1966 nbtouse * sizeof(digit)); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1967 bslice->ob_size = nbtouse; |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1968 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
|
1969 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
|
1970 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
|
1971 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1972 /* Add into result. */ |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1973 (void)v_iadd(ret->ob_digit + nbdone, ret->ob_size - nbdone, |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1974 product->ob_digit, product->ob_size); |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1975 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
|
1976 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1977 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
|
1978 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
|
1979 } |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1980 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1981 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
|
1982 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
|
1983 |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1984 fail: |
|
801a44ab2aae
Added new function k_lopsided_mul(), which is much more efficient than
Tim Peters <tim.peters@gmail.com>
parents:
24765
diff
changeset
|
1985 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
|
1986 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
|
1987 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
|
1988 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1989 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1990 static PyObject * |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1991 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
|
1992 { |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1993 PyLongObject *a, *b, *z; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1994 |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1995 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
|
1996 Py_INCREF(Py_NotImplemented); |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1997 return Py_NotImplemented; |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1998 } |
|
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
1999 |
|
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
|
2000 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
|
2001 /* 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
|
2002 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
|
2003 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
|
2004 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
|
2005 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
|
2006 return (PyObject *)z; |
| 230 | 2007 } |
| 2008 | |
| 613 | 2009 /* The / and % operators are now defined in terms of divmod(). |
| 2010 The expression a mod b has the value a - b*floor(a/b). | |
| 2011 The long_divrem function gives the remainder after division of | |
| 230 | 2012 |a| by |b|, with the sign of a. This is also expressed |
| 2013 as a - b*trunc(a/b), if trunc truncates towards zero. | |
| 2014 Some examples: | |
| 2015 a b a rem b a mod b | |
| 2016 13 10 3 3 | |
| 2017 -13 10 -3 7 | |
| 2018 13 -10 3 -7 | |
| 2019 -13 -10 -3 -3 | |
| 2020 So, to get from rem to mod, we have to add b if a and b | |
| 242 | 2021 have different signs. We then subtract one from the 'div' |
| 2022 part of the outcome to keep the invariant intact. */ | |
| 230 | 2023 |
| 613 | 2024 static int |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2025 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
|
2026 PyLongObject **pdiv, PyLongObject **pmod) |
| 613 | 2027 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2028 PyLongObject *div, *mod; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2029 |
| 613 | 2030 if (long_divrem(v, w, &div, &mod) < 0) |
| 2031 return -1; | |
|
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
|
2032 if ((mod->ob_size < 0 && w->ob_size > 0) || |
|
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
|
2033 (mod->ob_size > 0 && w->ob_size < 0)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2034 PyLongObject *temp; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2035 PyLongObject *one; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2036 temp = (PyLongObject *) long_add(mod, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2037 Py_DECREF(mod); |
| 613 | 2038 mod = temp; |
| 2039 if (mod == NULL) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2040 Py_DECREF(div); |
| 613 | 2041 return -1; |
| 2042 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2043 one = (PyLongObject *) PyLong_FromLong(1L); |
| 613 | 2044 if (one == NULL || |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2045 (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2046 Py_DECREF(mod); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2047 Py_DECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2048 Py_XDECREF(one); |
| 613 | 2049 return -1; |
| 2050 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2051 Py_DECREF(one); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2052 Py_DECREF(div); |
| 613 | 2053 div = temp; |
| 2054 } | |
| 2055 *pdiv = div; | |
| 2056 *pmod = mod; | |
| 2057 return 0; | |
| 2058 } | |
| 2059 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2060 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
|
2061 long_div(PyObject *v, PyObject *w) |
| 613 | 2062 { |
|
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
|
2063 PyLongObject *a, *b, *div, *mod; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2064 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2065 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
|
2066 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2067 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
|
2068 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
|
2069 Py_DECREF(b); |
| 613 | 2070 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
|
2071 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2072 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
|
2073 Py_DECREF(b); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2074 Py_DECREF(mod); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2075 return (PyObject *)div; |
| 613 | 2076 } |
| 2077 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2078 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
|
2079 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
|
2080 { |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2081 PyLongObject *a, *b, *div, *mod; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2082 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2083 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
|
2084 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2085 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
|
2086 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
|
2087 div = NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2088 else if (l_divmod(a, b, &div, &mod) < 0) |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2089 div = NULL; |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2090 else |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2091 Py_DECREF(mod); |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2092 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2093 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
|
2094 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
|
2095 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
|
2096 } |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2097 |
|
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2098 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
|
2099 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
|
2100 { |
|
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
|
2101 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
|
2102 double ad, bd; |
|
20868
2d9e41a72a68
long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents:
20277
diff
changeset
|
2103 int aexp, bexp, 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
|
2104 |
|
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
|
2105 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
|
2106 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
|
2107 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
|
2108 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
|
2109 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
|
2110 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
|
2111 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
|
2112 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
|
2113 |
|
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
|
2114 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
|
2115 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
|
2116 "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
|
2117 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
|
2118 } |
|
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
|
2119 |
|
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
|
2120 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */ |
|
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
|
2121 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
|
2122 aexp -= bexp; |
|
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
|
2123 if (aexp > INT_MAX / SHIFT) |
|
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
|
2124 goto overflow; |
|
19787
f814ea6bb667
long_true_divide: reliably force underflow to 0 when the denominator
Tim Peters <tim.peters@gmail.com>
parents:
19719
diff
changeset
|
2125 else if (aexp < -(INT_MAX / SHIFT)) |
|
f814ea6bb667
long_true_divide: reliably force underflow to 0 when the denominator
Tim Peters <tim.peters@gmail.com>
parents:
19719
diff
changeset
|
2126 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
|
2127 errno = 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
|
2128 ad = ldexp(ad, aexp * 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
|
2129 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
|
2130 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
|
2131 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
|
2132 |
|
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
|
2133 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
|
2134 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
|
2135 "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
|
2136 return NULL; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2137 |
|
19675
685dcae5c123
Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents:
19674
diff
changeset
|
2138 } |
|
685dcae5c123
Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents:
19674
diff
changeset
|
2139 |
|
685dcae5c123
Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents:
19674
diff
changeset
|
2140 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
|
2141 long_mod(PyObject *v, PyObject *w) |
| 613 | 2142 { |
|
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
|
2143 PyLongObject *a, *b, *div, *mod; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2144 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2145 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
|
2146 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2147 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
|
2148 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
|
2149 Py_DECREF(b); |
| 613 | 2150 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
|
2151 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2152 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
|
2153 Py_DECREF(b); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2154 Py_DECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2155 return (PyObject *)mod; |
| 613 | 2156 } |
| 2157 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2158 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
|
2159 long_divmod(PyObject *v, PyObject *w) |
| 230 | 2160 { |
|
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
|
2161 PyLongObject *a, *b, *div, *mod; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2162 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
|
2163 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2164 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
|
2165 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2166 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
|
2167 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
|
2168 Py_DECREF(b); |
| 230 | 2169 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
|
2170 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2171 z = PyTuple_New(2); |
| 230 | 2172 if (z != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2173 PyTuple_SetItem(z, 0, (PyObject *) div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2174 PyTuple_SetItem(z, 1, (PyObject *) mod); |
| 230 | 2175 } |
| 2176 else { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2177 Py_DECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2178 Py_DECREF(mod); |
| 230 | 2179 } |
|
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
|
2180 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
|
2181 Py_DECREF(b); |
| 230 | 2182 return z; |
| 2183 } | |
| 2184 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2185 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
|
2186 long_pow(PyObject *v, PyObject *w, PyObject *x) |
| 230 | 2187 { |
|
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
|
2188 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
|
2189 PyObject *c; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2190 PyLongObject *z, *div, *mod; |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2191 int size_b, i; |
|
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
|
2192 |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2193 CONVERT_BINOP(v, w, &a, &b); |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2194 if (PyLong_Check(x) || Py_None == 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
|
2195 c = 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
|
2196 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
|
2197 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2198 else if (PyInt_Check(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
|
2199 c = PyLong_FromLong(PyInt_AS_LONG(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
|
2200 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2201 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
|
2202 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
|
2203 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
|
2204 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
|
2205 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
|
2206 } |
|
19719
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2207 |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2208 if (c != Py_None && ((PyLongObject *)c)->ob_size == 0) { |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2209 PyErr_SetString(PyExc_ValueError, |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2210 "pow() 3rd argument cannot be 0"); |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2211 z = NULL; |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2212 goto error; |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2213 } |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2214 |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2215 size_b = b->ob_size; |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2216 if (size_b < 0) { |
|
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
|
2217 Py_DECREF(a); |
|
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
|
2218 Py_DECREF(b); |
|
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
|
2219 Py_DECREF(c); |
|
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
|
2220 if (x != Py_None) { |
|
19719
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2221 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
|
cdfdd5359411
Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents:
19717
diff
changeset
|
2222 "cannot be negative when 3rd argument specified"); |
|
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
|
2223 return NULL; |
|
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
|
2224 } |
|
18500
e6ecd8a07ea8
On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents:
18134
diff
changeset
|
2225 /* Return a float. This works because we know that |
|
e6ecd8a07ea8
On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents:
18134
diff
changeset
|
2226 this calls float_pow() which converts its |
|
e6ecd8a07ea8
On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents:
18134
diff
changeset
|
2227 arguments to double. */ |
|
e6ecd8a07ea8
On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents:
18134
diff
changeset
|
2228 return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2229 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2230 z = (PyLongObject *)PyLong_FromLong(1L); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2231 for (i = 0; i < size_b; ++i) { |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2232 digit bi = b->ob_digit[i]; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2233 int j; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2234 |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2235 for (j = 0; j < SHIFT; ++j) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2236 PyLongObject *temp; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2237 |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2238 if (bi & 1) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2239 temp = (PyLongObject *)long_mul(z, a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2240 Py_DECREF(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
|
2241 if (c!=Py_None && temp!=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
|
2242 if (l_divmod(temp,(PyLongObject *)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
|
2243 &div,&mod) < 0) { |
|
11380
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2244 Py_DECREF(temp); |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2245 z = NULL; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2246 goto error; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2247 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2248 Py_XDECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2249 Py_DECREF(temp); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2250 temp = mod; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2251 } |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2252 z = temp; |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2253 if (z == NULL) |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2254 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2255 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2256 bi >>= 1; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2257 if (bi == 0 && i+1 == size_b) |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2258 break; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2259 temp = (PyLongObject *)long_mul(a, a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2260 Py_DECREF(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
|
2261 if (c!=Py_None && temp!=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
|
2262 if (l_divmod(temp, (PyLongObject *)c, &div, |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2263 &mod) < 0) { |
|
11380
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2264 Py_DECREF(temp); |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2265 z = NULL; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2266 goto error; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2267 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2268 Py_XDECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2269 Py_DECREF(temp); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2270 temp = mod; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2271 } |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2272 a = temp; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2273 if (a == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2274 Py_DECREF(z); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2275 z = NULL; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2276 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2277 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2278 } |
|
2111
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
2279 if (a == NULL || z == NULL) |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2280 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
2281 } |
|
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
|
2282 if (c!=Py_None && 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
|
2283 if (l_divmod(z, (PyLongObject *)c, &div, &mod) < 0) { |
|
11380
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2284 Py_DECREF(z); |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2285 z = NULL; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2286 } |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2287 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2288 Py_XDECREF(div); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2289 Py_DECREF(z); |
|
11380
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2290 z = mod; |
|
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2291 } |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2292 } |
|
11380
3e3c71f4d2de
Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents:
11331
diff
changeset
|
2293 error: |
|
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
|
2294 Py_XDECREF(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
|
2295 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
|
2296 Py_DECREF(c); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2297 return (PyObject *)z; |
| 230 | 2298 } |
| 2299 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2300 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
|
2301 long_invert(PyLongObject *v) |
| 230 | 2302 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2303 /* Implement ~x as -(x+1) */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2304 PyLongObject *x; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2305 PyLongObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2306 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
|
2307 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
|
2308 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2309 x = (PyLongObject *) long_add(v, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2310 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
|
2311 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
|
2312 return NULL; |
|
19882
96f53f8831ee
long_invert(): tiny speed and space optimization.
Tim Peters <tim.peters@gmail.com>
parents:
19880
diff
changeset
|
2313 x->ob_size = -(x->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2314 return (PyObject *)x; |
| 230 | 2315 } |
| 2316 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2317 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
|
2318 long_pos(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
|
2319 { |
|
19880
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2320 if (PyLong_CheckExact(v)) { |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2321 Py_INCREF(v); |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2322 return (PyObject *)v; |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2323 } |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2324 else |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2325 return _PyLong_Copy(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
|
2326 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2327 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2328 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
|
2329 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
|
2330 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2331 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
|
2332 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
|
2333 /* -0 == 0 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2334 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2335 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
|
2336 } |
|
19880
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2337 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
|
2338 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
|
2339 z->ob_size = -(v->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2340 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
|
2341 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2342 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2343 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
|
2344 long_abs(PyLongObject *v) |
| 230 | 2345 { |
| 2346 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
|
2347 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
|
2348 else |
|
d1cd4cd72271
More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19837
diff
changeset
|
2349 return long_pos(v); |
| 230 | 2350 } |
| 2351 | |
| 242 | 2352 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
|
2353 long_nonzero(PyLongObject *v) |
| 242 | 2354 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2355 return ABS(v->ob_size) != 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
|
2356 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2357 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2358 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
|
2359 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
|
2360 { |
|
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
|
2361 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
|
2362 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
|
2363 long shiftby; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2364 int newsize, wordshift, loshift, hishift, i, j; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2365 digit lomask, himask; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2366 |
|
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
|
2367 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
|
2368 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2369 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
|
2370 /* 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
|
2371 PyLongObject *a1, *a2; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2372 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
|
2373 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
|
2374 goto rshift_error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2375 a2 = (PyLongObject *) long_rshift(a1, b); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2376 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
|
2377 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
|
2378 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
|
2379 z = (PyLongObject *) long_invert(a2); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2380 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
|
2381 } |
|
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
|
2382 else { |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2383 |
|
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
|
2384 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
|
2385 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
|
2386 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
|
2387 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
|
2388 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
|
2389 "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
|
2390 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
|
2391 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2392 wordshift = shiftby / SHIFT; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2393 newsize = ABS(a->ob_size) - wordshift; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2394 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
|
2395 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
|
2396 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
|
2397 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
|
2398 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
|
2399 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2400 loshift = shiftby % SHIFT; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2401 hishift = SHIFT - loshift; |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2402 lomask = ((digit)1 << hishift) - 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
|
2403 himask = MASK ^ 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
|
2404 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
|
2405 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
|
2406 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
|
2407 if (a->ob_size < 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
|
2408 z->ob_size = -(z->ob_size); |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2409 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
|
2410 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
|
2411 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
|
2412 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
|
2413 (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
|
2414 } |
|
8e402aeca0a3
Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2415 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
|
2416 } |
|
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
|
2417 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
|
2418 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
|
2419 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
|
2420 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
|
2421 |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2422 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2423 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2424 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
|
2425 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
|
2426 { |
|
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
|
2427 /* 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
|
2428 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
|
2429 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
|
2430 long shiftby; |
|
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
|
2431 int oldsize, newsize, wordshift, remshift, i, j; |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2432 twodigits accum; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2433 |
|
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
|
2434 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
|
2435 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2436 shiftby = PyLong_AsLong((PyObject *)b); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2437 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
|
2438 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
|
2439 if (shiftby < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2440 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
|
2441 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
|
2442 } |
|
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
|
2443 if ((long)(int)shiftby != shiftby) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2444 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2445 "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
|
2446 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
|
2447 } |
|
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
|
2448 /* wordshift, remshift = divmod(shiftby, SHIFT) */ |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2449 wordshift = (int)shiftby / SHIFT; |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2450 remshift = (int)shiftby - wordshift * SHIFT; |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2451 |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2452 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
|
2453 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
|
2454 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
|
2455 ++newsize; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2456 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
|
2457 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
|
2458 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
|
2459 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
|
2460 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
|
2461 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
|
2462 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
|
2463 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
|
2464 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
|
2465 accum |= (twodigits)a->ob_digit[j] << remshift; |
|
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
|
2466 z->ob_digit[i] = (digit)(accum & MASK); |
|
7e380df00d00
New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
5197
diff
changeset
|
2467 accum >>= 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
|
2468 } |
|
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
|
2469 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
|
2470 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
|
2471 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
|
2472 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
|
2473 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
|
2474 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
|
2475 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
|
2476 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
|
2477 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
|
2478 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2479 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2480 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2481 /* 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
|
2482 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2483 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
|
2484 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
|
2485 int op, /* '&', '|', '^' */ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2486 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
|
2487 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2488 digit maska, maskb; /* 0 or MASK */ |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2489 int negz; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2490 int size_a, size_b, size_z; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2491 PyLongObject *z; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2492 int i; |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
2493 digit diga, digb; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2494 PyObject *v; |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2495 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2496 if (a->ob_size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2497 a = (PyLongObject *) long_invert(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
|
2498 maska = MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2499 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2500 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2501 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
|
2502 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
|
2503 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2504 if (b->ob_size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2505 b = (PyLongObject *) long_invert(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
|
2506 maskb = 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
|
2507 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2508 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2509 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
|
2510 maskb = 0; |
|
546
937cbc0be6ec
Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
2511 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2512 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2513 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
|
2514 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
|
2515 case '^': |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2516 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
|
2517 maska ^= MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2518 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
|
2519 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2520 break; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
2521 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
|
2522 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
|
2523 op = '|'; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2524 maska ^= MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2525 maskb ^= MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2526 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
|
2527 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2528 break; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2529 case '|': |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2530 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
|
2531 op = '&'; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2532 maska ^= MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2533 maskb ^= MASK; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2534 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
|
2535 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2536 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
|
2537 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2538 |
|
8933
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2539 /* 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
|
2540 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
|
2541 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
|
2542 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
|
2543 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
|
2544 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
|
2545 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
|
2546 whose length should be ignored. |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2547 */ |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2548 |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2549 size_a = a->ob_size; |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2550 size_b = b->ob_size; |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2551 size_z = op == '&' |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2552 ? (maska |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2553 ? size_b |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2554 : (maskb ? size_a : MIN(size_a, size_b))) |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2555 : MAX(size_a, size_b); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2556 z = _PyLong_New(size_z); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2557 if (a == NULL || b == NULL || z == NULL) { |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2558 Py_XDECREF(a); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2559 Py_XDECREF(b); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2560 Py_XDECREF(z); |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2561 return NULL; |
|
dc6edfd4bae5
Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents:
8822
diff
changeset
|
2562 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2563 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
2564 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
|
2565 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
|
2566 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
|
2567 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
|
2568 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
|
2569 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
|
2570 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
|
2571 } |
|
546
937cbc0be6ec
Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
2572 } |
|
24745
ba035f4b47c1
Cautious introduction of a patch that started from
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
2573 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2574 Py_DECREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2575 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
|
2576 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
|
2577 if (negz == 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2578 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
|
2579 v = long_invert(z); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2580 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
|
2581 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
|
2582 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2583 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2584 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
|
2585 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
|
2586 { |
|
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
|
2587 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
|
2588 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
|
2589 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
|
2590 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
|
2591 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
|
2592 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
|
2593 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
|
2594 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2595 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2596 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
|
2597 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
|
2598 { |
|
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
|
2599 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
|
2600 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
|
2601 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
|
2602 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
|
2603 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
|
2604 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
|
2605 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
|
2606 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
2607 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2608 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
|
2609 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
|
2610 { |
|
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
|
2611 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
|
2612 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
|
2613 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
|
2614 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
|
2615 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
|
2616 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
|
2617 return c; |
| 242 | 2618 } |
| 2619 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
2620 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
|
2621 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
|
2622 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2623 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
|
2624 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw)); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2625 Py_INCREF(*pv); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
2626 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
2627 } |
|
19976
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19925
diff
changeset
|
2628 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
|
2629 Py_INCREF(*pv); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19925
diff
changeset
|
2630 Py_INCREF(*pw); |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19925
diff
changeset
|
2631 return 0; |
|
da00188d5270
Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents:
19925
diff
changeset
|
2632 } |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
2633 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
|
2634 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
2635 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2636 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
|
2637 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
|
2638 { |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2639 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
|
2640 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
|
2641 } |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2642 |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2643 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
|
2644 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
|
2645 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2646 long x; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2647 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
|
2648 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
|
2649 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
|
2650 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
|
2651 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
|
2652 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
|
2653 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
|
2654 } |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2655 else |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2656 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
|
2657 } |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2658 else |
|
d31ddc66b1b5
Change int() so that passing a string, unicode, float or long argument
Walter Dörwald <walter@livinglogic.de>
parents:
26081
diff
changeset
|
2659 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
|
2660 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2661 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
|
2662 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2663 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2664 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
|
2665 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
|
2666 { |
|
5118
002865af55e5
Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents:
4870
diff
changeset
|
2667 double result; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2668 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
|
2669 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
|
2670 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2671 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
|
2672 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2673 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2674 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
|
2675 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
|
2676 { |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
2677 return long_format(v, 8, 1); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2678 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2679 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2680 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
|
2681 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
|
2682 { |
|
11628
47f93a969366
long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents:
11380
diff
changeset
|
2683 return long_format(v, 16, 1); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
2684 } |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24231
diff
changeset
|
2685 |
|
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24231
diff
changeset
|
2686 static PyObject * |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2687 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
|
2688 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2689 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2690 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
|
2691 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2692 PyObject *x = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2693 int base = -909; /* unlikely! */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2694 static char *kwlist[] = {"x", "base", 0}; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2695 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2696 if (type != &PyLong_Type) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2697 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
|
2698 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
|
2699 &x, &base)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2700 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2701 if (x == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2702 return PyLong_FromLong(0L); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2703 if (base == -909) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2704 return PyNumber_Long(x); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2705 else if (PyString_Check(x)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2706 return PyLong_FromString(PyString_AS_STRING(x), NULL, base); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
2707 #ifdef Py_USING_UNICODE |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2708 else if (PyUnicode_Check(x)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2709 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
|
2710 PyUnicode_GET_SIZE(x), |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2711 base); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19051
diff
changeset
|
2712 #endif |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2713 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2714 PyErr_SetString(PyExc_TypeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2715 "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
|
2716 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2717 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2718 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2719 |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2720 /* 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
|
2721 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
|
2722 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
|
2723 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
|
2724 */ |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2725 static PyObject * |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2726 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
|
2727 { |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2728 PyLongObject *tmp, *new; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2729 int i, n; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2730 |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2731 assert(PyType_IsSubtype(type, &PyLong_Type)); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2732 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
|
2733 if (tmp == NULL) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2734 return NULL; |
|
19837
5158e4789459
SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19787
diff
changeset
|
2735 assert(PyLong_CheckExact(tmp)); |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2736 n = tmp->ob_size; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2737 if (n < 0) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2738 n = -n; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2739 new = (PyLongObject *)type->tp_alloc(type, n); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2740 if (new == NULL) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2741 return NULL; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2742 assert(PyLong_Check(new)); |
|
19610
c773daa44840
Ah, the joy of writing test cases...
Guido van Rossum <guido@python.org>
parents:
19552
diff
changeset
|
2743 new->ob_size = tmp->ob_size; |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2744 for (i = 0; i < n; i++) |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2745 new->ob_digit[i] = tmp->ob_digit[i]; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2746 Py_DECREF(tmp); |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2747 return (PyObject *)new; |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2748 } |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2749 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2750 static PyObject * |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2751 long_getnewargs(PyLongObject *v) |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2752 { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2753 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
|
2754 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2755 |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2756 static PyMethodDef long_methods[] = { |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2757 {"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS}, |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2758 {NULL, NULL} /* sentinel */ |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2759 }; |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2760 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23248
diff
changeset
|
2761 PyDoc_STRVAR(long_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2762 "long(x[, base]) -> integer\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2763 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2764 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
|
2765 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
|
2766 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
|
2767 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
|
2768 converting a non-string."); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2769 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2770 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
|
2771 (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
|
2772 (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
|
2773 (binaryfunc) long_mul, /*nb_multiply*/ |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19610
diff
changeset
|
2774 (binaryfunc) long_classic_div, /*nb_divide*/ |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2775 (binaryfunc) long_mod, /*nb_remainder*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2776 (binaryfunc) long_divmod, /*nb_divmod*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2777 (ternaryfunc) long_pow, /*nb_power*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2778 (unaryfunc) long_neg, /*nb_negative*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2779 (unaryfunc) long_pos, /*tp_positive*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2780 (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
|
2781 (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
|
2782 (unaryfunc) long_invert, /*nb_invert*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2783 (binaryfunc) long_lshift, /*nb_lshift*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2784 (binaryfunc) long_rshift, /*nb_rshift*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2785 (binaryfunc) long_and, /*nb_and*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2786 (binaryfunc) long_xor, /*nb_xor*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2787 (binaryfunc) long_or, /*nb_or*/ |
|
13418
85520c220067
Got RID of redundant coercions in longobject.c (as spotted by Greg
Tim Peters <tim.peters@gmail.com>
parents:
13406
diff
changeset
|
2788 (coercion) long_coerce, /*nb_coerce*/ |
|
13406
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2789 (unaryfunc) long_int, /*nb_int*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2790 (unaryfunc) long_long, /*nb_long*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2791 (unaryfunc) long_float, /*nb_float*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2792 (unaryfunc) long_oct, /*nb_oct*/ |
|
a503af7e7fb7
Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
2793 (unaryfunc) long_hex, /*nb_hex*/ |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2794 0, /* nb_inplace_add */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2795 0, /* nb_inplace_subtract */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2796 0, /* nb_inplace_multiply */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2797 0, /* nb_inplace_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2798 0, /* nb_inplace_remainder */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2799 0, /* nb_inplace_power */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2800 0, /* nb_inplace_lshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2801 0, /* nb_inplace_rshift */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2802 0, /* nb_inplace_and */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2803 0, /* nb_inplace_xor */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2804 0, /* nb_inplace_or */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2805 (binaryfunc)long_div, /* nb_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2806 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
|
2807 0, /* nb_inplace_floor_divide */ |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
2808 0, /* nb_inplace_true_divide */ |
| 230 | 2809 }; |
| 2810 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2811 PyTypeObject PyLong_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2812 PyObject_HEAD_INIT(&PyType_Type) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2813 0, /* ob_size */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2814 "long", /* tp_name */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2815 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2816 sizeof(digit), /* tp_itemsize */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2817 (destructor)long_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2818 0, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2819 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2820 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2821 (cmpfunc)long_compare, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2822 (reprfunc)long_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2823 &long_as_number, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2824 0, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2825 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2826 (hashfunc)long_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2827 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2828 (reprfunc)long_str, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2829 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2830 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2831 0, /* tp_as_buffer */ |
|
19552
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2832 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
6c30d20d27f2
Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents:
19343
diff
changeset
|
2833 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2834 long_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2835 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2836 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2837 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2838 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2839 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2840 0, /* tp_iternext */ |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27487
diff
changeset
|
2841 long_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2842 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2843 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2844 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2845 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2846 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2847 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2848 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2849 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2850 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18607
diff
changeset
|
2851 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
|
2852 PyObject_Del, /* tp_free */ |
| 230 | 2853 }; |
