Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1 | |
| 2 | /* Long (arbitrary precision) integer object implementation */ |
| 3 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 4 | /* XXX The functional organization of this file is terrible */ |
| 5 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 6 | #include "Python.h" |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 7 | #include "longintrepr.h" |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 9 | #include <ctype.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 11 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 12 | |
| 13 | /* Forward */ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 14 | static PyLongObject *long_normalize(PyLongObject *); |
| 15 | static PyLongObject *mul1(PyLongObject *, wdigit); |
| 16 | static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 17 | static PyLongObject *divrem1(PyLongObject *, digit, digit *); |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 18 | static PyObject *long_format(PyObject *aa, int base, int addL); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 20 | static int ticker; /* XXX Could be shared with ceval? */ |
| 21 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 22 | #define SIGCHECK(PyTryBlock) \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 23 | if (--ticker < 0) { \ |
| 24 | ticker = 100; \ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | if (PyErr_CheckSignals()) { PyTryBlock; } \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 28 | /* Normalize (remove leading zeros from) a long int object. |
| 29 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 30 | of the algorithms used, this could save at most be one word anyway. */ |
| 31 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 33 | long_normalize(register PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 34 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 35 | int j = ABS(v->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 36 | register int i = j; |
| 37 | |
| 38 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 39 | --i; |
| 40 | if (i != j) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 41 | v->ob_size = (v->ob_size < 0) ? -(i) : i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 42 | return v; |
| 43 | } |
| 44 | |
| 45 | /* Allocate a new long int object with size digits. |
| 46 | Return NULL and set exception if we run out of memory. */ |
| 47 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 48 | PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 49 | _PyLong_New(int size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 50 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 51 | return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 54 | PyObject * |
| 55 | _PyLong_Copy(PyLongObject *src) |
| 56 | { |
| 57 | PyLongObject *result; |
| 58 | int i; |
| 59 | |
| 60 | assert(src != NULL); |
| 61 | i = src->ob_size; |
| 62 | if (i < 0) |
| 63 | i = -(i); |
| 64 | result = _PyLong_New(i); |
| 65 | if (result != NULL) { |
Tim Peters | 5329cdb | 2002-03-02 04:18:04 +0000 | [diff] [blame] | 66 | result->ob_size = src->ob_size; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 67 | while (--i >= 0) |
| 68 | result->ob_digit[i] = src->ob_digit[i]; |
| 69 | } |
| 70 | return (PyObject *)result; |
| 71 | } |
| 72 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 73 | /* Create a new long int object from a C long int */ |
| 74 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 76 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 77 | { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 78 | PyLongObject *v; |
| 79 | unsigned long t; /* unsigned so >> doesn't propagate sign bit */ |
| 80 | int ndigits = 0; |
| 81 | int negative = 0; |
| 82 | |
| 83 | if (ival < 0) { |
| 84 | ival = -ival; |
| 85 | negative = 1; |
| 86 | } |
| 87 | |
| 88 | /* Count the number of Python digits. |
| 89 | We used to pick 5 ("big enough for anything"), but that's a |
| 90 | waste of time and space given that 5*15 = 75 bits are rarely |
| 91 | needed. */ |
| 92 | t = (unsigned long)ival; |
| 93 | while (t) { |
| 94 | ++ndigits; |
| 95 | t >>= SHIFT; |
| 96 | } |
| 97 | v = _PyLong_New(ndigits); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 98 | if (v != NULL) { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 99 | digit *p = v->ob_digit; |
| 100 | v->ob_size = negative ? -ndigits : ndigits; |
| 101 | t = (unsigned long)ival; |
| 102 | while (t) { |
| 103 | *p++ = (digit)(t & MASK); |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 104 | t >>= SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 105 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 106 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 110 | /* Create a new long int object from a C unsigned long int */ |
| 111 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 113 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 114 | { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 115 | PyLongObject *v; |
| 116 | unsigned long t; |
| 117 | int ndigits = 0; |
| 118 | |
| 119 | /* Count the number of Python digits. */ |
| 120 | t = (unsigned long)ival; |
| 121 | while (t) { |
| 122 | ++ndigits; |
| 123 | t >>= SHIFT; |
| 124 | } |
| 125 | v = _PyLong_New(ndigits); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 126 | if (v != NULL) { |
Tim Peters | ce9de2f | 2001-06-14 04:56:19 +0000 | [diff] [blame] | 127 | digit *p = v->ob_digit; |
| 128 | v->ob_size = ndigits; |
| 129 | while (ival) { |
| 130 | *p++ = (digit)(ival & MASK); |
| 131 | ival >>= SHIFT; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 132 | } |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 133 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 137 | /* Create a new long int object from a C double */ |
| 138 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 139 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 141 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | PyLongObject *v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 143 | double frac; |
| 144 | int i, ndig, expo, neg; |
| 145 | neg = 0; |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 146 | if (Py_IS_INFINITY(dval)) { |
Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 147 | PyErr_SetString(PyExc_OverflowError, |
| 148 | "cannot convert float infinity to long"); |
| 149 | return NULL; |
| 150 | } |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 151 | if (dval < 0.0) { |
| 152 | neg = 1; |
| 153 | dval = -dval; |
| 154 | } |
| 155 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 156 | if (expo <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | return PyLong_FromLong(0L); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 158 | ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 159 | v = _PyLong_New(ndig); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 160 | if (v == NULL) |
| 161 | return NULL; |
| 162 | frac = ldexp(frac, (expo-1) % SHIFT + 1); |
| 163 | for (i = ndig; --i >= 0; ) { |
| 164 | long bits = (long)frac; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 165 | v->ob_digit[i] = (digit) bits; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 166 | frac = frac - (double)bits; |
| 167 | frac = ldexp(frac, SHIFT); |
| 168 | } |
| 169 | if (neg) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 170 | v->ob_size = -(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 174 | /* Get a C long int from a long int object. |
| 175 | Returns -1 and sets an error condition if overflow occurs. */ |
| 176 | |
| 177 | long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 178 | PyLong_AsLong(PyObject *vv) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 179 | { |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 180 | /* This version by Tim Peters */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | register PyLongObject *v; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 182 | unsigned long x, prev; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 183 | int i, sign; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 185 | if (vv == NULL || !PyLong_Check(vv)) { |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 186 | if (vv != NULL && PyInt_Check(vv)) |
| 187 | return PyInt_AsLong(vv); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 189 | return -1; |
| 190 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | v = (PyLongObject *)vv; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 192 | i = v->ob_size; |
| 193 | sign = 1; |
| 194 | x = 0; |
| 195 | if (i < 0) { |
| 196 | sign = -1; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 197 | i = -(i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 198 | } |
| 199 | while (--i >= 0) { |
| 200 | prev = x; |
| 201 | x = (x << SHIFT) + v->ob_digit[i]; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 202 | if ((x >> SHIFT) != prev) |
| 203 | goto overflow; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 204 | } |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 205 | /* Haven't lost any bits, but if the sign bit is set we're in |
| 206 | * trouble *unless* this is the min negative number. So, |
| 207 | * trouble iff sign bit set && (positive || some bit set other |
| 208 | * than the sign bit). |
| 209 | */ |
| 210 | if ((long)x < 0 && (sign > 0 || (x << 1) != 0)) |
| 211 | goto overflow; |
| 212 | return (long)x * sign; |
| 213 | |
| 214 | overflow: |
| 215 | PyErr_SetString(PyExc_OverflowError, |
Skip Montanaro | bafedec | 2001-09-13 19:05:30 +0000 | [diff] [blame] | 216 | "long int too large to convert to int"); |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 217 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 220 | /* Get a C long int from a long int object. |
| 221 | Returns -1 and sets an error condition if overflow occurs. */ |
| 222 | |
| 223 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 224 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 225 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 226 | register PyLongObject *v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 227 | unsigned long x, prev; |
| 228 | int i; |
| 229 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | if (vv == NULL || !PyLong_Check(vv)) { |
| 231 | PyErr_BadInternalCall(); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 232 | return (unsigned long) -1; |
| 233 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 234 | v = (PyLongObject *)vv; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 235 | i = v->ob_size; |
| 236 | x = 0; |
| 237 | if (i < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 239 | "can't convert negative value to unsigned long"); |
| 240 | return (unsigned long) -1; |
| 241 | } |
| 242 | while (--i >= 0) { |
| 243 | prev = x; |
| 244 | x = (x << SHIFT) + v->ob_digit[i]; |
| 245 | if ((x >> SHIFT) != prev) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 246 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 247 | "long int too large to convert"); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 248 | return (unsigned long) -1; |
| 249 | } |
| 250 | } |
| 251 | return x; |
| 252 | } |
| 253 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 254 | PyObject * |
| 255 | _PyLong_FromByteArray(const unsigned char* bytes, size_t n, |
| 256 | int little_endian, int is_signed) |
| 257 | { |
| 258 | const unsigned char* pstartbyte;/* LSB of bytes */ |
| 259 | int incr; /* direction to move pstartbyte */ |
| 260 | const unsigned char* pendbyte; /* MSB of bytes */ |
| 261 | size_t numsignificantbytes; /* number of bytes that matter */ |
| 262 | size_t ndigits; /* number of Python long digits */ |
| 263 | PyLongObject* v; /* result */ |
| 264 | int idigit = 0; /* next free index in v->ob_digit */ |
| 265 | |
| 266 | if (n == 0) |
| 267 | return PyLong_FromLong(0L); |
| 268 | |
| 269 | if (little_endian) { |
| 270 | pstartbyte = bytes; |
| 271 | pendbyte = bytes + n - 1; |
| 272 | incr = 1; |
| 273 | } |
| 274 | else { |
| 275 | pstartbyte = bytes + n - 1; |
| 276 | pendbyte = bytes; |
| 277 | incr = -1; |
| 278 | } |
| 279 | |
| 280 | if (is_signed) |
| 281 | is_signed = *pendbyte >= 0x80; |
| 282 | |
| 283 | /* Compute numsignificantbytes. This consists of finding the most |
| 284 | significant byte. Leading 0 bytes are insignficant if the number |
| 285 | is positive, and leading 0xff bytes if negative. */ |
| 286 | { |
| 287 | size_t i; |
| 288 | const unsigned char* p = pendbyte; |
| 289 | const int pincr = -incr; /* search MSB to LSB */ |
| 290 | const unsigned char insignficant = is_signed ? 0xff : 0x00; |
| 291 | |
| 292 | for (i = 0; i < n; ++i, p += pincr) { |
| 293 | if (*p != insignficant) |
| 294 | break; |
| 295 | } |
| 296 | numsignificantbytes = n - i; |
| 297 | /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so |
| 298 | actually has 2 significant bytes. OTOH, 0xff0001 == |
| 299 | -0x00ffff, so we wouldn't *need* to bump it there; but we |
| 300 | do for 0xffff = -0x0001. To be safe without bothering to |
| 301 | check every case, bump it regardless. */ |
| 302 | if (is_signed && numsignificantbytes < n) |
| 303 | ++numsignificantbytes; |
| 304 | } |
| 305 | |
| 306 | /* How many Python long digits do we need? We have |
| 307 | 8*numsignificantbytes bits, and each Python long digit has SHIFT |
| 308 | bits, so it's the ceiling of the quotient. */ |
| 309 | ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT; |
| 310 | if (ndigits > (size_t)INT_MAX) |
| 311 | return PyErr_NoMemory(); |
| 312 | v = _PyLong_New((int)ndigits); |
| 313 | if (v == NULL) |
| 314 | return NULL; |
| 315 | |
| 316 | /* Copy the bits over. The tricky parts are computing 2's-comp on |
| 317 | the fly for signed numbers, and dealing with the mismatch between |
| 318 | 8-bit bytes and (probably) 15-bit Python digits.*/ |
| 319 | { |
| 320 | size_t i; |
Tim Peters | f251d06 | 2001-06-13 21:09:15 +0000 | [diff] [blame] | 321 | twodigits carry = 1; /* for 2's-comp calculation */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 322 | twodigits accum = 0; /* sliding register */ |
| 323 | unsigned int accumbits = 0; /* number of bits in accum */ |
| 324 | const unsigned char* p = pstartbyte; |
| 325 | |
| 326 | for (i = 0; i < numsignificantbytes; ++i, p += incr) { |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 327 | twodigits thisbyte = *p; |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 328 | /* Compute correction for 2's comp, if needed. */ |
| 329 | if (is_signed) { |
| 330 | thisbyte = (0xff ^ thisbyte) + carry; |
| 331 | carry = thisbyte >> 8; |
| 332 | thisbyte &= 0xff; |
| 333 | } |
| 334 | /* Because we're going LSB to MSB, thisbyte is |
| 335 | more significant than what's already in accum, |
| 336 | so needs to be prepended to accum. */ |
| 337 | accum |= thisbyte << accumbits; |
| 338 | accumbits += 8; |
| 339 | if (accumbits >= SHIFT) { |
| 340 | /* There's enough to fill a Python digit. */ |
| 341 | assert(idigit < (int)ndigits); |
| 342 | v->ob_digit[idigit] = (digit)(accum & MASK); |
| 343 | ++idigit; |
| 344 | accum >>= SHIFT; |
| 345 | accumbits -= SHIFT; |
| 346 | assert(accumbits < SHIFT); |
| 347 | } |
| 348 | } |
| 349 | assert(accumbits < SHIFT); |
| 350 | if (accumbits) { |
| 351 | assert(idigit < (int)ndigits); |
| 352 | v->ob_digit[idigit] = (digit)accum; |
| 353 | ++idigit; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | v->ob_size = is_signed ? -idigit : idigit; |
| 358 | return (PyObject *)long_normalize(v); |
| 359 | } |
| 360 | |
| 361 | int |
| 362 | _PyLong_AsByteArray(PyLongObject* v, |
| 363 | unsigned char* bytes, size_t n, |
| 364 | int little_endian, int is_signed) |
| 365 | { |
| 366 | int i; /* index into v->ob_digit */ |
| 367 | int ndigits; /* |v->ob_size| */ |
| 368 | twodigits accum; /* sliding register */ |
| 369 | unsigned int accumbits; /* # bits in accum */ |
| 370 | int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ |
| 371 | twodigits carry; /* for computing 2's-comp */ |
| 372 | size_t j; /* # bytes filled */ |
| 373 | unsigned char* p; /* pointer to next byte in bytes */ |
| 374 | int pincr; /* direction to move p */ |
| 375 | |
| 376 | assert(v != NULL && PyLong_Check(v)); |
| 377 | |
| 378 | if (v->ob_size < 0) { |
| 379 | ndigits = -(v->ob_size); |
| 380 | if (!is_signed) { |
| 381 | PyErr_SetString(PyExc_TypeError, |
| 382 | "can't convert negative long to unsigned"); |
| 383 | return -1; |
| 384 | } |
| 385 | do_twos_comp = 1; |
| 386 | } |
| 387 | else { |
| 388 | ndigits = v->ob_size; |
| 389 | do_twos_comp = 0; |
| 390 | } |
| 391 | |
| 392 | if (little_endian) { |
| 393 | p = bytes; |
| 394 | pincr = 1; |
| 395 | } |
| 396 | else { |
| 397 | p = bytes + n - 1; |
| 398 | pincr = -1; |
| 399 | } |
| 400 | |
Tim Peters | 898cf85 | 2001-06-13 20:50:08 +0000 | [diff] [blame] | 401 | /* Copy over all the Python digits. |
| 402 | It's crucial that every Python digit except for the MSD contribute |
| 403 | exactly SHIFT bits to the total, so first assert that the long is |
| 404 | normalized. */ |
| 405 | assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 406 | j = 0; |
| 407 | accum = 0; |
| 408 | accumbits = 0; |
| 409 | carry = do_twos_comp ? 1 : 0; |
| 410 | for (i = 0; i < ndigits; ++i) { |
| 411 | twodigits thisdigit = v->ob_digit[i]; |
| 412 | if (do_twos_comp) { |
| 413 | thisdigit = (thisdigit ^ MASK) + carry; |
| 414 | carry = thisdigit >> SHIFT; |
| 415 | thisdigit &= MASK; |
| 416 | } |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 417 | /* Because we're going LSB to MSB, thisdigit is more |
| 418 | significant than what's already in accum, so needs to be |
| 419 | prepended to accum. */ |
| 420 | accum |= thisdigit << accumbits; |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 421 | accumbits += SHIFT; |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 422 | |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 423 | /* The most-significant digit may be (probably is) at least |
| 424 | partly empty. */ |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 425 | if (i == ndigits - 1) { |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 426 | /* Count # of sign bits -- they needn't be stored, |
| 427 | * although for signed conversion we need later to |
| 428 | * make sure at least one sign bit gets stored. |
| 429 | * First shift conceptual sign bit to real sign bit. |
| 430 | */ |
| 431 | stwodigits s = (stwodigits)(thisdigit << |
| 432 | (8*sizeof(stwodigits) - SHIFT)); |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 433 | unsigned int nsignbits = 0; |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 434 | while ((s < 0) == do_twos_comp && nsignbits < SHIFT) { |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 435 | ++nsignbits; |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 436 | s <<= 1; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 437 | } |
Tim Peters | ede0509 | 2001-06-14 08:53:38 +0000 | [diff] [blame] | 438 | accumbits -= nsignbits; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 439 | } |
Tim Peters | 8bc84b4 | 2001-06-12 19:17:03 +0000 | [diff] [blame] | 440 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 441 | /* Store as many bytes as possible. */ |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 442 | while (accumbits >= 8) { |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 443 | if (j >= n) |
| 444 | goto Overflow; |
| 445 | ++j; |
| 446 | *p = (unsigned char)(accum & 0xff); |
| 447 | p += pincr; |
| 448 | accumbits -= 8; |
| 449 | accum >>= 8; |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 450 | } |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Store the straggler (if any). */ |
| 454 | assert(accumbits < 8); |
| 455 | assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ |
Tim Peters | 7a3bfc3 | 2001-06-12 01:22:22 +0000 | [diff] [blame] | 456 | if (accumbits > 0) { |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 457 | if (j >= n) |
| 458 | goto Overflow; |
| 459 | ++j; |
| 460 | if (do_twos_comp) { |
| 461 | /* Fill leading bits of the byte with sign bits |
| 462 | (appropriately pretending that the long had an |
| 463 | infinite supply of sign bits). */ |
| 464 | accum |= (~(twodigits)0) << accumbits; |
| 465 | } |
| 466 | *p = (unsigned char)(accum & 0xff); |
| 467 | p += pincr; |
| 468 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 469 | else if (j == n && n > 0 && is_signed) { |
| 470 | /* The main loop filled the byte array exactly, so the code |
| 471 | just above didn't get to ensure there's a sign bit, and the |
| 472 | loop below wouldn't add one either. Make sure a sign bit |
| 473 | exists. */ |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 474 | unsigned char msb = *(p - pincr); |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 475 | int sign_bit_set = msb >= 0x80; |
| 476 | assert(accumbits == 0); |
| 477 | if (sign_bit_set == do_twos_comp) |
| 478 | return 0; |
| 479 | else |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 480 | goto Overflow; |
| 481 | } |
Tim Peters | 05607ad | 2001-06-13 21:01:27 +0000 | [diff] [blame] | 482 | |
| 483 | /* Fill remaining bytes with copies of the sign bit. */ |
| 484 | { |
| 485 | unsigned char signbyte = do_twos_comp ? 0xffU : 0U; |
| 486 | for ( ; j < n; ++j, p += pincr) |
| 487 | *p = signbyte; |
| 488 | } |
| 489 | |
Tim Peters | 2a9b367 | 2001-06-11 21:23:58 +0000 | [diff] [blame] | 490 | return 0; |
| 491 | |
| 492 | Overflow: |
| 493 | PyErr_SetString(PyExc_OverflowError, "long too big to convert"); |
| 494 | return -1; |
| 495 | |
| 496 | } |
| 497 | |
Tim Peters | a1c1b0f | 2001-09-04 02:50:49 +0000 | [diff] [blame] | 498 | double |
| 499 | _PyLong_AsScaledDouble(PyObject *vv, int *exponent) |
| 500 | { |
| 501 | /* NBITS_WANTED should be > the number of bits in a double's precision, |
| 502 | but small enough so that 2**NBITS_WANTED is within the normal double |
| 503 | range. nbitsneeded is set to 1 less than that because the most-significant |
| 504 | Python digit contains at least 1 significant bit, but we don't want to |
| 505 | bother counting them (catering to the worst case cheaply). |
| 506 | |
| 507 | 57 is one more than VAX-D double precision; I (Tim) don't know of a double |
| 508 | format with more precision than that; it's 1 larger so that we add in at |
| 509 | least one round bit to stand in for the ignored least-significant bits. |
| 510 | */ |
| 511 | #define NBITS_WANTED 57 |
| 512 | PyLongObject *v; |
| 513 | double x; |
| 514 | const double multiplier = (double)(1L << SHIFT); |
| 515 | int i, sign; |
| 516 | int nbitsneeded; |
| 517 | |
| 518 | if (vv == NULL || !PyLong_Check(vv)) { |
| 519 | PyErr_BadInternalCall(); |
| 520 | return -1; |
| 521 | } |
| 522 | v = (PyLongObject *)vv; |
| 523 | i = v->ob_size; |
| 524 | sign = 1; |
| 525 | if (i < 0) { |
| 526 | sign = -1; |
| 527 | i = -(i); |
| 528 | } |
| 529 | else if (i == 0) { |
| 530 | *exponent = 0; |
| 531 | return 0.0; |
| 532 | } |
| 533 | --i; |
| 534 | x = (double)v->ob_digit[i]; |
| 535 | nbitsneeded = NBITS_WANTED - 1; |
| 536 | /* Invariant: i Python digits remain unaccounted for. */ |
| 537 | while (i > 0 && nbitsneeded > 0) { |
| 538 | --i; |
| 539 | x = x * multiplier + (double)v->ob_digit[i]; |
| 540 | nbitsneeded -= SHIFT; |
| 541 | } |
| 542 | /* There are i digits we didn't shift in. Pretending they're all |
| 543 | zeroes, the true value is x * 2**(i*SHIFT). */ |
| 544 | *exponent = i; |
| 545 | assert(x > 0.0); |
| 546 | return x * sign; |
| 547 | #undef NBITS_WANTED |
| 548 | } |
| 549 | |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 550 | /* Get a C double from a long int object. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 551 | |
| 552 | double |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 553 | PyLong_AsDouble(PyObject *vv) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 554 | { |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 555 | int e; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 556 | double x; |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | if (vv == NULL || !PyLong_Check(vv)) { |
| 559 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 560 | return -1; |
| 561 | } |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 562 | x = _PyLong_AsScaledDouble(vv, &e); |
| 563 | if (x == -1.0 && PyErr_Occurred()) |
| 564 | return -1.0; |
| 565 | if (e > INT_MAX / SHIFT) |
| 566 | goto overflow; |
| 567 | errno = 0; |
| 568 | x = ldexp(x, e * SHIFT); |
Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 569 | if (Py_OVERFLOWED(x)) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 570 | goto overflow; |
| 571 | return x; |
| 572 | |
| 573 | overflow: |
| 574 | PyErr_SetString(PyExc_OverflowError, |
| 575 | "long int too large to convert to float"); |
| 576 | return -1.0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 579 | /* Create a new long (or int) object from a C pointer */ |
| 580 | |
| 581 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 582 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 583 | { |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 584 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 585 | return PyInt_FromLong((long)p); |
| 586 | #else |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 587 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 588 | #ifndef HAVE_LONG_LONG |
| 589 | # error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 590 | #endif |
| 591 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
| 592 | # error "PyLong_FromVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" |
| 593 | #endif |
| 594 | /* optimize null pointers */ |
| 595 | if (p == NULL) |
| 596 | return PyInt_FromLong(0); |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 597 | return PyLong_FromLongLong((LONG_LONG)p); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 598 | |
| 599 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* Get a C pointer from a long object (or an int object in some cases) */ |
| 603 | |
| 604 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 605 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 606 | { |
| 607 | /* This function will allow int or long objects. If vv is neither, |
| 608 | then the PyLong_AsLong*() functions will raise the exception: |
| 609 | PyExc_SystemError, "bad argument to internal function" |
| 610 | */ |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 611 | #if SIZEOF_VOID_P <= SIZEOF_LONG |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 612 | long x; |
| 613 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 614 | if (PyInt_Check(vv)) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 615 | x = PyInt_AS_LONG(vv); |
| 616 | else |
| 617 | x = PyLong_AsLong(vv); |
| 618 | #else |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 619 | |
| 620 | #ifndef HAVE_LONG_LONG |
| 621 | # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" |
| 622 | #endif |
| 623 | #if SIZEOF_LONG_LONG < SIZEOF_VOID_P |
| 624 | # error "PyLong_AsVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" |
| 625 | #endif |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 626 | LONG_LONG x; |
| 627 | |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 628 | if (PyInt_Check(vv)) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 629 | x = PyInt_AS_LONG(vv); |
| 630 | else |
| 631 | x = PyLong_AsLongLong(vv); |
Tim Peters | 70128a1 | 2001-06-16 08:48:40 +0000 | [diff] [blame] | 632 | |
| 633 | #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 634 | |
| 635 | if (x == -1 && PyErr_Occurred()) |
| 636 | return NULL; |
| 637 | return (void *)x; |
| 638 | } |
| 639 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 640 | #ifdef HAVE_LONG_LONG |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 641 | |
| 642 | /* Initial LONG_LONG support by Chris Herborth (chrish@qnx.com), later |
| 643 | * rewritten to use the newer PyLong_{As,From}ByteArray API. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 644 | */ |
| 645 | |
Tim Peters | cf37dfc | 2001-06-14 18:42:50 +0000 | [diff] [blame] | 646 | #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 647 | |
| 648 | /* Create a new long int object from a C LONG_LONG int. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 649 | |
| 650 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 651 | PyLong_FromLongLong(LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 652 | { |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 653 | LONG_LONG bytes = ival; |
| 654 | int one = 1; |
| 655 | return _PyLong_FromByteArray( |
| 656 | (unsigned char *)&bytes, |
| 657 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 660 | /* Create a new long int object from a C unsigned LONG_LONG int. */ |
| 661 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 662 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 663 | PyLong_FromUnsignedLongLong(unsigned LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 664 | { |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 665 | unsigned LONG_LONG bytes = ival; |
| 666 | int one = 1; |
| 667 | return _PyLong_FromByteArray( |
| 668 | (unsigned char *)&bytes, |
| 669 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 672 | /* Get a C LONG_LONG int from a long int object. |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 673 | Return -1 and set an error if overflow occurs. */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 674 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 675 | LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 676 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 677 | { |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 678 | LONG_LONG bytes; |
| 679 | int one = 1; |
| 680 | int res; |
| 681 | |
Tim Peters | d38b1c7 | 2001-09-30 05:09:37 +0000 | [diff] [blame] | 682 | if (vv == NULL) { |
| 683 | PyErr_BadInternalCall(); |
| 684 | return -1; |
| 685 | } |
| 686 | if (!PyLong_Check(vv)) { |
| 687 | if (PyInt_Check(vv)) |
| 688 | return (LONG_LONG)PyInt_AsLong(vv); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 689 | PyErr_BadInternalCall(); |
| 690 | return -1; |
| 691 | } |
| 692 | |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 693 | res = _PyLong_AsByteArray( |
| 694 | (PyLongObject *)vv, (unsigned char *)&bytes, |
| 695 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 696 | |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 697 | /* Plan 9 can't handle LONG_LONG in ? : expressions */ |
| 698 | if (res < 0) |
| 699 | return (LONG_LONG)res; |
| 700 | else |
| 701 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 704 | /* Get a C unsigned LONG_LONG int from a long int object. |
| 705 | Return -1 and set an error if overflow occurs. */ |
| 706 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 707 | unsigned LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 708 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 709 | { |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 710 | unsigned LONG_LONG bytes; |
| 711 | int one = 1; |
| 712 | int res; |
| 713 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 714 | if (vv == NULL || !PyLong_Check(vv)) { |
| 715 | PyErr_BadInternalCall(); |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 716 | return -1; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 719 | res = _PyLong_AsByteArray( |
| 720 | (PyLongObject *)vv, (unsigned char *)&bytes, |
| 721 | SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 722 | |
Martin v. Löwis | c8bb9eb | 2002-03-09 12:02:59 +0000 | [diff] [blame] | 723 | /* Plan 9 can't handle LONG_LONG in ? : expressions */ |
| 724 | if (res < 0) |
| 725 | return (unsigned LONG_LONG)res; |
| 726 | else |
| 727 | return bytes; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 728 | } |
Tim Peters | d1a7da6 | 2001-06-13 00:35:57 +0000 | [diff] [blame] | 729 | |
| 730 | #undef IS_LITTLE_ENDIAN |
| 731 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 732 | #endif /* HAVE_LONG_LONG */ |
| 733 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 734 | |
| 735 | static int |
| 736 | convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { |
| 737 | if (PyLong_Check(v)) { |
| 738 | *a = (PyLongObject *) v; |
| 739 | Py_INCREF(v); |
| 740 | } |
| 741 | else if (PyInt_Check(v)) { |
| 742 | *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v)); |
| 743 | } |
| 744 | else { |
| 745 | return 0; |
| 746 | } |
| 747 | if (PyLong_Check(w)) { |
| 748 | *b = (PyLongObject *) w; |
| 749 | Py_INCREF(w); |
| 750 | } |
| 751 | else if (PyInt_Check(w)) { |
| 752 | *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w)); |
| 753 | } |
| 754 | else { |
| 755 | Py_DECREF(*a); |
| 756 | return 0; |
| 757 | } |
| 758 | return 1; |
| 759 | } |
| 760 | |
| 761 | #define CONVERT_BINOP(v, w, a, b) \ |
| 762 | if (!convert_binop(v, w, a, b)) { \ |
| 763 | Py_INCREF(Py_NotImplemented); \ |
| 764 | return Py_NotImplemented; \ |
| 765 | } |
| 766 | |
| 767 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 768 | /* Multiply by a single digit, ignoring the sign. */ |
| 769 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 770 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 771 | mul1(PyLongObject *a, wdigit n) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 772 | { |
| 773 | return muladd1(a, n, (digit)0); |
| 774 | } |
| 775 | |
| 776 | /* Multiply by a single digit and add a single digit, ignoring the sign. */ |
| 777 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 778 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 779 | muladd1(PyLongObject *a, wdigit n, wdigit extra) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 780 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 781 | int size_a = ABS(a->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 782 | PyLongObject *z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 783 | twodigits carry = extra; |
| 784 | int i; |
| 785 | |
| 786 | if (z == NULL) |
| 787 | return NULL; |
| 788 | for (i = 0; i < size_a; ++i) { |
| 789 | carry += (twodigits)a->ob_digit[i] * n; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 790 | z->ob_digit[i] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 791 | carry >>= SHIFT; |
| 792 | } |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 793 | z->ob_digit[i] = (digit) carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 794 | return long_normalize(z); |
| 795 | } |
| 796 | |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 797 | /* Divide long pin, w/ size digits, by non-zero digit n, storing quotient |
| 798 | in pout, and returning the remainder. pin and pout point at the LSD. |
| 799 | It's OK for pin == pout on entry, which saves oodles of mallocs/frees in |
| 800 | long_format, but that should be done with great care since longs are |
| 801 | immutable. */ |
| 802 | |
| 803 | static digit |
| 804 | inplace_divrem1(digit *pout, digit *pin, int size, digit n) |
| 805 | { |
| 806 | twodigits rem = 0; |
| 807 | |
| 808 | assert(n > 0 && n <= MASK); |
| 809 | pin += size; |
| 810 | pout += size; |
| 811 | while (--size >= 0) { |
| 812 | digit hi; |
| 813 | rem = (rem << SHIFT) + *--pin; |
| 814 | *--pout = hi = (digit)(rem / n); |
| 815 | rem -= hi * n; |
| 816 | } |
| 817 | return (digit)rem; |
| 818 | } |
| 819 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 820 | /* Divide a long integer by a digit, returning both the quotient |
| 821 | (as function result) and the remainder (through *prem). |
| 822 | The sign of a is ignored; n should not be zero. */ |
| 823 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 824 | static PyLongObject * |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 825 | divrem1(PyLongObject *a, digit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 826 | { |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 827 | const int size = ABS(a->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 828 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 829 | |
| 830 | assert(n > 0 && n <= MASK); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 831 | z = _PyLong_New(size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 832 | if (z == NULL) |
| 833 | return NULL; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 834 | *prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 835 | return long_normalize(z); |
| 836 | } |
| 837 | |
| 838 | /* 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] | 839 | Return a string object. |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 840 | If base is 8 or 16, add the proper prefix '0' or '0x'. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 842 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 843 | long_format(PyObject *aa, int base, int addL) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 844 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 845 | register PyLongObject *a = (PyLongObject *)aa; |
| 846 | PyStringObject *str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 847 | int i; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 848 | const int size_a = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 849 | char *p; |
| 850 | int bits; |
| 851 | char sign = '\0'; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 853 | if (a == NULL || !PyLong_Check(a)) { |
| 854 | PyErr_BadInternalCall(); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 855 | return NULL; |
| 856 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 857 | assert(base >= 2 && base <= 36); |
| 858 | |
| 859 | /* Compute a rough upper bound for the length of the string */ |
| 860 | i = base; |
| 861 | bits = 0; |
| 862 | while (i > 1) { |
| 863 | ++bits; |
| 864 | i >>= 1; |
| 865 | } |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 866 | i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 867 | str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 868 | if (str == NULL) |
| 869 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 870 | p = PyString_AS_STRING(str) + i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 871 | *p = '\0'; |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 872 | if (addL) |
| 873 | *--p = 'L'; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 874 | if (a->ob_size < 0) |
| 875 | sign = '-'; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 877 | if (a->ob_size == 0) { |
| 878 | *--p = '0'; |
| 879 | } |
| 880 | else if ((base & (base - 1)) == 0) { |
| 881 | /* JRH: special case for power-of-2 bases */ |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 882 | twodigits accum = 0; |
| 883 | int accumbits = 0; /* # of bits in accum */ |
| 884 | int basebits = 1; /* # of bits in base-1 */ |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 885 | i = base; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 886 | while ((i >>= 1) > 1) |
| 887 | ++basebits; |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 888 | |
| 889 | for (i = 0; i < size_a; ++i) { |
| 890 | accum |= a->ob_digit[i] << accumbits; |
| 891 | accumbits += SHIFT; |
| 892 | assert(accumbits >= basebits); |
| 893 | do { |
Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 894 | char cdigit = (char)(accum & (base - 1)); |
| 895 | cdigit += (cdigit < 10) ? '0' : 'A'-10; |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 896 | assert(p > PyString_AS_STRING(str)); |
Martin v. Löwis | a5854c2 | 2002-02-16 23:39:10 +0000 | [diff] [blame] | 897 | *--p = cdigit; |
Tim Peters | 586b2e3 | 2001-07-15 09:11:14 +0000 | [diff] [blame] | 898 | accumbits -= basebits; |
| 899 | accum >>= basebits; |
| 900 | } while (i < size_a-1 ? accumbits >= basebits : |
| 901 | accum > 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 902 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 903 | } |
| 904 | else { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 905 | /* Not 0, and base not a power of 2. Divide repeatedly by |
| 906 | base, but for speed use the highest power of base that |
| 907 | fits in a digit. */ |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 908 | int size = size_a; |
| 909 | digit *pin = a->ob_digit; |
| 910 | PyLongObject *scratch; |
| 911 | /* powbasw <- largest power of base that fits in a digit. */ |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 912 | digit powbase = base; /* powbase == base ** power */ |
| 913 | int power = 1; |
| 914 | for (;;) { |
| 915 | unsigned long newpow = powbase * (unsigned long)base; |
| 916 | if (newpow >> SHIFT) /* doesn't fit in a digit */ |
| 917 | break; |
| 918 | powbase = (digit)newpow; |
| 919 | ++power; |
| 920 | } |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 921 | |
| 922 | /* Get a scratch area for repeated division. */ |
| 923 | scratch = _PyLong_New(size); |
| 924 | if (scratch == NULL) { |
| 925 | Py_DECREF(str); |
| 926 | return NULL; |
| 927 | } |
| 928 | |
| 929 | /* Repeatedly divide by powbase. */ |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 930 | do { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 931 | int ntostore = power; |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 932 | digit rem = inplace_divrem1(scratch->ob_digit, |
| 933 | pin, size, powbase); |
| 934 | pin = scratch->ob_digit; /* no need to use a again */ |
| 935 | if (pin[size - 1] == 0) |
| 936 | --size; |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 937 | SIGCHECK({ |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 938 | Py_DECREF(scratch); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 939 | Py_DECREF(str); |
| 940 | return NULL; |
| 941 | }) |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 942 | |
| 943 | /* Break rem into digits. */ |
Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 944 | assert(ntostore > 0); |
| 945 | do { |
Tim Peters | fad225f | 2001-07-13 02:59:26 +0000 | [diff] [blame] | 946 | digit nextrem = (digit)(rem / base); |
| 947 | char c = (char)(rem - nextrem * base); |
| 948 | assert(p > PyString_AS_STRING(str)); |
| 949 | c += (c < 10) ? '0' : 'A'-10; |
| 950 | *--p = c; |
| 951 | rem = nextrem; |
Tim Peters | c8a6b9b | 2001-07-14 11:01:28 +0000 | [diff] [blame] | 952 | --ntostore; |
| 953 | /* Termination is a bit delicate: must not |
| 954 | store leading zeroes, so must get out if |
Tim Peters | 212e614 | 2001-07-14 12:23:19 +0000 | [diff] [blame] | 955 | remaining quotient and rem are both 0. */ |
| 956 | } while (ntostore && (size || rem)); |
| 957 | } while (size != 0); |
| 958 | Py_DECREF(scratch); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Guido van Rossum | 2c47542 | 1992-08-14 15:13:07 +0000 | [diff] [blame] | 961 | if (base == 8) { |
| 962 | if (size_a != 0) |
| 963 | *--p = '0'; |
| 964 | } |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 965 | else if (base == 16) { |
| 966 | *--p = 'x'; |
| 967 | *--p = '0'; |
| 968 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 969 | else if (base != 10) { |
| 970 | *--p = '#'; |
| 971 | *--p = '0' + base%10; |
| 972 | if (base > 10) |
| 973 | *--p = '0' + base/10; |
| 974 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 975 | if (sign) |
| 976 | *--p = sign; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | if (p != PyString_AS_STRING(str)) { |
| 978 | char *q = PyString_AS_STRING(str); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 979 | assert(p > q); |
| 980 | do { |
| 981 | } while ((*q++ = *p++) != '\0'); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 982 | q--; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | _PyString_Resize((PyObject **)&str, |
| 984 | (int) (q - PyString_AS_STRING(str))); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 985 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 986 | return (PyObject *)str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 989 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 990 | PyLong_FromString(char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 991 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 992 | int sign = 1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 993 | char *start, *orig_str = str; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 995 | |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 996 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 997 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 998 | "long() arg 2 must be >= 2 and <= 36"); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1001 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1002 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1003 | if (*str == '+') |
| 1004 | ++str; |
| 1005 | else if (*str == '-') { |
| 1006 | ++str; |
| 1007 | sign = -1; |
| 1008 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 1009 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1010 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1011 | if (base == 0) { |
| 1012 | if (str[0] != '0') |
| 1013 | base = 10; |
| 1014 | else if (str[1] == 'x' || str[1] == 'X') |
| 1015 | base = 16; |
| 1016 | else |
| 1017 | base = 8; |
| 1018 | } |
| 1019 | if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) |
| 1020 | str += 2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1021 | z = _PyLong_New(0); |
Guido van Rossum | e676297 | 1998-06-22 03:54:46 +0000 | [diff] [blame] | 1022 | start = str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1023 | for ( ; z != NULL; ++str) { |
| 1024 | int k = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1025 | PyLongObject *temp; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (*str <= '9') |
| 1028 | k = *str - '0'; |
| 1029 | else if (*str >= 'a') |
| 1030 | k = *str - 'a' + 10; |
| 1031 | else if (*str >= 'A') |
| 1032 | k = *str - 'A' + 10; |
| 1033 | if (k < 0 || k >= base) |
| 1034 | break; |
| 1035 | temp = muladd1(z, (digit)base, (digit)k); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1036 | Py_DECREF(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1037 | z = temp; |
| 1038 | } |
Guido van Rossum | ac6a37a | 1998-08-04 15:04:06 +0000 | [diff] [blame] | 1039 | if (z == NULL) |
| 1040 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1041 | if (str == start) |
| 1042 | goto onError; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1043 | if (sign < 0 && z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1044 | z->ob_size = -(z->ob_size); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1045 | if (*str == 'L' || *str == 'l') |
| 1046 | str++; |
| 1047 | while (*str && isspace(Py_CHARMASK(*str))) |
| 1048 | str++; |
| 1049 | if (*str != '\0') |
| 1050 | goto onError; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1051 | if (pend) |
| 1052 | *pend = str; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1053 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1054 | |
| 1055 | onError: |
| 1056 | PyErr_Format(PyExc_ValueError, |
| 1057 | "invalid literal for long(): %.200s", orig_str); |
| 1058 | Py_XDECREF(z); |
| 1059 | return NULL; |
| 1060 | } |
| 1061 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1062 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1063 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1064 | PyLong_FromUnicode(Py_UNICODE *u, int length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 1065 | { |
| 1066 | char buffer[256]; |
| 1067 | |
| 1068 | if (length >= sizeof(buffer)) { |
| 1069 | PyErr_SetString(PyExc_ValueError, |
| 1070 | "long() literal too large to convert"); |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) |
| 1074 | return NULL; |
| 1075 | |
| 1076 | return PyLong_FromString(buffer, NULL, base); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1077 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1078 | #endif |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1079 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1080 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1081 | static PyLongObject *x_divrem |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1082 | (PyLongObject *, PyLongObject *, PyLongObject **); |
| 1083 | static PyObject *long_pos(PyLongObject *); |
| 1084 | static int long_divrem(PyLongObject *, PyLongObject *, |
| 1085 | PyLongObject **, PyLongObject **); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1086 | |
| 1087 | /* Long division with remainder, top-level routine */ |
| 1088 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1089 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1090 | long_divrem(PyLongObject *a, PyLongObject *b, |
| 1091 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1092 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1093 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1094 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (size_b == 0) { |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1097 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1098 | "long division or modulo by zero"); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1099 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1100 | } |
| 1101 | if (size_a < size_b || |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 1102 | (size_a == size_b && |
| 1103 | 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] | 1104 | /* |a| < |b|. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1105 | *pdiv = _PyLong_New(0); |
| 1106 | Py_INCREF(a); |
| 1107 | *prem = (PyLongObject *) a; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1108 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1109 | } |
| 1110 | if (size_b == 1) { |
| 1111 | digit rem = 0; |
| 1112 | z = divrem1(a, b->ob_digit[0], &rem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1113 | if (z == NULL) |
| 1114 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1115 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1116 | } |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1117 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1118 | z = x_divrem(a, b, prem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1119 | if (z == NULL) |
| 1120 | return -1; |
| 1121 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1122 | /* Set the signs. |
| 1123 | The quotient z has the sign of a*b; |
| 1124 | the remainder r has the sign of a, |
| 1125 | so a = b*z + r. */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1126 | if ((a->ob_size < 0) != (b->ob_size < 0)) |
| 1127 | z->ob_size = -(z->ob_size); |
| 1128 | if (a->ob_size < 0 && (*prem)->ob_size != 0) |
| 1129 | (*prem)->ob_size = -((*prem)->ob_size); |
| 1130 | *pdiv = z; |
| 1131 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1134 | /* Unsigned long division with remainder -- the algorithm */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1135 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1136 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1137 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1138 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1139 | 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] | 1140 | digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1141 | PyLongObject *v = mul1(v1, d); |
| 1142 | PyLongObject *w = mul1(w1, d); |
| 1143 | PyLongObject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1144 | int j, k; |
| 1145 | |
| 1146 | if (v == NULL || w == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1147 | Py_XDECREF(v); |
| 1148 | Py_XDECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1149 | return NULL; |
| 1150 | } |
| 1151 | |
| 1152 | 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] | 1153 | 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] | 1154 | 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] | 1155 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1156 | size_v = ABS(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1157 | a = _PyLong_New(size_v - size_w + 1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1158 | |
| 1159 | for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { |
| 1160 | digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; |
| 1161 | twodigits q; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1162 | stwodigits carry = 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1163 | int i; |
| 1164 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1165 | SIGCHECK({ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1166 | Py_DECREF(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1167 | a = NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1168 | break; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1169 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1170 | if (vj == w->ob_digit[size_w-1]) |
| 1171 | q = MASK; |
| 1172 | else |
| 1173 | q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) / |
| 1174 | w->ob_digit[size_w-1]; |
| 1175 | |
| 1176 | while (w->ob_digit[size_w-2]*q > |
| 1177 | (( |
| 1178 | ((twodigits)vj << SHIFT) |
| 1179 | + v->ob_digit[j-1] |
| 1180 | - q*w->ob_digit[size_w-1] |
| 1181 | ) << SHIFT) |
| 1182 | + v->ob_digit[j-2]) |
| 1183 | --q; |
| 1184 | |
| 1185 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 1186 | twodigits z = w->ob_digit[i] * q; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1187 | digit zz = (digit) (z >> SHIFT); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1188 | carry += v->ob_digit[i+k] - z |
| 1189 | + ((twodigits)zz << SHIFT); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1190 | v->ob_digit[i+k] = carry & MASK; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1191 | carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, |
| 1192 | carry, SHIFT); |
| 1193 | carry -= zz; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | if (i+k < size_v) { |
| 1197 | carry += v->ob_digit[i+k]; |
| 1198 | v->ob_digit[i+k] = 0; |
| 1199 | } |
| 1200 | |
| 1201 | if (carry == 0) |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1202 | a->ob_digit[k] = (digit) q; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1203 | else { |
| 1204 | assert(carry == -1); |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1205 | a->ob_digit[k] = (digit) q-1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1206 | carry = 0; |
| 1207 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 1208 | carry += v->ob_digit[i+k] + w->ob_digit[i]; |
| 1209 | v->ob_digit[i+k] = carry & MASK; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1210 | carry = Py_ARITHMETIC_RIGHT_SHIFT( |
| 1211 | BASE_TWODIGITS_TYPE, |
| 1212 | carry, SHIFT); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | } /* for j, k */ |
| 1216 | |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 1217 | if (a == NULL) |
| 1218 | *prem = NULL; |
| 1219 | else { |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1220 | a = long_normalize(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1221 | *prem = divrem1(v, d, &d); |
| 1222 | /* d receives the (unused) remainder */ |
| 1223 | if (*prem == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1224 | Py_DECREF(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1225 | a = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1226 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1227 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1228 | Py_DECREF(v); |
| 1229 | Py_DECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1230 | return a; |
| 1231 | } |
| 1232 | |
| 1233 | /* Methods */ |
| 1234 | |
| 1235 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1236 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1237 | { |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1238 | v->ob_type->tp_free(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1241 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1242 | long_repr(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1243 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 1244 | return long_format(v, 10, 1); |
| 1245 | } |
| 1246 | |
| 1247 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1248 | long_str(PyObject *v) |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 1249 | { |
| 1250 | return long_format(v, 10, 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1254 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1255 | { |
| 1256 | int sign; |
| 1257 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1258 | if (a->ob_size != b->ob_size) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1259 | if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1260 | sign = 0; |
| 1261 | else |
| 1262 | sign = a->ob_size - b->ob_size; |
| 1263 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1264 | else { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1265 | int i = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1266 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 1267 | ; |
| 1268 | if (i < 0) |
| 1269 | sign = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1270 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1271 | sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 1272 | if (a->ob_size < 0) |
| 1273 | sign = -sign; |
| 1274 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1275 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1276 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1279 | static long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1280 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1281 | { |
| 1282 | long x; |
| 1283 | int i, sign; |
| 1284 | |
| 1285 | /* This is designed so that Python ints and longs with the |
| 1286 | same value hash to the same value, otherwise comparisons |
| 1287 | of mapping keys will turn out weird */ |
| 1288 | i = v->ob_size; |
| 1289 | sign = 1; |
| 1290 | x = 0; |
| 1291 | if (i < 0) { |
| 1292 | sign = -1; |
| 1293 | i = -(i); |
| 1294 | } |
| 1295 | while (--i >= 0) { |
| 1296 | /* Force a 32-bit circular shift */ |
| 1297 | x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); |
| 1298 | x += v->ob_digit[i]; |
| 1299 | } |
| 1300 | x = x * sign; |
| 1301 | if (x == -1) |
| 1302 | x = -2; |
| 1303 | return x; |
| 1304 | } |
| 1305 | |
| 1306 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1307 | /* Add the absolute values of two long integers. */ |
| 1308 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1309 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1310 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1311 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1312 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1313 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1314 | int i; |
| 1315 | digit carry = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1316 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1317 | /* Ensure a is the larger of the two: */ |
| 1318 | if (size_a < size_b) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1319 | { PyLongObject *temp = a; a = b; b = temp; } |
| 1320 | { int size_temp = size_a; |
| 1321 | size_a = size_b; |
| 1322 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1323 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1324 | z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1325 | if (z == NULL) |
| 1326 | return NULL; |
| 1327 | for (i = 0; i < size_b; ++i) { |
| 1328 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 1329 | z->ob_digit[i] = carry & MASK; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1330 | carry >>= SHIFT; |
| 1331 | } |
| 1332 | for (; i < size_a; ++i) { |
| 1333 | carry += a->ob_digit[i]; |
| 1334 | z->ob_digit[i] = carry & MASK; |
| 1335 | carry >>= SHIFT; |
| 1336 | } |
| 1337 | z->ob_digit[i] = carry; |
| 1338 | return long_normalize(z); |
| 1339 | } |
| 1340 | |
| 1341 | /* Subtract the absolute values of two integers. */ |
| 1342 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1343 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1344 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1345 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1346 | int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1347 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1348 | int i; |
| 1349 | int sign = 1; |
| 1350 | digit borrow = 0; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1351 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1352 | /* Ensure a is the larger of the two: */ |
| 1353 | if (size_a < size_b) { |
| 1354 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1355 | { PyLongObject *temp = a; a = b; b = temp; } |
| 1356 | { int size_temp = size_a; |
| 1357 | size_a = size_b; |
| 1358 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1359 | } |
| 1360 | else if (size_a == size_b) { |
| 1361 | /* Find highest digit where a and b differ: */ |
| 1362 | i = size_a; |
| 1363 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 1364 | ; |
| 1365 | if (i < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1366 | return _PyLong_New(0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1367 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 1368 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1369 | { PyLongObject *temp = a; a = b; b = temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1370 | } |
| 1371 | size_a = size_b = i+1; |
| 1372 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1373 | z = _PyLong_New(size_a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1374 | if (z == NULL) |
| 1375 | return NULL; |
| 1376 | for (i = 0; i < size_b; ++i) { |
| 1377 | /* The following assumes unsigned arithmetic |
| 1378 | works module 2**N for some N>SHIFT. */ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1379 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1380 | z->ob_digit[i] = borrow & MASK; |
| 1381 | borrow >>= SHIFT; |
| 1382 | borrow &= 1; /* Keep only one sign bit */ |
| 1383 | } |
| 1384 | for (; i < size_a; ++i) { |
| 1385 | borrow = a->ob_digit[i] - borrow; |
| 1386 | z->ob_digit[i] = borrow & MASK; |
| 1387 | borrow >>= SHIFT; |
Tim Peters | 43f04a3 | 2000-07-08 02:26:47 +0000 | [diff] [blame] | 1388 | borrow &= 1; /* Keep only one sign bit */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1389 | } |
| 1390 | assert(borrow == 0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1391 | if (sign < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1392 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1393 | return long_normalize(z); |
| 1394 | } |
| 1395 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1396 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1397 | long_add(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1398 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1399 | PyLongObject *a, *b, *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1400 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1401 | CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); |
| 1402 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1403 | if (a->ob_size < 0) { |
| 1404 | if (b->ob_size < 0) { |
| 1405 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1406 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1407 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1408 | } |
| 1409 | else |
| 1410 | z = x_sub(b, a); |
| 1411 | } |
| 1412 | else { |
| 1413 | if (b->ob_size < 0) |
| 1414 | z = x_sub(a, b); |
| 1415 | else |
| 1416 | z = x_add(a, b); |
| 1417 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1418 | Py_DECREF(a); |
| 1419 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1420 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1423 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1424 | long_sub(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1425 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1426 | PyLongObject *a, *b, *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1427 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1428 | CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); |
| 1429 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1430 | if (a->ob_size < 0) { |
| 1431 | if (b->ob_size < 0) |
| 1432 | z = x_sub(a, b); |
| 1433 | else |
| 1434 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1435 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1436 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1437 | } |
| 1438 | else { |
| 1439 | if (b->ob_size < 0) |
| 1440 | z = x_add(a, b); |
| 1441 | else |
| 1442 | z = x_sub(a, b); |
| 1443 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1444 | Py_DECREF(a); |
| 1445 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1446 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1449 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1450 | long_repeat(PyObject *v, PyLongObject *w) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1451 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1452 | /* sequence * long */ |
| 1453 | long n = PyLong_AsLong((PyObject *) w); |
| 1454 | if (n == -1 && PyErr_Occurred()) |
| 1455 | return NULL; |
| 1456 | else |
| 1457 | return (*v->ob_type->tp_as_sequence->sq_repeat)(v, n); |
| 1458 | } |
| 1459 | |
| 1460 | static PyObject * |
| 1461 | long_mul(PyLongObject *v, PyLongObject *w) |
| 1462 | { |
| 1463 | PyLongObject *a, *b, *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1464 | int size_a; |
| 1465 | int size_b; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1466 | int i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1467 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 1468 | if (!convert_binop((PyObject *)v, (PyObject *)w, &a, &b)) { |
| 1469 | if (!PyLong_Check(v) && |
| 1470 | v->ob_type->tp_as_sequence && |
| 1471 | v->ob_type->tp_as_sequence->sq_repeat) |
| 1472 | return long_repeat((PyObject *)v, w); |
| 1473 | if (!PyLong_Check(w) && |
| 1474 | w->ob_type->tp_as_sequence && |
| 1475 | w->ob_type->tp_as_sequence->sq_repeat) |
| 1476 | return long_repeat((PyObject *)w, v); |
| 1477 | Py_INCREF(Py_NotImplemented); |
| 1478 | return Py_NotImplemented; |
| 1479 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1480 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1481 | size_a = ABS(a->ob_size); |
| 1482 | size_b = ABS(b->ob_size); |
Guido van Rossum | ba71a24 | 2000-04-10 17:31:58 +0000 | [diff] [blame] | 1483 | if (size_a > size_b) { |
| 1484 | /* we are faster with the small object on the left */ |
| 1485 | int hold_sa = size_a; |
| 1486 | PyLongObject *hold_a = a; |
| 1487 | size_a = size_b; |
| 1488 | size_b = hold_sa; |
| 1489 | a = b; |
| 1490 | b = hold_a; |
| 1491 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1492 | z = _PyLong_New(size_a + size_b); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1493 | if (z == NULL) { |
| 1494 | Py_DECREF(a); |
| 1495 | Py_DECREF(b); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1496 | return NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1497 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1498 | for (i = 0; i < z->ob_size; ++i) |
| 1499 | z->ob_digit[i] = 0; |
| 1500 | for (i = 0; i < size_a; ++i) { |
| 1501 | twodigits carry = 0; |
| 1502 | twodigits f = a->ob_digit[i]; |
| 1503 | int j; |
| 1504 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1505 | SIGCHECK({ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1506 | Py_DECREF(a); |
| 1507 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1508 | Py_DECREF(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1509 | return NULL; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1510 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1511 | for (j = 0; j < size_b; ++j) { |
| 1512 | carry += z->ob_digit[i+j] + b->ob_digit[j] * f; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1513 | z->ob_digit[i+j] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1514 | carry >>= SHIFT; |
| 1515 | } |
| 1516 | for (; carry != 0; ++j) { |
| 1517 | assert(i+j < z->ob_size); |
| 1518 | carry += z->ob_digit[i+j]; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1519 | z->ob_digit[i+j] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1520 | carry >>= SHIFT; |
| 1521 | } |
| 1522 | } |
| 1523 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1524 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1525 | if (b->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1526 | z->ob_size = -(z->ob_size); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1527 | Py_DECREF(a); |
| 1528 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1529 | return (PyObject *) long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1532 | /* The / and % operators are now defined in terms of divmod(). |
| 1533 | The expression a mod b has the value a - b*floor(a/b). |
| 1534 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1535 | |a| by |b|, with the sign of a. This is also expressed |
| 1536 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 1537 | Some examples: |
| 1538 | a b a rem b a mod b |
| 1539 | 13 10 3 3 |
| 1540 | -13 10 -3 7 |
| 1541 | 13 -10 3 -7 |
| 1542 | -13 -10 -3 -3 |
| 1543 | 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] | 1544 | have different signs. We then subtract one from the 'div' |
| 1545 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1546 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1547 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1548 | l_divmod(PyLongObject *v, PyLongObject *w, |
| 1549 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1550 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1551 | PyLongObject *div, *mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1552 | |
| 1553 | if (long_divrem(v, w, &div, &mod) < 0) |
| 1554 | return -1; |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 1555 | if ((mod->ob_size < 0 && w->ob_size > 0) || |
| 1556 | (mod->ob_size > 0 && w->ob_size < 0)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1557 | PyLongObject *temp; |
| 1558 | PyLongObject *one; |
| 1559 | temp = (PyLongObject *) long_add(mod, w); |
| 1560 | Py_DECREF(mod); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1561 | mod = temp; |
| 1562 | if (mod == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1563 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1564 | return -1; |
| 1565 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1566 | one = (PyLongObject *) PyLong_FromLong(1L); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1567 | if (one == NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1568 | (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
| 1569 | Py_DECREF(mod); |
| 1570 | Py_DECREF(div); |
| 1571 | Py_XDECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1572 | return -1; |
| 1573 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1574 | Py_DECREF(one); |
| 1575 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1576 | div = temp; |
| 1577 | } |
| 1578 | *pdiv = div; |
| 1579 | *pmod = mod; |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1583 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1584 | long_div(PyObject *v, PyObject *w) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1585 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1586 | PyLongObject *a, *b, *div, *mod; |
| 1587 | |
| 1588 | CONVERT_BINOP(v, w, &a, &b); |
| 1589 | |
| 1590 | if (l_divmod(a, b, &div, &mod) < 0) { |
| 1591 | Py_DECREF(a); |
| 1592 | Py_DECREF(b); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1593 | return NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1594 | } |
| 1595 | Py_DECREF(a); |
| 1596 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1597 | Py_DECREF(mod); |
| 1598 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1601 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1602 | long_classic_div(PyObject *v, PyObject *w) |
| 1603 | { |
| 1604 | PyLongObject *a, *b, *div, *mod; |
| 1605 | |
| 1606 | CONVERT_BINOP(v, w, &a, &b); |
| 1607 | |
| 1608 | if (Py_DivisionWarningFlag && |
| 1609 | PyErr_Warn(PyExc_DeprecationWarning, "classic long division") < 0) |
| 1610 | div = NULL; |
| 1611 | else if (l_divmod(a, b, &div, &mod) < 0) |
| 1612 | div = NULL; |
| 1613 | else |
| 1614 | Py_DECREF(mod); |
| 1615 | |
| 1616 | Py_DECREF(a); |
| 1617 | Py_DECREF(b); |
| 1618 | return (PyObject *)div; |
| 1619 | } |
| 1620 | |
| 1621 | static PyObject * |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 1622 | long_true_divide(PyObject *v, PyObject *w) |
| 1623 | { |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 1624 | PyLongObject *a, *b; |
| 1625 | double ad, bd; |
Tim Peters | 6f97e49 | 2001-11-04 23:09:40 +0000 | [diff] [blame] | 1626 | int aexp, bexp, failed; |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 1627 | |
| 1628 | CONVERT_BINOP(v, w, &a, &b); |
| 1629 | ad = _PyLong_AsScaledDouble((PyObject *)a, &aexp); |
| 1630 | bd = _PyLong_AsScaledDouble((PyObject *)b, &bexp); |
Tim Peters | 6f97e49 | 2001-11-04 23:09:40 +0000 | [diff] [blame] | 1631 | failed = (ad == -1.0 || bd == -1.0) && PyErr_Occurred(); |
| 1632 | Py_DECREF(a); |
| 1633 | Py_DECREF(b); |
| 1634 | if (failed) |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 1635 | return NULL; |
| 1636 | |
| 1637 | if (bd == 0.0) { |
| 1638 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 1639 | "long division or modulo by zero"); |
| 1640 | return NULL; |
| 1641 | } |
| 1642 | |
| 1643 | /* True value is very close to ad/bd * 2**(SHIFT*(aexp-bexp)) */ |
| 1644 | ad /= bd; /* overflow/underflow impossible here */ |
| 1645 | aexp -= bexp; |
| 1646 | if (aexp > INT_MAX / SHIFT) |
| 1647 | goto overflow; |
Tim Peters | e56ed9b | 2001-09-06 21:59:14 +0000 | [diff] [blame] | 1648 | else if (aexp < -(INT_MAX / SHIFT)) |
| 1649 | return PyFloat_FromDouble(0.0); /* underflow to 0 */ |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 1650 | errno = 0; |
| 1651 | ad = ldexp(ad, aexp * SHIFT); |
Tim Peters | 57f282a | 2001-09-05 05:38:10 +0000 | [diff] [blame] | 1652 | if (Py_OVERFLOWED(ad)) /* ignore underflow to 0.0 */ |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 1653 | goto overflow; |
| 1654 | return PyFloat_FromDouble(ad); |
| 1655 | |
| 1656 | overflow: |
| 1657 | PyErr_SetString(PyExc_OverflowError, |
| 1658 | "long/long too large for a float"); |
| 1659 | return NULL; |
| 1660 | |
Tim Peters | 20dab9f | 2001-09-04 05:31:47 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1664 | long_mod(PyObject *v, PyObject *w) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1665 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1666 | PyLongObject *a, *b, *div, *mod; |
| 1667 | |
| 1668 | CONVERT_BINOP(v, w, &a, &b); |
| 1669 | |
| 1670 | if (l_divmod(a, b, &div, &mod) < 0) { |
| 1671 | Py_DECREF(a); |
| 1672 | Py_DECREF(b); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1673 | return NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1674 | } |
| 1675 | Py_DECREF(a); |
| 1676 | Py_DECREF(b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1677 | Py_DECREF(div); |
| 1678 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1681 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1682 | long_divmod(PyObject *v, PyObject *w) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1683 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1684 | PyLongObject *a, *b, *div, *mod; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1685 | PyObject *z; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1686 | |
| 1687 | CONVERT_BINOP(v, w, &a, &b); |
| 1688 | |
| 1689 | if (l_divmod(a, b, &div, &mod) < 0) { |
| 1690 | Py_DECREF(a); |
| 1691 | Py_DECREF(b); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1692 | return NULL; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1693 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1694 | z = PyTuple_New(2); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1695 | if (z != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1696 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 1697 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1698 | } |
| 1699 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1700 | Py_DECREF(div); |
| 1701 | Py_DECREF(mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1702 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1703 | Py_DECREF(a); |
| 1704 | Py_DECREF(b); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1705 | return z; |
| 1706 | } |
| 1707 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1708 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1709 | long_pow(PyObject *v, PyObject *w, PyObject *x) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1710 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1711 | PyLongObject *a, *b; |
| 1712 | PyObject *c; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1713 | PyLongObject *z, *div, *mod; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1714 | int size_b, i; |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1715 | |
| 1716 | CONVERT_BINOP(v, w, &a, &b); |
| 1717 | if (PyLong_Check(x) || Py_None == x) { |
| 1718 | c = x; |
| 1719 | Py_INCREF(x); |
| 1720 | } |
| 1721 | else if (PyInt_Check(x)) { |
| 1722 | c = PyLong_FromLong(PyInt_AS_LONG(x)); |
| 1723 | } |
| 1724 | else { |
| 1725 | Py_DECREF(a); |
| 1726 | Py_DECREF(b); |
| 1727 | Py_INCREF(Py_NotImplemented); |
| 1728 | return Py_NotImplemented; |
| 1729 | } |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (c != Py_None && ((PyLongObject *)c)->ob_size == 0) { |
| 1732 | PyErr_SetString(PyExc_ValueError, |
| 1733 | "pow() 3rd argument cannot be 0"); |
| 1734 | z = NULL; |
| 1735 | goto error; |
| 1736 | } |
| 1737 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1738 | size_b = b->ob_size; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1739 | if (size_b < 0) { |
Guido van Rossum | 0ec9aba | 2001-07-12 11:21:17 +0000 | [diff] [blame] | 1740 | Py_DECREF(a); |
| 1741 | Py_DECREF(b); |
| 1742 | Py_DECREF(c); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 1743 | if (x != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 1744 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
| 1745 | "cannot be negative when 3rd argument specified"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 1746 | return NULL; |
| 1747 | } |
| 1748 | /* Return a float. This works because we know that |
| 1749 | this calls float_pow() which converts its |
| 1750 | arguments to double. */ |
Guido van Rossum | 0ec9aba | 2001-07-12 11:21:17 +0000 | [diff] [blame] | 1751 | return PyFloat_Type.tp_as_number->nb_power(v, w, x); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1752 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1753 | z = (PyLongObject *)PyLong_FromLong(1L); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1754 | for (i = 0; i < size_b; ++i) { |
| 1755 | digit bi = b->ob_digit[i]; |
| 1756 | int j; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1757 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1758 | for (j = 0; j < SHIFT; ++j) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1759 | PyLongObject *temp; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1760 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1761 | if (bi & 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1762 | temp = (PyLongObject *)long_mul(z, a); |
| 1763 | Py_DECREF(z); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1764 | if (c!=Py_None && temp!=NULL) { |
| 1765 | if (l_divmod(temp,(PyLongObject *)c, |
| 1766 | &div,&mod) < 0) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1767 | Py_DECREF(temp); |
| 1768 | z = NULL; |
| 1769 | goto error; |
| 1770 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1771 | Py_XDECREF(div); |
| 1772 | Py_DECREF(temp); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1773 | temp = mod; |
| 1774 | } |
| 1775 | z = temp; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1776 | if (z == NULL) |
| 1777 | break; |
| 1778 | } |
| 1779 | bi >>= 1; |
| 1780 | if (bi == 0 && i+1 == size_b) |
| 1781 | break; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1782 | temp = (PyLongObject *)long_mul(a, a); |
| 1783 | Py_DECREF(a); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1784 | if (c!=Py_None && temp!=NULL) { |
| 1785 | if (l_divmod(temp, (PyLongObject *)c, &div, |
| 1786 | &mod) < 0) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1787 | Py_DECREF(temp); |
| 1788 | z = NULL; |
| 1789 | goto error; |
| 1790 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1791 | Py_XDECREF(div); |
| 1792 | Py_DECREF(temp); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1793 | temp = mod; |
| 1794 | } |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1795 | a = temp; |
| 1796 | if (a == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1797 | Py_DECREF(z); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1798 | z = NULL; |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 1802 | if (a == NULL || z == NULL) |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1805 | if (c!=Py_None && z!=NULL) { |
| 1806 | if (l_divmod(z, (PyLongObject *)c, &div, &mod) < 0) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1807 | Py_DECREF(z); |
| 1808 | z = NULL; |
| 1809 | } |
| 1810 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1811 | Py_XDECREF(div); |
| 1812 | Py_DECREF(z); |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1813 | z = mod; |
| 1814 | } |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1815 | } |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1816 | error: |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1817 | Py_XDECREF(a); |
| 1818 | Py_DECREF(b); |
| 1819 | Py_DECREF(c); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1820 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1823 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1824 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1825 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1826 | /* Implement ~x as -(x+1) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1827 | PyLongObject *x; |
| 1828 | PyLongObject *w; |
| 1829 | w = (PyLongObject *)PyLong_FromLong(1L); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1830 | if (w == NULL) |
| 1831 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1832 | x = (PyLongObject *) long_add(v, w); |
| 1833 | Py_DECREF(w); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1834 | if (x == NULL) |
| 1835 | return NULL; |
Tim Peters | 40c397d | 2001-09-11 23:24:22 +0000 | [diff] [blame] | 1836 | x->ob_size = -(x->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1837 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1840 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1841 | long_pos(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1842 | { |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1843 | if (PyLong_CheckExact(v)) { |
| 1844 | Py_INCREF(v); |
| 1845 | return (PyObject *)v; |
| 1846 | } |
| 1847 | else |
| 1848 | return _PyLong_Copy(v); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1851 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1852 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1853 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1854 | PyLongObject *z; |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1855 | if (v->ob_size == 0 && PyLong_CheckExact(v)) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1856 | /* -0 == 0 */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1857 | Py_INCREF(v); |
| 1858 | return (PyObject *) v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1859 | } |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1860 | z = (PyLongObject *)_PyLong_Copy(v); |
| 1861 | if (z != NULL) |
| 1862 | z->ob_size = -(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1863 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1866 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1867 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1868 | { |
| 1869 | if (v->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1870 | return long_neg(v); |
Tim Peters | 69c2de3 | 2001-09-11 22:31:33 +0000 | [diff] [blame] | 1871 | else |
| 1872 | return long_pos(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1875 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1876 | long_nonzero(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1877 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1878 | return ABS(v->ob_size) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1881 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1882 | long_rshift(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1883 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1884 | PyLongObject *a, *b; |
| 1885 | PyLongObject *z = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1886 | long shiftby; |
| 1887 | int newsize, wordshift, loshift, hishift, i, j; |
| 1888 | digit lomask, himask; |
| 1889 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1890 | CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); |
| 1891 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1892 | if (a->ob_size < 0) { |
| 1893 | /* Right shifting negative numbers is harder */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1894 | PyLongObject *a1, *a2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1895 | a1 = (PyLongObject *) long_invert(a); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1896 | if (a1 == NULL) |
| 1897 | goto rshift_error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1898 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 1899 | Py_DECREF(a1); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1900 | if (a2 == NULL) |
| 1901 | goto rshift_error; |
| 1902 | z = (PyLongObject *) long_invert(a2); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1903 | Py_DECREF(a2); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1904 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1905 | else { |
| 1906 | |
| 1907 | shiftby = PyLong_AsLong((PyObject *)b); |
| 1908 | if (shiftby == -1L && PyErr_Occurred()) |
| 1909 | goto rshift_error; |
| 1910 | if (shiftby < 0) { |
| 1911 | PyErr_SetString(PyExc_ValueError, |
| 1912 | "negative shift count"); |
| 1913 | goto rshift_error; |
| 1914 | } |
| 1915 | wordshift = shiftby / SHIFT; |
| 1916 | newsize = ABS(a->ob_size) - wordshift; |
| 1917 | if (newsize <= 0) { |
| 1918 | z = _PyLong_New(0); |
| 1919 | Py_DECREF(a); |
| 1920 | Py_DECREF(b); |
| 1921 | return (PyObject *)z; |
| 1922 | } |
| 1923 | loshift = shiftby % SHIFT; |
| 1924 | hishift = SHIFT - loshift; |
| 1925 | lomask = ((digit)1 << hishift) - 1; |
| 1926 | himask = MASK ^ lomask; |
| 1927 | z = _PyLong_New(newsize); |
| 1928 | if (z == NULL) |
| 1929 | goto rshift_error; |
| 1930 | if (a->ob_size < 0) |
| 1931 | z->ob_size = -(z->ob_size); |
| 1932 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 1933 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 1934 | if (i+1 < newsize) |
| 1935 | z->ob_digit[i] |= |
| 1936 | (a->ob_digit[j+1] << hishift) & himask; |
| 1937 | } |
| 1938 | z = long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1939 | } |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1940 | rshift_error: |
| 1941 | Py_DECREF(a); |
| 1942 | Py_DECREF(b); |
| 1943 | return (PyObject *) z; |
| 1944 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1947 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1948 | long_lshift(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1949 | { |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1950 | /* This version due to Tim Peters */ |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1951 | PyLongObject *a, *b; |
| 1952 | PyLongObject *z = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1953 | long shiftby; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1954 | int oldsize, newsize, wordshift, remshift, i, j; |
| 1955 | twodigits accum; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1956 | |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1957 | CONVERT_BINOP(v, w, &a, &b); |
| 1958 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1959 | shiftby = PyLong_AsLong((PyObject *)b); |
| 1960 | if (shiftby == -1L && PyErr_Occurred()) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1961 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1962 | if (shiftby < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1963 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1964 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1965 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1966 | if ((long)(int)shiftby != shiftby) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1967 | PyErr_SetString(PyExc_ValueError, |
| 1968 | "outrageous left shift count"); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1969 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1970 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1971 | /* wordshift, remshift = divmod(shiftby, SHIFT) */ |
| 1972 | wordshift = (int)shiftby / SHIFT; |
| 1973 | remshift = (int)shiftby - wordshift * SHIFT; |
| 1974 | |
| 1975 | oldsize = ABS(a->ob_size); |
| 1976 | newsize = oldsize + wordshift; |
| 1977 | if (remshift) |
| 1978 | ++newsize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1979 | z = _PyLong_New(newsize); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1980 | if (z == NULL) |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1981 | goto lshift_error; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1982 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1983 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1984 | for (i = 0; i < wordshift; i++) |
| 1985 | z->ob_digit[i] = 0; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1986 | accum = 0; |
| 1987 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 1988 | accum |= a->ob_digit[j] << remshift; |
| 1989 | z->ob_digit[i] = (digit)(accum & MASK); |
| 1990 | accum >>= SHIFT; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1991 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1992 | if (remshift) |
| 1993 | z->ob_digit[newsize-1] = (digit)accum; |
| 1994 | else |
| 1995 | assert(!accum); |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 1996 | z = long_normalize(z); |
| 1997 | lshift_error: |
| 1998 | Py_DECREF(a); |
| 1999 | Py_DECREF(b); |
| 2000 | return (PyObject *) z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2003 | |
| 2004 | /* Bitwise and/xor/or operations */ |
| 2005 | |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 2006 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 2007 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 2008 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2009 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2010 | long_bitwise(PyLongObject *a, |
| 2011 | int op, /* '&', '|', '^' */ |
| 2012 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2013 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2014 | digit maska, maskb; /* 0 or MASK */ |
| 2015 | int negz; |
| 2016 | int size_a, size_b, size_z; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2017 | PyLongObject *z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2018 | int i; |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 2019 | digit diga, digb; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2020 | PyObject *v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2021 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2022 | if (a->ob_size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2023 | a = (PyLongObject *) long_invert(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2024 | maska = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2025 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2026 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2027 | Py_INCREF(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2028 | maska = 0; |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 2029 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2030 | if (b->ob_size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2031 | b = (PyLongObject *) long_invert(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2032 | maskb = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2033 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2034 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2035 | Py_INCREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2036 | maskb = 0; |
| 2037 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2038 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2039 | negz = 0; |
| 2040 | switch (op) { |
| 2041 | case '^': |
| 2042 | if (maska != maskb) { |
| 2043 | maska ^= MASK; |
| 2044 | negz = -1; |
| 2045 | } |
| 2046 | break; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 2047 | case '&': |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2048 | if (maska && maskb) { |
| 2049 | op = '|'; |
| 2050 | maska ^= MASK; |
| 2051 | maskb ^= MASK; |
| 2052 | negz = -1; |
| 2053 | } |
| 2054 | break; |
| 2055 | case '|': |
| 2056 | if (maska || maskb) { |
| 2057 | op = '&'; |
| 2058 | maska ^= MASK; |
| 2059 | maskb ^= MASK; |
| 2060 | negz = -1; |
| 2061 | } |
| 2062 | break; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2063 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2064 | |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 2065 | /* JRH: The original logic here was to allocate the result value (z) |
| 2066 | as the longer of the two operands. However, there are some cases |
| 2067 | where the result is guaranteed to be shorter than that: AND of two |
| 2068 | positives, OR of two negatives: use the shorter number. AND with |
| 2069 | mixed signs: use the positive number. OR with mixed signs: use the |
| 2070 | negative number. After the transformations above, op will be '&' |
| 2071 | iff one of these cases applies, and mask will be non-0 for operands |
| 2072 | whose length should be ignored. |
| 2073 | */ |
| 2074 | |
| 2075 | size_a = a->ob_size; |
| 2076 | size_b = b->ob_size; |
| 2077 | size_z = op == '&' |
| 2078 | ? (maska |
| 2079 | ? size_b |
| 2080 | : (maskb ? size_a : MIN(size_a, size_b))) |
| 2081 | : MAX(size_a, size_b); |
| 2082 | z = _PyLong_New(size_z); |
| 2083 | if (a == NULL || b == NULL || z == NULL) { |
| 2084 | Py_XDECREF(a); |
| 2085 | Py_XDECREF(b); |
| 2086 | Py_XDECREF(z); |
| 2087 | return NULL; |
| 2088 | } |
| 2089 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2090 | for (i = 0; i < size_z; ++i) { |
| 2091 | diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; |
| 2092 | digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; |
| 2093 | switch (op) { |
| 2094 | case '&': z->ob_digit[i] = diga & digb; break; |
| 2095 | case '|': z->ob_digit[i] = diga | digb; break; |
| 2096 | case '^': z->ob_digit[i] = diga ^ digb; break; |
| 2097 | } |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 2098 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2099 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2100 | Py_DECREF(a); |
| 2101 | Py_DECREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2102 | z = long_normalize(z); |
| 2103 | if (negz == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2104 | return (PyObject *) z; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2105 | v = long_invert(z); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2106 | Py_DECREF(z); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 2107 | return v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2110 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2111 | long_and(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2112 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2113 | PyLongObject *a, *b; |
| 2114 | PyObject *c; |
| 2115 | CONVERT_BINOP(v, w, &a, &b); |
| 2116 | c = long_bitwise(a, '&', b); |
| 2117 | Py_DECREF(a); |
| 2118 | Py_DECREF(b); |
| 2119 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2122 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2123 | long_xor(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2124 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2125 | PyLongObject *a, *b; |
| 2126 | PyObject *c; |
| 2127 | CONVERT_BINOP(v, w, &a, &b); |
| 2128 | c = long_bitwise(a, '^', b); |
| 2129 | Py_DECREF(a); |
| 2130 | Py_DECREF(b); |
| 2131 | return c; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2134 | static PyObject * |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2135 | long_or(PyObject *v, PyObject *w) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 2136 | { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2137 | PyLongObject *a, *b; |
| 2138 | PyObject *c; |
| 2139 | CONVERT_BINOP(v, w, &a, &b); |
| 2140 | c = long_bitwise(a, '|', b); |
| 2141 | Py_DECREF(a); |
| 2142 | Py_DECREF(b); |
| 2143 | return c; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 2146 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2147 | long_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 2148 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2149 | if (PyInt_Check(*pw)) { |
Neil Schemenauer | ba872e2 | 2001-01-04 01:46:03 +0000 | [diff] [blame] | 2150 | *pw = PyLong_FromLong(PyInt_AS_LONG(*pw)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2151 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 2152 | return 0; |
| 2153 | } |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 2154 | else if (PyLong_Check(*pw)) { |
| 2155 | Py_INCREF(*pv); |
| 2156 | Py_INCREF(*pw); |
| 2157 | return 0; |
| 2158 | } |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 2159 | return 1; /* Can't do it */ |
| 2160 | } |
| 2161 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2162 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2163 | long_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2164 | { |
| 2165 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2166 | x = PyLong_AsLong(v); |
| 2167 | if (PyErr_Occurred()) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2168 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2169 | return PyInt_FromLong(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2172 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2173 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2174 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2175 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2176 | return v; |
| 2177 | } |
| 2178 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2179 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2180 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2181 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 2182 | double result; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2183 | result = PyLong_AsDouble(v); |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 2184 | if (result == -1.0 && PyErr_Occurred()) |
| 2185 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2186 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2189 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2190 | long_oct(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2191 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 2192 | return long_format(v, 8, 1); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2195 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2196 | long_hex(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2197 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 2198 | return long_format(v, 16, 1); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2199 | } |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2200 | staticforward PyObject * |
| 2201 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 2202 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2203 | static PyObject * |
| 2204 | long_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2205 | { |
| 2206 | PyObject *x = NULL; |
| 2207 | int base = -909; /* unlikely! */ |
| 2208 | static char *kwlist[] = {"x", "base", 0}; |
| 2209 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2210 | if (type != &PyLong_Type) |
| 2211 | return long_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2212 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist, |
| 2213 | &x, &base)) |
| 2214 | return NULL; |
| 2215 | if (x == NULL) |
| 2216 | return PyLong_FromLong(0L); |
| 2217 | if (base == -909) |
| 2218 | return PyNumber_Long(x); |
| 2219 | else if (PyString_Check(x)) |
| 2220 | return PyLong_FromString(PyString_AS_STRING(x), NULL, base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2221 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2222 | else if (PyUnicode_Check(x)) |
| 2223 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 2224 | PyUnicode_GET_SIZE(x), |
| 2225 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2226 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2227 | else { |
| 2228 | PyErr_SetString(PyExc_TypeError, |
| 2229 | "long() can't convert non-string with explicit base"); |
| 2230 | return NULL; |
| 2231 | } |
| 2232 | } |
| 2233 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2234 | /* Wimpy, slow approach to tp_new calls for subtypes of long: |
| 2235 | first create a regular long from whatever arguments we got, |
| 2236 | then allocate a subtype instance and initialize it from |
| 2237 | the regular long. The regular long is then thrown away. |
| 2238 | */ |
| 2239 | static PyObject * |
| 2240 | long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2241 | { |
| 2242 | PyLongObject *tmp, *new; |
| 2243 | int i, n; |
| 2244 | |
| 2245 | assert(PyType_IsSubtype(type, &PyLong_Type)); |
| 2246 | tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds); |
| 2247 | if (tmp == NULL) |
| 2248 | return NULL; |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 2249 | assert(PyLong_CheckExact(tmp)); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2250 | n = tmp->ob_size; |
| 2251 | if (n < 0) |
| 2252 | n = -n; |
| 2253 | new = (PyLongObject *)type->tp_alloc(type, n); |
| 2254 | if (new == NULL) |
| 2255 | return NULL; |
| 2256 | assert(PyLong_Check(new)); |
Guido van Rossum | 13228a6 | 2001-08-30 15:54:44 +0000 | [diff] [blame] | 2257 | new->ob_size = tmp->ob_size; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2258 | for (i = 0; i < n; i++) |
| 2259 | new->ob_digit[i] = tmp->ob_digit[i]; |
| 2260 | Py_DECREF(tmp); |
| 2261 | return (PyObject *)new; |
| 2262 | } |
| 2263 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2264 | static char long_doc[] = |
| 2265 | "long(x[, base]) -> integer\n\ |
| 2266 | \n\ |
| 2267 | Convert a string or number to a long integer, if possible. A floating\n\ |
| 2268 | point argument will be truncated towards zero (this does not include a\n\ |
| 2269 | string representation of a floating point number!) When converting a\n\ |
| 2270 | string, use the optional base. It is an error to supply a base when\n\ |
| 2271 | converting a non-string."; |
| 2272 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2273 | static PyNumberMethods long_as_number = { |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2274 | (binaryfunc) long_add, /*nb_add*/ |
| 2275 | (binaryfunc) long_sub, /*nb_subtract*/ |
| 2276 | (binaryfunc) long_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 2277 | (binaryfunc) long_classic_div, /*nb_divide*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2278 | (binaryfunc) long_mod, /*nb_remainder*/ |
| 2279 | (binaryfunc) long_divmod, /*nb_divmod*/ |
| 2280 | (ternaryfunc) long_pow, /*nb_power*/ |
| 2281 | (unaryfunc) long_neg, /*nb_negative*/ |
| 2282 | (unaryfunc) long_pos, /*tp_positive*/ |
| 2283 | (unaryfunc) long_abs, /*tp_absolute*/ |
| 2284 | (inquiry) long_nonzero, /*tp_nonzero*/ |
| 2285 | (unaryfunc) long_invert, /*nb_invert*/ |
| 2286 | (binaryfunc) long_lshift, /*nb_lshift*/ |
| 2287 | (binaryfunc) long_rshift, /*nb_rshift*/ |
| 2288 | (binaryfunc) long_and, /*nb_and*/ |
| 2289 | (binaryfunc) long_xor, /*nb_xor*/ |
| 2290 | (binaryfunc) long_or, /*nb_or*/ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 2291 | (coercion) long_coerce, /*nb_coerce*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 2292 | (unaryfunc) long_int, /*nb_int*/ |
| 2293 | (unaryfunc) long_long, /*nb_long*/ |
| 2294 | (unaryfunc) long_float, /*nb_float*/ |
| 2295 | (unaryfunc) long_oct, /*nb_oct*/ |
| 2296 | (unaryfunc) long_hex, /*nb_hex*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2297 | 0, /* nb_inplace_add */ |
| 2298 | 0, /* nb_inplace_subtract */ |
| 2299 | 0, /* nb_inplace_multiply */ |
| 2300 | 0, /* nb_inplace_divide */ |
| 2301 | 0, /* nb_inplace_remainder */ |
| 2302 | 0, /* nb_inplace_power */ |
| 2303 | 0, /* nb_inplace_lshift */ |
| 2304 | 0, /* nb_inplace_rshift */ |
| 2305 | 0, /* nb_inplace_and */ |
| 2306 | 0, /* nb_inplace_xor */ |
| 2307 | 0, /* nb_inplace_or */ |
| 2308 | (binaryfunc)long_div, /* nb_floor_divide */ |
| 2309 | long_true_divide, /* nb_true_divide */ |
| 2310 | 0, /* nb_inplace_floor_divide */ |
| 2311 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2312 | }; |
| 2313 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2314 | PyTypeObject PyLong_Type = { |
| 2315 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2316 | 0, /* ob_size */ |
| 2317 | "long", /* tp_name */ |
| 2318 | sizeof(PyLongObject) - sizeof(digit), /* tp_basicsize */ |
| 2319 | sizeof(digit), /* tp_itemsize */ |
| 2320 | (destructor)long_dealloc, /* tp_dealloc */ |
| 2321 | 0, /* tp_print */ |
| 2322 | 0, /* tp_getattr */ |
| 2323 | 0, /* tp_setattr */ |
| 2324 | (cmpfunc)long_compare, /* tp_compare */ |
| 2325 | (reprfunc)long_repr, /* tp_repr */ |
| 2326 | &long_as_number, /* tp_as_number */ |
| 2327 | 0, /* tp_as_sequence */ |
| 2328 | 0, /* tp_as_mapping */ |
| 2329 | (hashfunc)long_hash, /* tp_hash */ |
| 2330 | 0, /* tp_call */ |
| 2331 | (reprfunc)long_str, /* tp_str */ |
| 2332 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2333 | 0, /* tp_setattro */ |
| 2334 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 2335 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 2336 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2337 | long_doc, /* tp_doc */ |
| 2338 | 0, /* tp_traverse */ |
| 2339 | 0, /* tp_clear */ |
| 2340 | 0, /* tp_richcompare */ |
| 2341 | 0, /* tp_weaklistoffset */ |
| 2342 | 0, /* tp_iter */ |
| 2343 | 0, /* tp_iternext */ |
| 2344 | 0, /* tp_methods */ |
| 2345 | 0, /* tp_members */ |
| 2346 | 0, /* tp_getset */ |
| 2347 | 0, /* tp_base */ |
| 2348 | 0, /* tp_dict */ |
| 2349 | 0, /* tp_descr_get */ |
| 2350 | 0, /* tp_descr_set */ |
| 2351 | 0, /* tp_dictoffset */ |
| 2352 | 0, /* tp_init */ |
| 2353 | 0, /* tp_alloc */ |
| 2354 | long_new, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 2355 | PyObject_Del, /* tp_free */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 2356 | }; |