1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:07:38 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Integer object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1994-08-29 09:48:32 -03:00
|
|
|
#ifdef HAVE_LIMITS_H
|
1993-10-26 12:21:51 -03:00
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LONG_MAX
|
|
|
|
#define LONG_MAX 0X7FFFFFFFL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LONG_MIN
|
|
|
|
#define LONG_MIN (-LONG_MAX-1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CHAR_BIT
|
|
|
|
#define CHAR_BIT 8
|
|
|
|
#endif
|
|
|
|
|
1993-11-23 13:53:17 -04:00
|
|
|
#ifndef LONG_BIT
|
1993-10-26 12:21:51 -03:00
|
|
|
#define LONG_BIT (CHAR_BIT * sizeof(long))
|
1993-11-23 13:53:17 -04:00
|
|
|
#endif
|
1993-10-26 12:21:51 -03:00
|
|
|
|
1993-12-24 06:22:45 -04:00
|
|
|
long
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInt_GetMax()
|
1993-12-24 06:22:45 -04:00
|
|
|
{
|
|
|
|
return LONG_MAX; /* To initialize sys.maxint */
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Standard Booleans */
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject _Py_ZeroStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyInt_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0
|
|
|
|
};
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject _Py_TrueStruct = {
|
|
|
|
PyObject_HEAD_INIT(&PyInt_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
1
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-12-10 09:57:36 -04:00
|
|
|
err_ovf(msg)
|
|
|
|
char *msg;
|
1990-10-14 17:02:26 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_OverflowError, msg);
|
1990-10-14 17:02:26 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Integers are quite normal objects, to make object handling uniform.
|
|
|
|
(Using odd pointers to represent integers would save much space
|
|
|
|
but require extra checks for this special case throughout the code.)
|
|
|
|
Since, a typical Python program spends much of its time allocating
|
|
|
|
and deallocating integers, these operations should be very fast.
|
|
|
|
Therefore we use a dedicated allocation scheme with a much lower
|
|
|
|
overhead (in space and time) than straight malloc(): a simple
|
|
|
|
dedicated free list, filled when necessary with memory from malloc().
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
|
1999-03-12 15:43:17 -04:00
|
|
|
#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
|
|
|
|
#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
|
1999-03-10 18:55:24 -04:00
|
|
|
|
|
|
|
#define PyMem_MALLOC malloc
|
|
|
|
#define PyMem_FREE free
|
1999-03-12 15:43:17 -04:00
|
|
|
|
|
|
|
struct _intblock {
|
|
|
|
struct _intblock *next;
|
|
|
|
PyIntObject objects[N_INTOBJECTS];
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _intblock PyIntBlock;
|
|
|
|
|
|
|
|
static PyIntBlock *block_list = NULL;
|
|
|
|
static PyIntObject *free_list = NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyIntObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
fill_free_list()
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *p, *q;
|
1999-03-12 15:43:17 -04:00
|
|
|
p = (PyIntObject *)PyMem_MALLOC(sizeof(PyIntBlock));
|
1990-12-20 11:06:42 -04:00
|
|
|
if (p == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyIntObject *)PyErr_NoMemory();
|
1999-03-12 15:43:17 -04:00
|
|
|
((PyIntBlock *)p)->next = block_list;
|
|
|
|
block_list = (PyIntBlock *)p;
|
|
|
|
p = &((PyIntBlock *)p)->objects[0];
|
1990-12-20 11:06:42 -04:00
|
|
|
q = p + N_INTOBJECTS;
|
|
|
|
while (--q > p)
|
1999-03-10 18:55:24 -04:00
|
|
|
q->ob_type = (struct _typeobject *)(q-1);
|
|
|
|
q->ob_type = NULL;
|
1990-12-20 11:06:42 -04:00
|
|
|
return p + N_INTOBJECTS - 1;
|
|
|
|
}
|
|
|
|
|
1993-10-15 13:18:48 -03:00
|
|
|
#ifndef NSMALLPOSINTS
|
|
|
|
#define NSMALLPOSINTS 100
|
|
|
|
#endif
|
|
|
|
#ifndef NSMALLNEGINTS
|
|
|
|
#define NSMALLNEGINTS 1
|
|
|
|
#endif
|
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
/* References to small integers are saved in this array so that they
|
|
|
|
can be shared.
|
|
|
|
The integers that are saved are those in the range
|
|
|
|
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
|
|
|
|
*/
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
|
1993-10-15 13:18:48 -03:00
|
|
|
#endif
|
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
int quick_int_allocs, quick_neg_int_allocs;
|
|
|
|
#endif
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyInt_FromLong(ival)
|
1990-10-14 09:07:46 -03:00
|
|
|
long ival;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyIntObject *v;
|
1993-10-15 13:18:48 -03:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
|
|
|
|
(v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1993-10-15 13:18:48 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
if (ival >= 0)
|
|
|
|
quick_int_allocs++;
|
|
|
|
else
|
|
|
|
quick_neg_int_allocs++;
|
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) v;
|
1993-10-15 13:18:48 -03:00
|
|
|
}
|
|
|
|
#endif
|
1990-12-20 11:06:42 -04:00
|
|
|
if (free_list == NULL) {
|
|
|
|
if ((free_list = fill_free_list()) == NULL)
|
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
v = free_list;
|
1999-03-10 18:55:24 -04:00
|
|
|
free_list = (PyIntObject *)v->ob_type;
|
1997-05-02 00:12:38 -03:00
|
|
|
v->ob_type = &PyInt_Type;
|
1990-12-20 11:06:42 -04:00
|
|
|
v->ob_ival = ival;
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_NewReference(v);
|
1993-10-15 13:18:48 -03:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
|
|
|
|
/* save this one for a following allocation */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1993-10-15 13:18:48 -03:00
|
|
|
small_ints[ival + NSMALLNEGINTS] = v;
|
|
|
|
}
|
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) v;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
int_dealloc(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1999-03-10 18:55:24 -04:00
|
|
|
v->ob_type = (struct _typeobject *)free_list;
|
1990-12-20 11:06:42 -04:00
|
|
|
free_list = v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
long
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInt_AsLong(op)
|
|
|
|
register PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyNumberMethods *nb;
|
|
|
|
PyIntObject *io;
|
1994-08-29 09:48:32 -03:00
|
|
|
long val;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
if (op && PyInt_Check(op))
|
|
|
|
return PyInt_AS_LONG((PyIntObject*) op);
|
1994-08-29 09:48:32 -03:00
|
|
|
|
|
|
|
if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
|
|
|
|
nb->nb_int == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1990-10-14 09:07:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1994-08-29 09:48:32 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
io = (PyIntObject*) (*nb->nb_int) (op);
|
1994-08-29 09:48:32 -03:00
|
|
|
if (io == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(io)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"nb_int should return int object");
|
1994-08-29 09:48:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
val = PyInt_AS_LONG(io);
|
|
|
|
Py_DECREF(io);
|
1994-08-29 09:48:32 -03:00
|
|
|
|
|
|
|
return val;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
1992-03-27 13:31:02 -04:00
|
|
|
/* ARGSUSED */
|
1991-06-07 13:10:43 -03:00
|
|
|
static int
|
1990-12-20 11:06:42 -04:00
|
|
|
int_print(v, fp, flags)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
FILE *fp;
|
1992-03-27 13:31:02 -04:00
|
|
|
int flags; /* Not used but required by interface */
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
fprintf(fp, "%ld", v->ob_ival);
|
1991-06-07 13:10:43 -03:00
|
|
|
return 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_repr(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
sprintf(buf, "%ld", v->ob_ival);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1990-12-20 11:06:42 -04:00
|
|
|
int_compare(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v, *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
register long i = v->ob_ival;
|
|
|
|
register long j = w->ob_ival;
|
|
|
|
return (i < j) ? -1 : (i > j) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
|
|
|
int_hash(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-01-06 18:53:20 -04:00
|
|
|
/* XXX If this is changed, you also need to change the way
|
|
|
|
Python's long, float and complex types are hashed. */
|
1993-03-29 06:43:31 -04:00
|
|
|
long x = v -> ob_ival;
|
|
|
|
if (x == -1)
|
|
|
|
x = -2;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_add(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
register long a, b, x;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1990-10-14 09:07:46 -03:00
|
|
|
x = a + b;
|
1990-10-14 17:02:26 -03:00
|
|
|
if ((x^a) < 0 && (x^b) < 0)
|
1991-12-10 09:57:36 -04:00
|
|
|
return err_ovf("integer addition");
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_sub(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
register long a, b, x;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1990-10-14 09:07:46 -03:00
|
|
|
x = a - b;
|
1990-10-14 17:02:26 -03:00
|
|
|
if ((x^a) < 0 && (x^~b) < 0)
|
1991-12-10 09:57:36 -04:00
|
|
|
return err_ovf("integer subtraction");
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:48:32 -03:00
|
|
|
/*
|
|
|
|
Integer overflow checking used to be done using a double, but on 64
|
|
|
|
bit machines (where both long and double are 64 bit) this fails
|
|
|
|
because the double doesn't have enouvg precision. John Tromp suggests
|
|
|
|
the following algorithm:
|
|
|
|
|
|
|
|
Suppose again we normalize a and b to be nonnegative.
|
|
|
|
Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
|
|
|
|
Now we test ah and bh against zero and get essentially 3 possible outcomes.
|
|
|
|
|
|
|
|
1) both ah and bh > 0 : then report overflow
|
|
|
|
|
|
|
|
2) both ah and bh = 0 : then compute a*b and report overflow if it comes out
|
|
|
|
negative
|
|
|
|
|
|
|
|
3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
|
|
|
|
compute al*bl and report overflow if it's negative
|
|
|
|
add (ah*bl)<<32 to al*bl and report overflow if
|
|
|
|
it's negative
|
|
|
|
|
|
|
|
In case of no overflow the result is then negated if necessary.
|
|
|
|
|
|
|
|
The majority of cases will be 2), in which case this method is the same as
|
|
|
|
what I suggested before. If multiplication is expensive enough, then the
|
|
|
|
other method is faster on case 3), but also more work to program, so I
|
|
|
|
guess the above is the preferred solution.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_mul(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1994-08-29 09:48:32 -03:00
|
|
|
long a, b, ah, bh, x, y;
|
|
|
|
int s = 1;
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1994-08-29 09:48:32 -03:00
|
|
|
ah = a >> (LONG_BIT/2);
|
|
|
|
bh = b >> (LONG_BIT/2);
|
|
|
|
|
|
|
|
/* Quick test for common case: two small positive ints */
|
|
|
|
|
|
|
|
if (ah == 0 && bh == 0) {
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x);
|
1994-08-29 09:48:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Arrange that a >= b >= 0 */
|
|
|
|
|
|
|
|
if (a < 0) {
|
|
|
|
a = -a;
|
|
|
|
if (a < 0) {
|
|
|
|
/* Largest negative */
|
|
|
|
if (b == 0 || b == 1) {
|
|
|
|
x = a*b;
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
s = -s;
|
|
|
|
ah = a >> (LONG_BIT/2);
|
|
|
|
}
|
|
|
|
if (b < 0) {
|
|
|
|
b = -b;
|
|
|
|
if (b < 0) {
|
|
|
|
/* Largest negative */
|
1996-12-06 16:14:43 -04:00
|
|
|
if (a == 0 || (a == 1 && s == 1)) {
|
1994-08-29 09:48:32 -03:00
|
|
|
x = a*b;
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
s = -s;
|
|
|
|
bh = b >> (LONG_BIT/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1) both ah and bh > 0 : then report overflow */
|
|
|
|
|
|
|
|
if (ah != 0 && bh != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/* 2) both ah and bh = 0 : then compute a*b and report
|
|
|
|
overflow if it comes out negative */
|
|
|
|
|
|
|
|
if (ah == 0 && bh == 0) {
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x*s);
|
1994-08-29 09:48:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (a < b) {
|
|
|
|
/* Swap */
|
|
|
|
x = a;
|
|
|
|
a = b;
|
|
|
|
b = x;
|
|
|
|
ah = bh;
|
|
|
|
/* bh not used beyond this point */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
|
|
|
|
it's >= 2^31
|
|
|
|
compute al*bl and report overflow if it's negative
|
|
|
|
add (ah*bl)<<32 to al*bl and report overflow if
|
|
|
|
it's negative
|
|
|
|
(NB b == bl in this case, and we make a = al) */
|
|
|
|
|
|
|
|
y = ah*b;
|
1997-01-06 18:53:20 -04:00
|
|
|
if (y >= (1L << (LONG_BIT/2 - 1)))
|
1994-08-29 09:48:32 -03:00
|
|
|
goto bad;
|
|
|
|
a &= (1L << (LONG_BIT/2)) - 1;
|
|
|
|
x = a*b;
|
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
1997-01-06 18:53:20 -04:00
|
|
|
x += y << (LONG_BIT/2);
|
1994-08-29 09:48:32 -03:00
|
|
|
if (x < 0)
|
|
|
|
goto bad;
|
|
|
|
ok:
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x * s);
|
1994-08-29 09:48:32 -03:00
|
|
|
|
|
|
|
bad:
|
|
|
|
return err_ovf("integer multiplication");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1992-01-19 12:28:51 -04:00
|
|
|
static int
|
|
|
|
i_divmod(x, y, p_xdivy, p_xmody)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyIntObject *x, *y;
|
1992-01-19 12:28:51 -04:00
|
|
|
long *p_xdivy, *p_xmody;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-01-19 12:28:51 -04:00
|
|
|
long xi = x->ob_ival;
|
|
|
|
long yi = y->ob_ival;
|
|
|
|
long xdivy, xmody;
|
|
|
|
|
|
|
|
if (yi == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ZeroDivisionError,
|
|
|
|
"integer division or modulo");
|
1992-01-19 12:28:51 -04:00
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1992-01-19 12:28:51 -04:00
|
|
|
if (yi < 0) {
|
|
|
|
if (xi < 0)
|
|
|
|
xdivy = -xi / -yi;
|
1991-10-24 11:59:31 -03:00
|
|
|
else
|
1992-01-19 12:28:51 -04:00
|
|
|
xdivy = - (xi / -yi);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
else {
|
1992-01-19 12:28:51 -04:00
|
|
|
if (xi < 0)
|
|
|
|
xdivy = - (-xi / yi);
|
1991-10-24 11:59:31 -03:00
|
|
|
else
|
1992-01-19 12:28:51 -04:00
|
|
|
xdivy = xi / yi;
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
1992-01-19 12:28:51 -04:00
|
|
|
xmody = xi - xdivy*yi;
|
1996-12-06 16:14:43 -04:00
|
|
|
if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
|
1992-01-19 12:28:51 -04:00
|
|
|
xmody += yi;
|
|
|
|
xdivy -= 1;
|
|
|
|
}
|
|
|
|
*p_xdivy = xdivy;
|
|
|
|
*p_xmody = xmody;
|
|
|
|
return 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-01-19 12:28:51 -04:00
|
|
|
int_div(x, y)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *x;
|
|
|
|
PyIntObject *y;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-01-19 12:28:51 -04:00
|
|
|
long d, m;
|
|
|
|
if (i_divmod(x, y, &d, &m) < 0)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(d);
|
1992-01-19 12:28:51 -04:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-01-19 12:28:51 -04:00
|
|
|
int_mod(x, y)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *x;
|
|
|
|
PyIntObject *y;
|
1992-01-19 12:28:51 -04:00
|
|
|
{
|
|
|
|
long d, m;
|
|
|
|
if (i_divmod(x, y, &d, &m) < 0)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(m);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-05-05 17:08:27 -03:00
|
|
|
int_divmod(x, y)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *x;
|
|
|
|
PyIntObject *y;
|
1991-05-05 17:08:27 -03:00
|
|
|
{
|
1992-01-19 12:28:51 -04:00
|
|
|
long d, m;
|
|
|
|
if (i_divmod(x, y, &d, &m) < 0)
|
1991-05-05 17:08:27 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
return Py_BuildValue("(ll)", d, m);
|
1991-05-05 17:08:27 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1994-08-29 09:48:32 -03:00
|
|
|
int_pow(v, w, z)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
|
|
|
PyIntObject *z;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1994-08-29 09:48:32 -03:00
|
|
|
#if 1
|
1996-12-06 16:14:43 -04:00
|
|
|
register long iv, iw, iz=0, ix, temp, prev;
|
1994-08-29 09:48:32 -03:00
|
|
|
iv = v->ob_ival;
|
|
|
|
iw = w->ob_ival;
|
|
|
|
if (iw < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"integer to the negative power");
|
1994-08-29 09:48:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((PyObject *)z != Py_None) {
|
1994-08-29 09:48:32 -03:00
|
|
|
iz = z->ob_ival;
|
1996-12-06 16:14:43 -04:00
|
|
|
if (iz == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"pow(x, y, z) with z==0");
|
1996-12-06 16:14:43 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1994-08-29 09:48:32 -03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX: The original exponentiation code stopped looping
|
|
|
|
* when temp hit zero; this code will continue onwards
|
|
|
|
* unnecessarily, but at least it won't cause any errors.
|
|
|
|
* Hopefully the speed improvement from the fast exponentiation
|
|
|
|
* will compensate for the slight inefficiency.
|
|
|
|
* XXX: Better handling of overflows is desperately needed.
|
|
|
|
*/
|
|
|
|
temp = iv;
|
|
|
|
ix = 1;
|
|
|
|
while (iw > 0) {
|
|
|
|
prev = ix; /* Save value for overflow check */
|
|
|
|
if (iw & 1) {
|
|
|
|
ix = ix*temp;
|
|
|
|
if (temp == 0)
|
|
|
|
break; /* Avoid ix / 0 */
|
|
|
|
if (ix / temp != prev)
|
|
|
|
return err_ovf("integer pow()");
|
|
|
|
}
|
|
|
|
iw >>= 1; /* Shift exponent down by 1 bit */
|
|
|
|
if (iw==0) break;
|
|
|
|
prev = temp;
|
|
|
|
temp *= temp; /* Square the value of temp */
|
|
|
|
if (prev!=0 && temp/prev!=prev)
|
|
|
|
return err_ovf("integer pow()");
|
1996-12-06 16:14:43 -04:00
|
|
|
if (iz) {
|
1994-08-29 09:48:32 -03:00
|
|
|
/* If we did a multiplication, perform a modulo */
|
|
|
|
ix = ix % iz;
|
|
|
|
temp = temp % iz;
|
|
|
|
}
|
|
|
|
}
|
1996-12-06 16:14:43 -04:00
|
|
|
if (iz) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *t1, *t2;
|
1994-08-29 09:48:32 -03:00
|
|
|
long int div, mod;
|
1997-05-02 00:12:38 -03:00
|
|
|
t1=PyInt_FromLong(ix);
|
|
|
|
t2=PyInt_FromLong(iz);
|
1994-08-29 09:48:32 -03:00
|
|
|
if (t1==NULL || t2==NULL ||
|
1997-05-02 00:12:38 -03:00
|
|
|
i_divmod((PyIntObject *)t1,
|
|
|
|
(PyIntObject *)t2, &div, &mod)<0)
|
|
|
|
{
|
|
|
|
Py_XDECREF(t1);
|
|
|
|
Py_XDECREF(t2);
|
1994-08-29 09:48:32 -03:00
|
|
|
return(NULL);
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(t1);
|
|
|
|
Py_DECREF(t2);
|
1994-08-29 09:48:32 -03:00
|
|
|
ix=mod;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(ix);
|
1994-08-29 09:48:32 -03:00
|
|
|
#else
|
1990-10-14 09:07:46 -03:00
|
|
|
register long iv, iw, ix;
|
|
|
|
iv = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
iw = w->ob_ival;
|
1991-05-05 17:08:27 -03:00
|
|
|
if (iw < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"integer to the negative power");
|
1991-05-05 17:08:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if ((PyObject *)z != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"pow(int, int, int) not yet supported");
|
1994-08-29 09:48:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
ix = 1;
|
1991-05-05 17:08:27 -03:00
|
|
|
while (--iw >= 0) {
|
|
|
|
long prev = ix;
|
1990-10-14 09:07:46 -03:00
|
|
|
ix = ix * iv;
|
1991-05-05 17:08:27 -03:00
|
|
|
if (iv == 0)
|
|
|
|
break; /* 0 to some power -- avoid ix / 0 */
|
|
|
|
if (ix / iv != prev)
|
1991-12-10 09:57:36 -04:00
|
|
|
return err_ovf("integer pow()");
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(ix);
|
1994-08-29 09:48:32 -03:00
|
|
|
#endif
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_neg(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
register long a, x;
|
|
|
|
a = v->ob_ival;
|
|
|
|
x = -a;
|
1990-10-14 17:02:26 -03:00
|
|
|
if (a < 0 && x < 0)
|
1991-12-10 09:57:36 -04:00
|
|
|
return err_ovf("integer negation");
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(x);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-12-20 11:06:42 -04:00
|
|
|
int_pos(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *)v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-05-05 17:08:27 -03:00
|
|
|
int_abs(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1991-05-05 17:08:27 -03:00
|
|
|
{
|
|
|
|
if (v->ob_ival >= 0)
|
|
|
|
return int_pos(v);
|
|
|
|
else
|
|
|
|
return int_neg(v);
|
|
|
|
}
|
|
|
|
|
1991-05-14 09:05:32 -03:00
|
|
|
static int
|
|
|
|
int_nonzero(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1991-05-14 09:05:32 -03:00
|
|
|
{
|
|
|
|
return v->ob_ival != 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_invert(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(~v->ob_ival);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_lshift(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
register long a, b;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1992-01-14 14:33:22 -04:00
|
|
|
if (b < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
1992-01-14 14:33:22 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (a == 0 || b == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *) v;
|
1992-01-14 14:33:22 -04:00
|
|
|
}
|
1993-10-26 12:21:51 -03:00
|
|
|
if (b >= LONG_BIT) {
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(0L);
|
1992-01-14 14:33:22 -04:00
|
|
|
}
|
|
|
|
a = (unsigned long)a << b;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(a);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_rshift(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
register long a, b;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1992-01-14 14:33:22 -04:00
|
|
|
if (b < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "negative shift count");
|
1992-01-14 14:33:22 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (a == 0 || b == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *) v;
|
1992-01-14 14:33:22 -04:00
|
|
|
}
|
1993-10-26 12:21:51 -03:00
|
|
|
if (b >= LONG_BIT) {
|
1992-01-14 14:33:22 -04:00
|
|
|
if (a < 0)
|
|
|
|
a = -1;
|
|
|
|
else
|
|
|
|
a = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (a < 0)
|
|
|
|
a = ~( ~(unsigned long)a >> b );
|
|
|
|
else
|
|
|
|
a = (unsigned long)a >> b;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(a);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_and(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
register long a, b;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(a & b);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_xor(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
register long a, b;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(a ^ b);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-24 11:59:31 -03:00
|
|
|
int_or(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
|
|
|
PyIntObject *w;
|
1991-10-24 11:59:31 -03:00
|
|
|
{
|
|
|
|
register long a, b;
|
|
|
|
a = v->ob_ival;
|
1992-01-19 12:28:51 -04:00
|
|
|
b = w->ob_ival;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong(a | b);
|
1991-10-24 11:59:31 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-09-12 08:09:23 -03:00
|
|
|
int_int(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
return (PyObject *)v;
|
1992-09-12 08:09:23 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-09-12 08:09:23 -03:00
|
|
|
int_long(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyLong_FromLong((v -> ob_ival));
|
1992-09-12 08:09:23 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-09-12 08:09:23 -03:00
|
|
|
int_float(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyFloat_FromDouble((double)(v -> ob_ival));
|
1992-09-12 08:09:23 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-09-12 08:09:23 -03:00
|
|
|
int_oct(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
{
|
1997-01-14 11:43:41 -04:00
|
|
|
char buf[100];
|
1993-03-29 06:43:31 -04:00
|
|
|
long x = v -> ob_ival;
|
1992-09-12 08:09:23 -03:00
|
|
|
if (x == 0)
|
|
|
|
strcpy(buf, "0");
|
|
|
|
else
|
1997-01-12 15:48:03 -04:00
|
|
|
sprintf(buf, "0%lo", x);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1992-09-12 08:09:23 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-09-12 08:09:23 -03:00
|
|
|
int_hex(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyIntObject *v;
|
1992-09-12 08:09:23 -03:00
|
|
|
{
|
1997-01-14 11:43:41 -04:00
|
|
|
char buf[100];
|
1993-03-29 06:43:31 -04:00
|
|
|
long x = v -> ob_ival;
|
1997-01-12 15:48:03 -04:00
|
|
|
sprintf(buf, "0x%lx", x);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1992-09-12 08:09:23 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyNumberMethods int_as_number = {
|
1994-08-29 09:48:32 -03:00
|
|
|
(binaryfunc)int_add, /*nb_add*/
|
|
|
|
(binaryfunc)int_sub, /*nb_subtract*/
|
|
|
|
(binaryfunc)int_mul, /*nb_multiply*/
|
|
|
|
(binaryfunc)int_div, /*nb_divide*/
|
|
|
|
(binaryfunc)int_mod, /*nb_remainder*/
|
|
|
|
(binaryfunc)int_divmod, /*nb_divmod*/
|
|
|
|
(ternaryfunc)int_pow, /*nb_power*/
|
|
|
|
(unaryfunc)int_neg, /*nb_negative*/
|
|
|
|
(unaryfunc)int_pos, /*nb_positive*/
|
|
|
|
(unaryfunc)int_abs, /*nb_absolute*/
|
|
|
|
(inquiry)int_nonzero, /*nb_nonzero*/
|
|
|
|
(unaryfunc)int_invert, /*nb_invert*/
|
|
|
|
(binaryfunc)int_lshift, /*nb_lshift*/
|
|
|
|
(binaryfunc)int_rshift, /*nb_rshift*/
|
|
|
|
(binaryfunc)int_and, /*nb_and*/
|
|
|
|
(binaryfunc)int_xor, /*nb_xor*/
|
|
|
|
(binaryfunc)int_or, /*nb_or*/
|
1992-09-12 08:09:23 -03:00
|
|
|
0, /*nb_coerce*/
|
1994-08-29 09:48:32 -03:00
|
|
|
(unaryfunc)int_int, /*nb_int*/
|
|
|
|
(unaryfunc)int_long, /*nb_long*/
|
|
|
|
(unaryfunc)int_float, /*nb_float*/
|
|
|
|
(unaryfunc)int_oct, /*nb_oct*/
|
|
|
|
(unaryfunc)int_hex, /*nb_hex*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyInt_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
"int",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyIntObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1994-08-29 09:48:32 -03:00
|
|
|
(destructor)int_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)int_print, /*tp_print*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
1994-08-29 09:48:32 -03:00
|
|
|
(cmpfunc)int_compare, /*tp_compare*/
|
|
|
|
(reprfunc)int_repr, /*tp_repr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
&int_as_number, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-29 09:48:32 -03:00
|
|
|
(hashfunc)int_hash, /*tp_hash*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1997-08-04 23:16:08 -03:00
|
|
|
|
|
|
|
void
|
|
|
|
PyInt_Fini()
|
|
|
|
{
|
1999-03-12 15:43:17 -04:00
|
|
|
PyIntObject *p;
|
|
|
|
PyIntBlock *list, *next;
|
1997-08-04 23:16:08 -03:00
|
|
|
int i;
|
1999-03-10 18:55:24 -04:00
|
|
|
int bc, bf; /* block count, number of freed blocks */
|
|
|
|
int irem, isum; /* remaining unfreed ints per block, total */
|
1997-08-04 23:16:08 -03:00
|
|
|
|
1999-03-10 18:55:24 -04:00
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
PyIntObject **q;
|
|
|
|
|
|
|
|
i = NSMALLNEGINTS + NSMALLPOSINTS;
|
|
|
|
q = small_ints;
|
|
|
|
while (--i >= 0) {
|
|
|
|
Py_XDECREF(*q);
|
|
|
|
*q++ = NULL;
|
|
|
|
}
|
1997-08-04 23:16:08 -03:00
|
|
|
#endif
|
1999-03-10 18:55:24 -04:00
|
|
|
bc = 0;
|
|
|
|
bf = 0;
|
|
|
|
isum = 0;
|
|
|
|
list = block_list;
|
|
|
|
block_list = NULL;
|
1999-03-19 16:30:39 -04:00
|
|
|
free_list = NULL;
|
1999-03-10 18:55:24 -04:00
|
|
|
while (list != NULL) {
|
|
|
|
bc++;
|
|
|
|
irem = 0;
|
1999-03-19 16:30:39 -04:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
1999-03-10 18:55:24 -04:00
|
|
|
if (PyInt_Check(p) && p->ob_refcnt != 0)
|
|
|
|
irem++;
|
|
|
|
}
|
1999-03-12 15:43:17 -04:00
|
|
|
next = list->next;
|
1999-03-10 18:55:24 -04:00
|
|
|
if (irem) {
|
1999-03-12 15:43:17 -04:00
|
|
|
list->next = block_list;
|
|
|
|
block_list = list;
|
1999-03-19 16:30:39 -04:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
|
|
|
if (!PyInt_Check(p) || p->ob_refcnt == 0) {
|
|
|
|
p->ob_type = (struct _typeobject *)
|
|
|
|
free_list;
|
|
|
|
free_list = p;
|
|
|
|
}
|
|
|
|
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
|
|
|
|
else if (-NSMALLNEGINTS <= p->ob_ival &&
|
|
|
|
p->ob_ival < NSMALLPOSINTS &&
|
|
|
|
small_ints[p->ob_ival +
|
|
|
|
NSMALLNEGINTS] == NULL) {
|
|
|
|
Py_INCREF(p);
|
|
|
|
small_ints[p->ob_ival +
|
|
|
|
NSMALLNEGINTS] = p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
1999-03-10 18:55:24 -04:00
|
|
|
}
|
|
|
|
else {
|
1999-03-12 15:43:17 -04:00
|
|
|
PyMem_FREE(list);
|
1999-03-10 18:55:24 -04:00
|
|
|
bf++;
|
|
|
|
}
|
|
|
|
isum += irem;
|
1999-03-12 15:43:17 -04:00
|
|
|
list = next;
|
1999-03-10 18:55:24 -04:00
|
|
|
}
|
1999-03-12 15:43:17 -04:00
|
|
|
if (!Py_VerboseFlag)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "# cleanup ints");
|
|
|
|
if (!isum) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr,
|
|
|
|
": %d unfreed int%s in %d out of %d block%s\n",
|
|
|
|
isum, isum == 1 ? "" : "s",
|
|
|
|
bc - bf, bc, bc == 1 ? "" : "s");
|
|
|
|
}
|
|
|
|
if (Py_VerboseFlag > 1) {
|
|
|
|
list = block_list;
|
|
|
|
while (list != NULL) {
|
1999-03-19 16:30:39 -04:00
|
|
|
for (i = 0, p = &list->objects[0];
|
|
|
|
i < N_INTOBJECTS;
|
|
|
|
i++, p++) {
|
1999-03-12 15:43:17 -04:00
|
|
|
if (PyInt_Check(p) && p->ob_refcnt != 0)
|
|
|
|
fprintf(stderr,
|
1999-03-19 16:30:39 -04:00
|
|
|
"# <int at %lx, refcnt=%d, val=%ld>\n",
|
1999-03-12 15:43:17 -04:00
|
|
|
p, p->ob_refcnt, p->ob_ival);
|
|
|
|
}
|
|
|
|
list = list->next;
|
1999-03-10 18:55:24 -04:00
|
|
|
}
|
|
|
|
}
|
1997-08-04 23:16:08 -03:00
|
|
|
}
|