Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 11 | /* MPZ module */ |
| 12 | |
| 13 | /* This module provides an interface to an alternate Multi-Precision |
| 14 | library, GNU MP in this case */ |
| 15 | |
| 16 | /* XXX note: everywhere where mpz_size is called, |
| 17 | sizeof (limb) == sizeof (long) has been assumed. */ |
| 18 | |
| 19 | |
| 20 | /* MPZ objects */ |
| 21 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 22 | #include "Python.h" |
Guido van Rossum | a597dde | 1995-01-10 20:56:29 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 24 | #include <assert.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | #include <sys/types.h> /* For size_t */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | ** These are the cpp-flags used in this file... |
| 29 | ** |
| 30 | ** |
| 31 | ** MPZ_MDIV_BUG works around the mpz_m{div,mod,...} routines. |
| 32 | ** This bug has been fixed in a later release of |
| 33 | ** GMP. |
| 34 | ** |
| 35 | ** MPZ_GET_STR_BUG mpz_get_str corrupts memory, seems to be fixed |
| 36 | ** in a later release |
| 37 | ** |
| 38 | ** MPZ_DEBUG generates a bunch of diagnostic messages |
| 39 | ** |
| 40 | ** MPZ_SPARE_MALLOC if set, results in extra code that tries to |
| 41 | ** minimize the creation of extra objects. |
| 42 | ** |
| 43 | ** MPZ_TEST_DIV extra diagnostic output on stderr, when division |
| 44 | ** routines are involved |
| 45 | ** |
| 46 | ** MPZ_LIB_DOES_CHECKING if set, assumes that mpz library doesn't call |
| 47 | ** alloca with arg < 0 (when casted to a signed |
| 48 | ** integral type). |
| 49 | ** |
| 50 | ** MPZ_CONVERSIONS_AS_METHODS if set, presents the conversions as |
| 51 | ** methods. e.g., `mpz(5).long() == 5L' |
| 52 | ** Later, Guido provided an interface to the |
| 53 | ** standard functions. So this flag has no been |
| 54 | ** cleared, and `long(mpz(5)) == 5L' |
| 55 | ** |
| 56 | ** MP_TEST_ALLOC If set, you would discover why MPZ_GET_STR_BUG |
| 57 | ** is needed |
| 58 | ** |
| 59 | ** MAKEDUMMYINT Must be set if dynamic linking will be used |
| 60 | */ |
| 61 | |
| 62 | |
| 63 | /* |
| 64 | ** IMHO, mpz_m{div,mod,divmod}() do the wrong things when the denominator < 0 |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 65 | ** This has been fixed with gmp release 2.0 |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 66 | */ |
| 67 | /*#define MPZ_MDIV_BUG fixed the (for me) nexessary parts in libgmp.a */ |
| 68 | /* |
| 69 | ** IMO, mpz_get_str() assumes a bit too large target space, if he doesn't |
| 70 | ** allocate it himself |
| 71 | */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 72 | |
| 73 | #include "gmp.h" |
Guido van Rossum | e7ef74d | 2000-02-24 15:26:30 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 57e846f | 1997-08-17 19:08:33 +0000 | [diff] [blame] | 75 | #if __GNU_MP__ + 0 == 2 |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 76 | #define GMP2 |
Andrew M. Kuchling | b6f6e95 | 2000-02-25 22:23:31 +0000 | [diff] [blame] | 77 | #define BITS_PER_MP_LIMB mp_bits_per_limb |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 78 | #else |
| 79 | #define MPZ_GET_STR_BUG |
Andrew M. Kuchling | b6f6e95 | 2000-02-25 22:23:31 +0000 | [diff] [blame] | 80 | #include "gmp-mparam.h" |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 83 | typedef struct { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 84 | PyObject_HEAD |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 85 | MP_INT mpz; /* the actual number */ |
| 86 | } mpzobject; |
| 87 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 88 | staticforward PyTypeObject MPZtype; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 89 | |
| 90 | #define is_mpzobject(v) ((v)->ob_type == &MPZtype) |
| 91 | |
| 92 | static const char initialiser_name[] = "mpz"; |
| 93 | |
| 94 | /* #define MPZ_DEBUG */ |
| 95 | |
| 96 | static mpzobject * |
| 97 | newmpzobject() |
| 98 | { |
| 99 | mpzobject *mpzp; |
| 100 | |
| 101 | |
| 102 | #ifdef MPZ_DEBUG |
| 103 | fputs( "mpz_object() called...\n", stderr ); |
| 104 | #endif /* def MPZ_DEBUG */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 105 | mpzp = PyObject_New(mpzobject, &MPZtype); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 106 | if (mpzp == NULL) |
| 107 | return NULL; |
| 108 | |
| 109 | mpz_init(&mpzp->mpz); /* actual initialisation */ |
| 110 | return mpzp; |
| 111 | } /* newmpzobject() */ |
| 112 | |
| 113 | #ifdef MPZ_GET_STR_BUG |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 114 | #include "longlong.h" |
| 115 | #endif /* def MPZ_GET_STR_BUG */ |
| 116 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 117 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 118 | mpz_format(objp, base, withname) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 119 | PyObject *objp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 120 | int base; |
| 121 | unsigned char withname; |
| 122 | { |
| 123 | mpzobject *mpzp = (mpzobject *)objp; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 124 | PyStringObject *strobjp; |
Guido van Rossum | 2650a42 | 2000-06-28 21:29:47 +0000 | [diff] [blame] | 125 | size_t i; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 126 | int cmpres; |
| 127 | int taglong; |
| 128 | char *cp; |
| 129 | char prefix[5], *tcp; |
| 130 | |
| 131 | |
| 132 | tcp = &prefix[0]; |
| 133 | |
| 134 | if (mpzp == NULL || !is_mpzobject(mpzp)) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 135 | PyErr_BadInternalCall(); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | assert(base >= 2 && base <= 36); |
| 140 | |
| 141 | if (withname) |
| 142 | i = strlen(initialiser_name) + 2; /* e.g. 'mpz(' + ')' */ |
| 143 | else |
| 144 | i = 0; |
| 145 | |
| 146 | if ((cmpres = mpz_cmp_si(&mpzp->mpz, 0L)) == 0) |
| 147 | base = 10; /* '0' in every base, right */ |
| 148 | else if (cmpres < 0) { |
| 149 | *tcp++ = '-'; |
| 150 | i += 1; /* space to hold '-' */ |
| 151 | } |
| 152 | |
| 153 | #ifdef MPZ_DEBUG |
| 154 | fprintf(stderr, "mpz_format: mpz_sizeinbase %d\n", |
| 155 | (int)mpz_sizeinbase(&mpzp->mpz, base)); |
| 156 | #endif /* def MPZ_DEBUG */ |
| 157 | #ifdef MPZ_GET_STR_BUG |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 158 | #ifdef GMP2 |
| 159 | i += ((size_t) abs(mpzp->mpz._mp_size) * BITS_PER_MP_LIMB |
| 160 | * __mp_bases[base].chars_per_bit_exactly) + 1; |
| 161 | #else |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 162 | i += ((size_t) abs(mpzp->mpz.size) * BITS_PER_MP_LIMB |
| 163 | * __mp_bases[base].chars_per_bit_exactly) + 1; |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 164 | #endif |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 165 | #else /* def MPZ_GET_STR_BUG */ |
| 166 | i += (int)mpz_sizeinbase(&mpzp->mpz, base); |
| 167 | #endif /* def MPZ_GET_STR_BUG else */ |
| 168 | |
| 169 | if (base == 16) { |
| 170 | *tcp++ = '0'; |
| 171 | *tcp++ = 'x'; |
| 172 | i += 2; /* space to hold '0x' */ |
| 173 | } |
| 174 | else if (base == 8) { |
| 175 | *tcp++ = '0'; |
| 176 | i += 1; /* space to hold the extra '0' */ |
| 177 | } |
| 178 | else if (base > 10) { |
| 179 | *tcp++ = '0' + base / 10; |
| 180 | *tcp++ = '0' + base % 10; |
| 181 | *tcp++ = '#'; |
| 182 | i += 3; /* space to hold e.g. '12#' */ |
| 183 | } |
| 184 | else if (base < 10) { |
| 185 | *tcp++ = '0' + base; |
| 186 | *tcp++ = '#'; |
| 187 | i += 2; /* space to hold e.g. '6#' */ |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | ** the following code looks if we need a 'L' attached to the number |
| 192 | ** it will also attach an 'L' to the value -0x80000000 |
| 193 | */ |
| 194 | taglong = 0; |
| 195 | if (mpz_size(&mpzp->mpz) > 1 |
| 196 | || (long)mpz_get_ui(&mpzp->mpz) < 0L) { |
| 197 | taglong = 1; |
| 198 | i += 1; /* space to hold 'L' */ |
| 199 | } |
| 200 | |
| 201 | #ifdef MPZ_DEBUG |
| 202 | fprintf(stderr, "mpz_format: requesting string size %d\n", i); |
| 203 | #endif /* def MPZ_DEBUG */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 204 | if ((strobjp = |
| 205 | (PyStringObject *)PyString_FromStringAndSize((char *)0, i)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 206 | == NULL) |
| 207 | return NULL; |
| 208 | |
| 209 | /* get the beginning of the string memory and start copying things */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 210 | cp = PyString_AS_STRING(strobjp); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 211 | if (withname) { |
| 212 | strcpy(cp, initialiser_name); |
| 213 | cp += strlen(initialiser_name); |
| 214 | *cp++ = '('; /*')'*/ |
| 215 | } |
| 216 | |
| 217 | /* copy the already prepared prefix; e.g. sign and base indicator */ |
| 218 | *tcp = '\0'; |
| 219 | strcpy(cp, prefix); |
| 220 | cp += tcp - prefix; |
| 221 | |
| 222 | /* since' we have the sign already, let the lib think it's a positive |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 223 | number */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 224 | if (cmpres < 0) |
| 225 | mpz_neg(&mpzp->mpz,&mpzp->mpz); /* hack Hack HAck HACk HACK */ |
| 226 | (void)mpz_get_str(cp, base, &mpzp->mpz); |
| 227 | if (cmpres < 0) |
| 228 | mpz_neg(&mpzp->mpz,&mpzp->mpz); /* hack Hack HAck HACk HACK */ |
| 229 | #ifdef MPZ_DEBUG |
| 230 | fprintf(stderr, "mpz_format: base (ultim) %d, mpz_get_str: %s\n", |
| 231 | base, cp); |
| 232 | #endif /* def MPZ_DEBUG */ |
| 233 | cp += strlen(cp); |
| 234 | |
| 235 | if (taglong) |
| 236 | *cp++ = 'L'; |
| 237 | if (withname) |
| 238 | *cp++ = /*'('*/ ')'; |
| 239 | |
| 240 | *cp = '\0'; |
| 241 | |
| 242 | #ifdef MPZ_DEBUG |
| 243 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 244 | "mpz_format: cp (str end) %p, begin %p, diff %d, i %d\n", |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 245 | cp, PyString_AS_STRING(strobjp), |
| 246 | cp - PyString_AS_STRING(strobjp), i); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 247 | #endif /* def MPZ_DEBUG */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 248 | assert(cp - PyString_AS_STRING(strobjp) <= i); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 249 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 250 | if (cp - PyString_AS_STRING(strobjp) != i) { |
| 251 | strobjp->ob_size -= i - (cp - PyString_AS_STRING(strobjp)); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 254 | return (PyObject *)strobjp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 255 | } /* mpz_format() */ |
| 256 | |
| 257 | /* MPZ methods */ |
| 258 | |
| 259 | static void |
| 260 | mpz_dealloc(mpzp) |
| 261 | mpzobject *mpzp; |
| 262 | { |
| 263 | #ifdef MPZ_DEBUG |
| 264 | fputs( "mpz_dealloc() called...\n", stderr ); |
| 265 | #endif /* def MPZ_DEBUG */ |
| 266 | mpz_clear(&mpzp->mpz); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 267 | PyObject_Del(mpzp); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 268 | } /* mpz_dealloc() */ |
| 269 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 270 | |
| 271 | /* pointers to frequently used values 0, 1 and -1 */ |
| 272 | static mpzobject *mpz_value_zero, *mpz_value_one, *mpz_value_mone; |
| 273 | |
| 274 | static int |
| 275 | mpz_compare(a, b) |
| 276 | mpzobject *a, *b; |
| 277 | { |
| 278 | int cmpres; |
| 279 | |
| 280 | |
| 281 | /* guido sez it's better to return -1, 0 or 1 */ |
| 282 | return (cmpres = mpz_cmp( &a->mpz, &b->mpz )) == 0 ? 0 |
| 283 | : cmpres > 0 ? 1 : -1; |
| 284 | } /* mpz_compare() */ |
| 285 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 286 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 287 | mpz_addition(a, b) |
| 288 | mpzobject *a; |
| 289 | mpzobject *b; |
| 290 | { |
| 291 | mpzobject *z; |
| 292 | |
| 293 | |
| 294 | #ifdef MPZ_SPARE_MALLOC |
| 295 | if (mpz_cmp_ui(&a->mpz, (unsigned long int)0) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 296 | Py_INCREF(b); |
| 297 | return (PyObject *)b; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (mpz_cmp_ui(&b->mpz, (unsigned long int)0) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 301 | Py_INCREF(a); |
| 302 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 303 | } |
| 304 | #endif /* def MPZ_SPARE_MALLOC */ |
| 305 | |
| 306 | if ((z = newmpzobject()) == NULL) |
| 307 | return NULL; |
| 308 | |
| 309 | mpz_add(&z->mpz, &a->mpz, &b->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 310 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 311 | } /* mpz_addition() */ |
| 312 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 313 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 314 | mpz_substract(a, b) |
| 315 | mpzobject *a; |
| 316 | mpzobject *b; |
| 317 | { |
| 318 | mpzobject *z; |
| 319 | |
| 320 | |
| 321 | #ifdef MPZ_SPARE_MALLOC |
| 322 | if (mpz_cmp_ui(&b->mpz, (unsigned long int)0) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 323 | Py_INCREF(a); |
| 324 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 325 | } |
| 326 | #endif /* MPZ_SPARE_MALLOC */ |
| 327 | |
| 328 | if ((z = newmpzobject()) == NULL) |
| 329 | return NULL; |
| 330 | |
| 331 | mpz_sub(&z->mpz, &a->mpz, &b->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 332 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 333 | } /* mpz_substract() */ |
| 334 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 335 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 336 | mpz_multiply(a, b) |
| 337 | mpzobject *a; |
| 338 | mpzobject *b; |
| 339 | { |
| 340 | #ifdef MPZ_SPARE_MALLOC |
| 341 | int cmpres; |
| 342 | #endif /* def MPZ_SPARE_MALLOC */ |
| 343 | mpzobject *z; |
| 344 | |
| 345 | |
| 346 | #ifdef MPZ_SPARE_MALLOC |
| 347 | if ((cmpres = mpz_cmp_ui(&a->mpz, (unsigned long int)0)) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 348 | Py_INCREF(mpz_value_zero); |
| 349 | return (PyObject *)mpz_value_zero; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 350 | } |
| 351 | if (cmpres > 0 && mpz_cmp_ui(&a->mpz, (unsigned long int)1) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 352 | Py_INCREF(b); |
| 353 | return (PyObject *)b; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | if ((cmpres = mpz_cmp_ui(&b->mpz, (unsigned long_int)0)) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 357 | Py_INCREF(mpz_value_zero); |
| 358 | return (PyObject *)mpz_value_zero; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 359 | } |
| 360 | if (cmpres > 0 && mpz_cmp_ui(&b->mpz, (unsigned long int)1) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 361 | Py_INCREF(a); |
| 362 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 363 | } |
| 364 | #endif /* MPZ_SPARE_MALLOC */ |
| 365 | |
| 366 | if ((z = newmpzobject()) == NULL) |
| 367 | return NULL; |
| 368 | |
| 369 | mpz_mul( &z->mpz, &a->mpz, &b->mpz ); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 370 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 371 | |
| 372 | } /* mpz_multiply() */ |
| 373 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 374 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 375 | mpz_divide(a, b) |
| 376 | mpzobject *a; |
| 377 | mpzobject *b; |
| 378 | { |
| 379 | #ifdef MPZ_SPARE_MALLOC |
| 380 | int cmpres; |
| 381 | #endif /* def MPZ_SPARE_MALLOC */ |
| 382 | mpzobject *z; |
| 383 | |
| 384 | |
| 385 | if (( |
| 386 | #ifdef MPZ_SPARE_MALLOC |
| 387 | cmpres = |
| 388 | #endif /* def MPZ_SPARE_MALLOC */ |
| 389 | mpz_cmp_ui(&b->mpz, (unsigned long int)0)) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_ZeroDivisionError, "mpz./ by zero"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | #ifdef MPZ_SPARE_MALLOC |
| 394 | if (cmpres > 0 && mpz_cmp_ui(&b->mpz(unsigned long int)1) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 395 | Py_INCREF(a); |
| 396 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 397 | } |
| 398 | #endif /* def MPZ_SPARE_MALLOC */ |
| 399 | |
| 400 | if ((z = newmpzobject()) == NULL) |
| 401 | return NULL; |
| 402 | |
| 403 | #ifdef MPZ_TEST_DIV |
| 404 | fputs("mpz_divide: div result", stderr); |
| 405 | mpz_div(&z->mpz, &a->mpz, &b->mpz); |
| 406 | mpz_out_str(stderr, 10, &z->mpz); |
| 407 | putc('\n', stderr); |
| 408 | #endif /* def MPZ_TEST_DIV */ |
| 409 | #ifdef MPZ_MDIV_BUG |
| 410 | if ((mpz_cmp_ui(&a->mpz, (unsigned long int)0) < 0) |
| 411 | != (mpz_cmp_ui(&b->mpz, (unsigned long int)0) < 0)) { |
| 412 | /* |
| 413 | ** numerator has other sign than denominator: we have |
| 414 | ** to look at the remainder for a correction, since mpz_mdiv |
| 415 | ** also calls mpz_divmod, I can as well do it myself |
| 416 | */ |
| 417 | MP_INT tmpmpz; |
| 418 | |
| 419 | |
| 420 | mpz_init(&tmpmpz); |
| 421 | mpz_divmod(&z->mpz, &tmpmpz, &a->mpz, &b->mpz); |
| 422 | |
| 423 | if (mpz_cmp_ui(&tmpmpz, (unsigned long int)0) != 0) |
| 424 | mpz_sub_ui(&z->mpz, &z->mpz, (unsigned long int)1); |
| 425 | |
| 426 | mpz_clear(&tmpmpz); |
| 427 | } |
| 428 | else |
| 429 | mpz_div(&z->mpz, &a->mpz, &b->mpz); |
| 430 | /* the ``naive'' implementation does it right for operands |
| 431 | having the same sign */ |
| 432 | |
| 433 | #else /* def MPZ_MDIV_BUG */ |
| 434 | mpz_mdiv(&z->mpz, &a->mpz, &b->mpz); |
| 435 | #endif /* def MPZ_MDIV_BUG else */ |
| 436 | #ifdef MPZ_TEST_DIV |
| 437 | fputs("mpz_divide: mdiv result", stderr); |
| 438 | mpz_out_str(stderr, 10, &z->mpz); |
| 439 | putc('\n', stderr); |
| 440 | #endif /* def MPZ_TEST_DIV */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 441 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 442 | |
| 443 | } /* mpz_divide() */ |
| 444 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 445 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 446 | mpz_remainder(a, b) |
| 447 | mpzobject *a; |
| 448 | mpzobject *b; |
| 449 | { |
| 450 | #ifdef MPZ_SPARE_MALLOC |
| 451 | int cmpres; |
| 452 | #endif /* def MPZ_SPARE_MALLOC */ |
| 453 | mpzobject *z; |
| 454 | |
| 455 | |
| 456 | if (( |
| 457 | #ifdef MPZ_SPARE_MALLOC |
| 458 | cmpres = |
| 459 | #endif /* def MPZ_SPARE_MALLOC */ |
| 460 | mpz_cmp_ui(&b->mpz, (unsigned long int)0)) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 461 | PyErr_SetString(PyExc_ZeroDivisionError, "mpz.% by zero"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | #ifdef MPZ_SPARE_MALLOC |
| 465 | if (cmpres > 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 466 | if ((cmpres = mpz_cmp_ui(&b->mpz, (unsigned long int)2)) == 0) |
| 467 | { |
| 468 | Py_INCREF(mpz_value_one); |
| 469 | return (PyObject *)mpz_value_one; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 470 | } |
| 471 | if (cmpres < 0) { |
| 472 | /* b must be 1 now */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 473 | Py_INCREF(mpz_value_zero); |
| 474 | return (PyObject *)mpz_value_zero; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | #endif /* def MPZ_SPARE_MALLOC */ |
| 478 | |
| 479 | if ((z = newmpzobject()) == NULL) |
| 480 | return NULL; |
| 481 | |
| 482 | #ifdef MPZ_TEST_DIV |
| 483 | fputs("mpz_remain: mod result", stderr); |
| 484 | mpz_mod(&z->mpz, &a->mpz, &b->mpz); |
| 485 | mpz_out_str(stderr, 10, &z->mpz); |
| 486 | putc('\n', stderr); |
| 487 | #endif /* def MPZ_TEST_DIV */ |
| 488 | #ifdef MPZ_MDIV_BUG |
| 489 | |
| 490 | /* the ``naive'' implementation does it right for operands |
| 491 | having the same sign */ |
| 492 | mpz_mod(&z->mpz, &a->mpz, &b->mpz); |
| 493 | |
| 494 | /* assumption: z, a and b all point to different locations */ |
| 495 | if ((mpz_cmp_ui(&a->mpz, (unsigned long int)0) < 0) |
| 496 | != (mpz_cmp_ui(&b->mpz, (unsigned long int)0) < 0) |
| 497 | && mpz_cmp_ui(&z->mpz, (unsigned long int)0) != 0) |
| 498 | mpz_add(&z->mpz, &z->mpz, &b->mpz); |
| 499 | /* |
| 500 | ** numerator has other sign than denominator: we have |
| 501 | ** to look at the remainder for a correction, since mpz_mdiv |
| 502 | ** also calls mpz_divmod, I can as well do it myself |
| 503 | */ |
| 504 | #else /* def MPZ_MDIV_BUG */ |
| 505 | mpz_mmod(&z->mpz, &a->mpz, &b->mpz); |
| 506 | #endif /* def MPZ_MDIV_BUG else */ |
| 507 | #ifdef MPZ_TEST_DIV |
| 508 | fputs("mpz_remain: mmod result", stderr); |
| 509 | mpz_out_str(stderr, 10, &z->mpz); |
| 510 | putc('\n', stderr); |
| 511 | #endif /* def MPZ_TEST_DIV */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 512 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 513 | |
| 514 | } /* mpz_remainder() */ |
| 515 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 516 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 517 | mpz_div_and_mod(a, b) |
| 518 | mpzobject *a; |
| 519 | mpzobject *b; |
| 520 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 521 | PyObject *z = NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 522 | mpzobject *x = NULL, *y = NULL; |
| 523 | |
| 524 | |
| 525 | if (mpz_cmp_ui(&b->mpz, (unsigned long int)0) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 526 | PyErr_SetString(PyExc_ZeroDivisionError, "mpz.divmod by zero"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | } |
| 529 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 530 | if ((z = PyTuple_New(2)) == NULL |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 531 | || (x = newmpzobject()) == NULL |
| 532 | || (y = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 533 | Py_XDECREF(z); |
| 534 | Py_XDECREF(x); |
| 535 | Py_XDECREF(y); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 536 | return NULL; |
| 537 | } |
| 538 | |
| 539 | #ifdef MPZ_TEST_DIV |
| 540 | fputs("mpz_divmod: dm result", stderr); |
| 541 | mpz_divmod(&x->mpz, &y->mpz, &a->mpz, &b->mpz); |
| 542 | mpz_out_str(stderr, 10, &x->mpz); |
| 543 | putc('\n', stderr); |
| 544 | mpz_out_str(stderr, 10, &y->mpz); |
| 545 | putc('\n', stderr); |
| 546 | #endif /* def MPZ_TEST_DIV */ |
| 547 | #ifdef MPZ_MDIV_BUG |
| 548 | mpz_divmod(&x->mpz, &y->mpz, &a->mpz, &b->mpz); |
| 549 | if ((mpz_cmp_ui(&a->mpz, (unsigned long int)0) < 0) |
| 550 | != (mpz_cmp_ui(&b->mpz, (unsigned long int)0) < 0) |
| 551 | && mpz_cmp_ui(&y->mpz, (unsigned long int)0) != 0) { |
| 552 | /* |
| 553 | ** numerator has other sign than denominator: we have |
| 554 | ** to look at the remainder for a correction. |
| 555 | */ |
| 556 | mpz_add(&y->mpz, &y->mpz, &b->mpz); |
| 557 | mpz_sub_ui(&x->mpz, &x->mpz, (unsigned long int)1); |
| 558 | } |
| 559 | #else /* def MPZ_MDIV_BUG */ |
| 560 | mpz_mdivmod( &x->mpz, &y->mpz, &a->mpz, &b->mpz ); |
| 561 | #endif /* def MPZ_MDIV_BUG else */ |
| 562 | #ifdef MPZ_TEST_DIV |
| 563 | fputs("mpz_divmod: mdm result", stderr); |
| 564 | mpz_out_str(stderr, 10, &x->mpz); |
| 565 | putc('\n', stderr); |
| 566 | mpz_out_str(stderr, 10, &y->mpz); |
| 567 | putc('\n', stderr); |
| 568 | #endif /* def MPZ_TEST_DIV */ |
| 569 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 570 | (void)PyTuple_SetItem(z, 0, (PyObject *)x); |
| 571 | (void)PyTuple_SetItem(z, 1, (PyObject *)y); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 572 | |
| 573 | return z; |
| 574 | } /* mpz_div_and_mod() */ |
| 575 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 576 | static PyObject * |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 577 | mpz_power(a, b, m) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 578 | mpzobject *a; |
| 579 | mpzobject *b; |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 580 | mpzobject *m; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 581 | { |
| 582 | mpzobject *z; |
| 583 | int cmpres; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 584 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 585 | if ((PyObject *)m != Py_None) { |
| 586 | mpzobject *z2; |
| 587 | Py_INCREF(Py_None); |
| 588 | z=(mpzobject *)mpz_power(a, b, (mpzobject *)Py_None); |
| 589 | Py_DECREF(Py_None); |
| 590 | if (z==NULL) return((PyObject *)z); |
| 591 | z2=(mpzobject *)mpz_remainder(z, m); |
| 592 | Py_DECREF(z); |
| 593 | return((PyObject *)z2); |
| 594 | } |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 595 | |
| 596 | if ((cmpres = mpz_cmp_ui(&b->mpz, (unsigned long int)0)) == 0) { |
| 597 | /* the gnu-mp lib sets pow(0,0) to 0, we to 1 */ |
| 598 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 599 | Py_INCREF(mpz_value_one); |
| 600 | return (PyObject *)mpz_value_one; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | if (cmpres < 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 604 | PyErr_SetString(PyExc_ValueError, |
| 605 | "mpz.pow to negative exponent"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | if ((cmpres = mpz_cmp_ui(&a->mpz, (unsigned long int)0)) == 0) { |
| 610 | /* the base is 0 */ |
| 611 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 612 | Py_INCREF(mpz_value_zero); |
| 613 | return (PyObject *)mpz_value_zero; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 614 | } |
| 615 | else if (cmpres > 0 |
| 616 | && mpz_cmp_ui(&a->mpz, (unsigned long int)1) == 0) { |
| 617 | /* the base is 1 */ |
| 618 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 619 | Py_INCREF(mpz_value_one); |
| 620 | return (PyObject *)mpz_value_one; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 621 | } |
| 622 | else if (cmpres < 0 |
| 623 | && mpz_cmp_si(&a->mpz, (long int)-1) == 0) { |
| 624 | |
| 625 | MP_INT tmpmpz; |
| 626 | /* the base is -1: pow(-1, any) == 1,-1 for even,uneven b */ |
| 627 | /* XXX this code needs to be optimized: what's better? |
| 628 | mpz_mmod_ui or mpz_mod_2exp, I choose for the latter |
| 629 | for *un*obvious reasons */ |
| 630 | |
| 631 | /* is the exponent even? */ |
| 632 | mpz_init(&tmpmpz); |
| 633 | |
| 634 | /* look to the remainder after a division by (1 << 1) */ |
| 635 | mpz_mod_2exp(&tmpmpz, &b->mpz, (unsigned long int)1); |
| 636 | |
| 637 | if (mpz_cmp_ui(&tmpmpz, (unsigned int)0) == 0) { |
| 638 | mpz_clear(&tmpmpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 639 | Py_INCREF(mpz_value_one); |
| 640 | return (PyObject *)mpz_value_one; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 641 | } |
| 642 | mpz_clear(&tmpmpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 643 | Py_INCREF(mpz_value_mone); |
| 644 | return (PyObject *)mpz_value_mone; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | #ifdef MPZ_LIB_DOES_CHECKING |
| 648 | /* check if it's doable: sizeof(exp) > sizeof(long) && |
| 649 | abs(base) > 1 ?? --> No Way */ |
| 650 | if (mpz_size(&b->mpz) > 1) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 651 | return (PyObject *)PyErr_NoMemory(); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 652 | #else /* def MPZ_LIB_DOES_CHECKING */ |
| 653 | /* wet finger method */ |
| 654 | if (mpz_cmp_ui(&b->mpz, (unsigned long int)0x10000) >= 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 655 | PyErr_SetString(PyExc_ValueError, |
| 656 | "mpz.pow outrageous exponent"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
| 659 | #endif /* def MPZ_LIB_DOES_CHECKING else */ |
| 660 | |
| 661 | if ((z = newmpzobject()) == NULL) |
| 662 | return NULL; |
| 663 | |
| 664 | mpz_pow_ui(&z->mpz, &a->mpz, mpz_get_ui(&b->mpz)); |
| 665 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 666 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 667 | } /* mpz_power() */ |
| 668 | |
| 669 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 670 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 671 | mpz_negative(v) |
| 672 | mpzobject *v; |
| 673 | { |
| 674 | mpzobject *z; |
| 675 | |
| 676 | |
| 677 | #ifdef MPZ_SPARE_MALLOC |
| 678 | if (mpz_cmp_ui(&v->mpz, (unsigned long int)0) == 0) { |
| 679 | /* -0 == 0 */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 680 | Py_INCREF(v); |
| 681 | return (PyObject *)v; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 682 | } |
| 683 | #endif /* def MPZ_SPARE_MALLOC */ |
| 684 | |
| 685 | if ((z = newmpzobject()) == NULL) |
| 686 | return NULL; |
| 687 | |
| 688 | mpz_neg(&z->mpz, &v->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 689 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 690 | } /* mpz_negative() */ |
| 691 | |
| 692 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 693 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 694 | mpz_positive(v) |
| 695 | mpzobject *v; |
| 696 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 697 | Py_INCREF(v); |
| 698 | return (PyObject *)v; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 699 | } /* mpz_positive() */ |
| 700 | |
| 701 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 702 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 703 | mpz_absolute(v) |
| 704 | mpzobject *v; |
| 705 | { |
| 706 | mpzobject *z; |
| 707 | |
| 708 | |
| 709 | if (mpz_cmp_ui(&v->mpz, (unsigned long int)0) >= 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 710 | Py_INCREF(v); |
| 711 | return (PyObject *)v; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | if ((z = newmpzobject()) == NULL) |
| 715 | return NULL; |
| 716 | |
| 717 | mpz_neg(&z->mpz, &v->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 718 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 719 | } /* mpz_absolute() */ |
| 720 | |
| 721 | static int |
| 722 | mpz_nonzero(v) |
| 723 | mpzobject *v; |
| 724 | { |
| 725 | return mpz_cmp_ui(&v->mpz, (unsigned long int)0) != 0; |
| 726 | } /* mpz_nonzero() */ |
| 727 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 728 | static PyObject * |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 729 | py_mpz_invert(v) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 730 | mpzobject *v; |
| 731 | { |
| 732 | mpzobject *z; |
| 733 | |
| 734 | |
| 735 | /* I think mpz_com does exactly what needed */ |
| 736 | if ((z = newmpzobject()) == NULL) |
| 737 | return NULL; |
| 738 | |
| 739 | mpz_com(&z->mpz, &v->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 740 | return (PyObject *)z; |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 741 | } /* py_mpz_invert() */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 742 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 743 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 744 | mpz_lshift(a, b) |
| 745 | mpzobject *a; |
| 746 | mpzobject *b; |
| 747 | { |
| 748 | int cmpres; |
| 749 | mpzobject *z; |
| 750 | |
| 751 | |
| 752 | if ((cmpres = mpz_cmp_ui(&b->mpz, (unsigned long int)0)) == 0) { |
| 753 | /* a << 0 == a */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 754 | Py_INCREF(a); |
| 755 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (cmpres < 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 759 | PyErr_SetString(PyExc_ValueError, |
| 760 | "mpz.<< negative shift count"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | #ifdef MPZ_LIB_DOES_CHECKING |
| 765 | if (mpz_size(&b->mpz) > 1) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 766 | return (PyObject *)PyErr_NoMemory(); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 767 | #else /* def MPZ_LIB_DOES_CHECKING */ |
| 768 | /* wet finger method */ |
| 769 | if (mpz_cmp_ui(&b->mpz, (unsigned long int)0x10000) >= 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 770 | PyErr_SetString(PyExc_ValueError, |
| 771 | "mpz.<< outrageous shift count"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 772 | return NULL; |
| 773 | } |
| 774 | #endif /* def MPZ_LIB_DOES_CHECKING else */ |
| 775 | |
| 776 | if ((z = newmpzobject()) == NULL) |
| 777 | return NULL; |
| 778 | |
| 779 | mpz_mul_2exp(&z->mpz, &a->mpz, mpz_get_ui(&b->mpz)); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 780 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 781 | } /* mpz_lshift() */ |
| 782 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 783 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 784 | mpz_rshift(a, b) |
| 785 | mpzobject *a; |
| 786 | mpzobject *b; |
| 787 | { |
| 788 | int cmpres; |
| 789 | mpzobject *z; |
| 790 | |
| 791 | |
| 792 | if ((cmpres = mpz_cmp_ui(&b->mpz, (unsigned long int)0)) == 0) { |
| 793 | /* a >> 0 == a */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 794 | Py_INCREF(a); |
| 795 | return (PyObject *)a; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (cmpres < 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 799 | PyErr_SetString(PyExc_ValueError, |
| 800 | "mpz.>> negative shift count"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | |
| 804 | if (mpz_size(&b->mpz) > 1) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 805 | return (PyObject *)PyErr_NoMemory(); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 806 | |
| 807 | if ((z = newmpzobject()) == NULL) |
| 808 | return NULL; |
| 809 | |
| 810 | mpz_div_2exp(&z->mpz, &a->mpz, mpz_get_ui(&b->mpz)); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 811 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 812 | } /* mpz_rshift() */ |
| 813 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 814 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 815 | mpz_andfunc(a, b) |
| 816 | mpzobject *a; |
| 817 | mpzobject *b; |
| 818 | { |
| 819 | mpzobject *z; |
| 820 | |
| 821 | |
| 822 | if ((z = newmpzobject()) == NULL) |
| 823 | return NULL; |
| 824 | |
| 825 | mpz_and(&z->mpz, &a->mpz, &b->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 826 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 827 | } /* mpz_andfunc() */ |
| 828 | |
| 829 | /* hack Hack HAck HACk HACK, XXX this code is dead slow */ |
| 830 | void |
| 831 | mpz_xor(res, op1, op2) |
| 832 | MP_INT *res; |
| 833 | const MP_INT *op1; |
| 834 | const MP_INT *op2; |
| 835 | { |
| 836 | MP_INT tmpmpz; |
| 837 | |
| 838 | mpz_init(&tmpmpz); |
| 839 | |
| 840 | mpz_and(res, op1, op2); |
| 841 | mpz_com(&tmpmpz, res); |
| 842 | mpz_ior(res, op1, op2); |
| 843 | mpz_and(res, res, &tmpmpz); |
| 844 | |
| 845 | mpz_clear(&tmpmpz); |
| 846 | } /* mpz_xor() HACK */ |
| 847 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 848 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 849 | mpz_xorfunc(a, b) |
| 850 | mpzobject *a; |
| 851 | mpzobject *b; |
| 852 | { |
| 853 | mpzobject *z; |
| 854 | |
| 855 | |
| 856 | if ((z = newmpzobject()) == NULL) |
| 857 | return NULL; |
| 858 | |
| 859 | mpz_xor(&z->mpz, &a->mpz, &b->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 860 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 861 | } /* mpz_xorfunc() */ |
| 862 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 863 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 864 | mpz_orfunc(a, b) |
| 865 | mpzobject *a; |
| 866 | mpzobject *b; |
| 867 | { |
| 868 | mpzobject *z; |
| 869 | |
| 870 | |
| 871 | if ((z = newmpzobject()) == NULL) |
| 872 | return NULL; |
| 873 | |
| 874 | mpz_ior(&z->mpz, &a->mpz, &b->mpz); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 875 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 876 | } /* mpz_orfunc() */ |
| 877 | |
| 878 | /* MPZ initialisation */ |
| 879 | |
| 880 | #include "longintrepr.h" |
| 881 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 882 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 883 | MPZ_mpz(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 884 | PyObject *self; |
| 885 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 886 | { |
| 887 | mpzobject *mpzp; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 888 | PyObject *objp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 889 | |
| 890 | |
| 891 | #ifdef MPZ_DEBUG |
| 892 | fputs("MPZ_mpz() called...\n", stderr); |
| 893 | #endif /* def MPZ_DEBUG */ |
| 894 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 895 | if (!PyArg_Parse(args, "O", &objp)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 896 | return NULL; |
| 897 | |
| 898 | /* at least we know it's some object */ |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 899 | /* note DON't Py_DECREF args NEITHER objp */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 900 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 901 | if (PyInt_Check(objp)) { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 902 | long lval; |
| 903 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 904 | if (!PyArg_Parse(objp, "l", &lval)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 905 | return NULL; |
| 906 | |
| 907 | if (lval == (long)0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 908 | Py_INCREF(mpz_value_zero); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 909 | mpzp = mpz_value_zero; |
| 910 | } |
| 911 | else if (lval == (long)1) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 912 | Py_INCREF(mpz_value_one); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 913 | mpzp = mpz_value_one; |
| 914 | } |
| 915 | else if ((mpzp = newmpzobject()) == NULL) |
| 916 | return NULL; |
| 917 | else mpz_set_si(&mpzp->mpz, lval); |
| 918 | } |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 919 | else if (PyLong_Check(objp)) { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 920 | MP_INT mplongdigit; |
| 921 | int i; |
| 922 | unsigned char isnegative; |
| 923 | |
| 924 | |
| 925 | if ((mpzp = newmpzobject()) == NULL) |
| 926 | return NULL; |
| 927 | |
| 928 | mpz_set_si(&mpzp->mpz, 0L); |
| 929 | mpz_init(&mplongdigit); |
| 930 | |
| 931 | /* how we're gonna handle this? */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 932 | if ((isnegative = |
| 933 | ((i = ((PyLongObject *)objp)->ob_size) < 0) )) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 934 | i = -i; |
| 935 | |
| 936 | while (i--) { |
| 937 | mpz_set_ui(&mplongdigit, |
| 938 | (unsigned long) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 939 | ((PyLongObject *)objp)->ob_digit[i]); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 940 | mpz_mul_2exp(&mplongdigit,&mplongdigit, |
| 941 | (unsigned long int)i * SHIFT); |
| 942 | mpz_ior(&mpzp->mpz, &mpzp->mpz, &mplongdigit); |
| 943 | } |
| 944 | |
| 945 | if (isnegative) |
| 946 | mpz_neg(&mpzp->mpz, &mpzp->mpz); |
| 947 | |
| 948 | /* get rid of allocation for tmp variable */ |
| 949 | mpz_clear(&mplongdigit); |
| 950 | } |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 951 | else if (PyString_Check(objp)) { |
Andrew M. Kuchling | 4c07f81 | 1998-12-14 19:36:14 +0000 | [diff] [blame] | 952 | unsigned char *cp = (unsigned char *)PyString_AS_STRING(objp); |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 953 | int len = PyString_GET_SIZE(objp); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 954 | MP_INT mplongdigit; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 955 | |
| 956 | if ((mpzp = newmpzobject()) == NULL) |
| 957 | return NULL; |
| 958 | |
| 959 | mpz_set_si(&mpzp->mpz, 0L); |
| 960 | mpz_init(&mplongdigit); |
| 961 | |
| 962 | /* let's do it the same way as with the long conversion: |
| 963 | without thinking how it can be faster (-: :-) */ |
| 964 | |
| 965 | cp += len; |
| 966 | while (len--) { |
| 967 | mpz_set_ui(&mplongdigit, (unsigned long)*--cp ); |
| 968 | mpz_mul_2exp(&mplongdigit,&mplongdigit, |
| 969 | (unsigned long int)len * 8); |
| 970 | mpz_ior(&mpzp->mpz, &mpzp->mpz, &mplongdigit); |
| 971 | } |
| 972 | |
| 973 | /* get rid of allocation for tmp variable */ |
| 974 | mpz_clear(&mplongdigit); |
| 975 | } |
| 976 | else if (is_mpzobject(objp)) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 977 | Py_INCREF(objp); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 978 | mpzp = (mpzobject *)objp; |
| 979 | } |
| 980 | else { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 981 | PyErr_SetString(PyExc_TypeError, |
| 982 | "mpz.mpz() expects integer, long, string or mpz object argument"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 983 | return NULL; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | #ifdef MPZ_DEBUG |
| 988 | fputs("MPZ_mpz: created mpz=", stderr); |
| 989 | mpz_out_str(stderr, 10, &mpzp->mpz); |
| 990 | putc('\n', stderr); |
| 991 | #endif /* def MPZ_DEBUG */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 992 | return (PyObject *)mpzp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 993 | } /* MPZ_mpz() */ |
| 994 | |
| 995 | static mpzobject * |
| 996 | mpz_mpzcoerce(z) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 997 | PyObject *z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 998 | { |
| 999 | /* shortcut: 9 out of 10 times the type is already ok */ |
| 1000 | if (is_mpzobject(z)) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1001 | Py_INCREF(z); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1002 | return (mpzobject *)z; /* coercion succeeded */ |
| 1003 | } |
| 1004 | |
| 1005 | /* what types do we accept?: intobjects and longobjects */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1006 | if (PyInt_Check(z) || PyLong_Check(z)) |
| 1007 | return (mpzobject *)MPZ_mpz((PyObject *)NULL, z); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1008 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1009 | PyErr_SetString(PyExc_TypeError, |
| 1010 | "number coercion (to mpzobject) failed"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1011 | return NULL; |
| 1012 | } /* mpz_mpzcoerce() */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1013 | |
| 1014 | /* Forward */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1015 | static void mpz_divm Py_PROTO((MP_INT *res, const MP_INT *num, |
| 1016 | const MP_INT *den, const MP_INT *mod)); |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 1017 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1018 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1019 | MPZ_powm(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1020 | PyObject *self; |
| 1021 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1022 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1023 | PyObject *base, *exp, *mod; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1024 | mpzobject *mpzbase = NULL, *mpzexp = NULL, *mpzmod = NULL; |
| 1025 | mpzobject *z; |
| 1026 | int tstres; |
| 1027 | |
| 1028 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1029 | if (!PyArg_Parse(args, "(OOO)", &base, &exp, &mod)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1030 | return NULL; |
| 1031 | |
| 1032 | if ((mpzbase = mpz_mpzcoerce(base)) == NULL |
| 1033 | || (mpzexp = mpz_mpzcoerce(exp)) == NULL |
| 1034 | || (mpzmod = mpz_mpzcoerce(mod)) == NULL |
| 1035 | || (z = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1036 | Py_XDECREF(mpzbase); |
| 1037 | Py_XDECREF(mpzexp); |
| 1038 | Py_XDECREF(mpzmod); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1039 | return NULL; |
| 1040 | } |
| 1041 | |
| 1042 | if ((tstres=mpz_cmp_ui(&mpzexp->mpz, (unsigned long int)0)) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1043 | Py_INCREF(mpz_value_one); |
| 1044 | return (PyObject *)mpz_value_one; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | if (tstres < 0) { |
| 1048 | MP_INT absexp; |
| 1049 | /* negative exp */ |
| 1050 | |
| 1051 | mpz_init_set(&absexp, &mpzexp->mpz); |
| 1052 | mpz_abs(&absexp, &absexp); |
| 1053 | mpz_powm(&z->mpz, &mpzbase->mpz, &absexp, &mpzmod->mpz); |
| 1054 | |
| 1055 | mpz_divm(&z->mpz, &mpz_value_one->mpz, &z->mpz, &mpzmod->mpz); |
| 1056 | |
| 1057 | mpz_clear(&absexp); |
| 1058 | } |
| 1059 | else { |
| 1060 | mpz_powm(&z->mpz, &mpzbase->mpz, &mpzexp->mpz, &mpzmod->mpz); |
| 1061 | } |
| 1062 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1063 | Py_DECREF(mpzbase); |
| 1064 | Py_DECREF(mpzexp); |
| 1065 | Py_DECREF(mpzmod); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1066 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1067 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1068 | } /* MPZ_powm() */ |
| 1069 | |
| 1070 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1071 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1072 | MPZ_gcd(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1073 | PyObject *self; |
| 1074 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1075 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1076 | PyObject *op1, *op2; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1077 | mpzobject *mpzop1 = NULL, *mpzop2 = NULL; |
| 1078 | mpzobject *z; |
| 1079 | |
| 1080 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1081 | if (!PyArg_Parse(args, "(OO)", &op1, &op2)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1082 | return NULL; |
| 1083 | |
| 1084 | if ((mpzop1 = mpz_mpzcoerce(op1)) == NULL |
| 1085 | || (mpzop2 = mpz_mpzcoerce(op2)) == NULL |
| 1086 | || (z = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1087 | Py_XDECREF(mpzop1); |
| 1088 | Py_XDECREF(mpzop2); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1089 | return NULL; |
| 1090 | } |
| 1091 | |
| 1092 | /* ok, we have three mpzobjects, and an initialised result holder */ |
| 1093 | mpz_gcd(&z->mpz, &mpzop1->mpz, &mpzop2->mpz); |
| 1094 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1095 | Py_DECREF(mpzop1); |
| 1096 | Py_DECREF(mpzop2); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1097 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1098 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1099 | } /* MPZ_gcd() */ |
| 1100 | |
| 1101 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1102 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1103 | MPZ_gcdext(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1104 | PyObject *self; |
| 1105 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1106 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1107 | PyObject *op1, *op2, *z = NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1108 | mpzobject *mpzop1 = NULL, *mpzop2 = NULL; |
| 1109 | mpzobject *g = NULL, *s = NULL, *t = NULL; |
| 1110 | |
| 1111 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1112 | if (!PyArg_Parse(args, "(OO)", &op1, &op2)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1113 | return NULL; |
| 1114 | |
| 1115 | if ((mpzop1 = mpz_mpzcoerce(op1)) == NULL |
| 1116 | || (mpzop2 = mpz_mpzcoerce(op2)) == NULL |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1117 | || (z = PyTuple_New(3)) == NULL |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1118 | || (g = newmpzobject()) == NULL |
| 1119 | || (s = newmpzobject()) == NULL |
| 1120 | || (t = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1121 | Py_XDECREF(mpzop1); |
| 1122 | Py_XDECREF(mpzop2); |
| 1123 | Py_XDECREF(z); |
| 1124 | Py_XDECREF(g); |
| 1125 | Py_XDECREF(s); |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 1126 | /*Py_XDECREF(t);*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1127 | return NULL; |
| 1128 | } |
| 1129 | |
| 1130 | mpz_gcdext(&g->mpz, &s->mpz, &t->mpz, &mpzop1->mpz, &mpzop2->mpz); |
| 1131 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1132 | Py_DECREF(mpzop1); |
| 1133 | Py_DECREF(mpzop2); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1134 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1135 | (void)PyTuple_SetItem(z, 0, (PyObject *)g); |
| 1136 | (void)PyTuple_SetItem(z, 1, (PyObject *)s); |
| 1137 | (void)PyTuple_SetItem(z, 2, (PyObject *)t); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1138 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1139 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1140 | } /* MPZ_gcdext() */ |
| 1141 | |
| 1142 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1143 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1144 | MPZ_sqrt(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1145 | PyObject *self; |
| 1146 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1147 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1148 | PyObject *op; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1149 | mpzobject *mpzop = NULL; |
| 1150 | mpzobject *z; |
| 1151 | |
| 1152 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1153 | if (!PyArg_Parse(args, "O", &op)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1154 | return NULL; |
| 1155 | |
| 1156 | if ((mpzop = mpz_mpzcoerce(op)) == NULL |
| 1157 | || (z = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1158 | Py_XDECREF(mpzop); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1159 | return NULL; |
| 1160 | } |
| 1161 | |
| 1162 | mpz_sqrt(&z->mpz, &mpzop->mpz); |
| 1163 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1164 | Py_DECREF(mpzop); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1165 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1166 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1167 | } /* MPZ_sqrt() */ |
| 1168 | |
| 1169 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1170 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1171 | MPZ_sqrtrem(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1172 | PyObject *self; |
| 1173 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1174 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1175 | PyObject *op, *z = NULL; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1176 | mpzobject *mpzop = NULL; |
| 1177 | mpzobject *root = NULL, *rem = NULL; |
| 1178 | |
| 1179 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1180 | if (!PyArg_Parse(args, "O", &op)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | |
| 1183 | if ((mpzop = mpz_mpzcoerce(op)) == NULL |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1184 | || (z = PyTuple_New(2)) == NULL |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1185 | || (root = newmpzobject()) == NULL |
| 1186 | || (rem = newmpzobject()) == NULL) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1187 | Py_XDECREF(mpzop); |
| 1188 | Py_XDECREF(z); |
| 1189 | Py_XDECREF(root); |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 1190 | /*Py_XDECREF(rem);*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1191 | return NULL; |
| 1192 | } |
| 1193 | |
| 1194 | mpz_sqrtrem(&root->mpz, &rem->mpz, &mpzop->mpz); |
| 1195 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1196 | Py_DECREF(mpzop); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1197 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1198 | (void)PyTuple_SetItem(z, 0, (PyObject *)root); |
| 1199 | (void)PyTuple_SetItem(z, 1, (PyObject *)rem); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1200 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1201 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1202 | } /* MPZ_sqrtrem() */ |
| 1203 | |
| 1204 | |
Guido van Rossum | 67a5fdb | 1993-12-17 12:09:14 +0000 | [diff] [blame] | 1205 | static void |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1206 | #if __STDC__ |
| 1207 | mpz_divm(MP_INT *res, const MP_INT *num, const MP_INT *den, const MP_INT *mod) |
| 1208 | #else |
| 1209 | mpz_divm(res, num, den, mod) |
| 1210 | MP_INT *res; |
| 1211 | const MP_INT *num; |
| 1212 | const MP_INT *den; |
| 1213 | const MP_INT *mod; |
| 1214 | #endif |
| 1215 | { |
| 1216 | MP_INT s0, s1, q, r, x, d0, d1; |
| 1217 | |
| 1218 | mpz_init_set(&s0, num); |
| 1219 | mpz_init_set_ui(&s1, 0); |
| 1220 | mpz_init(&q); |
| 1221 | mpz_init(&r); |
| 1222 | mpz_init(&x); |
| 1223 | mpz_init_set(&d0, den); |
| 1224 | mpz_init_set(&d1, mod); |
| 1225 | |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1226 | #ifdef GMP2 |
| 1227 | while (d1._mp_size != 0) { |
| 1228 | #else |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1229 | while (d1.size != 0) { |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1230 | #endif |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1231 | mpz_divmod(&q, &r, &d0, &d1); |
| 1232 | mpz_set(&d0, &d1); |
| 1233 | mpz_set(&d1, &r); |
| 1234 | |
| 1235 | mpz_mul(&x, &s1, &q); |
| 1236 | mpz_sub(&x, &s0, &x); |
| 1237 | mpz_set(&s0, &s1); |
| 1238 | mpz_set(&s1, &x); |
| 1239 | } |
| 1240 | |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1241 | #ifdef GMP2 |
| 1242 | if (d0._mp_size != 1 || d0._mp_d[0] != 1) |
| 1243 | res->_mp_size = 0; /* trouble: the gcd != 1; set s to zero */ |
| 1244 | #else |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1245 | if (d0.size != 1 || d0.d[0] != 1) |
| 1246 | res->size = 0; /* trouble: the gcd != 1; set s to zero */ |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1247 | #endif |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1248 | else { |
| 1249 | #ifdef MPZ_MDIV_BUG |
| 1250 | /* watch out here! first check the signs, and then perform |
| 1251 | the mpz_mod() since mod could point to res */ |
| 1252 | if ((s0.size < 0) != (mod->size < 0)) { |
| 1253 | mpz_mod(res, &s0, mod); |
| 1254 | |
| 1255 | if (res->size) |
| 1256 | mpz_add(res, res, mod); |
| 1257 | } |
| 1258 | else |
| 1259 | mpz_mod(res, &s0, mod); |
| 1260 | |
| 1261 | #else /* def MPZ_MDIV_BUG */ |
| 1262 | mpz_mmod(res, &s0, mod); |
| 1263 | #endif /* def MPZ_MDIV_BUG else */ |
| 1264 | } |
| 1265 | |
| 1266 | mpz_clear(&s0); |
| 1267 | mpz_clear(&s1); |
| 1268 | mpz_clear(&q); |
| 1269 | mpz_clear(&r); |
| 1270 | mpz_clear(&x); |
| 1271 | mpz_clear(&d0); |
| 1272 | mpz_clear(&d1); |
| 1273 | } /* mpz_divm() */ |
| 1274 | |
| 1275 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1276 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1277 | MPZ_divm(self, args) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1278 | PyObject *self; |
| 1279 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1280 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1281 | PyObject *num, *den, *mod; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1282 | mpzobject *mpznum, *mpzden, *mpzmod = NULL; |
| 1283 | mpzobject *z = NULL; |
| 1284 | |
| 1285 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1286 | if (!PyArg_Parse(args, "(OOO)", &num, &den, &mod)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1287 | return NULL; |
| 1288 | |
| 1289 | if ((mpznum = mpz_mpzcoerce(num)) == NULL |
| 1290 | || (mpzden = mpz_mpzcoerce(den)) == NULL |
| 1291 | || (mpzmod = mpz_mpzcoerce(mod)) == NULL |
| 1292 | || (z = newmpzobject()) == NULL ) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1293 | Py_XDECREF(mpznum); |
| 1294 | Py_XDECREF(mpzden); |
| 1295 | Py_XDECREF(mpzmod); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1296 | return NULL; |
| 1297 | } |
| 1298 | |
| 1299 | mpz_divm(&z->mpz, &mpznum->mpz, &mpzden->mpz, &mpzmod->mpz); |
| 1300 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1301 | Py_DECREF(mpznum); |
| 1302 | Py_DECREF(mpzden); |
| 1303 | Py_DECREF(mpzmod); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (mpz_cmp_ui(&z->mpz, (unsigned long int)0) == 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1306 | Py_DECREF(z); |
| 1307 | PyErr_SetString(PyExc_ValueError, |
| 1308 | "gcd(den, mod) != 1 or num == 0"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1309 | return NULL; |
| 1310 | } |
| 1311 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1312 | return (PyObject *)z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1313 | } /* MPZ_divm() */ |
| 1314 | |
| 1315 | |
| 1316 | /* MPZ methods-as-attributes */ |
| 1317 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1318 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1319 | mpz_int(self, args) |
| 1320 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1321 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1322 | #else /* def MPZ_CONVERSIONS_AS_METHODS */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1323 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1324 | mpz_int(self) |
| 1325 | mpzobject *self; |
| 1326 | #endif /* def MPZ_CONVERSIONS_AS_METHODS else */ |
| 1327 | { |
| 1328 | long sli; |
| 1329 | |
| 1330 | |
| 1331 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1332 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1333 | return NULL; |
| 1334 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1335 | |
| 1336 | if (mpz_size(&self->mpz) > 1 |
| 1337 | || (sli = (long)mpz_get_ui(&self->mpz)) < (long)0 ) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1338 | PyErr_SetString(PyExc_ValueError, |
| 1339 | "mpz.int() arg too long to convert"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1340 | return NULL; |
| 1341 | } |
| 1342 | |
| 1343 | if (mpz_cmp_ui(&self->mpz, (unsigned long)0) < 0) |
| 1344 | sli = -sli; |
| 1345 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1346 | return PyInt_FromLong(sli); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1347 | } /* mpz_int() */ |
| 1348 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1349 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1350 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
| 1351 | mpz_long(self, args) |
| 1352 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1353 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1354 | #else /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1355 | mpz_long(self) |
| 1356 | mpzobject *self; |
| 1357 | #endif /* def MPZ_CONVERSIONS_AS_METHODS else */ |
| 1358 | { |
| 1359 | int i, isnegative; |
| 1360 | unsigned long int uli; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1361 | PyLongObject *longobjp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1362 | int ldcount; |
| 1363 | int bitpointer, newbitpointer; |
| 1364 | MP_INT mpzscratch; |
| 1365 | |
| 1366 | |
| 1367 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1368 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1369 | return NULL; |
| 1370 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1371 | |
| 1372 | /* determine length of python-long to be allocated */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1373 | if ((longobjp = _PyLong_New(i = (int) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1374 | ((mpz_size(&self->mpz) * BITS_PER_MP_LIMB |
| 1375 | + SHIFT - 1) / |
| 1376 | SHIFT))) == NULL) |
| 1377 | return NULL; |
| 1378 | |
| 1379 | /* determine sign, and copy self to scratch var */ |
| 1380 | mpz_init_set(&mpzscratch, &self->mpz); |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1381 | if ((isnegative = (mpz_cmp_ui(&self->mpz, (unsigned long int)0) < 0))) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1382 | mpz_neg(&mpzscratch, &mpzscratch); |
| 1383 | |
| 1384 | /* let those bits come, let those bits go, |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 1385 | e.g. dismantle mpzscratch, build PyLongObject */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1386 | |
| 1387 | bitpointer = 0; /* the number of valid bits in stock */ |
| 1388 | newbitpointer = 0; |
| 1389 | ldcount = 0; /* the python-long limb counter */ |
| 1390 | uli = (unsigned long int)0; |
| 1391 | while (i--) { |
| 1392 | longobjp->ob_digit[ldcount] = uli & MASK; |
| 1393 | |
| 1394 | /* check if we've had enough bits for this digit */ |
| 1395 | if (bitpointer < SHIFT) { |
| 1396 | uli = mpz_get_ui(&mpzscratch); |
| 1397 | longobjp->ob_digit[ldcount] |= |
| 1398 | (uli << bitpointer) & MASK; |
| 1399 | uli >>= SHIFT-bitpointer; |
| 1400 | bitpointer += BITS_PER_MP_LIMB; |
| 1401 | mpz_div_2exp(&mpzscratch, &mpzscratch, |
| 1402 | BITS_PER_MP_LIMB); |
| 1403 | } |
| 1404 | else |
| 1405 | uli >>= SHIFT; |
| 1406 | bitpointer -= SHIFT; |
| 1407 | ldcount++; |
| 1408 | } |
| 1409 | |
| 1410 | assert(mpz_cmp_ui(&mpzscratch, (unsigned long int)0) == 0); |
| 1411 | mpz_clear(&mpzscratch); |
| 1412 | assert(ldcount <= longobjp->ob_size); |
| 1413 | |
| 1414 | /* long_normalize() is file-static */ |
| 1415 | /* longobjp = long_normalize(longobjp); */ |
| 1416 | while (ldcount > 0 && longobjp->ob_digit[ldcount-1] == 0) |
| 1417 | ldcount--; |
| 1418 | longobjp->ob_size = ldcount; |
| 1419 | |
| 1420 | |
| 1421 | if (isnegative) |
| 1422 | longobjp->ob_size = -longobjp->ob_size; |
| 1423 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1424 | return (PyObject *)longobjp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1425 | |
| 1426 | } /* mpz_long() */ |
| 1427 | |
| 1428 | |
| 1429 | /* I would have avoided pow() anyways, so ... */ |
| 1430 | static const double multiplier = 256.0 * 256.0 * 256.0 * 256.0; |
| 1431 | |
| 1432 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1433 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1434 | mpz_float(self, args) |
| 1435 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1436 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1437 | #else /* def MPZ_CONVERSIONS_AS_METHODS */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1438 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1439 | mpz_float(self) |
| 1440 | mpzobject *self; |
| 1441 | #endif /* def MPZ_CONVERSIONS_AS_METHODS else */ |
| 1442 | { |
| 1443 | int i, isnegative; |
| 1444 | double x; |
| 1445 | double mulstate; |
| 1446 | MP_INT mpzscratch; |
| 1447 | |
| 1448 | |
| 1449 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1450 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1451 | return NULL; |
| 1452 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1453 | |
| 1454 | i = (int)mpz_size(&self->mpz); |
| 1455 | |
| 1456 | /* determine sign, and copy abs(self) to scratch var */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1457 | if ((isnegative = (mpz_cmp_ui(&self->mpz, (unsigned long int)0) < 0))) |
| 1458 | { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1459 | mpz_init(&mpzscratch); |
| 1460 | mpz_neg(&mpzscratch, &self->mpz); |
| 1461 | } |
| 1462 | else |
| 1463 | mpz_init_set(&mpzscratch, &self->mpz); |
| 1464 | |
| 1465 | /* let those bits come, let those bits go, |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 1466 | e.g. dismantle mpzscratch, build PyFloatObject */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1467 | |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 1468 | /* Can this overflow? Dunno, protect against that possibility. */ |
| 1469 | PyFPE_START_PROTECT("mpz_float", return 0) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1470 | x = 0.0; |
| 1471 | mulstate = 1.0; |
| 1472 | while (i--) { |
| 1473 | x += mulstate * mpz_get_ui(&mpzscratch); |
| 1474 | mulstate *= multiplier; |
| 1475 | mpz_div_2exp(&mpzscratch, &mpzscratch, BITS_PER_MP_LIMB); |
| 1476 | } |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1477 | PyFPE_END_PROTECT(mulstate) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1478 | |
| 1479 | assert(mpz_cmp_ui(&mpzscratch, (unsigned long int)0) == 0); |
| 1480 | mpz_clear(&mpzscratch); |
| 1481 | |
| 1482 | if (isnegative) |
| 1483 | x = -x; |
| 1484 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1485 | return PyFloat_FromDouble(x); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1486 | |
| 1487 | } /* mpz_float() */ |
| 1488 | |
| 1489 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1490 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1491 | mpz_hex(self, args) |
| 1492 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1493 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1494 | #else /* def MPZ_CONVERSIONS_AS_METHODS */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1495 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1496 | mpz_hex(self) |
| 1497 | mpzobject *self; |
| 1498 | #endif /* def MPZ_CONVERSIONS_AS_METHODS else */ |
| 1499 | { |
| 1500 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1501 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1502 | return NULL; |
| 1503 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1504 | |
| 1505 | return mpz_format(self, 16, (unsigned char)1); |
| 1506 | } /* mpz_hex() */ |
| 1507 | |
| 1508 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1509 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1510 | mpz_oct(self, args) |
| 1511 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1512 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1513 | #else /* def MPZ_CONVERSIONS_AS_METHODS */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1514 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1515 | mpz_oct(self) |
| 1516 | mpzobject *self; |
| 1517 | #endif /* def MPZ_CONVERSIONS_AS_METHODS else */ |
| 1518 | { |
| 1519 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1520 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1521 | return NULL; |
| 1522 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
| 1523 | |
| 1524 | return mpz_format(self, 8, (unsigned char)1); |
| 1525 | } /* mpz_oct() */ |
| 1526 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1527 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1528 | mpz_binary(self, args) |
| 1529 | mpzobject *self; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1530 | PyObject *args; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1531 | { |
| 1532 | int size; |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1533 | PyStringObject *strobjp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1534 | char *cp; |
| 1535 | MP_INT mp; |
| 1536 | unsigned long ldigit; |
| 1537 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1538 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1539 | return NULL; |
| 1540 | |
| 1541 | if (mpz_cmp_ui(&self->mpz, (unsigned long int)0) < 0) { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1542 | PyErr_SetString(PyExc_ValueError, |
| 1543 | "mpz.binary() arg must be >= 0"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1544 | return NULL; |
| 1545 | } |
| 1546 | |
| 1547 | mpz_init_set(&mp, &self->mpz); |
| 1548 | size = (int)mpz_size(&mp); |
| 1549 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1550 | if ((strobjp = (PyStringObject *) |
| 1551 | PyString_FromStringAndSize( |
| 1552 | (char *)0, size * sizeof (unsigned long int))) == NULL) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1553 | return NULL; |
| 1554 | |
| 1555 | /* get the beginning of the string memory and start copying things */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1556 | cp = PyString_AS_STRING(strobjp); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1557 | |
| 1558 | /* this has been programmed using a (fairly) decent lib-i/f it could |
| 1559 | be must faster if we looked into the GMP lib */ |
| 1560 | while (size--) { |
| 1561 | ldigit = mpz_get_ui(&mp); |
| 1562 | mpz_div_2exp(&mp, &mp, BITS_PER_MP_LIMB); |
| 1563 | *cp++ = (unsigned char)(ldigit & 0xFF); |
| 1564 | *cp++ = (unsigned char)((ldigit >>= 8) & 0xFF); |
| 1565 | *cp++ = (unsigned char)((ldigit >>= 8) & 0xFF); |
| 1566 | *cp++ = (unsigned char)((ldigit >>= 8) & 0xFF); |
| 1567 | } |
| 1568 | |
| 1569 | while (strobjp->ob_size && !*--cp) |
| 1570 | strobjp->ob_size--; |
| 1571 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1572 | return (PyObject *)strobjp; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1573 | } /* mpz_binary() */ |
| 1574 | |
| 1575 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1576 | static PyMethodDef mpz_methods[] = { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1577 | #ifdef MPZ_CONVERSIONS_AS_METHODS |
| 1578 | {"int", mpz_int}, |
| 1579 | {"long", mpz_long}, |
| 1580 | {"float", mpz_float}, |
| 1581 | {"hex", mpz_hex}, |
| 1582 | {"oct", mpz_oct}, |
| 1583 | #endif /* def MPZ_CONVERSIONS_AS_METHODS */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1584 | {"binary", (PyCFunction)mpz_binary}, |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1585 | {NULL, NULL} /* sentinel */ |
| 1586 | }; |
| 1587 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1588 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1589 | mpz_getattr(self, name) |
| 1590 | mpzobject *self; |
| 1591 | char *name; |
| 1592 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1593 | return Py_FindMethod(mpz_methods, (PyObject *)self, name); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1594 | } /* mpz_getattr() */ |
| 1595 | |
| 1596 | |
| 1597 | static int |
| 1598 | mpz_coerce(pv, pw) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1599 | PyObject **pv; |
| 1600 | PyObject **pw; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1601 | { |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1602 | PyObject *z; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1603 | |
| 1604 | #ifdef MPZ_DEBUG |
| 1605 | fputs("mpz_coerce() called...\n", stderr); |
| 1606 | #endif /* def MPZ_DEBUG */ |
| 1607 | |
| 1608 | assert(is_mpzobject(*pv)); |
| 1609 | |
| 1610 | /* always convert other arg to mpz value, except for floats */ |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1611 | if (!PyFloat_Check(*pw)) { |
| 1612 | if ((z = (PyObject *)mpz_mpzcoerce(*pw)) == NULL) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1613 | return -1; /* -1: an error always has been set */ |
| 1614 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1615 | Py_INCREF(*pv); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1616 | *pw = z; |
| 1617 | } |
| 1618 | else { |
| 1619 | if ((z = mpz_float(*pv, NULL)) == NULL) |
| 1620 | return -1; |
| 1621 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1622 | Py_INCREF(*pw); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1623 | *pv = z; |
| 1624 | } |
| 1625 | return 0; /* coercion succeeded */ |
| 1626 | |
| 1627 | } /* mpz_coerce() */ |
| 1628 | |
| 1629 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1630 | static PyObject * |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1631 | mpz_repr(v) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1632 | PyObject *v; |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1633 | { |
| 1634 | return mpz_format(v, 10, (unsigned char)1); |
| 1635 | } /* mpz_repr() */ |
| 1636 | |
| 1637 | |
| 1638 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1639 | #define UF (unaryfunc) |
| 1640 | #define BF (binaryfunc) |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1641 | #define TF (ternaryfunc) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1642 | #define IF (inquiry) |
| 1643 | #define CF (coercion) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1644 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1645 | static PyNumberMethods mpz_as_number = { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1646 | BF mpz_addition, /*nb_add*/ |
| 1647 | BF mpz_substract, /*nb_subtract*/ |
| 1648 | BF mpz_multiply, /*nb_multiply*/ |
| 1649 | BF mpz_divide, /*nb_divide*/ |
| 1650 | BF mpz_remainder, /*nb_remainder*/ |
| 1651 | BF mpz_div_and_mod, /*nb_divmod*/ |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 1652 | TF mpz_power, /*nb_power*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1653 | UF mpz_negative, /*nb_negative*/ |
| 1654 | UF mpz_positive, /*tp_positive*/ |
| 1655 | UF mpz_absolute, /*tp_absolute*/ |
| 1656 | IF mpz_nonzero, /*tp_nonzero*/ |
Guido van Rossum | 272841c | 1996-08-19 23:06:45 +0000 | [diff] [blame] | 1657 | UF py_mpz_invert, /*nb_invert*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1658 | BF mpz_lshift, /*nb_lshift*/ |
| 1659 | BF mpz_rshift, /*nb_rshift*/ |
| 1660 | BF mpz_andfunc, /*nb_and*/ |
| 1661 | BF mpz_xorfunc, /*nb_xor*/ |
| 1662 | BF mpz_orfunc, /*nb_or*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1663 | CF mpz_coerce, /*nb_coerce*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1664 | #ifndef MPZ_CONVERSIONS_AS_METHODS |
| 1665 | UF mpz_int, /*nb_int*/ |
| 1666 | UF mpz_long, /*nb_long*/ |
| 1667 | UF mpz_float, /*nb_float*/ |
| 1668 | UF mpz_oct, /*nb_oct*/ |
| 1669 | UF mpz_hex, /*nb_hex*/ |
| 1670 | #endif /* ndef MPZ_CONVERSIONS_AS_METHODS */ |
| 1671 | }; |
| 1672 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1673 | static PyTypeObject MPZtype = { |
| 1674 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1675 | 0, /*ob_size*/ |
| 1676 | "mpz", /*tp_name*/ |
| 1677 | sizeof(mpzobject), /*tp_size*/ |
| 1678 | 0, /*tp_itemsize*/ |
| 1679 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1680 | (destructor)mpz_dealloc, /*tp_dealloc*/ |
| 1681 | 0, /*tp_print*/ |
| 1682 | (getattrfunc)mpz_getattr, /*tp_getattr*/ |
| 1683 | 0, /*tp_setattr*/ |
| 1684 | (cmpfunc)mpz_compare, /*tp_compare*/ |
| 1685 | (reprfunc)mpz_repr, /*tp_repr*/ |
| 1686 | &mpz_as_number, /*tp_as_number*/ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1687 | }; |
| 1688 | |
| 1689 | /* List of functions exported by this module */ |
| 1690 | |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1691 | static PyMethodDef mpz_functions[] = { |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1692 | #if 0 |
| 1693 | {initialiser_name, MPZ_mpz}, |
| 1694 | #else /* 0 */ |
Barry Warsaw | abb7efe | 1996-12-09 23:22:35 +0000 | [diff] [blame] | 1695 | /* until guido ``fixes'' struct PyMethodDef */ |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1696 | {(char *)initialiser_name, MPZ_mpz}, |
| 1697 | #endif /* 0 else */ |
| 1698 | {"powm", MPZ_powm}, |
| 1699 | {"gcd", MPZ_gcd}, |
| 1700 | {"gcdext", MPZ_gcdext}, |
| 1701 | {"sqrt", MPZ_sqrt}, |
| 1702 | {"sqrtrem", MPZ_sqrtrem}, |
| 1703 | {"divm", MPZ_divm}, |
| 1704 | {NULL, NULL} /* Sentinel */ |
| 1705 | }; |
| 1706 | |
| 1707 | |
| 1708 | /* #define MP_TEST_ALLOC */ |
| 1709 | |
| 1710 | #ifdef MP_TEST_ALLOC |
| 1711 | #define MP_TEST_SIZE 4 |
| 1712 | static const char mp_test_magic[MP_TEST_SIZE] = {'\xAA','\xAA','\xAA','\xAA'}; |
| 1713 | static mp_test_error( location ) |
| 1714 | int *location; |
| 1715 | { |
| 1716 | /* assumptions: *alloc returns address dividable by 4, |
| 1717 | mpz_* routines allocate in chunks dividable by four */ |
| 1718 | fprintf(stderr, "MP_TEST_ERROR: location holds 0x%08d\n", *location ); |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1719 | Py_FatalError("MP_TEST_ERROR"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1720 | } /* static mp_test_error() */ |
| 1721 | #define MP_EXTRA_ALLOC(size) ((size) + MP_TEST_SIZE) |
| 1722 | #define MP_SET_TEST(basep,size) (void)memcpy( ((char *)(basep))+(size), mp_test_magic, MP_TEST_SIZE) |
| 1723 | #define MP_DO_TEST(basep,size) if ( !memcmp( ((char *)(basep))+(size), mp_test_magic, MP_TEST_SIZE ) ) \ |
| 1724 | ; \ |
| 1725 | else \ |
| 1726 | mp_test_error((int *)((char *)(basep) + size)) |
| 1727 | #else /* def MP_TEST_ALLOC */ |
| 1728 | #define MP_EXTRA_ALLOC(size) (size) |
| 1729 | #define MP_SET_TEST(basep,size) |
| 1730 | #define MP_DO_TEST(basep,size) |
| 1731 | #endif /* def MP_TEST_ALLOC else */ |
| 1732 | |
| 1733 | void *mp_allocate( alloc_size ) |
| 1734 | size_t alloc_size; |
| 1735 | { |
| 1736 | void *res; |
| 1737 | |
| 1738 | #ifdef MPZ_DEBUG |
| 1739 | fprintf(stderr, "mp_allocate : size %ld\n", |
| 1740 | alloc_size); |
| 1741 | #endif /* def MPZ_DEBUG */ |
| 1742 | |
| 1743 | if ( (res = malloc(MP_EXTRA_ALLOC(alloc_size))) == NULL ) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1744 | Py_FatalError("mp_allocate failure"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1745 | |
| 1746 | #ifdef MPZ_DEBUG |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1747 | fprintf(stderr, "mp_allocate : address %08p\n", res); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1748 | #endif /* def MPZ_DEBUG */ |
| 1749 | |
| 1750 | MP_SET_TEST(res,alloc_size); |
| 1751 | |
| 1752 | return res; |
| 1753 | } /* mp_allocate() */ |
| 1754 | |
| 1755 | |
| 1756 | void *mp_reallocate( ptr, old_size, new_size ) |
| 1757 | void *ptr; |
| 1758 | size_t old_size; |
| 1759 | size_t new_size; |
| 1760 | { |
| 1761 | void *res; |
| 1762 | |
| 1763 | #ifdef MPZ_DEBUG |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1764 | fprintf(stderr, "mp_reallocate: old address %08p, old size %ld\n", |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1765 | ptr, old_size); |
| 1766 | #endif /* def MPZ_DEBUG */ |
| 1767 | |
| 1768 | MP_DO_TEST(ptr, old_size); |
| 1769 | |
| 1770 | if ( (res = realloc(ptr, MP_EXTRA_ALLOC(new_size))) == NULL ) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1771 | Py_FatalError("mp_reallocate failure"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1772 | |
| 1773 | #ifdef MPZ_DEBUG |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1774 | fprintf(stderr, "mp_reallocate: new address %08p, new size %ld\n", |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1775 | res, new_size); |
| 1776 | #endif /* def MPZ_DEBUG */ |
| 1777 | |
| 1778 | MP_SET_TEST(res, new_size); |
| 1779 | |
| 1780 | return res; |
| 1781 | } /* mp_reallocate() */ |
| 1782 | |
| 1783 | |
| 1784 | void mp_free( ptr, size ) |
| 1785 | void *ptr; |
| 1786 | size_t size; |
| 1787 | { |
| 1788 | |
| 1789 | #ifdef MPZ_DEBUG |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1790 | fprintf(stderr, "mp_free : old address %08p, old size %ld\n", |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1791 | ptr, size); |
| 1792 | #endif /* def MPZ_DEBUG */ |
| 1793 | |
| 1794 | MP_DO_TEST(ptr, size); |
| 1795 | free(ptr); |
| 1796 | } /* mp_free() */ |
| 1797 | |
| 1798 | |
| 1799 | |
| 1800 | /* Initialize this module. */ |
| 1801 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1802 | DL_EXPORT(void) |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1803 | initmpz() |
| 1804 | { |
Fred Drake | fcc6c68 | 1998-04-03 15:33:43 +0000 | [diff] [blame] | 1805 | PyObject *module; |
| 1806 | PyObject *dict; |
| 1807 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1808 | #ifdef MPZ_DEBUG |
| 1809 | fputs( "initmpz() called...\n", stderr ); |
| 1810 | #endif /* def MPZ_DEBUG */ |
| 1811 | |
| 1812 | mp_set_memory_functions( mp_allocate, mp_reallocate, mp_free ); |
Fred Drake | fcc6c68 | 1998-04-03 15:33:43 +0000 | [diff] [blame] | 1813 | module = Py_InitModule("mpz", mpz_functions); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1814 | |
| 1815 | /* create some frequently used constants */ |
| 1816 | if ((mpz_value_zero = newmpzobject()) == NULL) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1817 | Py_FatalError("initmpz: can't initialize mpz constants"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1818 | mpz_set_ui(&mpz_value_zero->mpz, (unsigned long int)0); |
| 1819 | |
| 1820 | if ((mpz_value_one = newmpzobject()) == NULL) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1821 | Py_FatalError("initmpz: can't initialize mpz constants"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1822 | mpz_set_ui(&mpz_value_one->mpz, (unsigned long int)1); |
| 1823 | |
| 1824 | if ((mpz_value_mone = newmpzobject()) == NULL) |
Barry Warsaw | 3bdf746 | 1996-12-09 23:16:31 +0000 | [diff] [blame] | 1825 | Py_FatalError("initmpz: can't initialize mpz constants"); |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1826 | mpz_set_si(&mpz_value_mone->mpz, (long)-1); |
| 1827 | |
Fred Drake | fcc6c68 | 1998-04-03 15:33:43 +0000 | [diff] [blame] | 1828 | dict = PyModule_GetDict(module); |
| 1829 | if (dict != NULL) { |
| 1830 | PyDict_SetItemString(dict, "MPZType", (PyObject*)&MPZtype); |
| 1831 | } |
| 1832 | |
Guido van Rossum | 5f59d60 | 1992-12-14 16:59:51 +0000 | [diff] [blame] | 1833 | } /* initmpz() */ |
| 1834 | #ifdef MAKEDUMMYINT |
| 1835 | int _mpz_dummy_int; /* XXX otherwise, we're .bss-less (DYNLOAD->Jack?) */ |
| 1836 | #endif /* def MAKEDUMMYINT */ |