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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/* mpfr_set_ld -- convert a machine long double to
a multiple precision floating-point number
Copyright 2002 Free Software Foundation, Inc.
This file is part of the MPFR Library.
The MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
The MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <float.h>
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
#ifndef LDBL_MANT_DIG
#define LDBL_MANT_DIG 113 /* works also if long double == quad */
#endif
#ifndef DBL_MANT_DIG
#define DBL_MANT_DIG 53
#endif
int
mpfr_set_ld (mpfr_ptr r, long double d, mp_rnd_t rnd_mode)
{
mpfr_t t, u;
int inexact, shift_exp = 0, inexact2 = 0;
mpfr_init2 (t, LDBL_MANT_DIG);
mpfr_init2 (u, DBL_MANT_DIG);
mpfr_set_ui (t, 0, GMP_RNDN);
while (d != 0.0)
{
mpfr_set_d (u, (double) d, GMP_RNDN);
if (mpfr_inf_p (u) && (d + d != d))
{ /* d is neither +Inf nor -Inf and u is Inf: this implies that an
overflow occured, i.e. the exponent of d is too large for the
double format */
long double div10, div11, div12, div13;
div10 = (long double) (double) 1.797693134862315907729e308; /* 2^(2^10) */
div11 = div10 * div10; /* 2^(2^11) */
div12 = div11 * div11; /* 2^(2^12) */
div13 = div12 * div12; /* 2^(2^13) */
if (ABS(d) >= div13)
{
d = d / div13; /* exact */
shift_exp += 8192;
}
if (ABS(d) >= div12)
{
d = d / div12; /* exact */
shift_exp += 4096;
}
if (ABS(d) >= div11)
{
d = d / div11; /* exact */
shift_exp += 2048;
}
if (ABS(d) >= div10)
{
d = d / div10; /* exact */
shift_exp += 1024;
}
}
else if (MPFR_IS_ZERO(u) == 0 && (d != 0.0)) /* underflow */
{
long double div10, div11, div12, div13;
div10 = (long double) (double) 5.5626846462680034577255e-309; /* 2^(-2^10) */
div11 = div10 * div10; /* 2^(-2^11) */
div12 = div11 * div11; /* 2^(-2^12) */
div13 = div12 * div12; /* 2^(-2^13) */
if (ABS(d) <= div13)
{
d = d / div13; /* exact */
shift_exp -= 8192;
}
if (ABS(d) <= div12)
{
d = d / div12; /* exact */
shift_exp -= 4096;
}
if (ABS(d) <= div11)
{
d = d / div11; /* exact */
shift_exp -= 2048;
}
if (ABS(d) <= div10)
{
d = d / div10; /* exact */
shift_exp -= 1024;
}
}
mpfr_add (t, t, u, GMP_RNDN); /* exact */
if (!mpfr_number_p (t))
break;
d = d - (long double) mpfr_get_d1 (u); /* exact */
}
/* now t is exactly the input value d */
inexact = mpfr_set (r, t, rnd_mode);
if (shift_exp > 0)
inexact2 = mpfr_mul_2exp (r, r, shift_exp, rnd_mode);
else if (shift_exp < 0)
inexact2 = mpfr_div_2exp (r, r, -shift_exp, rnd_mode);
if (inexact2) /* overflow */
inexact = inexact2;
mpfr_clear (t);
mpfr_clear (u);
return inexact;
}
|