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 | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 9 | #include <assert.h> |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 12 | #define ABS(x) ((x) < 0 ? -(x) : (x)) |
| 13 | |
| 14 | /* Forward */ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 15 | static PyLongObject *long_normalize(PyLongObject *); |
| 16 | static PyLongObject *mul1(PyLongObject *, wdigit); |
| 17 | static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); |
| 18 | static PyLongObject *divrem1(PyLongObject *, wdigit, digit *); |
| 19 | static PyObject *long_format(PyObject *aa, int base, int addL); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 21 | static int ticker; /* XXX Could be shared with ceval? */ |
| 22 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 23 | #define SIGCHECK(PyTryBlock) \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 24 | if (--ticker < 0) { \ |
| 25 | ticker = 100; \ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 26 | if (PyErr_CheckSignals()) { PyTryBlock; } \ |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 29 | /* Normalize (remove leading zeros from) a long int object. |
| 30 | Doesn't attempt to free the storage--in most cases, due to the nature |
| 31 | of the algorithms used, this could save at most be one word anyway. */ |
| 32 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 34 | long_normalize(register PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 35 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 36 | int j = ABS(v->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 37 | register int i = j; |
| 38 | |
| 39 | while (i > 0 && v->ob_digit[i-1] == 0) |
| 40 | --i; |
| 41 | if (i != j) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 42 | v->ob_size = (v->ob_size < 0) ? -(i) : i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 43 | return v; |
| 44 | } |
| 45 | |
| 46 | /* Allocate a new long int object with size digits. |
| 47 | Return NULL and set exception if we run out of memory. */ |
| 48 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 50 | _PyLong_New(int size) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 51 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* Create a new long int object from a C long int */ |
| 56 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 58 | PyLong_FromLong(long ival) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 59 | { |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 60 | /* Assume a C long fits in at most 5 'digits' */ |
| 61 | /* Works on both 32- and 64-bit machines */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | PyLongObject *v = _PyLong_New(5); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 63 | if (v != NULL) { |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 64 | unsigned long t = ival; |
| 65 | int i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 66 | if (ival < 0) { |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 67 | t = -ival; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 68 | v->ob_size = -(v->ob_size); |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 69 | } |
| 70 | for (i = 0; i < 5; i++) { |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 71 | v->ob_digit[i] = (digit) (t & MASK); |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 72 | t >>= SHIFT; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 73 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 74 | v = long_normalize(v); |
| 75 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | return (PyObject *)v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 79 | /* Create a new long int object from a C unsigned long int */ |
| 80 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 82 | PyLong_FromUnsignedLong(unsigned long ival) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 83 | { |
| 84 | /* Assume a C long fits in at most 5 'digits' */ |
| 85 | /* Works on both 32- and 64-bit machines */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | PyLongObject *v = _PyLong_New(5); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 87 | if (v != NULL) { |
| 88 | unsigned long t = ival; |
| 89 | int i; |
| 90 | for (i = 0; i < 5; i++) { |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 91 | v->ob_digit[i] = (digit) (t & MASK); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 92 | t >>= SHIFT; |
| 93 | } |
| 94 | v = long_normalize(v); |
| 95 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | return (PyObject *)v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 99 | /* Create a new long int object from a C double */ |
| 100 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | PyObject * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | PyLong_FromDouble(double dval) |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 104 | PyLongObject *v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 105 | double frac; |
| 106 | int i, ndig, expo, neg; |
| 107 | neg = 0; |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 108 | if (Py_IS_INFINITY(dval)) { |
Guido van Rossum | 1a23c24 | 1999-09-27 17:11:52 +0000 | [diff] [blame] | 109 | PyErr_SetString(PyExc_OverflowError, |
| 110 | "cannot convert float infinity to long"); |
| 111 | return NULL; |
| 112 | } |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 113 | if (dval < 0.0) { |
| 114 | neg = 1; |
| 115 | dval = -dval; |
| 116 | } |
| 117 | frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ |
| 118 | if (expo <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 119 | return PyLong_FromLong(0L); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 120 | ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | v = _PyLong_New(ndig); |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 122 | if (v == NULL) |
| 123 | return NULL; |
| 124 | frac = ldexp(frac, (expo-1) % SHIFT + 1); |
| 125 | for (i = ndig; --i >= 0; ) { |
| 126 | long bits = (long)frac; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 127 | v->ob_digit[i] = (digit) bits; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 128 | frac = frac - (double)bits; |
| 129 | frac = ldexp(frac, SHIFT); |
| 130 | } |
| 131 | if (neg) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 132 | v->ob_size = -(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | return (PyObject *)v; |
Guido van Rossum | 149e9ea | 1991-06-03 10:58:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 136 | /* Get a C long int from a long int object. |
| 137 | Returns -1 and sets an error condition if overflow occurs. */ |
| 138 | |
| 139 | long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 140 | PyLong_AsLong(PyObject *vv) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 141 | { |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 142 | /* This version by Tim Peters */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | register PyLongObject *v; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 144 | unsigned long x, prev; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 145 | int i, sign; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 147 | if (vv == NULL || !PyLong_Check(vv)) { |
| 148 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 149 | return -1; |
| 150 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | v = (PyLongObject *)vv; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 152 | i = v->ob_size; |
| 153 | sign = 1; |
| 154 | x = 0; |
| 155 | if (i < 0) { |
| 156 | sign = -1; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 157 | i = -(i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 158 | } |
| 159 | while (--i >= 0) { |
| 160 | prev = x; |
| 161 | x = (x << SHIFT) + v->ob_digit[i]; |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 162 | if ((x >> SHIFT) != prev) |
| 163 | goto overflow; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 164 | } |
Guido van Rossum | f753181 | 1998-05-26 14:33:37 +0000 | [diff] [blame] | 165 | /* Haven't lost any bits, but if the sign bit is set we're in |
| 166 | * trouble *unless* this is the min negative number. So, |
| 167 | * trouble iff sign bit set && (positive || some bit set other |
| 168 | * than the sign bit). |
| 169 | */ |
| 170 | if ((long)x < 0 && (sign > 0 || (x << 1) != 0)) |
| 171 | goto overflow; |
| 172 | return (long)x * sign; |
| 173 | |
| 174 | overflow: |
| 175 | PyErr_SetString(PyExc_OverflowError, |
| 176 | "long int too long to convert"); |
| 177 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 180 | /* Get a C long int from a long int object. |
| 181 | Returns -1 and sets an error condition if overflow occurs. */ |
| 182 | |
| 183 | unsigned long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 184 | PyLong_AsUnsignedLong(PyObject *vv) |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 185 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | register PyLongObject *v; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 187 | unsigned long x, prev; |
| 188 | int i; |
| 189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 190 | if (vv == NULL || !PyLong_Check(vv)) { |
| 191 | PyErr_BadInternalCall(); |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 192 | return (unsigned long) -1; |
| 193 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 194 | v = (PyLongObject *)vv; |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 195 | i = v->ob_size; |
| 196 | x = 0; |
| 197 | if (i < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 198 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 199 | "can't convert negative value to unsigned long"); |
| 200 | return (unsigned long) -1; |
| 201 | } |
| 202 | while (--i >= 0) { |
| 203 | prev = x; |
| 204 | x = (x << SHIFT) + v->ob_digit[i]; |
| 205 | if ((x >> SHIFT) != prev) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 206 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 53756b1 | 1997-01-03 17:14:46 +0000 | [diff] [blame] | 207 | "long int too long to convert"); |
| 208 | return (unsigned long) -1; |
| 209 | } |
| 210 | } |
| 211 | return x; |
| 212 | } |
| 213 | |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 214 | /* Get a C double from a long int object. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 215 | |
| 216 | double |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 217 | PyLong_AsDouble(PyObject *vv) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 218 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 219 | register PyLongObject *v; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 220 | double x; |
| 221 | double multiplier = (double) (1L << SHIFT); |
| 222 | int i, sign; |
| 223 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | if (vv == NULL || !PyLong_Check(vv)) { |
| 225 | PyErr_BadInternalCall(); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 226 | return -1; |
| 227 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 228 | v = (PyLongObject *)vv; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 229 | i = v->ob_size; |
| 230 | sign = 1; |
| 231 | x = 0.0; |
| 232 | if (i < 0) { |
| 233 | sign = -1; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 234 | i = -(i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 235 | } |
| 236 | while (--i >= 0) { |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 237 | x = x*multiplier + (double)v->ob_digit[i]; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 238 | } |
| 239 | return x * sign; |
| 240 | } |
| 241 | |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 242 | /* Create a new long (or int) object from a C pointer */ |
| 243 | |
| 244 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 245 | PyLong_FromVoidPtr(void *p) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 246 | { |
| 247 | #if SIZEOF_VOID_P == SIZEOF_LONG |
| 248 | return PyInt_FromLong((long)p); |
| 249 | #else |
| 250 | /* optimize null pointers */ |
| 251 | if ( p == NULL ) |
| 252 | return PyInt_FromLong(0); |
| 253 | |
| 254 | /* we can assume that HAVE_LONG_LONG is true. if not, then the |
| 255 | configuration process should have bailed (having big pointers |
| 256 | without long longs seems non-sensical) */ |
| 257 | return PyLong_FromLongLong((LONG_LONG)p); |
| 258 | #endif /* SIZEOF_VOID_P == SIZEOF_LONG */ |
| 259 | } |
| 260 | |
| 261 | /* Get a C pointer from a long object (or an int object in some cases) */ |
| 262 | |
| 263 | void * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 264 | PyLong_AsVoidPtr(PyObject *vv) |
Guido van Rossum | 78694d9 | 1998-09-18 14:14:13 +0000 | [diff] [blame] | 265 | { |
| 266 | /* This function will allow int or long objects. If vv is neither, |
| 267 | then the PyLong_AsLong*() functions will raise the exception: |
| 268 | PyExc_SystemError, "bad argument to internal function" |
| 269 | */ |
| 270 | |
| 271 | #if SIZEOF_VOID_P == SIZEOF_LONG |
| 272 | long x; |
| 273 | |
| 274 | if ( PyInt_Check(vv) ) |
| 275 | x = PyInt_AS_LONG(vv); |
| 276 | else |
| 277 | x = PyLong_AsLong(vv); |
| 278 | #else |
| 279 | /* we can assume that HAVE_LONG_LONG is true. if not, then the |
| 280 | configuration process should have bailed (having big pointers |
| 281 | without long longs seems non-sensical) */ |
| 282 | LONG_LONG x; |
| 283 | |
| 284 | if ( PyInt_Check(vv) ) |
| 285 | x = PyInt_AS_LONG(vv); |
| 286 | else |
| 287 | x = PyLong_AsLongLong(vv); |
| 288 | #endif /* SIZEOF_VOID_P == SIZEOF_LONG */ |
| 289 | |
| 290 | if (x == -1 && PyErr_Occurred()) |
| 291 | return NULL; |
| 292 | return (void *)x; |
| 293 | } |
| 294 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 295 | #ifdef HAVE_LONG_LONG |
| 296 | /* |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 297 | * LONG_LONG support by Chris Herborth (chrish@qnx.com) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 298 | * |
| 299 | * For better or worse :-), I tried to follow the coding style already |
| 300 | * here. |
| 301 | */ |
| 302 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 303 | /* 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] | 304 | |
| 305 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 306 | PyLong_FromLongLong(LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 307 | { |
| 308 | #if SIZEOF_LONG_LONG == SIZEOF_LONG |
| 309 | /* In case the compiler is faking it. */ |
| 310 | return PyLong_FromLong( (long)ival ); |
| 311 | #else |
Fred Drake | 4c7fdfc | 2000-06-01 18:37:36 +0000 | [diff] [blame] | 312 | if ((LONG_LONG)LONG_MIN <= ival && ival <= (LONG_LONG)LONG_MAX) { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 313 | return PyLong_FromLong( (long)ival ); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 314 | } |
Fred Drake | 4c7fdfc | 2000-06-01 18:37:36 +0000 | [diff] [blame] | 315 | else if (0 <= ival && ival <= (unsigned LONG_LONG)ULONG_MAX) { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 316 | return PyLong_FromUnsignedLong( (unsigned long)ival ); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 317 | } |
| 318 | else { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 319 | /* Assume a C LONG_LONG fits in at most 10 'digits'. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 320 | * Should be OK if we're assuming long fits in 5. |
| 321 | */ |
| 322 | PyLongObject *v = _PyLong_New(10); |
| 323 | |
| 324 | if (v != NULL) { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 325 | unsigned LONG_LONG t = ival; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 326 | int i; |
| 327 | if (ival < 0) { |
| 328 | t = -ival; |
| 329 | v->ob_size = -(v->ob_size); |
| 330 | } |
| 331 | |
| 332 | for (i = 0; i < 10; i++) { |
| 333 | v->ob_digit[i] = (digit) (t & MASK); |
| 334 | t >>= SHIFT; |
| 335 | } |
| 336 | |
| 337 | v = long_normalize(v); |
| 338 | } |
| 339 | |
| 340 | return (PyObject *)v; |
| 341 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 342 | #endif |
| 343 | } |
| 344 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 345 | /* Create a new long int object from a C unsigned LONG_LONG int */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 346 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 347 | PyLong_FromUnsignedLongLong(unsigned LONG_LONG ival) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 348 | { |
| 349 | #if SIZEOF_LONG_LONG == SIZEOF_LONG |
| 350 | /* In case the compiler is faking it. */ |
| 351 | return PyLong_FromUnsignedLong( (unsigned long)ival ); |
| 352 | #else |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 353 | if( ival <= (unsigned LONG_LONG)ULONG_MAX ) { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 354 | return PyLong_FromUnsignedLong( (unsigned long)ival ); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 355 | } |
| 356 | else { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 357 | /* Assume a C long fits in at most 10 'digits'. */ |
| 358 | PyLongObject *v = _PyLong_New(10); |
| 359 | |
| 360 | if (v != NULL) { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 361 | unsigned LONG_LONG t = ival; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 362 | int i; |
| 363 | for (i = 0; i < 10; i++) { |
| 364 | v->ob_digit[i] = (digit) (t & MASK); |
| 365 | t >>= SHIFT; |
| 366 | } |
| 367 | |
| 368 | v = long_normalize(v); |
| 369 | } |
| 370 | |
| 371 | return (PyObject *)v; |
| 372 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 373 | #endif |
| 374 | } |
| 375 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 376 | /* Get a C LONG_LONG int from a long int object. |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 377 | Returns -1 and sets an error condition if overflow occurs. */ |
| 378 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 379 | LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 380 | PyLong_AsLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 381 | { |
| 382 | #if SIZEOF_LONG_LONG == SIZEOF_LONG |
| 383 | /* In case the compiler is faking it. */ |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 384 | return (LONG_LONG)PyLong_AsLong( vv ); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 385 | #else |
| 386 | register PyLongObject *v; |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 387 | LONG_LONG x, prev; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 388 | int i, sign; |
| 389 | |
| 390 | if (vv == NULL || !PyLong_Check(vv)) { |
| 391 | PyErr_BadInternalCall(); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | v = (PyLongObject *)vv; |
| 396 | i = v->ob_size; |
| 397 | sign = 1; |
| 398 | x = 0; |
| 399 | |
| 400 | if (i < 0) { |
| 401 | sign = -1; |
| 402 | i = -(i); |
| 403 | } |
| 404 | |
| 405 | while (--i >= 0) { |
| 406 | prev = x; |
| 407 | x = (x << SHIFT) + v->ob_digit[i]; |
| 408 | if ((x >> SHIFT) != prev) { |
| 409 | PyErr_SetString(PyExc_OverflowError, |
| 410 | "long int too long to convert"); |
| 411 | return -1; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | return x * sign; |
| 416 | #endif |
| 417 | } |
| 418 | |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 419 | unsigned LONG_LONG |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 420 | PyLong_AsUnsignedLongLong(PyObject *vv) |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 421 | { |
| 422 | #if SIZEOF_LONG_LONG == 4 |
| 423 | /* In case the compiler is faking it. */ |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 424 | return (unsigned LONG_LONG)PyLong_AsUnsignedLong( vv ); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 425 | #else |
| 426 | register PyLongObject *v; |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 427 | unsigned LONG_LONG x, prev; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 428 | int i; |
| 429 | |
| 430 | if (vv == NULL || !PyLong_Check(vv)) { |
| 431 | PyErr_BadInternalCall(); |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 432 | return (unsigned LONG_LONG) -1; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | v = (PyLongObject *)vv; |
| 436 | i = v->ob_size; |
| 437 | x = 0; |
| 438 | |
| 439 | if (i < 0) { |
| 440 | PyErr_SetString(PyExc_OverflowError, |
| 441 | "can't convert negative value to unsigned long"); |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 442 | return (unsigned LONG_LONG) -1; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | while (--i >= 0) { |
| 446 | prev = x; |
| 447 | x = (x << SHIFT) + v->ob_digit[i]; |
| 448 | if ((x >> SHIFT) != prev) { |
| 449 | PyErr_SetString(PyExc_OverflowError, |
| 450 | "long int too long to convert"); |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 451 | return (unsigned LONG_LONG) -1; |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | return x; |
| 456 | #endif |
| 457 | } |
| 458 | #endif /* HAVE_LONG_LONG */ |
| 459 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 460 | /* Multiply by a single digit, ignoring the sign. */ |
| 461 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 462 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 463 | mul1(PyLongObject *a, wdigit n) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 464 | { |
| 465 | return muladd1(a, n, (digit)0); |
| 466 | } |
| 467 | |
| 468 | /* Multiply by a single digit and add a single digit, ignoring the sign. */ |
| 469 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 471 | muladd1(PyLongObject *a, wdigit n, wdigit extra) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 472 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 473 | int size_a = ABS(a->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 474 | PyLongObject *z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 475 | twodigits carry = extra; |
| 476 | int i; |
| 477 | |
| 478 | if (z == NULL) |
| 479 | return NULL; |
| 480 | for (i = 0; i < size_a; ++i) { |
| 481 | carry += (twodigits)a->ob_digit[i] * n; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 482 | z->ob_digit[i] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 483 | carry >>= SHIFT; |
| 484 | } |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 485 | z->ob_digit[i] = (digit) carry; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 486 | return long_normalize(z); |
| 487 | } |
| 488 | |
| 489 | /* Divide a long integer by a digit, returning both the quotient |
| 490 | (as function result) and the remainder (through *prem). |
| 491 | The sign of a is ignored; n should not be zero. */ |
| 492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 494 | divrem1(PyLongObject *a, wdigit n, digit *prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 495 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 496 | int size = ABS(a->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 497 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 498 | int i; |
| 499 | twodigits rem = 0; |
| 500 | |
| 501 | assert(n > 0 && n <= MASK); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | z = _PyLong_New(size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 503 | if (z == NULL) |
| 504 | return NULL; |
| 505 | for (i = size; --i >= 0; ) { |
| 506 | rem = (rem << SHIFT) + a->ob_digit[i]; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 507 | z->ob_digit[i] = (digit) (rem/n); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 508 | rem %= n; |
| 509 | } |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 510 | *prem = (digit) rem; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 511 | return long_normalize(z); |
| 512 | } |
| 513 | |
| 514 | /* 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] | 515 | Return a string object. |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 516 | 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] | 517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 519 | long_format(PyObject *aa, int base, int addL) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 520 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 521 | register PyLongObject *a = (PyLongObject *)aa; |
| 522 | PyStringObject *str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 523 | int i; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 524 | int size_a = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 525 | char *p; |
| 526 | int bits; |
| 527 | char sign = '\0'; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 528 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 529 | if (a == NULL || !PyLong_Check(a)) { |
| 530 | PyErr_BadInternalCall(); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 531 | return NULL; |
| 532 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 533 | assert(base >= 2 && base <= 36); |
| 534 | |
| 535 | /* Compute a rough upper bound for the length of the string */ |
| 536 | i = base; |
| 537 | bits = 0; |
| 538 | while (i > 1) { |
| 539 | ++bits; |
| 540 | i >>= 1; |
| 541 | } |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 542 | i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 543 | str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 544 | if (str == NULL) |
| 545 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 546 | p = PyString_AS_STRING(str) + i; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 547 | *p = '\0'; |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 548 | if (addL) |
| 549 | *--p = 'L'; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 550 | if (a->ob_size < 0) |
| 551 | sign = '-'; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 553 | if (a->ob_size == 0) { |
| 554 | *--p = '0'; |
| 555 | } |
| 556 | else if ((base & (base - 1)) == 0) { |
| 557 | /* JRH: special case for power-of-2 bases */ |
| 558 | twodigits temp = a->ob_digit[0]; |
| 559 | int bitsleft = SHIFT; |
| 560 | int rem; |
| 561 | int last = abs(a->ob_size); |
| 562 | int basebits = 1; |
| 563 | i = base; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 564 | while ((i >>= 1) > 1) |
| 565 | ++basebits; |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 566 | |
| 567 | i = 0; |
| 568 | for (;;) { |
| 569 | while (bitsleft >= basebits) { |
| 570 | if ((temp == 0) && (i >= last - 1)) break; |
| 571 | rem = temp & (base - 1); |
| 572 | if (rem < 10) |
| 573 | rem += '0'; |
| 574 | else |
| 575 | rem += 'A' - 10; |
| 576 | assert(p > PyString_AS_STRING(str)); |
| 577 | *--p = (char) rem; |
| 578 | bitsleft -= basebits; |
| 579 | temp >>= basebits; |
| 580 | } |
| 581 | if (++i >= last) { |
| 582 | if (temp == 0) break; |
| 583 | bitsleft = 99; |
| 584 | /* loop again to pick up final digits */ |
| 585 | } |
| 586 | else { |
| 587 | temp = (a->ob_digit[i] << bitsleft) | temp; |
| 588 | bitsleft += SHIFT; |
| 589 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 590 | } |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 591 | } |
| 592 | else { |
| 593 | Py_INCREF(a); |
| 594 | do { |
| 595 | digit rem; |
| 596 | PyLongObject *temp = divrem1(a, (digit)base, &rem); |
| 597 | if (temp == NULL) { |
| 598 | Py_DECREF(a); |
| 599 | Py_DECREF(str); |
| 600 | return NULL; |
| 601 | } |
| 602 | if (rem < 10) |
| 603 | rem += '0'; |
| 604 | else |
| 605 | rem += 'A'-10; |
| 606 | assert(p > PyString_AS_STRING(str)); |
| 607 | *--p = (char) rem; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | Py_DECREF(a); |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 609 | a = temp; |
| 610 | SIGCHECK({ |
| 611 | Py_DECREF(a); |
| 612 | Py_DECREF(str); |
| 613 | return NULL; |
| 614 | }) |
| 615 | } while (ABS(a->ob_size) != 0); |
| 616 | Py_DECREF(a); |
| 617 | } |
| 618 | |
Guido van Rossum | 2c47542 | 1992-08-14 15:13:07 +0000 | [diff] [blame] | 619 | if (base == 8) { |
| 620 | if (size_a != 0) |
| 621 | *--p = '0'; |
| 622 | } |
Guido van Rossum | 3d3037d | 1991-10-24 14:55:57 +0000 | [diff] [blame] | 623 | else if (base == 16) { |
| 624 | *--p = 'x'; |
| 625 | *--p = '0'; |
| 626 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 627 | else if (base != 10) { |
| 628 | *--p = '#'; |
| 629 | *--p = '0' + base%10; |
| 630 | if (base > 10) |
| 631 | *--p = '0' + base/10; |
| 632 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 633 | if (sign) |
| 634 | *--p = sign; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | if (p != PyString_AS_STRING(str)) { |
| 636 | char *q = PyString_AS_STRING(str); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 637 | assert(p > q); |
| 638 | do { |
| 639 | } while ((*q++ = *p++) != '\0'); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 640 | q--; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 641 | _PyString_Resize((PyObject **)&str, |
| 642 | (int) (q - PyString_AS_STRING(str))); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 643 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 644 | return (PyObject *)str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 647 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 648 | PyLong_FromString(char *str, char **pend, int base) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 649 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 650 | int sign = 1; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 651 | char *start, *orig_str = str; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 653 | |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 654 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 655 | PyErr_SetString(PyExc_ValueError, |
| 656 | "invalid base for long literal"); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 659 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 660 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 661 | if (*str == '+') |
| 662 | ++str; |
| 663 | else if (*str == '-') { |
| 664 | ++str; |
| 665 | sign = -1; |
| 666 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 667 | while (*str != '\0' && isspace(Py_CHARMASK(*str))) |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 668 | str++; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 669 | if (base == 0) { |
| 670 | if (str[0] != '0') |
| 671 | base = 10; |
| 672 | else if (str[1] == 'x' || str[1] == 'X') |
| 673 | base = 16; |
| 674 | else |
| 675 | base = 8; |
| 676 | } |
| 677 | if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) |
| 678 | str += 2; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 679 | z = _PyLong_New(0); |
Guido van Rossum | e676297 | 1998-06-22 03:54:46 +0000 | [diff] [blame] | 680 | start = str; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 681 | for ( ; z != NULL; ++str) { |
| 682 | int k = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 683 | PyLongObject *temp; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 684 | |
| 685 | if (*str <= '9') |
| 686 | k = *str - '0'; |
| 687 | else if (*str >= 'a') |
| 688 | k = *str - 'a' + 10; |
| 689 | else if (*str >= 'A') |
| 690 | k = *str - 'A' + 10; |
| 691 | if (k < 0 || k >= base) |
| 692 | break; |
| 693 | temp = muladd1(z, (digit)base, (digit)k); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 694 | Py_DECREF(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 695 | z = temp; |
| 696 | } |
Guido van Rossum | ac6a37a | 1998-08-04 15:04:06 +0000 | [diff] [blame] | 697 | if (z == NULL) |
| 698 | return NULL; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 699 | if (str == start) |
| 700 | goto onError; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 701 | if (sign < 0 && z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 702 | z->ob_size = -(z->ob_size); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 703 | if (*str == 'L' || *str == 'l') |
| 704 | str++; |
| 705 | while (*str && isspace(Py_CHARMASK(*str))) |
| 706 | str++; |
| 707 | if (*str != '\0') |
| 708 | goto onError; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 709 | if (pend) |
| 710 | *pend = str; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 711 | return (PyObject *) z; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 712 | |
| 713 | onError: |
| 714 | PyErr_Format(PyExc_ValueError, |
| 715 | "invalid literal for long(): %.200s", orig_str); |
| 716 | Py_XDECREF(z); |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 721 | PyLong_FromUnicode(Py_UNICODE *u, int length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 722 | { |
| 723 | char buffer[256]; |
| 724 | |
| 725 | if (length >= sizeof(buffer)) { |
| 726 | PyErr_SetString(PyExc_ValueError, |
| 727 | "long() literal too large to convert"); |
| 728 | return NULL; |
| 729 | } |
| 730 | if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) |
| 731 | return NULL; |
| 732 | |
| 733 | return PyLong_FromString(buffer, NULL, base); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 736 | /* forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 737 | static PyLongObject *x_divrem |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 738 | (PyLongObject *, PyLongObject *, PyLongObject **); |
| 739 | static PyObject *long_pos(PyLongObject *); |
| 740 | static int long_divrem(PyLongObject *, PyLongObject *, |
| 741 | PyLongObject **, PyLongObject **); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 742 | |
| 743 | /* Long division with remainder, top-level routine */ |
| 744 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 745 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 746 | long_divrem(PyLongObject *a, PyLongObject *b, |
| 747 | PyLongObject **pdiv, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 748 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 749 | 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] | 750 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 751 | |
| 752 | if (size_b == 0) { |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 753 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 754 | "long division or modulo"); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 755 | return -1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 756 | } |
| 757 | if (size_a < size_b || |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 758 | (size_a == size_b && |
| 759 | 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] | 760 | /* |a| < |b|. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 761 | *pdiv = _PyLong_New(0); |
| 762 | Py_INCREF(a); |
| 763 | *prem = (PyLongObject *) a; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 764 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 765 | } |
| 766 | if (size_b == 1) { |
| 767 | digit rem = 0; |
| 768 | z = divrem1(a, b->ob_digit[0], &rem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 769 | if (z == NULL) |
| 770 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 771 | *prem = (PyLongObject *) PyLong_FromLong((long)rem); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 772 | } |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 773 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 774 | z = x_divrem(a, b, prem); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 775 | if (z == NULL) |
| 776 | return -1; |
| 777 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 778 | /* Set the signs. |
| 779 | The quotient z has the sign of a*b; |
| 780 | the remainder r has the sign of a, |
| 781 | so a = b*z + r. */ |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 782 | if ((a->ob_size < 0) != (b->ob_size < 0)) |
| 783 | z->ob_size = -(z->ob_size); |
| 784 | if (a->ob_size < 0 && (*prem)->ob_size != 0) |
| 785 | (*prem)->ob_size = -((*prem)->ob_size); |
| 786 | *pdiv = z; |
| 787 | return 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 790 | /* Unsigned long division with remainder -- the algorithm */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 792 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 793 | x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 794 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 795 | 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] | 796 | 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] | 797 | PyLongObject *v = mul1(v1, d); |
| 798 | PyLongObject *w = mul1(w1, d); |
| 799 | PyLongObject *a; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 800 | int j, k; |
| 801 | |
| 802 | if (v == NULL || w == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 803 | Py_XDECREF(v); |
| 804 | Py_XDECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 805 | return NULL; |
| 806 | } |
| 807 | |
| 808 | 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] | 809 | 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] | 810 | 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] | 811 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 812 | size_v = ABS(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 813 | a = _PyLong_New(size_v - size_w + 1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 814 | |
| 815 | for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { |
| 816 | digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; |
| 817 | twodigits q; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 818 | stwodigits carry = 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 819 | int i; |
| 820 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 821 | SIGCHECK({ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 822 | Py_DECREF(a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 823 | a = NULL; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 824 | break; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 825 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 826 | if (vj == w->ob_digit[size_w-1]) |
| 827 | q = MASK; |
| 828 | else |
| 829 | q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) / |
| 830 | w->ob_digit[size_w-1]; |
| 831 | |
| 832 | while (w->ob_digit[size_w-2]*q > |
| 833 | (( |
| 834 | ((twodigits)vj << SHIFT) |
| 835 | + v->ob_digit[j-1] |
| 836 | - q*w->ob_digit[size_w-1] |
| 837 | ) << SHIFT) |
| 838 | + v->ob_digit[j-2]) |
| 839 | --q; |
| 840 | |
| 841 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 842 | twodigits z = w->ob_digit[i] * q; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 843 | digit zz = (digit) (z >> SHIFT); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 844 | carry += v->ob_digit[i+k] - z |
| 845 | + ((twodigits)zz << SHIFT); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 846 | v->ob_digit[i+k] = carry & MASK; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 847 | carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, |
| 848 | carry, SHIFT); |
| 849 | carry -= zz; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | if (i+k < size_v) { |
| 853 | carry += v->ob_digit[i+k]; |
| 854 | v->ob_digit[i+k] = 0; |
| 855 | } |
| 856 | |
| 857 | if (carry == 0) |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 858 | a->ob_digit[k] = (digit) q; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 859 | else { |
| 860 | assert(carry == -1); |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 861 | a->ob_digit[k] = (digit) q-1; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 862 | carry = 0; |
| 863 | for (i = 0; i < size_w && i+k < size_v; ++i) { |
| 864 | carry += v->ob_digit[i+k] + w->ob_digit[i]; |
| 865 | v->ob_digit[i+k] = carry & MASK; |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 866 | carry = Py_ARITHMETIC_RIGHT_SHIFT( |
| 867 | BASE_TWODIGITS_TYPE, |
| 868 | carry, SHIFT); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } /* for j, k */ |
| 872 | |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 873 | if (a == NULL) |
| 874 | *prem = NULL; |
| 875 | else { |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 876 | a = long_normalize(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 877 | *prem = divrem1(v, d, &d); |
| 878 | /* d receives the (unused) remainder */ |
| 879 | if (*prem == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 880 | Py_DECREF(a); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 881 | a = NULL; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 882 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 883 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 884 | Py_DECREF(v); |
| 885 | Py_DECREF(w); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 886 | return a; |
| 887 | } |
| 888 | |
| 889 | /* Methods */ |
| 890 | |
| 891 | static void |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 892 | long_dealloc(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 893 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 894 | PyObject_DEL(v); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 897 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 898 | long_repr(PyObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 899 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 900 | return long_format(v, 10, 1); |
| 901 | } |
| 902 | |
| 903 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 904 | long_str(PyObject *v) |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 905 | { |
| 906 | return long_format(v, 10, 0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 910 | long_compare(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 911 | { |
| 912 | int sign; |
| 913 | |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 914 | if (a->ob_size != b->ob_size) { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 915 | if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 916 | sign = 0; |
| 917 | else |
| 918 | sign = a->ob_size - b->ob_size; |
| 919 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 920 | else { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 921 | int i = ABS(a->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 922 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 923 | ; |
| 924 | if (i < 0) |
| 925 | sign = 0; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 926 | else { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 927 | sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; |
Guido van Rossum | 0b0db8e | 1993-01-21 16:07:51 +0000 | [diff] [blame] | 928 | if (a->ob_size < 0) |
| 929 | sign = -sign; |
| 930 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 931 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 932 | return sign < 0 ? -1 : sign > 0 ? 1 : 0; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 935 | static long |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 936 | long_hash(PyLongObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 937 | { |
| 938 | long x; |
| 939 | int i, sign; |
| 940 | |
| 941 | /* This is designed so that Python ints and longs with the |
| 942 | same value hash to the same value, otherwise comparisons |
| 943 | of mapping keys will turn out weird */ |
| 944 | i = v->ob_size; |
| 945 | sign = 1; |
| 946 | x = 0; |
| 947 | if (i < 0) { |
| 948 | sign = -1; |
| 949 | i = -(i); |
| 950 | } |
| 951 | while (--i >= 0) { |
| 952 | /* Force a 32-bit circular shift */ |
| 953 | x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); |
| 954 | x += v->ob_digit[i]; |
| 955 | } |
| 956 | x = x * sign; |
| 957 | if (x == -1) |
| 958 | x = -2; |
| 959 | return x; |
| 960 | } |
| 961 | |
| 962 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 963 | /* Add the absolute values of two long integers. */ |
| 964 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 965 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 966 | x_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 967 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 968 | 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] | 969 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 970 | int i; |
| 971 | digit carry = 0; |
| 972 | |
| 973 | /* Ensure a is the larger of the two: */ |
| 974 | if (size_a < size_b) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 975 | { PyLongObject *temp = a; a = b; b = temp; } |
| 976 | { int size_temp = size_a; |
| 977 | size_a = size_b; |
| 978 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 979 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 980 | z = _PyLong_New(size_a+1); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 981 | if (z == NULL) |
| 982 | return NULL; |
| 983 | for (i = 0; i < size_b; ++i) { |
| 984 | carry += a->ob_digit[i] + b->ob_digit[i]; |
| 985 | z->ob_digit[i] = carry & MASK; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 986 | carry >>= SHIFT; |
| 987 | } |
| 988 | for (; i < size_a; ++i) { |
| 989 | carry += a->ob_digit[i]; |
| 990 | z->ob_digit[i] = carry & MASK; |
| 991 | carry >>= SHIFT; |
| 992 | } |
| 993 | z->ob_digit[i] = carry; |
| 994 | return long_normalize(z); |
| 995 | } |
| 996 | |
| 997 | /* Subtract the absolute values of two integers. */ |
| 998 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 999 | static PyLongObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1000 | x_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1001 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1002 | 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] | 1003 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1004 | int i; |
| 1005 | int sign = 1; |
| 1006 | digit borrow = 0; |
| 1007 | |
| 1008 | /* Ensure a is the larger of the two: */ |
| 1009 | if (size_a < size_b) { |
| 1010 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1011 | { PyLongObject *temp = a; a = b; b = temp; } |
| 1012 | { int size_temp = size_a; |
| 1013 | size_a = size_b; |
| 1014 | size_b = size_temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1015 | } |
| 1016 | else if (size_a == size_b) { |
| 1017 | /* Find highest digit where a and b differ: */ |
| 1018 | i = size_a; |
| 1019 | while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) |
| 1020 | ; |
| 1021 | if (i < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1022 | return _PyLong_New(0); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1023 | if (a->ob_digit[i] < b->ob_digit[i]) { |
| 1024 | sign = -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1025 | { PyLongObject *temp = a; a = b; b = temp; } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1026 | } |
| 1027 | size_a = size_b = i+1; |
| 1028 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1029 | z = _PyLong_New(size_a); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1030 | if (z == NULL) |
| 1031 | return NULL; |
| 1032 | for (i = 0; i < size_b; ++i) { |
| 1033 | /* The following assumes unsigned arithmetic |
| 1034 | works module 2**N for some N>SHIFT. */ |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1035 | borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1036 | z->ob_digit[i] = borrow & MASK; |
| 1037 | borrow >>= SHIFT; |
| 1038 | borrow &= 1; /* Keep only one sign bit */ |
| 1039 | } |
| 1040 | for (; i < size_a; ++i) { |
| 1041 | borrow = a->ob_digit[i] - borrow; |
| 1042 | z->ob_digit[i] = borrow & MASK; |
| 1043 | borrow >>= SHIFT; |
Tim Peters | 43f04a3 | 2000-07-08 02:26:47 +0000 | [diff] [blame] | 1044 | borrow &= 1; /* Keep only one sign bit */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1045 | } |
| 1046 | assert(borrow == 0); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1047 | if (sign < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1048 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1049 | return long_normalize(z); |
| 1050 | } |
| 1051 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1052 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1053 | long_add(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1054 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1055 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1056 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1057 | if (a->ob_size < 0) { |
| 1058 | if (b->ob_size < 0) { |
| 1059 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1060 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1061 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1062 | } |
| 1063 | else |
| 1064 | z = x_sub(b, a); |
| 1065 | } |
| 1066 | else { |
| 1067 | if (b->ob_size < 0) |
| 1068 | z = x_sub(a, b); |
| 1069 | else |
| 1070 | z = x_add(a, b); |
| 1071 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1072 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1076 | long_sub(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1077 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1080 | if (a->ob_size < 0) { |
| 1081 | if (b->ob_size < 0) |
| 1082 | z = x_sub(a, b); |
| 1083 | else |
| 1084 | z = x_add(a, b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1085 | if (z != NULL && z->ob_size != 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1086 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1087 | } |
| 1088 | else { |
| 1089 | if (b->ob_size < 0) |
| 1090 | z = x_add(a, b); |
| 1091 | else |
| 1092 | z = x_sub(a, b); |
| 1093 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1094 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1097 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1098 | long_mul(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1099 | { |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1100 | int size_a; |
| 1101 | int size_b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1102 | PyLongObject *z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1103 | int i; |
| 1104 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1105 | size_a = ABS(a->ob_size); |
| 1106 | size_b = ABS(b->ob_size); |
Guido van Rossum | ba71a24 | 2000-04-10 17:31:58 +0000 | [diff] [blame] | 1107 | if (size_a > size_b) { |
| 1108 | /* we are faster with the small object on the left */ |
| 1109 | int hold_sa = size_a; |
| 1110 | PyLongObject *hold_a = a; |
| 1111 | size_a = size_b; |
| 1112 | size_b = hold_sa; |
| 1113 | a = b; |
| 1114 | b = hold_a; |
| 1115 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1116 | z = _PyLong_New(size_a + size_b); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1117 | if (z == NULL) |
| 1118 | return NULL; |
| 1119 | for (i = 0; i < z->ob_size; ++i) |
| 1120 | z->ob_digit[i] = 0; |
| 1121 | for (i = 0; i < size_a; ++i) { |
| 1122 | twodigits carry = 0; |
| 1123 | twodigits f = a->ob_digit[i]; |
| 1124 | int j; |
| 1125 | |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1126 | SIGCHECK({ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1127 | Py_DECREF(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1128 | return NULL; |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1129 | }) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1130 | for (j = 0; j < size_b; ++j) { |
| 1131 | carry += z->ob_digit[i+j] + b->ob_digit[j] * f; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1132 | z->ob_digit[i+j] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1133 | carry >>= SHIFT; |
| 1134 | } |
| 1135 | for (; carry != 0; ++j) { |
| 1136 | assert(i+j < z->ob_size); |
| 1137 | carry += z->ob_digit[i+j]; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 1138 | z->ob_digit[i+j] = (digit) (carry & MASK); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1139 | carry >>= SHIFT; |
| 1140 | } |
| 1141 | } |
| 1142 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1143 | z->ob_size = -(z->ob_size); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1144 | if (b->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1145 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1146 | return (PyObject *) long_normalize(z); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1149 | /* The / and % operators are now defined in terms of divmod(). |
| 1150 | The expression a mod b has the value a - b*floor(a/b). |
| 1151 | The long_divrem function gives the remainder after division of |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1152 | |a| by |b|, with the sign of a. This is also expressed |
| 1153 | as a - b*trunc(a/b), if trunc truncates towards zero. |
| 1154 | Some examples: |
| 1155 | a b a rem b a mod b |
| 1156 | 13 10 3 3 |
| 1157 | -13 10 -3 7 |
| 1158 | 13 -10 3 -7 |
| 1159 | -13 -10 -3 -3 |
| 1160 | 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] | 1161 | have different signs. We then subtract one from the 'div' |
| 1162 | part of the outcome to keep the invariant intact. */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1163 | |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1164 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1165 | l_divmod(PyLongObject *v, PyLongObject *w, |
| 1166 | PyLongObject **pdiv, PyLongObject **pmod) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1167 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1168 | PyLongObject *div, *mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1169 | |
| 1170 | if (long_divrem(v, w, &div, &mod) < 0) |
| 1171 | return -1; |
Guido van Rossum | 472c04f | 1996-12-05 21:57:21 +0000 | [diff] [blame] | 1172 | if ((mod->ob_size < 0 && w->ob_size > 0) || |
| 1173 | (mod->ob_size > 0 && w->ob_size < 0)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1174 | PyLongObject *temp; |
| 1175 | PyLongObject *one; |
| 1176 | temp = (PyLongObject *) long_add(mod, w); |
| 1177 | Py_DECREF(mod); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1178 | mod = temp; |
| 1179 | if (mod == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1180 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1181 | return -1; |
| 1182 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1183 | one = (PyLongObject *) PyLong_FromLong(1L); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1184 | if (one == NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1185 | (temp = (PyLongObject *) long_sub(div, one)) == NULL) { |
| 1186 | Py_DECREF(mod); |
| 1187 | Py_DECREF(div); |
| 1188 | Py_XDECREF(one); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1189 | return -1; |
| 1190 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1191 | Py_DECREF(one); |
| 1192 | Py_DECREF(div); |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1193 | div = temp; |
| 1194 | } |
| 1195 | *pdiv = div; |
| 1196 | *pmod = mod; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1200 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1201 | long_div(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1202 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1203 | PyLongObject *div, *mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1204 | if (l_divmod(v, w, &div, &mod) < 0) |
| 1205 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1206 | Py_DECREF(mod); |
| 1207 | return (PyObject *)div; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1210 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1211 | long_mod(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1212 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1213 | PyLongObject *div, *mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1214 | if (l_divmod(v, w, &div, &mod) < 0) |
| 1215 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1216 | Py_DECREF(div); |
| 1217 | return (PyObject *)mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1220 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1221 | long_divmod(PyLongObject *v, PyLongObject *w) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1222 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1223 | PyObject *z; |
| 1224 | PyLongObject *div, *mod; |
Guido van Rossum | e32e014 | 1992-01-19 16:31:05 +0000 | [diff] [blame] | 1225 | if (l_divmod(v, w, &div, &mod) < 0) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1226 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1227 | z = PyTuple_New(2); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1228 | if (z != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1229 | PyTuple_SetItem(z, 0, (PyObject *) div); |
| 1230 | PyTuple_SetItem(z, 1, (PyObject *) mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1231 | } |
| 1232 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1233 | Py_DECREF(div); |
| 1234 | Py_DECREF(mod); |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1235 | } |
| 1236 | return z; |
| 1237 | } |
| 1238 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1239 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1240 | long_pow(PyLongObject *a, PyLongObject *b, PyLongObject *c) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1241 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1242 | PyLongObject *z, *div, *mod; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1243 | int size_b, i; |
| 1244 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1245 | size_b = b->ob_size; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1246 | if (size_b < 0) { |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame^] | 1247 | if (a->ob_size) |
| 1248 | PyErr_SetString(PyExc_ValueError, |
| 1249 | "long integer to a negative power"); |
| 1250 | else |
| 1251 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 1252 | "zero to a negative power"); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1253 | return NULL; |
| 1254 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1255 | z = (PyLongObject *)PyLong_FromLong(1L); |
| 1256 | Py_INCREF(a); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1257 | for (i = 0; i < size_b; ++i) { |
| 1258 | digit bi = b->ob_digit[i]; |
| 1259 | int j; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1260 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1261 | for (j = 0; j < SHIFT; ++j) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1262 | PyLongObject *temp; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1263 | |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1264 | if (bi & 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1265 | temp = (PyLongObject *)long_mul(z, a); |
| 1266 | Py_DECREF(z); |
| 1267 | if ((PyObject*)c!=Py_None && temp!=NULL) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1268 | if (l_divmod(temp,c,&div,&mod) < 0) { |
| 1269 | Py_DECREF(temp); |
| 1270 | z = NULL; |
| 1271 | goto error; |
| 1272 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1273 | Py_XDECREF(div); |
| 1274 | Py_DECREF(temp); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1275 | temp = mod; |
| 1276 | } |
| 1277 | z = temp; |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1278 | if (z == NULL) |
| 1279 | break; |
| 1280 | } |
| 1281 | bi >>= 1; |
| 1282 | if (bi == 0 && i+1 == size_b) |
| 1283 | break; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1284 | temp = (PyLongObject *)long_mul(a, a); |
| 1285 | Py_DECREF(a); |
| 1286 | if ((PyObject*)c!=Py_None && temp!=NULL) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1287 | if (l_divmod(temp, c, &div, &mod) < 0) { |
| 1288 | Py_DECREF(temp); |
| 1289 | z = NULL; |
| 1290 | goto error; |
| 1291 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1292 | Py_XDECREF(div); |
| 1293 | Py_DECREF(temp); |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1294 | temp = mod; |
| 1295 | } |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1296 | a = temp; |
| 1297 | if (a == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1298 | Py_DECREF(z); |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1299 | z = NULL; |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
Guido van Rossum | c206c76 | 1995-01-10 15:23:19 +0000 | [diff] [blame] | 1303 | if (a == NULL || z == NULL) |
Guido van Rossum | c7ec9c9 | 1991-05-28 21:58:16 +0000 | [diff] [blame] | 1304 | break; |
| 1305 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1306 | Py_XDECREF(a); |
| 1307 | if ((PyObject*)c!=Py_None && z!=NULL) { |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1308 | if (l_divmod(z, c, &div, &mod) < 0) { |
| 1309 | Py_DECREF(z); |
| 1310 | z = NULL; |
| 1311 | } |
| 1312 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1313 | Py_XDECREF(div); |
| 1314 | Py_DECREF(z); |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1315 | z = mod; |
| 1316 | } |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1317 | } |
Guido van Rossum | 2c7b8fe | 1999-10-11 22:34:41 +0000 | [diff] [blame] | 1318 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1319 | return (PyObject *)z; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1322 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1323 | long_invert(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1324 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1325 | /* Implement ~x as -(x+1) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1326 | PyLongObject *x; |
| 1327 | PyLongObject *w; |
| 1328 | w = (PyLongObject *)PyLong_FromLong(1L); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1329 | if (w == NULL) |
| 1330 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1331 | x = (PyLongObject *) long_add(v, w); |
| 1332 | Py_DECREF(w); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1333 | if (x == NULL) |
| 1334 | return NULL; |
| 1335 | if (x->ob_size != 0) |
| 1336 | x->ob_size = -(x->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1337 | return (PyObject *)x; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1340 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1341 | long_pos(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1342 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1343 | Py_INCREF(v); |
| 1344 | return (PyObject *)v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1347 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1348 | long_neg(PyLongObject *v) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1349 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1350 | PyLongObject *z; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1351 | int i, n; |
| 1352 | n = ABS(v->ob_size); |
| 1353 | if (n == 0) { |
| 1354 | /* -0 == 0 */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1355 | Py_INCREF(v); |
| 1356 | return (PyObject *) v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1357 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1358 | z = _PyLong_New(ABS(n)); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1359 | if (z == NULL) |
| 1360 | return NULL; |
| 1361 | for (i = 0; i < n; i++) |
| 1362 | z->ob_digit[i] = v->ob_digit[i]; |
| 1363 | z->ob_size = -(v->ob_size); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1364 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1367 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1368 | long_abs(PyLongObject *v) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1369 | { |
| 1370 | if (v->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1371 | return long_neg(v); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1372 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1373 | Py_INCREF(v); |
| 1374 | return (PyObject *)v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1375 | } |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1378 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1379 | long_nonzero(PyLongObject *v) |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1380 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1381 | return ABS(v->ob_size) != 0; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1384 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1385 | long_rshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1386 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1387 | PyLongObject *z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1388 | long shiftby; |
| 1389 | int newsize, wordshift, loshift, hishift, i, j; |
| 1390 | digit lomask, himask; |
| 1391 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1392 | if (a->ob_size < 0) { |
| 1393 | /* Right shifting negative numbers is harder */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1394 | PyLongObject *a1, *a2, *a3; |
| 1395 | a1 = (PyLongObject *) long_invert(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1396 | if (a1 == NULL) return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1397 | a2 = (PyLongObject *) long_rshift(a1, b); |
| 1398 | Py_DECREF(a1); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1399 | if (a2 == NULL) return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1400 | a3 = (PyLongObject *) long_invert(a2); |
| 1401 | Py_DECREF(a2); |
| 1402 | return (PyObject *) a3; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1403 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1404 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1405 | shiftby = PyLong_AsLong((PyObject *)b); |
| 1406 | if (shiftby == -1L && PyErr_Occurred()) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1407 | return NULL; |
| 1408 | if (shiftby < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1409 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1410 | return NULL; |
| 1411 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1412 | wordshift = shiftby / SHIFT; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1413 | newsize = ABS(a->ob_size) - wordshift; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1414 | if (newsize <= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1415 | z = _PyLong_New(0); |
| 1416 | return (PyObject *)z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1417 | } |
| 1418 | loshift = shiftby % SHIFT; |
| 1419 | hishift = SHIFT - loshift; |
| 1420 | lomask = ((digit)1 << hishift) - 1; |
| 1421 | himask = MASK ^ lomask; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1422 | z = _PyLong_New(newsize); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1423 | if (z == NULL) |
| 1424 | return NULL; |
| 1425 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1426 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1427 | for (i = 0, j = wordshift; i < newsize; i++, j++) { |
| 1428 | z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; |
| 1429 | if (i+1 < newsize) |
| 1430 | z->ob_digit[i] |= |
| 1431 | (a->ob_digit[j+1] << hishift) & himask; |
| 1432 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1433 | return (PyObject *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1436 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1437 | long_lshift(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1438 | { |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1439 | /* This version due to Tim Peters */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1440 | PyLongObject *z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1441 | long shiftby; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1442 | int oldsize, newsize, wordshift, remshift, i, j; |
| 1443 | twodigits accum; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1444 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1445 | shiftby = PyLong_AsLong((PyObject *)b); |
| 1446 | if (shiftby == -1L && PyErr_Occurred()) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | if (shiftby < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1449 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1450 | return NULL; |
| 1451 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1452 | if ((long)(int)shiftby != shiftby) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1453 | PyErr_SetString(PyExc_ValueError, |
| 1454 | "outrageous left shift count"); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1455 | return NULL; |
| 1456 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1457 | /* wordshift, remshift = divmod(shiftby, SHIFT) */ |
| 1458 | wordshift = (int)shiftby / SHIFT; |
| 1459 | remshift = (int)shiftby - wordshift * SHIFT; |
| 1460 | |
| 1461 | oldsize = ABS(a->ob_size); |
| 1462 | newsize = oldsize + wordshift; |
| 1463 | if (remshift) |
| 1464 | ++newsize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1465 | z = _PyLong_New(newsize); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1466 | if (z == NULL) |
| 1467 | return NULL; |
| 1468 | if (a->ob_size < 0) |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1469 | z->ob_size = -(z->ob_size); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1470 | for (i = 0; i < wordshift; i++) |
| 1471 | z->ob_digit[i] = 0; |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1472 | accum = 0; |
| 1473 | for (i = wordshift, j = 0; j < oldsize; i++, j++) { |
| 1474 | accum |= a->ob_digit[j] << remshift; |
| 1475 | z->ob_digit[i] = (digit)(accum & MASK); |
| 1476 | accum >>= SHIFT; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1477 | } |
Guido van Rossum | f2e499b | 1997-03-16 00:37:59 +0000 | [diff] [blame] | 1478 | if (remshift) |
| 1479 | z->ob_digit[newsize-1] = (digit)accum; |
| 1480 | else |
| 1481 | assert(!accum); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1482 | return (PyObject *) long_normalize(z); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Bitwise and/xor/or operations */ |
| 1487 | |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1488 | #define MAX(x, y) ((x) < (y) ? (y) : (x)) |
| 1489 | #define MIN(x, y) ((x) > (y) ? (y) : (x)) |
| 1490 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1491 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1492 | long_bitwise(PyLongObject *a, |
| 1493 | int op, /* '&', '|', '^' */ |
| 1494 | PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1495 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1496 | digit maska, maskb; /* 0 or MASK */ |
| 1497 | int negz; |
| 1498 | int size_a, size_b, size_z; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1499 | PyLongObject *z; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1500 | int i; |
Guido van Rossum | 8b27d92 | 1992-03-27 17:27:05 +0000 | [diff] [blame] | 1501 | digit diga, digb; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1502 | PyObject *v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1503 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1504 | if (a->ob_size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1505 | a = (PyLongObject *) long_invert(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1506 | maska = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1507 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1508 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1509 | Py_INCREF(a); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1510 | maska = 0; |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1511 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1512 | if (b->ob_size < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1513 | b = (PyLongObject *) long_invert(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1514 | maskb = MASK; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1515 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1516 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1517 | Py_INCREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1518 | maskb = 0; |
| 1519 | } |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1520 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1521 | negz = 0; |
| 1522 | switch (op) { |
| 1523 | case '^': |
| 1524 | if (maska != maskb) { |
| 1525 | maska ^= MASK; |
| 1526 | negz = -1; |
| 1527 | } |
| 1528 | break; |
Guido van Rossum | eb1fafc | 1994-08-29 12:47:19 +0000 | [diff] [blame] | 1529 | case '&': |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1530 | if (maska && maskb) { |
| 1531 | op = '|'; |
| 1532 | maska ^= MASK; |
| 1533 | maskb ^= MASK; |
| 1534 | negz = -1; |
| 1535 | } |
| 1536 | break; |
| 1537 | case '|': |
| 1538 | if (maska || maskb) { |
| 1539 | op = '&'; |
| 1540 | maska ^= MASK; |
| 1541 | maskb ^= MASK; |
| 1542 | negz = -1; |
| 1543 | } |
| 1544 | break; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1545 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1546 | |
Guido van Rossum | bd3a527 | 1998-08-11 15:04:47 +0000 | [diff] [blame] | 1547 | /* JRH: The original logic here was to allocate the result value (z) |
| 1548 | as the longer of the two operands. However, there are some cases |
| 1549 | where the result is guaranteed to be shorter than that: AND of two |
| 1550 | positives, OR of two negatives: use the shorter number. AND with |
| 1551 | mixed signs: use the positive number. OR with mixed signs: use the |
| 1552 | negative number. After the transformations above, op will be '&' |
| 1553 | iff one of these cases applies, and mask will be non-0 for operands |
| 1554 | whose length should be ignored. |
| 1555 | */ |
| 1556 | |
| 1557 | size_a = a->ob_size; |
| 1558 | size_b = b->ob_size; |
| 1559 | size_z = op == '&' |
| 1560 | ? (maska |
| 1561 | ? size_b |
| 1562 | : (maskb ? size_a : MIN(size_a, size_b))) |
| 1563 | : MAX(size_a, size_b); |
| 1564 | z = _PyLong_New(size_z); |
| 1565 | if (a == NULL || b == NULL || z == NULL) { |
| 1566 | Py_XDECREF(a); |
| 1567 | Py_XDECREF(b); |
| 1568 | Py_XDECREF(z); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1572 | for (i = 0; i < size_z; ++i) { |
| 1573 | diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; |
| 1574 | digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; |
| 1575 | switch (op) { |
| 1576 | case '&': z->ob_digit[i] = diga & digb; break; |
| 1577 | case '|': z->ob_digit[i] = diga | digb; break; |
| 1578 | case '^': z->ob_digit[i] = diga ^ digb; break; |
| 1579 | } |
Guido van Rossum | afbb8db | 1991-12-31 13:14:13 +0000 | [diff] [blame] | 1580 | } |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1582 | Py_DECREF(a); |
| 1583 | Py_DECREF(b); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1584 | z = long_normalize(z); |
| 1585 | if (negz == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1586 | return (PyObject *) z; |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1587 | v = long_invert(z); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1588 | Py_DECREF(z); |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1589 | return v; |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1592 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1593 | long_and(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1594 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1595 | return long_bitwise(a, '&', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1598 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1599 | long_xor(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1600 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1601 | return long_bitwise(a, '^', b); |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1604 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1605 | long_or(PyLongObject *a, PyLongObject *b) |
Guido van Rossum | c6913e7 | 1991-11-19 20:26:46 +0000 | [diff] [blame] | 1606 | { |
Guido van Rossum | 4c260ff | 1992-01-14 18:36:43 +0000 | [diff] [blame] | 1607 | return long_bitwise(a, '|', b); |
Guido van Rossum | 23d6f0e | 1991-05-14 12:06:49 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 1610 | static int |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1611 | long_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1612 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1613 | if (PyInt_Check(*pw)) { |
| 1614 | *pw = PyLong_FromLong(PyInt_AsLong(*pw)); |
| 1615 | Py_INCREF(*pv); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | return 1; /* Can't do it */ |
| 1619 | } |
| 1620 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1621 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1622 | long_int(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1623 | { |
| 1624 | long x; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1625 | x = PyLong_AsLong(v); |
| 1626 | if (PyErr_Occurred()) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1627 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1628 | return PyInt_FromLong(x); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1631 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1632 | long_long(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1633 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1634 | Py_INCREF(v); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1635 | return v; |
| 1636 | } |
| 1637 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1638 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1639 | long_float(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1640 | { |
Guido van Rossum | 09e6ad0 | 1997-02-14 22:54:21 +0000 | [diff] [blame] | 1641 | double result; |
| 1642 | PyFPE_START_PROTECT("long_float", return 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1643 | result = PyLong_AsDouble(v); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1644 | PyFPE_END_PROTECT(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1645 | return PyFloat_FromDouble(result); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1648 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1649 | long_oct(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1650 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 1651 | return long_format(v, 8, 1); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1654 | static PyObject * |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1655 | long_hex(PyObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1656 | { |
Fred Drake | 121ee27 | 1999-12-23 15:41:28 +0000 | [diff] [blame] | 1657 | return long_format(v, 16, 1); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1660 | static PyNumberMethods long_as_number = { |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1661 | (binaryfunc) long_add, /*nb_add*/ |
| 1662 | (binaryfunc) long_sub, /*nb_subtract*/ |
| 1663 | (binaryfunc) long_mul, /*nb_multiply*/ |
| 1664 | (binaryfunc) long_div, /*nb_divide*/ |
| 1665 | (binaryfunc) long_mod, /*nb_remainder*/ |
| 1666 | (binaryfunc) long_divmod, /*nb_divmod*/ |
| 1667 | (ternaryfunc) long_pow, /*nb_power*/ |
| 1668 | (unaryfunc) long_neg, /*nb_negative*/ |
| 1669 | (unaryfunc) long_pos, /*tp_positive*/ |
| 1670 | (unaryfunc) long_abs, /*tp_absolute*/ |
| 1671 | (inquiry) long_nonzero, /*tp_nonzero*/ |
| 1672 | (unaryfunc) long_invert, /*nb_invert*/ |
| 1673 | (binaryfunc) long_lshift, /*nb_lshift*/ |
| 1674 | (binaryfunc) long_rshift, /*nb_rshift*/ |
| 1675 | (binaryfunc) long_and, /*nb_and*/ |
| 1676 | (binaryfunc) long_xor, /*nb_xor*/ |
| 1677 | (binaryfunc) long_or, /*nb_or*/ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 1678 | (coercion) long_coerce, /*nb_coerce*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1679 | (unaryfunc) long_int, /*nb_int*/ |
| 1680 | (unaryfunc) long_long, /*nb_long*/ |
| 1681 | (unaryfunc) long_float, /*nb_float*/ |
| 1682 | (unaryfunc) long_oct, /*nb_oct*/ |
| 1683 | (unaryfunc) long_hex, /*nb_hex*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1684 | }; |
| 1685 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1686 | PyTypeObject PyLong_Type = { |
| 1687 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1688 | 0, |
| 1689 | "long int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1690 | sizeof(PyLongObject) - sizeof(digit), |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1691 | sizeof(digit), |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1692 | (destructor)long_dealloc, /*tp_dealloc*/ |
| 1693 | 0, /*tp_print*/ |
| 1694 | 0, /*tp_getattr*/ |
| 1695 | 0, /*tp_setattr*/ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 1696 | (cmpfunc)long_compare, /*tp_compare*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1697 | (reprfunc)long_repr, /*tp_repr*/ |
| 1698 | &long_as_number, /*tp_as_number*/ |
| 1699 | 0, /*tp_as_sequence*/ |
| 1700 | 0, /*tp_as_mapping*/ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 1701 | (hashfunc)long_hash, /*tp_hash*/ |
Tim Peters | 9f688bf | 2000-07-07 15:53:28 +0000 | [diff] [blame] | 1702 | 0, /*tp_call*/ |
| 1703 | (reprfunc)long_str, /*tp_str*/ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 1704 | }; |