summaryrefslogtreecommitdiffstats
path: root/Objects/intobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/intobject.c')
-rw-r--r--Objects/intobject.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index 28f7606..a4d50be 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -546,6 +546,17 @@ int_mul(PyObject *v, PyObject *w)
}
}
+/* Integer overflow checking for unary negation: on a 2's-complement
+ * box, -x overflows iff x is the most negative long. In this case we
+ * get -x == x. However, -x is undefined (by C) if x /is/ the most
+ * negative long (it's a signed overflow case), and some compilers care.
+ * So we cast x to unsigned long first. However, then other compilers
+ * warn about applying unary minus to an unsigned operand. Hence the
+ * weird "0-".
+ */
+#define UNARY_NEG_WOULD_OVERFLOW(x) \
+ ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
+
/* Return type of i_divmod */
enum divmod_result {
DIVMOD_OK, /* Correct result */
@@ -564,14 +575,8 @@ i_divmod(register long x, register long y,
"integer division or modulo by zero");
return DIVMOD_ERROR;
}
- /* (-sys.maxint-1)/-1 is the only overflow case. x is the most
- * negative long iff x < 0 and, on a 2's-complement box, x == -x.
- * However, -x is undefined (by C) if x /is/ the most negative long
- * (it's a signed overflow case), and some compilers care. So we cast
- * x to unsigned long first. However, then other compilers warn about
- * applying unary minus to an unsigned operand. Hence the weird "0-".
- */
- if (y == -1 && x < 0 && (unsigned long)x == 0-(unsigned long)x)
+ /* (-sys.maxint-1)/-1 is the only overflow case. */
+ if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
return DIVMOD_OVERFLOW;
xdivy = x / y;
xmody = x - xdivy * y;
@@ -762,7 +767,8 @@ int_neg(PyIntObject *v)
{
register long a;
a = v->ob_ival;
- if (a < 0 && (unsigned long)a == 0-(unsigned long)a) {
+ /* check for overflow */
+ if (UNARY_NEG_WOULD_OVERFLOW(a)) {
PyObject *o = PyLong_FromLong(a);
if (o != NULL) {
PyObject *result = PyNumber_Negative(o);