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