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