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