summaryrefslogtreecommitdiffstats
path: root/mpi-patches/mpi-to-double
blob: 608e9dc3e722526ae75b2c7c78b1e18a912f1aee (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	2012-03-20 22:20:10.242815758 -0700
+++ mpi-1.8.6/mpi.c	2012-03-21 06:48:36.401050757 -0700
@@ -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);
@@ -2329,6 +2330,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	2012-03-20 22:20:09.994676258 -0700
+++ mpi-1.8.6/mpi.h	2012-03-20 22:20:10.498959758 -0700
@@ -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