1#
2# These tests require gmpy and test the limits of the 32-bit build. The
3# limits of the 64-bit build are so large that they cannot be tested
4# on accessible hardware.
5#
6
7import sys
8from decimal import *
9from gmpy import mpz
10
11
12_PyHASH_MODULUS = sys.hash_info.modulus
13# hash values to use for positive and negative infinities, and nans
14_PyHASH_INF = sys.hash_info.inf
15_PyHASH_NAN = sys.hash_info.nan
16
17# _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
18_PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
19
20def xhash(coeff, exp):
21    sign = 1
22    if coeff < 0:
23        sign = -1
24        coeff = -coeff
25    if exp >= 0:
26        exp_hash = pow(10, exp, _PyHASH_MODULUS)
27    else:
28        exp_hash = pow(_PyHASH_10INV, -exp, _PyHASH_MODULUS)
29    hash_ = coeff * exp_hash % _PyHASH_MODULUS
30    ans = hash_ if sign == 1 else -hash_
31    return -2 if ans == -1 else ans
32
33
34x = mpz(10) ** 425000000 - 1
35coeff = int(x)
36
37d = Decimal('9' * 425000000 + 'e-849999999')
38
39h1 = xhash(coeff, -849999999)
40h2 = hash(d)
41
42assert h2 == h1
43