Commit | Line | Data |
---|---|---|
d16aafd8 | 1 | /* Floating point routines for GDB, the GNU debugger. |
f1908289 | 2 | |
61baf725 | 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
d16aafd8 AC |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
d16aafd8 AC |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d16aafd8 AC |
19 | |
20 | /* Support for converting target fp numbers into host DOUBLEST format. */ | |
21 | ||
22 | /* XXX - This code should really be in libiberty/floatformat.c, | |
23 | however configuration issues with libiberty made this very | |
24 | difficult to do in the available time. */ | |
25 | ||
26 | #include "defs.h" | |
27 | #include "doublest.h" | |
28 | #include "floatformat.h" | |
96d2f608 | 29 | #include "gdbtypes.h" |
d16aafd8 | 30 | #include <math.h> /* ldexp */ |
325fac50 | 31 | #include <algorithm> |
d16aafd8 AC |
32 | |
33 | /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not | |
34 | going to bother with trying to muck around with whether it is defined in | |
35 | a system header, what we do if not, etc. */ | |
36 | #define FLOATFORMAT_CHAR_BIT 8 | |
37 | ||
fcab3fb5 RE |
38 | /* The number of bytes that the largest floating-point type that we |
39 | can convert to doublest will need. */ | |
40 | #define FLOATFORMAT_LARGEST_BYTES 16 | |
41 | ||
d16aafd8 AC |
42 | /* Extract a field which starts at START and is LEN bytes long. DATA and |
43 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
44 | static unsigned long | |
108d6ead | 45 | get_field (const bfd_byte *data, enum floatformat_byteorders order, |
d16aafd8 AC |
46 | unsigned int total_len, unsigned int start, unsigned int len) |
47 | { | |
48 | unsigned long result; | |
49 | unsigned int cur_byte; | |
50 | int cur_bitshift; | |
51 | ||
fcab3fb5 RE |
52 | /* Caller must byte-swap words before calling this routine. */ |
53 | gdb_assert (order == floatformat_little || order == floatformat_big); | |
54 | ||
d16aafd8 | 55 | /* Start at the least significant part of the field. */ |
fcab3fb5 | 56 | if (order == floatformat_little) |
d16aafd8 AC |
57 | { |
58 | /* We start counting from the other end (i.e, from the high bytes | |
59 | rather than the low bytes). As such, we need to be concerned | |
0963b4bd | 60 | with what happens if bit 0 doesn't start on a byte boundary. |
d16aafd8 AC |
61 | I.e, we need to properly handle the case where total_len is |
62 | not evenly divisible by 8. So we compute ``excess'' which | |
63 | represents the number of bits from the end of our starting | |
0963b4bd | 64 | byte needed to get to bit 0. */ |
d16aafd8 | 65 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); |
9a619af0 | 66 | |
d16aafd8 AC |
67 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) |
68 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
69 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
70 | - FLOATFORMAT_CHAR_BIT; | |
71 | } | |
72 | else | |
73 | { | |
74 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
75 | cur_bitshift = | |
76 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
77 | } | |
78 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
79 | result = *(data + cur_byte) >> (-cur_bitshift); | |
80 | else | |
81 | result = 0; | |
82 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
fcab3fb5 | 83 | if (order == floatformat_little) |
d16aafd8 AC |
84 | ++cur_byte; |
85 | else | |
86 | --cur_byte; | |
87 | ||
88 | /* Move towards the most significant part of the field. */ | |
89 | while (cur_bitshift < len) | |
90 | { | |
91 | result |= (unsigned long)*(data + cur_byte) << cur_bitshift; | |
92 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
c35f4ffc AC |
93 | switch (order) |
94 | { | |
95 | case floatformat_little: | |
96 | ++cur_byte; | |
97 | break; | |
98 | case floatformat_big: | |
99 | --cur_byte; | |
100 | break; | |
c35f4ffc | 101 | } |
d16aafd8 AC |
102 | } |
103 | if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT) | |
0963b4bd | 104 | /* Mask out bits which are not part of the field. */ |
d16aafd8 AC |
105 | result &= ((1UL << len) - 1); |
106 | return result; | |
107 | } | |
108 | ||
0a3e99f6 MK |
109 | /* Normalize the byte order of FROM into TO. If no normalization is |
110 | needed then FMT->byteorder is returned and TO is not changed; | |
111 | otherwise the format of the normalized form in TO is returned. */ | |
112 | ||
fcab3fb5 RE |
113 | static enum floatformat_byteorders |
114 | floatformat_normalize_byteorder (const struct floatformat *fmt, | |
115 | const void *from, void *to) | |
116 | { | |
117 | const unsigned char *swapin; | |
118 | unsigned char *swapout; | |
119 | int words; | |
120 | ||
121 | if (fmt->byteorder == floatformat_little | |
122 | || fmt->byteorder == floatformat_big) | |
123 | return fmt->byteorder; | |
124 | ||
fcab3fb5 RE |
125 | words = fmt->totalsize / FLOATFORMAT_CHAR_BIT; |
126 | words >>= 2; | |
127 | ||
128 | swapout = (unsigned char *)to; | |
129 | swapin = (const unsigned char *)from; | |
130 | ||
0a3e99f6 MK |
131 | if (fmt->byteorder == floatformat_vax) |
132 | { | |
133 | while (words-- > 0) | |
134 | { | |
135 | *swapout++ = swapin[1]; | |
136 | *swapout++ = swapin[0]; | |
137 | *swapout++ = swapin[3]; | |
138 | *swapout++ = swapin[2]; | |
139 | swapin += 4; | |
140 | } | |
141 | /* This may look weird, since VAX is little-endian, but it is | |
142 | easier to translate to big-endian than to little-endian. */ | |
143 | return floatformat_big; | |
144 | } | |
145 | else | |
fcab3fb5 | 146 | { |
0a3e99f6 MK |
147 | gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword); |
148 | ||
149 | while (words-- > 0) | |
150 | { | |
151 | *swapout++ = swapin[3]; | |
152 | *swapout++ = swapin[2]; | |
153 | *swapout++ = swapin[1]; | |
154 | *swapout++ = swapin[0]; | |
155 | swapin += 4; | |
156 | } | |
157 | return floatformat_big; | |
fcab3fb5 | 158 | } |
fcab3fb5 RE |
159 | } |
160 | ||
d16aafd8 AC |
161 | /* Convert from FMT to a DOUBLEST. |
162 | FROM is the address of the extended float. | |
163 | Store the DOUBLEST in *TO. */ | |
164 | ||
c422e771 AC |
165 | static void |
166 | convert_floatformat_to_doublest (const struct floatformat *fmt, | |
167 | const void *from, | |
168 | DOUBLEST *to) | |
d16aafd8 AC |
169 | { |
170 | unsigned char *ufrom = (unsigned char *) from; | |
171 | DOUBLEST dto; | |
172 | long exponent; | |
173 | unsigned long mant; | |
174 | unsigned int mant_bits, mant_off; | |
175 | int mant_bits_left; | |
0963b4bd | 176 | int special_exponent; /* It's a NaN, denorm or zero. */ |
fcab3fb5 RE |
177 | enum floatformat_byteorders order; |
178 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
20389057 | 179 | enum float_kind kind; |
fcab3fb5 RE |
180 | |
181 | gdb_assert (fmt->totalsize | |
182 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
d16aafd8 | 183 | |
20389057 DJ |
184 | /* For non-numbers, reuse libiberty's logic to find the correct |
185 | format. We do not lose any precision in this case by passing | |
186 | through a double. */ | |
9a3c8263 | 187 | kind = floatformat_classify (fmt, (const bfd_byte *) from); |
20389057 DJ |
188 | if (kind == float_infinite || kind == float_nan) |
189 | { | |
190 | double dto; | |
9a619af0 | 191 | |
f5aee5ee AM |
192 | floatformat_to_double (fmt->split_half ? fmt->split_half : fmt, |
193 | from, &dto); | |
20389057 DJ |
194 | *to = (DOUBLEST) dto; |
195 | return; | |
196 | } | |
197 | ||
fcab3fb5 | 198 | order = floatformat_normalize_byteorder (fmt, ufrom, newfrom); |
d16aafd8 | 199 | |
fcab3fb5 RE |
200 | if (order != fmt->byteorder) |
201 | ufrom = newfrom; | |
d16aafd8 | 202 | |
b14d30e1 JM |
203 | if (fmt->split_half) |
204 | { | |
542a88d0 | 205 | DOUBLEST dtop, dbot; |
9a619af0 | 206 | |
542a88d0 | 207 | floatformat_to_doublest (fmt->split_half, ufrom, &dtop); |
b14d30e1 JM |
208 | /* Preserve the sign of 0, which is the sign of the top |
209 | half. */ | |
210 | if (dtop == 0.0) | |
211 | { | |
542a88d0 | 212 | *to = dtop; |
b14d30e1 JM |
213 | return; |
214 | } | |
542a88d0 | 215 | floatformat_to_doublest (fmt->split_half, |
b14d30e1 JM |
216 | ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, |
217 | &dbot); | |
542a88d0 | 218 | *to = dtop + dbot; |
b14d30e1 JM |
219 | return; |
220 | } | |
221 | ||
fcab3fb5 RE |
222 | exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start, |
223 | fmt->exp_len); | |
d16aafd8 AC |
224 | /* Note that if exponent indicates a NaN, we can't really do anything useful |
225 | (not knowing if the host has NaN's, or how to build one). So it will | |
226 | end up as an infinity or something close; that is OK. */ | |
227 | ||
228 | mant_bits_left = fmt->man_len; | |
229 | mant_off = fmt->man_start; | |
230 | dto = 0.0; | |
231 | ||
232 | special_exponent = exponent == 0 || exponent == fmt->exp_nan; | |
233 | ||
0963b4bd MS |
234 | /* Don't bias NaNs. Use minimum exponent for denorms. For |
235 | simplicity, we don't check for zero as the exponent doesn't matter. | |
236 | Note the cast to int; exp_bias is unsigned, so it's important to | |
237 | make sure the operation is done in signed arithmetic. */ | |
d16aafd8 AC |
238 | if (!special_exponent) |
239 | exponent -= fmt->exp_bias; | |
240 | else if (exponent == 0) | |
1c704f11 | 241 | exponent = 1 - fmt->exp_bias; |
d16aafd8 AC |
242 | |
243 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
0963b4bd | 244 | who cares. */ |
d16aafd8 AC |
245 | |
246 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
247 | increment the exponent by one to account for the integer bit. */ | |
248 | ||
249 | if (!special_exponent) | |
250 | { | |
251 | if (fmt->intbit == floatformat_intbit_no) | |
252 | dto = ldexp (1.0, exponent); | |
253 | else | |
254 | exponent++; | |
255 | } | |
256 | ||
257 | while (mant_bits_left > 0) | |
258 | { | |
325fac50 | 259 | mant_bits = std::min (mant_bits_left, 32); |
d16aafd8 | 260 | |
fcab3fb5 | 261 | mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 AC |
262 | |
263 | dto += ldexp ((double) mant, exponent - mant_bits); | |
264 | exponent -= mant_bits; | |
265 | mant_off += mant_bits; | |
266 | mant_bits_left -= mant_bits; | |
267 | } | |
268 | ||
269 | /* Negate it if negative. */ | |
fcab3fb5 | 270 | if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1)) |
d16aafd8 AC |
271 | dto = -dto; |
272 | *to = dto; | |
273 | } | |
274 | \f | |
d16aafd8 AC |
275 | /* Set a field which starts at START and is LEN bytes long. DATA and |
276 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
277 | static void | |
278 | put_field (unsigned char *data, enum floatformat_byteorders order, | |
279 | unsigned int total_len, unsigned int start, unsigned int len, | |
280 | unsigned long stuff_to_put) | |
281 | { | |
282 | unsigned int cur_byte; | |
283 | int cur_bitshift; | |
284 | ||
fcab3fb5 RE |
285 | /* Caller must byte-swap words before calling this routine. */ |
286 | gdb_assert (order == floatformat_little || order == floatformat_big); | |
287 | ||
d16aafd8 | 288 | /* Start at the least significant part of the field. */ |
fcab3fb5 | 289 | if (order == floatformat_little) |
d16aafd8 AC |
290 | { |
291 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
9a619af0 | 292 | |
d16aafd8 AC |
293 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) |
294 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
295 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
296 | - FLOATFORMAT_CHAR_BIT; | |
297 | } | |
298 | else | |
299 | { | |
300 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
301 | cur_bitshift = | |
302 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
303 | } | |
304 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
305 | { | |
306 | *(data + cur_byte) &= | |
307 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) | |
308 | << (-cur_bitshift)); | |
309 | *(data + cur_byte) |= | |
310 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
311 | } | |
312 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
fcab3fb5 | 313 | if (order == floatformat_little) |
d16aafd8 AC |
314 | ++cur_byte; |
315 | else | |
316 | --cur_byte; | |
317 | ||
318 | /* Move towards the most significant part of the field. */ | |
319 | while (cur_bitshift < len) | |
320 | { | |
321 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
322 | { | |
323 | /* This is the last byte. */ | |
324 | *(data + cur_byte) &= | |
325 | ~((1 << (len - cur_bitshift)) - 1); | |
326 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
327 | } | |
328 | else | |
329 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
330 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
331 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
fcab3fb5 | 332 | if (order == floatformat_little) |
d16aafd8 AC |
333 | ++cur_byte; |
334 | else | |
335 | --cur_byte; | |
336 | } | |
337 | } | |
338 | ||
0a3e99f6 MK |
339 | /* The converse: convert the DOUBLEST *FROM to an extended float and |
340 | store where TO points. Neither FROM nor TO have any alignment | |
d16aafd8 AC |
341 | restrictions. */ |
342 | ||
c422e771 | 343 | static void |
cc2f3c35 | 344 | convert_doublest_to_floatformat (const struct floatformat *fmt, |
0a3e99f6 | 345 | const DOUBLEST *from, void *to) |
d16aafd8 AC |
346 | { |
347 | DOUBLEST dfrom; | |
348 | int exponent; | |
349 | DOUBLEST mant; | |
350 | unsigned int mant_bits, mant_off; | |
351 | int mant_bits_left; | |
352 | unsigned char *uto = (unsigned char *) to; | |
fcab3fb5 | 353 | enum floatformat_byteorders order = fmt->byteorder; |
0a3e99f6 | 354 | unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; |
fcab3fb5 | 355 | |
0a3e99f6 | 356 | if (order != floatformat_little) |
fcab3fb5 | 357 | order = floatformat_big; |
d16aafd8 | 358 | |
0a3e99f6 MK |
359 | if (order != fmt->byteorder) |
360 | uto = newto; | |
361 | ||
d16aafd8 AC |
362 | memcpy (&dfrom, from, sizeof (dfrom)); |
363 | memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) | |
364 | / FLOATFORMAT_CHAR_BIT); | |
b14d30e1 JM |
365 | |
366 | if (fmt->split_half) | |
367 | { | |
368 | /* Use static volatile to ensure that any excess precision is | |
369 | removed via storing in memory, and so the top half really is | |
370 | the result of converting to double. */ | |
371 | static volatile double dtop, dbot; | |
542a88d0 | 372 | DOUBLEST dtopnv, dbotnv; |
9a619af0 | 373 | |
b14d30e1 JM |
374 | dtop = (double) dfrom; |
375 | /* If the rounded top half is Inf, the bottom must be 0 not NaN | |
376 | or Inf. */ | |
377 | if (dtop + dtop == dtop && dtop != 0.0) | |
378 | dbot = 0.0; | |
379 | else | |
380 | dbot = (double) (dfrom - (DOUBLEST) dtop); | |
381 | dtopnv = dtop; | |
382 | dbotnv = dbot; | |
542a88d0 LM |
383 | floatformat_from_doublest (fmt->split_half, &dtopnv, uto); |
384 | floatformat_from_doublest (fmt->split_half, &dbotnv, | |
b14d30e1 JM |
385 | (uto |
386 | + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2)); | |
387 | return; | |
388 | } | |
389 | ||
d16aafd8 AC |
390 | if (dfrom == 0) |
391 | return; /* Result is zero */ | |
392 | if (dfrom != dfrom) /* Result is NaN */ | |
393 | { | |
394 | /* From is NaN */ | |
fcab3fb5 | 395 | put_field (uto, order, fmt->totalsize, fmt->exp_start, |
d16aafd8 | 396 | fmt->exp_len, fmt->exp_nan); |
0963b4bd | 397 | /* Be sure it's not infinity, but NaN value is irrel. */ |
fcab3fb5 | 398 | put_field (uto, order, fmt->totalsize, fmt->man_start, |
fbe12357 | 399 | fmt->man_len, 1); |
fcab3fb5 | 400 | goto finalize_byteorder; |
d16aafd8 AC |
401 | } |
402 | ||
403 | /* If negative, set the sign bit. */ | |
404 | if (dfrom < 0) | |
405 | { | |
fcab3fb5 | 406 | put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1); |
d16aafd8 AC |
407 | dfrom = -dfrom; |
408 | } | |
409 | ||
0963b4bd | 410 | if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */ |
d16aafd8 AC |
411 | { |
412 | /* Infinity exponent is same as NaN's. */ | |
fcab3fb5 | 413 | put_field (uto, order, fmt->totalsize, fmt->exp_start, |
d16aafd8 AC |
414 | fmt->exp_len, fmt->exp_nan); |
415 | /* Infinity mantissa is all zeroes. */ | |
fcab3fb5 | 416 | put_field (uto, order, fmt->totalsize, fmt->man_start, |
d16aafd8 | 417 | fmt->man_len, 0); |
fcab3fb5 | 418 | goto finalize_byteorder; |
d16aafd8 AC |
419 | } |
420 | ||
421 | #ifdef HAVE_LONG_DOUBLE | |
85d3b769 | 422 | mant = frexpl (dfrom, &exponent); |
d16aafd8 AC |
423 | #else |
424 | mant = frexp (dfrom, &exponent); | |
425 | #endif | |
426 | ||
33d7655b JB |
427 | if (exponent + fmt->exp_bias <= 0) |
428 | { | |
429 | /* The value is too small to be expressed in the destination | |
430 | type (not enough bits in the exponent. Treat as 0. */ | |
431 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
432 | fmt->exp_len, 0); | |
433 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
434 | fmt->man_len, 0); | |
435 | goto finalize_byteorder; | |
436 | } | |
437 | ||
32560274 | 438 | if (exponent + fmt->exp_bias >= (1 << fmt->exp_len)) |
33d7655b JB |
439 | { |
440 | /* The value is too large to fit into the destination. | |
441 | Treat as infinity. */ | |
442 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
443 | fmt->exp_len, fmt->exp_nan); | |
444 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
445 | fmt->man_len, 0); | |
446 | goto finalize_byteorder; | |
447 | } | |
448 | ||
fcab3fb5 | 449 | put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, |
d16aafd8 AC |
450 | exponent + fmt->exp_bias - 1); |
451 | ||
452 | mant_bits_left = fmt->man_len; | |
453 | mant_off = fmt->man_start; | |
454 | while (mant_bits_left > 0) | |
455 | { | |
456 | unsigned long mant_long; | |
9a619af0 | 457 | |
d16aafd8 AC |
458 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; |
459 | ||
460 | mant *= 4294967296.0; | |
461 | mant_long = ((unsigned long) mant) & 0xffffffffL; | |
462 | mant -= mant_long; | |
463 | ||
464 | /* If the integer bit is implicit, then we need to discard it. | |
465 | If we are discarding a zero, we should be (but are not) creating | |
466 | a denormalized number which means adjusting the exponent | |
467 | (I think). */ | |
468 | if (mant_bits_left == fmt->man_len | |
469 | && fmt->intbit == floatformat_intbit_no) | |
470 | { | |
471 | mant_long <<= 1; | |
472 | mant_long &= 0xffffffffL; | |
06194148 JJ |
473 | /* If we are processing the top 32 mantissa bits of a doublest |
474 | so as to convert to a float value with implied integer bit, | |
475 | we will only be putting 31 of those 32 bits into the | |
476 | final value due to the discarding of the top bit. In the | |
477 | case of a small float value where the number of mantissa | |
478 | bits is less than 32, discarding the top bit does not alter | |
479 | the number of bits we will be adding to the result. */ | |
480 | if (mant_bits == 32) | |
481 | mant_bits -= 1; | |
d16aafd8 AC |
482 | } |
483 | ||
484 | if (mant_bits < 32) | |
485 | { | |
486 | /* The bits we want are in the most significant MANT_BITS bits of | |
487 | mant_long. Move them to the least significant. */ | |
488 | mant_long >>= 32 - mant_bits; | |
489 | } | |
490 | ||
fcab3fb5 | 491 | put_field (uto, order, fmt->totalsize, |
d16aafd8 AC |
492 | mant_off, mant_bits, mant_long); |
493 | mant_off += mant_bits; | |
494 | mant_bits_left -= mant_bits; | |
495 | } | |
fcab3fb5 RE |
496 | |
497 | finalize_byteorder: | |
498 | /* Do we need to byte-swap the words in the result? */ | |
499 | if (order != fmt->byteorder) | |
0a3e99f6 | 500 | floatformat_normalize_byteorder (fmt, newto, to); |
d16aafd8 AC |
501 | } |
502 | ||
503 | /* Check if VAL (which is assumed to be a floating point number whose | |
504 | format is described by FMT) is negative. */ | |
505 | ||
506 | int | |
108d6ead AC |
507 | floatformat_is_negative (const struct floatformat *fmt, |
508 | const bfd_byte *uval) | |
d16aafd8 | 509 | { |
fcab3fb5 RE |
510 | enum floatformat_byteorders order; |
511 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
512 | ||
069e84fd | 513 | gdb_assert (fmt != NULL); |
fcab3fb5 RE |
514 | gdb_assert (fmt->totalsize |
515 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
516 | ||
f5aee5ee AM |
517 | /* An IBM long double (a two element array of double) always takes the |
518 | sign of the first double. */ | |
519 | if (fmt->split_half) | |
520 | fmt = fmt->split_half; | |
521 | ||
fcab3fb5 RE |
522 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); |
523 | ||
524 | if (order != fmt->byteorder) | |
525 | uval = newfrom; | |
526 | ||
527 | return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1); | |
d16aafd8 AC |
528 | } |
529 | ||
530 | /* Check if VAL is "not a number" (NaN) for FMT. */ | |
531 | ||
20389057 DJ |
532 | enum float_kind |
533 | floatformat_classify (const struct floatformat *fmt, | |
534 | const bfd_byte *uval) | |
d16aafd8 | 535 | { |
d16aafd8 AC |
536 | long exponent; |
537 | unsigned long mant; | |
538 | unsigned int mant_bits, mant_off; | |
539 | int mant_bits_left; | |
fcab3fb5 RE |
540 | enum floatformat_byteorders order; |
541 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
20389057 | 542 | int mant_zero; |
fcab3fb5 | 543 | |
069e84fd | 544 | gdb_assert (fmt != NULL); |
fcab3fb5 RE |
545 | gdb_assert (fmt->totalsize |
546 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
547 | ||
f5aee5ee AM |
548 | /* An IBM long double (a two element array of double) can be classified |
549 | by looking at the first double. inf and nan are specified as | |
550 | ignoring the second double. zero and subnormal will always have | |
551 | the second double 0.0 if the long double is correctly rounded. */ | |
552 | if (fmt->split_half) | |
553 | fmt = fmt->split_half; | |
554 | ||
fcab3fb5 RE |
555 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); |
556 | ||
557 | if (order != fmt->byteorder) | |
558 | uval = newfrom; | |
069e84fd | 559 | |
fcab3fb5 RE |
560 | exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start, |
561 | fmt->exp_len); | |
d16aafd8 | 562 | |
d16aafd8 AC |
563 | mant_bits_left = fmt->man_len; |
564 | mant_off = fmt->man_start; | |
565 | ||
20389057 | 566 | mant_zero = 1; |
d16aafd8 AC |
567 | while (mant_bits_left > 0) |
568 | { | |
325fac50 | 569 | mant_bits = std::min (mant_bits_left, 32); |
d16aafd8 | 570 | |
fcab3fb5 | 571 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 AC |
572 | |
573 | /* If there is an explicit integer bit, mask it off. */ | |
574 | if (mant_off == fmt->man_start | |
575 | && fmt->intbit == floatformat_intbit_yes) | |
576 | mant &= ~(1 << (mant_bits - 1)); | |
577 | ||
578 | if (mant) | |
20389057 DJ |
579 | { |
580 | mant_zero = 0; | |
581 | break; | |
582 | } | |
d16aafd8 AC |
583 | |
584 | mant_off += mant_bits; | |
585 | mant_bits_left -= mant_bits; | |
586 | } | |
587 | ||
20389057 DJ |
588 | /* If exp_nan is not set, assume that inf, NaN, and subnormals are not |
589 | supported. */ | |
590 | if (! fmt->exp_nan) | |
591 | { | |
592 | if (mant_zero) | |
593 | return float_zero; | |
594 | else | |
595 | return float_normal; | |
596 | } | |
597 | ||
598 | if (exponent == 0 && !mant_zero) | |
599 | return float_subnormal; | |
600 | ||
601 | if (exponent == fmt->exp_nan) | |
602 | { | |
603 | if (mant_zero) | |
604 | return float_infinite; | |
605 | else | |
606 | return float_nan; | |
607 | } | |
608 | ||
609 | if (mant_zero) | |
610 | return float_zero; | |
611 | ||
612 | return float_normal; | |
d16aafd8 AC |
613 | } |
614 | ||
615 | /* Convert the mantissa of VAL (which is assumed to be a floating | |
616 | point number whose format is described by FMT) into a hexadecimal | |
617 | and store it in a static string. Return a pointer to that string. */ | |
618 | ||
108d6ead AC |
619 | const char * |
620 | floatformat_mantissa (const struct floatformat *fmt, | |
621 | const bfd_byte *val) | |
d16aafd8 AC |
622 | { |
623 | unsigned char *uval = (unsigned char *) val; | |
624 | unsigned long mant; | |
625 | unsigned int mant_bits, mant_off; | |
626 | int mant_bits_left; | |
627 | static char res[50]; | |
628 | char buf[9]; | |
27df76f3 | 629 | int len; |
fcab3fb5 RE |
630 | enum floatformat_byteorders order; |
631 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
632 | ||
633 | gdb_assert (fmt != NULL); | |
634 | gdb_assert (fmt->totalsize | |
635 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
636 | ||
f5aee5ee AM |
637 | /* For IBM long double (a two element array of double), return the |
638 | mantissa of the first double. The problem with returning the | |
639 | actual mantissa from both doubles is that there can be an | |
640 | arbitrary number of implied 0's or 1's between the mantissas | |
641 | of the first and second double. In any case, this function | |
642 | is only used for dumping out nans, and a nan is specified to | |
643 | ignore the value in the second double. */ | |
644 | if (fmt->split_half) | |
645 | fmt = fmt->split_half; | |
646 | ||
fcab3fb5 RE |
647 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); |
648 | ||
649 | if (order != fmt->byteorder) | |
650 | uval = newfrom; | |
651 | ||
652 | if (! fmt->exp_nan) | |
653 | return 0; | |
d16aafd8 AC |
654 | |
655 | /* Make sure we have enough room to store the mantissa. */ | |
656 | gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); | |
657 | ||
658 | mant_off = fmt->man_start; | |
659 | mant_bits_left = fmt->man_len; | |
660 | mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; | |
661 | ||
fcab3fb5 | 662 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 | 663 | |
27df76f3 | 664 | len = xsnprintf (res, sizeof res, "%lx", mant); |
d16aafd8 AC |
665 | |
666 | mant_off += mant_bits; | |
667 | mant_bits_left -= mant_bits; | |
27df76f3 | 668 | |
d16aafd8 AC |
669 | while (mant_bits_left > 0) |
670 | { | |
fcab3fb5 | 671 | mant = get_field (uval, order, fmt->totalsize, mant_off, 32); |
d16aafd8 | 672 | |
27df76f3 MK |
673 | xsnprintf (buf, sizeof buf, "%08lx", mant); |
674 | gdb_assert (len + strlen (buf) <= sizeof res); | |
d16aafd8 AC |
675 | strcat (res, buf); |
676 | ||
677 | mant_off += 32; | |
678 | mant_bits_left -= 32; | |
679 | } | |
680 | ||
681 | return res; | |
682 | } | |
683 | ||
d16aafd8 | 684 | \f |
c422e771 AC |
685 | /* Convert TO/FROM target to the hosts DOUBLEST floating-point format. |
686 | ||
687 | If the host and target formats agree, we just copy the raw data | |
688 | into the appropriate type of variable and return, letting the host | |
689 | increase precision as necessary. Otherwise, we call the conversion | |
d7a87b5e PA |
690 | routine and let it do the dirty work. Note that even if the target |
691 | and host floating-point formats match, the length of the types | |
692 | might still be different, so the conversion routines must make sure | |
693 | to not overrun any buffers. For example, on x86, long double is | |
694 | the 80-bit extended precision type on both 32-bit and 64-bit ABIs, | |
695 | but by default it is stored as 12 bytes on 32-bit, and 16 bytes on | |
696 | 64-bit, for alignment reasons. See comment in store_typed_floating | |
697 | for a discussion about zeroing out remaining bytes in the target | |
698 | buffer. */ | |
c422e771 | 699 | |
c35f4ffc AC |
700 | static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT; |
701 | static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT; | |
3e43a32a MS |
702 | static const struct floatformat *host_long_double_format |
703 | = GDB_HOST_LONG_DOUBLE_FORMAT; | |
c422e771 | 704 | |
b79497cb PA |
705 | /* See doublest.h. */ |
706 | ||
707 | size_t | |
708 | floatformat_totalsize_bytes (const struct floatformat *fmt) | |
709 | { | |
710 | return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) | |
711 | / FLOATFORMAT_CHAR_BIT); | |
712 | } | |
713 | ||
c422e771 AC |
714 | void |
715 | floatformat_to_doublest (const struct floatformat *fmt, | |
716 | const void *in, DOUBLEST *out) | |
717 | { | |
718 | gdb_assert (fmt != NULL); | |
d7a87b5e | 719 | |
c422e771 AC |
720 | if (fmt == host_float_format) |
721 | { | |
d7a87b5e | 722 | float val = 0; |
9a619af0 | 723 | |
d7a87b5e | 724 | memcpy (&val, in, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
725 | *out = val; |
726 | } | |
727 | else if (fmt == host_double_format) | |
728 | { | |
d7a87b5e | 729 | double val = 0; |
9a619af0 | 730 | |
d7a87b5e | 731 | memcpy (&val, in, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
732 | *out = val; |
733 | } | |
734 | else if (fmt == host_long_double_format) | |
735 | { | |
d7a87b5e | 736 | long double val = 0; |
9a619af0 | 737 | |
d7a87b5e | 738 | memcpy (&val, in, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
739 | *out = val; |
740 | } | |
741 | else | |
742 | convert_floatformat_to_doublest (fmt, in, out); | |
743 | } | |
744 | ||
745 | void | |
746 | floatformat_from_doublest (const struct floatformat *fmt, | |
747 | const DOUBLEST *in, void *out) | |
748 | { | |
749 | gdb_assert (fmt != NULL); | |
d7a87b5e | 750 | |
c422e771 AC |
751 | if (fmt == host_float_format) |
752 | { | |
753 | float val = *in; | |
9a619af0 | 754 | |
d7a87b5e | 755 | memcpy (out, &val, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
756 | } |
757 | else if (fmt == host_double_format) | |
758 | { | |
759 | double val = *in; | |
9a619af0 | 760 | |
d7a87b5e | 761 | memcpy (out, &val, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
762 | } |
763 | else if (fmt == host_long_double_format) | |
764 | { | |
765 | long double val = *in; | |
9a619af0 | 766 | |
d7a87b5e | 767 | memcpy (out, &val, floatformat_totalsize_bytes (fmt)); |
c422e771 AC |
768 | } |
769 | else | |
770 | convert_doublest_to_floatformat (fmt, in, out); | |
771 | } | |
d16aafd8 | 772 | |
c422e771 | 773 | \f |
9b790ce7 UW |
774 | /* Return the floating-point format for a floating-point variable of |
775 | type TYPE. */ | |
87ffba60 | 776 | |
c2f05ac9 AC |
777 | const struct floatformat * |
778 | floatformat_from_type (const struct type *type) | |
779 | { | |
50810684 | 780 | struct gdbarch *gdbarch = get_type_arch (type); |
b79497cb | 781 | const struct floatformat *fmt; |
9a619af0 | 782 | |
c2f05ac9 | 783 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT); |
9b790ce7 UW |
784 | gdb_assert (TYPE_FLOATFORMAT (type)); |
785 | fmt = TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)]; | |
b79497cb PA |
786 | gdb_assert (TYPE_LENGTH (type) >= floatformat_totalsize_bytes (fmt)); |
787 | return fmt; | |
c2f05ac9 AC |
788 | } |
789 | ||
87ffba60 MK |
790 | /* Extract a floating-point number of type TYPE from a target-order |
791 | byte-stream at ADDR. Returns the value as type DOUBLEST. */ | |
96d2f608 AC |
792 | |
793 | DOUBLEST | |
794 | extract_typed_floating (const void *addr, const struct type *type) | |
795 | { | |
e035e373 | 796 | const struct floatformat *fmt = floatformat_from_type (type); |
96d2f608 | 797 | DOUBLEST retval; |
87ffba60 | 798 | |
e035e373 | 799 | floatformat_to_doublest (fmt, addr, &retval); |
96d2f608 AC |
800 | return retval; |
801 | } | |
802 | ||
87ffba60 MK |
803 | /* Store VAL as a floating-point number of type TYPE to a target-order |
804 | byte-stream at ADDR. */ | |
805 | ||
96d2f608 AC |
806 | void |
807 | store_typed_floating (void *addr, const struct type *type, DOUBLEST val) | |
808 | { | |
e035e373 | 809 | const struct floatformat *fmt = floatformat_from_type (type); |
87ffba60 MK |
810 | |
811 | /* FIXME: kettenis/2001-10-28: It is debatable whether we should | |
812 | zero out any remaining bytes in the target buffer when TYPE is | |
813 | longer than the actual underlying floating-point format. Perhaps | |
814 | we should store a fixed bitpattern in those remaining bytes, | |
815 | instead of zero, or perhaps we shouldn't touch those remaining | |
816 | bytes at all. | |
817 | ||
818 | NOTE: cagney/2001-10-28: With the way things currently work, it | |
819 | isn't a good idea to leave the end bits undefined. This is | |
820 | because GDB writes out the entire sizeof(<floating>) bits of the | |
821 | floating-point type even though the value might only be stored | |
822 | in, and the target processor may only refer to, the first N < | |
823 | TYPE_LENGTH (type) bits. If the end of the buffer wasn't | |
824 | initialized, GDB would write undefined data to the target. An | |
825 | errant program, refering to that undefined data, would then | |
43686d64 MK |
826 | become non-deterministic. |
827 | ||
828 | See also the function convert_typed_floating below. */ | |
96d2f608 | 829 | memset (addr, 0, TYPE_LENGTH (type)); |
87ffba60 | 830 | |
e035e373 | 831 | floatformat_from_doublest (fmt, &val, addr); |
96d2f608 | 832 | } |
43686d64 MK |
833 | |
834 | /* Convert a floating-point number of type FROM_TYPE from a | |
835 | target-order byte-stream at FROM to a floating-point number of type | |
836 | TO_TYPE, and store it to a target-order byte-stream at TO. */ | |
837 | ||
838 | void | |
839 | convert_typed_floating (const void *from, const struct type *from_type, | |
840 | void *to, const struct type *to_type) | |
841 | { | |
c2f05ac9 AC |
842 | const struct floatformat *from_fmt = floatformat_from_type (from_type); |
843 | const struct floatformat *to_fmt = floatformat_from_type (to_type); | |
43686d64 | 844 | |
43686d64 MK |
845 | if (from_fmt == NULL || to_fmt == NULL) |
846 | { | |
847 | /* If we don't know the floating-point format of FROM_TYPE or | |
848 | TO_TYPE, there's not much we can do. We might make the | |
849 | assumption that if the length of FROM_TYPE and TO_TYPE match, | |
850 | their floating-point format would match too, but that | |
851 | assumption might be wrong on targets that support | |
852 | floating-point types that only differ in endianness for | |
853 | example. So we warn instead, and zero out the target buffer. */ | |
8a3fe4f8 | 854 | warning (_("Can't convert floating-point number to desired type.")); |
43686d64 MK |
855 | memset (to, 0, TYPE_LENGTH (to_type)); |
856 | } | |
857 | else if (from_fmt == to_fmt) | |
858 | { | |
859 | /* We're in business. The floating-point format of FROM_TYPE | |
860 | and TO_TYPE match. However, even though the floating-point | |
861 | format matches, the length of the type might still be | |
862 | different. Make sure we don't overrun any buffers. See | |
863 | comment in store_typed_floating for a discussion about | |
864 | zeroing out remaining bytes in the target buffer. */ | |
865 | memset (to, 0, TYPE_LENGTH (to_type)); | |
325fac50 PA |
866 | memcpy (to, from, std::min (TYPE_LENGTH (from_type), |
867 | TYPE_LENGTH (to_type))); | |
43686d64 MK |
868 | } |
869 | else | |
870 | { | |
871 | /* The floating-point types don't match. The best we can do | |
938f5214 | 872 | (apart from simulating the target FPU) is converting to the |
43686d64 MK |
873 | widest floating-point type supported by the host, and then |
874 | again to the desired type. */ | |
875 | DOUBLEST d; | |
876 | ||
877 | floatformat_to_doublest (from_fmt, from, &d); | |
878 | floatformat_from_doublest (to_fmt, &d, to); | |
879 | } | |
880 | } |