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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* mpn_get_d -- limbs to double conversion.
THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
FUTURE GNU MP RELEASES.
Copyright 2003, 2004 Free Software Foundation, Inc.
Copyright 2021 William Hart (adaption to Flint)
This file is part of the GNU MP Library.
The GNU MP 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 GNU MP 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 GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
/* Return the value {ptr,size}*2^exp, and negative if sign<0.
Must have size>=1, and a non-zero high limb ptr[size-1].
{ptr,size} is truncated towards zero. This is consistent with other gmp
conversions, like mpz_set_f or mpz_set_q, and is easy to implement and
test.
In the past conversions had attempted (imperfectly) to let the hardware
float rounding mode take effect, but that gets tricky since multiple
roundings need to be avoided, or taken into account, and denorms mean the
effective precision of the mantissa is not constant. (For reference,
mpz_get_d on IEEE systems was ok, except it operated on the absolute
value. mpf_get_d and mpq_get_d suffered from multiple roundings and from
not always using enough bits to get the rounding right.)
It's felt that GMP is not primarily concerned with hardware floats, and
really isn't enhanced by getting involved with hardware rounding modes
(which could even be some weird unknown style), so something unambiguous
and straightforward is best.
The IEEE code below is the usual case, it knows either a 32-bit or 64-bit
limb and is done with shifts and masks. The 64-bit case in particular
should come out nice and compact.
The generic code works one bit at a time, which will be quite slow, but
should support any binary-based "double" and be safe against any rounding
mode. Note in particular it works on IEEE systems too.
Traps:
Hardware traps for overflow to infinity, underflow to zero, or
unsupported denorms may or may not be taken. The IEEE code works bitwise
and so probably won't trigger them, the generic code works by float
operations and so probably will. This difference might be thought less
than ideal, but again its felt straightforward code is better than trying
to get intimate with hardware exceptions (of perhaps unknown nature).
Not done:
mpz_get_d in the past handled size==1 with a cast limb->double. This
might still be worthwhile there (for up to the mantissa many bits), but
for mpn_get_d here, the cost of applying "exp" to the resulting exponent
would probably use up any benefit a cast may have over bit twiddling.
Also, if the exponent is pushed into denorm range then bit twiddling is
the only option, to ensure the desired truncation is obtained.
Other:
For reference, note that HPPA 8000, 8200, 8500 and 8600 trap FCNV,UDW,DBL
to the kernel for values >= 2^63. This makes it slow, and worse the
Linux kernel (what versions?) apparently uses untested code in its trap
handling routines, and gets the sign wrong. We don't use such a limb to
double cast, neither in the IEEE or generic code. */
/* assumes unsigned int is at least 32 bits */
;
;
double