annotate Objects/longobject.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 1d1a01d956a2
children 928c1282e5e2
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
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
9 #include <ctype.h>
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
11 #define ABS(x) ((x) < 0 ? -(x) : (x))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
12
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
13 /* Forward */
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
14 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
15 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
16 static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
17 static PyLongObject *divrem1(PyLongObject *, digit, digit *);
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
18 static PyObject *long_format(PyObject *aa, int base, int addL);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
19
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
20 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
21
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
22 #define SIGCHECK(PyTryBlock) \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
23 if (--ticker < 0) { \
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
24 ticker = 100; \
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
25 if (PyErr_CheckSignals()) { PyTryBlock; } \
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
26 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
27
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 /* Normalize (remove leading zeros from) a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 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
30 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
31
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
32 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
33 long_normalize(register PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
35 int j = ABS(v->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 register int i = j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 while (i > 0 && v->ob_digit[i-1] == 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 --i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 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
41 v->ob_size = (v->ob_size < 0) ? -(i) : i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 return v;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 }
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 /* Allocate a new long int object with size digits.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 Return NULL and set exception if we run out of memory. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
48 PyLongObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
49 _PyLong_New(int size)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
51 return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
54 PyObject *
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
55 _PyLong_Copy(PyLongObject *src)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
56 {
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
57 PyLongObject *result;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
58 int i;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
59
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
60 assert(src != NULL);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
61 i = src->ob_size;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
62 if (i < 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
63 i = -(i);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
64 result = _PyLong_New(i);
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
65 if (result != NULL) {
22147
1d1a01d956a2 backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21968
diff changeset
66 result->ob_size = src->ob_size;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
67 while (--i >= 0)
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
68 result->ob_digit[i] = src->ob_digit[i];
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
69 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
70 return (PyObject *)result;
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
71 }
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
72
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 /* Create a new long int object from a C long int */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
75 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
76 PyLong_FromLong(long ival)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
78 PyLongObject *v;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
79 unsigned long t; /* unsigned so >> doesn't propagate sign bit */
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
80 int ndigits = 0;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
81 int negative = 0;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
82
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
83 if (ival < 0) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
84 ival = -ival;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
85 negative = 1;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
86 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
87
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
88 /* Count the number of Python digits.
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
89 We used to pick 5 ("big enough for anything"), but that's a
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
90 waste of time and space given that 5*15 = 75 bits are rarely
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
91 needed. */
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
92 t = (unsigned long)ival;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
93 while (t) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
94 ++ndigits;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
95 t >>= SHIFT;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
96 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
97 v = _PyLong_New(ndigits);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if (v != NULL) {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
99 digit *p = v->ob_digit;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
100 v->ob_size = negative ? -ndigits : ndigits;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
101 t = (unsigned long)ival;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
102 while (t) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
103 *p++ = (digit)(t & MASK);
4587
09d94e9eb101 Fix newlongobject so it will work for 64-bit as well as 32-bit hardware
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
104 t >>= SHIFT;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
107 return (PyObject *)v;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
110 /* 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
111
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
112 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
113 PyLong_FromUnsignedLong(unsigned long ival)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
114 {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
115 PyLongObject *v;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
116 unsigned long t;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
117 int ndigits = 0;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
118
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
119 /* Count the number of Python digits. */
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
120 t = (unsigned long)ival;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
121 while (t) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
122 ++ndigits;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
123 t >>= SHIFT;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
124 }
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
125 v = _PyLong_New(ndigits);
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
126 if (v != NULL) {
18114
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
127 digit *p = v->ob_digit;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
128 v->ob_size = ndigits;
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
129 while (ival) {
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
130 *p++ = (digit)(ival & MASK);
31cd0db0f09f PyLong_From{Unsigned,}Long: count the # of digits first, so no more space
Tim Peters <tim.peters@gmail.com>
parents: 18110
diff changeset
131 ival >>= SHIFT;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
132 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
133 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
134 return (PyObject *)v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
135 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
136
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
137 /* 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
138
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
139 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
140 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
141 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
142 PyLongObject *v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
143 double frac;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
144 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
145 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
146 if (Py_IS_INFINITY(dval)) {
11331
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
147 PyErr_SetString(PyExc_OverflowError,
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
148 "cannot convert float infinity to long");
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
149 return NULL;
4fdc9c1ecaf6 Patch by Tim Peters fixing PR#89:
Guido van Rossum <guido@python.org>
parents: 9969
diff changeset
150 }
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
151 if (dval < 0.0) {
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
152 neg = 1;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
153 dval = -dval;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
154 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
155 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
156 if (expo <= 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
157 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
158 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
159 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
160 if (v == NULL)
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
161 return NULL;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
162 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
163 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
164 long bits = (long)frac;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
165 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
166 frac = frac - (double)bits;
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
167 frac = ldexp(frac, SHIFT);
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
168 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
169 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
170 v->ob_size = -(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
171 return (PyObject *)v;
254
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
172 }
71b0bd5ed162 Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents: 250
diff changeset
173
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 /* Get a C long int from a long int object.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 Returns -1 and sets an error condition if overflow occurs. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 long
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
178 PyLong_AsLong(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
180 /* This version by Tim Peters */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
181 register PyLongObject *v;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
182 unsigned long x, prev;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 int i, sign;
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
184
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
185 if (vv == NULL || !PyLong_Check(vv)) {
19925
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
186 if (vv != NULL && PyInt_Check(vv))
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
187 return PyInt_AsLong(vv);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
188 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
191 v = (PyLongObject *)vv;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 i = v->ob_size;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 x = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 if (i < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 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
197 i = -(i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 while (--i >= 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 prev = x;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 x = (x << SHIFT) + v->ob_digit[i];
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
202 if ((x >> SHIFT) != prev)
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
203 goto overflow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 }
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
205 /* 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
206 * 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
207 * 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
208 * than the sign bit).
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
209 */
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
210 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
211 goto overflow;
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
212 return (long)x * sign;
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
213
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
214 overflow:
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
215 PyErr_SetString(PyExc_OverflowError,
19897
84376356ff89 based upon a suggestion in c.l.py, this slight expansion of the
Skip Montanaro <skip@pobox.com>
parents: 19882
diff changeset
216 "long int too large to convert to int");
8425
e2d41bf1a0f0 Subject: Buglet in PyLong_AsLong
Guido van Rossum <guido@python.org>
parents: 7981
diff changeset
217 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
220 /* 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
221 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
222
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
223 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
224 PyLong_AsUnsignedLong(PyObject *vv)
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
225 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
226 register PyLongObject *v;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
227 unsigned long x, prev;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
228 int i;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
229
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
230 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
231 PyErr_BadInternalCall();
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
232 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
233 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
234 v = (PyLongObject *)vv;
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
235 i = v->ob_size;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
236 x = 0;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
237 if (i < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
238 PyErr_SetString(PyExc_OverflowError,
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
239 "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
240 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
241 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
242 while (--i >= 0) {
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
243 prev = x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
244 x = (x << SHIFT) + v->ob_digit[i];
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
245 if ((x >> SHIFT) != prev) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
246 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
247 "long int too large to convert");
4870
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
248 return (unsigned long) -1;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
249 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
250 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
251 return x;
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
252 }
68f4134869db Added PyLong_FromUnsignedLong() and PyLong_AsUnsignedLong().
Guido van Rossum <guido@python.org>
parents: 4587
diff changeset
253
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
254 PyObject *
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
255 _PyLong_FromByteArray(const unsigned char* bytes, size_t n,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
256 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
257 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
258 const unsigned char* pstartbyte;/* LSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
259 int incr; /* direction to move pstartbyte */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
260 const unsigned char* pendbyte; /* MSB of bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
261 size_t numsignificantbytes; /* number of bytes that matter */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
262 size_t ndigits; /* number of Python long digits */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
263 PyLongObject* v; /* result */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
264 int idigit = 0; /* next free index in v->ob_digit */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
265
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
266 if (n == 0)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
267 return PyLong_FromLong(0L);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
268
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
269 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
270 pstartbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
271 pendbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
272 incr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
273 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
274 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
275 pstartbyte = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
276 pendbyte = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
277 incr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
278 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
279
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
280 if (is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
281 is_signed = *pendbyte >= 0x80;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
282
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
283 /* Compute numsignificantbytes. This consists of finding the most
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
284 significant byte. Leading 0 bytes are insignficant if the number
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
285 is positive, and leading 0xff bytes if negative. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
286 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
287 size_t i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
288 const unsigned char* p = pendbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
289 const int pincr = -incr; /* search MSB to LSB */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
290 const unsigned char insignficant = is_signed ? 0xff : 0x00;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
291
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
292 for (i = 0; i < n; ++i, p += pincr) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
293 if (*p != insignficant)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
294 break;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
295 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
296 numsignificantbytes = n - i;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
297 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
298 actually has 2 significant bytes. OTOH, 0xff0001 ==
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
299 -0x00ffff, so we wouldn't *need* to bump it there; but we
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
300 do for 0xffff = -0x0001. To be safe without bothering to
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
301 check every case, bump it regardless. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
302 if (is_signed && numsignificantbytes < n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
303 ++numsignificantbytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
304 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
305
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
306 /* How many Python long digits do we need? We have
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
307 8*numsignificantbytes bits, and each Python long digit has SHIFT
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
308 bits, so it's the ceiling of the quotient. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
309 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
310 if (ndigits > (size_t)INT_MAX)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
311 return PyErr_NoMemory();
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
312 v = _PyLong_New((int)ndigits);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
313 if (v == NULL)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
314 return NULL;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
315
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
316 /* Copy the bits over. The tricky parts are computing 2's-comp on
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
317 the fly for signed numbers, and dealing with the mismatch between
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
318 8-bit bytes and (probably) 15-bit Python digits.*/
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
319 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
320 size_t i;
18110
e21cd4135f3d _PyLong_FromByteArray: changed decl of "carry" to match "thisbyte". No
Tim Peters <tim.peters@gmail.com>
parents: 18109
diff changeset
321 twodigits carry = 1; /* for 2's-comp calculation */
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
322 twodigits accum = 0; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
323 unsigned int accumbits = 0; /* number of bits in accum */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
324 const unsigned char* p = pstartbyte;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
325
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
326 for (i = 0; i < numsignificantbytes; ++i, p += incr) {
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
327 twodigits thisbyte = *p;
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
328 /* Compute correction for 2's comp, if needed. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
329 if (is_signed) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
330 thisbyte = (0xff ^ thisbyte) + carry;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
331 carry = thisbyte >> 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
332 thisbyte &= 0xff;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
333 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
334 /* Because we're going LSB to MSB, thisbyte is
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
335 more significant than what's already in accum,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
336 so needs to be prepended to accum. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
337 accum |= thisbyte << accumbits;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
338 accumbits += 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
339 if (accumbits >= SHIFT) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
340 /* There's enough to fill a Python digit. */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
341 assert(idigit < (int)ndigits);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
342 v->ob_digit[idigit] = (digit)(accum & MASK);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
343 ++idigit;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
344 accum >>= SHIFT;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
345 accumbits -= SHIFT;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
346 assert(accumbits < SHIFT);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
347 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
348 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
349 assert(accumbits < SHIFT);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
350 if (accumbits) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
351 assert(idigit < (int)ndigits);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
352 v->ob_digit[idigit] = (digit)accum;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
353 ++idigit;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
354 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
355 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
356
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
357 v->ob_size = is_signed ? -idigit : idigit;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
358 return (PyObject *)long_normalize(v);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
359 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
360
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
361 int
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
362 _PyLong_AsByteArray(PyLongObject* v,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
363 unsigned char* bytes, size_t n,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
364 int little_endian, int is_signed)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
365 {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
366 int i; /* index into v->ob_digit */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
367 int ndigits; /* |v->ob_size| */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
368 twodigits accum; /* sliding register */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
369 unsigned int accumbits; /* # bits in accum */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
370 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
371 twodigits carry; /* for computing 2's-comp */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
372 size_t j; /* # bytes filled */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
373 unsigned char* p; /* pointer to next byte in bytes */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
374 int pincr; /* direction to move p */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
375
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
376 assert(v != NULL && PyLong_Check(v));
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
377
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
378 if (v->ob_size < 0) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
379 ndigits = -(v->ob_size);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
380 if (!is_signed) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
381 PyErr_SetString(PyExc_TypeError,
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
382 "can't convert negative long to unsigned");
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
383 return -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
384 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
385 do_twos_comp = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
386 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
387 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
388 ndigits = v->ob_size;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
389 do_twos_comp = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
390 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
391
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
392 if (little_endian) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
393 p = bytes;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
394 pincr = 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
395 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
396 else {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
397 p = bytes + n - 1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
398 pincr = -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
399 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
400
18108
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
401 /* Copy over all the Python digits.
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
402 It's crucial that every Python digit except for the MSD contribute
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
403 exactly SHIFT bits to the total, so first assert that the long is
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
404 normalized. */
8cf72b187b3a _PyLong_AsByteArray: added assert that the input is normalized. This is
Tim Peters <tim.peters@gmail.com>
parents: 18107
diff changeset
405 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
406 j = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
407 accum = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
408 accumbits = 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
409 carry = do_twos_comp ? 1 : 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
410 for (i = 0; i < ndigits; ++i) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
411 twodigits thisdigit = v->ob_digit[i];
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
412 if (do_twos_comp) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
413 thisdigit = (thisdigit ^ MASK) + carry;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
414 carry = thisdigit >> SHIFT;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
415 thisdigit &= MASK;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
416 }
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
417 /* Because we're going LSB to MSB, thisdigit is more
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
418 significant than what's already in accum, so needs to be
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
419 prepended to accum. */
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
420 accum |= thisdigit << accumbits;
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
421 accumbits += SHIFT;
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
422
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
423 /* The most-significant digit may be (probably is) at least
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
424 partly empty. */
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
425 if (i == ndigits - 1) {
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
426 /* Count # of sign bits -- they needn't be stored,
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
427 * although for signed conversion we need later to
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
428 * make sure at least one sign bit gets stored.
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
429 * First shift conceptual sign bit to real sign bit.
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
430 */
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
431 stwodigits s = (stwodigits)(thisdigit <<
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
432 (8*sizeof(stwodigits) - SHIFT));
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
433 unsigned int nsignbits = 0;
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
434 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
435 ++nsignbits;
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
436 s <<= 1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
437 }
18115
2ecaf9e21a04 _PyLong_AsByteArray: simplify the logic for dealing with the most-
Tim Peters <tim.peters@gmail.com>
parents: 18114
diff changeset
438 accumbits -= nsignbits;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
439 }
18091
a001fcb76177 _PyLong_{As,From}ByteArray: Minor code rearrangement aimed at improving
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
440
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
441 /* Store as many bytes as possible. */
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
442 while (accumbits >= 8) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
443 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
444 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
445 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
446 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
447 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
448 accumbits -= 8;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
449 accum >>= 8;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
450 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
451 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
452
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
453 /* Store the straggler (if any). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
454 assert(accumbits < 8);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
455 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18082
diff changeset
456 if (accumbits > 0) {
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
457 if (j >= n)
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
458 goto Overflow;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
459 ++j;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
460 if (do_twos_comp) {
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
461 /* Fill leading bits of the byte with sign bits
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
462 (appropriately pretending that the long had an
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
463 infinite supply of sign bits). */
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
464 accum |= (~(twodigits)0) << accumbits;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
465 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
466 *p = (unsigned char)(accum & 0xff);
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
467 p += pincr;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
468 }
18109
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
469 else if (j == n && n > 0 && is_signed) {
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
470 /* The main loop filled the byte array exactly, so the code
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
471 just above didn't get to ensure there's a sign bit, and the
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
472 loop below wouldn't add one either. Make sure a sign bit
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
473 exists. */
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
474 unsigned char msb = *(p - pincr);
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
475 int sign_bit_set = msb >= 0x80;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
476 assert(accumbits == 0);
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
477 if (sign_bit_set == do_twos_comp)
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
478 return 0;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
479 else
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
480 goto Overflow;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
481 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
482
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
483 /* Fill remaining bytes with copies of the sign bit. */
18109
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
484 {
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
485 unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
486 for ( ; j < n; ++j, p += pincr)
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
487 *p = signbyte;
be2ddb25b698 _PyLong_AsByteArray: Don't do the "delicate overflow" check unless it's
Tim Peters <tim.peters@gmail.com>
parents: 18108
diff changeset
488 }
18082
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
489
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
490 return 0;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
491
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
492 Overflow:
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
493 PyErr_SetString(PyExc_OverflowError, "long too big to convert");
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
494 return -1;
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
495
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
496 }
dd09f11fa40d Two new private longobject API functions,
Tim Peters <tim.peters@gmail.com>
parents: 15946
diff changeset
497
19671
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
498 double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
499 _PyLong_AsScaledDouble(PyObject *vv, int *exponent)
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
500 {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
501 /* NBITS_WANTED should be > the number of bits in a double's precision,
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
502 but small enough so that 2**NBITS_WANTED is within the normal double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
503 range. nbitsneeded is set to 1 less than that because the most-significant
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
504 Python digit contains at least 1 significant bit, but we don't want to
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
505 bother counting them (catering to the worst case cheaply).
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
506
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
507 57 is one more than VAX-D double precision; I (Tim) don't know of a double
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
508 format with more precision than that; it's 1 larger so that we add in at
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
509 least one round bit to stand in for the ignored least-significant bits.
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
510 */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
511 #define NBITS_WANTED 57
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
512 PyLongObject *v;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
513 double x;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
514 const double multiplier = (double)(1L << SHIFT);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
515 int i, sign;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
516 int nbitsneeded;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
517
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
518 if (vv == NULL || !PyLong_Check(vv)) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
519 PyErr_BadInternalCall();
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
520 return -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
521 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
522 v = (PyLongObject *)vv;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
523 i = v->ob_size;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
524 sign = 1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
525 if (i < 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
526 sign = -1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
527 i = -(i);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
528 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
529 else if (i == 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
530 *exponent = 0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
531 return 0.0;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
532 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
533 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
534 x = (double)v->ob_digit[i];
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
535 nbitsneeded = NBITS_WANTED - 1;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
536 /* Invariant: i Python digits remain unaccounted for. */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
537 while (i > 0 && nbitsneeded > 0) {
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
538 --i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
539 x = x * multiplier + (double)v->ob_digit[i];
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
540 nbitsneeded -= SHIFT;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
541 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
542 /* There are i digits we didn't shift in. Pretending they're all
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
543 zeroes, the true value is x * 2**(i*SHIFT). */
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
544 *exponent = i;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
545 assert(x > 0.0);
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
546 return x * sign;
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
547 #undef NBITS_WANTED
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
548 }
7df300d284c6 Introduce new private API function _PyLong_AsScaledDouble. Not used yet,
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
549
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
550 /* Get a C double from a long int object. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 double
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
553 PyLong_AsDouble(PyObject *vv)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 {
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
555 int e;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 double x;
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
557
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
558 if (vv == NULL || !PyLong_Check(vv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
559 PyErr_BadInternalCall();
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 return -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 }
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
562 x = _PyLong_AsScaledDouble(vv, &e);
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
563 if (x == -1.0 && PyErr_Occurred())
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
564 return -1.0;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
565 if (e > INT_MAX / SHIFT)
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
566 goto overflow;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
567 errno = 0;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
568 x = ldexp(x, e * SHIFT);
19717
01de0a703069 Try to recover from that glibc's ldexp apparently doesn't set errno on
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
569 if (Py_OVERFLOWED(x))
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
570 goto overflow;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
571 return x;
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
572
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
573 overflow:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
574 PyErr_SetString(PyExc_OverflowError,
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
575 "long int too large to convert to float");
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
576 return -1.0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
579 /* 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
580
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
581 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
582 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
583 {
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
584 #if SIZEOF_VOID_P <= SIZEOF_LONG
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
585 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
586 #else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
587
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
588 #ifndef HAVE_LONG_LONG
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
589 # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
590 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
591 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
592 # error "PyLong_FromVoidPtr: sizeof(LONG_LONG) < sizeof(void*)"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
593 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
594 /* optimize null pointers */
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
595 if (p == NULL)
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
596 return PyInt_FromLong(0);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
597 return PyLong_FromLongLong((LONG_LONG)p);
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
598
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
599 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
600 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
601
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
602 /* 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
603
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
604 void *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
605 PyLong_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
606 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
607 /* 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
608 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
609 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
610 */
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
611 #if SIZEOF_VOID_P <= SIZEOF_LONG
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
612 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
613
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
614 if (PyInt_Check(vv))
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
615 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
616 else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
617 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
618 #else
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
619
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
620 #ifndef HAVE_LONG_LONG
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
621 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
622 #endif
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
623 #if SIZEOF_LONG_LONG < SIZEOF_VOID_P
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
624 # error "PyLong_AsVoidPtr: sizeof(LONG_LONG) < sizeof(void*)"
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
625 #endif
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
626 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
627
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
628 if (PyInt_Check(vv))
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
629 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
630 else
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
631 x = PyLong_AsLongLong(vv);
18134
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
632
eff8da7c3dcb PyLong_{As, From}VoidPtr: cleanup, replacing assumptions in comments with
Tim Peters <tim.peters@gmail.com>
parents: 18121
diff changeset
633 #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
634
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
635 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
636 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
637 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
638 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 9094
diff changeset
639
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
640 #ifdef HAVE_LONG_LONG
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
641
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
642 /* Initial LONG_LONG support by Chris Herborth (chrish@qnx.com), later
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
643 * rewritten to use the newer PyLong_{As,From}ByteArray API.
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
644 */
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
645
18121
166b7b365851 Change IS_LITTLE_ENDIAN macro -- a little faster now.
Tim Peters <tim.peters@gmail.com>
parents: 18115
diff changeset
646 #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
647
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
648 /* 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
649
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
650 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
651 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
652 {
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
653 LONG_LONG bytes = ival;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
654 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
655 return _PyLong_FromByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
656 (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
657 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
658 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
659
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
660 /* Create a new long int object from a C unsigned LONG_LONG int. */
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
661
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
662 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
663 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
664 {
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
665 unsigned LONG_LONG bytes = ival;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
666 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
667 return _PyLong_FromByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
668 (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
669 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
670 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
671
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
672 /* Get a C LONG_LONG int from a long int object.
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
673 Return -1 and set an error if overflow occurs. */
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
674
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
675 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
676 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
677 {
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
678 LONG_LONG bytes;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
679 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
680 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
681
20182
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
682 if (vv == NULL) {
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
683 PyErr_BadInternalCall();
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
684 return -1;
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
685 }
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
686 if (!PyLong_Check(vv)) {
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
687 if (PyInt_Check(vv))
6d73b54d9205 SF [#466125] PyLong_AsLongLong works for any integer.
Tim Peters <tim.peters@gmail.com>
parents: 19976
diff changeset
688 return (LONG_LONG)PyInt_AsLong(vv);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
689 PyErr_BadInternalCall();
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
690 return -1;
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
691 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
692
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
693 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
694 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
695 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
696
18107
c578b75ad62f PyLong_As{Unsigned,}LongLong: fiddled final result casting.
Tim Peters <tim.peters@gmail.com>
parents: 18093
diff changeset
697 return res < 0 ? (LONG_LONG)res : bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
698 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
699
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
700 /* Get a C unsigned LONG_LONG int from a long int object.
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
701 Return -1 and set an error if overflow occurs. */
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
702
9044
d437b6683e24 Patch by Mark Hammond to support 64-bit ints on MS platforms.
Guido van Rossum <guido@python.org>
parents: 8933
diff changeset
703 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
704 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
705 {
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
706 unsigned LONG_LONG bytes;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
707 int one = 1;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
708 int res;
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
709
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
710 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
711 PyErr_BadInternalCall();
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
712 return -1;
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
713 }
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
714
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
715 res = _PyLong_AsByteArray(
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
716 (PyLongObject *)vv, (unsigned char *)&bytes,
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
717 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
718
18107
c578b75ad62f PyLong_As{Unsigned,}LongLong: fiddled final result casting.
Tim Peters <tim.peters@gmail.com>
parents: 18093
diff changeset
719 return res < 0 ? (unsigned LONG_LONG)res : bytes;
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
720 }
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
721
18093
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
722 #undef IS_LITTLE_ENDIAN
6ea366a2b2a6 longobject.c:
Tim Peters <tim.peters@gmail.com>
parents: 18091
diff changeset
723
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8808
diff changeset
724 #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
725
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
726
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
727 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
728 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
729 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
730 *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
731 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
732 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
733 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
734 *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
735 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
736 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
737 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
738 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
739 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
740 *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
741 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
742 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
743 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
744 *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
745 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
746 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
747 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
748 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
749 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
750 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
751 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
752
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
753 #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
754 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
755 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
756 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
757 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
758
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
759
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 /* Multiply by a single digit, ignoring the sign. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
762 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
763 mul1(PyLongObject *a, wdigit n)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 return muladd1(a, n, (digit)0);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
767
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 /* 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
769
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
770 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
771 muladd1(PyLongObject *a, wdigit n, wdigit extra)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
773 int size_a = ABS(a->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
774 PyLongObject *z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 twodigits carry = extra;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 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
782 z->ob_digit[i] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
783 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 }
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
785 z->ob_digit[i] = (digit) carry;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
789 /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
790 in pout, and returning the remainder. pin and pout point at the LSD.
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
791 It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
792 long_format, but that should be done with great care since longs are
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
793 immutable. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
794
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
795 static digit
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
796 inplace_divrem1(digit *pout, digit *pin, int size, digit n)
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
797 {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
798 twodigits rem = 0;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
799
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
800 assert(n > 0 && n <= MASK);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
801 pin += size;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
802 pout += size;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
803 while (--size >= 0) {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
804 digit hi;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
805 rem = (rem << SHIFT) + *--pin;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
806 *--pout = hi = (digit)(rem / n);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
807 rem -= hi * n;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
808 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
809 return (digit)rem;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
810 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
811
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 /* Divide a long integer by a digit, returning both the quotient
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 (as function result) and the remainder (through *prem).
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 The sign of a is ignored; n should not be zero. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
816 static PyLongObject *
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
817 divrem1(PyLongObject *a, digit n, digit *prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 {
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
819 const int size = ABS(a->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
820 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 assert(n > 0 && n <= MASK);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
823 z = _PyLong_New(size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 return NULL;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
826 *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
829
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 /* 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
831 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
832 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
833
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
834 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
835 long_format(PyObject *aa, int base, int addL)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
837 register PyLongObject *a = (PyLongObject *)aa;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
838 PyStringObject *str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 int i;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
840 const int size_a = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 char *p;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 int bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 char sign = '\0';
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
844
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
845 if (a == NULL || !PyLong_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
846 PyErr_BadInternalCall();
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
847 return NULL;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
848 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 assert(base >= 2 && base <= 36);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 /* Compute a rough upper bound for the length of the string */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 i = base;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 bits = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 while (i > 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 ++bits;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 i >>= 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 }
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
858 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
859 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860 if (str == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
862 p = PyString_AS_STRING(str) + i;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 *p = '\0';
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
864 if (addL)
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
865 *--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
866 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
867 sign = '-';
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
868
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
869 if (a->ob_size == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
870 *--p = '0';
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
871 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
872 else if ((base & (base - 1)) == 0) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
873 /* JRH: special case for power-of-2 bases */
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
874 twodigits accum = 0;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
875 int accumbits = 0; /* # of bits in accum */
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
876 int basebits = 1; /* # of bits in base-1 */
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
877 i = base;
13423
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
878 while ((i >>= 1) > 1)
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
879 ++basebits;
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
880
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
881 for (i = 0; i < size_a; ++i) {
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
882 accum |= a->ob_digit[i] << accumbits;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
883 accumbits += SHIFT;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
884 assert(accumbits >= basebits);
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
885 do {
21968
62741b25ed65 Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents: 20868
diff changeset
886 char cdigit = (char)(accum & (base - 1));
62741b25ed65 Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents: 20868
diff changeset
887 cdigit += (cdigit < 10) ? '0' : 'A'-10;
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
888 assert(p > PyString_AS_STRING(str));
21968
62741b25ed65 Patch #508038: Do not use a type as a variable name.
Martin v. Löwis <martin@v.loewis.de>
parents: 20868
diff changeset
889 *--p = cdigit;
18605
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
890 accumbits -= basebits;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
891 accum >>= basebits;
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
892 } while (i < size_a-1 ? accumbits >= basebits :
0857fb489fa3 long_format: Simplify the overly elaborate base-is-a-power-of-2 code.
Tim Peters <tim.peters@gmail.com>
parents: 18594
diff changeset
893 accum > 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 }
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
895 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
896 else {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
897 /* Not 0, and base not a power of 2. Divide repeatedly by
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
898 base, but for speed use the highest power of base that
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
899 fits in a digit. */
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
900 int size = size_a;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
901 digit *pin = a->ob_digit;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
902 PyLongObject *scratch;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
903 /* powbasw <- largest power of base that fits in a digit. */
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
904 digit powbase = base; /* powbase == base ** power */
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
905 int power = 1;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
906 for (;;) {
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
907 unsigned long newpow = powbase * (unsigned long)base;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
908 if (newpow >> SHIFT) /* doesn't fit in a digit */
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
909 break;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
910 powbase = (digit)newpow;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
911 ++power;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
912 }
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
913
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
914 /* Get a scratch area for repeated division. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
915 scratch = _PyLong_New(size);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
916 if (scratch == NULL) {
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
917 Py_DECREF(str);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
918 return NULL;
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
919 }
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
920
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
921 /* Repeatedly divide by powbase. */
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
922 do {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
923 int ntostore = power;
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
924 digit rem = inplace_divrem1(scratch->ob_digit,
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
925 pin, size, powbase);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
926 pin = scratch->ob_digit; /* no need to use a again */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
927 if (pin[size - 1] == 0)
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
928 --size;
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
929 SIGCHECK({
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
930 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
931 Py_DECREF(str);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
932 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
933 })
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
934
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
935 /* Break rem into digits. */
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
936 assert(ntostore > 0);
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
937 do {
18536
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
938 digit nextrem = (digit)(rem / base);
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
939 char c = (char)(rem - nextrem * base);
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
940 assert(p > PyString_AS_STRING(str));
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
941 c += (c < 10) ? '0' : 'A'-10;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
942 *--p = c;
b287f2927db3 long_format(): Easy speedup for output bases that aren't a power of 2 (in
Tim Peters <tim.peters@gmail.com>
parents: 18500
diff changeset
943 rem = nextrem;
18593
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
944 --ntostore;
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
945 /* Termination is a bit delicate: must not
9a373a143a13 long_format(): Simplify new code a bit.
Tim Peters <tim.peters@gmail.com>
parents: 18536
diff changeset
946 store leading zeroes, so must get out if
18594
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
947 remaining quotient and rem are both 0. */
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
948 } while (ntostore && (size || rem));
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
949 } while (size != 0);
19250f9181dc divrem1 & long_format: found a clean way to factor divrem1 so that
Tim Peters <tim.peters@gmail.com>
parents: 18593
diff changeset
950 Py_DECREF(scratch);
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
951 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
952
958
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
953 if (base == 8) {
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
954 if (size_a != 0)
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
955 *--p = '0';
a736ef1840a5 oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
956 }
406
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
957 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
958 *--p = 'x';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
959 *--p = '0';
e628d09ce22f Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
960 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
961 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
962 *--p = '#';
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
963 *--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
964 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
965 *--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
966 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 if (sign)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 *--p = sign;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
969 if (p != PyString_AS_STRING(str)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
970 char *q = PyString_AS_STRING(str);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 assert(p > q);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 do {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 } while ((*q++ = *p++) != '\0');
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
974 q--;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
975 _PyString_Resize((PyObject **)&str,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
976 (int) (q - PyString_AS_STRING(str)));
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
978 return (PyObject *)str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
980
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
981 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
982 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
983 {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 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
985 char *start, *orig_str = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
986 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
987
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
988 if ((base != 0 && base < 2) || base > 36) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
989 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
990 "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
991 return NULL;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
992 }
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
993 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
994 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 if (*str == '+')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 else if (*str == '-') {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 ++str;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 sign = -1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 }
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
1001 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
1002 str++;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 if (base == 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 if (str[0] != '0')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 base = 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 else if (str[1] == 'x' || str[1] == 'X')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 base = 16;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009 base = 8;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1010 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 str += 2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1013 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
1014 start = str;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 for ( ; z != NULL; ++str) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 int k = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1017 PyLongObject *temp;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 if (*str <= '9')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 k = *str - '0';
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 else if (*str >= 'a')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 k = *str - 'a' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 else if (*str >= 'A')
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 k = *str - 'A' + 10;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 if (k < 0 || k >= base)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 break;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 temp = muladd1(z, (digit)base, (digit)k);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1028 Py_DECREF(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1029 z = temp;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 }
8808
78785985a2ab Fix a potential problem in PyLong_FromString(): could fall through the
Guido van Rossum <guido@python.org>
parents: 8533
diff changeset
1031 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
1032 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
1033 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
1034 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
1035 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
1036 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
1037 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
1038 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
1039 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
1040 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
1041 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
1042 goto onError;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1043 if (pend)
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1044 *pend = str;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1045 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
1046
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
1047 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
1048 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
1049 "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
1050 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
1051 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
1052 }
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
1053
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1054 #ifdef Py_USING_UNICODE
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11628
diff changeset
1055 PyObject *
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1056 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
1057 {
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
1058 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
1059
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
1060 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
1061 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
1062 "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
1063 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
1064 }
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
1065 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
1066 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
1067
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
1068 return PyLong_FromString(buffer, NULL, base);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
1070 #endif
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
1072 /* forward */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1073 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
1074 (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
1075 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
1076 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
1077 PyLongObject **, PyLongObject **);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 /* Long division with remainder, top-level routine */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1081 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
1082 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
1083 PyLongObject **pdiv, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1085 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
1086 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088 if (size_b == 0) {
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
1089 PyErr_SetString(PyExc_ZeroDivisionError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 15041
diff changeset
1090 "long division or modulo by zero");
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1091 return -1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 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
1094 (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
1095 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
1096 /* |a| < |b|. */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1097 *pdiv = _PyLong_New(0);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1098 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1099 *prem = (PyLongObject *) a;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1100 return 0;
230
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 if (size_b == 1) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 digit rem = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 z = divrem1(a, b->ob_digit[0], &rem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1105 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1106 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1107 *prem = (PyLongObject *) PyLong_FromLong((long)rem);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108 }
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1109 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 z = x_divrem(a, b, prem);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1111 if (z == NULL)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1112 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1113 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1114 /* Set the signs.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 The quotient z has the sign of a*b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116 the remainder r has the sign of a,
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117 so a = b*z + r. */
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1118 if ((a->ob_size < 0) != (b->ob_size < 0))
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1119 z->ob_size = -(z->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1120 if (a->ob_size < 0 && (*prem)->ob_size != 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1121 (*prem)->ob_size = -((*prem)->ob_size);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1122 *pdiv = z;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1123 return 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1124 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1126 /* Unsigned long division with remainder -- the algorithm */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1128 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
1129 x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1130 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1131 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
1132 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
1133 PyLongObject *v = mul1(v1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1134 PyLongObject *w = mul1(w1, d);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1135 PyLongObject *a;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1136 int j, k;
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 if (v == NULL || w == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1139 Py_XDECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1140 Py_XDECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 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
1145 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
1146 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
1147
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1148 size_v = ABS(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1149 a = _PyLong_New(size_v - size_w + 1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 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
1152 digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1153 twodigits q;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1154 stwodigits carry = 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1156
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1157 SIGCHECK({
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1158 Py_DECREF(a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1159 a = NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1160 break;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1161 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1162 if (vj == w->ob_digit[size_w-1])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163 q = MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) /
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 w->ob_digit[size_w-1];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 while (w->ob_digit[size_w-2]*q >
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 ((
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 ((twodigits)vj << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 + v->ob_digit[j-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 - q*w->ob_digit[size_w-1]
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173 ) << SHIFT)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 + v->ob_digit[j-2])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175 --q;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 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
1179 digit zz = (digit) (z >> SHIFT);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1180 carry += v->ob_digit[i+k] - z
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1181 + ((twodigits)zz << SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182 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
1183 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
1184 carry, SHIFT);
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1185 carry -= zz;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1186 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1187
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 if (i+k < size_v) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 carry += v->ob_digit[i+k];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 v->ob_digit[i+k] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 if (carry == 0)
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
1194 a->ob_digit[k] = (digit) q;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 assert(carry == -1);
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5199
diff changeset
1197 a->ob_digit[k] = (digit) q-1;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1198 carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 for (i = 0; i < size_w && i+k < size_v; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 carry += v->ob_digit[i+k] + w->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 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
1202 carry = Py_ARITHMETIC_RIGHT_SHIFT(
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1203 BASE_TWODIGITS_TYPE,
a87803eb9dfb Cray J90 fixes for long ints.
Tim Peters <tim.peters@gmail.com>
parents: 13420
diff changeset
1204 carry, SHIFT);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 } /* for j, k */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1209 if (a == NULL)
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1210 *prem = NULL;
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1211 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 a = long_normalize(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1213 *prem = divrem1(v, d, &d);
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1214 /* d receives the (unused) remainder */
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1215 if (*prem == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1216 Py_DECREF(a);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1217 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
1218 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1220 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1221 Py_DECREF(w);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 return a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 /* Methods */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1227 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
1228 long_dealloc(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 {
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20182
diff changeset
1230 v->ob_type->tp_free(v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1233 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
1234 long_repr(PyObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1236 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
1237 }
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1238
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1239 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
1240 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
1241 {
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
1242 return long_format(v, 10, 0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245 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
1246 long_compare(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1248 int sign;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1250 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
1251 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
1252 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
1253 else
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1254 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
1255 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 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
1257 int i = ABS(a->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1259 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 if (i < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 sign = 0;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1262 else {
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 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
1264 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
1265 sign = -sign;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1266 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 }
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1268 return sign < 0 ? -1 : sign > 0 ? 1 : 0;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1271 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
1272 long_hash(PyLongObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1273 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1274 long x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1275 int i, sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1276
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1277 /* 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
1278 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
1279 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
1280 i = v->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1281 sign = 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1282 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1283 if (i < 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1284 sign = -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1285 i = -(i);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1286 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1287 while (--i >= 0) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1288 /* Force a 32-bit circular shift */
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1289 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
1290 x += v->ob_digit[i];
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1291 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1292 x = x * sign;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1293 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1294 x = -2;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1295 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1296 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1297
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1298
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 /* Add the absolute values of two long integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1301 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
1302 x_add(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1304 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
1305 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 digit carry = 0;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1308
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 if (size_a < size_b) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1311 { PyLongObject *temp = a; a = b; b = temp; }
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1312 { int size_temp = size_a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1313 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1314 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1315 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1316 z = _PyLong_New(size_a+1);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 carry += a->ob_digit[i] + b->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 carry += a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 z->ob_digit[i] = carry & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329 z->ob_digit[i] = carry;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 }
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 /* Subtract the absolute values of two integers. */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1335 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
1336 x_sub(PyLongObject *a, PyLongObject *b)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1338 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
1339 PyLongObject *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340 int i;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 int sign = 1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 digit borrow = 0;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1343
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 /* Ensure a is the larger of the two: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 if (size_a < size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1347 { PyLongObject *temp = a; a = b; b = temp; }
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1348 { int size_temp = size_a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1349 size_a = size_b;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1350 size_b = size_temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 else if (size_a == size_b) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1353 /* Find highest digit where a and b differ: */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 i = size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 ;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1357 if (i < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1358 return _PyLong_New(0);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 if (a->ob_digit[i] < b->ob_digit[i]) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 sign = -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1361 { PyLongObject *temp = a; a = b; b = temp; }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363 size_a = size_b = i+1;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1365 z = _PyLong_New(size_a);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 if (z == NULL)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 return NULL;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 for (i = 0; i < size_b; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 /* The following assumes unsigned arithmetic
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 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
1371 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1373 borrow >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374 borrow &= 1; /* Keep only one sign bit */
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 for (; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 borrow = a->ob_digit[i] - borrow;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 z->ob_digit[i] = borrow & MASK;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379 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
1380 borrow &= 1; /* Keep only one sign bit */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382 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
1383 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
1384 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1385 return long_normalize(z);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1388 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
1389 long_add(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1390 {
15725
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1391 PyLongObject *a, *b, *z;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1392
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
1393 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
1394
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 if (b->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1397 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
1398 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
1399 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1401 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1402 z = x_sub(b, a);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 }
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
1410 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
1411 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1412 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1414
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1415 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
1416 long_sub(PyLongObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1417 {
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
1418 PyLongObject *a, *b, *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
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 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
1421
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422 if (a->ob_size < 0) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1423 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1425 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 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
1427 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
1428 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1429 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1430 else {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1431 if (b->ob_size < 0)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1432 z = x_add(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1433 else
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1434 z = x_sub(a, b);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1435 }
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
1436 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
1437 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1438 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1440
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1441 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
1442 long_repeat(PyObject *v, PyLongObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 {
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
1444 /* 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
1445 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
1446 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
1447 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
1448 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
1449 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
1450 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1451
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1452 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
1453 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
1454 {
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1455 PyLongObject *a, *b, *z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1456 int size_a;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1457 int size_b;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1458 int i;
19925
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1459
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1460 if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) {
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1461 if (!PyLong_Check(v) &&
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1462 v->ob_type->tp_as_sequence &&
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1463 v->ob_type->tp_as_sequence->sq_repeat)
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1464 return long_repeat((PyObject *)v, w);
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1465 if (!PyLong_Check(w) &&
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1466 w->ob_type->tp_as_sequence &&
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1467 w->ob_type->tp_as_sequence->sq_repeat)
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1468 return long_repeat((PyObject *)w, v);
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1469 Py_INCREF(Py_NotImplemented);
df409844b659 A fix for SF bug #461546 (bug in long_mul).
Guido van Rossum <guido@python.org>
parents: 19897
diff changeset
1470 return Py_NotImplemented;
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
1471 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1472
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1473 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
1474 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
1475 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
1476 /* 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
1477 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
1478 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
1479 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
1480 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
1481 a = b;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1482 b = hold_a;
fc557f431e87 Simple optimization by Christian Tismer, who gives credit to Lenny
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
1483 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1484 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
1485 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
1486 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
1487 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488 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
1489 }
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1490 for (i = 0; i < z->ob_size; ++i)
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1491 z->ob_digit[i] = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1492 for (i = 0; i < size_a; ++i) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1493 twodigits carry = 0;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1494 twodigits f = a->ob_digit[i];
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1495 int j;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1496
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1497 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
1498 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
1499 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1500 Py_DECREF(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1501 return NULL;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1502 })
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1503 for (j = 0; j < size_b; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504 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
1505 z->ob_digit[i+j] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1506 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1507 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 for (; carry != 0; ++j) {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1509 assert(i+j < z->ob_size);
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510 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
1511 z->ob_digit[i+j] = (digit) (carry & MASK);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1512 carry >>= SHIFT;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1514 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515 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
1516 z->ob_size = -(z->ob_size);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1517 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
1518 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
1519 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
1520 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1521 return (PyObject *) long_normalize(z);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1524 /* The / and % operators are now defined in terms of divmod().
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1525 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
1526 The long_divrem function gives the remainder after division of
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 |a| by |b|, with the sign of a. This is also expressed
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528 as a - b*trunc(a/b), if trunc truncates towards zero.
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529 Some examples:
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1530 a b a rem b a mod b
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 13 10 3 3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532 -13 10 -3 7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1533 13 -10 3 -7
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1534 -13 -10 -3 -3
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1535 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
1536 have different signs. We then subtract one from the 'div'
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1537 part of the outcome to keep the invariant intact. */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1539 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
1540 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
1541 PyLongObject **pdiv, PyLongObject **pmod)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1542 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1543 PyLongObject *div, *mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1544
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1545 if (long_divrem(v, w, &div, &mod) < 0)
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1546 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
1547 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
1548 (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
1549 PyLongObject *temp;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1550 PyLongObject *one;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1551 temp = (PyLongObject *) long_add(mod, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1552 Py_DECREF(mod);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1553 mod = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1554 if (mod == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1555 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1556 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1557 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1558 one = (PyLongObject *) PyLong_FromLong(1L);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1559 if (one == NULL ||
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1560 (temp = (PyLongObject *) long_sub(div, one)) == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1561 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1562 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1563 Py_XDECREF(one);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1564 return -1;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1565 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1566 Py_DECREF(one);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1567 Py_DECREF(div);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1568 div = temp;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1569 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1570 *pdiv = div;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1571 *pmod = mod;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1572 return 0;
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1573 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1574
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1575 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
1576 long_div(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1577 {
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
1578 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
1579
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 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
1581
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1582 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
1583 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
1584 Py_DECREF(b);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1585 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
1586 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1587 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
1588 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1589 Py_DECREF(mod);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1590 return (PyObject *)div;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1591 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1592
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1593 static PyObject *
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1594 long_classic_div(PyObject *v, PyObject *w)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1595 {
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1596 PyLongObject *a, *b, *div, *mod;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1597
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1598 CONVERT_BINOP(v, w, &a, &b);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1599
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1600 if (Py_DivisionWarningFlag &&
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1601 PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1602 div = NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1603 else if (l_divmod(a, b, &div, &mod) < 0)
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1604 div = NULL;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1605 else
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1606 Py_DECREF(mod);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1607
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1608 Py_DECREF(a);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1609 Py_DECREF(b);
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1610 return (PyObject *)div;
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1611 }
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1612
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
1613 static PyObject *
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
1614 long_true_divide(PyObject *v, PyObject *w)
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
1615 {
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1616 PyLongObject *a, *b;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1617 double ad, bd;
20868
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1618 int aexp, bexp, failed;
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1619
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1620 CONVERT_BINOP(v, w, &a, &b);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1621 ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1622 bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp);
20868
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1623 failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred();
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1624 Py_DECREF(a);
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1625 Py_DECREF(b);
2d9e41a72a68 long_true_divide(): decref its converted arguments. test_long_future.py
Tim Peters <tim.peters@gmail.com>
parents: 20277
diff changeset
1626 if (failed)
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1627 return NULL;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1628
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1629 if (bd == 0.0) {
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1630 PyErr_SetString(PyExc_ZeroDivisionError,
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1631 "long division or modulo by zero");
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1632 return NULL;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1633 }
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1634
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1635 /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1636 ad /= bd; /* overflow/underflow impossible here */
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1637 aexp -= bexp;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1638 if (aexp > INT_MAX / SHIFT)
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1639 goto overflow;
19787
f814ea6bb667 long_true_divide: reliably force underflow to 0 when the denominator
Tim Peters <tim.peters@gmail.com>
parents: 19719
diff changeset
1640 else if (aexp < -(INT_MAX / SHIFT))
f814ea6bb667 long_true_divide: reliably force underflow to 0 when the denominator
Tim Peters <tim.peters@gmail.com>
parents: 19719
diff changeset
1641 return PyFloat_FromDouble(0.0); /* underflow to 0 */
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1642 errno = 0;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1643 ad = ldexp(ad, aexp * SHIFT);
19717
01de0a703069 Try to recover from that glibc's ldexp apparently doesn't set errno on
Tim Peters <tim.peters@gmail.com>
parents: 19677
diff changeset
1644 if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */
19677
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1645 goto overflow;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1646 return PyFloat_FromDouble(ad);
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1647
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1648 overflow:
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1649 PyErr_SetString(PyExc_OverflowError,
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1650 "long/long too large for a float");
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1651 return NULL;
91533675d6cb Change long/long true division to return as many good bits as it can;
Tim Peters <tim.peters@gmail.com>
parents: 19675
diff changeset
1652
19675
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
1653 }
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
1654
685dcae5c123 Move long_true_divide next to the other division routines (for clarity!).
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
1655 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
1656 long_mod(PyObject *v, PyObject *w)
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1657 {
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
1658 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
1659
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1660 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
1661
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1662 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
1663 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
1664 Py_DECREF(b);
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1665 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
1666 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1667 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
1668 Py_DECREF(b);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1669 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1670 return (PyObject *)mod;
613
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1671 }
405a2fec7454 Add prototypes.
Guido van Rossum <guido@python.org>
parents: 590
diff changeset
1672
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1673 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
1674 long_divmod(PyObject *v, PyObject *w)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1675 {
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
1676 PyLongObject *a, *b, *div, *mod;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1677 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
1678
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1679 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
1680
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1681 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
1682 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
1683 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1684 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
1685 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1686 z = PyTuple_New(2);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1687 if (z != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1688 PyTuple_SetItem(z, 0, (PyObject *) div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1689 PyTuple_SetItem(z, 1, (PyObject *) mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1690 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1691 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1692 Py_DECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1693 Py_DECREF(mod);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1694 }
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
1695 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
1696 Py_DECREF(b);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1697 return z;
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1698 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1699
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1700 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
1701 long_pow(PyObject *v, PyObject *w, PyObject *x)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1702 {
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
1703 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
1704 PyObject *c;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1705 PyLongObject *z, *div, *mod;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1706 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
1707
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1708 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
1709 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
1710 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
1711 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
1712 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1713 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
1714 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
1715 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1716 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
1717 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
1718 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
1719 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
1720 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
1721 }
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1722
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1723 if (c != Py_None && ((PyLongObject *)c)->ob_size == 0) {
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1724 PyErr_SetString(PyExc_ValueError,
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1725 "pow() 3rd argument cannot be 0");
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1726 z = NULL;
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1727 goto error;
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1728 }
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1729
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1730 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
1731 if (size_b < 0) {
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1732 Py_DECREF(a);
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1733 Py_DECREF(b);
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1734 Py_DECREF(c);
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1735 if (x != Py_None) {
19719
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1736 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
cdfdd5359411 Make the error msgs in our pow() implementations consistent.
Tim Peters <tim.peters@gmail.com>
parents: 19717
diff changeset
1737 "cannot be negative when 3rd argument specified");
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1738 return NULL;
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1739 }
18500
e6ecd8a07ea8 On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18134
diff changeset
1740 /* Return a float. This works because we know that
e6ecd8a07ea8 On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18134
diff changeset
1741 this calls float_pow() which converts its
e6ecd8a07ea8 On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18134
diff changeset
1742 arguments to double. */
e6ecd8a07ea8 On long to the negative long power, let float handle it instead of
Guido van Rossum <guido@python.org>
parents: 18134
diff changeset
1743 return PyFloat_Type.tp_as_number->nb_power(v, w, x);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1744 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1745 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
1746 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
1747 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
1748 int j;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1749
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1750 for (j = 0; j < SHIFT; ++j) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1751 PyLongObject *temp;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1752
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1753 if (bi & 1) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1754 temp = (PyLongObject *)long_mul(z, a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1755 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
1756 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
1757 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
1758 &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
1759 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
1760 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
1761 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
1762 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1763 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1764 Py_DECREF(temp);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1765 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1766 }
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1767 z = temp;
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1768 if (z == NULL)
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1769 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1770 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1771 bi >>= 1;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1772 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
1773 break;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1774 temp = (PyLongObject *)long_mul(a, a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1775 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
1776 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
1777 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
1778 &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
1779 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
1780 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
1781 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
1782 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1783 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1784 Py_DECREF(temp);
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1785 temp = mod;
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1786 }
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1787 a = temp;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1788 if (a == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1789 Py_DECREF(z);
250
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1790 z = NULL;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1791 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1792 }
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1793 }
2111
30cbe024df40 fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
1794 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
1795 break;
b9986009d7dc Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents: 242
diff changeset
1796 }
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
1797 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
1798 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
1799 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
1800 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
1801 }
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
1802 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1803 Py_XDECREF(div);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1804 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
1805 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
1806 }
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
1807 }
11380
3e3c71f4d2de Fix PR#66. Solution: add error checking around l_divmod() calls in
Guido van Rossum <guido@python.org>
parents: 11331
diff changeset
1808 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
1809 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
1810 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
1811 Py_DECREF(c);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1812 return (PyObject *)z;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1813 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1814
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1815 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
1816 long_invert(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1817 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1818 /* Implement ~x as -(x+1) */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1819 PyLongObject *x;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1820 PyLongObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1821 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
1822 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
1823 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1824 x = (PyLongObject *) long_add(v, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1825 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
1826 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
1827 return NULL;
19882
96f53f8831ee long_invert(): tiny speed and space optimization.
Tim Peters <tim.peters@gmail.com>
parents: 19880
diff changeset
1828 x->ob_size = -(x->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1829 return (PyObject *)x;
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1830 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1831
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1832 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
1833 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
1834 {
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1835 if (PyLong_CheckExact(v)) {
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1836 Py_INCREF(v);
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1837 return (PyObject *)v;
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1838 }
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1839 else
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1840 return _PyLong_Copy(v);
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1841 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1842
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1843 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
1844 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
1845 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1846 PyLongObject *z;
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1847 if (v->ob_size == 0 && PyLong_CheckExact(v)) {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1848 /* -0 == 0 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1849 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1850 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
1851 }
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1852 z = (PyLongObject *)_PyLong_Copy(v);
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1853 if (z != NULL)
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1854 z->ob_size = -(v->ob_size);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1855 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
1856 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1857
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1858 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
1859 long_abs(PyLongObject *v)
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1860 {
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1861 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
1862 return long_neg(v);
19880
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1863 else
d1cd4cd72271 More bug 460020. Disable a number of long optimizations for long subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19837
diff changeset
1864 return long_pos(v);
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1865 }
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1866
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1867 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
1868 long_nonzero(PyLongObject *v)
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
1869 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1870 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
1871 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1872
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1873 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
1874 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
1875 {
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
1876 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
1877 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
1878 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
1879 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
1880 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
1881
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
1882 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
1883
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1884 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
1885 /* 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
1886 PyLongObject *a1, *a2;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1887 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
1888 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
1889 goto rshift_error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1890 a2 = (PyLongObject *) long_rshift(a1, b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1891 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
1892 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
1893 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
1894 z = (PyLongObject *) long_invert(a2);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1895 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
1896 }
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
1897 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
1898
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1899 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
1900 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
1901 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
1902 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
1903 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
1904 "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
1905 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
1906 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1907 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
1908 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
1909 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
1910 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
1911 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
1912 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
1913 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
1914 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1915 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
1916 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
1917 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
1918 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
1919 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
1920 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
1921 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
1922 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
1923 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
1924 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
1925 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
1926 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
1927 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
1928 (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
1929 }
8e402aeca0a3 Make long a new style number type. Sequence repeat is now done here
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1930 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
1931 }
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
1932 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
1933 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
1934 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
1935 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
1936
443
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1937 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1938
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1939 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
1940 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
1941 {
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
1942 /* 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
1943 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
1944 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
1945 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
1946 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
1947 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
1948
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
1949 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
1950
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1951 shiftby = PyLong_AsLong((PyObject *)b);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1952 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
1953 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
1954 if (shiftby < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1955 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
1956 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
1957 }
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
1958 if ((long)(int)shiftby != shiftby) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1959 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1960 "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
1961 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
1962 }
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
1963 /* 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
1964 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
1965 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
1966
7e380df00d00 New long_lshift, without restriction on size of shift count, by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1967 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
1968 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
1969 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
1970 ++newsize;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1971 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
1972 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
1973 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
1974 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
1975 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
1976 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
1977 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
1978 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
1979 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
1980 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
1981 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
1982 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
1983 }
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
1984 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
1985 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
1986 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
1987 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
1988 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
1989 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
1990 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
1991 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
1992 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
1993 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
1994
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1995
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
1996 /* 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
1997
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
1998 #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
1999 #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
2000
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2001 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
2002 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
2003 int op, /* '&', '|', '^' */
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2004 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
2005 {
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2006 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
2007 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
2008 int size_a, size_b, size_z;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2009 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
2010 int i;
743
dbcb49d742a0 Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents: 613
diff changeset
2011 digit diga, digb;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2012 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
2013
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2014 if (a->ob_size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2015 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
2016 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
2017 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2018 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2019 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
2020 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
2021 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2022 if (b->ob_size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2023 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
2024 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
2025 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2026 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2027 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
2028 maskb = 0;
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
2029 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2030
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2031 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
2032 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
2033 case '^':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2034 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
2035 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
2036 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
2037 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2038 break;
1837
87364d9e3444 New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
2039 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
2040 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
2041 op = '|';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2042 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
2043 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
2044 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
2045 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2046 break;
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2047 case '|':
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2048 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
2049 op = '&';
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2050 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
2051 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
2052 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
2053 }
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2054 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
2055 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2056
8933
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2057 /* 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
2058 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
2059 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
2060 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
2061 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
2062 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
2063 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
2064 whose length should be ignored.
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2065 */
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2066
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2067 size_a = a->ob_size;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2068 size_b = b->ob_size;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2069 size_z = op == '&'
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2070 ? (maska
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2071 ? size_b
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2072 : (maskb ? size_a : MIN(size_a, size_b)))
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2073 : MAX(size_a, size_b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2074 z = _PyLong_New(size_z);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2075 if (a == NULL || b == NULL || z == NULL) {
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2076 Py_XDECREF(a);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2077 Py_XDECREF(b);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2078 Py_XDECREF(z);
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2079 return NULL;
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2080 }
dc6edfd4bae5 Two patches by Jason Harper:
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
2081
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2082 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
2083 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
2084 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
2085 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
2086 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
2087 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
2088 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
2089 }
546
937cbc0be6ec Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
2090 }
590
f11fae8ee1e4 Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents: 546
diff changeset
2091
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2092 Py_DECREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2093 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
2094 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
2095 if (negz == 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2096 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
2097 v = long_invert(z);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2098 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
2099 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
2100 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2101
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2102 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
2103 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
2104 {
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
2105 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
2106 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
2107 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
2108 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
2109 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
2110 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
2111 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
2112 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2113
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2114 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
2115 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
2116 {
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
2117 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
2118 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
2119 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
2120 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
2121 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
2122 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
2123 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
2124 }
9f85beb1f7c8 Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents: 406
diff changeset
2125
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2126 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
2127 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
2128 {
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
2129 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
2130 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
2131 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
2132 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
2133 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
2134 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
2135 return c;
242
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2136 }
316a0d9c0096 Many small changes
Guido van Rossum <guido@python.org>
parents: 230
diff changeset
2137
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
2138 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
2139 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
2140 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2141 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
2142 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2143 Py_INCREF(*pv);
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
2144 return 0;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
2145 }
19976
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
2146 else if (PyLong_Check(*pw)) {
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
2147 Py_INCREF(*pv);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
2148 Py_INCREF(*pw);
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
2149 return 0;
da00188d5270 Add additional coercion support for "self subtypes" to int, long,
Guido van Rossum <guido@python.org>
parents: 19925
diff changeset
2150 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
2151 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
2152 }
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
2153
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2154 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
2155 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
2156 {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2157 long x;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2158 x = PyLong_AsLong(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2159 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
2160 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2161 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
2162 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2163
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2164 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
2165 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
2166 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2167 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
2168 return v;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2169 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2170
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2171 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
2172 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
2173 {
5118
002865af55e5 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 4870
diff changeset
2174 double result;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2175 result = PyLong_AsDouble(v);
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
2176 if (result == -1.0 && PyErr_Occurred())
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19671
diff changeset
2177 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2178 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
2179 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2180
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2181 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
2182 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
2183 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2184 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
2185 }
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2186
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2187 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
2188 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
2189 {
11628
47f93a969366 long_format(): Now takes a third parameter, addL; iff true, a
Fred Drake <fdrake@acm.org>
parents: 11380
diff changeset
2190 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
2191 }
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2192 staticforward PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2193 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 958
diff changeset
2194
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2195 static PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2196 long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2197 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2198 PyObject *x = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2199 int base = -909; /* unlikely! */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2200 static char *kwlist[] = {"x", "base", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2201
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2202 if (type != &PyLong_Type)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2203 return long_subtype_new(type, args, kwds); /* Wimp out */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2204 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2205 &x, &base))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2206 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2207 if (x == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2208 return PyLong_FromLong(0L);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2209 if (base == -909)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2210 return PyNumber_Long(x);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2211 else if (PyString_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2212 return PyLong_FromString(PyString_AS_STRING(x), NULL, base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
2213 #ifdef Py_USING_UNICODE
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2214 else if (PyUnicode_Check(x))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2215 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2216 PyUnicode_GET_SIZE(x),
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2217 base);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19051
diff changeset
2218 #endif
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2219 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2220 PyErr_SetString(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2221 "long() can't convert non-string with explicit base");
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2222 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2223 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2224 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2225
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2226 /* Wimpy, slow approach to tp_new calls for subtypes of long:
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2227 first create a regular long from whatever arguments we got,
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2228 then allocate a subtype instance and initialize it from
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2229 the regular long. The regular long is then thrown away.
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2230 */
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2231 static PyObject *
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2232 long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2233 {
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2234 PyLongObject *tmp, *new;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2235 int i, n;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2236
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2237 assert(PyType_IsSubtype(type, &PyLong_Type));
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2238 tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2239 if (tmp == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2240 return NULL;
19837
5158e4789459 SF bug #460020: bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19787
diff changeset
2241 assert(PyLong_CheckExact(tmp));
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2242 n = tmp->ob_size;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2243 if (n < 0)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2244 n = -n;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2245 new = (PyLongObject *)type->tp_alloc(type, n);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2246 if (new == NULL)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2247 return NULL;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2248 assert(PyLong_Check(new));
19610
c773daa44840 Ah, the joy of writing test cases...
Guido van Rossum <guido@python.org>
parents: 19552
diff changeset
2249 new->ob_size = tmp->ob_size;
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2250 for (i = 0; i < n; i++)
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2251 new->ob_digit[i] = tmp->ob_digit[i];
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2252 Py_DECREF(tmp);
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2253 return (PyObject *)new;
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2254 }
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2255
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2256 static char long_doc[] =
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2257 "long(x[, base]) -> integer\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2258 \n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2259 Convert a string or number to a long integer, if possible. A floating\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2260 point argument will be truncated towards zero (this does not include a\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2261 string representation of a floating point number!) When converting a\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2262 string, use the optional base. It is an error to supply a base when\n\
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2263 converting a non-string.";
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2264
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2265 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
2266 (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
2267 (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
2268 (binaryfunc) long_mul, /*nb_multiply*/
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19610
diff changeset
2269 (binaryfunc) long_classic_div, /*nb_divide*/
13406
a503af7e7fb7 Some cleanup of longs in prepartion for Cray J90 fixes: got
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
2270 (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
2271 (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
2272 (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
2273 (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
2274 (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
2275 (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
2276 (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
2277 (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
2278 (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
2279 (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
2280 (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
2281 (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
2282 (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
2283 (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
2284 (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
2285 (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
2286 (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
2287 (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
2288 (unaryfunc) long_hex, /*nb_hex*/
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2289 0, /* nb_inplace_add */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2290 0, /* nb_inplace_subtract */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2291 0, /* nb_inplace_multiply */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2292 0, /* nb_inplace_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2293 0, /* nb_inplace_remainder */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2294 0, /* nb_inplace_power */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2295 0, /* nb_inplace_lshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2296 0, /* nb_inplace_rshift */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2297 0, /* nb_inplace_and */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2298 0, /* nb_inplace_xor */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2299 0, /* nb_inplace_or */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2300 (binaryfunc)long_div, /* nb_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2301 long_true_divide, /* nb_true_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2302 0, /* nb_inplace_floor_divide */
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
2303 0, /* nb_inplace_true_divide */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2304 };
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2305
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2306 PyTypeObject PyLong_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2307 PyObject_HEAD_INIT(&PyType_Type)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2308 0, /* ob_size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2309 "long", /* tp_name */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2310 sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2311 sizeof(digit), /* tp_itemsize */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2312 (destructor)long_dealloc, /* tp_dealloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2313 0, /* tp_print */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2314 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2315 0, /* tp_setattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2316 (cmpfunc)long_compare, /* tp_compare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2317 (reprfunc)long_repr, /* tp_repr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2318 &long_as_number, /* tp_as_number */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2319 0, /* tp_as_sequence */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2320 0, /* tp_as_mapping */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2321 (hashfunc)long_hash, /* tp_hash */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2322 0, /* tp_call */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2323 (reprfunc)long_str, /* tp_str */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2324 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2325 0, /* tp_setattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2326 0, /* tp_as_buffer */
19552
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2327 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
6c30d20d27f2 Make int, long and float subclassable.
Guido van Rossum <guido@python.org>
parents: 19343
diff changeset
2328 Py_TPFLAGS_BASETYPE, /* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2329 long_doc, /* tp_doc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2330 0, /* tp_traverse */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2331 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2332 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2333 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2334 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2335 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2336 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2337 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2338 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2339 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2340 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2341 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2342 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2343 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2344 0, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2345 0, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18607
diff changeset
2346 long_new, /* tp_new */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20182
diff changeset
2347 _PyObject_Del, /* tp_free */
230
7ba9612058a4 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2348 };