mirror of
https://github.com/python/cpython.git
synced 2025-08-12 12:58:50 +00:00
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. ........
This commit is contained in:
parent
f66326f8bd
commit
24058549f5
1 changed files with 9 additions and 8 deletions
|
@ -1962,7 +1962,7 @@ long_compare(PyLongObject *a, PyLongObject *b)
|
||||||
static long
|
static long
|
||||||
long_hash(PyLongObject *v)
|
long_hash(PyLongObject *v)
|
||||||
{
|
{
|
||||||
long x;
|
unsigned long x;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
int sign;
|
int sign;
|
||||||
|
|
||||||
|
@ -1977,17 +1977,18 @@ long_hash(PyLongObject *v)
|
||||||
i = -(i);
|
i = -(i);
|
||||||
}
|
}
|
||||||
#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
|
#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
|
||||||
/* The following loop produces a C long x such that (unsigned long)x
|
/* The following loop produces a C unsigned long x such that x is
|
||||||
is congruent to the absolute value of v modulo ULONG_MAX. The
|
congruent to the absolute value of v modulo ULONG_MAX. The
|
||||||
resulting x is nonzero if and only if v is. */
|
resulting x is nonzero if and only if v is. */
|
||||||
while (--i >= 0) {
|
while (--i >= 0) {
|
||||||
/* Force a native long #-bits (32 or 64) circular shift */
|
/* 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];
|
x += v->ob_digit[i];
|
||||||
/* If the addition above overflowed (thinking of x as
|
/* If the addition above overflowed we compensate by
|
||||||
unsigned), we compensate by incrementing. This preserves
|
incrementing. This preserves the value modulo
|
||||||
the value modulo ULONG_MAX. */
|
ULONG_MAX. */
|
||||||
if ((unsigned long)x < v->ob_digit[i])
|
if (x < v->ob_digit[i])
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
#undef LONG_BIT_PyLong_SHIFT
|
#undef LONG_BIT_PyLong_SHIFT
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue