Repository navigation
Expand file tree
/
Copy pathmpf_set_double.c
More file actions
98 lines (86 loc) · 2.1 KB
/
Copy pathmpf_set_double.c
File metadata and controls
98 lines (86 loc) · 2.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "tomfloat.h"
#include <math.h>
#if __STDC_VERSION__ >= 199901L
# include <fenv.h>
#endif
#include <float.h>
// set the double to the value of the mp_float if possible
// this method relies on the IEEE-754 complience of the libc!
int mpf_set_double(mp_float * a, double *d)
{
mp_float cp;
int err;
//mp_digit high,low;
int digits_needed, i, limit;
double multiplier;
if ((err = mpf_init_copy(a, &cp)) != MP_OKAY) {
return err;
}
// cheap check for boundaries
if ((err = mpf_normalize_to(&cp, DBL_MANT_DIG)) != MP_OKAY) {
mpf_clear(&cp);
return err;
}
#if __STDC_VERSION__ >= 199901L
if (cp.exp < -1022) {
*d = -INFINITY;
goto END;
} else if (cp.exp > 1023) {
*d = INFINITY;
goto END;
}
#else
if (cp.exp < -1022) {
*d = -HUGE_VAL;
goto END;
} else if (cp.exp > 1023) {
*d = HUGE_VAL;
goto END;
}
#endif
else if (mpf_isnan(a)) {
*d = sqrt(-1);
goto END;
} else if (mpf_iszero(a)) {
*d = (a->mantissa.sign == MP_NEG) ? -0.0 : 0.0;
goto END;
}
// TODO: feed the double directly (but beware of endianess!)
// Shamelessly ported from the libbtommath function mp_get_double
digits_needed = ((DBL_MANT_DIG + MP_DIGIT_BIT) / MP_DIGIT_BIT) + 1;
if ((err = mpf_copy(a, &cp)) != MP_OKAY) {
return err;
}
if ((err = mpf_normalize_to(&cp, DBL_MANT_DIG + MP_DIGIT_BIT)) != MP_OKAY) {
mpf_clear(&cp);
return err;
}
multiplier = (double) (MP_MASK + 1);
*d = 0.0;
/* Could be assumed, couldn't it? */
mp_clamp(&cp.mantissa);
i = cp.mantissa.used;
limit = (i <= digits_needed) ? 0 : i - digits_needed;
while (i-- > limit) {
*d += cp.mantissa.dp[i];
*d *= multiplier;
}
if (cp.mantissa.sign == MP_NEG) {
*d *= -1.0;
}
*d *= pow(2.0, i * DIGIT_BIT);
// The content of the mantissa is in *d now, apply exponent
*d *= pow(2.0, (double) (cp.exp));
/* Handle overflow */
#if __STDC_VERSION__ >= 199901L
if (*d == INFINITY) {
feraiseexcept(FE_OVERFLOW);
#else
if (*d == HUGE_VAL) {
#endif
return MP_RANGE;
}
END:
mpf_clear(&cp);
return MP_OKAY;
}