home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Objects / intobject.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  20KB  |  958 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. #include <ctype.h>
  36. #include "other/intobject_c.h"
  37.  
  38. #ifdef HAVE_LIMITS_H
  39. #include <limits.h>
  40. #endif
  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((PyObject *)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. PyObject *
  224. PyInt_FromString(s, pend, base)
  225.     char *s;
  226.     char **pend;
  227.     int base;
  228. {
  229.     char *end;
  230.     long x;
  231.     char buffer[256]; /* For errors */
  232.  
  233.     if ((base != 0 && base < 2) || base > 36) {
  234.         PyErr_SetString(PyExc_ValueError, "invalid base for int()");
  235.         return NULL;
  236.     }
  237.  
  238.     while (*s && isspace(Py_CHARMASK(*s)))
  239.         s++;
  240.     errno = 0;
  241.     if (base == 0 && s[0] == '0')
  242.         x = (long) PyOS_strtoul(s, &end, base);
  243.     else
  244.         x = PyOS_strtol(s, &end, base);
  245.     if (end == s || !isalnum(end[-1]))
  246.         goto bad;
  247.     while (*end && isspace(Py_CHARMASK(*end)))
  248.         end++;
  249.     if (*end != '\0') {
  250.   bad:
  251.         sprintf(buffer, "invalid literal for int(): %.200s", s);
  252.         PyErr_SetString(PyExc_ValueError, buffer);
  253.         return NULL;
  254.     }
  255.     else if (errno != 0) {
  256.         sprintf(buffer, "int() literal too large: %.200s", s);
  257.         PyErr_SetString(PyExc_ValueError, buffer);
  258.         return NULL;
  259.     }
  260.     if (pend)
  261.         *pend = end;
  262.     return PyInt_FromLong(x);
  263. }
  264.  
  265. /* Methods */
  266.  
  267. /* ARGSUSED */
  268. static int
  269. int_print(v, fp, flags)
  270.     PyIntObject *v;
  271.     FILE *fp;
  272.     int flags; /* Not used but required by interface */
  273. {
  274.     fprintf(fp, "%ld", v->ob_ival);
  275.     return 0;
  276. }
  277.  
  278. static PyObject *
  279. int_repr(v)
  280.     PyIntObject *v;
  281. {
  282.     char buf[20];
  283.     sprintf(buf, "%ld", v->ob_ival);
  284.     return PyString_FromString(buf);
  285. }
  286.  
  287. static int
  288. int_compare(v, w)
  289.     PyIntObject *v, *w;
  290. {
  291.     register long i = v->ob_ival;
  292.     register long j = w->ob_ival;
  293.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  294. }
  295.  
  296. static long
  297. int_hash(v)
  298.     PyIntObject *v;
  299. {
  300.     /* XXX If this is changed, you also need to change the way
  301.        Python's long, float and complex types are hashed. */
  302.     long x = v -> ob_ival;
  303.     if (x == -1)
  304.         x = -2;
  305.     return x;
  306. }
  307.  
  308. static PyObject *
  309. int_add(v, w)
  310.     PyIntObject *v;
  311.     PyIntObject *w;
  312. {
  313.     register long a, b, x;
  314.     a = v->ob_ival;
  315.     b = w->ob_ival;
  316.     x = a + b;
  317.     if ((x^a) < 0 && (x^b) < 0)
  318.         return err_ovf("integer addition");
  319.     return PyInt_FromLong(x);
  320. }
  321.  
  322. static PyObject *
  323. int_sub(v, w)
  324.     PyIntObject *v;
  325.     PyIntObject *w;
  326. {
  327.     register long a, b, x;
  328.     a = v->ob_ival;
  329.     b = w->ob_ival;
  330.     x = a - b;
  331.     if ((x^a) < 0 && (x^~b) < 0)
  332.         return err_ovf("integer subtraction");
  333.     return PyInt_FromLong(x);
  334. }
  335.  
  336. /*
  337. Integer overflow checking used to be done using a double, but on 64
  338. bit machines (where both long and double are 64 bit) this fails
  339. because the double doesn't have enouvg precision.  John Tromp suggests
  340. the following algorithm:
  341.  
  342. Suppose again we normalize a and b to be nonnegative.
  343. Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
  344. Now we test ah and bh against zero and get essentially 3 possible outcomes.
  345.  
  346. 1) both ah and bh > 0 : then report overflow
  347.  
  348. 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
  349.                         negative
  350.  
  351. 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if it's >= 2^31
  352.                         compute al*bl and report overflow if it's negative
  353.                         add (ah*bl)<<32 to al*bl and report overflow if
  354.                         it's negative
  355.  
  356. In case of no overflow the result is then negated if necessary.
  357.  
  358. The majority of cases will be 2), in which case this method is the same as
  359. what I suggested before. If multiplication is expensive enough, then the
  360. other method is faster on case 3), but also more work to program, so I
  361. guess the above is the preferred solution.
  362.  
  363. */
  364.  
  365. static PyObject *
  366. int_mul(v, w)
  367.     PyIntObject *v;
  368.     PyIntObject *w;
  369. {
  370.     long a, b, ah, bh, x, y;
  371.     int s = 1;
  372.  
  373.     a = v->ob_ival;
  374.     b = w->ob_ival;
  375.     ah = a >> (LONG_BIT/2);
  376.     bh = b >> (LONG_BIT/2);
  377.  
  378.     /* Quick test for common case: two small positive ints */
  379.  
  380.     if (ah == 0 && bh == 0) {
  381.         x = a*b;
  382.         if (x < 0)
  383.             goto bad;
  384.         return PyInt_FromLong(x);
  385.     }
  386.  
  387.     /* Arrange that a >= b >= 0 */
  388.  
  389.     if (a < 0) {
  390.         a = -a;
  391.         if (a < 0) {
  392.             /* Largest negative */
  393.             if (b == 0 || b == 1) {
  394.                 x = a*b;
  395.                 goto ok;
  396.             }
  397.             else
  398.                 goto bad;
  399.         }
  400.         s = -s;
  401.         ah = a >> (LONG_BIT/2);
  402.     }
  403.     if (b < 0) {
  404.         b = -b;
  405.         if (b < 0) {
  406.             /* Largest negative */
  407.             if (a == 0 || (a == 1 && s == 1)) {
  408.                 x = a*b;
  409.                 goto ok;
  410.             }
  411.             else
  412.                 goto bad;
  413.         }
  414.         s = -s;
  415.         bh = b >> (LONG_BIT/2);
  416.     }
  417.  
  418.     /* 1) both ah and bh > 0 : then report overflow */
  419.  
  420.     if (ah != 0 && bh != 0)
  421.         goto bad;
  422.  
  423.     /* 2) both ah and bh = 0 : then compute a*b and report
  424.                    overflow if it comes out negative */
  425.  
  426.     if (ah == 0 && bh == 0) {
  427.         x = a*b;
  428.         if (x < 0)
  429.             goto bad;
  430.         return PyInt_FromLong(x*s);
  431.     }
  432.  
  433.     if (a < b) {
  434.         /* Swap */
  435.         x = a;
  436.         a = b;
  437.         b = x;
  438.         ah = bh;
  439.         /* bh not used beyond this point */
  440.     }
  441.  
  442.     /* 3) ah > 0 and bh = 0  : compute ah*bl and report overflow if
  443.                    it's >= 2^31
  444.                         compute al*bl and report overflow if it's negative
  445.                         add (ah*bl)<<32 to al*bl and report overflow if
  446.                         it's negative
  447.             (NB b == bl in this case, and we make a = al) */
  448.  
  449.     y = ah*b;
  450.     if (y >= (1L << (LONG_BIT/2 - 1)))
  451.         goto bad;
  452.     a &= (1L << (LONG_BIT/2)) - 1;
  453.     x = a*b;
  454.     if (x < 0)
  455.         goto bad;
  456.     x += y << (LONG_BIT/2);
  457.     if (x < 0)
  458.         goto bad;
  459.  ok:
  460.     return PyInt_FromLong(x * s);
  461.  
  462.  bad:
  463.     return err_ovf("integer multiplication");
  464. }
  465.  
  466. static int
  467. i_divmod(x, y, p_xdivy, p_xmody)
  468.     register PyIntObject *x, *y;
  469.     long *p_xdivy, *p_xmody;
  470. {
  471.     long xi = x->ob_ival;
  472.     long yi = y->ob_ival;
  473.     long xdivy, xmody;
  474.     
  475.     if (yi == 0) {
  476.         PyErr_SetString(PyExc_ZeroDivisionError,
  477.                 "integer division or modulo");
  478.         return -1;
  479.     }
  480.     if (yi < 0) {
  481.         if (xi < 0) {
  482.             if (yi == -1 && -xi < 0) {
  483.                 /* most negative / -1 */
  484.                 err_ovf("integer division");
  485.                 return -1;
  486.             }
  487.             xdivy = -xi / -yi;
  488.         }
  489.         else
  490.             xdivy = - (xi / -yi);
  491.     }
  492.     else {
  493.         if (xi < 0)
  494.             xdivy = - (-xi / yi);
  495.         else
  496.             xdivy = xi / yi;
  497.     }
  498.     xmody = xi - xdivy*yi;
  499.     if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
  500.         xmody += yi;
  501.         xdivy -= 1;
  502.     }
  503.     *p_xdivy = xdivy;
  504.     *p_xmody = xmody;
  505.     return 0;
  506. }
  507.  
  508. static PyObject *
  509. int_div(x, y)
  510.     PyIntObject *x;
  511.     PyIntObject *y;
  512. {
  513.     long d, m;
  514.     if (i_divmod(x, y, &d, &m) < 0)
  515.         return NULL;
  516.     return PyInt_FromLong(d);
  517. }
  518.  
  519. static PyObject *
  520. int_mod(x, y)
  521.     PyIntObject *x;
  522.     PyIntObject *y;
  523. {
  524.     long d, m;
  525.     if (i_divmod(x, y, &d, &m) < 0)
  526.         return NULL;
  527.     return PyInt_FromLong(m);
  528. }
  529.  
  530. static PyObject *
  531. int_divmod(x, y)
  532.     PyIntObject *x;
  533.     PyIntObject *y;
  534. {
  535.     long d, m;
  536.     if (i_divmod(x, y, &d, &m) < 0)
  537.         return NULL;
  538.     return Py_BuildValue("(ll)", d, m);
  539. }
  540.  
  541. static PyObject *
  542. int_pow(v, w, z)
  543.     PyIntObject *v;
  544.     PyIntObject *w;
  545.     PyIntObject *z;
  546. {
  547. #if 1
  548.     register long iv, iw, iz=0, ix, temp, prev;
  549.     iv = v->ob_ival;
  550.     iw = w->ob_ival;
  551.     if (iw < 0) {
  552.         PyErr_SetString(PyExc_ValueError,
  553.                 "integer to the negative power");
  554.         return NULL;
  555.     }
  556.      if ((PyObject *)z != Py_None) {
  557.         iz = z->ob_ival;
  558.         if (iz == 0) {
  559.             PyErr_SetString(PyExc_ValueError,
  560.                     "pow(x, y, z) with z==0");
  561.             return NULL;
  562.         }
  563.     }
  564.     /*
  565.      * XXX: The original exponentiation code stopped looping
  566.      * when temp hit zero; this code will continue onwards
  567.      * unnecessarily, but at least it won't cause any errors.
  568.      * Hopefully the speed improvement from the fast exponentiation
  569.      * will compensate for the slight inefficiency.
  570.      * XXX: Better handling of overflows is desperately needed.
  571.      */
  572.      temp = iv;
  573.     ix = 1;
  574.     while (iw > 0) {
  575.          prev = ix;    /* Save value for overflow check */
  576.          if (iw & 1) {    
  577.              ix = ix*temp;
  578.             if (temp == 0)
  579.                 break; /* Avoid ix / 0 */
  580.             if (ix / temp != prev)
  581.                 return err_ovf("integer exponentiation");
  582.         }
  583.          iw >>= 1;    /* Shift exponent down by 1 bit */
  584.             if (iw==0) break;
  585.          prev = temp;
  586.          temp *= temp;    /* Square the value of temp */
  587.          if (prev!=0 && temp/prev!=prev)
  588.             return err_ovf("integer exponentiation");
  589.          if (iz) {
  590.             /* If we did a multiplication, perform a modulo */
  591.              ix = ix % iz;
  592.              temp = temp % iz;
  593.         }
  594.     }
  595.     if (iz) {
  596.          PyObject *t1, *t2;
  597.          long int div, mod;
  598.          t1=PyInt_FromLong(ix); 
  599.         t2=PyInt_FromLong(iz);
  600.          if (t1==NULL || t2==NULL ||
  601.              i_divmod((PyIntObject *)t1,
  602.                  (PyIntObject *)t2, &div, &mod)<0)
  603.         {
  604.              Py_XDECREF(t1);
  605.              Py_XDECREF(t2);
  606.             return(NULL);
  607.         }
  608.         Py_DECREF(t1);
  609.         Py_DECREF(t2);
  610.          ix=mod;
  611.     }
  612.     return PyInt_FromLong(ix);
  613. #else
  614.     register long iv, iw, ix;
  615.     iv = v->ob_ival;
  616.     iw = w->ob_ival;
  617.     if (iw < 0) {
  618.         PyErr_SetString(PyExc_ValueError,
  619.                 "integer to the negative power");
  620.         return NULL;
  621.     }
  622.     if ((PyObject *)z != Py_None) {
  623.         PyErr_SetString(PyExc_TypeError,
  624.                 "pow(int, int, int) not yet supported");
  625.         return NULL;
  626.     }
  627.     ix = 1;
  628.     while (--iw >= 0) {
  629.         long prev = ix;
  630.         ix = ix * iv;
  631.         if (iv == 0)
  632.             break; /* 0 to some power -- avoid ix / 0 */
  633.         if (ix / iv != prev)
  634.             return err_ovf("integer exponentiation");
  635.     }
  636.     return PyInt_FromLong(ix);
  637. #endif
  638. }                
  639.  
  640. static PyObject *
  641. int_neg(v)
  642.     PyIntObject *v;
  643. {
  644.     register long a, x;
  645.     a = v->ob_ival;
  646.     x = -a;
  647.     if (a < 0 && x < 0)
  648.         return err_ovf("integer negation");
  649.     return PyInt_FromLong(x);
  650. }
  651.  
  652. static PyObject *
  653. int_pos(v)
  654.     PyIntObject *v;
  655. {
  656.     Py_INCREF(v);
  657.     return (PyObject *)v;
  658. }
  659.  
  660. static PyObject *
  661. int_abs(v)
  662.     PyIntObject *v;
  663. {
  664.     if (v->ob_ival >= 0)
  665.         return int_pos(v);
  666.     else
  667.         return int_neg(v);
  668. }
  669.  
  670. static int
  671. int_nonzero(v)
  672.     PyIntObject *v;
  673. {
  674.     return v->ob_ival != 0;
  675. }
  676.  
  677. static PyObject *
  678. int_invert(v)
  679.     PyIntObject *v;
  680. {
  681.     return PyInt_FromLong(~v->ob_ival);
  682. }
  683.  
  684. static PyObject *
  685. int_lshift(v, w)
  686.     PyIntObject *v;
  687.     PyIntObject *w;
  688. {
  689.     register long a, b;
  690.     a = v->ob_ival;
  691.     b = w->ob_ival;
  692.     if (b < 0) {
  693.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  694.         return NULL;
  695.     }
  696.     if (a == 0 || b == 0) {
  697.         Py_INCREF(v);
  698.         return (PyObject *) v;
  699.     }
  700.     if (b >= LONG_BIT) {
  701.         return PyInt_FromLong(0L);
  702.     }
  703.     a = (unsigned long)a << b;
  704.     return PyInt_FromLong(a);
  705. }
  706.  
  707. static PyObject *
  708. int_rshift(v, w)
  709.     PyIntObject *v;
  710.     PyIntObject *w;
  711. {
  712.     register long a, b;
  713.     a = v->ob_ival;
  714.     b = w->ob_ival;
  715.     if (b < 0) {
  716.         PyErr_SetString(PyExc_ValueError, "negative shift count");
  717.         return NULL;
  718.     }
  719.     if (a == 0 || b == 0) {
  720.         Py_INCREF(v);
  721.         return (PyObject *) v;
  722.     }
  723.     if (b >= LONG_BIT) {
  724.         if (a < 0)
  725.             a = -1;
  726.         else
  727.             a = 0;
  728.     }
  729.     else {
  730.         if (a < 0)
  731.             a = ~( ~(unsigned long)a >> b );
  732.         else
  733.             a = (unsigned long)a >> b;
  734.     }
  735.     return PyInt_FromLong(a);
  736. }
  737.  
  738. static PyObject *
  739. int_and(v, w)
  740.     PyIntObject *v;
  741.     PyIntObject *w;
  742. {
  743.     register long a, b;
  744.     a = v->ob_ival;
  745.     b = w->ob_ival;
  746.     return PyInt_FromLong(a & b);
  747. }
  748.  
  749. static PyObject *
  750. int_xor(v, w)
  751.     PyIntObject *v;
  752.     PyIntObject *w;
  753. {
  754.     register long a, b;
  755.     a = v->ob_ival;
  756.     b = w->ob_ival;
  757.     return PyInt_FromLong(a ^ b);
  758. }
  759.  
  760. static PyObject *
  761. int_or(v, w)
  762.     PyIntObject *v;
  763.     PyIntObject *w;
  764. {
  765.     register long a, b;
  766.     a = v->ob_ival;
  767.     b = w->ob_ival;
  768.     return PyInt_FromLong(a | b);
  769. }
  770.  
  771. static PyObject *
  772. int_int(v)
  773.     PyIntObject *v;
  774. {
  775.     Py_INCREF(v);
  776.     return (PyObject *)v;
  777. }
  778.  
  779. static PyObject *
  780. int_long(v)
  781.     PyIntObject *v;
  782. {
  783.     return PyLong_FromLong((v -> ob_ival));
  784. }
  785.  
  786. static PyObject *
  787. int_float(v)
  788.     PyIntObject *v;
  789. {
  790. #ifndef WITHOUT_FLOAT
  791.     return PyFloat_FromDouble((double)(v -> ob_ival));
  792. #else /* !WITHOUT_FLOAT */
  793.     PyErr_SetString(PyExc_MissingFeatureError,
  794.                "Float objects are not provided in this python build");
  795.     return NULL;
  796. #endif /* WITHOUT_FLOAT */
  797. }
  798.  
  799. static PyObject *
  800. int_oct(v)
  801.     PyIntObject *v;
  802. {
  803.     char buf[100];
  804.     long x = v -> ob_ival;
  805.     if (x == 0)
  806.         strcpy(buf, "0");
  807.     else
  808.         sprintf(buf, "0%lo", x);
  809.     return PyString_FromString(buf);
  810. }
  811.  
  812. static PyObject *
  813. int_hex(v)
  814.     PyIntObject *v;
  815. {
  816.     char buf[100];
  817.     long x = v -> ob_ival;
  818.     sprintf(buf, "0x%lx", x);
  819.     return PyString_FromString(buf);
  820. }
  821.  
  822. static PyNumberMethods int_as_number = {
  823.     (binaryfunc)int_add, /*nb_add*/
  824.     (binaryfunc)int_sub, /*nb_subtract*/
  825.     (binaryfunc)int_mul, /*nb_multiply*/
  826.     (binaryfunc)int_div, /*nb_divide*/
  827.     (binaryfunc)int_mod, /*nb_remainder*/
  828.     (binaryfunc)int_divmod, /*nb_divmod*/
  829.     (ternaryfunc)int_pow, /*nb_power*/
  830.     (unaryfunc)int_neg, /*nb_negative*/
  831.     (unaryfunc)int_pos, /*nb_positive*/
  832.     (unaryfunc)int_abs, /*nb_absolute*/
  833.     (inquiry)int_nonzero, /*nb_nonzero*/
  834.     (unaryfunc)int_invert, /*nb_invert*/
  835.     (binaryfunc)int_lshift, /*nb_lshift*/
  836.     (binaryfunc)int_rshift, /*nb_rshift*/
  837.     (binaryfunc)int_and, /*nb_and*/
  838.     (binaryfunc)int_xor, /*nb_xor*/
  839.     (binaryfunc)int_or, /*nb_or*/
  840.     0,        /*nb_coerce*/
  841.     (unaryfunc)int_int, /*nb_int*/
  842.     (unaryfunc)int_long, /*nb_long*/
  843.     (unaryfunc)int_float, /*nb_float*/
  844.     (unaryfunc)int_oct, /*nb_oct*/
  845.     (unaryfunc)int_hex, /*nb_hex*/
  846. };
  847.  
  848. PyTypeObject PyInt_Type = {
  849.     PyObject_HEAD_INIT(&PyType_Type)
  850.     0,
  851.     "int",
  852.     sizeof(PyIntObject),
  853.     0,
  854.     (destructor)int_dealloc, /*tp_dealloc*/
  855.     (printfunc)int_print, /*tp_print*/
  856.     0,        /*tp_getattr*/
  857.     0,        /*tp_setattr*/
  858.     (cmpfunc)int_compare, /*tp_compare*/
  859.     (reprfunc)int_repr, /*tp_repr*/
  860.     &int_as_number,    /*tp_as_number*/
  861.     0,        /*tp_as_sequence*/
  862.     0,        /*tp_as_mapping*/
  863.     (hashfunc)int_hash, /*tp_hash*/
  864. };
  865.  
  866. void
  867. PyInt_Fini()
  868. {
  869.     PyIntObject *p;
  870.     PyIntBlock *list, *next;
  871.     int i;
  872.     int bc, bf;    /* block count, number of freed blocks */
  873.     int irem, isum;    /* remaining unfreed ints per block, total */
  874.  
  875. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  876.         PyIntObject **q;
  877.  
  878.         i = NSMALLNEGINTS + NSMALLPOSINTS;
  879.         q = small_ints;
  880.         while (--i >= 0) {
  881.                 Py_XDECREF(*q);
  882.                 *q++ = NULL;
  883.         }
  884. #endif
  885.     bc = 0;
  886.     bf = 0;
  887.     isum = 0;
  888.     list = block_list;
  889.     block_list = NULL;
  890.     free_list = NULL;
  891.     while (list != NULL) {
  892.         bc++;
  893.         irem = 0;
  894.         for (i = 0, p = &list->objects[0];
  895.              i < N_INTOBJECTS;
  896.              i++, p++) {
  897.             if (PyInt_Check(p) && p->ob_refcnt != 0)
  898.                 irem++;
  899.         }
  900.         next = list->next;
  901.         if (irem) {
  902.             list->next = block_list;
  903.             block_list = list;
  904.             for (i = 0, p = &list->objects[0];
  905.                  i < N_INTOBJECTS;
  906.                  i++, p++) {
  907.                 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
  908.                     p->ob_type = (struct _typeobject *)
  909.                         free_list;
  910.                     free_list = p;
  911.                 }
  912. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  913.                 else if (-NSMALLNEGINTS <= p->ob_ival &&
  914.                      p->ob_ival < NSMALLPOSINTS &&
  915.                      small_ints[p->ob_ival +
  916.                             NSMALLNEGINTS] == NULL) {
  917.                     Py_INCREF(p);
  918.                     small_ints[p->ob_ival +
  919.                            NSMALLNEGINTS] = p;
  920.                 }
  921. #endif
  922.             }
  923.         }
  924.         else {
  925.             PyMem_FREE(list);
  926.             bf++;
  927.         }
  928.         isum += irem;
  929.         list = next;
  930.     }
  931.     if (!Py_VerboseFlag)
  932.         return;
  933.     fprintf(stderr, "# cleanup ints");
  934.     if (!isum) {
  935.         fprintf(stderr, "\n");
  936.     }
  937.     else {
  938.         fprintf(stderr,
  939.             ": %d unfreed int%s in %d out of %d block%s\n",
  940.             isum, isum == 1 ? "" : "s",
  941.             bc - bf, bc, bc == 1 ? "" : "s");
  942.     }
  943.     if (Py_VerboseFlag > 1) {
  944.         list = block_list;
  945.         while (list != NULL) {
  946.             for (i = 0, p = &list->objects[0];
  947.                  i < N_INTOBJECTS;
  948.                  i++, p++) {
  949.                 if (PyInt_Check(p) && p->ob_refcnt != 0)
  950.                     fprintf(stderr,
  951.                 "#   <int at %lx, refcnt=%d, val=%ld>\n",
  952.                         p, p->ob_refcnt, p->ob_ival);
  953.             }
  954.             list = list->next;
  955.         }
  956.     }
  957. }
  958.