Index: Include/longintrepr.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/longintrepr.h,v retrieving revision 2.16 diff -c -r2.16 longintrepr.h *** Include/longintrepr.h 30 Aug 2004 02:44:36 -0000 2.16 --- Include/longintrepr.h 10 Apr 2005 23:40:20 -0000 *************** *** 33,54 **** #endif /* Long integer representation. ! The absolute value of a number is equal to ! SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) ! Negative numbers are represented with ob_size < 0; ! zero is represented by ob_size == 0. ! In a normalized number, ob_digit[abs(ob_size)-1] (the most significant digit) is never zero. Also, in all cases, for all valid i, 0 <= ob_digit[i] <= MASK. The allocation function takes care of allocating extra memory ! so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. ! ! CAUTION: Generic code manipulating subtypes of PyVarObject has to ! aware that longs abuse ob_size's sign bit. */ struct _longobject { PyObject_VAR_HEAD digit ob_digit[1]; }; --- 33,52 ---- #endif /* Long integer representation. ! The value of a number is equal to ! long_sign * SUM(for i=0 through ob_size-1) ob_digit[i] * 2**(SHIFT*i) ! (sign is -1 for negative, +1 for 0 and positive numbers; zero is ! represented by ob_size == 0) ! In a normalized number, ob_digit[ob_size-1] (the most significant digit) is never zero. Also, in all cases, for all valid i, 0 <= ob_digit[i] <= MASK. The allocation function takes care of allocating extra memory ! so that ob_digit[0] ... ob_digit[ob_size-1] are actually available. */ struct _longobject { PyObject_VAR_HEAD + short long_sign; digit ob_digit[1]; }; Index: Objects/longobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/longobject.c,v retrieving revision 1.166 diff -c -r1.166 longobject.c *** Objects/longobject.c 3 Mar 2005 12:26:34 -0000 1.166 --- Objects/longobject.c 10 Apr 2005 23:40:44 -0000 *************** *** 22,29 **** */ #define FIVEARY_CUTOFF 8 - #define ABS(x) ((x) < 0 ? -(x) : (x)) - #undef MIN #undef MAX #define MAX(x, y) ((x) < (y) ? (y) : (x)) --- 22,27 ---- *************** *** 49,61 **** static PyLongObject * long_normalize(register PyLongObject *v) { ! int j = ABS(v->ob_size); ! register int i = j; while (i > 0 && v->ob_digit[i-1] == 0) --i; ! if (i != j) ! v->ob_size = (v->ob_size < 0) ? -(i) : i; return v; } --- 47,57 ---- static PyLongObject * long_normalize(register PyLongObject *v) { ! register int i = v->ob_size; while (i > 0 && v->ob_digit[i-1] == 0) --i; ! v->ob_size = i; return v; } *************** *** 65,71 **** PyLongObject * _PyLong_New(int size) { ! return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); } PyObject * --- 61,69 ---- PyLongObject * _PyLong_New(int size) { ! PyLongObject* n = PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); ! if (n) n->long_sign = 1; ! return n; } PyObject * *************** *** 73,86 **** { PyLongObject *result; int i; ! assert(src != NULL); ! i = src->ob_size; ! if (i < 0) ! i = -(i); ! result = _PyLong_New(i); if (result != NULL) { ! result->ob_size = src->ob_size; while (--i >= 0) result->ob_digit[i] = src->ob_digit[i]; } --- 71,82 ---- { PyLongObject *result; int i; ! assert(src != NULL); ! result = _PyLong_New(src->ob_size); if (result != NULL) { ! result->long_sign = src->long_sign; ! i = src->ob_size; while (--i >= 0) result->ob_digit[i] = src->ob_digit[i]; } *************** *** 95,106 **** PyLongObject *v; unsigned long t; /* unsigned so >> doesn't propagate sign bit */ int ndigits = 0; ! int negative = 0; if (ival < 0) { ival = -ival; ! negative = 1; ! } /* Count the number of Python digits. We used to pick 5 ("big enough for anything"), but that's a --- 91,102 ---- PyLongObject *v; unsigned long t; /* unsigned so >> doesn't propagate sign bit */ int ndigits = 0; ! int sign = 1; if (ival < 0) { ival = -ival; ! sign = -1; ! } /* Count the number of Python digits. We used to pick 5 ("big enough for anything"), but that's a *************** *** 114,125 **** v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; - v->ob_size = negative ? -ndigits : ndigits; t = (unsigned long)ival; while (t) { *p++ = (digit)(t & MASK); t >>= SHIFT; } } return (PyObject *)v; } --- 110,121 ---- v = _PyLong_New(ndigits); if (v != NULL) { digit *p = v->ob_digit; t = (unsigned long)ival; while (t) { *p++ = (digit)(t & MASK); t >>= SHIFT; } + v->long_sign = sign; } return (PyObject *)v; } *************** *** 158,172 **** { PyLongObject *v; double frac; ! int i, ndig, expo, neg; ! neg = 0; if (Py_IS_INFINITY(dval)) { PyErr_SetString(PyExc_OverflowError, "cannot convert float infinity to long"); return NULL; } if (dval < 0.0) { ! neg = 1; dval = -dval; } frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ --- 154,168 ---- { PyLongObject *v; double frac; ! int i, ndig, expo, sign; ! sign = 1; if (Py_IS_INFINITY(dval)) { PyErr_SetString(PyExc_OverflowError, "cannot convert float infinity to long"); return NULL; } if (dval < 0.0) { ! sign = -1; dval = -dval; } frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ *************** *** 183,190 **** frac = frac - (double)bits; frac = ldexp(frac, SHIFT); } ! if (neg) ! v->ob_size = -(v->ob_size); return (PyObject *)v; } --- 179,185 ---- frac = frac - (double)bits; frac = ldexp(frac, SHIFT); } ! v->long_sign = sign; return (PyObject *)v; } *************** *** 197,203 **** /* This version by Tim Peters */ register PyLongObject *v; unsigned long x, prev; ! int i, sign; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) --- 192,198 ---- /* This version by Tim Peters */ register PyLongObject *v; unsigned long x, prev; ! int i; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) *************** *** 207,218 **** } v = (PyLongObject *)vv; i = v->ob_size; - sign = 1; x = 0; - if (i < 0) { - sign = -1; - i = -(i); - } while (--i >= 0) { prev = x; x = (x << SHIFT) + v->ob_digit[i]; --- 202,208 ---- *************** *** 224,232 **** * trouble iff sign bit set && (positive || some bit set other * than the sign bit). */ ! if ((long)x < 0 && (sign > 0 || (x << 1) != 0)) goto overflow; ! return (long)x * sign; overflow: PyErr_SetString(PyExc_OverflowError, --- 214,222 ---- * trouble iff sign bit set && (positive || some bit set other * than the sign bit). */ ! if ((long)x < 0 && (v->long_sign > 0 || (x << 1) != 0)) goto overflow; ! return (long)x * v->long_sign; overflow: PyErr_SetString(PyExc_OverflowError, *************** *** 260,266 **** v = (PyLongObject *)vv; i = v->ob_size; x = 0; ! if (i < 0) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned long"); return (unsigned long) -1; --- 250,256 ---- v = (PyLongObject *)vv; i = v->ob_size; x = 0; ! if (v->long_sign < 0) { PyErr_SetString(PyExc_OverflowError, "can't convert negative value to unsigned long"); return (unsigned long) -1; *************** *** 285,291 **** { register PyLongObject *v; unsigned long x; ! int i, sign; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) --- 275,281 ---- { register PyLongObject *v; unsigned long x; ! int i; if (vv == NULL || !PyLong_Check(vv)) { if (vv != NULL && PyInt_Check(vv)) *************** *** 295,310 **** } v = (PyLongObject *)vv; i = v->ob_size; - sign = 1; x = 0; - if (i < 0) { - sign = -1; - i = -i; - } while (--i >= 0) { x = (x << SHIFT) + v->ob_digit[i]; } ! return x * sign; } int --- 285,295 ---- } v = (PyLongObject *)vv; i = v->ob_size; x = 0; while (--i >= 0) { x = (x << SHIFT) + v->ob_digit[i]; } ! return x * v->long_sign; } int *************** *** 315,321 **** assert(v != NULL); assert(PyLong_Check(v)); ! return v->ob_size == 0 ? 0 : (v->ob_size < 0 ? -1 : 1); } size_t --- 300,306 ---- assert(v != NULL); assert(PyLong_Check(v)); ! return (v->ob_size == 0) ? 0 : v->long_sign; } size_t *************** *** 327,333 **** assert(v != NULL); assert(PyLong_Check(v)); ! ndigits = ABS(v->ob_size); assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); if (ndigits > 0) { digit msd = v->ob_digit[ndigits - 1]; --- 312,318 ---- assert(v != NULL); assert(PyLong_Check(v)); ! ndigits = v->ob_size; assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); if (ndigits > 0) { digit msd = v->ob_digit[ndigits - 1]; *************** *** 453,459 **** } } ! v->ob_size = is_signed ? -idigit : idigit; return (PyObject *)long_normalize(v); } --- 438,448 ---- } } ! v->ob_size = idigit; ! if (is_signed) ! v->long_sign = -1; ! else ! v->long_sign = 1; return (PyObject *)long_normalize(v); } *************** *** 474,481 **** assert(v != NULL && PyLong_Check(v)); ! if (v->ob_size < 0) { ! ndigits = -(v->ob_size); if (!is_signed) { PyErr_SetString(PyExc_TypeError, "can't convert negative long to unsigned"); --- 463,469 ---- assert(v != NULL && PyLong_Check(v)); ! if (v->long_sign < 0) { if (!is_signed) { PyErr_SetString(PyExc_TypeError, "can't convert negative long to unsigned"); *************** *** 484,492 **** do_twos_comp = 1; } else { - ndigits = v->ob_size; do_twos_comp = 0; } if (little_endian) { p = bytes; --- 472,481 ---- do_twos_comp = 1; } else { do_twos_comp = 0; } + ndigits = v->ob_size; + if (little_endian) { p = bytes; *************** *** 611,617 **** PyLongObject *v; double x; const double multiplier = (double)(1L << SHIFT); ! int i, sign; int nbitsneeded; if (vv == NULL || !PyLong_Check(vv)) { --- 600,606 ---- PyLongObject *v; double x; const double multiplier = (double)(1L << SHIFT); ! int i; int nbitsneeded; if (vv == NULL || !PyLong_Check(vv)) { *************** *** 620,631 **** } v = (PyLongObject *)vv; i = v->ob_size; ! sign = 1; ! if (i < 0) { ! sign = -1; ! i = -(i); ! } ! else if (i == 0) { *exponent = 0; return 0.0; } --- 609,615 ---- } v = (PyLongObject *)vv; i = v->ob_size; ! if (i == 0) { *exponent = 0; return 0.0; } *************** *** 642,648 **** zeroes, the true value is x * 2**(i*SHIFT). */ *exponent = i; assert(x > 0.0); ! return x * sign; #undef NBITS_WANTED } --- 626,632 ---- zeroes, the true value is x * 2**(i*SHIFT). */ *exponent = i; assert(x > 0.0); ! return x * v->long_sign; #undef NBITS_WANTED } *************** *** 855,861 **** { register PyLongObject *v; unsigned PY_LONG_LONG x; ! int i, sign; if (vv == NULL || !PyLong_Check(vv)) { PyErr_BadInternalCall(); --- 839,845 ---- { register PyLongObject *v; unsigned PY_LONG_LONG x; ! int i; if (vv == NULL || !PyLong_Check(vv)) { PyErr_BadInternalCall(); *************** *** 863,878 **** } v = (PyLongObject *)vv; i = v->ob_size; - sign = 1; x = 0; - if (i < 0) { - sign = -1; - i = -i; - } while (--i >= 0) { x = (x << SHIFT) + v->ob_digit[i]; } ! return x * sign; } #undef IS_LITTLE_ENDIAN --- 847,857 ---- } v = (PyLongObject *)vv; i = v->ob_size; x = 0; while (--i >= 0) { x = (x << SHIFT) + v->ob_digit[i]; } ! return x * v->long_sign; } #undef IS_LITTLE_ENDIAN *************** *** 976,982 **** static PyLongObject * muladd1(PyLongObject *a, wdigit n, wdigit extra) { ! int size_a = ABS(a->ob_size); PyLongObject *z = _PyLong_New(size_a+1); twodigits carry = extra; int i; --- 955,961 ---- static PyLongObject * muladd1(PyLongObject *a, wdigit n, wdigit extra) { ! int size_a = a->ob_size; PyLongObject *z = _PyLong_New(size_a+1); twodigits carry = extra; int i; *************** *** 1022,1028 **** static PyLongObject * divrem1(PyLongObject *a, digit n, digit *prem) { ! const int size = ABS(a->ob_size); PyLongObject *z; assert(n > 0 && n <= MASK); --- 1001,1007 ---- static PyLongObject * divrem1(PyLongObject *a, digit n, digit *prem) { ! const int size = a->ob_size; PyLongObject *z; assert(n > 0 && n <= MASK); *************** *** 1043,1049 **** register PyLongObject *a = (PyLongObject *)aa; PyStringObject *str; int i; ! const int size_a = ABS(a->ob_size); char *p; int bits; char sign = '\0'; --- 1022,1028 ---- register PyLongObject *a = (PyLongObject *)aa; PyStringObject *str; int i; ! const int size_a = a->ob_size; char *p; int bits; char sign = '\0'; *************** *** 1069,1075 **** *p = '\0'; if (addL) *--p = 'L'; ! if (a->ob_size < 0) sign = '-'; if (a->ob_size == 0) { --- 1048,1054 ---- *p = '\0'; if (addL) *--p = 'L'; ! if (a->long_sign < 0) sign = '-'; if (a->ob_size == 0) { *************** *** 1331,1338 **** return NULL; if (str == start) goto onError; ! if (sign < 0 && z != NULL && z->ob_size != 0) ! z->ob_size = -(z->ob_size); if (*str == 'L' || *str == 'l') str++; while (*str && isspace(Py_CHARMASK(*str))) --- 1310,1317 ---- return NULL; if (str == start) goto onError; ! if (z->ob_size != 0) ! z->long_sign = sign; if (*str == 'L' || *str == 'l') str++; while (*str && isspace(Py_CHARMASK(*str))) *************** *** 1383,1389 **** long_divrem(PyLongObject *a, PyLongObject *b, PyLongObject **pdiv, PyLongObject **prem) { ! int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; if (size_b == 0) { --- 1362,1368 ---- long_divrem(PyLongObject *a, PyLongObject *b, PyLongObject **pdiv, PyLongObject **prem) { ! int size_a = a->ob_size, size_b = b->ob_size; PyLongObject *z; if (size_b == 0) { *************** *** 1416,1425 **** The quotient z has the sign of a*b; the remainder r has the sign of a, so a = b*z + r. */ ! if ((a->ob_size < 0) != (b->ob_size < 0)) ! z->ob_size = -(z->ob_size); ! if (a->ob_size < 0 && (*prem)->ob_size != 0) ! (*prem)->ob_size = -((*prem)->ob_size); *pdiv = z; return 0; } --- 1395,1408 ---- The quotient z has the sign of a*b; the remainder r has the sign of a, so a = b*z + r. */ ! if ((a->long_sign < 0) != (b->long_sign < 0) && z->ob_size != 0) ! z->long_sign = -1; ! else ! z->long_sign = 1; ! if (a->long_sign < 0 && (*prem)->ob_size != 0) ! (*prem)->long_sign = -1; ! else ! (*prem)->long_sign = 1; *pdiv = z; return 0; } *************** *** 1429,1435 **** static PyLongObject * x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) { ! int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); PyLongObject *v = mul1(v1, d); PyLongObject *w = mul1(w1, d); --- 1412,1418 ---- static PyLongObject * x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) { ! int size_v = v1->ob_size, size_w = w1->ob_size; digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); PyLongObject *v = mul1(v1, d); PyLongObject *w = mul1(w1, d); *************** *** 1444,1452 **** assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */ ! assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ ! size_v = ABS(v->ob_size); a = _PyLong_New(size_v - size_w + 1); for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { --- 1427,1435 ---- assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */ ! assert(size_w == w->ob_size); /* That's how d was calculated */ ! size_v = v->ob_size; a = _PyLong_New(size_v - size_w + 1); for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { *************** *** 1548,1590 **** { int sign; ! if (a->ob_size != b->ob_size) { ! if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) ! sign = 0; ! else ! sign = a->ob_size - b->ob_size; } else { ! int i = ABS(a->ob_size); while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) ; if (i < 0) ! sign = 0; else { sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; ! if (a->ob_size < 0) sign = -sign; } } ! return sign < 0 ? -1 : sign > 0 ? 1 : 0; } static long long_hash(PyLongObject *v) { long x; ! int i, sign; /* This is designed so that Python ints and longs with the same value hash to the same value, otherwise comparisons of mapping keys will turn out weird */ i = v->ob_size; - sign = 1; x = 0; - if (i < 0) { - sign = -1; - i = -(i); - } #define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT) while (--i >= 0) { /* Force a native long #-bits (32 or 64) circular shift */ --- 1531,1565 ---- { int sign; ! if (a->ob_size*a->long_sign != b->ob_size*b->long_sign) { ! sign = a->ob_size*a->long_sign - b->ob_size*b->long_sign; } else { ! int i = a->ob_size; while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) ; if (i < 0) ! return 0; else { sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; ! if (a->long_sign < 0) sign = -sign; } } ! return sign < 0 ? -1 : 1; } static long long_hash(PyLongObject *v) { long x; ! int i; /* This is designed so that Python ints and longs with the same value hash to the same value, otherwise comparisons of mapping keys will turn out weird */ i = v->ob_size; x = 0; #define LONG_BIT_SHIFT (8*sizeof(long) - SHIFT) while (--i >= 0) { /* Force a native long #-bits (32 or 64) circular shift */ *************** *** 1592,1598 **** x += v->ob_digit[i]; } #undef LONG_BIT_SHIFT ! x = x * sign; if (x == -1) x = -2; return x; --- 1567,1573 ---- x += v->ob_digit[i]; } #undef LONG_BIT_SHIFT ! x = x * v->long_sign; if (x == -1) x = -2; return x; *************** *** 1604,1610 **** static PyLongObject * x_add(PyLongObject *a, PyLongObject *b) { ! int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; int i; digit carry = 0; --- 1579,1585 ---- static PyLongObject * x_add(PyLongObject *a, PyLongObject *b) { ! int size_a = a->ob_size, size_b = b->ob_size; PyLongObject *z; int i; digit carry = 0; *************** *** 1638,1644 **** static PyLongObject * x_sub(PyLongObject *a, PyLongObject *b) { ! int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); PyLongObject *z; int i; int sign = 1; --- 1613,1619 ---- static PyLongObject * x_sub(PyLongObject *a, PyLongObject *b) { ! int size_a = a->ob_size, size_b = b->ob_size; PyLongObject *z; int i; int sign = 1; *************** *** 1683,1690 **** borrow &= 1; /* Keep only one sign bit */ } assert(borrow == 0); ! if (sign < 0) ! z->ob_size = -(z->ob_size); return long_normalize(z); } --- 1658,1664 ---- borrow &= 1; /* Keep only one sign bit */ } assert(borrow == 0); ! z->long_sign = sign; return long_normalize(z); } *************** *** 1695,1711 **** CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->ob_size < 0) { ! if (b->ob_size < 0) { z = x_add(a, b); if (z != NULL && z->ob_size != 0) ! z->ob_size = -(z->ob_size); } else z = x_sub(b, a); } else { ! if (b->ob_size < 0) z = x_sub(a, b); else z = x_add(a, b); --- 1669,1685 ---- CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->long_sign < 0) { ! if (b->long_sign < 0) { z = x_add(a, b); if (z != NULL && z->ob_size != 0) ! z->long_sign = -1; } else z = x_sub(b, a); } else { ! if (b->long_sign < 0) z = x_sub(a, b); else z = x_add(a, b); *************** *** 1722,1737 **** CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->ob_size < 0) { ! if (b->ob_size < 0) z = x_sub(a, b); else z = x_add(a, b); if (z != NULL && z->ob_size != 0) ! z->ob_size = -(z->ob_size); } else { ! if (b->ob_size < 0) z = x_add(a, b); else z = x_sub(a, b); --- 1696,1711 ---- CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->long_sign < 0) { ! if (b->long_sign < 0) z = x_sub(a, b); else z = x_add(a, b); if (z != NULL && z->ob_size != 0) ! z->long_sign *= -1; } else { ! if (b->long_sign < 0) z = x_add(a, b); else z = x_sub(a, b); *************** *** 1748,1755 **** x_mul(PyLongObject *a, PyLongObject *b) { PyLongObject *z; ! int size_a = ABS(a->ob_size); ! int size_b = ABS(b->ob_size); int i; z = _PyLong_New(size_a + size_b); --- 1722,1729 ---- x_mul(PyLongObject *a, PyLongObject *b) { PyLongObject *z; ! int size_a = a->ob_size; ! int size_b = b->ob_size; int i; z = _PyLong_New(size_a + size_b); *************** *** 1840,1846 **** { PyLongObject *hi, *lo; int size_lo, size_hi; ! const int size_n = ABS(n->ob_size); size_lo = MIN(size_n, size); size_hi = size_n - size_lo; --- 1814,1820 ---- { PyLongObject *hi, *lo; int size_lo, size_hi; ! const int size_n = n->ob_size; size_lo = MIN(size_n, size); size_hi = size_n - size_lo; *************** *** 1869,1876 **** static PyLongObject * k_mul(PyLongObject *a, PyLongObject *b) { ! int asize = ABS(a->ob_size); ! int bsize = ABS(b->ob_size); PyLongObject *ah = NULL; PyLongObject *al = NULL; PyLongObject *bh = NULL; --- 1843,1850 ---- static PyLongObject * k_mul(PyLongObject *a, PyLongObject *b) { ! int asize = a->ob_size; ! int bsize = b->ob_size; PyLongObject *ah = NULL; PyLongObject *al = NULL; PyLongObject *bh = NULL; *************** *** 2090,2097 **** static PyLongObject * k_lopsided_mul(PyLongObject *a, PyLongObject *b) { ! const int asize = ABS(a->ob_size); ! int bsize = ABS(b->ob_size); int nbdone; /* # of b digits already multiplied */ PyLongObject *ret; PyLongObject *bslice = NULL; --- 2064,2071 ---- static PyLongObject * k_lopsided_mul(PyLongObject *a, PyLongObject *b) { ! const int asize = a->ob_size; ! int bsize = b->ob_size; int nbdone; /* # of b digits already multiplied */ PyLongObject *ret; PyLongObject *bslice = NULL; *************** *** 2153,2160 **** z = k_mul(a, b); /* Negate if exactly one of the inputs is negative. */ ! if (((a->ob_size ^ b->ob_size) < 0) && z) ! z->ob_size = -(z->ob_size); Py_DECREF(a); Py_DECREF(b); return (PyObject *)z; --- 2127,2134 ---- z = k_mul(a, b); /* Negate if exactly one of the inputs is negative. */ ! if (((a->long_sign ^ b->long_sign) < 0) && z && z->ob_size) ! z->long_sign = -1; Py_DECREF(a); Py_DECREF(b); return (PyObject *)z; *************** *** 2189,2196 **** if (long_divrem(v, w, &div, &mod) < 0) return -1; ! if ((mod->ob_size < 0 && w->ob_size > 0) || ! (mod->ob_size > 0 && w->ob_size < 0)) { PyLongObject *temp; PyLongObject *one; temp = (PyLongObject *) long_add(mod, w); --- 2163,2170 ---- if (long_divrem(v, w, &div, &mod) < 0) return -1; ! if ((mod->long_sign < 0 && w->long_sign > 0) || ! (mod->ob_size > 0 && mod->long_sign > 0 && w->long_sign < 0)) { PyLongObject *temp; PyLongObject *one; temp = (PyLongObject *) long_add(mod, w); *************** *** 2371,2377 **** return Py_NotImplemented; } ! if (b->ob_size < 0) { /* if exponent is negative */ if (c) { PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " "cannot be negative when 3rd argument specified"); --- 2345,2351 ---- return Py_NotImplemented; } ! if (b->long_sign < 0) { /* if exponent is negative */ if (c) { PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " "cannot be negative when 3rd argument specified"); *************** *** 2399,2405 **** /* if modulus < 0: negativeOutput = True modulus = -modulus */ ! if (c->ob_size < 0) { negativeOutput = 1; temp = (PyLongObject *)_PyLong_Copy(c); if (temp == NULL) --- 2373,2379 ---- /* if modulus < 0: negativeOutput = True modulus = -modulus */ ! if (c->long_sign < 0) { negativeOutput = 1; temp = (PyLongObject *)_PyLong_Copy(c); if (temp == NULL) *************** *** 2407,2413 **** Py_DECREF(c); c = temp; temp = NULL; ! c->ob_size = - c->ob_size; } /* if modulus == 1: --- 2381,2387 ---- Py_DECREF(c); c = temp; temp = NULL; ! c->long_sign = 1; } /* if modulus == 1: *************** *** 2420,2426 **** /* if base < 0: base = base % modulus Having the base positive just makes things easier. */ ! if (a->ob_size < 0) { if (l_divmod(a, c, NULL, &temp) < 0) goto Error; Py_DECREF(a); --- 2394,2400 ---- /* if base < 0: base = base % modulus Having the base positive just makes things easier. */ ! if (a->long_sign < 0) { if (l_divmod(a, c, NULL, &temp) < 0) goto Error; Py_DECREF(a); *************** *** 2535,2541 **** Py_DECREF(w); if (x == NULL) return NULL; ! x->ob_size = -(x->ob_size); return (PyObject *)x; } --- 2509,2516 ---- Py_DECREF(w); if (x == NULL) return NULL; ! if (x->ob_size) ! x->long_sign = -x->long_sign; return (PyObject *)x; } *************** *** 2560,2574 **** return (PyObject *) v; } z = (PyLongObject *)_PyLong_Copy(v); ! if (z != NULL) ! z->ob_size = -(v->ob_size); return (PyObject *)z; } static PyObject * long_abs(PyLongObject *v) { ! if (v->ob_size < 0) return long_neg(v); else return long_pos(v); --- 2535,2549 ---- return (PyObject *) v; } z = (PyLongObject *)_PyLong_Copy(v); ! if (z != NULL && z->ob_size) ! z->long_sign = -z->long_sign; return (PyObject *)z; } static PyObject * long_abs(PyLongObject *v) { ! if (v->long_sign < 0) return long_neg(v); else return long_pos(v); *************** *** 2577,2583 **** static int long_nonzero(PyLongObject *v) { ! return ABS(v->ob_size) != 0; } static PyObject * --- 2552,2558 ---- static int long_nonzero(PyLongObject *v) { ! return v->ob_size != 0; } static PyObject * *************** *** 2591,2597 **** CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->ob_size < 0) { /* Right shifting negative numbers is harder */ PyLongObject *a1, *a2; a1 = (PyLongObject *) long_invert(a); --- 2566,2572 ---- CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); ! if (a->long_sign < 0) { /* Right shifting negative numbers is harder */ PyLongObject *a1, *a2; a1 = (PyLongObject *) long_invert(a); *************** *** 2615,2621 **** goto rshift_error; } wordshift = shiftby / SHIFT; ! newsize = ABS(a->ob_size) - wordshift; if (newsize <= 0) { z = _PyLong_New(0); Py_DECREF(a); --- 2590,2596 ---- goto rshift_error; } wordshift = shiftby / SHIFT; ! newsize = a->ob_size - wordshift; if (newsize <= 0) { z = _PyLong_New(0); Py_DECREF(a); *************** *** 2629,2636 **** z = _PyLong_New(newsize); if (z == NULL) goto rshift_error; ! if (a->ob_size < 0) ! z->ob_size = -(z->ob_size); for (i = 0, j = wordshift; i < newsize; i++, j++) { z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; if (i+1 < newsize) --- 2604,2611 ---- z = _PyLong_New(newsize); if (z == NULL) goto rshift_error; ! if (a->long_sign < 0 && z->ob_size) ! z->long_sign = -1; for (i = 0, j = wordshift; i < newsize; i++, j++) { z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; if (i+1 < newsize) *************** *** 2674,2688 **** wordshift = (int)shiftby / SHIFT; remshift = (int)shiftby - wordshift * SHIFT; ! oldsize = ABS(a->ob_size); newsize = oldsize + wordshift; if (remshift) ++newsize; z = _PyLong_New(newsize); if (z == NULL) goto lshift_error; ! if (a->ob_size < 0) ! z->ob_size = -(z->ob_size); for (i = 0; i < wordshift; i++) z->ob_digit[i] = 0; accum = 0; --- 2649,2663 ---- wordshift = (int)shiftby / SHIFT; remshift = (int)shiftby - wordshift * SHIFT; ! oldsize = a->ob_size; newsize = oldsize + wordshift; if (remshift) ++newsize; z = _PyLong_New(newsize); if (z == NULL) goto lshift_error; ! if (a->long_sign < 0 && z->ob_size) ! z->long_sign = -1; for (i = 0; i < wordshift; i++) z->ob_digit[i] = 0; accum = 0; *************** *** 2718,2724 **** digit diga, digb; PyObject *v; ! if (a->ob_size < 0) { a = (PyLongObject *) long_invert(a); maska = MASK; } --- 2693,2699 ---- digit diga, digb; PyObject *v; ! if (a->long_sign < 0) { a = (PyLongObject *) long_invert(a); maska = MASK; } *************** *** 2726,2732 **** Py_INCREF(a); maska = 0; } ! if (b->ob_size < 0) { b = (PyLongObject *) long_invert(b); maskb = MASK; } --- 2701,2707 ---- Py_INCREF(a); maska = 0; } ! if (b->long_sign < 0) { b = (PyLongObject *) long_invert(b); maskb = MASK; } *************** *** 2959,2966 **** return NULL; assert(PyLong_CheckExact(tmp)); n = tmp->ob_size; - if (n < 0) - n = -n; new = (PyLongObject *)type->tp_alloc(type, n); if (new == NULL) { Py_DECREF(tmp); --- 2934,2939 ---- *************** *** 2968,2973 **** --- 2941,2947 ---- } assert(PyLong_Check(new)); new->ob_size = tmp->ob_size; + new->long_sign = tmp->long_sign; for (i = 0; i < n; i++) new->ob_digit[i] = tmp->ob_digit[i]; Py_DECREF(tmp); Index: Python/marshal.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/marshal.c,v retrieving revision 1.82 diff -c -r1.82 marshal.c *** Python/marshal.c 12 Jan 2005 16:00:55 -0000 1.82 --- Python/marshal.c 10 Apr 2005 23:40:50 -0000 *************** *** 158,166 **** PyLongObject *ob = (PyLongObject *)v; w_byte(TYPE_LONG, p); n = ob->ob_size; ! w_long((long)n, p); ! if (n < 0) ! n = -n; for (i = 0; i < n; i++) w_short(ob->ob_digit[i], p); } --- 158,164 ---- PyLongObject *ob = (PyLongObject *)v; w_byte(TYPE_LONG, p); n = ob->ob_size; ! w_long((long)n*ob->sign, p); for (i = 0; i < n; i++) w_short(ob->ob_digit[i], p); } *************** *** 488,494 **** ob = _PyLong_New(size); if (ob == NULL) return NULL; ! ob->ob_size = n; for (i = 0; i < size; i++) { int digit = r_short(p); if (digit < 0) { --- 486,492 ---- ob = _PyLong_New(size); if (ob == NULL) return NULL; ! if (n < 0) ob->sign = -1; for (i = 0; i < size; i++) { int digit = r_short(p); if (digit < 0) {