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