-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathgmpy2_hash.c
179 lines (158 loc) · 5.42 KB
/
gmpy2_hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* gmpy2_hash.c *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Python interface to the GMP, MPFR, and MPC multiple precision *
* libraries. *
* *
* Copyright 2000 - 2009 Alex Martelli *
* *
* Copyright 2008 - 2024 Case Van Horsen *
* *
* This file is part of GMPY2. *
* *
* GMPY2 is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation, either version 3 of the License, or (at your *
* option) any later version. *
* *
* GMPY2 is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public *
* License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "pythoncapi_compat.h"
static Py_hash_t
GMPy_MPZ_Hash_Slot(MPZ_Object *self)
{
Py_hash_t hash;
if (self->hash_cache != -1) {
return self->hash_cache;
}
hash = (Py_hash_t)mpn_mod_1(self->z->_mp_d, (mp_size_t)mpz_size(self->z), PyHASH_MODULUS);
if (mpz_sgn(self->z) < 0) {
hash = -hash;
}
if (hash == -1) {
hash = -2;
}
return (self->hash_cache = hash);
}
static Py_hash_t
GMPy_MPQ_Hash_Slot(MPQ_Object *self)
{
Py_hash_t hash = 0;
mpz_t temp, temp1, mask;
if (self->hash_cache != -1) {
return self->hash_cache;
}
mpz_init(temp);
mpz_init(temp1);
mpz_init(mask);
mpz_set_si(mask, 1);
mpz_mul_2exp(mask, mask, PyHASH_BITS);
mpz_sub_ui(mask, mask, 1);
if (!mpz_invert(temp, mpq_denref(self->q), mask)) {
mpz_clear(temp);
mpz_clear(temp1);
mpz_clear(mask);
hash = PyHASH_INF;
if (mpz_sgn(mpq_numref(self->q)) < 0) {
hash = -hash;
}
self->hash_cache = hash;
return hash;
}
mpz_set(temp1, mask);
mpz_sub_ui(temp1, temp1, 2);
mpz_powm(temp, mpq_denref(self->q), temp1, mask);
mpz_tdiv_r(temp1, mpq_numref(self->q), mask);
mpz_mul(temp, temp, temp1);
hash = (Py_hash_t)mpn_mod_1(temp->_mp_d, (mp_size_t)mpz_size(temp), PyHASH_MODULUS);
if (mpz_sgn(mpq_numref(self->q)) < 0) {
hash = -hash;
}
if (hash == -1) {
hash = -2;
}
mpz_clear(temp);
mpz_clear(temp1);
mpz_clear(mask);
self->hash_cache = hash;
return hash;
}
static Py_hash_t
_mpfr_hash(mpfr_t f)
{
Py_uhash_t hash = 0;
Py_ssize_t exp;
size_t msize;
int sign;
/* Handle special cases first */
if (!mpfr_number_p(f)) {
if (mpfr_inf_p(f)) {
if (mpfr_sgn(f) > 0) {
return PyHASH_INF;
}
else {
return -PyHASH_INF;
}
}
else {
#if PY_VERSION_HEX >= 0x030A00A0
return Py_HashPointer(f);
#else
return _PyHASH_NAN;
#endif
}
}
/* Calculate the number of limbs in the mantissa. */
msize = (f->_mpfr_prec + mp_bits_per_limb - 1) / mp_bits_per_limb;
/* Calculate the hash of the mantissa. */
if (mpfr_sgn(f) > 0) {
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, PyHASH_MODULUS);
sign = 1;
}
else if (mpfr_sgn(f) < 0) {
hash = mpn_mod_1(f->_mpfr_d, (mp_size_t)msize, PyHASH_MODULUS);
sign = -1;
}
else {
return 0;
}
/* Calculate the final hash. */
exp = f->_mpfr_exp - (msize * mp_bits_per_limb);
exp = exp >= 0 ? exp % PyHASH_BITS : PyHASH_BITS-1-((-1-exp) % PyHASH_BITS);
hash = ((hash << exp) & PyHASH_MODULUS) | hash >> (PyHASH_BITS - exp);
hash *= sign;
if (hash == (Py_uhash_t)(-1)) {
hash = (Py_uhash_t)(-2);
}
return (Py_hash_t)hash;
}
static Py_hash_t
GMPy_MPFR_Hash_Slot(MPFR_Object *self)
{
if (self->hash_cache == -1) {
self->hash_cache = _mpfr_hash(self->f);
}
return self->hash_cache;
}
static Py_hash_t
GMPy_MPC_Hash_Slot(MPC_Object *self)
{
Py_uhash_t hashreal, hashimag, combined;
if (self->hash_cache != -1) {
return self->hash_cache;
}
hashreal = (Py_uhash_t)_mpfr_hash(mpc_realref(self->c));
hashimag = (Py_uhash_t)_mpfr_hash(mpc_imagref(self->c));
combined = hashreal + PyHASH_IMAG * hashimag;
if (combined == (Py_uhash_t)(-1)) {
combined = (Py_uhash_t)(-2);
}
self->hash_cache = combined;
return (Py_hash_t)combined;
}