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