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