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