PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 1ms

/src/gmpy_misc.c

http://gmpy.googlecode.com/
C | 167 lines | 115 code | 22 blank | 30 comment | 5 complexity | 8e060773815e9041d041251e488dbb71 MD5 | raw file
  1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2 * gmpy_misc.c                                                             *
  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/* Miscellaneous module-level functions and helper functions. */
 29
 30PyDoc_STRVAR(doc_license,
 31"license() -> string\n\n"
 32"Return string giving license information.");
 33
 34static PyObject *
 35Pygmpy_get_license(PyObject *self, PyObject *args)
 36{
 37    return Py_BuildValue("s", gmpy_license);
 38}
 39
 40PyDoc_STRVAR(doc_version,
 41"version() -> string\n\n"
 42"Return string giving current GMPY2 version.");
 43
 44static PyObject *
 45Pygmpy_get_version(PyObject *self, PyObject *args)
 46{
 47    return Py_BuildValue("s", gmpy_version);
 48}
 49
 50PyDoc_STRVAR(doc_cvsid,
 51"_cvsid() -> string\n\n"
 52"Return string giving current GMPY2 cvs Id.");
 53
 54static PyObject *
 55Pygmpy_get_cvsid(PyObject *self, PyObject *args)
 56{
 57    return Py_BuildValue("s", _gmpy_cvs);
 58}
 59
 60PyDoc_STRVAR(doc_mp_version,
 61"mp_version() -> string\n\n"
 62"Return string giving the name and version of the multiple precision\n"
 63"library used.");
 64
 65static PyObject *
 66Pygmpy_get_mp_version(PyObject *self, PyObject *args)
 67{
 68#ifndef __MPIR_VERSION
 69    return Py2or3String_FromFormat("%s %s", "GMP", gmp_version);
 70#else
 71    return Py2or3String_FromFormat("%s %s", "MPIR", mpir_version);
 72#endif
 73}
 74
 75PyDoc_STRVAR(doc_mpfr_version,
 76"mpfr_version() -> string\n\n"
 77"Return string giving current MPFR version. Return None if MPFR\n"
 78"support is not available.");
 79
 80static PyObject *
 81Pygmpy_get_mpfr_version(PyObject *self, PyObject *args)
 82{
 83#ifdef WITHMPFR
 84    return Py2or3String_FromFormat("%s %s", "MPFR",
 85                                   MPFR_VERSION_STRING);
 86#else
 87    Py_RETURN_NONE;
 88#endif
 89}
 90
 91PyDoc_STRVAR(doc_mpc_version,
 92"mpc_version() -> string\n\n"
 93"Return string giving current MPC version. Return None if MPC\n"
 94"support is not available.");
 95
 96static PyObject *
 97Pygmpy_get_mpc_version(PyObject *self, PyObject *args)
 98{
 99#ifdef WITHMPC
100    return Py2or3String_FromFormat("%s %s", "MPC",
101                                   MPC_VERSION_STRING);
102#else
103    Py_RETURN_NONE;
104#endif
105}
106
107PyDoc_STRVAR(doc_mp_limbsize,
108"mp_limbsize() -> integer\n\n\
109Return the number of bits per limb.");
110
111static PyObject *
112Pygmpy_get_mp_limbsize(PyObject *self, PyObject *args)
113{
114    return Py_BuildValue("i", mp_bits_per_limb);
115}
116
117/*
118 * access cache options
119 */
120
121PyDoc_STRVAR(doc_get_cache,
122"get_cache() -> (cache_size, object_size)\n\n\
123Return the current cache size (number of objects) and maximum size\n\
124per object (number of limbs) for all GMPY2 objects.");
125
126static PyObject *
127Pygmpy_get_cache(PyObject *self, PyObject *args)
128{
129    return Py_BuildValue("(ii)", global.cache_size, global.cache_obsize);
130}
131
132PyDoc_STRVAR(doc_set_cache,
133"set_cache(cache_size, object_size)\n\n\
134Set the current cache size (number of objects) and the maximum size\n\
135per object (number of limbs). Raises ValueError if cache size exceeds\n\
1361000 or object size exceeds 16384.");
137
138static PyObject *
139Pygmpy_set_cache(PyObject *self, PyObject *args)
140{
141    int newcache = -1, newsize = -1;
142
143    if (!PyArg_ParseTuple(args, "ii", &newcache, &newsize))
144        return NULL;
145    if (newcache<0 || newcache>MAX_CACHE) {
146        VALUE_ERROR("cache size must between 0 and 1000");
147        return NULL;
148    }
149    if (newsize<0 || newsize>MAX_CACHE_LIMBS) {
150        VALUE_ERROR("object size must between 0 and 16384");
151        return NULL;
152    }
153
154    global.cache_size = newcache;
155    global.cache_obsize = newsize;
156    set_zcache();
157    set_pympzcache();
158    set_pympqcache();
159    set_pyxmpzcache();
160#ifdef WITHMPFR
161    set_pympfrcache();
162#endif
163#ifdef WITHMPC
164    set_pympccache();
165#endif
166    Py_RETURN_NONE;
167}