source: sage/ext/mpz_pylong.c @ 37:84e8ffacb983

Revision 37:84e8ffacb983, 1.3 KB checked in by Gonzalo Tornaria <tornaria@…>, 7 years ago (diff)

[project @ mpz_pylong: add notices / polish headers]

Line 
1/* mpz <-> pylong conversion and "pythonhash" for mpz
2 *
3 * Author:  Gonzalo Tornaría <tornaria@math.utexas.edu>
4 * Date:    March 2006
5 * License: GPL v2
6 *
7 * this is free software: if it breaks, you get to keep all the pieces
8 */
9
10#include "mpn_pylong.h"
11#include "mpz_pylong.h"
12
13/* mpz python hash */
14long
15mpz_pythonhash (mpz_srcptr z)
16{
17  long x = mpn_pythonhash(z->_mp_d, abs(z->_mp_size));
18  if (z->_mp_size < 0)
19    x = -x;
20  if (x == -1)
21    x = -2;
22  return x;
23}
24
25/* mpz -> pylong conversion */
26PyObject *
27mpz_get_pylong(mpz_srcptr z)
28{
29  py_size_t size = mpn_pylong_size(z->_mp_d, abs(z->_mp_size));
30  PyLongObject *l = PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
31
32  if (l != NULL)
33  {
34    mpn_get_pylong(l->ob_digit, size, z->_mp_d, abs(z->_mp_size));
35    if (z->_mp_size < 0)
36      l->ob_size = -(l->ob_size);
37  }
38
39  return (PyObject *) l;
40}
41
42/* pylong -> mpz conversion */
43int
44mpz_set_pylong(mpz_ptr z, PyObject * ll)
45{
46  register PyLongObject * l = (PyLongObject *) ll;
47  mp_size_t size;
48  int i;
49
50  if (l==NULL || !PyLong_Check(l)) {
51    PyErr_BadInternalCall();
52    return -1;
53  }
54 
55  size = mpn_size_from_pylong(l->ob_digit, abs(l->ob_size));
56
57  if (z->_mp_alloc < size)
58    _mpz_realloc (z, size);
59
60  mpn_set_pylong(z->_mp_d, size, l->ob_digit, abs(l->ob_size));
61  z->_mp_size = l->ob_size < 0 ? -size : size;
62
63  return size;
64}
Note: See TracBrowser for help on using the repository browser.