Compatible numeric hashes - Code Review (original) (raw)
OLD
NEW
1 /* Long (arbitrary precision) integer object implementation */
1 /* Long (arbitrary precision) integer object implementation */
2
2
3 /* XXX The functional organization of this file is terrible */
3 /* XXX The functional organization of this file is terrible */
4
4
5 #include "Python.h"
5 #include "Python.h"
6 #include "longintrepr.h"
6 #include "longintrepr.h"
7 #include "structseq.h"
7 #include "structseq.h"
8
8
9 #include <float.h>
9 #include <float.h>
10 #include <ctype.h>
10 #include <ctype.h>
(...skipping 2554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
2565 case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
2565 case -1: return v->ob_digit[0]==1 ? -2 : -(sdigit)v->ob_digit[0];
2566 case 0: return 0;
2566 case 0: return 0;
2567 case 1: return v->ob_digit[0];
2567 case 1: return v->ob_digit[0];
2568 }
2568 }
2569 sign = 1;
2569 sign = 1;
2570 x = 0;
2570 x = 0;
2571 if (i < 0) {
2571 if (i < 0) {
2572 sign = -1;
2572 sign = -1;
2573 i = -(i);
2573 i = -(i);
2574 }
2574 }
2575 /* The following loop produces a C unsigned long x such that x is
2576 congruent to the absolute value of v modulo ULONG_MAX. The
2577 resulting x is nonzero if and only if v is. */
2578 while (--i >= 0) {
2575 while (--i >= 0) {
2579 » » /* Force a native long #-bits (32 or 64) circular shift */
2576 » » /* Rotate bottom 31 bits left by PyLong_SHIFT bits; in effect,
2580 » » x = (x >> (8*SIZEOF_LONG-PyLong_SHIFT)) | (x << PyLong_SHIFT);
2577 » » this multiplies by 2**PyLong_SHIFT modulo 2**31 - 1. */
2578 » » x = ((x << PyLong_SHIFT) & _PyHASH_MASK) |
2579 » » » (x >> (_PyHASH_BITS - PyLong_SHIFT));
2581 x += v->ob_digit[i];
2580 x += v->ob_digit[i];
2582 /* If the addition above overflowed we compensate by
2581 /* If the addition above overflowed we compensate by
2583 incrementing. This preserves the value modulo
2582 incrementing. This preserves the value modulo
2584 ULONG_MAX. */
2583 ULONG_MAX. */
2585 » » if (x < v->ob_digit[i])
2584 » » if (x > _PyHASH_MASK)
2586 » » » x++;
2585 » » » x -= _PyHASH_MASK;
2587 }
2586 }
2588 x = x * sign;
2587 x = x * sign;
2589 if (x == (unsigned long)-1)
2588 if (x == (unsigned long)-1)
2590 x = (unsigned long)-2;
2589 x = (unsigned long)-2;
2591 return (long)x;
2590 return (long)x;
2592 }
2591 }
2593
2592
2594
2593
2595 /* Add the absolute values of two long integers. */
2594 /* Add the absolute values of two long integers. */
2596
2595
(...skipping 2214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...
4811 reinitializations will fail. */
4810 reinitializations will fail. */
4812 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
4811 #if NSMALLNEGINTS + NSMALLPOSINTS > 0
4813 int i;
4812 int i;
4814 PyLongObject *v = small_ints;
4813 PyLongObject *v = small_ints;
4815 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
4814 for (i = 0; i < NSMALLNEGINTS + NSMALLPOSINTS; i++, v++) {
4816 _Py_DEC_REFTOTAL;
4815 _Py_DEC_REFTOTAL;
4817 _Py_ForgetReference((PyObject*)v);
4816 _Py_ForgetReference((PyObject*)v);
4818 }
4817 }
4819 #endif
4818 #endif
4820 }
4819 }
OLD
NEW