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