Mercurial > cpython-lru_cache-weakref
annotate Objects/longobject.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | 09d94e9eb101 |
| rev | line source |
|---|---|
| 230 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
3 The Netherlands. |
| 230 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 230 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 230 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
15 permission. |
| 230 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2482
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 230 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 32 /* Long (arbitrary precision) integer object implementation */ | |
| 33 | |
| 242 | 34 /* XXX The functional organization of this file is terrible */ |
| 35 | |
| 230 | 36 #include "allobjects.h" |
| 37 #include "longintrepr.h" | |
| 2482 | 38 #include "mymath.h" |
| 230 | 39 #include <assert.h> |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
40 #include <ctype.h> |
| 230 | 41 |
| 613 | 42 #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 43 | |
| 44 /* Forward */ | |
| 45 static longobject *long_normalize PROTO((longobject *)); | |
| 46 static longobject *mul1 PROTO((longobject *, wdigit)); | |
| 47 static longobject *muladd1 PROTO((longobject *, wdigit, wdigit)); | |
| 48 static longobject *divrem1 PROTO((longobject *, wdigit, digit *)); | |
|
1391
7c3245c18677
* ceval.c, longobject.c, methodobject.c, listnode.c, arraymodule.c,
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
49 static object *long_format PROTO((object *aa, int base)); |
| 613 | 50 |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
51 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
|
52 |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
53 #define SIGCHECK(block) \ |
| 242 | 54 if (--ticker < 0) { \ |
| 55 ticker = 100; \ | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
56 if (sigcheck()) { block; } \ |
| 242 | 57 } |
| 58 | |
| 230 | 59 /* Normalize (remove leading zeros from) a long int object. |
| 60 Doesn't attempt to free the storage--in most cases, due to the nature | |
| 61 of the algorithms used, this could save at most be one word anyway. */ | |
| 62 | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
63 static longobject * |
| 230 | 64 long_normalize(v) |
| 65 register longobject *v; | |
| 66 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
67 int j = ABS(v->ob_size); |
| 230 | 68 register int i = j; |
| 69 | |
| 70 while (i > 0 && v->ob_digit[i-1] == 0) | |
| 71 --i; | |
| 72 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
|
73 v->ob_size = (v->ob_size < 0) ? -(i) : i; |
| 230 | 74 return v; |
| 75 } | |
| 76 | |
| 77 /* Allocate a new long int object with size digits. | |
| 78 Return NULL and set exception if we run out of memory. */ | |
| 79 | |
| 80 longobject * | |
| 81 alloclongobject(size) | |
| 82 int size; | |
| 83 { | |
| 84 return NEWVAROBJ(longobject, &Longtype, size); | |
| 85 } | |
| 86 | |
| 87 /* Create a new long int object from a C long int */ | |
| 88 | |
| 89 object * | |
| 90 newlongobject(ival) | |
| 91 long ival; | |
| 92 { | |
| 93 /* Assume a C long fits in at most 3 'digits' */ | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
94 /* XXX On 64 bit machines this isn't true!!! */ |
| 230 | 95 longobject *v = alloclongobject(3); |
| 96 if (v != NULL) { | |
| 97 if (ival < 0) { | |
| 98 ival = -ival; | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
99 v->ob_size = -(v->ob_size); |
| 230 | 100 } |
| 101 v->ob_digit[0] = ival & MASK; | |
| 102 v->ob_digit[1] = (ival >> SHIFT) & MASK; | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
103 v->ob_digit[2] = ((unsigned long)ival >> (2*SHIFT)) & MASK; |
| 230 | 104 v = long_normalize(v); |
| 105 } | |
| 106 return (object *)v; | |
| 107 } | |
| 108 | |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
109 /* 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
|
110 |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
111 object * |
| 2482 | 112 #ifdef MPW |
| 113 dnewlongobject(double dval) | |
| 114 #else | |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
115 dnewlongobject(dval) |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
116 double dval; |
| 2482 | 117 #endif /* MPW */ |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
118 { |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
119 longobject *v; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
120 double frac; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
121 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
|
122 neg = 0; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
123 if (dval < 0.0) { |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
124 neg = 1; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
125 dval = -dval; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
126 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
127 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
|
128 if (expo <= 0) |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
129 return newlongobject(0L); |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
130 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
131 v = alloclongobject(ndig); |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
132 if (v == NULL) |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
133 return NULL; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
134 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
|
135 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
|
136 long bits = (long)frac; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
137 v->ob_digit[i] = bits; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
138 frac = frac - (double)bits; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
139 frac = ldexp(frac, SHIFT); |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
140 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
141 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
|
142 v->ob_size = -(v->ob_size); |
|
254
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
143 return (object *)v; |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
144 } |
|
71b0bd5ed162
Added dnewlongobject(), function to convert double to long int.
Guido van Rossum <guido@python.org>
parents:
250
diff
changeset
|
145 |
| 230 | 146 /* Get a C long int from a long int object. |
| 147 Returns -1 and sets an error condition if overflow occurs. */ | |
| 148 | |
| 149 long | |
| 150 getlongvalue(vv) | |
| 151 object *vv; | |
| 152 { | |
| 153 register longobject *v; | |
| 154 long x, prev; | |
| 155 int i, sign; | |
| 156 | |
| 157 if (vv == NULL || !is_longobject(vv)) { | |
| 158 err_badcall(); | |
| 159 return -1; | |
| 160 } | |
| 161 v = (longobject *)vv; | |
| 162 i = v->ob_size; | |
| 163 sign = 1; | |
| 164 x = 0; | |
| 165 if (i < 0) { | |
| 166 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
|
167 i = -(i); |
| 230 | 168 } |
| 169 while (--i >= 0) { | |
| 170 prev = x; | |
| 171 x = (x << SHIFT) + v->ob_digit[i]; | |
| 172 if ((x >> SHIFT) != prev) { | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1837
diff
changeset
|
173 err_setstr(OverflowError, |
| 230 | 174 "long int too long to convert"); |
| 175 return -1; | |
| 176 } | |
| 177 } | |
| 178 return x * sign; | |
| 179 } | |
| 180 | |
| 181 /* Get a C double from a long int object. No overflow check. */ | |
| 182 | |
| 183 double | |
| 184 dgetlongvalue(vv) | |
| 185 object *vv; | |
| 186 { | |
| 187 register longobject *v; | |
| 188 double x; | |
| 189 double multiplier = (double) (1L << SHIFT); | |
| 190 int i, sign; | |
| 191 | |
| 192 if (vv == NULL || !is_longobject(vv)) { | |
| 193 err_badcall(); | |
| 194 return -1; | |
| 195 } | |
| 196 v = (longobject *)vv; | |
| 197 i = v->ob_size; | |
| 198 sign = 1; | |
| 199 x = 0.0; | |
| 200 if (i < 0) { | |
| 201 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
|
202 i = -(i); |
| 230 | 203 } |
| 204 while (--i >= 0) { | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
205 x = x*multiplier + (double)v->ob_digit[i]; |
| 230 | 206 } |
| 207 return x * sign; | |
| 208 } | |
| 209 | |
| 210 /* Multiply by a single digit, ignoring the sign. */ | |
| 211 | |
| 613 | 212 static longobject * |
| 230 | 213 mul1(a, n) |
| 214 longobject *a; | |
| 242 | 215 wdigit n; |
| 230 | 216 { |
| 217 return muladd1(a, n, (digit)0); | |
| 218 } | |
| 219 | |
| 220 /* Multiply by a single digit and add a single digit, ignoring the sign. */ | |
| 221 | |
| 613 | 222 static longobject * |
| 230 | 223 muladd1(a, n, extra) |
| 224 longobject *a; | |
| 242 | 225 wdigit n; |
| 226 wdigit extra; | |
| 230 | 227 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
228 int size_a = ABS(a->ob_size); |
| 230 | 229 longobject *z = alloclongobject(size_a+1); |
| 230 twodigits carry = extra; | |
| 231 int i; | |
| 232 | |
| 233 if (z == NULL) | |
| 234 return NULL; | |
| 235 for (i = 0; i < size_a; ++i) { | |
| 236 carry += (twodigits)a->ob_digit[i] * n; | |
| 237 z->ob_digit[i] = carry & MASK; | |
| 238 carry >>= SHIFT; | |
| 239 } | |
| 240 z->ob_digit[i] = carry; | |
| 241 return long_normalize(z); | |
| 242 } | |
| 243 | |
| 244 /* Divide a long integer by a digit, returning both the quotient | |
| 245 (as function result) and the remainder (through *prem). | |
| 246 The sign of a is ignored; n should not be zero. */ | |
| 247 | |
| 613 | 248 static longobject * |
| 230 | 249 divrem1(a, n, prem) |
| 250 longobject *a; | |
| 242 | 251 wdigit n; |
| 230 | 252 digit *prem; |
| 253 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
254 int size = ABS(a->ob_size); |
| 230 | 255 longobject *z; |
| 256 int i; | |
| 257 twodigits rem = 0; | |
| 258 | |
| 259 assert(n > 0 && n <= MASK); | |
| 260 z = alloclongobject(size); | |
| 261 if (z == NULL) | |
| 262 return NULL; | |
| 263 for (i = size; --i >= 0; ) { | |
| 264 rem = (rem << SHIFT) + a->ob_digit[i]; | |
| 265 z->ob_digit[i] = rem/n; | |
| 266 rem %= n; | |
| 267 } | |
| 268 *prem = rem; | |
| 269 return long_normalize(z); | |
| 270 } | |
| 271 | |
| 272 /* 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
|
273 Return a string object. |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
274 If base is 8 or 16, add the proper prefix '0' or '0x'. |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
275 External linkage: used in bltinmodule.c by hex() and oct(). */ |
| 230 | 276 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
277 static object * |
| 613 | 278 long_format(aa, base) |
| 279 object *aa; | |
| 230 | 280 int base; |
| 281 { | |
| 613 | 282 register longobject *a = (longobject *)aa; |
| 230 | 283 stringobject *str; |
| 284 int i; | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
285 int size_a = ABS(a->ob_size); |
| 230 | 286 char *p; |
| 287 int bits; | |
| 288 char sign = '\0'; | |
| 613 | 289 |
| 290 if (a == NULL || !is_longobject(a)) { | |
| 291 err_badcall(); | |
| 292 return NULL; | |
| 293 } | |
| 230 | 294 assert(base >= 2 && base <= 36); |
| 295 | |
| 296 /* Compute a rough upper bound for the length of the string */ | |
| 297 i = base; | |
| 298 bits = 0; | |
| 299 while (i > 1) { | |
| 300 ++bits; | |
| 301 i >>= 1; | |
| 302 } | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
303 i = 6 + (size_a*SHIFT + bits-1) / bits; |
| 230 | 304 str = (stringobject *) newsizedstringobject((char *)0, i); |
| 305 if (str == NULL) | |
| 306 return NULL; | |
| 307 p = GETSTRINGVALUE(str) + i; | |
| 308 *p = '\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
|
309 *--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
|
310 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
|
311 sign = '-'; |
| 230 | 312 |
| 313 INCREF(a); | |
| 314 do { | |
| 315 digit rem; | |
| 316 longobject *temp = divrem1(a, (digit)base, &rem); | |
| 317 if (temp == NULL) { | |
| 318 DECREF(a); | |
| 319 DECREF(str); | |
| 320 return NULL; | |
| 321 } | |
| 322 if (rem < 10) | |
| 323 rem += '0'; | |
| 324 else | |
| 325 rem += 'A'-10; | |
| 326 assert(p > GETSTRINGVALUE(str)); | |
| 327 *--p = rem; | |
| 328 DECREF(a); | |
| 329 a = temp; | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
330 SIGCHECK({ |
| 230 | 331 DECREF(a); |
| 332 DECREF(str); | |
| 333 return NULL; | |
| 242 | 334 }) |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
335 } while (ABS(a->ob_size) != 0); |
| 230 | 336 DECREF(a); |
|
958
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
337 if (base == 8) { |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
338 if (size_a != 0) |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
339 *--p = '0'; |
|
a736ef1840a5
oct(0) should return '0', not '00'
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
340 } |
|
406
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
341 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
|
342 *--p = 'x'; |
|
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
343 *--p = '0'; |
|
e628d09ce22f
Changed convert to add '0' or '0x' prefix for oct/hex.
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
344 } |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
345 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
|
346 *--p = '#'; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
347 *--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
|
348 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
|
349 *--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
|
350 } |
| 230 | 351 if (sign) |
| 352 *--p = sign; | |
| 353 if (p != GETSTRINGVALUE(str)) { | |
| 354 char *q = GETSTRINGVALUE(str); | |
| 355 assert(p > q); | |
| 356 do { | |
| 357 } while ((*q++ = *p++) != '\0'); | |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
358 q--; |
| 230 | 359 resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str))); |
| 360 } | |
| 613 | 361 return (object *)str; |
| 230 | 362 } |
| 363 | |
|
2158
29720c5d9e7e
long_scan is no longer used
Guido van Rossum <guido@python.org>
parents:
2111
diff
changeset
|
364 #if 0 |
| 230 | 365 /* Convert a string to a long int object, in a given base. |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
366 Base zero implies a default depending on the number. |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
367 External linkage: used in compile.c and stropmodule.c. */ |
| 230 | 368 |
| 369 object * | |
| 370 long_scan(str, base) | |
| 371 char *str; | |
| 372 int base; | |
| 373 { | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
374 return long_escan(str, (char **)NULL, base); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
375 } |
|
2158
29720c5d9e7e
long_scan is no longer used
Guido van Rossum <guido@python.org>
parents:
2111
diff
changeset
|
376 #endif |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
377 |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
378 object * |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
379 long_escan(str, pend, base) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
380 char *str; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
381 char **pend; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
382 int base; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
383 { |
| 230 | 384 int sign = 1; |
| 385 longobject *z; | |
| 386 | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
387 if (base != 0 && base < 2 || base > 36) { |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
388 err_setstr(ValueError, "invalid base for long literal"); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
389 return NULL; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
390 } |
|
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
|
391 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
|
392 str++; |
| 230 | 393 if (*str == '+') |
| 394 ++str; | |
| 395 else if (*str == '-') { | |
| 396 ++str; | |
| 397 sign = -1; | |
| 398 } | |
|
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
|
399 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
|
400 str++; |
| 230 | 401 if (base == 0) { |
| 402 if (str[0] != '0') | |
| 403 base = 10; | |
| 404 else if (str[1] == 'x' || str[1] == 'X') | |
| 405 base = 16; | |
| 406 else | |
| 407 base = 8; | |
| 408 } | |
| 409 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) | |
| 410 str += 2; | |
| 411 z = alloclongobject(0); | |
| 412 for ( ; z != NULL; ++str) { | |
| 413 int k = -1; | |
| 414 longobject *temp; | |
| 415 | |
| 416 if (*str <= '9') | |
| 417 k = *str - '0'; | |
| 418 else if (*str >= 'a') | |
| 419 k = *str - 'a' + 10; | |
| 420 else if (*str >= 'A') | |
| 421 k = *str - 'A' + 10; | |
| 422 if (k < 0 || k >= base) | |
| 423 break; | |
| 424 temp = muladd1(z, (digit)base, (digit)k); | |
| 425 DECREF(z); | |
| 426 z = temp; | |
| 427 } | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
428 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
|
429 z->ob_size = -(z->ob_size); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
430 if (pend) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
431 *pend = str; |
| 230 | 432 return (object *) z; |
| 433 } | |
| 434 | |
| 435 static longobject *x_divrem PROTO((longobject *, longobject *, longobject **)); | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
436 static object *long_pos PROTO((longobject *)); |
| 613 | 437 static long_divrem PROTO((longobject *, longobject *, |
| 438 longobject **, longobject **)); | |
| 230 | 439 |
| 440 /* Long division with remainder, top-level routine */ | |
| 441 | |
| 613 | 442 static int |
| 443 long_divrem(a, b, pdiv, prem) | |
| 230 | 444 longobject *a, *b; |
| 613 | 445 longobject **pdiv; |
| 230 | 446 longobject **prem; |
| 447 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
448 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
| 230 | 449 longobject *z; |
| 450 | |
| 451 if (size_b == 0) { | |
| 613 | 452 err_setstr(ZeroDivisionError, "long division or modulo"); |
| 453 return -1; | |
| 230 | 454 } |
| 455 if (size_a < size_b || | |
| 242 | 456 size_a == size_b && |
| 457 a->ob_digit[size_a-1] < b->ob_digit[size_b-1]) { | |
| 230 | 458 /* |a| < |b|. */ |
| 613 | 459 *pdiv = alloclongobject(0); |
| 460 INCREF(a); | |
| 461 *prem = (longobject *) a; | |
| 462 return 0; | |
| 230 | 463 } |
| 464 if (size_b == 1) { | |
| 465 digit rem = 0; | |
| 466 z = divrem1(a, b->ob_digit[0], &rem); | |
| 613 | 467 if (z == NULL) |
| 468 return -1; | |
| 469 *prem = (longobject *) newlongobject((long)rem); | |
| 230 | 470 } |
| 613 | 471 else { |
| 230 | 472 z = x_divrem(a, b, prem); |
| 613 | 473 if (z == NULL) |
| 474 return -1; | |
| 475 } | |
| 230 | 476 /* Set the signs. |
| 477 The quotient z has the sign of a*b; | |
| 478 the remainder r has the sign of a, | |
| 479 so a = b*z + r. */ | |
| 613 | 480 if ((a->ob_size < 0) != (b->ob_size < 0)) |
| 481 z->ob_size = -(z->ob_size); | |
| 482 if (a->ob_size < 0 && (*prem)->ob_size != 0) | |
| 483 (*prem)->ob_size = -((*prem)->ob_size); | |
| 484 *pdiv = z; | |
| 485 return 0; | |
| 230 | 486 } |
| 487 | |
| 242 | 488 /* Unsigned long division with remainder -- the algorithm */ |
| 230 | 489 |
| 490 static longobject * | |
| 491 x_divrem(v1, w1, prem) | |
| 492 longobject *v1, *w1; | |
| 493 longobject **prem; | |
| 494 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
495 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); |
| 230 | 496 digit d = (twodigits)BASE / (w1->ob_digit[size_w-1] + 1); |
| 497 longobject *v = mul1(v1, d); | |
| 498 longobject *w = mul1(w1, d); | |
| 499 longobject *a; | |
| 500 int j, k; | |
| 501 | |
| 502 if (v == NULL || w == NULL) { | |
| 503 XDECREF(v); | |
| 504 XDECREF(w); | |
| 505 return NULL; | |
| 506 } | |
| 507 | |
| 508 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ | |
| 242 | 509 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
|
510 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ |
| 230 | 511 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
512 size_v = ABS(v->ob_size); |
| 230 | 513 a = alloclongobject(size_v - size_w + 1); |
| 514 | |
| 515 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { | |
| 516 digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; | |
| 517 twodigits q; | |
| 242 | 518 stwodigits carry = 0; |
| 230 | 519 int i; |
| 520 | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
521 SIGCHECK({ |
| 230 | 522 DECREF(a); |
| 523 a = NULL; | |
| 524 break; | |
| 242 | 525 }) |
| 230 | 526 if (vj == w->ob_digit[size_w-1]) |
| 527 q = MASK; | |
| 528 else | |
| 529 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) / | |
| 530 w->ob_digit[size_w-1]; | |
| 531 | |
| 532 while (w->ob_digit[size_w-2]*q > | |
| 533 (( | |
| 534 ((twodigits)vj << SHIFT) | |
| 535 + v->ob_digit[j-1] | |
| 536 - q*w->ob_digit[size_w-1] | |
| 537 ) << SHIFT) | |
| 538 + v->ob_digit[j-2]) | |
| 539 --q; | |
| 540 | |
| 541 for (i = 0; i < size_w && i+k < size_v; ++i) { | |
| 542 twodigits z = w->ob_digit[i] * q; | |
| 543 digit zz = z >> SHIFT; | |
| 544 carry += v->ob_digit[i+k] - z + ((twodigits)zz << SHIFT); | |
| 545 v->ob_digit[i+k] = carry & MASK; | |
| 546 carry = (carry >> SHIFT) - zz; | |
| 547 } | |
| 548 | |
| 549 if (i+k < size_v) { | |
| 550 carry += v->ob_digit[i+k]; | |
| 551 v->ob_digit[i+k] = 0; | |
| 552 } | |
| 553 | |
| 554 if (carry == 0) | |
| 555 a->ob_digit[k] = q; | |
| 556 else { | |
| 557 assert(carry == -1); | |
| 558 a->ob_digit[k] = q-1; | |
| 559 carry = 0; | |
| 560 for (i = 0; i < size_w && i+k < size_v; ++i) { | |
| 561 carry += v->ob_digit[i+k] + w->ob_digit[i]; | |
| 562 v->ob_digit[i+k] = carry & MASK; | |
| 563 carry >>= SHIFT; | |
| 564 } | |
| 565 } | |
| 566 } /* for j, k */ | |
| 567 | |
|
2111
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
568 if (a == NULL) |
|
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
569 *prem = NULL; |
|
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
570 else { |
| 230 | 571 a = long_normalize(a); |
| 613 | 572 *prem = divrem1(v, d, &d); |
| 573 /* d receives the (unused) remainder */ | |
| 574 if (*prem == NULL) { | |
| 575 DECREF(a); | |
| 576 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
|
577 } |
| 230 | 578 } |
| 579 DECREF(v); | |
| 580 DECREF(w); | |
| 581 return a; | |
| 582 } | |
| 583 | |
| 584 /* Methods */ | |
| 585 | |
| 613 | 586 /* Forward */ |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
587 static void long_dealloc PROTO((object *)); |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
588 static object *long_repr PROTO((object *)); |
| 613 | 589 static int long_compare PROTO((longobject *, longobject *)); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
590 static long long_hash PROTO((longobject *)); |
| 613 | 591 |
| 592 static object *long_add PROTO((longobject *, longobject *)); | |
| 593 static object *long_sub PROTO((longobject *, longobject *)); | |
| 594 static object *long_mul PROTO((longobject *, longobject *)); | |
| 595 static object *long_div PROTO((longobject *, longobject *)); | |
| 596 static object *long_mod PROTO((longobject *, longobject *)); | |
| 597 static object *long_divmod PROTO((longobject *, longobject *)); | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
598 static object *long_pow PROTO((longobject *, longobject *, longobject *)); |
| 613 | 599 static object *long_neg PROTO((longobject *)); |
| 600 static object *long_pos PROTO((longobject *)); | |
| 601 static object *long_abs PROTO((longobject *)); | |
| 602 static int long_nonzero PROTO((longobject *)); | |
| 603 static object *long_invert PROTO((longobject *)); | |
| 604 static object *long_lshift PROTO((longobject *, longobject *)); | |
| 605 static object *long_rshift PROTO((longobject *, longobject *)); | |
| 606 static object *long_and PROTO((longobject *, longobject *)); | |
| 607 static object *long_xor PROTO((longobject *, longobject *)); | |
| 608 static object *long_or PROTO((longobject *, longobject *)); | |
| 609 | |
| 230 | 610 static void |
| 611 long_dealloc(v) | |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
612 object *v; |
| 230 | 613 { |
| 614 DEL(v); | |
| 615 } | |
| 616 | |
| 617 static object * | |
| 618 long_repr(v) | |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
619 object *v; |
| 230 | 620 { |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
621 return long_format(v, 10); |
| 230 | 622 } |
| 623 | |
| 624 static int | |
| 625 long_compare(a, b) | |
| 626 longobject *a, *b; | |
| 627 { | |
| 628 int sign; | |
| 629 | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
630 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
|
631 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
|
632 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
|
633 else |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
634 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
|
635 } |
| 230 | 636 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
|
637 int i = ABS(a->ob_size); |
| 230 | 638 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 639 ; | |
| 640 if (i < 0) | |
| 641 sign = 0; | |
|
1191
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
642 else { |
| 230 | 643 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
|
644 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
|
645 sign = -sign; |
|
38f0054f44a7
Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
646 } |
| 230 | 647 } |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
648 return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
| 230 | 649 } |
| 650 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
651 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
652 long_hash(v) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
653 longobject *v; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
654 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
655 long x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
656 int i, sign; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
657 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
658 /* 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
|
659 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
|
660 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
|
661 i = v->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
662 sign = 1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
663 x = 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
664 if (i < 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
665 sign = -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
666 i = -(i); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
667 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
668 while (--i >= 0) { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
669 /* Force a 32-bit circular shift */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
670 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
|
671 x += v->ob_digit[i]; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
672 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
673 x = x * sign; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
674 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
675 x = -2; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
676 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
677 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
678 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
679 |
| 230 | 680 /* Add the absolute values of two long integers. */ |
| 681 | |
| 682 static longobject *x_add PROTO((longobject *, longobject *)); | |
| 683 static longobject * | |
| 684 x_add(a, b) | |
| 685 longobject *a, *b; | |
| 686 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
687 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
| 230 | 688 longobject *z; |
| 689 int i; | |
| 690 digit carry = 0; | |
| 691 | |
| 692 /* Ensure a is the larger of the two: */ | |
| 693 if (size_a < size_b) { | |
| 694 { longobject *temp = a; a = b; b = temp; } | |
| 695 { int size_temp = size_a; size_a = size_b; size_b = size_temp; } | |
| 696 } | |
| 697 z = alloclongobject(size_a+1); | |
| 698 if (z == NULL) | |
| 699 return NULL; | |
| 700 for (i = 0; i < size_b; ++i) { | |
| 701 carry += a->ob_digit[i] + b->ob_digit[i]; | |
| 702 z->ob_digit[i] = carry & MASK; | |
| 703 /* The following assumes unsigned shifts don't | |
| 704 propagate the sign bit. */ | |
| 705 carry >>= SHIFT; | |
| 706 } | |
| 707 for (; i < size_a; ++i) { | |
| 708 carry += a->ob_digit[i]; | |
| 709 z->ob_digit[i] = carry & MASK; | |
| 710 carry >>= SHIFT; | |
| 711 } | |
| 712 z->ob_digit[i] = carry; | |
| 713 return long_normalize(z); | |
| 714 } | |
| 715 | |
| 716 /* Subtract the absolute values of two integers. */ | |
| 717 | |
| 718 static longobject *x_sub PROTO((longobject *, longobject *)); | |
| 719 static longobject * | |
| 720 x_sub(a, b) | |
| 721 longobject *a, *b; | |
| 722 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
723 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
| 230 | 724 longobject *z; |
| 725 int i; | |
| 726 int sign = 1; | |
| 727 digit borrow = 0; | |
| 728 | |
| 729 /* Ensure a is the larger of the two: */ | |
| 730 if (size_a < size_b) { | |
| 731 sign = -1; | |
| 732 { longobject *temp = a; a = b; b = temp; } | |
| 733 { int size_temp = size_a; size_a = size_b; size_b = size_temp; } | |
| 734 } | |
| 735 else if (size_a == size_b) { | |
| 736 /* Find highest digit where a and b differ: */ | |
| 737 i = size_a; | |
| 738 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) | |
| 739 ; | |
| 740 if (i < 0) | |
| 741 return alloclongobject(0); | |
| 742 if (a->ob_digit[i] < b->ob_digit[i]) { | |
| 743 sign = -1; | |
| 744 { longobject *temp = a; a = b; b = temp; } | |
| 745 } | |
| 746 size_a = size_b = i+1; | |
| 747 } | |
| 748 z = alloclongobject(size_a); | |
| 749 if (z == NULL) | |
| 750 return NULL; | |
| 751 for (i = 0; i < size_b; ++i) { | |
| 752 /* The following assumes unsigned arithmetic | |
| 753 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
|
754 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 230 | 755 z->ob_digit[i] = borrow & MASK; |
| 756 borrow >>= SHIFT; | |
| 757 borrow &= 1; /* Keep only one sign bit */ | |
| 758 } | |
| 759 for (; i < size_a; ++i) { | |
| 760 borrow = a->ob_digit[i] - borrow; | |
| 761 z->ob_digit[i] = borrow & MASK; | |
| 762 borrow >>= SHIFT; | |
| 763 } | |
| 764 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
|
765 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
|
766 z->ob_size = -(z->ob_size); |
| 230 | 767 return long_normalize(z); |
| 768 } | |
| 769 | |
| 770 static object * | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
771 long_add(a, b) |
| 230 | 772 longobject *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
|
773 longobject *b; |
| 230 | 774 { |
| 775 longobject *z; | |
| 776 | |
| 777 if (a->ob_size < 0) { | |
| 778 if (b->ob_size < 0) { | |
| 779 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
|
780 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
|
781 z->ob_size = -(z->ob_size); |
| 230 | 782 } |
| 783 else | |
| 784 z = x_sub(b, a); | |
| 785 } | |
| 786 else { | |
| 787 if (b->ob_size < 0) | |
| 788 z = x_sub(a, b); | |
| 789 else | |
| 790 z = x_add(a, b); | |
| 791 } | |
| 792 return (object *)z; | |
| 793 } | |
| 794 | |
| 795 static object * | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
796 long_sub(a, b) |
| 230 | 797 longobject *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
|
798 longobject *b; |
| 230 | 799 { |
| 800 longobject *z; | |
| 801 | |
| 802 if (a->ob_size < 0) { | |
| 803 if (b->ob_size < 0) | |
| 804 z = x_sub(a, b); | |
| 805 else | |
| 806 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
|
807 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
|
808 z->ob_size = -(z->ob_size); |
| 230 | 809 } |
| 810 else { | |
| 811 if (b->ob_size < 0) | |
| 812 z = x_add(a, b); | |
| 813 else | |
| 814 z = x_sub(a, b); | |
| 815 } | |
| 816 return (object *)z; | |
| 817 } | |
| 818 | |
| 819 static object * | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
820 long_mul(a, b) |
| 230 | 821 longobject *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
|
822 longobject *b; |
| 230 | 823 { |
| 824 int size_a; | |
| 825 int size_b; | |
| 826 longobject *z; | |
| 827 int i; | |
| 828 | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
829 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
|
830 size_b = ABS(b->ob_size); |
| 230 | 831 z = alloclongobject(size_a + size_b); |
| 832 if (z == NULL) | |
| 833 return NULL; | |
| 834 for (i = 0; i < z->ob_size; ++i) | |
| 835 z->ob_digit[i] = 0; | |
| 836 for (i = 0; i < size_a; ++i) { | |
| 837 twodigits carry = 0; | |
| 838 twodigits f = a->ob_digit[i]; | |
| 839 int j; | |
| 840 | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
841 SIGCHECK({ |
| 230 | 842 DECREF(z); |
| 843 return NULL; | |
| 242 | 844 }) |
| 230 | 845 for (j = 0; j < size_b; ++j) { |
| 846 carry += z->ob_digit[i+j] + b->ob_digit[j] * f; | |
| 847 z->ob_digit[i+j] = carry & MASK; | |
| 848 carry >>= SHIFT; | |
| 849 } | |
| 850 for (; carry != 0; ++j) { | |
| 851 assert(i+j < z->ob_size); | |
| 852 carry += z->ob_digit[i+j]; | |
| 853 z->ob_digit[i+j] = carry & MASK; | |
| 854 carry >>= SHIFT; | |
| 855 } | |
| 856 } | |
| 857 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
|
858 z->ob_size = -(z->ob_size); |
| 230 | 859 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
|
860 z->ob_size = -(z->ob_size); |
| 230 | 861 return (object *) long_normalize(z); |
| 862 } | |
| 863 | |
| 613 | 864 /* The / and % operators are now defined in terms of divmod(). |
| 865 The expression a mod b has the value a - b*floor(a/b). | |
| 866 The long_divrem function gives the remainder after division of | |
| 230 | 867 |a| by |b|, with the sign of a. This is also expressed |
| 868 as a - b*trunc(a/b), if trunc truncates towards zero. | |
| 869 Some examples: | |
| 870 a b a rem b a mod b | |
| 871 13 10 3 3 | |
| 872 -13 10 -3 7 | |
| 873 13 -10 3 -7 | |
| 874 -13 -10 -3 -3 | |
| 875 So, to get from rem to mod, we have to add b if a and b | |
| 242 | 876 have different signs. We then subtract one from the 'div' |
| 877 part of the outcome to keep the invariant intact. */ | |
| 230 | 878 |
| 613 | 879 static int l_divmod PROTO((longobject *, longobject *, |
| 880 longobject **, longobject **)); | |
| 881 static int | |
| 882 l_divmod(v, w, pdiv, pmod) | |
| 883 longobject *v; | |
| 884 longobject *w; | |
| 885 longobject **pdiv; | |
| 886 longobject **pmod; | |
| 887 { | |
| 888 longobject *div, *mod; | |
| 889 | |
| 890 if (long_divrem(v, w, &div, &mod) < 0) | |
| 891 return -1; | |
| 892 if (mod->ob_size < 0 && w->ob_size > 0 || | |
| 893 mod->ob_size > 0 && w->ob_size < 0) { | |
| 894 longobject *temp; | |
| 895 longobject *one; | |
| 896 temp = (longobject *) long_add(mod, w); | |
| 897 DECREF(mod); | |
| 898 mod = temp; | |
| 899 if (mod == NULL) { | |
| 900 DECREF(div); | |
| 901 return -1; | |
| 902 } | |
| 903 one = (longobject *) newlongobject(1L); | |
| 904 if (one == NULL || | |
| 905 (temp = (longobject *) long_sub(div, one)) == NULL) { | |
| 906 DECREF(mod); | |
| 907 DECREF(div); | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
908 XDECREF(one); |
| 613 | 909 return -1; |
| 910 } | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
911 DECREF(one); |
| 613 | 912 DECREF(div); |
| 913 div = temp; | |
| 914 } | |
| 915 *pdiv = div; | |
| 916 *pmod = mod; | |
| 917 return 0; | |
| 918 } | |
| 919 | |
| 920 static object * | |
| 921 long_div(v, w) | |
| 922 longobject *v; | |
| 923 longobject *w; | |
| 924 { | |
| 925 longobject *div, *mod; | |
| 926 if (l_divmod(v, w, &div, &mod) < 0) | |
| 927 return NULL; | |
| 928 DECREF(mod); | |
| 929 return (object *)div; | |
| 930 } | |
| 931 | |
| 932 static object * | |
| 933 long_mod(v, w) | |
| 934 longobject *v; | |
| 935 longobject *w; | |
| 936 { | |
| 937 longobject *div, *mod; | |
| 938 if (l_divmod(v, w, &div, &mod) < 0) | |
| 939 return NULL; | |
| 940 DECREF(div); | |
| 941 return (object *)mod; | |
| 942 } | |
| 943 | |
| 230 | 944 static object * |
| 945 long_divmod(v, w) | |
| 946 longobject *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
|
947 longobject *w; |
| 230 | 948 { |
| 949 object *z; | |
| 613 | 950 longobject *div, *mod; |
| 951 if (l_divmod(v, w, &div, &mod) < 0) | |
| 230 | 952 return NULL; |
| 953 z = newtupleobject(2); | |
| 954 if (z != NULL) { | |
| 955 settupleitem(z, 0, (object *) div); | |
| 613 | 956 settupleitem(z, 1, (object *) mod); |
| 230 | 957 } |
| 958 else { | |
| 959 DECREF(div); | |
| 613 | 960 DECREF(mod); |
| 230 | 961 } |
| 962 return z; | |
| 963 } | |
| 964 | |
| 965 static object * | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
966 long_pow(a, b, c) |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
967 longobject *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
|
968 longobject *b; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
969 longobject *c; |
| 230 | 970 { |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
971 longobject *z, *div, *mod; |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
972 int size_b, i; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
973 |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
974 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
|
975 if (size_b < 0) { |
| 475 | 976 err_setstr(ValueError, "long integer to the negative power"); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
977 return NULL; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
978 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
979 z = (longobject *)newlongobject(1L); |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
980 INCREF(a); |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
981 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
|
982 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
|
983 int j; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
984 |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
985 for (j = 0; j < SHIFT; ++j) { |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
986 longobject *temp; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
987 |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
988 if (bi & 1) { |
| 613 | 989 temp = (longobject *)long_mul(z, a); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
990 DECREF(z); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
991 if ((object*)c!=None && temp!=NULL) { |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
992 l_divmod(temp, c, &div, &mod); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
993 XDECREF(div); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
994 DECREF(temp); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
995 temp = mod; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
996 } |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
997 z = temp; |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
998 if (z == NULL) |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
999 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1000 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1001 bi >>= 1; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1002 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
|
1003 break; |
| 613 | 1004 temp = (longobject *)long_mul(a, a); |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1005 DECREF(a); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1006 if ((object*)c!=None && temp!=NULL) { |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1007 l_divmod(temp, c, &div, &mod); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1008 XDECREF(div); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1009 DECREF(temp); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1010 temp = mod; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1011 } |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1012 a = temp; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1013 if (a == NULL) { |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1014 DECREF(z); |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1015 z = NULL; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1016 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1017 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1018 } |
|
2111
30cbe024df40
fix memory leak and null pointer dereference
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
1019 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
|
1020 break; |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1021 } |
|
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1022 XDECREF(a); |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1023 if ((object*)c!=None && z!=NULL) { |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1024 l_divmod(z, c, &div, &mod); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1025 XDECREF(div); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1026 DECREF(z); |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1027 z=mod; |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1028 } |
|
250
b9986009d7dc
Fix off-by-one error in long_repr.
Guido van Rossum <guido@python.org>
parents:
242
diff
changeset
|
1029 return (object *)z; |
| 230 | 1030 } |
| 1031 | |
| 1032 static object * | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1033 long_invert(v) |
| 230 | 1034 longobject *v; |
| 1035 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1036 /* Implement ~x as -(x+1) */ |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1037 longobject *x; |
| 613 | 1038 longobject *w; |
| 1039 w = (longobject *)newlongobject(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
|
1040 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
|
1041 return NULL; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1042 x = (longobject *) long_add(v, w); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1043 DECREF(w); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1044 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
|
1045 return NULL; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1046 if (x->ob_size != 0) |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1047 x->ob_size = -(x->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
|
1048 return (object *)x; |
| 230 | 1049 } |
| 1050 | |
| 1051 static object * | |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1052 long_pos(v) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1053 longobject *v; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1054 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1055 INCREF(v); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1056 return (object *)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
|
1057 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1058 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1059 static object * |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1060 long_neg(v) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1061 longobject *v; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1062 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1063 longobject *z; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1064 int i, n; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1065 n = ABS(v->ob_size); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1066 if (n == 0) { |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1067 /* -0 == 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
|
1068 INCREF(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
|
1069 return (object *) 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
|
1070 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1071 z = alloclongobject(ABS(n)); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1072 if (z == NULL) |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1073 return NULL; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1074 for (i = 0; i < n; i++) |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1075 z->ob_digit[i] = v->ob_digit[i]; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1076 z->ob_size = -(v->ob_size); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1077 return (object *)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
|
1078 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1079 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1080 static object * |
| 230 | 1081 long_abs(v) |
| 1082 longobject *v; | |
| 1083 { | |
| 1084 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
|
1085 return long_neg(v); |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1086 else { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1087 INCREF(v); |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1088 return (object *)v; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1089 } |
| 230 | 1090 } |
| 1091 | |
| 242 | 1092 static int |
| 1093 long_nonzero(v) | |
| 1094 longobject *v; | |
| 1095 { | |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1096 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
|
1097 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1098 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1099 static object * |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1100 long_rshift(a, b) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1101 longobject *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
|
1102 longobject *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
|
1103 { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1104 longobject *z; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1105 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
|
1106 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
|
1107 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
|
1108 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1109 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
|
1110 /* Right shifting negative numbers is harder */ |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1111 longobject *a1, *a2, *a3; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1112 a1 = (longobject *) long_invert(a); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1113 if (a1 == NULL) return NULL; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1114 a2 = (longobject *) long_rshift(a1, b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1115 DECREF(a1); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1116 if (a2 == NULL) return NULL; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1117 a3 = (longobject *) long_invert(a2); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1118 DECREF(a2); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1119 return (object *) a3; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1120 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1121 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1122 shiftby = getlongvalue((object *)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
|
1123 if (shiftby == -1L && err_occurred()) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1124 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1125 if (shiftby < 0) { |
| 475 | 1126 err_setstr(ValueError, "negative shift count"); |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1127 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1128 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1129 wordshift = shiftby / SHIFT; |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1130 newsize = ABS(a->ob_size) - wordshift; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1131 if (newsize <= 0) { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1132 z = alloclongobject(0); |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1133 return (object *)z; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1134 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1135 loshift = shiftby % SHIFT; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1136 hishift = SHIFT - loshift; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1137 lomask = ((digit)1 << hishift) - 1; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1138 himask = MASK ^ lomask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1139 z = alloclongobject(newsize); |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1140 if (z == NULL) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1141 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1142 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
|
1143 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
|
1144 for (i = 0, j = wordshift; i < newsize; 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
|
1145 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1146 if (i+1 < newsize) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1147 z->ob_digit[i] |= |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1148 (a->ob_digit[j+1] << hishift) & himask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1149 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1150 return (object *) 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
|
1151 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1152 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1153 static object * |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1154 long_lshift(a, b) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1155 longobject *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
|
1156 longobject *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
|
1157 { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1158 longobject *z; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1159 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
|
1160 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
|
1161 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
|
1162 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1163 shiftby = getlongvalue((object *)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
|
1164 if (shiftby == -1L && err_occurred()) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1165 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1166 if (shiftby < 0) { |
| 475 | 1167 err_setstr(ValueError, "negative shift count"); |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1168 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1169 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1170 if (shiftby > MASK) { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1171 err_setstr(ValueError, "outrageous left shift count"); |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1172 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1173 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1174 if (shiftby % SHIFT == 0) { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1175 wordshift = shiftby / SHIFT; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1176 loshift = 0; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1177 hishift = SHIFT; |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1178 newsize = ABS(a->ob_size) + wordshift; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1179 lomask = MASK; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1180 himask = 0; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1181 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1182 else { |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1183 wordshift = shiftby / SHIFT + 1; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1184 loshift = SHIFT - shiftby%SHIFT; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1185 hishift = shiftby % SHIFT; |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1186 newsize = ABS(a->ob_size) + wordshift; |
|
443
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1187 lomask = ((digit)1 << hishift) - 1; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1188 himask = MASK ^ lomask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1189 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1190 z = alloclongobject(newsize); |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1191 if (z == NULL) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1192 return NULL; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1193 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
|
1194 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
|
1195 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
|
1196 z->ob_digit[i] = 0; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1197 for (i = wordshift, j = 0; i < newsize; 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
|
1198 if (i > 0) |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1199 z->ob_digit[i-1] |= |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1200 (a->ob_digit[j] << hishift) & himask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1201 z->ob_digit[i] = |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1202 (a->ob_digit[j] >> loshift) & lomask; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1203 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1204 return (object *) 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
|
1205 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1206 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1207 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1208 /* 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
|
1209 |
|
546
937cbc0be6ec
Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
1210 #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
|
1211 #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
|
1212 |
| 613 | 1213 static object *long_bitwise PROTO((longobject *, int, longobject *)); |
| 1214 static object * | |
| 1215 long_bitwise(a, op, 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
|
1216 longobject *a; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1217 int op; /* '&', '|', '^' */ |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1218 longobject *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
|
1219 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1220 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
|
1221 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
|
1222 int size_a, size_b, size_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
|
1223 longobject *z; |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1224 int i; |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1225 digit diga, digb; |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1226 object *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
|
1227 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1228 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
|
1229 a = (longobject *) long_invert(a); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1230 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
|
1231 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1232 else { |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1233 INCREF(a); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1234 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
|
1235 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1236 if (b->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
|
1237 b = (longobject *) long_invert(b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1238 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
|
1239 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1240 else { |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1241 INCREF(b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1242 maskb = 0; |
|
546
937cbc0be6ec
Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
1243 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1244 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1245 size_a = 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
|
1246 size_b = b->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
|
1247 size_z = MAX(size_a, size_b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1248 z = alloclongobject(size_z); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1249 if (a == NULL || b == NULL || z == NULL) { |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1250 XDECREF(a); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1251 XDECREF(b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1252 XDECREF(z); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1253 return 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
|
1254 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1255 |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1256 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
|
1257 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
|
1258 case '^': |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1259 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
|
1260 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
|
1261 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
|
1262 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1263 break; |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1264 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
|
1265 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
|
1266 op = '|'; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1267 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
|
1268 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
|
1269 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
|
1270 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1271 break; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1272 case '|': |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1273 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
|
1274 op = '&'; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1275 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
|
1276 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
|
1277 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
|
1278 } |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1279 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
|
1280 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1281 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1282 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
|
1283 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
|
1284 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
|
1285 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
|
1286 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
|
1287 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
|
1288 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
|
1289 } |
|
546
937cbc0be6ec
Fixed bug in long masking ops.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
1290 } |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1291 |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1292 DECREF(a); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1293 DECREF(b); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1294 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
|
1295 if (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
|
1296 return (object *) z; |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1297 v = long_invert(z); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1298 DECREF(z); |
|
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1299 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
|
1300 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1301 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1302 static object * |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1303 long_and(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
|
1304 longobject *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
|
1305 longobject *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
|
1306 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1307 return long_bitwise(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
|
1308 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1309 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1310 static object * |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1311 long_xor(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
|
1312 longobject *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
|
1313 longobject *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
|
1314 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1315 return long_bitwise(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
|
1316 } |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1317 |
|
9f85beb1f7c8
Made the sign use one's complement; implemented shifting and masking operators.
Guido van Rossum <guido@python.org>
parents:
406
diff
changeset
|
1318 static object * |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1319 long_or(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
|
1320 longobject *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
|
1321 longobject *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
|
1322 { |
|
590
f11fae8ee1e4
Changed to 2's complement bitwise ops. Got rid of ZABS etc.
Guido van Rossum <guido@python.org>
parents:
546
diff
changeset
|
1323 return long_bitwise(a, '|', b); |
| 242 | 1324 } |
| 1325 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1326 static int |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1327 long_coerce(pv, pw) |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1328 object **pv; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1329 object **pw; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1330 { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1331 if (is_intobject(*pw)) { |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1332 *pw = newlongobject(getintvalue(*pw)); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1333 INCREF(*pv); |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1334 return 0; |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1335 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1336 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
|
1337 } |
|
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1338 |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1339 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1340 long_int(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1341 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1342 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1343 long x; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1344 x = getlongvalue(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1345 if (err_occurred()) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1346 return NULL; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1347 return newintobject(x); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1348 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1349 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1350 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1351 long_long(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1352 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1353 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1354 INCREF(v); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1355 return v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1356 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1357 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1358 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1359 long_float(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1360 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1361 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1362 return newfloatobject(dgetlongvalue(v)); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1363 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1364 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1365 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1366 long_oct(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1367 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1368 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1369 return long_format(v, 8); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1370 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1371 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1372 static object * |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1373 long_hex(v) |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1374 object *v; |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1375 { |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1376 return long_format(v, 16); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1377 } |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1378 |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1379 |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1380 #define UF (unaryfunc) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1381 #define BF (binaryfunc) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1382 #define TF (ternaryfunc) |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1383 #define IF (inquiry) |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1384 |
| 230 | 1385 static number_methods long_as_number = { |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1386 BF long_add, /*nb_add*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1387 BF long_sub, /*nb_subtract*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1388 BF long_mul, /*nb_multiply*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1389 BF long_div, /*nb_divide*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1390 BF long_mod, /*nb_remainder*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1391 BF long_divmod, /*nb_divmod*/ |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1392 TF long_pow, /*nb_power*/ |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1393 UF long_neg, /*nb_negative*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1394 UF long_pos, /*tp_positive*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1395 UF long_abs, /*tp_absolute*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1396 IF long_nonzero,/*tp_nonzero*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1397 UF long_invert, /*nb_invert*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1398 BF long_lshift, /*nb_lshift*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1399 BF long_rshift, /*nb_rshift*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1400 BF long_and, /*nb_and*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1401 BF long_xor, /*nb_xor*/ |
|
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1402 BF long_or, /*nb_or*/ |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
1403 (int (*) FPROTO((object **, object **))) |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1404 (coercion)long_coerce, /*nb_coerce*/ |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1405 UF long_int, /*nb_int*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1406 UF long_long, /*nb_long*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1407 UF long_float, /*nb_float*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1408 UF long_oct, /*nb_oct*/ |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
958
diff
changeset
|
1409 UF long_hex, /*nb_hex*/ |
| 230 | 1410 }; |
| 1411 | |
| 1412 typeobject Longtype = { | |
| 1413 OB_HEAD_INIT(&Typetype) | |
| 1414 0, | |
| 1415 "long int", | |
| 1416 sizeof(longobject) - sizeof(digit), | |
| 1417 sizeof(digit), | |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1418 (destructor)long_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
1419 0, /*tp_print*/ |
| 230 | 1420 0, /*tp_getattr*/ |
| 1421 0, /*tp_setattr*/ | |
|
743
dbcb49d742a0
Lint stuff (involves casts, yuck!)
Guido van Rossum <guido@python.org>
parents:
613
diff
changeset
|
1422 (int (*) FPROTO((object *, object *))) |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1423 (cmpfunc)long_compare, /*tp_compare*/ |
|
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1424 (reprfunc)long_repr, /*tp_repr*/ |
| 230 | 1425 &long_as_number,/*tp_as_number*/ |
| 1426 0, /*tp_as_sequence*/ | |
| 1427 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1428 (long (*) FPROTO((object *))) |
|
1837
87364d9e3444
New patches by Andrew to fix various problems
Guido van Rossum <guido@python.org>
parents:
1391
diff
changeset
|
1429 (hashfunc)long_hash, /*tp_hash*/ |
| 230 | 1430 }; |
