diff Objects/longobject.c @ 51083:c23e40d1abbc 3.0

Merged revisions 68980 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r68980 | mark.dickinson | 2009-01-26 21:56:07 +0000 (Mon, 26 Jan 2009) | 14 lines Merged revisions 68974-68975 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r68974 | mark.dickinson | 2009-01-26 21:36:30 +0000 (Mon, 26 Jan 2009) | 4 lines Fix undefined behaviour (left shift of negative value) in long_hash. Also, rewrap a line of length > 79, and update comments. ........ r68975 | mark.dickinson | 2009-01-26 21:40:08 +0000 (Mon, 26 Jan 2009) | 2 lines Fix comment. ........ ................
author Mark Dickinson <dickinsm@gmail.com>
date Mon, 26 Jan 2009 22:05:51 +0000
parents 45a5276e5dcc
children 9c1f06b273f7
line wrap: on
line diff
--- a/Objects/longobject.c	Mon Jan 26 22:03:09 2009 +0000
+++ b/Objects/longobject.c	Mon Jan 26 22:05:51 2009 +0000
@@ -2274,7 +2274,7 @@
 static long
 long_hash(PyLongObject *v)
 {
-	long x;
+	unsigned long x;
 	Py_ssize_t i;
 	int sign;
 
@@ -2294,17 +2294,18 @@
 		i = -(i);
 	}
 #define LONG_BIT_PyLong_SHIFT	(8*sizeof(long) - PyLong_SHIFT)
-	/* The following loop produces a C long x such that (unsigned long)x
-	   is congruent to the absolute value of v modulo ULONG_MAX.  The
+	/* The following loop produces a C unsigned long x such that x is
+	   congruent to the absolute value of v modulo ULONG_MAX.  The
 	   resulting x is nonzero if and only if v is. */
 	while (--i >= 0) {
 		/* Force a native long #-bits (32 or 64) circular shift */
-		x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
+		x = ((x << PyLong_SHIFT) & ~PyLong_MASK) |
+			((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
 		x += v->ob_digit[i];
-		/* If the addition above overflowed (thinking of x as
-		   unsigned), we compensate by incrementing.  This preserves
-		   the value modulo ULONG_MAX. */
-		if ((unsigned long)x < v->ob_digit[i])
+		/* If the addition above overflowed we compensate by
+		   incrementing.  This preserves the value modulo
+		   ULONG_MAX. */
+		if (x < v->ob_digit[i])
 			x++;
 	}
 #undef LONG_BIT_PyLong_SHIFT