annotate Objects/longobject.c @ 17322:bd110eadc19a legacy-trunk

One-liner critical fix from Jim Fulton: Eric's string-method crusade got the order backwards in a line (for .find()).
author Tim Peters <tim.peters@gmail.com>
date Thu, 05 Apr 2001 22:26:23 +0000
parents 4f621708514b
children dd09f11fa40d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Long (arbitrary precision) integer object implementation */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
4 /* XXX The functional organization of this file is terrible */
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
5
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
6 #include "Python.h"
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "longintrepr.h"
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
8
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #include <assert.h>
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
10 #include <ctype.h>
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
12 #define ABS(x) ((x) < 0 ? -(x) : (x))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
13
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
14 /* Forward */
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
15 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
16 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
17 static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
18 static PyLongObject *divrem1(PyLongObject *, wdigit, digit *);
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
19 static PyObject *long_format(PyObject *aa, int base, int addL);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
20
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
21 static int ticker; /* XXX Could be shared with ceval? */
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
22
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
23 #define SIGCHECK(PyTryBlock) \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
24 if (--ticker < 0) { \
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
25 ticker = 100; \
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
26 if (PyErr_CheckSignals()) { PyTryBlock; } \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
27 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
28
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 /* Normalize (remove leading zeros from) a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 Doesn't attempt to free the storage--in most cases, due to the nature
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 of the algorithms used, this could save at most be one word anyway. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
33 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
34 long_normalize(register PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
36 int j = ABS(v->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 register int i = j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 while (i > 0 && v->ob_digit[i-1] == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 --i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 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
42 v->ob_size = (v->ob_size < 0) ? -(i) : i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 /* Allocate a new long int object with size digits.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 Return NULL and set exception if we run out of memory. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
49 PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
50 _PyLong_New(int size)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
52 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 /* Create a new long int object from a C long int */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
57 PyObject *
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_FromLong(long ival)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
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
60 /* Assume a C long fits in at most 5 'digits' */
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
61 /* Works on both 32- and 64-bit machines */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
62 PyLongObject *v = _PyLong_New(5);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 if (v != NULL) {
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
64 unsigned long t = ival;
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
65 int i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if (ival < 0) {
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
67 t = -ival;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
68 v->ob_size = -(v->ob_size);
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
69 }
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
70 for (i = 0; i < 5; i++) {
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
71 v->ob_digit[i] = (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
72 t >>= SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 v = long_normalize(v);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
76 return (PyObject *)v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
79 /* 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
80
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
81 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
82 PyLong_FromUnsignedLong(unsigned long ival)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
83 {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
84 /* Assume a C long fits in at most 5 'digits' */
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
85 /* Works on both 32- and 64-bit machines */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
86 PyLongObject *v = _PyLong_New(5);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
87 if (v != NULL) {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
88 unsigned long t = ival;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
89 int i;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
90 for (i = 0; i < 5; i++) {
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
91 v->ob_digit[i] = (digit) (t & MASK);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
92 t >>= SHIFT;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
93 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
94 v = long_normalize(v);
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
95 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
96 return (PyObject *)v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
97 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
98
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
99 /* 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
100
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
101 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
102 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
103 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
104 PyLongObject *v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
105 double frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
106 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
107 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
108 if (Py_IS_INFINITY(dval)) {
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
109 PyErr_SetString(PyExc_OverflowError,
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
110 "cannot convert float infinity to long");
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
111 return NULL;
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
112 }
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
113 if (dval < 0.0) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
114 neg = 1;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
115 dval = -dval;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
116 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
117 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
118 if (expo <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
119 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
120 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
121 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
122 if (v == NULL)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
123 return NULL;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
124 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
125 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
126 long bits = (long)frac;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
127 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
128 frac = frac - (double)bits;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
129 frac = ldexp(frac, SHIFT);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
130 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
131 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
132 v->ob_size = -(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
133 return (PyObject *)v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
134 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
135
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 /* Get a C long int from a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 Returns -1 and sets an error condition if overflow occurs. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 long
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
140 PyLong_AsLong(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 {
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
142 /* This version by Tim Peters */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
143 register PyLongObject *v;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
144 unsigned long x, prev;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 int i, sign;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
146
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
147 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
148 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
151 v = (PyLongObject *)vv;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 x = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 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
157 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 while (--i >= 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 prev = x;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 x = (x << SHIFT) + v->ob_digit[i];
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
162 if ((x >> SHIFT) != prev)
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
163 goto overflow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 }
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
165 /* 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
166 * 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
167 * 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
168 * than the sign bit).
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
169 */
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
170 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
171 goto overflow;
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
172 return (long)x * sign;
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
173
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
174 overflow:
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
175 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
176 "long int too large to convert");
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
177 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
180 /* Get a C long int from a long int object.
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
181 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
182
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
183 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
184 PyLong_AsUnsignedLong(PyObject *vv)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
185 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
186 register PyLongObject *v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
187 unsigned long x, prev;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
188 int i;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
189
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
190 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
191 PyErr_BadInternalCall();
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
192 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
193 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
194 v = (PyLongObject *)vv;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
195 i = v->ob_size;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
196 x = 0;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
197 if (i < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
198 PyErr_SetString(PyExc_OverflowError,
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
199 "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
200 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
201 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
202 while (--i >= 0) {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
203 prev = x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
204 x = (x << SHIFT) + v->ob_digit[i];
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
205 if ((x >> SHIFT) != prev) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
206 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
207 "long int too large to convert");
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
208 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
209 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
210 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
211 return x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
212 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
213
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
214 /* Get a C double from a long int object. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 double
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
217 PyLong_AsDouble(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
219 register PyLongObject *v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 double x;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 double multiplier = (double) (1L << SHIFT);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 int i, sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
224 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
225 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
228 v = (PyLongObject *)vv;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 x = 0.0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 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
234 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 while (--i >= 0) {
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
237 x = x*multiplier + (double)v->ob_digit[i];
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 return x * sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
242 /* 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
243
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
244 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
245 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
246 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
247 #if SIZEOF_VOID_P == SIZEOF_LONG
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
248 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
249 #else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
250 /* optimize null pointers */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
251 if ( p == NULL )
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
252 return PyInt_FromLong(0);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
253
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
254 /* we can assume that HAVE_LONG_LONG is true. if not, then the
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
255 configuration process should have bailed (having big pointers
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
256 without long longs seems non-sensical) */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
257 return PyLong_FromLongLong((LONG_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
258 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
259 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
260
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
261 /* 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
262
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
263 void *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
264 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
265 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
266 /* 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
267 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
268 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
269 */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
270
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
271 #if SIZEOF_VOID_P == SIZEOF_LONG
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
272 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
273
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
274 if ( PyInt_Check(vv) )
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
275 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
276 else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
277 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
278 #else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
279 /* we can assume that HAVE_LONG_LONG is true. if not, then the
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
280 configuration process should have bailed (having big pointers
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
281 without long longs seems non-sensical) */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
282 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
283
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
284 if ( PyInt_Check(vv) )
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
285 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
286 else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
287 x = PyLong_AsLongLong(vv);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
288 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
289
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
290 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
291 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
292 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
293 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
294
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
295 #ifdef HAVE_LONG_LONG
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
296 /*
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
297 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
298 *
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
299 * For better or worse :-), I tried to follow the coding style already
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
300 * here.
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
301 */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
302
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
303 /* 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
304
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
305 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
306 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
307 {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
308 #if SIZEOF_LONG_LONG == SIZEOF_LONG
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
309 /* In case the compiler is faking it. */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
310 return PyLong_FromLong( (long)ival );
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
311 #else
12821
f2db60943d6b Trent Mick <trentm@ActiveState.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
312 if ((LONG_LONG)LONG_MIN <= ival && ival <= (LONG_LONG)LONG_MAX) {
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
313 return PyLong_FromLong( (long)ival );
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
314 }
12821
f2db60943d6b Trent Mick <trentm@ActiveState.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
315 else if (0 <= ival && ival <= (unsigned LONG_LONG)ULONG_MAX) {
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
316 return PyLong_FromUnsignedLong( (unsigned long)ival );
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
317 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
318 else {
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
319 /* Assume a C LONG_LONG fits in at most 10 'digits'.
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
320 * Should be OK if we're assuming long fits in 5.
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
321 */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
322 PyLongObject *v = _PyLong_New(10);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
323
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
324 if (v != NULL) {
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
325 unsigned LONG_LONG t = ival;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
326 int i;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
327 if (ival < 0) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
328 t = -ival;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
329 v->ob_size = -(v->ob_size);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
330 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
331
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
332 for (i = 0; i < 10; i++) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
333 v->ob_digit[i] = (digit) (t & MASK);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
334 t >>= SHIFT;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
335 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
336
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
337 v = long_normalize(v);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
338 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
339
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
340 return (PyObject *)v;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
341 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
342 #endif
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
343 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
344
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
345 /* Create a new long int object from a C unsigned 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
346 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
347 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
348 {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
349 #if SIZEOF_LONG_LONG == SIZEOF_LONG
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
350 /* In case the compiler is faking it. */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
351 return PyLong_FromUnsignedLong( (unsigned long)ival );
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
352 #else
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
353 if( ival <= (unsigned LONG_LONG)ULONG_MAX ) {
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
354 return PyLong_FromUnsignedLong( (unsigned long)ival );
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
355 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
356 else {
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
357 /* Assume a C long fits in at most 10 'digits'. */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
358 PyLongObject *v = _PyLong_New(10);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
359
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
360 if (v != NULL) {
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
361 unsigned LONG_LONG t = ival;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
362 int i;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
363 for (i = 0; i < 10; i++) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
364 v->ob_digit[i] = (digit) (t & MASK);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
365 t >>= SHIFT;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
366 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
367
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
368 v = long_normalize(v);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
369 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
370
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
371 return (PyObject *)v;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
372 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
373 #endif
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
374 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
375
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
376 /* Get a C LONG_LONG int from a long int object.
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
377 Returns -1 and sets an error condition if overflow occurs. */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
378
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
379 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
380 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
381 {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
382 #if SIZEOF_LONG_LONG == SIZEOF_LONG
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
383 /* In case the compiler is faking it. */
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
384 return (LONG_LONG)PyLong_AsLong( vv );
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
385 #else
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
386 register PyLongObject *v;
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
387 LONG_LONG x, prev;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
388 int i, sign;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
389
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
390 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
391 PyErr_BadInternalCall();
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
392 return -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
393 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
394
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
395 v = (PyLongObject *)vv;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
396 i = v->ob_size;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
397 sign = 1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
398 x = 0;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
399
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
400 if (i < 0) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
401 sign = -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
402 i = -(i);
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
403 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
404
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
405 while (--i >= 0) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
406 prev = x;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
407 x = (x << SHIFT) + v->ob_digit[i];
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
408 if ((x >> SHIFT) != prev) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
409 PyErr_SetString(PyExc_OverflowError,
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
410 "long int too long to convert");
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
411 return -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
412 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
413 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
414
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
415 return x * sign;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
416 #endif
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
417 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
418
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
419 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
420 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
421 {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
422 #if SIZEOF_LONG_LONG == 4
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
423 /* In case the compiler is faking it. */
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
424 return (unsigned LONG_LONG)PyLong_AsUnsignedLong( vv );
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
425 #else
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
426 register PyLongObject *v;
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
427 unsigned LONG_LONG x, prev;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
428 int i;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
429
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
430 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
431 PyErr_BadInternalCall();
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
432 return (unsigned LONG_LONG) -1;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
433 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
434
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
435 v = (PyLongObject *)vv;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
436 i = v->ob_size;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
437 x = 0;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
438
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
439 if (i < 0) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
440 PyErr_SetString(PyExc_OverflowError,
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
441 "can't convert negative value to unsigned long");
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
442 return (unsigned LONG_LONG) -1;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
443 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
444
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
445 while (--i >= 0) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
446 prev = x;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
447 x = (x << SHIFT) + v->ob_digit[i];
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
448 if ((x >> SHIFT) != prev) {
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
449 PyErr_SetString(PyExc_OverflowError,
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
450 "long int too long to convert");
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
451 return (unsigned LONG_LONG) -1;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
452 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
453 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
454
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
455 return x;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
456 #endif
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
457 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
458 #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
459
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
460
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
461 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
462 convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **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
463 if (PyLong_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
464 *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
465 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
466 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
467 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
468 *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
469 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
470 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
471 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
472 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
473 if (PyLong_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
474 *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
475 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
476 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
477 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
478 *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
479 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
480 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
481 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
482 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
483 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
484 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
485 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
486
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
487 #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
488 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
489 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
490 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
491 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
492
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
493
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 /* Multiply by a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
496 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
497 mul1(PyLongObject *a, wdigit n)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 return muladd1(a, n, (digit)0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 /* Multiply by a single digit and add a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
504 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
505 muladd1(PyLongObject *a, wdigit n, wdigit extra)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
507 int size_a = ABS(a->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
508 PyLongObject *z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 twodigits carry = extra;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 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
516 z->ob_digit[i] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 }
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
519 z->ob_digit[i] = (digit) carry;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 /* Divide a long integer by a digit, returning both the quotient
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 (as function result) and the remainder (through *prem).
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 The sign of a is ignored; n should not be zero. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
527 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
528 divrem1(PyLongObject *a, wdigit n, digit *prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
530 int size = ABS(a->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
531 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 twodigits rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 assert(n > 0 && n <= MASK);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
536 z = _PyLong_New(size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 for (i = size; --i >= 0; ) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 rem = (rem << SHIFT) + a->ob_digit[i];
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
541 z->ob_digit[i] = (digit) (rem/n);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 rem %= n;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 }
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
544 *prem = (digit) rem;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 /* 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
549 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
550 If base is 8 or 16, add the proper prefix '0' or '0x'. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
552 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
553 long_format(PyObject *aa, int base, int addL)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
555 register PyLongObject *a = (PyLongObject *)aa;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
556 PyStringObject *str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 int i;
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
558 int size_a = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 char *p;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 int bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 char sign = '\0';
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
562
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
563 if (a == NULL || !PyLong_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
564 PyErr_BadInternalCall();
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
565 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
566 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 assert(base >= 2 && base <= 36);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 /* Compute a rough upper bound for the length of the string */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 i = base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 bits = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 while (i > 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 ++bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 i >>= 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 }
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
576 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
577 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 if (str == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
580 p = PyString_AS_STRING(str) + i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 *p = '\0';
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
582 if (addL)
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
583 *--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
584 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
585 sign = '-';
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
586
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
587 if (a->ob_size == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
588 *--p = '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
589 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
590 else if ((base & (base - 1)) == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
591 /* JRH: special case for power-of-2 bases */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
592 twodigits temp = a->ob_digit[0];
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
593 int bitsleft = SHIFT;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
594 int rem;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
595 int last = abs(a->ob_size);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
596 int basebits = 1;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
597 i = base;
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
598 while ((i >>= 1) > 1)
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
599 ++basebits;
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
600
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
601 i = 0;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
602 for (;;) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
603 while (bitsleft >= basebits) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
604 if ((temp == 0) && (i >= last - 1)) break;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
605 rem = temp & (base - 1);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
606 if (rem < 10)
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
607 rem += '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
608 else
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
609 rem += 'A' - 10;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
610 assert(p > PyString_AS_STRING(str));
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
611 *--p = (char) rem;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
612 bitsleft -= basebits;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
613 temp >>= basebits;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
614 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
615 if (++i >= last) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
616 if (temp == 0) break;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
617 bitsleft = 99;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
618 /* loop again to pick up final digits */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
619 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
620 else {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
621 temp = (a->ob_digit[i] << bitsleft) | temp;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
622 bitsleft += SHIFT;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
623 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 }
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
625 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
626 else {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
627 Py_INCREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
628 do {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
629 digit rem;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
630 PyLongObject *temp = divrem1(a, (digit)base, &rem);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
631 if (temp == NULL) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
632 Py_DECREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
633 Py_DECREF(str);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
634 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
635 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
636 if (rem < 10)
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
637 rem += '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
638 else
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
639 rem += 'A'-10;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
640 assert(p > PyString_AS_STRING(str));
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
641 *--p = (char) rem;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
642 Py_DECREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
643 a = temp;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
644 SIGCHECK({
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
645 Py_DECREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
646 Py_DECREF(str);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
647 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
648 })
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
649 } while (ABS(a->ob_size) != 0);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
650 Py_DECREF(a);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
651 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
652
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
653 if (base == 8) {
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
654 if (size_a != 0)
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
655 *--p = '0';
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
656 }
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
657 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
658 *--p = 'x';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
659 *--p = '0';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
660 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
661 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
662 *--p = '#';
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
663 *--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
664 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
665 *--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
666 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 if (sign)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 *--p = sign;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
669 if (p != PyString_AS_STRING(str)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
670 char *q = PyString_AS_STRING(str);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 assert(p > q);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 } while ((*q++ = *p++) != '\0');
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
674 q--;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
675 _PyString_Resize((PyObject **)&str,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
676 (int) (q - PyString_AS_STRING(str)));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
678 return (PyObject *)str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
681 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
682 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
683 {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 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
685 char *start, *orig_str = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
686 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
687
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
688 if ((base != 0 && base < 2) || base > 36) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
689 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
690 "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
691 return NULL;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
692 }
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
693 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
694 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 if (*str == '+')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 else if (*str == '-') {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 }
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
701 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
702 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 if (base == 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 if (str[0] != '0')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 base = 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 else if (str[1] == 'x' || str[1] == 'X')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 base = 16;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 base = 8;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 str += 2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
713 z = _PyLong_New(0);
8533
e5d06a602469 Add check in long-to-int conversion for at least one digit.
Guido van Rossum <guido@python.org>
parents: 8425
diff changeset
714 start = str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 for ( ; z != NULL; ++str) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 int k = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
717 PyLongObject *temp;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 if (*str <= '9')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 k = *str - '0';
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 else if (*str >= 'a')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 k = *str - 'a' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 else if (*str >= 'A')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 k = *str - 'A' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 if (k < 0 || k >= base)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 break;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 temp = muladd1(z, (digit)base, (digit)k);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
728 Py_DECREF(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 z = temp;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 }
8808
78785985a2ab Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents: 8533
diff changeset
731 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
732 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
733 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
734 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
735 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
736 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
737 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
738 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
739 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
740 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
741 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
742 goto onError;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
743 if (pend)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
744 *pend = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
745 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
746
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
747 onError:
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
748 PyErr_Format(PyExc_ValueError,
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
749 "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
750 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
751 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
752 }
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
753
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
754 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
755 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
756 {
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
757 char buffer[256];
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
758
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
759 if (length >= sizeof(buffer)) {
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
760 PyErr_SetString(PyExc_ValueError,
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
761 "long() literal too large to convert");
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
762 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
763 }
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
764 if (PyUnicode_EncodeDecimal(u, length, buffer, 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
765 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
766
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
767 return PyLong_FromString(buffer, NULL, base);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
769
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
770 /* forward */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
771 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
772 (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
773 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
774 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
775 PyLongObject **, PyLongObject **);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
776
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 /* Long division with remainder, top-level routine */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
779 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
780 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
781 PyLongObject **pdiv, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
783 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
784 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 if (size_b == 0) {
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
787 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
788 "long division or modulo by zero");
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
789 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 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
792 (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
793 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 /* |a| < |b|. */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
795 *pdiv = _PyLong_New(0);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
796 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
797 *prem = (PyLongObject *) a;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
798 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 if (size_b == 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 digit rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 z = divrem1(a, b->ob_digit[0], &rem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
803 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
804 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
805 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
807 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 z = x_divrem(a, b, prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
809 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
810 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
811 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 /* Set the signs.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 The quotient z has the sign of a*b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 the remainder r has the sign of a,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 so a = b*z + r. */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
816 if ((a->ob_size < 0) != (b->ob_size < 0))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
817 z->ob_size = -(z->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
818 if (a->ob_size < 0 && (*prem)->ob_size != 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
819 (*prem)->ob_size = -((*prem)->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
820 *pdiv = z;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
821 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
823
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
824 /* Unsigned long division with remainder -- the algorithm */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
825
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
826 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
827 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
829 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
830 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
831 PyLongObject *v = mul1(v1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
832 PyLongObject *w = mul1(w1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
833 PyLongObject *a;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
834 int j, k;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 if (v == NULL || w == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
837 Py_XDECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
838 Py_XDECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
843 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
844 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
845
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
846 size_v = ABS(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
847 a = _PyLong_New(size_v - size_w + 1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
848
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 twodigits q;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
852 stwodigits carry = 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
855 SIGCHECK({
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
856 Py_DECREF(a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 a = NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 break;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
859 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 if (vj == w->ob_digit[size_w-1])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 q = MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 w->ob_digit[size_w-1];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
865
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 while (w->ob_digit[size_w-2]*q >
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 ((
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 ((twodigits)vj << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 + v->ob_digit[j-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
870 - q*w->ob_digit[size_w-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 ) << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 + v->ob_digit[j-2])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 --q;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
874
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
876 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
877 digit zz = (digit) (z >> SHIFT);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
878 carry += v->ob_digit[i+k] - z
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
879 + ((twodigits)zz << SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 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
881 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
882 carry, SHIFT);
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
883 carry -= zz;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
885
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
886 if (i+k < size_v) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 carry += v->ob_digit[i+k];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 v->ob_digit[i+k] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
890
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
891 if (carry == 0)
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
892 a->ob_digit[k] = (digit) q;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
893 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 assert(carry == -1);
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
895 a->ob_digit[k] = (digit) q-1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
897 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 carry += v->ob_digit[i+k] + w->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 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
900 carry = Py_ARITHMETIC_RIGHT_SHIFT(
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
901 BASE_TWODIGITS_TYPE,
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
902 carry, SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
904 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 } /* for j, k */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
906
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
907 if (a == NULL)
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
908 *prem = NULL;
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
909 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
910 a = long_normalize(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
911 *prem = divrem1(v, d, &d);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
912 /* d receives the (unused) remainder */
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
913 if (*prem == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
914 Py_DECREF(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
915 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
916 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
918 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
919 Py_DECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
920 return a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
922
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 /* Methods */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
924
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 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
926 long_dealloc(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12341
diff changeset
928 PyObject_DEL(v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
929 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
930
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
931 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
932 long_repr(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
933 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
934 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
935 }
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
936
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
937 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
938 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
939 {
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
940 return long_format(v, 10, 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
942
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 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
944 long_compare(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 int sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
947
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
948 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
949 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
950 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
951 else
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
952 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
953 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 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
955 int i = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 sign = 0;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
960 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 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
962 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
963 sign = -sign;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
964 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
966 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
968
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
969 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
970 long_hash(PyLongObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
971 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
972 long x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
973 int i, sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
974
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
975 /* 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
976 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
977 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
978 i = v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
979 sign = 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
980 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
981 if (i < 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
982 sign = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
983 i = -(i);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
984 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
985 while (--i >= 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
986 /* Force a 32-bit circular shift */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
987 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
988 x += v->ob_digit[i];
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
989 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
990 x = x * sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
991 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
992 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
993 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
994 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
995
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
996
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 /* Add the absolute values of two long integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
998
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
999 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
1000 x_add(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1002 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
1003 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 digit carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 if (size_a < size_b) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1009 { PyLongObject *temp = a; a = b; b = temp; }
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1010 { int size_temp = size_a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1011 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1012 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1014 z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 carry += a->ob_digit[i] + b->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 carry += a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 /* Subtract the absolute values of two integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1033 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
1034 x_sub(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1036 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
1037 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 digit borrow = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1045 { PyLongObject *temp = a; a = b; b = temp; }
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1046 { int size_temp = size_a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1047 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1048 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 else if (size_a == size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 /* Find highest digit where a and b differ: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052 i = size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 if (i < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1056 return _PyLong_New(0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 if (a->ob_digit[i] < b->ob_digit[i]) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1059 { PyLongObject *temp = a; a = b; b = temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 size_a = size_b = i+1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1063 z = _PyLong_New(size_a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 /* The following assumes unsigned arithmetic
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 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
1069 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 borrow >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072 borrow &= 1; /* Keep only one sign bit */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 borrow = a->ob_digit[i] - borrow;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 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
1078 borrow &= 1; /* Keep only one sign bit */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 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
1081 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
1082 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1085
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1086 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
1087 long_add(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 {
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
1089 PyLongObject *a, *b, *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090
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
1091 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
1092
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094 if (b->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 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
1096 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
1097 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 z = x_sub(b, a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1102 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 }
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
1108 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
1109 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1110 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1113 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
1114 long_sub(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 {
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
1116 PyLongObject *a, *b, *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117
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
1118 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
1119
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1122 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1124 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
1125 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
1126 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 }
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
1134 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
1135 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1136 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1139 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
1140 long_repeat(PyObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 {
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
1142 /* sequence * long */
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1143 long n = PyLong_AsLong((PyObject *) 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
1144 if (n == -1 && 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
1145 return 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
1146 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
1147 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, n);
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1148 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1149
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1150 static PyObject *
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1151 long_mul(PyLongObject *v, 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
1152 {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1153 PyLongObject *a, *b, *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 int size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 int size_b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1157
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
1158 if (v->ob_type->tp_as_sequence &&
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1159 v->ob_type->tp_as_sequence->sq_repeat) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1160 return long_repeat((PyObject *)v, 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
1161 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1162 else if (w->ob_type->tp_as_sequence &&
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1163 w->ob_type->tp_as_sequence->sq_repeat) {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1164 return long_repeat((PyObject *)w, 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
1165 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1166
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1167 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
1168
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1169 size_a = ABS(a->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1170 size_b = ABS(b->ob_size);
12341
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1171 if (size_a > size_b) {
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1172 /* we are faster with the small object on the left */
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1173 int hold_sa = size_a;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1174 PyLongObject *hold_a = a;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1175 size_a = size_b;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1176 size_b = hold_sa;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1177 a = b;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1178 b = hold_a;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1179 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1180 z = _PyLong_New(size_a + size_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
1181 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
1182 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
1183 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 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
1185 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1186 for (i = 0; i < z->ob_size; ++i)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1187 z->ob_digit[i] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 twodigits carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 twodigits f = a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191 int j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1193 SIGCHECK({
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
1194 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
1195 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1196 Py_DECREF(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 return NULL;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1198 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 for (j = 0; j < size_b; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 carry += z->ob_digit[i+j] + b->ob_digit[j] * f;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
1201 z->ob_digit[i+j] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 for (; carry != 0; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 assert(i+j < z->ob_size);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 carry += z->ob_digit[i+j];
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
1207 z->ob_digit[i+j] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 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
1212 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 if (b->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
1214 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
1215 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
1216 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1217 return (PyObject *) long_normalize(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1220 /* The / and % operators are now defined in terms of divmod().
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1221 The expression a mod b has the value a - b*floor(a/b).
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1222 The long_divrem function gives the remainder after division of
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 |a| by |b|, with the sign of a. This is also expressed
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 as a - b*trunc(a/b), if trunc truncates towards zero.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 Some examples:
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 a b a rem b a mod b
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 13 10 3 3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 -13 10 -3 7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 13 -10 3 -7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1230 -13 -10 -3 -3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 So, to get from rem to mod, we have to add b if a and b
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1232 have different signs. We then subtract one from the 'div'
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1233 part of the outcome to keep the invariant intact. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1235 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
1236 l_divmod(PyLongObject *v, PyLongObject *w,
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1237 PyLongObject **pdiv, PyLongObject **pmod)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1238 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1239 PyLongObject *div, *mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1240
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1241 if (long_divrem(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1242 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
1243 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
1244 (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
1245 PyLongObject *temp;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1246 PyLongObject *one;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1247 temp = (PyLongObject *) long_add(mod, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1248 Py_DECREF(mod);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1249 mod = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1250 if (mod == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1251 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1252 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1253 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1254 one = (PyLongObject *) PyLong_FromLong(1L);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1255 if (one == NULL ||
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1256 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1257 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1258 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1259 Py_XDECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1260 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1261 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1262 Py_DECREF(one);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1263 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1264 div = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1265 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1266 *pdiv = div;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1267 *pmod = mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1268 return 0;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1269 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1270
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1271 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
1272 long_div(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1273 {
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
1274 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
1275
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1276 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
1277
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1278 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
1279 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
1280 Py_DECREF(b);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1281 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
1282 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1283 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
1284 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1285 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1286 return (PyObject *)div;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1287 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1288
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1289 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
1290 long_mod(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1291 {
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
1292 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
1293
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1294 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
1295
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1296 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
1297 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
1298 Py_DECREF(b);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1299 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
1300 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1301 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
1302 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1303 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1304 return (PyObject *)mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1305 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1306
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1307 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
1308 long_divmod(PyObject *v, PyObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 {
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
1310 PyLongObject *a, *b, *div, *mod;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1311 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
1312
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1313 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
1314
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1315 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
1316 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
1317 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 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
1319 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1320 z = PyTuple_New(2);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 if (z != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1322 PyTuple_SetItem(z, 0, (PyObject *) div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1323 PyTuple_SetItem(z, 1, (PyObject *) mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1326 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1327 Py_DECREF(mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 }
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
1329 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
1330 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 return z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1334 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
1335 long_pow(PyObject *v, PyObject *w, PyObject *x)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1336 {
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
1337 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
1338 PyObject *c;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1339 PyLongObject *z, *div, *mod;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1340 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
1341
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1342 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
1343 if (PyLong_Check(x) || Py_None == 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
1344 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
1345 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
1346 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1347 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
1348 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
1349 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1350 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
1351 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
1352 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
1353 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
1354 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
1355 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1356
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1357 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
1358 if (size_b < 0) {
15041
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1359 if (a->ob_size)
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1360 PyErr_SetString(PyExc_ValueError,
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1361 "long integer to a negative power");
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1362 else
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1363 PyErr_SetString(PyExc_ZeroDivisionError,
7eefde90ec0e SF bug 115831 and Ping's SF patch 101751, 0.0**-2.0 returns inf rather than
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1364 "zero to a negative power");
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
1365 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
1366 goto error;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1367 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1368 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
1369 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
1370 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
1371 int j;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1372
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1373 for (j = 0; j < SHIFT; ++j) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1374 PyLongObject *temp;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1375
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1376 if (bi & 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1377 temp = (PyLongObject *)long_mul(z, a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1378 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
1379 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
1380 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
1381 &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
1382 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
1383 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
1384 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
1385 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1386 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1387 Py_DECREF(temp);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1388 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1389 }
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1390 z = temp;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1391 if (z == NULL)
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1392 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1393 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1394 bi >>= 1;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1395 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
1396 break;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1397 temp = (PyLongObject *)long_mul(a, a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1398 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
1399 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
1400 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
1401 &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
1402 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
1403 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
1404 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
1405 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1406 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1407 Py_DECREF(temp);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1408 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1409 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1410 a = temp;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1411 if (a == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1412 Py_DECREF(z);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1413 z = NULL;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1414 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1415 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1416 }
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1417 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
1418 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1419 }
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
1420 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
1421 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
1422 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
1423 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
1424 }
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
1425 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1426 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1427 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
1428 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
1429 }
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1430 }
11380
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
1431 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
1432 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
1433 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
1434 Py_DECREF(c);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1435 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1436 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1438 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
1439 long_invert(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1440 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1441 /* Implement ~x as -(x+1) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1442 PyLongObject *x;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1443 PyLongObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1444 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
1445 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
1446 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1447 x = (PyLongObject *) long_add(v, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1448 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
1449 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
1450 return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1451 if (x->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
1452 x->ob_size = -(x->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1453 return (PyObject *)x;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1454 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1455
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1456 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
1457 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
1458 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1459 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1460 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
1461 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1462
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1463 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
1464 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
1465 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1466 PyLongObject *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
1467 int i, n;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1468 n = ABS(v->ob_size);
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1469 if (n == 0) {
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1470 /* -0 == 0 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1471 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1472 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
1473 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1474 z = _PyLong_New(ABS(n));
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1475 if (z == NULL)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1476 return NULL;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1477 for (i = 0; i < n; i++)
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1478 z->ob_digit[i] = v->ob_digit[i];
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1479 z->ob_size = -(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1480 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
1481 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1482
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1483 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
1484 long_abs(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 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
1487 return long_neg(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
1488 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1489 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1490 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
1491 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1492 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1493
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1494 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
1495 long_nonzero(PyLongObject *v)
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1496 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1497 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
1498 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1499
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1500 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
1501 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
1502 {
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
1503 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
1504 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
1505 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
1506 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
1507 digit lomask, himask;
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1508
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
1509 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
1510
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1511 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
1512 /* 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
1513 PyLongObject *a1, *a2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1514 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
1515 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
1516 goto rshift_error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1517 a2 = (PyLongObject *) long_rshift(a1, b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1518 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
1519 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
1520 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
1521 z = (PyLongObject *) long_invert(a2);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1522 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
1523 }
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
1524 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
1525
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1526 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
1527 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
1528 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
1529 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
1530 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
1531 "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
1532 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
1533 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1534 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
1535 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
1536 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
1537 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
1538 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
1539 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
1540 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
1541 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1542 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
1543 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
1544 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
1545 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
1546 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
1547 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
1548 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
1549 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
1550 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
1551 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
1552 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
1553 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
1554 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
1555 (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
1556 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1557 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
1558 }
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
1559 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
1560 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
1561 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
1562 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
1563
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1564 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1565
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1566 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
1567 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
1568 {
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
1569 /* 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
1570 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
1571 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
1572 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
1573 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
1574 twodigits accum;
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1575
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
1576 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
1577
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1578 shiftby = PyLong_AsLong((PyObject *)b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1579 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
1580 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
1581 if (shiftby < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1582 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
1583 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
1584 }
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
1585 if ((long)(int)shiftby != shiftby) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1586 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1587 "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
1588 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
1589 }
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
1590 /* 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
1591 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
1592 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
1593
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1594 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
1595 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
1596 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
1597 ++newsize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1598 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
1599 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
1600 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
1601 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
1602 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
1603 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
1604 z->ob_digit[i] = 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
1605 accum = 0;
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1606 for (i = wordshift, j = 0; j < oldsize; 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
1607 accum |= a->ob_digit[j] << 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
1608 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
1609 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
1610 }
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
1611 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
1612 z->ob_digit[newsize-1] = (digit)accum;
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1613 else
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1614 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
1615 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
1616 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
1617 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
1618 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
1619 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
1620 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1621
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1622
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1623 /* 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
1624
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1625 #define MAX(x, y) ((x) < (y) ? (y) : (x))
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1626 #define MIN(x, y) ((x) > (y) ? (y) : (x))
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1627
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1628 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
1629 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
1630 int op, /* '&', '|', '^' */
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1631 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
1632 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1633 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
1634 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
1635 int size_a, size_b, size_z;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1636 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
1637 int i;
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
1638 digit diga, digb;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1639 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
1640
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1641 if (a->ob_size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1642 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
1643 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
1644 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1645 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1646 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
1647 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
1648 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1649 if (b->ob_size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1650 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
1651 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
1652 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1653 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1654 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
1655 maskb = 0;
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1656 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1657
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1658 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
1659 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
1660 case '^':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1661 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
1662 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
1663 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
1664 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1665 break;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1666 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
1667 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
1668 op = '|';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1669 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
1670 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
1671 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
1672 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1673 break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1674 case '|':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1675 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
1676 op = '&';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1677 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
1678 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
1679 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
1680 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1681 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
1682 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1683
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1684 /* 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
1685 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
1686 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
1687 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
1688 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
1689 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
1690 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
1691 whose length should be ignored.
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1692 */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1693
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1694 size_a = a->ob_size;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1695 size_b = b->ob_size;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1696 size_z = op == '&'
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1697 ? (maska
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1698 ? size_b
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1699 : (maskb ? size_a : MIN(size_a, size_b)))
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1700 : MAX(size_a, size_b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1701 z = _PyLong_New(size_z);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1702 if (a == NULL || b == NULL || z == NULL) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1703 Py_XDECREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1704 Py_XDECREF(b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1705 Py_XDECREF(z);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1706 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1707 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1708
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1709 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
1710 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
1711 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
1712 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
1713 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
1714 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
1715 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
1716 }
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1717 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1718
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1719 Py_DECREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1720 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
1721 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
1722 if (negz == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1723 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
1724 v = long_invert(z);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1725 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
1726 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
1727 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1728
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1729 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
1730 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
1731 {
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
1732 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
1733 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
1734 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
1735 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
1736 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
1737 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
1738 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
1739 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1740
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1741 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
1742 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
1743 {
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
1744 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
1745 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
1746 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
1747 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
1748 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
1749 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
1750 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
1751 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1752
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1753 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
1754 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
1755 {
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
1756 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
1757 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
1758 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
1759 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
1760 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
1761 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
1762 return c;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1763 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1764
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1765 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
1766 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
1767 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1768 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
1769 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1770 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1771 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1772 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1773 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
1774 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1775
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1776 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
1777 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
1778 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1779 long x;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1780 x = PyLong_AsLong(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1781 if (PyErr_Occurred())
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1782 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1783 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
1784 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1785
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1786 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
1787 long_long(PyObject *v)
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1788 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1789 Py_INCREF(v);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1790 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1791 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1792
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1793 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
1794 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
1795 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
1796 double result;
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
1797 PyFPE_START_PROTECT("long_float", return 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1798 result = PyLong_AsDouble(v);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5118
diff changeset
1799 PyFPE_END_PROTECT(result)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1800 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
1801 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1802
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1803 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
1804 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
1805 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1806 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
1807 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1808
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1809 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
1810 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
1811 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1812 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
1813 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
1814
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1815 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
1816 (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
1817 (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
1818 (binaryfunc) long_mul, /*nb_multiply*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1819 (binaryfunc) long_div, /*nb_divide*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1820 (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
1821 (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
1822 (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
1823 (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
1824 (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
1825 (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
1826 (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
1827 (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
1828 (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
1829 (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
1830 (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
1831 (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
1832 (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
1833 (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
1834 (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
1835 (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
1836 (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
1837 (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
1838 (unaryfunc) long_hex, /*nb_hex*/
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
1839 0, /*nb_inplace_add*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1840 0, /*nb_inplace_subtract*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1841 0, /*nb_inplace_multiply*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1842 0, /*nb_inplace_divide*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1843 0, /*nb_inplace_remainder*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1844 0, /*nb_inplace_power*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1845 0, /*nb_inplace_lshift*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1846 0, /*nb_inplace_rshift*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1847 0, /*nb_inplace_and*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1848 0, /*nb_inplace_xor*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1849 0, /*nb_inplace_or*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1850 };
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1851
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1852 PyTypeObject PyLong_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1853 PyObject_HEAD_INIT(&PyType_Type)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1854 0,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1855 "long int",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1856 sizeof(PyLongObject) - sizeof(digit),
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1857 sizeof(digit),
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1858 (destructor)long_dealloc, /*tp_dealloc*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1859 0, /*tp_print*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1860 0, /*tp_getattr*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1861 0, /*tp_setattr*/
13418
85520c220067 Got RID of redundant coercions in longobject.c (as spotted by Greg
Tim Peters <tim.peters@gmail.com>
parents: 13406
diff changeset
1862 (cmpfunc)long_compare, /*tp_compare*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1863 (reprfunc)long_repr, /*tp_repr*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1864 &long_as_number, /*tp_as_number*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1865 0, /*tp_as_sequence*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1866 0, /*tp_as_mapping*/
13418
85520c220067 Got RID of redundant coercions in longobject.c (as spotted by Greg
Tim Peters <tim.peters@gmail.com>
parents: 13406
diff changeset
1867 (hashfunc)long_hash, /*tp_hash*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1868 0, /*tp_call*/
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1869 (reprfunc)long_str, /*tp_str*/
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
1870 0, /*tp_getattro*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1871 0, /*tp_setattro*/
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1872 0, /*tp_as_buffer*/
15946
4f621708514b Rich comparisons fall-out:
Guido van Rossum <guido@python.org>
parents: 15725
diff changeset
1873 Py_TPFLAGS_CHECKTYPES /*tp_flags*/
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1874 };