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