Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 34679b7 | 1993-01-26 13:33:44 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 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 | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame^] | 264 | static object * |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 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 *)); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 558 | static long long_hash PROTO((longobject *)); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 559 | |
| 560 | static object *long_add PROTO((longobject *, longobject *)); |
| 561 | static object *long_sub PROTO((longobject *, longobject *)); |
| 562 | static object *long_mul PROTO((longobject *, longobject *)); |
| 563 | static object *long_div PROTO((longobject *, longobject *)); |
| 564 | static object *long_mod PROTO((longobject *, longobject *)); |
| 565 | static object *long_divmod PROTO((longobject *, longobject *)); |
| 566 | static object *long_pow PROTO((longobject *, longobject *)); |
| 567 | static object *long_neg PROTO((longobject *)); |
| 568 | static object *long_pos PROTO((longobject *)); |
| 569 | static object *long_abs PROTO((longobject *)); |
| 570 | static int long_nonzero PROTO((longobject *)); |
| 571 | static object *long_invert PROTO((longobject *)); |
| 572 | static object *long_lshift PROTO((longobject *, longobject *)); |
| 573 | static object *long_rshift PROTO((longobject *, longobject *)); |
| 574 | static object *long_and PROTO((longobject *, longobject *)); |
| 575 | static object *long_xor PROTO((longobject *, longobject *)); |
| 576 | static object *long_or PROTO((longobject *, longobject *)); |
| 577 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 578 | static void |
| 579 | long_dealloc(v) |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 580 | object *v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 581 | { |
| 582 | DEL(v); |
| 583 | } |
| 584 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 585 | static object * |
| 586 | long_repr(v) |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 587 | object *v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 588 | { |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 589 | return long_format(v, 10); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static int |
| 593 | long_compare(a, b) |
| 594 | longobject *a, *b; |
| 595 | { |
| 596 | int sign; |
| 597 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 598 | if (a->ob_size != b->ob_size) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 599 | if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 600 | sign = 0; |
| 601 | else |
| 602 | sign = a->ob_size - b->ob_size; |
| 603 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 604 | else { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 605 | int i = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 606 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 607 | ; |
| 608 | if (i < 0) |
| 609 | sign = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 610 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 611 | sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 612 | if (a->ob_size < 0) |
| 613 | sign = -sign; |
| 614 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 615 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 616 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 619 | static long |
| 620 | long_hash(v) |
| 621 | longobject *v; |
| 622 | { |
| 623 | long x; |
| 624 | int i, sign; |
| 625 | |
| 626 | /* This is designed so that Python ints and longs with the |
| 627 | same value hash to the same value, otherwise comparisons |
| 628 | of mapping keys will turn out weird */ |
| 629 | i = v->ob_size; |
| 630 | sign = 1; |
| 631 | x = 0; |
| 632 | if (i < 0) { |
| 633 | sign = -1; |
| 634 | i = -(i); |
| 635 | } |
| 636 | while (--i >= 0) { |
| 637 | /* Force a 32-bit circular shift */ |
| 638 | x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); |
| 639 | x += v->ob_digit[i]; |
| 640 | } |
| 641 | x = x * sign; |
| 642 | if (x == -1) |
| 643 | x = -2; |
| 644 | return x; |
| 645 | } |
| 646 | |
| 647 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 648 | /* Add the absolute values of two long integers. */ |
| 649 | |
| 650 | static longobject *x_add PROTO((longobject *, longobject *)); |
| 651 | static longobject * |
| 652 | x_add(a, b) |
| 653 | longobject *a, *b; |
| 654 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 655 | 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] | 656 | longobject *z; |
| 657 | int i; |
| 658 | digit carry = 0; |
| 659 | |
| 660 | /* Ensure a is the larger of the two: */ |
| 661 | if (size_a < size_b) { |
| 662 | { longobject *temp = a; a = b; b = temp; } |
| 663 | { int size_temp = size_a; size_a = size_b; size_b = size_temp; } |
| 664 | } |
| 665 | z = alloclongobject(size_a+1); |
| 666 | if (z == NULL) |
| 667 | return NULL; |
| 668 | for (i = 0; i < size_b; ++i) { |
| 669 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 670 | z->ob_digit[i] = carry & MASK; |
| 671 | /* The following assumes unsigned shifts don't |
| 672 | propagate the sign bit. */ |
| 673 | carry >>= SHIFT; |
| 674 | } |
| 675 | for (; i < size_a; ++i) { |
| 676 | carry += a->ob_digit[i]; |
| 677 | z->ob_digit[i] = carry & MASK; |
| 678 | carry >>= SHIFT; |
| 679 | } |
| 680 | z->ob_digit[i] = carry; |
| 681 | return long_normalize(z); |
| 682 | } |
| 683 | |
| 684 | /* Subtract the absolute values of two integers. */ |
| 685 | |
| 686 | static longobject *x_sub PROTO((longobject *, longobject *)); |
| 687 | static longobject * |
| 688 | x_sub(a, b) |
| 689 | longobject *a, *b; |
| 690 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 691 | 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] | 692 | longobject *z; |
| 693 | int i; |
| 694 | int sign = 1; |
| 695 | digit borrow = 0; |
| 696 | |
| 697 | /* Ensure a is the larger of the two: */ |
| 698 | if (size_a < size_b) { |
| 699 | sign = -1; |
| 700 | { longobject *temp = a; a = b; b = temp; } |
| 701 | { int size_temp = size_a; size_a = size_b; size_b = size_temp; } |
| 702 | } |
| 703 | else if (size_a == size_b) { |
| 704 | /* Find highest digit where a and b differ: */ |
| 705 | i = size_a; |
| 706 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 707 | ; |
| 708 | if (i < 0) |
| 709 | return alloclongobject(0); |
| 710 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 711 | sign = -1; |
| 712 | { longobject *temp = a; a = b; b = temp; } |
| 713 | } |
| 714 | size_a = size_b = i+1; |
| 715 | } |
| 716 | z = alloclongobject(size_a); |
| 717 | if (z == NULL) |
| 718 | return NULL; |
| 719 | for (i = 0; i < size_b; ++i) { |
| 720 | /* The following assumes unsigned arithmetic |
| 721 | works module 2**N for some N>SHIFT. */ |
| 722 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
| 723 | z->ob_digit[i] = borrow & MASK; |
| 724 | borrow >>= SHIFT; |
| 725 | borrow &= 1; /* Keep only one sign bit */ |
| 726 | } |
| 727 | for (; i < size_a; ++i) { |
| 728 | borrow = a->ob_digit[i] - borrow; |
| 729 | z->ob_digit[i] = borrow & MASK; |
| 730 | borrow >>= SHIFT; |
| 731 | } |
| 732 | assert(borrow == 0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 733 | if (sign < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 734 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 735 | return long_normalize(z); |
| 736 | } |
| 737 | |
| 738 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 739 | long_add(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 740 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 741 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 742 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 743 | longobject *z; |
| 744 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 745 | if (a->ob_size < 0) { |
| 746 | if (b->ob_size < 0) { |
| 747 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 748 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 749 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 750 | } |
| 751 | else |
| 752 | z = x_sub(b, a); |
| 753 | } |
| 754 | else { |
| 755 | if (b->ob_size < 0) |
| 756 | z = x_sub(a, b); |
| 757 | else |
| 758 | z = x_add(a, b); |
| 759 | } |
| 760 | return (object *)z; |
| 761 | } |
| 762 | |
| 763 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 764 | long_sub(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 765 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 766 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 767 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 768 | longobject *z; |
| 769 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 770 | if (a->ob_size < 0) { |
| 771 | if (b->ob_size < 0) |
| 772 | z = x_sub(a, b); |
| 773 | else |
| 774 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 775 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 776 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 777 | } |
| 778 | else { |
| 779 | if (b->ob_size < 0) |
| 780 | z = x_add(a, b); |
| 781 | else |
| 782 | z = x_sub(a, b); |
| 783 | } |
| 784 | return (object *)z; |
| 785 | } |
| 786 | |
| 787 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 788 | long_mul(a, b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 789 | longobject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 790 | longobject *b; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 791 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 792 | int size_a; |
| 793 | int size_b; |
| 794 | longobject *z; |
| 795 | int i; |
| 796 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 797 | size_a = ABS(a->ob_size); |
| 798 | size_b = ABS(b->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 799 | z = alloclongobject(size_a + size_b); |
| 800 | if (z == NULL) |
| 801 | return NULL; |
| 802 | for (i = 0; i < z->ob_size; ++i) |
| 803 | z->ob_digit[i] = 0; |
| 804 | for (i = 0; i < size_a; ++i) { |
| 805 | twodigits carry = 0; |
| 806 | twodigits f = a->ob_digit[i]; |
| 807 | int j; |
| 808 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 809 | INTRCHECK({ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 810 | DECREF(z); |
| 811 | err_set(KeyboardInterrupt); |
| 812 | return NULL; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 813 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 814 | for (j = 0; j < size_b; ++j) { |
| 815 | carry += z->ob_digit[i+j] + b->ob_digit[j] * f; |
| 816 | z->ob_digit[i+j] = carry & MASK; |
| 817 | carry >>= SHIFT; |
| 818 | } |
| 819 | for (; carry != 0; ++j) { |
| 820 | assert(i+j < z->ob_size); |
| 821 | carry += z->ob_digit[i+j]; |
| 822 | z->ob_digit[i+j] = carry & MASK; |
| 823 | carry >>= SHIFT; |
| 824 | } |
| 825 | } |
| 826 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 827 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 828 | if (b->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 829 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 830 | return (object *) long_normalize(z); |
| 831 | } |
| 832 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 833 | /* The / and % operators are now defined in terms of divmod(). |
| 834 | The expression a mod b has the value a - b*floor(a/b). |
| 835 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 836 | |a| by |b|, with the sign of a. This is also expressed |
| 837 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 838 | Some examples: |
| 839 | a b a rem b a mod b |
| 840 | 13 10 3 3 |
| 841 | -13 10 -3 7 |
| 842 | 13 -10 3 -7 |
| 843 | -13 -10 -3 -3 |
| 844 | 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] | 845 | have different signs. We then subtract one from the 'div' |
| 846 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 847 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 848 | static int l_divmod PROTO((longobject *, longobject *, |
| 849 | longobject **, longobject **)); |
| 850 | static int |
| 851 | l_divmod(v, w, pdiv, pmod) |
| 852 | longobject *v; |
| 853 | longobject *w; |
| 854 | longobject **pdiv; |
| 855 | longobject **pmod; |
| 856 | { |
| 857 | longobject *div, *mod; |
| 858 | |
| 859 | if (long_divrem(v, w, &div, &mod) < 0) |
| 860 | return -1; |
| 861 | if (mod->ob_size < 0 && w->ob_size > 0 || |
| 862 | mod->ob_size > 0 && w->ob_size < 0) { |
| 863 | longobject *temp; |
| 864 | longobject *one; |
| 865 | temp = (longobject *) long_add(mod, w); |
| 866 | DECREF(mod); |
| 867 | mod = temp; |
| 868 | if (mod == NULL) { |
| 869 | DECREF(div); |
| 870 | return -1; |
| 871 | } |
| 872 | one = (longobject *) newlongobject(1L); |
| 873 | if (one == NULL || |
| 874 | (temp = (longobject *) long_sub(div, one)) == NULL) { |
| 875 | DECREF(mod); |
| 876 | DECREF(div); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 877 | XDECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 878 | return -1; |
| 879 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 880 | DECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 881 | DECREF(div); |
| 882 | div = temp; |
| 883 | } |
| 884 | *pdiv = div; |
| 885 | *pmod = mod; |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | static object * |
| 890 | long_div(v, w) |
| 891 | longobject *v; |
| 892 | longobject *w; |
| 893 | { |
| 894 | longobject *div, *mod; |
| 895 | if (l_divmod(v, w, &div, &mod) < 0) |
| 896 | return NULL; |
| 897 | DECREF(mod); |
| 898 | return (object *)div; |
| 899 | } |
| 900 | |
| 901 | static object * |
| 902 | long_mod(v, w) |
| 903 | longobject *v; |
| 904 | longobject *w; |
| 905 | { |
| 906 | longobject *div, *mod; |
| 907 | if (l_divmod(v, w, &div, &mod) < 0) |
| 908 | return NULL; |
| 909 | DECREF(div); |
| 910 | return (object *)mod; |
| 911 | } |
| 912 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 913 | static object * |
| 914 | long_divmod(v, w) |
| 915 | longobject *v; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 916 | longobject *w; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 917 | { |
| 918 | object *z; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 919 | longobject *div, *mod; |
| 920 | if (l_divmod(v, w, &div, &mod) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 921 | return NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 922 | z = newtupleobject(2); |
| 923 | if (z != NULL) { |
| 924 | settupleitem(z, 0, (object *) div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 925 | settupleitem(z, 1, (object *) mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 926 | } |
| 927 | else { |
| 928 | DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 929 | DECREF(mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 930 | } |
| 931 | return z; |
| 932 | } |
| 933 | |
| 934 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 935 | long_pow(a, b) |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 936 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 937 | longobject *b; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 938 | { |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 939 | longobject *z; |
| 940 | int size_b, i; |
| 941 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 942 | size_b = b->ob_size; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 943 | if (size_b < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 944 | err_setstr(ValueError, "long integer to the negative power"); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 945 | return NULL; |
| 946 | } |
| 947 | |
| 948 | z = (longobject *)newlongobject(1L); |
| 949 | |
| 950 | INCREF(a); |
| 951 | for (i = 0; i < size_b; ++i) { |
| 952 | digit bi = b->ob_digit[i]; |
| 953 | int j; |
| 954 | |
| 955 | for (j = 0; j < SHIFT; ++j) { |
| 956 | longobject *temp; |
| 957 | |
| 958 | if (bi & 1) { |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 959 | temp = (longobject *)long_mul(z, a); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 960 | DECREF(z); |
| 961 | z = temp; |
| 962 | if (z == NULL) |
| 963 | break; |
| 964 | } |
| 965 | bi >>= 1; |
| 966 | if (bi == 0 && i+1 == size_b) |
| 967 | break; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 968 | temp = (longobject *)long_mul(a, a); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 969 | DECREF(a); |
| 970 | a = temp; |
| 971 | if (a == NULL) { |
| 972 | DECREF(z); |
| 973 | z = NULL; |
| 974 | break; |
| 975 | } |
| 976 | } |
| 977 | if (a == NULL) |
| 978 | break; |
| 979 | } |
| 980 | XDECREF(a); |
| 981 | return (object *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static object * |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 985 | long_invert(v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 986 | longobject *v; |
| 987 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 988 | /* Implement ~x as -(x+1) */ |
| 989 | longobject *x; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 990 | longobject *w; |
| 991 | w = (longobject *)newlongobject(1L); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 992 | if (w == NULL) |
| 993 | return NULL; |
| 994 | x = (longobject *) long_add(v, w); |
| 995 | DECREF(w); |
| 996 | if (x == NULL) |
| 997 | return NULL; |
| 998 | if (x->ob_size != 0) |
| 999 | x->ob_size = -(x->ob_size); |
| 1000 | return (object *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | static object * |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1004 | long_pos(v) |
| 1005 | longobject *v; |
| 1006 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1007 | INCREF(v); |
| 1008 | return (object *)v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static object * |
| 1012 | long_neg(v) |
| 1013 | longobject *v; |
| 1014 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1015 | longobject *z; |
| 1016 | int i, n; |
| 1017 | n = ABS(v->ob_size); |
| 1018 | if (n == 0) { |
| 1019 | /* -0 == 0 */ |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1020 | INCREF(v); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1021 | return (object *) v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1022 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1023 | z = alloclongobject(ABS(n)); |
| 1024 | if (z == NULL) |
| 1025 | return NULL; |
| 1026 | for (i = 0; i < n; i++) |
| 1027 | z->ob_digit[i] = v->ob_digit[i]; |
| 1028 | z->ob_size = -(v->ob_size); |
| 1029 | return (object *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static object * |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1033 | long_abs(v) |
| 1034 | longobject *v; |
| 1035 | { |
| 1036 | if (v->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1037 | return long_neg(v); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1038 | else { |
| 1039 | INCREF(v); |
| 1040 | return (object *)v; |
| 1041 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1044 | static int |
| 1045 | long_nonzero(v) |
| 1046 | longobject *v; |
| 1047 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1048 | return ABS(v->ob_size) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | static object * |
| 1052 | long_rshift(a, b) |
| 1053 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1054 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1055 | { |
| 1056 | longobject *z; |
| 1057 | long shiftby; |
| 1058 | int newsize, wordshift, loshift, hishift, i, j; |
| 1059 | digit lomask, himask; |
| 1060 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1061 | if (a->ob_size < 0) { |
| 1062 | /* Right shifting negative numbers is harder */ |
| 1063 | longobject *a1, *a2, *a3; |
| 1064 | a1 = (longobject *) long_invert(a); |
| 1065 | if (a1 == NULL) return NULL; |
| 1066 | a2 = (longobject *) long_rshift(a1, b); |
| 1067 | DECREF(a1); |
| 1068 | if (a2 == NULL) return NULL; |
| 1069 | a3 = (longobject *) long_invert(a2); |
| 1070 | DECREF(a2); |
| 1071 | return (object *) a3; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1072 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1073 | |
| 1074 | shiftby = getlongvalue((object *)b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1075 | if (shiftby == -1L && err_occurred()) |
| 1076 | return NULL; |
| 1077 | if (shiftby < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1078 | err_setstr(ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1079 | return NULL; |
| 1080 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1081 | wordshift = shiftby / SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1082 | newsize = ABS(a->ob_size) - wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1083 | if (newsize <= 0) { |
| 1084 | z = alloclongobject(0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1085 | return (object *)z; |
| 1086 | } |
| 1087 | loshift = shiftby % SHIFT; |
| 1088 | hishift = SHIFT - loshift; |
| 1089 | lomask = ((digit)1 << hishift) - 1; |
| 1090 | himask = MASK ^ lomask; |
| 1091 | z = alloclongobject(newsize); |
| 1092 | if (z == NULL) |
| 1093 | return NULL; |
| 1094 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1095 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1096 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 1097 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 1098 | if (i+1 < newsize) |
| 1099 | z->ob_digit[i] |= |
| 1100 | (a->ob_digit[j+1] << hishift) & himask; |
| 1101 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1102 | return (object *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static object * |
| 1106 | long_lshift(a, b) |
| 1107 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1108 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1109 | { |
| 1110 | longobject *z; |
| 1111 | long shiftby; |
| 1112 | int newsize, wordshift, loshift, hishift, i, j; |
| 1113 | digit lomask, himask; |
| 1114 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1115 | shiftby = getlongvalue((object *)b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1116 | if (shiftby == -1L && err_occurred()) |
| 1117 | return NULL; |
| 1118 | if (shiftby < 0) { |
Guido van Rossum | 87e7ea7 | 1991-12-10 14:00:03 +0000 | [diff] [blame] | 1119 | err_setstr(ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1120 | return NULL; |
| 1121 | } |
| 1122 | if (shiftby > MASK) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1123 | err_setstr(ValueError, "outrageous left shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1124 | return NULL; |
| 1125 | } |
| 1126 | if (shiftby % SHIFT == 0) { |
| 1127 | wordshift = shiftby / SHIFT; |
| 1128 | loshift = 0; |
| 1129 | hishift = 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 | lomask = MASK; |
| 1132 | himask = 0; |
| 1133 | } |
| 1134 | else { |
| 1135 | wordshift = shiftby / SHIFT + 1; |
| 1136 | loshift = SHIFT - shiftby%SHIFT; |
| 1137 | hishift = shiftby % SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1138 | newsize = ABS(a->ob_size) + wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1139 | lomask = ((digit)1 << hishift) - 1; |
| 1140 | himask = MASK ^ lomask; |
| 1141 | } |
| 1142 | z = alloclongobject(newsize); |
| 1143 | if (z == NULL) |
| 1144 | return NULL; |
| 1145 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1146 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1147 | for (i = 0; i < wordshift; i++) |
| 1148 | z->ob_digit[i] = 0; |
| 1149 | for (i = wordshift, j = 0; i < newsize; i++, j++) { |
| 1150 | if (i > 0) |
| 1151 | z->ob_digit[i-1] |= |
| 1152 | (a->ob_digit[j] << hishift) & himask; |
| 1153 | z->ob_digit[i] = |
| 1154 | (a->ob_digit[j] >> loshift) & lomask; |
| 1155 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1156 | return (object *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* Bitwise and/xor/or operations */ |
| 1161 | |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1162 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 1163 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 1164 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1165 | static object *long_bitwise PROTO((longobject *, int, longobject *)); |
| 1166 | static object * |
| 1167 | long_bitwise(a, op, b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1168 | longobject *a; |
| 1169 | int op; /* '&', '|', '^' */ |
| 1170 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1171 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1172 | digit maska, maskb; /* 0 or MASK */ |
| 1173 | int negz; |
| 1174 | int size_a, size_b, size_z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1175 | longobject *z; |
| 1176 | int i; |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1177 | digit diga, digb; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1178 | object *v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1180 | if (a->ob_size < 0) { |
| 1181 | a = (longobject *) long_invert(a); |
| 1182 | maska = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1183 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1184 | else { |
| 1185 | INCREF(a); |
| 1186 | maska = 0; |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1187 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1188 | if (b->ob_size < 0) { |
| 1189 | b = (longobject *) long_invert(b); |
| 1190 | maskb = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1191 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1192 | else { |
| 1193 | INCREF(b); |
| 1194 | maskb = 0; |
| 1195 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1196 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1197 | size_a = a->ob_size; |
| 1198 | size_b = b->ob_size; |
| 1199 | size_z = MAX(size_a, size_b); |
| 1200 | z = alloclongobject(size_z); |
| 1201 | if (a == NULL || b == NULL || z == NULL) { |
| 1202 | XDECREF(a); |
| 1203 | XDECREF(b); |
| 1204 | XDECREF(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1205 | return NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1206 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1207 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1208 | negz = 0; |
| 1209 | switch (op) { |
| 1210 | case '^': |
| 1211 | if (maska != maskb) { |
| 1212 | maska ^= MASK; |
| 1213 | negz = -1; |
| 1214 | } |
| 1215 | break; |
| 1216 | case '&': |
| 1217 | if (maska && maskb) { |
| 1218 | op = '|'; |
| 1219 | maska ^= MASK; |
| 1220 | maskb ^= MASK; |
| 1221 | negz = -1; |
| 1222 | } |
| 1223 | break; |
| 1224 | case '|': |
| 1225 | if (maska || maskb) { |
| 1226 | op = '&'; |
| 1227 | maska ^= MASK; |
| 1228 | maskb ^= MASK; |
| 1229 | negz = -1; |
| 1230 | } |
| 1231 | break; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1232 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1233 | |
| 1234 | for (i = 0; i < size_z; ++i) { |
| 1235 | diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; |
| 1236 | digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; |
| 1237 | switch (op) { |
| 1238 | case '&': z->ob_digit[i] = diga & digb; break; |
| 1239 | case '|': z->ob_digit[i] = diga | digb; break; |
| 1240 | case '^': z->ob_digit[i] = diga ^ digb; break; |
| 1241 | } |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1242 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1243 | |
| 1244 | DECREF(a); |
| 1245 | DECREF(b); |
| 1246 | z = long_normalize(z); |
| 1247 | if (negz == 0) |
| 1248 | return (object *) z; |
| 1249 | v = long_invert(z); |
| 1250 | DECREF(z); |
| 1251 | return v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1254 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1255 | long_and(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1256 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1257 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1258 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1259 | return long_bitwise(a, '&', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1263 | long_xor(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1264 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1265 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1266 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1267 | return long_bitwise(a, '^', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static object * |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1271 | long_or(a, b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1272 | longobject *a; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1273 | longobject *b; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1274 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1275 | return long_bitwise(a, '|', b); |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame^] | 1278 | static int |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1279 | long_coerce(pv, pw) |
| 1280 | object **pv; |
| 1281 | object **pw; |
| 1282 | { |
| 1283 | if (is_intobject(*pw)) { |
| 1284 | *pw = newlongobject(getintvalue(*pw)); |
| 1285 | INCREF(*pv); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | return 1; /* Can't do it */ |
| 1289 | } |
| 1290 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1291 | static object * |
| 1292 | long_int(v) |
| 1293 | object *v; |
| 1294 | { |
| 1295 | long x; |
| 1296 | x = getlongvalue(v); |
| 1297 | if (err_occurred()) |
| 1298 | return NULL; |
| 1299 | return newintobject(x); |
| 1300 | } |
| 1301 | |
| 1302 | static object * |
| 1303 | long_long(v) |
| 1304 | object *v; |
| 1305 | { |
| 1306 | INCREF(v); |
| 1307 | return v; |
| 1308 | } |
| 1309 | |
| 1310 | static object * |
| 1311 | long_float(v) |
| 1312 | object *v; |
| 1313 | { |
| 1314 | return newfloatobject(dgetlongvalue(v)); |
| 1315 | } |
| 1316 | |
| 1317 | static object * |
| 1318 | long_oct(v) |
| 1319 | object *v; |
| 1320 | { |
| 1321 | return long_format(v, 8); |
| 1322 | } |
| 1323 | |
| 1324 | static object * |
| 1325 | long_hex(v) |
| 1326 | object *v; |
| 1327 | { |
| 1328 | return long_format(v, 16); |
| 1329 | } |
| 1330 | |
| 1331 | |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1332 | #define UF (object* (*) FPROTO((object *))) /* Unary function */ |
| 1333 | #define BF (object* (*) FPROTO((object *, object *))) /* Binary function */ |
| 1334 | #define IF (int (*) FPROTO((object *))) /* Int function */ |
| 1335 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1336 | static number_methods long_as_number = { |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1337 | BF long_add, /*nb_add*/ |
| 1338 | BF long_sub, /*nb_subtract*/ |
| 1339 | BF long_mul, /*nb_multiply*/ |
| 1340 | BF long_div, /*nb_divide*/ |
| 1341 | BF long_mod, /*nb_remainder*/ |
| 1342 | BF long_divmod, /*nb_divmod*/ |
| 1343 | BF long_pow, /*nb_power*/ |
| 1344 | UF long_neg, /*nb_negative*/ |
| 1345 | UF long_pos, /*tp_positive*/ |
| 1346 | UF long_abs, /*tp_absolute*/ |
| 1347 | IF long_nonzero,/*tp_nonzero*/ |
| 1348 | UF long_invert, /*nb_invert*/ |
| 1349 | BF long_lshift, /*nb_lshift*/ |
| 1350 | BF long_rshift, /*nb_rshift*/ |
| 1351 | BF long_and, /*nb_and*/ |
| 1352 | BF long_xor, /*nb_xor*/ |
| 1353 | BF long_or, /*nb_or*/ |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1354 | (int (*) FPROTO((object **, object **))) |
| 1355 | long_coerce, /*nb_coerce*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1356 | UF long_int, /*nb_int*/ |
| 1357 | UF long_long, /*nb_long*/ |
| 1358 | UF long_float, /*nb_float*/ |
| 1359 | UF long_oct, /*nb_oct*/ |
| 1360 | UF long_hex, /*nb_hex*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1361 | }; |
| 1362 | |
| 1363 | typeobject Longtype = { |
| 1364 | OB_HEAD_INIT(&Typetype) |
| 1365 | 0, |
| 1366 | "long int", |
| 1367 | sizeof(longobject) - sizeof(digit), |
| 1368 | sizeof(digit), |
| 1369 | long_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1370 | 0, /*tp_print*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1371 | 0, /*tp_getattr*/ |
| 1372 | 0, /*tp_setattr*/ |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1373 | (int (*) FPROTO((object *, object *))) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1374 | long_compare, /*tp_compare*/ |
| 1375 | long_repr, /*tp_repr*/ |
| 1376 | &long_as_number,/*tp_as_number*/ |
| 1377 | 0, /*tp_as_sequence*/ |
| 1378 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1379 | (long (*) FPROTO((object *))) |
| 1380 | long_hash, /*tp_hash*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1381 | }; |