summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2006-09-05 01:47:53 (GMT)
committerTim Peters <tim.peters@gmail.com>2006-09-05 01:47:53 (GMT)
commitcbdd350dd7e393933437d5c12eabe8e47b6a40d9 (patch)
tree2762ddd912dcd47079d896b0d262fcfcb4675d94 /Objects
parent3467352fe21270db6ed09d2d0213d02cc8c1e9e5 (diff)
downloadcpython-cbdd350dd7e393933437d5c12eabe8e47b6a40d9.zip
cpython-cbdd350dd7e393933437d5c12eabe8e47b6a40d9.tar.gz
cpython-cbdd350dd7e393933437d5c12eabe8e47b6a40d9.tar.bz2
i_divmod(): As discussed on Python-Dev, changed the overflow
checking to live happily with recent gcc optimizations that assume signed integer arithmetic never overflows.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/intobject.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/Objects/intobject.c b/Objects/intobject.c
index c7137df..b94e3e9 100644
--- a/Objects/intobject.c
+++ b/Objects/intobject.c
@@ -564,8 +564,14 @@ 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. */
- if (y == -1 && x < 0 && x == -x)
+ /* (-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)
return DIVMOD_OVERFLOW;
xdivy = x / y;
xmody = x - xdivy * y;