blob: 3facb923ad208c007011386c82dd25ba2feaf961 (
plain)
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
|
Index: mpi-1.8.6/mpi.c
===================================================================
--- mpi-1.8.6.orig/mpi.c 2015-02-07 19:33:11.780282866 -0800
+++ mpi-1.8.6/mpi.c 2015-02-07 19:33:14.608233363 -0800
@@ -14,6 +14,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <math.h>
typedef unsigned char mem_t;
extern mem_t *chk_calloc(size_t n, size_t size);
@@ -2333,6 +2334,29 @@
/* }}} */
+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) */
Index: mpi-1.8.6/mpi.h
===================================================================
--- mpi-1.8.6.orig/mpi.h 2015-02-07 19:33:08.688337078 -0800
+++ mpi-1.8.6/mpi.h 2015-02-07 19:33:14.612233293 -0800
@@ -187,6 +187,11 @@
#endif /* end MP_NUMTH */
/*------------------------------------------------------------------------*/
+/* Conversions */
+
+mp_err mp_to_double(mp_int *mp, double *d);
+
+/*------------------------------------------------------------------------*/
/* Input and output */
#if MP_IOFUNC
|