From 555f6d6e75948601f763d84410cb4148786b0059 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 22 Apr 2015 19:55:07 -0700 Subject: mpi-to-double patch * mpi/mpi.c (mp_to_double): New function. * mpi/mpi.h (mp_to_double): Declared. --- mpi/mpi.c | 24 ++++++++++++++++++++++++ mpi/mpi.h | 5 +++++ 2 files changed, 29 insertions(+) (limited to 'mpi') diff --git a/mpi/mpi.c b/mpi/mpi.c index 8ea4b1ba..33815a9d 100644 --- a/mpi/mpi.c +++ b/mpi/mpi.c @@ -14,6 +14,7 @@ #include #include #include +#include typedef unsigned char mem_t; extern mem_t *chk_calloc(size_t n, size_t size); @@ -2333,6 +2334,29 @@ X: /* }}} */ +mp_err mp_to_double(mp_int *mp, double *d) +{ + int ix; + mp_size used = USED(mp); + mp_digit *dp = DIGITS(mp); + static double mult; + double out = dp[used - 1]; + + if (!mult) + mult = pow(2.0, MP_DIGIT_BIT); + + for (ix = (int) used - 2; ix >= 0; ix--) { + out = out * mult; + out += (double) dp[ix]; + } + + if (SIGN(mp) == MP_NEG) + out = -out; + + *d = out; + return MP_OKAY; +} + /*------------------------------------------------------------------------*/ /* {{{ mp_print(mp, ofp) */ diff --git a/mpi/mpi.h b/mpi/mpi.h index ba255419..c6136c15 100644 --- a/mpi/mpi.h +++ b/mpi/mpi.h @@ -186,6 +186,11 @@ mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y); mp_err mp_invmod(mp_int *a, mp_int *m, mp_int *c); #endif /* end MP_NUMTH */ +/*------------------------------------------------------------------------*/ +/* Conversions */ + +mp_err mp_to_double(mp_int *mp, double *d); + /*------------------------------------------------------------------------*/ /* Input and output */ -- cgit v1.2.3