PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gmpy.h

http://gmpy.googlecode.com/
C Header | 304 lines | 172 code | 52 blank | 80 comment | 14 complexity | e3c4a7c4ddbdd3c3b27be54f4ad56113 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * gmpy.h *
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * Python interface to the GMP or MPIR, MPFR, and MPC multiple precision *
  5. * libraries. *
  6. * *
  7. * Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
  8. * 2008, 2009 Alex Martelli *
  9. * *
  10. * Copyright 2008, 2009, 2010, 2011, 2012, 2013 Case Van Horsen *
  11. * *
  12. * This file is part of GMPY2. *
  13. * *
  14. * GMPY2 is free software: you can redistribute it and/or modify it under *
  15. * the terms of the GNU Lesser General Public License as published by the *
  16. * Free Software Foundation, either version 3 of the License, or (at your *
  17. * option) any later version. *
  18. * *
  19. * GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
  22. * License for more details. *
  23. * *
  24. * You should have received a copy of the GNU Lesser General Public *
  25. * License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
  26. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  27. /*
  28. gmpy C API extension header file.
  29. Part of Python's gmpy module since version 0.4
  30. Created by Pearu Peterson <pearu@cens.ioc.ee>, November 2000.
  31. Edited by A. Martelli <aleaxit@yahoo.com>, December 2000.
  32. Edited by Case Van Horsen <casevh@gmail.com>, 2009, 2010, 2011.
  33. Version 1.02, February 2007.
  34. Version 1.03, June 2008
  35. Version 1.04, June 2008 (no changes)
  36. Version 1.05, February 2009 (support MPIR)
  37. Version 1.20, January 2010 (remove obsolete MS hacks) casevh
  38. Version 2.00, April 2010 (change to gmpy2) casevh
  39. October 2010 (added Py_hash_t) casevh
  40. December 2010 (added mpfr, mpc) casevh
  41. January 2011 (add Pygmpy_context) casevh
  42. April 2011 (split into multiple files) casevh
  43. */
  44. #ifndef Py_GMPYMODULE_H
  45. #define Py_GMPYMODULE_H
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. /* Check for Python version requirements. */
  50. #if PY_VERSION_HEX < 0x02060000
  51. # error "GMPY2 requires Python 2.6 or later."
  52. #endif
  53. #if PY_VERSION_HEX < 0x030200A4
  54. typedef long Py_hash_t;
  55. typedef unsigned long Py_uhash_t;
  56. # define _PyHASH_IMAG 1000003
  57. #endif
  58. /* Define various macros to deal with differences between Python 2 and 3. */
  59. #if (PY_MAJOR_VERSION == 3)
  60. #define PY3
  61. #define Py2or3String_FromString PyUnicode_FromString
  62. #define Py2or3String_FromFormat PyUnicode_FromFormat
  63. #define Py2or3String_Check PyUnicode_Check
  64. #define Py2or3String_Format PyUnicode_Format
  65. #define Py2or3String_AsString PyUnicode_AS_DATA
  66. #define PyStrOrUnicode_Check(op) (PyBytes_Check(op) || PyUnicode_Check(op))
  67. #define PyIntOrLong_FromLong PyLong_FromLong
  68. #define PyIntOrLong_Check(op) PyLong_Check(op)
  69. #define PyIntOrLong_FromSize_t PyLong_FromSize_t
  70. #define PyIntOrLong_FromSsize_t PyLong_FromSsize_t
  71. #define PyIntOrLong_AsSsize_t PyLong_AsSsize_t
  72. #define PyIntOrLong_AsLong PyLong_AsLong
  73. #else
  74. #define PY2
  75. #define Py2or3String_FromString PyString_FromString
  76. #define Py2or3String_FromFormat PyString_FromFormat
  77. #define Py2or3String_Check PyString_Check
  78. #define Py2or3String_Format PyString_Format
  79. #define Py2or3String_AsString PyString_AsString
  80. #define PyStrOrUnicode_Check(op) (PyString_Check(op) || PyUnicode_Check(op))
  81. #define PyIntOrLong_FromLong PyInt_FromLong
  82. #define PyIntOrLong_Check(op) (PyInt_Check(op) || PyLong_Check(op))
  83. #define PyIntOrLong_FromSize_t PyInt_FromSize_t
  84. #define PyIntOrLong_FromSsize_t PyInt_FromSsize_t
  85. #define PyIntOrLong_AsSsize_t PyInt_AsSsize_t
  86. #define PyIntOrLong_AsLong PyInt_AsLong
  87. #endif
  88. /* Support MPIR, if requested. */
  89. #ifdef MPIR
  90. # include "mpir.h"
  91. #else
  92. # include "gmp.h"
  93. #endif
  94. #if defined(MS_WIN32) && defined(_MSC_VER)
  95. /* so one won't need to link explicitly to gmp.lib...: */
  96. # if defined(MPIR)
  97. # pragma comment(lib,"mpir.lib")
  98. # else
  99. # pragma comment(lib,"gmp.lib")
  100. # endif
  101. # define isnan _isnan
  102. # define isinf !_finite
  103. # define USE_ALLOCA 1
  104. # define inline __inline
  105. #endif
  106. /* MPIR 2.6 introduce new data types - MPIR_UI and MPIR_SI - that correspond
  107. * to the "optimal" integer type for pass values to GMP/MPIR. On almost all
  108. * systems, these types correspond to "unsigned long" and "long". But on
  109. * 64-bit Windows, these types correspond to "unsigned long long" and
  110. * "long long".
  111. */
  112. #ifndef BITS_PER_UI
  113. /* Assume we are NOT using MPIR > 2.5. */
  114. #define BITS_PER_UI BITS_PER_ULONG
  115. typedef unsigned long mpir_ui;
  116. typedef long mpir_si;
  117. #define mpz_fits_si_p mpz_fits_slong_p
  118. #define mpz_fits_ui_p mpz_fits_ulong_p
  119. #endif
  120. #ifdef _WIN64
  121. #define PyIntOrLong_FromSI PyLong_FromLongLong
  122. #define PyIntOrLong_AsSI PyLong_AsLongLong
  123. #else
  124. #define PyIntOrLong_FromSI PyLong_FromLong
  125. #define PyIntOrLong_AsSI PyLong_AsLong
  126. #endif
  127. #ifdef __GNUC__
  128. # define USE_ALLOCA 1
  129. #endif
  130. #ifndef alloca
  131. # ifdef __GNUC__
  132. # define alloca __builtin_alloca
  133. # else
  134. # ifdef _MSC_VER
  135. # include <malloc.h>
  136. # define alloca _alloca
  137. # else
  138. # if HAVE_ALLOCA_H
  139. # include <alloca.h>
  140. # else
  141. char *alloca ();
  142. # endif
  143. # endif
  144. # endif
  145. #endif
  146. #define ALLOC_THRESHOLD 8192
  147. #define TYPE_ERROR(msg) PyErr_SetString(PyExc_TypeError, msg)
  148. #define VALUE_ERROR(msg) PyErr_SetString(PyExc_ValueError, msg)
  149. #define ZERO_ERROR(msg) PyErr_SetString(PyExc_ZeroDivisionError, msg)
  150. #define SYSTEM_ERROR(msg) PyErr_SetString(PyExc_SystemError, msg)
  151. #define OVERFLOW_ERROR(msg) PyErr_SetString(PyExc_OverflowError, msg)
  152. #define GMPY_DEFAULT -1
  153. /* To prevent excessive memory usage, we don't want to save very large
  154. * numbers in the cache. The default value specified in the options
  155. * structure is 128 words (512 bytes on 32-bit platforms, 1024 bytes on
  156. * 64-bit platforms).
  157. */
  158. #define MAX_CACHE_LIMBS 16384
  159. /* The maximum number of objects that can be saved in a cache is specified
  160. * here. The default value is 100.*/
  161. #define MAX_CACHE 1000
  162. /* Choose which memory manager is used: Python or C.
  163. * NOTE: The use of PyMem is not compatible with Sage, therefore it is
  164. * disabled by default.
  165. * Use -DUSE_PYMEM to enable.
  166. */
  167. #ifdef USE_PYMEM
  168. # define GMPY_FREE(NAME) PyMem_FR(NAME)
  169. # define GMPY_MALLOC(NAME) PyMem_Malloc(NAME)
  170. # define GMPY_REALLOC(NAME, SIZE) PyMem_Realloc(NAME, SIZE)
  171. #else
  172. # define GMPY_FREE(NAME) free(NAME)
  173. # define GMPY_MALLOC(NAME) malloc(NAME)
  174. # define GMPY_REALLOC(NAME, SIZE) realloc(NAME, SIZE)
  175. #endif
  176. #ifdef USE_ALLOCA
  177. # define TEMP_ALLOC(B, S) \
  178. if(S < ALLOC_THRESHOLD) { \
  179. B = alloca(S); \
  180. } else { \
  181. if(!(B = GMPY_MALLOC(S))) { \
  182. PyErr_NoMemory(); \
  183. return NULL; \
  184. } \
  185. }
  186. # define TEMP_FREE(B, S) if(S >= ALLOC_THRESHOLD) GMPY_FREE(B)
  187. #else
  188. # define TEMP_ALLOC(B, S) \
  189. if(!(B = GMPY_MALLOC(S))) { \
  190. PyErr_NoMemory(); \
  191. return NULL; \
  192. }
  193. # define TEMP_FREE(B, S) GMPY_FREE(B)
  194. #endif
  195. /* Various defs to mask differences between Python versions. */
  196. #define Py_RETURN_NOTIMPLEMENTED\
  197. return Py_INCREF(Py_NotImplemented), Py_NotImplemented
  198. #ifndef Py_SIZE
  199. #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
  200. #endif
  201. #ifndef Py_TYPE
  202. #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
  203. #endif
  204. /* The gmpy_args.h file includes macros that are used for argument
  205. * processing.
  206. */
  207. #include "gmpy_args.h"
  208. #include "gmpy_mpz.h"
  209. #include "gmpy_xmpz.h"
  210. #include "gmpy_mpz_divmod.h"
  211. #include "gmpy_mpz_divmod2exp.h"
  212. #include "gmpy_mpz_inplace.h"
  213. #include "gmpy_xmpz_inplace.h"
  214. #include "gmpy_mpq.h"
  215. #ifdef WITHMPFR
  216. # include "mpfr.h"
  217. # include "gmpy_mpfr.h"
  218. # if MPFR_VERSION < 0x030100
  219. # error gmpy2 requires MPFR 3.1.0 or later
  220. # endif
  221. # include "gmpy_context.h"
  222. #endif
  223. #ifdef WITHMPC
  224. # include "mpc.h"
  225. # include "gmpy_mpc.h"
  226. # if MPC_VERSION < 0x010000
  227. # error gmpy2 requires MPC 1.0.0 or later
  228. # endif
  229. #endif
  230. #include "gmpy_convert.h"
  231. /* Support object caching, creation, and deletion. */
  232. #include "gmpy_cache.h"
  233. /* Suport for miscellaneous functions (ie. version, license, etc.). */
  234. #include "gmpy_misc.h"
  235. /* Support basic arithmetic operations on mpz, mpq, mpfr, and mpc types. */
  236. #include "gmpy_basic.h"
  237. /* Support operations other than those in gmpy_basic. */
  238. #include "gmpy_mpany.h"
  239. /* Support conversion to/from binary format. */
  240. #include "gmpy_binary.h"
  241. /* Support random number generators. */
  242. #include "gmpy_random.h"
  243. /* Support Lucas sequences. */
  244. #include "gmpy_mpz_lucas.h"
  245. /* Support probable-prime tests. */
  246. #include "gmpy_mpz_prp.h"
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif /* !defined(Py_GMPYMODULE_H */