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