home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Source / Objects / intobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-25  |  18.4 KB  |  904 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Integer object implementation */
  33.  
  34. #include "Python.h"
  35.  
  36. #ifdef HAVE_LIMITS_H
  37. #include <limits.h>
  38. #endif
  39.  
  40. #include "protos/intobject.h"
  41.  
  42. #ifndef LONG_MAX
  43. #define LONG_MAX 0X7FFFFFFFL
  44. #endif
  45.  
  46. #ifndef LONG_MIN
  47. #define LONG_MIN (-LONG_MAX-1)
  48. #endif
  49.  
  50. #ifndef CHAR_BIT
  51. #define CHAR_BIT 8
  52. #endif
  53.  
  54. #ifndef LONG_BIT
  55. #define LONG_BIT (CHAR_BIT * sizeof(long))
  56. #endif
  57.  
  58. long
  59. PyInt_GetMax()
  60. {
  61.     return LONG_MAX;    /* To initialize sys.maxint */
  62. }
  63.  
  64. /* Standard Booleans */
  65.  
  66. PyIntObject _Py_ZeroStruct = {
  67.     PyObject_HEAD_INIT(&PyInt_Type)
  68.     0
  69. };
  70.  
  71. PyIntObject _Py_TrueStruct = {
  72.     PyObject_HEAD_INIT(&PyInt_Type)
  73.     1
  74. };
  75.  
  76. static PyObject *
  77. err_ovf(msg)
  78.     char *msg;
  79. {
  80.     PyErr_SetString(PyExc_OverflowError, msg);
  81.     return NULL;
  82. }
  83.  
  84. /* Integers are quite normal objects, to make object handling uniform.
  85.    (Using odd pointers to represent integers would save much space
  86.    but require extra checks for this special case throughout the code.)
  87.    Since, a typical Python program spends much of its time allocating
  88.    and deallocating integers, these operations should be very fast.
  89.    Therefore we use a dedicated allocation scheme with a much lower
  90.    overhead (in space and time) than straight malloc(): a simple
  91.    dedicated free list, filled when necessary with memory from malloc().
  92. */
  93.  
  94. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  95. #define BHEAD_SIZE    8    /* Enough for a 64-bit pointer */
  96. #define N_INTOBJECTS    ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
  97.  
  98. #define PyMem_MALLOC    malloc
  99. #define PyMem_FREE    free
  100.  
  101. struct _intblock {
  102.     struct _intblock *next;
  103.     PyIntObject objects[N_INTOBJECTS];
  104. };
  105.  
  106. typedef struct _intblock PyIntBlock;
  107.  
  108. static PyIntBlock *block_list = NULL;
  109. static PyIntObject *free_list = NULL;
  110.  
  111. static PyIntObject *
  112. fill_free_list()
  113. {
  114.     PyIntObject *p, *q;
  115.     p = (PyIntObject *)PyMem_MALLOC(sizeof(PyIntBlock));
  116.     if (p == NULL)
  117.         return (PyIntObject *)PyErr_NoMemory();
  118.     ((PyIntBlock *)p)->next = block_list;
  119.     block_list = (PyIntBlock *)p;
  120.     p = &((PyIntBlock *)p)->objects[0];
  121.     q = p + N_INTOBJECTS;
  122.     while (--q > p)
  123.         q->ob_type = (struct _typeobject *)(q-1);
  124.     q->ob_type = NULL;
  125.     return p + N_INTOBJECTS - 1;
  126. }
  127.  
  128. #ifndef NSMALLPOSINTS
  129. #define NSMALLPOSINTS        100
  130. #endif
  131. #ifndef NSMALLNEGINTS
  132. #define NSMALLNEGINTS        1
  133. #endif
  134. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  135. /* References to small integers are saved in this array so that they
  136.    can be shared.
  137.    The integers that are saved are those in the range
  138.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  139. */
  140. static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  141. #endif
  142. #ifdef COUNT_ALLOCS
  143. int quick_int_allocs, quick_neg_int_allocs;
  144. #endif
  145.  
  146. PyObject *
  147. PyInt_FromLong(ival)
  148.     long ival;
  149. {
  150.     register PyIntObject *v;
  151. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  152.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
  153.         (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
  154.         Py_INCREF(v);
  155. #ifdef COUNT_ALLOCS
  156.         if (ival >= 0)
  157.             quick_int_allocs++;
  158.         else
  159.             quick_neg_int_allocs++;
  160. #endif
  161.         return (PyObject *) v;
  162.     }
  163. #endif
  164.     if (free_list == NULL) {
  165.         if ((free_list = fill_free_list()) == NULL)
  166.             return NULL;
  167.     }
  168.     v = free_list;
  169.     free_list = (PyIntObject *)v->ob_type;
  170.     v->ob_type = &PyInt_Type;
  171.     v->ob_ival = ival;
  172.     _Py_NewReference(v);
  173. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  174.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  175.         /* save this one for a following allocation */
  176.         Py_INCREF(v);
  177.         small_ints[ival + NSMALLNEGINTS] = v;
  178.     }
  179. #endif
  180.     return (PyObject *) v;
  181. }
  182.  
  183. static void
  184. int_dealloc(v)
  185.     PyIntObject *v;
  186. {
  187.     v->ob_type = (struct _typeobject *)free_list;
  188.     free_list = v;
  189. }
  190.  
  191. long
  192. PyInt_AsLong(op)
  193.     register PyObject *op;
  194. {
  195.     PyNumberMethods *nb;
  196.     PyIntObject *io;
  197.     long val;
  198.     
  199.     if (op && PyInt_Check(op))
  200.         return PyInt_AS_LONG((PyIntObject*) op);
  201.     
  202.     if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
  203.         nb->nb_int == NULL) {
  204.         PyErr_BadArgument();
  205.         return -1;
  206.     }
  207.     
  208.     io = (PyIntObject*) (*nb->nb_int) (op);
  209.     if (io == NULL)
  210.         return -1;
  211.     if (!PyInt_Check(io)) {
  212.         PyErr_SetString(PyExc_TypeError,
  213.                 "nb_int should return int object");
  214.         return -1;
  215.     }
  216.     
  217.     val = PyInt_AS_LONG(io);
  218.     Py_DECREF(io);
  219.     
  220.     return val;
  221. }
  222.  
  223. /* Methods */
  224.  
  225. /* ARGSUSED */
  226. static int
  227. int_print(v, fp, flags)
  228.     PyIntObject *v;
  229.     FILE *fp;
  230.     int flags; /* Not used but required by interface */
  231. {
  232.     fprintf(fp, "%ld", v->ob_ival);
  233.     return 0;
  234. }
  235.  
  236. static PyObject *
  237. int_repr(v)
  238.     PyIntObject *v;
  239. {
  240.     char buf[20];
  241.     sprintf(buf, "%ld", v->ob_ival);
  242.     return PyString_FromString(buf);
  243. }
  244.  
  245. static int
  246. int_compare(v, w)
  247.     PyIntObject *v, *w;
  248. {
  249.     register long i = v->ob_ival;
  250.     register long j = w->ob_ival;
  251.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  252. }
  253.  
  254. static long
  255. int_hash(v)
  256.     PyIntObject *v;
  257. {
  258.     /* XXX If this is changed, you also need to change the way
  259.        Python's long, float and complex types are hashed. */
  260.     long x = v -> ob_ival;
  261.     if (x == -1)
  262.         x = -2;
  263.     return x;
  264. }
  265.  
  266. static PyObject *
  267. int_add(v, w)
  268.     PyIntObject *v;
  269.     PyIntObject *w;
  270. {
  271.     register long a, b, x;
  272.     a = v->ob_ival;
  273.     b = w->ob_ival;
  274.     x = a + b;
  275.     if ((x^a) < 0 && (x^b) < 0)
  276.         return err_ovf("integer addition");
  277.     return PyInt_FromLong(x);
  278. }
  279.  
  280. static PyObject *
  281. int_sub(v, w)
  282.     PyIntObject *v;
  283.     PyIntObject *w;
  284. {
  285.     register long a, b, x;
  286.     a = v->ob_ival;
  287.     b = w->ob_ival;
  288.     x = a - b;
  289.     if ((x^a) < 0 && (x^~b) < 0)
  290.         return err_ovf("integer subtraction");
  291.     return PyInt_FromLong(x);
  292. }
  293.  
  294. /*
  295. Integer overflow checking used to be done using a double, but on 64
  296. bit machines (where both long and double are 64 bit) this fails
  297. because the double doesn't have enouvg precision.  John Tromp suggests
  298. the following algorithm:
  299.  
  300. Suppose again we normalize a and b to be nonnegative.
  301. Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
  302. Now we test ah and bh against zero and get essentially 3 possible outcomes.
  303.  
  304. 1) both ah and bh > 0 : then report overflow
  305.  
  306. 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
  307.                         negative
  308.  
  309. 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
  310.                         compute al*bl and report overflow if it's negative
  311.                         add (ah*bl)<<32 to al*bl and report overflow if
  312.                         it's negative
  313.  
  314. In case of no overflow the result is then negated if necessary.
  315.  
  316. The majority of cases will be 2), in which case this method is the same as
  317. what I suggested before. If multiplication is expensive enough, then the
  318. other method is faster on case 3), but also more work to program, so I
  319. guess the above is the preferred solution.
  320.  
  321. */
  322.  
  323. static PyObject *
  324. int_mul(v, w)
  325.     PyIntObject *v;
  326.     PyIntObject *w;
  327. {
  328.     long a, b, ah, bh, x, y;
  329.     int s = 1;
  330.  
  331.     a = v->ob_ival;
  332.     b = w->ob_ival;
  333.     ah = a >> (LONG_BIT/2);
  334.     bh = b >> (LONG_BIT/2);
  335.  
  336.     /* Quick test for common case: two small positive ints */
  337.  
  338.     if (ah == 0 && bh == 0) {
  339.         x = a*b;
  340.         if (x < 0)
  341.             goto bad;
  342.         return PyInt_FromLong(x);
  343.     }
  344.  
  345.     /* Arrange that a >= b >= 0 */
  346.  
  347.     if (a < 0) {
  348.         a = -a;
  349.         if (a < 0) {
  350.             /* Largest negative */
  351.             if (b == 0 || b == 1) {
  352.                 x = a*b;
  353.                 goto ok;
  354.             }
  355.             else
  356.                 goto bad;
  357.         }
  358.         s = -s;
  359.         ah = a >> (LONG_BIT/2);
  360.     }
  361.     if (b < 0) {
  362.         b = -b;
  363.         if (b < 0) {
  364.             /* Largest negative */
  365.             if (a == 0 || (a == 1 && s == 1)) {
  366.                 x = a*b;
  367.                 goto ok;
  368.             }
  369.             else
  370.                 goto bad;
  371.         }
  372.         s = -s;
  373.         bh = b >> (LONG_BIT/2);
  374.     }
  375.  
  376.     /* 1) both ah and bh > 0 : then report overflow */
  377.  
  378.     if (ah != 0 && bh != 0)
  379.         goto bad;
  380.  
  381.     /* 2) both ah and bh = 0 : then compute a*b and report
  382.                    overflow if it comes out negative */
  383.  
  384.     if (ah == 0 && bh == 0) {
  385.         x = a*b;
  386.         if (x < 0)
  387.             goto bad;
  388.         return PyInt_FromLong(x*s);
  389.     }
  390.  
  391.     if (a < b) {
  392.         /* Swap */
  393.         x = a;
  394.         a = b;
  395.         b = x;
  396.         ah = bh;
  397.         /* bh not used beyond this point */
  398.     }
  399.  
  400.     /* 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if
  401.                    it's >= 2^31
  402.                         compute al*bl and report overflow if it's negative
  403.                         add (ah*bl)<<32 to al*bl and report overflow if
  404.                         it's negative
  405.             (NB b == bl in this case, and we make a = al) */
  406.  
  407.     y = ah*b;
  408.     if (y >= (1L << (LONG_BIT/2 - 1)))
  409.         goto bad;
  410.     a &= (1L << (LONG_BIT/2)) - 1;
  411.     x = a*b;
  412.     if (x < 0)
  413.         goto bad;
  414.     x += y << (LONG_BIT/2);
  415.     if (x < 0)
  416.         goto bad;
  417.  ok:
  418.     return PyInt_FromLong(x * s);
  419.  
  420.  bad:
  421.     return err_ovf("integer multiplication");
  422. }
  423.  
  424. static int
  425. i_divmod(x, y, p_xdivy, p_xmody)
  426.     register PyIntObject *x, *y;
  427.     long *p_xdivy, *p_xmody;
  428. {
  429.     long xi = x->ob_ival;
  430.     long yi = y->ob_ival;
  431.     long xdivy, xmody;
  432.     
  433.     if (yi == 0) {
  434.         PyErr_SetString(PyExc_ZeroDivisionError,
  435.                 "integer division or modulo");
  436.         return -1;
  437.     }
  438.     if (yi < 0) {
  439.         if (xi < 0)
  440.             xdivy = -xi / -yi;
  441.         else
  442.             xdivy = - (xi / -yi);
  443.     }
  444.     else {
  445.         if (xi < 0)
  446.             xdivy = - (-xi / yi);
  447.         else
  448.             xdivy = xi / yi;
  449.     }
  450.     xmody = xi - xdivy*yi;
  451.     if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
  452.         xmody += yi;
  453.         xdivy -= 1;
  454.     }
  455.     *p_xdivy = xdivy;
  456.     *p_xmody = xmody;
  457.     return 0;
  458. }
  459.  
  460. static PyObject *
  461. int_div(x, y)
  462.     PyIntObject *x;
  463.     PyIntObject *y;
  464. {
  465.     long d, m;
  466.     if (i_divmod(x, y, &d, &m) < 0)
  467.         return NULL;
  468.     return PyInt_FromLong(d);
  469. }
  470.  
  471. static PyObject *
  472. int_mod(x, y)
  473.     PyIntObject *x;
  474.     PyIntObject *y;
  475. {
  476.     long d, m;
  477.     if (i_divmod(x, y, &d, &m) < 0)
  478.         return NULL;
  479.     return PyInt_FromLong(m);
  480. }
  481.  
  482. static PyObject *
  483. int_divmod(x, y)
  484.     PyIntObject *x;
  485.     PyIntObject *y;
  486. {
  487.     long d, m;
  488.     if (i_divmod(x, y, &d, &m) < 0)
  489.         return NULL;
  490.     return Py_BuildValue("(ll)", d, m);
  491. }
  492.  
  493. static PyObject *
  494. int_pow(v, w, z)
  495.     PyIntObject *v;
  496.     PyIntObject *w;
  497.     PyIntObject *z;
  498. {
  499. #if 1
  500.     register long iv, iw, iz=0, ix, temp, prev;
  501.     iv = v->ob_ival;
  502.     iw = w->ob_ival;
  503.     if (iw < 0) {
  504.         PyErr_SetString(PyExc_ValueError,
  505.                 "integer to the negative power");
  506.         return NULL;
  507.     }
  508.      if ((PyObject *)z != Py_None) {
  509.         iz = z->ob_ival;
  510.         if (iz == 0) {
  511.             PyErr_SetString(PyExc_ValueError,
  512.                     "pow(x, y, z) with z==0");
  513.             return NULL;
  514.         }
  515.     }
  516.     /*
  517.      * XXX: The original exponentiation code stopped looping
  518.      * when temp hit zero; this code will continue onwards
  519.      * unnecessarily, but at least it won't cause any errors.
  520.      * Hopefully the speed improvement from the fast exponentiation
  521.      * will compensate for the slight inefficiency.
  522.      * XXX: Better handling of overflows is desperately needed.
  523.      */
  524.      temp = iv;
  525.     ix = 1;
  526.     while (iw > 0) {
  527.          prev = ix;    /* Save value for overflow check */
  528.          if (iw & 1) {    
  529.              ix = ix*temp;
  530.             if (temp == 0)
  531.                 break; /* Avoid ix / 0 */
  532.             if (ix / temp != prev)
  533.                 return err_ovf("integer pow()");
  534.         }
  535.          iw >>= 1;    /* Shift exponent down by 1 bit */
  536.             if (iw==0) break;
  537.          prev = temp;
  538.          temp *= temp;    /* Square the value of temp */
  539.          if (prev!=0 && temp/prev!=prev)
  540.             return err_ovf("integer pow()");
  541.          if (iz) {
  542.             /* If we did a multiplication, perform a modulo */
  543.              ix = ix % iz;
  544.              temp = temp % iz;
  545.         }
  546.     }
  547.     if (iz) {
  548.          PyObject *t1, *t2;
  549.          long int div, mod;
  550.          t1=PyInt_FromLong(ix); 
  551.         t2=PyInt_FromLong(iz);
  552.          if (t1==NULL || t2==NULL ||
  553.              i_divmod((PyIntObject *)t1,
  554.                  (PyIntObject *)t2, &div, &mod)<0)
  555.         {
  556.              Py_XDECREF(t1);
  557.              Py_XDECREF(t2);
  558.             return(NULL);
  559.         }
  560.         Py_DECREF(t1);
  561.         Py_DECREF(t2);
  562.          ix=mod;
  563.     }
  564.     return PyInt_FromLong(ix);
  565. #else
  566.     register long iv, iw, ix;
  567.     iv = v->ob_ival;
  568.     iw = w->ob_ival;
  569.     if (iw < 0) {
  570.         PyErr_SetString(PyExc_ValueError,
  571.                 "integer to the negative power");
  572.         return NULL;
  573.     }
  574.     if ((PyObject *)z != Py_None) {
  575.         PyErr_SetString(PyExc_TypeError,
  576.                 "pow(int, int, int) not yet supported");
  577.         return NULL;
  578.     }
  579.     ix = 1;
  580.     while (--iw >= 0) {
  581.         long prev = ix;
  582.         ix = ix * iv;
  583.         if (iv == 0)
  584.             break; /* 0 to some power -- avoid ix / 0 */
  585.         if (ix / iv != prev)
  586.             return err_ovf("integer pow()");
  587.     }
  588.     return PyInt_FromLong(ix);
  589. #endif
  590. }                
  591.  
  592. static PyObject *
  593. int_neg(v)
  594.     PyIntObject *v;
  595. {
  596.     register long a, x;
  597.     a = v->ob_ival;
  598.     x = -a;
  599.     if (a < 0 && x < 0)
  600.         return err_ovf("integer negation");
  601.     return PyInt_FromLong(x);
  602. }
  603.  
  604. static PyObject *
  605. int_pos(v)
  606.     PyIntObject *v;
  607. {
  608.     Py_INCREF(v);
  609.     return (PyObject *)v;
  610. }
  611.  
  612. static PyObject *
  613. int_abs(v)
  614.     PyIntObject *v;
  615. {
  616.     if (v->ob_ival >= 0)
  617.         return int_pos(v);
  618.     else
  619.         return int_neg(v);
  620. }
  621.  
  622. static int
  623. int_nonzero(v)
  624.     PyIntObject *v;
  625. {
  626.     return v->ob_ival != 0;
  627. }
  628.  
  629. static PyObject *
  630. int_invert(v)
  631.     PyIntObject *v;
  632. {
  633.     return PyInt_FromLong(~v->ob_ival);
  634. }
  635.  
  636. static PyObject *
  637. int_lshift(v, w)
  638.     PyIntObject *v;
  639.     PyIntObject *w;
  640. {
  641.     register long a, b;
  642.     a = v->ob_ival;
  643.     b = w->ob_ival;
  644.     if (b < 0) {
  645.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  646.         return NULL;
  647.     }
  648.     if (a == 0 || b == 0) {
  649.         Py_INCREF(v);
  650.         return (PyObject *) v;
  651.     }
  652.     if (b >= LONG_BIT) {
  653.         return PyInt_FromLong(0L);
  654.     }
  655.     a = (unsigned long)a << b;
  656.     return PyInt_FromLong(a);
  657. }
  658.  
  659. static PyObject *
  660. int_rshift(v, w)
  661.     PyIntObject *v;
  662.     PyIntObject *w;
  663. {
  664.     register long a, b;
  665.     a = v->ob_ival;
  666.     b = w->ob_ival;
  667.     if (b < 0) {
  668.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  669.         return NULL;
  670.     }
  671.     if (a == 0 || b == 0) {
  672.         Py_INCREF(v);
  673.         return (PyObject *) v;
  674.     }
  675.     if (b >= LONG_BIT) {
  676.         if (a < 0)
  677.             a = -1;
  678.         else
  679.             a = 0;
  680.     }
  681.     else {
  682.         if (a < 0)
  683.             a = ~( ~(unsigned long)a >> b );
  684.         else
  685.             a = (unsigned long)a >> b;
  686.     }
  687.     return PyInt_FromLong(a);
  688. }
  689.  
  690. static PyObject *
  691. int_and(v, w)
  692.     PyIntObject *v;
  693.     PyIntObject *w;
  694. {
  695.     register long a, b;
  696.     a = v->ob_ival;
  697.     b = w->ob_ival;
  698.     return PyInt_FromLong(a & b);
  699. }
  700.  
  701. static PyObject *
  702. int_xor(v, w)
  703.     PyIntObject *v;
  704.     PyIntObject *w;
  705. {
  706.     register long a, b;
  707.     a = v->ob_ival;
  708.     b = w->ob_ival;
  709.     return PyInt_FromLong(a ^ b);
  710. }
  711.  
  712. static PyObject *
  713. int_or(v, w)
  714.     PyIntObject *v;
  715.     PyIntObject *w;
  716. {
  717.     register long a, b;
  718.     a = v->ob_ival;
  719.     b = w->ob_ival;
  720.     return PyInt_FromLong(a | b);
  721. }
  722.  
  723. static PyObject *
  724. int_int(v)
  725.     PyIntObject *v;
  726. {
  727.     Py_INCREF(v);
  728.     return (PyObject *)v;
  729. }
  730.  
  731. static PyObject *
  732. int_long(v)
  733.     PyIntObject *v;
  734. {
  735.     return PyLong_FromLong((v -> ob_ival));
  736. }
  737.  
  738. static PyObject *
  739. int_float(v)
  740.     PyIntObject *v;
  741. {
  742.     return PyFloat_FromDouble((double)(v -> ob_ival));
  743. }
  744.  
  745. static PyObject *
  746. int_oct(v)
  747.     PyIntObject *v;
  748. {
  749.     char buf[100];
  750.     long x = v -> ob_ival;
  751.     if (x == 0)
  752.         strcpy(buf, "0");
  753.     else
  754.         sprintf(buf, "0%lo", x);
  755.     return PyString_FromString(buf);
  756. }
  757.  
  758. static PyObject *
  759. int_hex(v)
  760.     PyIntObject *v;
  761. {
  762.     char buf[100];
  763.     long x = v -> ob_ival;
  764.     sprintf(buf, "0x%lx", x);
  765.     return PyString_FromString(buf);
  766. }
  767.  
  768. static PyNumberMethods int_as_number = {
  769.     (binaryfunc)int_add, /*nb_add*/
  770.     (binaryfunc)int_sub, /*nb_subtract*/
  771.     (binaryfunc)int_mul, /*nb_multiply*/
  772.     (binaryfunc)int_div, /*nb_divide*/
  773.     (binaryfunc)int_mod, /*nb_remainder*/
  774.     (binaryfunc)int_divmod, /*nb_divmod*/
  775.     (ternaryfunc)int_pow, /*nb_power*/
  776.     (unaryfunc)int_neg, /*nb_negative*/
  777.     (unaryfunc)int_pos, /*nb_positive*/
  778.     (unaryfunc)int_abs, /*nb_absolute*/
  779.     (inquiry)int_nonzero, /*nb_nonzero*/
  780.     (unaryfunc)int_invert, /*nb_invert*/
  781.     (binaryfunc)int_lshift, /*nb_lshift*/
  782.     (binaryfunc)int_rshift, /*nb_rshift*/
  783.     (binaryfunc)int_and, /*nb_and*/
  784.     (binaryfunc)int_xor, /*nb_xor*/
  785.     (binaryfunc)int_or, /*nb_or*/
  786.     0,        /*nb_coerce*/
  787.     (unaryfunc)int_int, /*nb_int*/
  788.     (unaryfunc)int_long, /*nb_long*/
  789.     (unaryfunc)int_float, /*nb_float*/
  790.     (unaryfunc)int_oct, /*nb_oct*/
  791.     (unaryfunc)int_hex, /*nb_hex*/
  792. };
  793.  
  794. PyTypeObject PyInt_Type = {
  795.     PyObject_HEAD_INIT(&PyType_Type)
  796.     0,
  797.     "int",
  798.     sizeof(PyIntObject),
  799.     0,
  800.     (destructor)int_dealloc, /*tp_dealloc*/
  801.     (printfunc)int_print, /*tp_print*/
  802.     0,        /*tp_getattr*/
  803.     0,        /*tp_setattr*/
  804.     (cmpfunc)int_compare, /*tp_compare*/
  805.     (reprfunc)int_repr, /*tp_repr*/
  806.     &int_as_number,    /*tp_as_number*/
  807.     0,        /*tp_as_sequence*/
  808.     0,        /*tp_as_mapping*/
  809.     (hashfunc)int_hash, /*tp_hash*/
  810. };
  811.  
  812. void
  813. PyInt_Fini()
  814. {
  815.     PyIntObject *p;
  816.     PyIntBlock *list, *next;
  817.     int i;
  818.     int bc, bf;    /* block count, number of freed blocks */
  819.     int irem, isum;    /* remaining unfreed ints per block, total */
  820.  
  821. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  822.         PyIntObject **q;
  823.  
  824.         i = NSMALLNEGINTS + NSMALLPOSINTS;
  825.         q = small_ints;
  826.         while (--i >= 0) {
  827.                 Py_XDECREF(*q);
  828.                 *q++ = NULL;
  829.         }
  830. #endif
  831.     bc = 0;
  832.     bf = 0;
  833.     isum = 0;
  834.     list = block_list;
  835.     block_list = NULL;
  836.     free_list = NULL;
  837.     while (list != NULL) {
  838.         bc++;
  839.         irem = 0;
  840.         for (i = 0, p = &list->objects[0];
  841.              i < N_INTOBJECTS;
  842.              i++, p++) {
  843.             if (PyInt_Check(p) && p->ob_refcnt != 0)
  844.                 irem++;
  845.         }
  846.         next = list->next;
  847.         if (irem) {
  848.             list->next = block_list;
  849.             block_list = list;
  850.             for (i = 0, p = &list->objects[0];
  851.                  i < N_INTOBJECTS;
  852.                  i++, p++) {
  853.                 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
  854.                     p->ob_type = (struct _typeobject *)
  855.                         free_list;
  856.                     free_list = p;
  857.                 }
  858. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  859.                 else if (-NSMALLNEGINTS <= p->ob_ival &&
  860.                      p->ob_ival < NSMALLPOSINTS &&
  861.                      small_ints[p->ob_ival +
  862.                             NSMALLNEGINTS] == NULL) {
  863.                     Py_INCREF(p);
  864.                     small_ints[p->ob_ival +
  865.                            NSMALLNEGINTS] = p;
  866.                 }
  867. #endif
  868.             }
  869.         }
  870.         else {
  871.             PyMem_FREE(list);
  872.             bf++;
  873.         }
  874.         isum += irem;
  875.         list = next;
  876.     }
  877.     if (!Py_VerboseFlag)
  878.         return;
  879.     fprintf(stderr, "# cleanup ints");
  880.     if (!isum) {
  881.         fprintf(stderr, "\n");
  882.     }
  883.     else {
  884.         fprintf(stderr,
  885.             ": %d unfreed int%s in %d out of %d block%s\n",
  886.             isum, isum == 1 ? "" : "s",
  887.             bc - bf, bc, bc == 1 ? "" : "s");
  888.     }
  889.     if (Py_VerboseFlag > 1) {
  890.         list = block_list;
  891.         while (list != NULL) {
  892.             for (i = 0, p = &list->objects[0];
  893.                  i < N_INTOBJECTS;
  894.                  i++, p++) {
  895.                 if (PyInt_Check(p) && p->ob_refcnt != 0)
  896.                     fprintf(stderr,
  897.                 "#   <int at %lx, refcnt=%d, val=%ld>\n",
  898.                         p, p->ob_refcnt, p->ob_ival);
  899.             }
  900.             list = list->next;
  901.         }
  902.     }
  903. }
  904.