summaryrefslogtreecommitdiffstats
path: root/mpi
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-04-22 19:54:20 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-04-22 19:54:20 -0700
commit13ea02f16c9cdb87c74dd35c63e25ffa96718483 (patch)
tree532ee1fa5f6ca3d4bd6be452bfb2783694e9c103 /mpi
parentdb10bad508a85f1bbd86130210bbee93ff1b6f4c (diff)
downloadtxr-13ea02f16c9cdb87c74dd35c63e25ffa96718483.tar.gz
txr-13ea02f16c9cdb87c74dd35c63e25ffa96718483.tar.bz2
txr-13ea02f16c9cdb87c74dd35c63e25ffa96718483.zip
add-mp-hash patch
* mpi/mpi.c (mp_hash): New function. * mpi/mpi.c (mp_hash): Declared.
Diffstat (limited to 'mpi')
-rw-r--r--mpi/mpi.c28
-rw-r--r--mpi/mpi.h2
2 files changed, 30 insertions, 0 deletions
diff --git a/mpi/mpi.c b/mpi/mpi.c
index 26334854..48c7203f 100644
--- a/mpi/mpi.c
+++ b/mpi/mpi.c
@@ -1958,6 +1958,34 @@ int mp_iseven(mp_int *a)
/* }}} */
+unsigned long mp_hash(mp_int *a)
+{
+#if SIZEOF_LONG > MP_DIGIT_SIZE
+ unsigned long hash;
+ int ix;
+
+ if (USED(a) >= 2 * SIZEOF_LONG / MP_DIGIT_SIZE) {
+ unsigned long omega = 0;
+ unsigned long alpha = 0;
+ for (ix = 0; ix < SIZEOF_LONG / MP_DIGIT_SIZE; ix++)
+ omega = (omega << MP_DIGIT_BIT) | DIGIT(a, ix);
+ for (ix = USED(a) - SIZEOF_LONG / MP_DIGIT_SIZE; ix < USED(a); ix++)
+ alpha = (alpha << MP_DIGIT_BIT) | DIGIT(a, ix);
+ hash = alpha + omega;
+ } else {
+ hash = 0;
+
+ for (ix = 0; ix < USED(a); ix++)
+ hash = (hash << MP_DIGIT_BIT) | DIGIT(a, ix);
+ }
+#else
+ mp_digit omega = DIGIT(a, 0);
+ mp_digit alpha = DIGIT(a, USED(a) - 1);
+ unsigned long hash = alpha + omega;
+#endif
+ return SIGN(a) == MP_NEG ? ~hash : hash;
+}
+
/*------------------------------------------------------------------------*/
/* {{{ Number theoretic functions */
diff --git a/mpi/mpi.h b/mpi/mpi.h
index c32b0d3a..51ffc00d 100644
--- a/mpi/mpi.h
+++ b/mpi/mpi.h
@@ -165,6 +165,8 @@ int mp_cmp_int(mp_int *a, long z);
int mp_isodd(mp_int *a);
int mp_iseven(mp_int *a);
+unsigned long mp_hash(mp_int *a);
+
/*------------------------------------------------------------------------*/
/* Number theoretic */