Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame^] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Long (arbitrary precision) integer object implementation */ |
| 33 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 34 | /* XXX The functional organization of this file is terrible */ |
| 35 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 36 | #include "allobjects.h" |
| 37 | #include "longintrepr.h" |
Guido van Rossum | 687ec18 | 1995-03-04 22:43:47 +0000 | [diff] [blame] | 38 | #include "mymath.h" |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 39 | #include <assert.h> |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 40 | #include <ctype.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 *)); |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 49 | static object *long_format PROTO((object *aa, int base)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 51 | static int ticker; /* XXX Could be shared with ceval? */ |
| 52 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 53 | #define SIGCHECK(block) \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 54 | if (--ticker < 0) { \ |
| 55 | ticker = 100; \ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 56 | if (sigcheck()) { block; } \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 63 | static longobject * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 64 | long_normalize(v) |
| 65 | register longobject *v; |
| 66 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 67 | int j = ABS(v->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 68 | register int i = j; |
| 69 | |
| 70 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 71 | --i; |
| 72 | if (i != j) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 73 | v->ob_size = (v->ob_size < 0) ? -(i) : i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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' */ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 94 | /* XXX On 64 bit machines this isn't true!!! */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 95 | longobject *v = alloclongobject(3); |
| 96 | if (v != NULL) { |
| 97 | if (ival < 0) { |
| 98 | ival = -ival; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 99 | v->ob_size = -(v->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 100 | } |
| 101 | v->ob_digit[0] = ival & MASK; |
| 102 | v->ob_digit[1] = (ival >> SHIFT) & MASK; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 103 | v->ob_digit[2] = ((unsigned long)ival >> (2*SHIFT)) & MASK; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 104 | v = long_normalize(v); |
| 105 | } |
| 106 | return (object *)v; |
| 107 | } |
| 108 | |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 109 | /* Create a new long int object from a C double */ |
| 110 | |
| 111 | object * |
Guido van Rossum | 687ec18 | 1995-03-04 22:43:47 +0000 | [diff] [blame] | 112 | #ifdef MPW |
| 113 | dnewlongobject(double dval) |
| 114 | #else |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 115 | dnewlongobject(dval) |
| 116 | double dval; |
Guido van Rossum | 687ec18 | 1995-03-04 22:43:47 +0000 | [diff] [blame] | 117 | #endif /* MPW */ |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 118 | { |
| 119 | longobject *v; |
| 120 | double frac; |
| 121 | int i, ndig, expo, neg; |
| 122 | neg = 0; |
| 123 | if (dval < 0.0) { |
| 124 | neg = 1; |
| 125 | dval = -dval; |
| 126 | } |
| 127 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 128 | if (expo <= 0) |
| 129 | return newlongobject(0L); |
| 130 | ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ |
| 131 | v = alloclongobject(ndig); |
| 132 | if (v == NULL) |
| 133 | return NULL; |
| 134 | frac = ldexp(frac, (expo-1) % SHIFT + 1); |
| 135 | for (i = ndig; --i >= 0; ) { |
| 136 | long bits = (long)frac; |
| 137 | v->ob_digit[i] = bits; |
| 138 | frac = frac - (double)bits; |
| 139 | frac = ldexp(frac, SHIFT); |
| 140 | } |
| 141 | if (neg) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 142 | v->ob_size = -(v->ob_size); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 143 | return (object *)v; |
| 144 | } |
| 145 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 167 | i = -(i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 168 | } |
| 169 | while (--i >= 0) { |
| 170 | prev = x; |
| 171 | x = (x << SHIFT) + v->ob_digit[i]; |
| 172 | if ((x >> SHIFT) != prev) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 173 | err_setstr(OverflowError, |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 202 | i = -(i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 203 | } |
| 204 | while (--i >= 0) { |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 205 | x = x*multiplier + (double)v->ob_digit[i]; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 206 | } |
| 207 | return x * sign; |
| 208 | } |
| 209 | |
| 210 | /* Multiply by a single digit, ignoring the sign. */ |
| 211 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 212 | static longobject * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 213 | mul1(a, n) |
| 214 | longobject *a; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 215 | wdigit n; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 222 | static longobject * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 223 | muladd1(a, n, extra) |
| 224 | longobject *a; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 225 | wdigit n; |
| 226 | wdigit extra; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 227 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 228 | int size_a = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 248 | static longobject * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 249 | divrem1(a, n, prem) |
| 250 | longobject *a; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 251 | wdigit n; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 252 | digit *prem; |
| 253 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 254 | int size = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 273 | Return a string object. |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 274 | If base is 8 or 16, add the proper prefix '0' or '0x'. |
| 275 | External linkage: used in bltinmodule.c by hex() and oct(). */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 277 | static object * |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 278 | long_format(aa, base) |
| 279 | object *aa; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 280 | int base; |
| 281 | { |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 282 | register longobject *a = (longobject *)aa; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 283 | stringobject *str; |
| 284 | int i; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 285 | int size_a = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 286 | char *p; |
| 287 | int bits; |
| 288 | char sign = '\0'; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 289 | |
| 290 | if (a == NULL || !is_longobject(a)) { |
| 291 | err_badcall(); |
| 292 | return NULL; |
| 293 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 303 | i = 6 + (size_a*SHIFT + bits-1) / bits; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 304 | str = (stringobject *) newsizedstringobject((char *)0, i); |
| 305 | if (str == NULL) |
| 306 | return NULL; |
| 307 | p = GETSTRINGVALUE(str) + i; |
| 308 | *p = '\0'; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 309 | *--p = 'L'; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 310 | if (a->ob_size < 0) |
| 311 | sign = '-'; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 330 | SIGCHECK({ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 331 | DECREF(a); |
| 332 | DECREF(str); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 333 | return NULL; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 334 | }) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 335 | } while (ABS(a->ob_size) != 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 336 | DECREF(a); |
Guido van Rossum | 2c47542 | 1992-08-14 15:13:07 +0000 | [diff] [blame] | 337 | if (base == 8) { |
| 338 | if (size_a != 0) |
| 339 | *--p = '0'; |
| 340 | } |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 341 | else if (base == 16) { |
| 342 | *--p = 'x'; |
| 343 | *--p = '0'; |
| 344 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 345 | else if (base != 10) { |
| 346 | *--p = '#'; |
| 347 | *--p = '0' + base%10; |
| 348 | if (base > 10) |
| 349 | *--p = '0' + base/10; |
| 350 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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'); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 358 | q--; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 359 | resizestring((object **)&str, (int) (q - GETSTRINGVALUE(str))); |
| 360 | } |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 361 | return (object *)str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 3535f6e | 1995-01-17 16:34:13 +0000 | [diff] [blame] | 364 | #if 0 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 365 | /* Convert a string to a long int object, in a given base. |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 366 | Base zero implies a default depending on the number. |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 367 | External linkage: used in compile.c and stropmodule.c. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 368 | |
| 369 | object * |
| 370 | long_scan(str, base) |
| 371 | char *str; |
| 372 | int base; |
| 373 | { |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 374 | return long_escan(str, (char **)NULL, base); |
| 375 | } |
Guido van Rossum | 3535f6e | 1995-01-17 16:34:13 +0000 | [diff] [blame] | 376 | #endif |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 377 | |
| 378 | object * |
| 379 | long_escan(str, pend, base) |
| 380 | char *str; |
| 381 | char **pend; |
| 382 | int base; |
| 383 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 384 | int sign = 1; |
| 385 | longobject *z; |
| 386 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 387 | if (base != 0 && base < 2 || base > 36) { |
| 388 | err_setstr(ValueError, "invalid base for long literal"); |
| 389 | return NULL; |
| 390 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 391 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 392 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 393 | if (*str == '+') |
| 394 | ++str; |
| 395 | else if (*str == '-') { |
| 396 | ++str; |
| 397 | sign = -1; |
| 398 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 399 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 400 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 428 | if (sign < 0 && z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 429 | z->ob_size = -(z->ob_size); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 430 | if (pend) |
| 431 | *pend = str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 432 | return (object *) z; |
| 433 | } |
| 434 | |
| 435 | static longobject *x_divrem PROTO((longobject *, longobject *, longobject **)); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 436 | static object *long_pos PROTO((longobject *)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 437 | static long_divrem PROTO((longobject *, longobject *, |
| 438 | longobject **, longobject **)); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 439 | |
| 440 | /* Long division with remainder, top-level routine */ |
| 441 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 442 | static int |
| 443 | long_divrem(a, b, pdiv, prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 444 | longobject *a, *b; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 445 | longobject **pdiv; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 446 | longobject **prem; |
| 447 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 448 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 449 | longobject *z; |
| 450 | |
| 451 | if (size_b == 0) { |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 452 | err_setstr(ZeroDivisionError, "long division or modulo"); |
| 453 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 454 | } |
| 455 | if (size_a < size_b || |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 456 | size_a == size_b && |
| 457 | a->ob_digit[size_a-1] < b->ob_digit[size_b-1]) { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 458 | /* |a| < |b|. */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 459 | *pdiv = alloclongobject(0); |
| 460 | INCREF(a); |
| 461 | *prem = (longobject *) a; |
| 462 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 463 | } |
| 464 | if (size_b == 1) { |
| 465 | digit rem = 0; |
| 466 | z = divrem1(a, b->ob_digit[0], &rem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 467 | if (z == NULL) |
| 468 | return -1; |
| 469 | *prem = (longobject *) newlongobject((long)rem); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 470 | } |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 471 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 472 | z = x_divrem(a, b, prem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 473 | if (z == NULL) |
| 474 | return -1; |
| 475 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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. */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 488 | /* Unsigned long division with remainder -- the algorithm */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 489 | |
| 490 | static longobject * |
| 491 | x_divrem(v1, w1, prem) |
| 492 | longobject *v1, *w1; |
| 493 | longobject **prem; |
| 494 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 495 | int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 509 | assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */ |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 510 | assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 512 | size_v = ABS(v->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 518 | stwodigits carry = 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 519 | int i; |
| 520 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 521 | SIGCHECK({ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 522 | DECREF(a); |
| 523 | a = NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 524 | break; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 525 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 568 | if (a == NULL) |
| 569 | *prem = NULL; |
| 570 | else { |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 571 | a = long_normalize(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 572 | *prem = divrem1(v, d, &d); |
| 573 | /* d receives the (unused) remainder */ |
| 574 | if (*prem == NULL) { |
| 575 | DECREF(a); |
| 576 | a = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 577 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 578 | } |
| 579 | DECREF(v); |
| 580 | DECREF(w); |
| 581 | return a; |
| 582 | } |
| 583 | |
| 584 | /* Methods */ |
| 585 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 586 | /* Forward */ |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 587 | static void long_dealloc PROTO((object *)); |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 588 | static object *long_repr PROTO((object *)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 589 | static int long_compare PROTO((longobject *, longobject *)); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 590 | static long long_hash PROTO((longobject *)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 *)); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 598 | static object *long_pow PROTO((longobject *, longobject *, longobject *)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 610 | static void |
| 611 | long_dealloc(v) |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 612 | object *v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 613 | { |
| 614 | DEL(v); |
| 615 | } |
| 616 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 617 | static object * |
| 618 | long_repr(v) |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 619 | object *v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 620 | { |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 621 | return long_format(v, 10); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static int |
| 625 | long_compare(a, b) |
| 626 | longobject *a, *b; |
| 627 | { |
| 628 | int sign; |
| 629 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 630 | if (a->ob_size != b->ob_size) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 631 | if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 632 | sign = 0; |
| 633 | else |
| 634 | sign = a->ob_size - b->ob_size; |
| 635 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 636 | else { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 637 | int i = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 638 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 639 | ; |
| 640 | if (i < 0) |
| 641 | sign = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 642 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 643 | sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 644 | if (a->ob_size < 0) |
| 645 | sign = -sign; |
| 646 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 647 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 648 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 651 | static long |
| 652 | long_hash(v) |
| 653 | longobject *v; |
| 654 | { |
| 655 | long x; |
| 656 | int i, sign; |
| 657 | |
| 658 | /* This is designed so that Python ints and longs with the |
| 659 | same value hash to the same value, otherwise comparisons |
| 660 | of mapping keys will turn out weird */ |
| 661 | i = v->ob_size; |
| 662 | sign = 1; |
| 663 | x = 0; |
| 664 | if (i < 0) { |
| 665 | sign = -1; |
| 666 | i = -(i); |
| 667 | } |
| 668 | while (--i >= 0) { |
| 669 | /* Force a 32-bit circular shift */ |
| 670 | x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); |
| 671 | x += v->ob_digit[i]; |
| 672 | } |
| 673 | x = x * sign; |
| 674 | if (x == -1) |
| 675 | x = -2; |
| 676 | return x; |
| 677 | } |
| 678 | |
| 679 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 687 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 723 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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. */ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 754 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 765 | if (sign < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 766 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 767 | return long_normalize(z); |
| 768 | } |
| 769 | |
| 770 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 771 | long_add(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 772 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 773 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 774 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 775 | longobject *z; |
| 776 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 777 | if (a->ob_size < 0) { |
| 778 | if (b->ob_size < 0) { |
| 779 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 780 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 781 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 796 | long_sub(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 797 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 798 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 799 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 800 | longobject *z; |
| 801 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 807 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 808 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 820 | long_mul(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 821 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 822 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 823 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 824 | int size_a; |
| 825 | int size_b; |
| 826 | longobject *z; |
| 827 | int i; |
| 828 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 829 | size_a = ABS(a->ob_size); |
| 830 | size_b = ABS(b->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 841 | SIGCHECK({ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 842 | DECREF(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 843 | return NULL; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 844 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 858 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 859 | if (b->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 860 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 861 | return (object *) long_normalize(z); |
| 862 | } |
| 863 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 876 | have different signs. We then subtract one from the 'div' |
| 877 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 878 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 908 | XDECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 909 | return -1; |
| 910 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 911 | DECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 944 | static object * |
| 945 | long_divmod(v, w) |
| 946 | longobject *v; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 947 | longobject *w; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 948 | { |
| 949 | object *z; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 950 | longobject *div, *mod; |
| 951 | if (l_divmod(v, w, &div, &mod) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 952 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 953 | z = newtupleobject(2); |
| 954 | if (z != NULL) { |
| 955 | settupleitem(z, 0, (object *) div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 956 | settupleitem(z, 1, (object *) mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 957 | } |
| 958 | else { |
| 959 | DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 960 | DECREF(mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 961 | } |
| 962 | return z; |
| 963 | } |
| 964 | |
| 965 | static object * |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 966 | long_pow(a, b, c) |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 967 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 968 | longobject *b; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 969 | longobject *c; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 970 | { |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 971 | longobject *z, *div, *mod; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 972 | int size_b, i; |
| 973 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 974 | size_b = b->ob_size; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 975 | if (size_b < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 976 | err_setstr(ValueError, "long integer to the negative power"); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 977 | return NULL; |
| 978 | } |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 979 | z = (longobject *)newlongobject(1L); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 980 | INCREF(a); |
| 981 | for (i = 0; i < size_b; ++i) { |
| 982 | digit bi = b->ob_digit[i]; |
| 983 | int j; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 984 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 985 | for (j = 0; j < SHIFT; ++j) { |
| 986 | longobject *temp; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 987 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 988 | if (bi & 1) { |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 989 | temp = (longobject *)long_mul(z, a); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 990 | DECREF(z); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 991 | if ((object*)c!=None && temp!=NULL) { |
| 992 | l_divmod(temp, c, &div, &mod); |
| 993 | XDECREF(div); |
| 994 | DECREF(temp); |
| 995 | temp = mod; |
| 996 | } |
| 997 | z = temp; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 998 | if (z == NULL) |
| 999 | break; |
| 1000 | } |
| 1001 | bi >>= 1; |
| 1002 | if (bi == 0 && i+1 == size_b) |
| 1003 | break; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1004 | temp = (longobject *)long_mul(a, a); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1005 | DECREF(a); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1006 | if ((object*)c!=None && temp!=NULL) { |
| 1007 | l_divmod(temp, c, &div, &mod); |
| 1008 | XDECREF(div); |
| 1009 | DECREF(temp); |
| 1010 | temp = mod; |
| 1011 | } |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1012 | a = temp; |
| 1013 | if (a == NULL) { |
| 1014 | DECREF(z); |
| 1015 | z = NULL; |
| 1016 | break; |
| 1017 | } |
| 1018 | } |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 1019 | if (a == NULL || z == NULL) |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | } |
| 1022 | XDECREF(a); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1023 | if ((object*)c!=None && z!=NULL) { |
| 1024 | l_divmod(z, c, &div, &mod); |
| 1025 | XDECREF(div); |
| 1026 | DECREF(z); |
| 1027 | z=mod; |
| 1028 | } |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1029 | return (object *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static object * |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1033 | long_invert(v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1034 | longobject *v; |
| 1035 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1036 | /* Implement ~x as -(x+1) */ |
| 1037 | longobject *x; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1038 | longobject *w; |
| 1039 | w = (longobject *)newlongobject(1L); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1040 | if (w == NULL) |
| 1041 | return NULL; |
| 1042 | x = (longobject *) long_add(v, w); |
| 1043 | DECREF(w); |
| 1044 | if (x == NULL) |
| 1045 | return NULL; |
| 1046 | if (x->ob_size != 0) |
| 1047 | x->ob_size = -(x->ob_size); |
| 1048 | return (object *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static object * |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1052 | long_pos(v) |
| 1053 | longobject *v; |
| 1054 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1055 | INCREF(v); |
| 1056 | return (object *)v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static object * |
| 1060 | long_neg(v) |
| 1061 | longobject *v; |
| 1062 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1063 | longobject *z; |
| 1064 | int i, n; |
| 1065 | n = ABS(v->ob_size); |
| 1066 | if (n == 0) { |
| 1067 | /* -0 == 0 */ |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1068 | INCREF(v); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1069 | return (object *) v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1070 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1071 | z = alloclongobject(ABS(n)); |
| 1072 | if (z == NULL) |
| 1073 | return NULL; |
| 1074 | for (i = 0; i < n; i++) |
| 1075 | z->ob_digit[i] = v->ob_digit[i]; |
| 1076 | z->ob_size = -(v->ob_size); |
| 1077 | return (object *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static object * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1081 | long_abs(v) |
| 1082 | longobject *v; |
| 1083 | { |
| 1084 | if (v->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1085 | return long_neg(v); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1086 | else { |
| 1087 | INCREF(v); |
| 1088 | return (object *)v; |
| 1089 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1092 | static int |
| 1093 | long_nonzero(v) |
| 1094 | longobject *v; |
| 1095 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1096 | return ABS(v->ob_size) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static object * |
| 1100 | long_rshift(a, b) |
| 1101 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1102 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1103 | { |
| 1104 | longobject *z; |
| 1105 | long shiftby; |
| 1106 | int newsize, wordshift, loshift, hishift, i, j; |
| 1107 | digit lomask, himask; |
| 1108 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1109 | if (a->ob_size < 0) { |
| 1110 | /* Right shifting negative numbers is harder */ |
| 1111 | longobject *a1, *a2, *a3; |
| 1112 | a1 = (longobject *) long_invert(a); |
| 1113 | if (a1 == NULL) return NULL; |
| 1114 | a2 = (longobject *) long_rshift(a1, b); |
| 1115 | DECREF(a1); |
| 1116 | if (a2 == NULL) return NULL; |
| 1117 | a3 = (longobject *) long_invert(a2); |
| 1118 | DECREF(a2); |
| 1119 | return (object *) a3; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1120 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1121 | |
| 1122 | shiftby = getlongvalue((object *)b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1123 | if (shiftby == -1L && err_occurred()) |
| 1124 | return NULL; |
| 1125 | if (shiftby < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1126 | err_setstr(ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1127 | return NULL; |
| 1128 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1129 | wordshift = shiftby / SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1130 | newsize = ABS(a->ob_size) - wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1131 | if (newsize <= 0) { |
| 1132 | z = alloclongobject(0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1133 | return (object *)z; |
| 1134 | } |
| 1135 | loshift = shiftby % SHIFT; |
| 1136 | hishift = SHIFT - loshift; |
| 1137 | lomask = ((digit)1 << hishift) - 1; |
| 1138 | himask = MASK ^ lomask; |
| 1139 | z = alloclongobject(newsize); |
| 1140 | if (z == NULL) |
| 1141 | return NULL; |
| 1142 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1143 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1144 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 1145 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 1146 | if (i+1 < newsize) |
| 1147 | z->ob_digit[i] |= |
| 1148 | (a->ob_digit[j+1] << hishift) & himask; |
| 1149 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1150 | return (object *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static object * |
| 1154 | long_lshift(a, b) |
| 1155 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1156 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1157 | { |
| 1158 | longobject *z; |
| 1159 | long shiftby; |
| 1160 | int newsize, wordshift, loshift, hishift, i, j; |
| 1161 | digit lomask, himask; |
| 1162 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1163 | shiftby = getlongvalue((object *)b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1164 | if (shiftby == -1L && err_occurred()) |
| 1165 | return NULL; |
| 1166 | if (shiftby < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1167 | err_setstr(ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1168 | return NULL; |
| 1169 | } |
| 1170 | if (shiftby > MASK) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1171 | err_setstr(ValueError, "outrageous left shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1172 | return NULL; |
| 1173 | } |
| 1174 | if (shiftby % SHIFT == 0) { |
| 1175 | wordshift = shiftby / SHIFT; |
| 1176 | loshift = 0; |
| 1177 | hishift = SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1178 | newsize = ABS(a->ob_size) + wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1179 | lomask = MASK; |
| 1180 | himask = 0; |
| 1181 | } |
| 1182 | else { |
| 1183 | wordshift = shiftby / SHIFT + 1; |
| 1184 | loshift = SHIFT - shiftby%SHIFT; |
| 1185 | hishift = shiftby % SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1186 | newsize = ABS(a->ob_size) + wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1187 | lomask = ((digit)1 << hishift) - 1; |
| 1188 | himask = MASK ^ lomask; |
| 1189 | } |
| 1190 | z = alloclongobject(newsize); |
| 1191 | if (z == NULL) |
| 1192 | return NULL; |
| 1193 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1194 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1195 | for (i = 0; i < wordshift; i++) |
| 1196 | z->ob_digit[i] = 0; |
| 1197 | for (i = wordshift, j = 0; i < newsize; i++, j++) { |
| 1198 | if (i > 0) |
| 1199 | z->ob_digit[i-1] |= |
| 1200 | (a->ob_digit[j] << hishift) & himask; |
| 1201 | z->ob_digit[i] = |
| 1202 | (a->ob_digit[j] >> loshift) & lomask; |
| 1203 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1204 | return (object *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1207 | |
| 1208 | /* Bitwise and/xor/or operations */ |
| 1209 | |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1210 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 1211 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 1212 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1213 | static object *long_bitwise PROTO((longobject *, int, longobject *)); |
| 1214 | static object * |
| 1215 | long_bitwise(a, op, b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1216 | longobject *a; |
| 1217 | int op; /* '&', '|', '^' */ |
| 1218 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1219 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1220 | digit maska, maskb; /* 0 or MASK */ |
| 1221 | int negz; |
| 1222 | int size_a, size_b, size_z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1223 | longobject *z; |
| 1224 | int i; |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1225 | digit diga, digb; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1226 | object *v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1227 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1228 | if (a->ob_size < 0) { |
| 1229 | a = (longobject *) long_invert(a); |
| 1230 | maska = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1231 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1232 | else { |
| 1233 | INCREF(a); |
| 1234 | maska = 0; |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1235 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1236 | if (b->ob_size < 0) { |
| 1237 | b = (longobject *) long_invert(b); |
| 1238 | maskb = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1239 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1240 | else { |
| 1241 | INCREF(b); |
| 1242 | maskb = 0; |
| 1243 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1244 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1245 | size_a = a->ob_size; |
| 1246 | size_b = b->ob_size; |
| 1247 | size_z = MAX(size_a, size_b); |
| 1248 | z = alloclongobject(size_z); |
| 1249 | if (a == NULL || b == NULL || z == NULL) { |
| 1250 | XDECREF(a); |
| 1251 | XDECREF(b); |
| 1252 | XDECREF(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1253 | return NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1254 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1255 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1256 | negz = 0; |
| 1257 | switch (op) { |
| 1258 | case '^': |
| 1259 | if (maska != maskb) { |
| 1260 | maska ^= MASK; |
| 1261 | negz = -1; |
| 1262 | } |
| 1263 | break; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1264 | case '&': |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1265 | if (maska && maskb) { |
| 1266 | op = '|'; |
| 1267 | maska ^= MASK; |
| 1268 | maskb ^= MASK; |
| 1269 | negz = -1; |
| 1270 | } |
| 1271 | break; |
| 1272 | case '|': |
| 1273 | if (maska || maskb) { |
| 1274 | op = '&'; |
| 1275 | maska ^= MASK; |
| 1276 | maskb ^= MASK; |
| 1277 | negz = -1; |
| 1278 | } |
| 1279 | break; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1280 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1281 | |
| 1282 | for (i = 0; i < size_z; ++i) { |
| 1283 | diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; |
| 1284 | digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; |
| 1285 | switch (op) { |
| 1286 | case '&': z->ob_digit[i] = diga & digb; break; |
| 1287 | case '|': z->ob_digit[i] = diga | digb; break; |
| 1288 | case '^': z->ob_digit[i] = diga ^ digb; break; |
| 1289 | } |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1290 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1291 | |
| 1292 | DECREF(a); |
| 1293 | DECREF(b); |
| 1294 | z = long_normalize(z); |
| 1295 | if (negz == 0) |
| 1296 | return (object *) z; |
| 1297 | v = long_invert(z); |
| 1298 | DECREF(z); |
| 1299 | return v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1302 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1303 | long_and(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1304 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1305 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1306 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1307 | return long_bitwise(a, '&', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1311 | long_xor(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1312 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1313 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1314 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1315 | return long_bitwise(a, '^', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1319 | long_or(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1320 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1321 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1322 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1323 | return long_bitwise(a, '|', b); |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1326 | static int |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1327 | long_coerce(pv, pw) |
| 1328 | object **pv; |
| 1329 | object **pw; |
| 1330 | { |
| 1331 | if (is_intobject(*pw)) { |
| 1332 | *pw = newlongobject(getintvalue(*pw)); |
| 1333 | INCREF(*pv); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | return 1; /* Can't do it */ |
| 1337 | } |
| 1338 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1339 | static object * |
| 1340 | long_int(v) |
| 1341 | object *v; |
| 1342 | { |
| 1343 | long x; |
| 1344 | x = getlongvalue(v); |
| 1345 | if (err_occurred()) |
| 1346 | return NULL; |
| 1347 | return newintobject(x); |
| 1348 | } |
| 1349 | |
| 1350 | static object * |
| 1351 | long_long(v) |
| 1352 | object *v; |
| 1353 | { |
| 1354 | INCREF(v); |
| 1355 | return v; |
| 1356 | } |
| 1357 | |
| 1358 | static object * |
| 1359 | long_float(v) |
| 1360 | object *v; |
| 1361 | { |
| 1362 | return newfloatobject(dgetlongvalue(v)); |
| 1363 | } |
| 1364 | |
| 1365 | static object * |
| 1366 | long_oct(v) |
| 1367 | object *v; |
| 1368 | { |
| 1369 | return long_format(v, 8); |
| 1370 | } |
| 1371 | |
| 1372 | static object * |
| 1373 | long_hex(v) |
| 1374 | object *v; |
| 1375 | { |
| 1376 | return long_format(v, 16); |
| 1377 | } |
| 1378 | |
| 1379 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1380 | #define UF (unaryfunc) |
| 1381 | #define BF (binaryfunc) |
| 1382 | #define TF (ternaryfunc) |
| 1383 | #define IF (inquiry) |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1384 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1385 | static number_methods long_as_number = { |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1386 | BF long_add, /*nb_add*/ |
| 1387 | BF long_sub, /*nb_subtract*/ |
| 1388 | BF long_mul, /*nb_multiply*/ |
| 1389 | BF long_div, /*nb_divide*/ |
| 1390 | BF long_mod, /*nb_remainder*/ |
| 1391 | BF long_divmod, /*nb_divmod*/ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1392 | TF long_pow, /*nb_power*/ |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1393 | UF long_neg, /*nb_negative*/ |
| 1394 | UF long_pos, /*tp_positive*/ |
| 1395 | UF long_abs, /*tp_absolute*/ |
| 1396 | IF long_nonzero,/*tp_nonzero*/ |
| 1397 | UF long_invert, /*nb_invert*/ |
| 1398 | BF long_lshift, /*nb_lshift*/ |
| 1399 | BF long_rshift, /*nb_rshift*/ |
| 1400 | BF long_and, /*nb_and*/ |
| 1401 | BF long_xor, /*nb_xor*/ |
| 1402 | BF long_or, /*nb_or*/ |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1403 | (int (*) FPROTO((object **, object **))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1404 | (coercion)long_coerce, /*nb_coerce*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1405 | UF long_int, /*nb_int*/ |
| 1406 | UF long_long, /*nb_long*/ |
| 1407 | UF long_float, /*nb_float*/ |
| 1408 | UF long_oct, /*nb_oct*/ |
| 1409 | UF long_hex, /*nb_hex*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1410 | }; |
| 1411 | |
| 1412 | typeobject Longtype = { |
| 1413 | OB_HEAD_INIT(&Typetype) |
| 1414 | 0, |
| 1415 | "long int", |
| 1416 | sizeof(longobject) - sizeof(digit), |
| 1417 | sizeof(digit), |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1418 | (destructor)long_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1419 | 0, /*tp_print*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1420 | 0, /*tp_getattr*/ |
| 1421 | 0, /*tp_setattr*/ |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1422 | (int (*) FPROTO((object *, object *))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1423 | (cmpfunc)long_compare, /*tp_compare*/ |
| 1424 | (reprfunc)long_repr, /*tp_repr*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1425 | &long_as_number,/*tp_as_number*/ |
| 1426 | 0, /*tp_as_sequence*/ |
| 1427 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1428 | (long (*) FPROTO((object *))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1429 | (hashfunc)long_hash, /*tp_hash*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1430 | }; |