home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Objects / intobject.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  11KB  |  580 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Integer object implementation */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29.  
  30. #ifdef __STDC__
  31. #include <limits.h>
  32. #endif
  33.  
  34. #ifndef LONG_MAX
  35. #define LONG_MAX 0X7FFFFFFFL
  36. #endif
  37.  
  38. #ifndef LONG_MIN
  39. #define LONG_MIN (-LONG_MAX-1)
  40. #endif
  41.  
  42. #ifndef CHAR_BIT
  43. #define CHAR_BIT 8
  44. #endif
  45.  
  46. #ifndef LONG_BIT
  47. #define LONG_BIT (CHAR_BIT * sizeof(long))
  48. #endif
  49.  
  50. long
  51. getmaxint()
  52. {
  53.     return LONG_MAX;    /* To initialize sys.maxint */
  54. }
  55.  
  56. /* Standard Booleans */
  57.  
  58. intobject FalseObject = {
  59.     OB_HEAD_INIT(&Inttype)
  60.     0
  61. };
  62.  
  63. intobject TrueObject = {
  64.     OB_HEAD_INIT(&Inttype)
  65.     1
  66. };
  67.  
  68. static object *
  69. err_ovf(msg)
  70.     char *msg;
  71. {
  72.     err_setstr(OverflowError, msg);
  73.     return NULL;
  74. }
  75.  
  76. /* Integers are quite normal objects, to make object handling uniform.
  77.    (Using odd pointers to represent integers would save much space
  78.    but require extra checks for this special case throughout the code.)
  79.    Since, a typical Python program spends much of its time allocating
  80.    and deallocating integers, these operations should be very fast.
  81.    Therefore we use a dedicated allocation scheme with a much lower
  82.    overhead (in space and time) than straight malloc(): a simple
  83.    dedicated free list, filled when necessary with memory from malloc().
  84. */
  85.  
  86. #define BLOCK_SIZE    1000    /* 1K less typical malloc overhead */
  87. #define N_INTOBJECTS    (BLOCK_SIZE / sizeof(intobject))
  88.  
  89. static intobject *
  90. fill_free_list()
  91. {
  92.     intobject *p, *q;
  93.     p = NEW(intobject, N_INTOBJECTS);
  94.     if (p == NULL)
  95.         return (intobject *)err_nomem();
  96.     q = p + N_INTOBJECTS;
  97.     while (--q > p)
  98.         *(intobject **)q = q-1;
  99.     *(intobject **)q = NULL;
  100.     return p + N_INTOBJECTS - 1;
  101. }
  102.  
  103. static intobject *free_list = NULL;
  104. #ifndef NSMALLPOSINTS
  105. #define NSMALLPOSINTS        100
  106. #endif
  107. #ifndef NSMALLNEGINTS
  108. #define NSMALLNEGINTS        1
  109. #endif
  110. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  111. /* References to small integers are saved in this array so that they
  112.    can be shared.
  113.    The integers that are saved are those in the range
  114.    -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
  115. */
  116. static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
  117. #endif
  118. #ifdef COUNT_ALLOCS
  119. int quick_int_allocs, quick_neg_int_allocs;
  120. #endif
  121.  
  122. object *
  123. newintobject(ival)
  124.     long ival;
  125. {
  126.     register intobject *v;
  127. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  128.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
  129.         (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
  130.         INCREF(v);
  131. #ifdef COUNT_ALLOCS
  132.         if (ival >= 0)
  133.             quick_int_allocs++;
  134.         else
  135.             quick_neg_int_allocs++;
  136. #endif
  137.         return (object *) v;
  138.     }
  139. #endif
  140.     if (free_list == NULL) {
  141.         if ((free_list = fill_free_list()) == NULL)
  142.             return NULL;
  143.     }
  144.     v = free_list;
  145.     free_list = *(intobject **)free_list;
  146.     v->ob_type = &Inttype;
  147.     v->ob_ival = ival;
  148.     NEWREF(v);
  149. #if NSMALLNEGINTS + NSMALLPOSINTS > 0
  150.     if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
  151.         /* save this one for a following allocation */
  152.         INCREF(v);
  153.         small_ints[ival + NSMALLNEGINTS] = v;
  154.     }
  155. #endif
  156.     return (object *) v;
  157. }
  158.  
  159. static void
  160. int_dealloc(v)
  161.     intobject *v;
  162. {
  163.     *(intobject **)v = free_list;
  164.     free_list = v;
  165. }
  166.  
  167. long
  168. getintvalue(op)
  169.     register object *op;
  170. {
  171.     if (!is_intobject(op)) {
  172.         err_badcall();
  173.         return -1;
  174.     }
  175.     else
  176.         return ((intobject *)op) -> ob_ival;
  177. }
  178.  
  179. /* Methods */
  180.  
  181. /* ARGSUSED */
  182. static int
  183. int_print(v, fp, flags)
  184.     intobject *v;
  185.     FILE *fp;
  186.     int flags; /* Not used but required by interface */
  187. {
  188.     fprintf(fp, "%ld", v->ob_ival);
  189.     return 0;
  190. }
  191.  
  192. static object *
  193. int_repr(v)
  194.     intobject *v;
  195. {
  196.     char buf[20];
  197.     sprintf(buf, "%ld", v->ob_ival);
  198.     return newstringobject(buf);
  199. }
  200.  
  201. static int
  202. int_compare(v, w)
  203.     intobject *v, *w;
  204. {
  205.     register long i = v->ob_ival;
  206.     register long j = w->ob_ival;
  207.     return (i < j) ? -1 : (i > j) ? 1 : 0;
  208. }
  209.  
  210. static long
  211. int_hash(v)
  212.     intobject *v;
  213. {
  214.     long x = v -> ob_ival;
  215.     if (x == -1)
  216.         x = -2;
  217.     return x;
  218. }
  219.  
  220. static object *
  221. int_add(v, w)
  222.     intobject *v;
  223.     intobject *w;
  224. {
  225.     register long a, b, x;
  226.     a = v->ob_ival;
  227.     b = w->ob_ival;
  228.     x = a + b;
  229.     if ((x^a) < 0 && (x^b) < 0)
  230.         return err_ovf("integer addition");
  231.     return newintobject(x);
  232. }
  233.  
  234. static object *
  235. int_sub(v, w)
  236.     intobject *v;
  237.     intobject *w;
  238. {
  239.     register long a, b, x;
  240.     a = v->ob_ival;
  241.     b = w->ob_ival;
  242.     x = a - b;
  243.     if ((x^a) < 0 && (x^~b) < 0)
  244.         return err_ovf("integer subtraction");
  245.     return newintobject(x);
  246. }
  247.  
  248. static object *
  249. int_mul(v, w)
  250.     intobject *v;
  251.     intobject *w;
  252. {
  253.     register long a, b;
  254.     double x;
  255.     a = v->ob_ival;
  256.     b = w->ob_ival;
  257.     x = (double)a * (double)b;
  258.     if (x > LONG_MAX || x < (double) (long) (LONG_MIN))
  259.         return err_ovf("integer multiplication");
  260.     return newintobject(a * b);
  261. }
  262.  
  263. static int
  264. i_divmod(x, y, p_xdivy, p_xmody)
  265.     register intobject *x, *y;
  266.     long *p_xdivy, *p_xmody;
  267. {
  268.     long xi = x->ob_ival;
  269.     long yi = y->ob_ival;
  270.     long xdivy, xmody;
  271.     
  272.     if (yi == 0) {
  273.         err_setstr(ZeroDivisionError, "integer division or modulo");
  274.         return -1;
  275.     }
  276.     if (yi < 0) {
  277.         if (xi < 0)
  278.             xdivy = -xi / -yi;
  279.         else
  280.             xdivy = - (xi / -yi);
  281.     }
  282.     else {
  283.         if (xi < 0)
  284.             xdivy = - (-xi / yi);
  285.         else
  286.             xdivy = xi / yi;
  287.     }
  288.     xmody = xi - xdivy*yi;
  289.     if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) {
  290.         xmody += yi;
  291.         xdivy -= 1;
  292.     }
  293.     *p_xdivy = xdivy;
  294.     *p_xmody = xmody;
  295.     return 0;
  296. }
  297.  
  298. static object *
  299. int_div(x, y)
  300.     intobject *x;
  301.     intobject *y;
  302. {
  303.     long d, m;
  304.     if (i_divmod(x, y, &d, &m) < 0)
  305.         return NULL;
  306.     return newintobject(d);
  307. }
  308.  
  309. static object *
  310. int_mod(x, y)
  311.     intobject *x;
  312.     intobject *y;
  313. {
  314.     long d, m;
  315.     if (i_divmod(x, y, &d, &m) < 0)
  316.         return NULL;
  317.     return newintobject(m);
  318. }
  319.  
  320. static object *
  321. int_divmod(x, y)
  322.     intobject *x;
  323.     intobject *y;
  324. {
  325.     object *v, *v0, *v1;
  326.     long d, m;
  327.     if (i_divmod(x, y, &d, &m) < 0)
  328.         return NULL;
  329.     return mkvalue("(ll)", d, m);
  330. }
  331.  
  332. static object *
  333. int_pow(v, w)
  334.     intobject *v;
  335.     intobject *w;
  336. {
  337.     register long iv, iw, ix;
  338.     iv = v->ob_ival;
  339.     iw = w->ob_ival;
  340.     if (iw < 0) {
  341.         err_setstr(ValueError, "integer to the negative power");
  342.         return NULL;
  343.     }
  344.     ix = 1;
  345.     while (--iw >= 0) {
  346.         long prev = ix;
  347.         ix = ix * iv;
  348.         if (iv == 0)
  349.             break; /* 0 to some power -- avoid ix / 0 */
  350.         if (ix / iv != prev)
  351.             return err_ovf("integer pow()");
  352.     }
  353.     return newintobject(ix);
  354. }
  355.  
  356. static object *
  357. int_neg(v)
  358.     intobject *v;
  359. {
  360.     register long a, x;
  361.     a = v->ob_ival;
  362.     x = -a;
  363.     if (a < 0 && x < 0)
  364.         return err_ovf("integer negation");
  365.     return newintobject(x);
  366. }
  367.  
  368. static object *
  369. int_pos(v)
  370.     intobject *v;
  371. {
  372.     INCREF(v);
  373.     return (object *)v;
  374. }
  375.  
  376. static object *
  377. int_abs(v)
  378.     intobject *v;
  379. {
  380.     if (v->ob_ival >= 0)
  381.         return int_pos(v);
  382.     else
  383.         return int_neg(v);
  384. }
  385.  
  386. static int
  387. int_nonzero(v)
  388.     intobject *v;
  389. {
  390.     return v->ob_ival != 0;
  391. }
  392.  
  393. static object *
  394. int_invert(v)
  395.     intobject *v;
  396. {
  397.     return newintobject(~v->ob_ival);
  398. }
  399.  
  400. static object *
  401. int_lshift(v, w)
  402.     intobject *v;
  403.     intobject *w;
  404. {
  405.     register long a, b;
  406.     a = v->ob_ival;
  407.     b = w->ob_ival;
  408.     if (b < 0) {
  409.         err_setstr(ValueError, "negative shift count");
  410.         return NULL;
  411.     }
  412.     if (a == 0 || b == 0) {
  413.         INCREF(v);
  414.         return (object *) v;
  415.     }
  416.     if (b >= LONG_BIT) {
  417.         return newintobject(0L);
  418.     }
  419.     a = (unsigned long)a << b;
  420.     return newintobject(a);
  421. }
  422.  
  423. static object *
  424. int_rshift(v, w)
  425.     intobject *v;
  426.     intobject *w;
  427. {
  428.     register long a, b;
  429.     a = v->ob_ival;
  430.     b = w->ob_ival;
  431.     if (b < 0) {
  432.         err_setstr(ValueError, "negative shift count");
  433.         return NULL;
  434.     }
  435.     if (a == 0 || b == 0) {
  436.         INCREF(v);
  437.         return (object *) v;
  438.     }
  439.     if (b >= LONG_BIT) {
  440.         if (a < 0)
  441.             a = -1;
  442.         else
  443.             a = 0;
  444.     }
  445.     else {
  446.         if (a < 0)
  447.             a = ~( ~(unsigned long)a >> b );
  448.         else
  449.             a = (unsigned long)a >> b;
  450.     }
  451.     return newintobject(a);
  452. }
  453.  
  454. static object *
  455. int_and(v, w)
  456.     intobject *v;
  457.     intobject *w;
  458. {
  459.     register long a, b;
  460.     a = v->ob_ival;
  461.     b = w->ob_ival;
  462.     return newintobject(a & b);
  463. }
  464.  
  465. static object *
  466. int_xor(v, w)
  467.     intobject *v;
  468.     intobject *w;
  469. {
  470.     register long a, b;
  471.     a = v->ob_ival;
  472.     b = w->ob_ival;
  473.     return newintobject(a ^ b);
  474. }
  475.  
  476. static object *
  477. int_or(v, w)
  478.     intobject *v;
  479.     intobject *w;
  480. {
  481.     register long a, b;
  482.     a = v->ob_ival;
  483.     b = w->ob_ival;
  484.     return newintobject(a | b);
  485. }
  486.  
  487. static object *
  488. int_int(v)
  489.     intobject *v;
  490. {
  491.     INCREF(v);
  492.     return (object *)v;
  493. }
  494.  
  495. static object *
  496. int_long(v)
  497.     intobject *v;
  498. {
  499.     return newlongobject((v -> ob_ival));
  500. }
  501.  
  502. static object *
  503. int_float(v)
  504.     intobject *v;
  505. {
  506.     return newfloatobject((double)(v -> ob_ival));
  507. }
  508.  
  509. static object *
  510. int_oct(v)
  511.     intobject *v;
  512. {
  513.     char buf[20];
  514.     long x = v -> ob_ival;
  515.     if (x == 0)
  516.         strcpy(buf, "0");
  517.     else if (x > 0)
  518.         sprintf(buf, "0%lo", x);
  519.     else
  520.         sprintf(buf, "-0%lo", -x);
  521.     return newstringobject(buf);
  522. }
  523.  
  524. static object *
  525. int_hex(v)
  526.     intobject *v;
  527. {
  528.     char buf[20];
  529.     long x = v -> ob_ival;
  530.     if (x >= 0)
  531.         sprintf(buf, "0x%lx", x);
  532.     else
  533.         sprintf(buf, "-0x%lx", -x);
  534.     return newstringobject(buf);
  535. }
  536.  
  537. static number_methods int_as_number = {
  538.     (binaryfunc)int_add, /*nb_add*/
  539.     (binaryfunc)int_sub, /*nb_subtract*/
  540.     (binaryfunc)int_mul, /*nb_multiply*/
  541.     (binaryfunc)int_div, /*nb_divide*/
  542.     (binaryfunc)int_mod, /*nb_remainder*/
  543.     (binaryfunc)int_divmod, /*nb_divmod*/
  544.     (binaryfunc)int_pow, /*nb_power*/
  545.     (unaryfunc)int_neg, /*nb_negative*/
  546.     (unaryfunc)int_pos, /*nb_positive*/
  547.     (unaryfunc)int_abs, /*nb_absolute*/
  548.     (inquiry)int_nonzero, /*nb_nonzero*/
  549.     (unaryfunc)int_invert, /*nb_invert*/
  550.     (binaryfunc)int_lshift, /*nb_lshift*/
  551.     (binaryfunc)int_rshift, /*nb_rshift*/
  552.     (binaryfunc)int_and, /*nb_and*/
  553.     (binaryfunc)int_xor, /*nb_xor*/
  554.     (binaryfunc)int_or, /*nb_or*/
  555.     0,        /*nb_coerce*/
  556.     (unaryfunc)int_int, /*nb_int*/
  557.     (unaryfunc)int_long, /*nb_long*/
  558.     (unaryfunc)int_float, /*nb_float*/
  559.     (unaryfunc)int_oct, /*nb_oct*/
  560.     (unaryfunc)int_hex, /*nb_hex*/
  561. };
  562.  
  563. typeobject Inttype = {
  564.     OB_HEAD_INIT(&Typetype)
  565.     0,
  566.     "int",
  567.     sizeof(intobject),
  568.     0,
  569.     (destructor)int_dealloc, /*tp_dealloc*/
  570.     (printfunc)int_print, /*tp_print*/
  571.     0,        /*tp_getattr*/
  572.     0,        /*tp_setattr*/
  573.     (cmpfunc)int_compare, /*tp_compare*/
  574.     (reprfunc)int_repr, /*tp_repr*/
  575.     &int_as_number,    /*tp_as_number*/
  576.     0,        /*tp_as_sequence*/
  577.     0,        /*tp_as_mapping*/
  578.     (hashfunc)int_hash, /*tp_hash*/
  579. };
  580.