forked from libtom/libtommath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn_mp_get_double.c
63 lines (48 loc) · 1.05 KB
/
bn_mp_get_double.c
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
59
60
61
62
63
#include <tommath.h>
#ifdef BN_MP_GET_DOUBLE_C
#ifndef DBL_MANT_DIG
#define DBL_MANT_DIG 53
#endif
static double st_pow(double d, int e){
double t;
if (e == 0) {
return d;
}
t = 1.0;
while(e > 1){
if(e % 2 == 0){
d *= d;
e /= 2;
} else {
t *= d;
d *= d;
e = (e - 1)/2;
}
}
return d * t;
}
/* Convert to double, assumes IEEE-754 conforming double. From code by
gerdr (Gerhard R.) https://github.com/gerdr */
int mp_get_double(mp_int *a, double *d)
{
int digits_needed, i, limit;
double multiplier;
/* digits_needed * DIGIT_BIT >= 64 bit */
digits_needed = ((DBL_MANT_DIG + DIGIT_BIT) / DIGIT_BIT) + 1;
multiplier = (double)(MP_MASK + 1);
*d = 0.0;
/* Could be assumed, couldn't it? */
mp_clamp(a);
i = a->used;
limit = (i <= digits_needed) ? 0 : i - digits_needed;
while (i-- > limit) {
*d += a->dp[i];
*d *= multiplier;
}
if (a->sign == MP_NEG) {
*d *= -1.0;
}
*d *= st_pow(2.0, i * DIGIT_BIT);
return MP_OKAY;
}
#endif