/ src / gmpy2.h
gmpy2.h
  1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2   * gmpy2.h                                                                 *
  3   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4   * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision   *
  5   * libraries.                                                              *
  6   *                                                                         *
  7   * Copyright 2000 - 2009 Alex Martelli                                     *
  8   *                                                                         *
  9   * Copyright 2008 - 2021 Case Van Horsen                                   *
 10   *                                                                         *
 11   * This file is part of GMPY2.                                             *
 12   *                                                                         *
 13   * GMPY2 is free software: you can redistribute it and/or modify it under  *
 14   * the terms of the GNU Lesser General Public License as published by the  *
 15   * Free Software Foundation, either version 3 of the License, or (at your  *
 16   * option) any later version.                                              *
 17   *                                                                         *
 18   * GMPY2 is distributed in the hope that it will be useful, but WITHOUT    *
 19   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or   *
 20   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public    *
 21   * License for more details.                                               *
 22   *                                                                         *
 23   * You should have received a copy of the GNU Lesser General Public        *
 24   * License along with GMPY2; if not, see <http://www.gnu.org/licenses/>    *
 25   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 26  
 27  
 28  /*
 29    gmpy C API extension header file.
 30    Part of Python's gmpy module since version 0.4
 31  
 32    Created by Pearu Peterson <pearu@cens.ioc.ee>, November 2000.
 33    Edited by A. Martelli <aleaxit@yahoo.com>, December 2000.
 34    Edited by Case Van Horsen <casevh@gmail.com>, 2009, 2010, 2011.
 35  
 36    Version 1.02, February 2007.
 37    Version 1.03, June 2008
 38    Version 1.04, June 2008 (no changes)
 39    Version 1.05, February 2009 (support MPIR)
 40    Version 1.20, January 2010 (remove obsolete MS hacks) casevh
 41    Version 2.00, April 2010 (change to gmpy2) casevh
 42                  October 2010 (added Py_hash_t) casevh
 43                  December 2010 (added mpfr, mpc) casevh
 44                  January 2011 (add Pygmpy_context) casevh
 45                  April 2011 (split into multiple files) casevh
 46    Version 2.10  August 2014 (reflect major rewrite during 2013/2014) casevh
 47   */
 48  
 49  #ifndef Py_GMPYMODULE_H
 50  #define Py_GMPYMODULE_H
 51  
 52  #ifdef __cplusplus
 53  extern "C" {
 54  #endif
 55  
 56  /* Structure of gmpy2.h
 57   *
 58   * Revised 17-APR-2017 casevh
 59   *
 60   * 1. Checks for specific Python versions.
 61   * 2. Include headers for GMP/MPIR, MPFR, and MPC.
 62   * 3. Define public C-API.
 63   *    1. Define gmpy2 types.
 64   *    2. Define the public API.
 65   *
 66   */
 67  
 68  /* Check for minimum Python version requirements. */
 69  
 70  #if PY_VERSION_HEX < 0x02060000
 71  #  error "GMPY2 requires Python 2.6 or later."
 72  #endif
 73  
 74  /* Include headers for GMP/MPIR, MPFR, and MPC. */
 75  
 76  #ifdef MPIR
 77  #  include <mpir.h>
 78  #else
 79  #  include <gmp.h>
 80  #endif
 81  
 82  #include <mpfr.h>
 83  #include <mpc.h>
 84  
 85  /* Check MPFR and MPC versions. */
 86  
 87  #if (!defined(MPC_VERSION) || (MPC_VERSION < MPC_VERSION_NUM(1,0,3)))
 88  #  error "GMPY2 requires MPC 1.0.3 or later."
 89  #endif
 90  
 91  #if (defined(MPC_VERSION) && (MPC_VERSION >= MPC_VERSION_NUM(1,1,0)))
 92  #  define MPC_110
 93  #endif
 94  
 95  
 96  #if PY_VERSION_HEX < 0x030200A4
 97  typedef long Py_hash_t;
 98  typedef unsigned long Py_uhash_t;
 99  #  define _PyHASH_IMAG 1000003
100  #endif
101  
102  /* GMPY2 Public API */
103  
104  /* Types
105   *    MPZ_Object
106   *    XMPZ_Object        (mutable version of MPZ_Object)
107   *    MPQ_Object
108   *    XMPQ_Object        (mutable version of MPQ_Object)
109   *    MPFR_Object
110   *    XMPFR_Object       (mutable version of MPFR_Object)
111   *    MPC_Object
112   *    XMPC_Object        (mutable version of MPC_Object)
113   *    CTXT_Object
114   *    CTXT_Manager_Object
115   *    RandomState_Object
116   */
117  
118  typedef struct {
119      PyObject_HEAD
120      mpz_t z;
121      Py_hash_t hash_cache;
122  } MPZ_Object;
123  
124  typedef struct {
125      PyObject_HEAD
126      mpz_t z;
127  } XMPZ_Object;
128  
129  typedef struct {
130      PyObject_HEAD
131      mpq_t q;
132      Py_hash_t  hash_cache;
133  } MPQ_Object;
134  
135  typedef struct {
136      PyObject_HEAD
137      mpfr_t f;
138      Py_hash_t hash_cache;
139      int rc;
140  } MPFR_Object;
141  
142  typedef struct {
143      PyObject_HEAD
144      mpc_t c;
145      Py_hash_t hash_cache;
146      int rc;
147  } MPC_Object;
148  
149  typedef struct {
150      PyObject_HEAD
151      gmp_randstate_t state;
152  } RandomState_Object;
153  
154  typedef struct {
155      mpfr_prec_t mpfr_prec;   /* current precision in bits, for MPFR */
156      mpfr_rnd_t mpfr_round;   /* current rounding mode for float (MPFR) */
157      mpfr_exp_t emax;         /* maximum exponent */
158      mpfr_exp_t emin;         /* minimum exponent */
159      int subnormalize;        /* if 1, subnormalization is performed */
160      int underflow;           /* did an underflow occur? */
161      int overflow;            /* did an overflow occur? */
162      int inexact;             /* was the result inexact? */
163      int invalid;             /* invalid operation (i.e. NaN)? */
164      int erange;              /* did a range error occur? */
165      int divzero;             /* divided by zero? */
166      int traps;               /* if 0, do not trap any exceptions */
167                               /* if not 0, then raise traps per bits above  */
168      mpfr_prec_t real_prec;   /* current precision in bits, for Re(MPC) */
169      mpfr_prec_t imag_prec;   /* current precision in bits, for Im(MPC) */
170      mpfr_rnd_t real_round;   /* current rounding mode for Re(MPC) */
171      mpfr_rnd_t imag_round;   /* current rounding mode for Im(MPC) */
172      int allow_complex;       /* if 1, allow mpfr functions to return an mpc */
173      int rational_division;   /* if 1, mpz/mpz returns an mpq result */
174  } gmpy_context;
175  
176  typedef struct {
177      PyObject_HEAD
178      gmpy_context ctx;
179  #ifndef WITHOUT_THREADS
180      PyThreadState *tstate;
181  #endif
182  } CTXT_Object;
183  
184  typedef struct {
185      PyObject_HEAD
186      CTXT_Object *new_context; /* Context that will be returned when
187                                 * __enter__ is called. */
188      CTXT_Object *old_context; /* Context that will restored when
189                                 * __exit__ is called. */
190  } CTXT_Manager_Object;
191  
192  #define MPZ(obj)  (((MPZ_Object*)(obj))->z)
193  #define MPQ(obj)  (((MPQ_Object*)(obj))->q)
194  #define MPFR(obj) (((MPFR_Object*)(obj))->f)
195  #define MPC(obj)  (((MPC_Object*)(obj))->c)
196  
197  /* Start of the C-API definitions */
198  
199  #define MPZ_Type_NUM          0
200  #define XMPZ_Type_NUM         1
201  #define MPQ_Type_NUM          2
202  #define XMPQ_Type_NUM         3
203  #define MPFR_Type_NUM         4
204  #define XMPFR_Type_NUM        5
205  #define MPC_Type_NUM          6
206  #define XMPC_Type_NUM         7
207  #define CTXT_Type_NUM         8
208  #define CTXT_Manager_Type_NUM 9
209  #define RandomState_Type_NUM  10
210  
211  /* The following functions are found in gmpy2_cache. */
212  
213  #define GMPy_MPZ_New_NUM            11
214  #define GMPy_MPZ_New_RETURN         MPZ_Object *
215  #define GMPy_MPZ_New_PROTO          (CTXT_Object *context)
216  
217  #define GMPy_MPZ_NewInit_NUM        12
218  #define GMPy_MPZ_NewInit_RETURN     PyObject *
219  #define GMPy_MPZ_NewInit_PROTO      (PyTypeObject *type, PyObject *args, PyObject *keywds)
220  
221  #define GMPy_MPZ_Dealloc_NUM        13
222  #define GMPy_MPZ_Dealloc_RETURN     void
223  #define GMPy_MPZ_Dealloc_PROTO      (MPZ_Object *self)
224  
225  /* The following function is found in gmpy2_convert_gmp. */
226  
227  #define GMPy_MPZ_ConvertArg_NUM     14
228  #define GMPy_MPZ_ConvertArg_RETURN  int
229  #define GMPy_MPZ_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
230  
231  /* The following functions are found in gmpy2_cache. */
232  
233  #define GMPy_XMPZ_New_NUM           15
234  #define GMPy_XMPZ_New_RETURN        XMPZ_Object *
235  #define GMPy_XMPZ_New_PROTO         (CTXT_Object *context)
236  
237  #define GMPy_XMPZ_NewInit_NUM       16
238  #define GMPy_XMPZ_NewInit_RETURN    PyObject *
239  #define GMPy_XMPZ_NewInit_PROTO     (PyTypeObject *type, PyObject *args, PyObject *keywds)
240  
241  #define GMPy_XMPZ_Dealloc_NUM       17
242  #define GMPy_XMPZ_Dealloc_RETURN    void
243  #define GMPy_XMPZ_Dealloc_PROTO     (XMPZ_Object *self)
244  
245  /* The following functions are found in gmpy2_cache. */
246  
247  #define GMPy_MPQ_New_NUM            18
248  #define GMPy_MPQ_New_RETURN         MPQ_Object *
249  #define GMPy_MPQ_New_PROTO          (CTXT_Object *context)
250  
251  #define GMPy_MPQ_NewInit_NUM        19
252  #define GMPy_MPQ_NewInit_RETURN     PyObject *
253  #define GMPy_MPQ_NewInit_PROTO      (PyTypeObject *type, PyObject *args, PyObject *keywds)
254  
255  #define GMPy_MPQ_Dealloc_NUM        20
256  #define GMPy_MPQ_Dealloc_RETURN     void
257  #define GMPy_MPQ_Dealloc_PROTO      (MPQ_Object *self)
258  
259  /* The following function is found in gmpy2_convert_gmp. */
260  
261  #define GMPy_MPQ_ConvertArg_NUM     21
262  #define GMPy_MPQ_ConvertArg_RETURN  int
263  #define GMPy_MPQ_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
264  
265  /* The following functions are found in gmpy2_cache. */
266  
267  #define GMPy_MPFR_New_NUM           22
268  #define GMPy_MPFR_New_RETURN        MPFR_Object *
269  #define GMPy_MPFR_New_PROTO         (mpfr_prec_t bits, CTXT_Object *context)
270  
271  #define GMPy_MPFR_NewInit_NUM       23
272  #define GMPy_MPFR_NewInit_RETURN    PyObject *
273  #define GMPy_MPFR_NewInit_PROTO     (PyTypeObject *type, PyObject *args, PyObject *keywds)
274  
275  #define GMPy_MPFR_Dealloc_NUM       24
276  #define GMPy_MPFR_Dealloc_RETURN    void
277  #define GMPy_MPFR_Dealloc_PROTO     (MPFR_Object *self)
278  
279  /* The following function is found in gmpy2_convert_gmp. */
280  
281  #define GMPy_MPFR_ConvertArg_NUM    25
282  #define GMPy_MPFR_ConvertArg_RETURN int
283  #define GMPy_MPFR_ConvertArg_PROTO  (PyObject *arg, PyObject **ptr)
284  
285  /* The following functions are found in gmpy2_cache. */
286  
287  #define GMPy_MPC_New_NUM             26
288  #define GMPy_MPC_New_RETURN          MPC_Object *
289  #define GMPy_MPC_New_PROTO           (mpfr_prec_t rprec, mpfr_prec_t iprec, CTXT_Object *context)
290  
291  #define GMPy_MPC_NewInit_NUM         27
292  #define GMPy_MPC_NewInit_RETURN      PyObject *
293  #define GMPy_MPC_NewInit_PROTO       (PyTypeObject *type, PyObject *args, PyObject *keywds)
294  
295  #define GMPy_MPC_Dealloc_NUM        28
296  #define GMPy_MPC_Dealloc_RETURN     void
297  #define GMPy_MPC_Dealloc_PROTO      (MPC_Object *self)
298  
299  /* The following function is found in gmpy2_convert_gmp. */
300  
301  #define GMPy_MPC_ConvertArg_NUM     29
302  #define GMPy_MPC_ConvertArg_RETURN  int
303  #define GMPy_MPC_ConvertArg_PROTO   (PyObject *arg, PyObject **ptr)
304  
305  /* Total number of C-API pointers. */
306  
307  #define GMPy_API_pointers 30
308  
309  /* End of C-API definitions. */
310  
311  #ifdef GMPY2_MODULE
312  
313  /* Define various macros to deal with differences between Python 2 and 3. */
314  
315  #if (PY_MAJOR_VERSION == 3)
316  #define PY3
317  #define Py2or3String_FromString     PyUnicode_FromString
318  #define Py2or3String_FromFormat     PyUnicode_FromFormat
319  #define Py2or3String_Check          PyUnicode_Check
320  #define Py2or3String_Format         PyUnicode_Format
321  #define Py2or3String_Type           Py_UCS4
322  #define Py2or3String_1Char(obj)     (PyUnicode_READY(obj) ? (Py_UCS4)0 : PyUnicode_READ_CHAR(obj, 0))
323  #define PyStrOrUnicode_Check(op)    (PyBytes_Check(op) || PyUnicode_Check(op))
324  #define PyIntOrLong_FromLong        PyLong_FromLong
325  #define PyIntOrLong_Check(op)       (PyLong_Check(op))
326  #define PyIntOrLong_CheckExact(op)  PyLong_CheckExact(op)
327  #define PyIntOrLong_FromSize_t      PyLong_FromSize_t
328  #define PyIntOrLong_FromSsize_t     PyLong_FromSsize_t
329  #define PyIntOrLong_AsSsize_t       PyLong_AsSsize_t
330  #define PyIntOrLong_AsLong          PyLong_AsLong
331  #else
332  #define PY2
333  #define Py2or3String_FromString     PyString_FromString
334  #define Py2or3String_FromFormat     PyString_FromFormat
335  #define Py2or3String_Check          PyString_Check
336  #define Py2or3String_Format         PyString_Format
337  #define Py2or3String_Type           char
338  #define Py2or3String_1Char(obj)     PyString_AsString(obj)[0]
339  #define PyStrOrUnicode_Check(op)    (PyString_Check(op) || PyUnicode_Check(op))
340  #define PyIntOrLong_FromLong        PyInt_FromLong
341  #define PyIntOrLong_Check(op)       (PyInt_Check(op) || PyLong_Check(op))
342  #define PyIntOrLong_CheckExact(op)  (PyInt_CheckExact(op) || PyLong_CheckExact(op))
343  #define PyIntOrLong_FromSize_t      PyInt_FromSize_t
344  #define PyIntOrLong_FromSsize_t     PyInt_FromSsize_t
345  #define PyIntOrLong_AsSsize_t       PyInt_AsSsize_t
346  #define PyIntOrLong_AsLong          PyInt_AsLong
347  #endif
348  
349  #ifndef ABS
350  #  define ABS(a)  (((a) < 0) ? -(a) : (a))
351  #endif
352  
353  #if defined(MS_WIN32) && defined(_MSC_VER)
354     /* so one won't need to link explicitly to gmp.lib...: */
355  #  if defined(MPIR)
356  #    pragma comment(lib,"mpir.lib")
357  #  else
358  #    pragma comment(lib,"gmp.lib")
359  #  endif
360  #  define USE_ALLOCA 1
361  #  define inline __inline
362  #endif
363  
364  #ifdef __GNUC__
365  #  define USE_ALLOCA 1
366  #endif
367  
368  #ifndef alloca
369  # ifdef __GNUC__
370  #  define alloca __builtin_alloca
371  # else
372  #   ifdef _MSC_VER
373  #    include <malloc.h>
374  #    define alloca _alloca
375  #   else
376  #    if HAVE_ALLOCA_H
377  #     include <alloca.h>
378  #    else
379         char *alloca ();
380  #    endif
381  #   endif
382  # endif
383  #endif
384  
385  #define ALLOC_THRESHOLD 8192
386  
387  #define INDEX_ERROR(msg)    PyErr_SetString(PyExc_IndexError, msg)
388  #define TYPE_ERROR(msg)     PyErr_SetString(PyExc_TypeError, msg)
389  #define VALUE_ERROR(msg)    PyErr_SetString(PyExc_ValueError, msg)
390  #define ZERO_ERROR(msg)     PyErr_SetString(PyExc_ZeroDivisionError, msg)
391  #define SYSTEM_ERROR(msg)   PyErr_SetString(PyExc_SystemError, msg)
392  #define OVERFLOW_ERROR(msg) PyErr_SetString(PyExc_OverflowError, msg)
393  #define RUNTIME_ERROR(msg)  PyErr_SetString(PyExc_RuntimeError, msg)
394  
395  #define GMPY_DEFAULT -1
396  
397  /* To prevent excessive memory usage, we don't want to save very large
398   * numbers in the cache. The default value specified in the options
399   * structure is 128 words (512 bytes on 32-bit platforms, 1024 bytes on
400   * 64-bit platforms).
401   */
402  #define MAX_CACHE_LIMBS 16384
403  
404  /* The maximum number of objects that can be saved in a cache is specified
405   * here. The default value is 100.*/
406  #define MAX_CACHE 1000
407  
408  #ifdef USE_ALLOCA
409  #  define TEMP_ALLOC(B, S)     \
410      if(S < ALLOC_THRESHOLD) {  \
411          B = alloca(S);         \
412      } else {                   \
413          if(!(B = malloc(S))) { \
414              PyErr_NoMemory();  \
415              return NULL;       \
416          }                      \
417      }
418  #  define TEMP_FREE(B, S) if(S >= ALLOC_THRESHOLD) free(B)
419  #else
420  #  define TEMP_ALLOC(B, S)     \
421      if(!(B = malloc(S)))  {    \
422          PyErr_NoMemory();      \
423          return NULL;           \
424      }
425  #  define TEMP_FREE(B, S) free(B)
426  #endif
427  
428  /* Various defs to mask differences between Python versions. */
429  
430  #define Py_RETURN_NOTIMPLEMENTED \
431      return Py_INCREF(Py_NotImplemented), Py_NotImplemented
432  
433  #ifndef Py_SIZE
434  #  define Py_SIZE(ob)     (((PyVarObject*)(ob))->ob_size)
435  #endif
436  
437  #ifndef Py_TYPE
438  #  define Py_TYPE(ob)     (((PyObject*)(ob))->ob_type)
439  #endif
440  
441  /* Import a collection of general purpose macros. */
442  
443  #include "gmpy2_macros.h"
444  
445  /* Import the files that complete the definition of the types defined above. */
446  
447  #include "gmpy2_mpz.h"
448  #include "gmpy2_xmpz.h"
449  #include "gmpy2_mpq.h"
450  #include "gmpy2_mpfr.h"
451  #include "gmpy2_mpc.h"
452  #include "gmpy2_context.h"
453  #include "gmpy2_random.h"
454  
455  /* Import the header files that provide the various functions. */
456  
457  /* Support object caching, creation, and deletion. */
458  
459  #include "gmpy2_cache.h"
460  
461  /* Suport for miscellaneous functions (ie. version, license, etc.). */
462  
463  #include "gmpy2_misc.h"
464  
465  /* Support conversion to/from binary format. */
466  
467  #include "gmpy2_binary.h"
468  
469  /* Support for mpz/xmpz specific functions. */
470  
471  #include "gmpy2_convert.h"
472  #include "gmpy2_convert_utils.h"
473  #include "gmpy2_convert_gmp.h"
474  #include "gmpy2_convert_mpfr.h"
475  #include "gmpy2_convert_mpc.h"
476  
477  #include "gmpy2_mpz_divmod.h"
478  #include "gmpy2_mpz_divmod2exp.h"
479  #include "gmpy2_mpz_pack.h"
480  #include "gmpy2_mpz_bitops.h"
481  #include "gmpy2_mpz_inplace.h"
482  #include "gmpy2_mpz_misc.h"
483  
484  #include "gmpy2_xmpz_inplace.h"
485  #include "gmpy2_xmpz_misc.h"
486  #include "gmpy2_xmpz_limbs.h"
487  
488  /* Support for mpq specific functions. */
489  
490  #include "gmpy2_mpq_misc.h"
491  
492  /* Support for mpfr specific functions. */
493  
494  #include "gmpy2_mpfr_misc.h"
495  
496  /* Support for mpc specific functions. */
497  
498  #include "gmpy2_mpc_misc.h"
499  
500  /* Support Lucas sequences. */
501  
502  #include "gmpy_mpz_lucas.h"
503  
504  /* Support probable-prime tests. */
505  
506  #include "gmpy_mpz_prp.h"
507  
508  /* Support higher-level Python methods and functions; generally not
509   * specific to a single type.
510   */
511  
512  #include "gmpy2_abs.h"
513  #include "gmpy2_add.h"
514  #include "gmpy2_divmod.h"
515  #include "gmpy2_floordiv.h"
516  #include "gmpy2_minus.h"
517  #include "gmpy2_mod.h"
518  #include "gmpy2_mul.h"
519  #include "gmpy2_plus.h"
520  #include "gmpy2_pow.h"
521  #include "gmpy2_sub.h"
522  #include "gmpy2_truediv.h"
523  #include "gmpy2_math.h"
524  #include "gmpy2_const.h"
525  #include "gmpy2_square.h"
526  #include "gmpy2_format.h"
527  #include "gmpy2_hash.h"
528  #include "gmpy2_fused.h"
529  #include "gmpy2_muldiv_2exp.h"
530  #include "gmpy2_predicate.h"
531  #include "gmpy2_sign.h"
532  #include "gmpy2_richcompare.h"
533  #include "gmpy2_cmp.h"
534  
535  #ifdef VECTOR
536  #  include "gmpy2_vector.h"
537  #endif /* defined(VECTOR) */
538  
539  #else /* defined(GMPY2_MODULE) */
540  
541  /* This section is used for other C-coded modules that use gmpy2's API. */
542  
543  static void **GMPy_C_API;
544  
545  #define MPZ_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPZ_Type_NUM])
546  #define XMPZ_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPZ_Type_NUM])
547  #define MPQ_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPQ_Type_NUM])
548  #define XMPQ_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPQ_Type_NUM])
549  #define MPFR_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPFR_Type_NUM])
550  #define XMPFR_Check(op)  ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPFR_Type_NUM])
551  #define MPC_Check(op)    ((op)->ob_type == (PyTypeObject*)GMPy_C_API[MPC_Type_NUM])
552  #define XMPC_Check(op)   ((op)->ob_type == (PyTypeObject*)GMPy_C_API[XMPC_Type_NUM])
553  
554  #define GMPy_MPZ_New         (*(GMPy_MPZ_New_RETURN         (*)GMPy_MPZ_New_PROTO)         GMPy_C_API[GMPy_MPZ_New_NUM])
555  #define GMPy_MPZ_NewInit     (*(GMPy_MPZ_NewInit_RETURN     (*)GMPy_MPZ_NewInit_PROTO)     GMPy_C_API[GMPy_MPZ_NewInit_NUM])
556  #define GMPy_MPZ_Dealloc     (*(GMPy_MPZ_Dealloc_RETURN     (*)GMPy_MPZ_Dealloc_PROTO)     GMPy_C_API[GMPy_MPZ_Dealloc_NUM])
557  #define GMPy_MPZ_ConvertArg  (*(GMPy_MPZ_ConvertArg_RETURN  (*)GMPy_MPZ_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPZ_ConvertArg_NUM])
558  
559  #define GMPy_XMPZ_New        (*(GMPy_XMPZ_New_RETURN        (*)GMPy_XMPZ_New_PROTO)        GMPy_C_API[GMPy_XMPZ_New_NUM])
560  #define GMPy_XMPZ_NewInit    (*(GMPy_XMPZ_NewInit_RETURN    (*)GMPy_XMPZ_NewInit_PROTO)    GMPy_C_API[GMPy_XMPZ_NewInit_NUM])
561  #define GMPy_XMPZ_Dealloc    (*(GMPy_XMPZ_Dealloc_RETURN    (*)GMPy_XMPZ_Dealloc_PROTO)    GMPy_C_API[GMPy_XMPZ_Dealloc_NUM])
562  
563  #define GMPy_MPQ_New         (*(GMPy_MPQ_New_RETURN         (*)GMPy_MPQ_New_PROTO)         GMPy_C_API[GMPy_MPQ_New_NUM])
564  #define GMPy_MPQ_NewInit     (*(GMPy_MPQ_NewInit_RETURN     (*)GMPy_MPQ_NewInit_PROTO)     GMPy_C_API[GMPy_MPQ_NewInit_NUM])
565  #define GMPy_MPQ_Dealloc     (*(GMPy_MPQ_Dealloc_RETURN     (*)GMPy_MPQ_Dealloc_PROTO)     GMPy_C_API[GMPy_MPQ_Dealloc_NUM])
566  #define GMPy_MPQ_ConvertArg  (*(GMPy_MPQ_ConvertArg_RETURN  (*)GMPy_MPQ_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPQ_ConvertArg_NUM])
567  
568  #define GMPy_MPFR_New        (*(GMPy_MPFR_New_RETURN        (*)GMPy_MPFR_New_PROTO)        GMPy_C_API[GMPy_MPFR_New_NUM])
569  #define GMPy_MPFR_NewInit    (*(GMPy_MPFR_NewInit_RETURN    (*)GMPy_MPFR_NewInit_PROTO)    GMPy_C_API[GMPy_MPFR_NewInit_NUM])
570  #define GMPy_MPFR_Dealloc    (*(GMPy_MPFR_Dealloc_RETURN    (*)GMPy_MPFR_Dealloc_PROTO)    GMPy_C_API[GMPy_MPFR_Dealloc_NUM])
571  #define GMPy_MPFR_ConvertArg (*(GMPy_MPFR_ConvertArg_RETURN (*)GMPy_MPFR_ConvertArg_PROTO) GMPy_C_API[GMPy_MPFR_ConvertArg_NUM])
572  
573  #define GMPy_MPC_New         (*(GMPy_MPC_New_RETURN         (*)GMPy_MPC_New_PROTO)         GMPy_C_API[GMPy_MPC_New_NUM])
574  #define GMPy_MPC_NewInit     (*(GMPy_MPC_NewInit_RETURN     (*)GMPy_MPC_NewInit_PROTO)     GMPy_C_API[GMPy_MPC_NewInit_NUM])
575  #define GMPy_MPC_Dealloc     (*(GMPy_MPC_Dealloc_RETURN     (*)GMPy_MPC_Dealloc_PROTO)     GMPy_C_API[GMPy_MPC_Dealloc_NUM])
576  #define GMPy_MPC_ConvertArg  (*(GMPy_MPC_ConvertArg_RETURN  (*)GMPy_MPC_ConvertArg_PROTO)  GMPy_C_API[GMPy_MPC_ConvertArg_NUM])
577  
578  static int
579  import_gmpy2(void)
580  {
581      GMPy_C_API = (void **)PyCapsule_Import("gmpy2._C_API", 0);
582      return (GMPy_C_API != NULL) ? 0 : -1;
583  }
584  
585  #endif /* defined(GMPY2_MODULE) */
586  
587  #ifdef __cplusplus
588  }
589  #endif /* defined(__cplusplus */
590  #endif /* !defined(Py_GMPYMODULE_H */