summaryrefslogtreecommitdiffstats
path: root/mpi-patches/mpi-to-double
blob: dae195b21aa694aa86f2838589c83db5994708f1 (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	2014-02-19 19:00:21.279452088 -0800
+++ mpi-1.8.6/mpi.c	2014-02-19 19:01:29.294491437 -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_malloc(size_t size);
@@ -2335,6 +2336,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	2014-02-19 18:59:53.335849528 -0800
+++ mpi-1.8.6/mpi.h	2014-02-19 19:01:29.302491325 -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