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