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 | |
20389057 DJ |
193 | floatformat_to_double (fmt, from, &dto); |
194 | *to = (DOUBLEST) dto; | |
195 | return; | |
196 | } | |
197 | ||
fcab3fb5 | 198 | order = floatformat_normalize_byteorder (fmt, ufrom, newfrom); |
d16aafd8 | 199 | |
fcab3fb5 RE |
200 | if (order != fmt->byteorder) |
201 | ufrom = newfrom; | |
d16aafd8 | 202 | |
b14d30e1 JM |
203 | if (fmt->split_half) |
204 | { | |
542a88d0 | 205 | DOUBLEST dtop, dbot; |
9a619af0 | 206 | |
542a88d0 | 207 | floatformat_to_doublest (fmt->split_half, ufrom, &dtop); |
b14d30e1 JM |
208 | /* Preserve the sign of 0, which is the sign of the top |
209 | half. */ | |
210 | if (dtop == 0.0) | |
211 | { | |
542a88d0 | 212 | *to = dtop; |
b14d30e1 JM |
213 | return; |
214 | } | |
542a88d0 | 215 | floatformat_to_doublest (fmt->split_half, |
b14d30e1 JM |
216 | ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, |
217 | &dbot); | |
542a88d0 | 218 | *to = dtop + dbot; |
b14d30e1 JM |
219 | return; |
220 | } | |
221 | ||
fcab3fb5 RE |
222 | exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start, |
223 | fmt->exp_len); | |
d16aafd8 AC |
224 | /* Note that if exponent indicates a NaN, we can't really do anything useful |
225 | (not knowing if the host has NaN's, or how to build one). So it will | |
226 | end up as an infinity or something close; that is OK. */ | |
227 | ||
228 | mant_bits_left = fmt->man_len; | |
229 | mant_off = fmt->man_start; | |
230 | dto = 0.0; | |
231 | ||
232 | special_exponent = exponent == 0 || exponent == fmt->exp_nan; | |
233 | ||
0963b4bd MS |
234 | /* Don't bias NaNs. Use minimum exponent for denorms. For |
235 | simplicity, we don't check for zero as the exponent doesn't matter. | |
236 | Note the cast to int; exp_bias is unsigned, so it's important to | |
237 | make sure the operation is done in signed arithmetic. */ | |
d16aafd8 AC |
238 | if (!special_exponent) |
239 | exponent -= fmt->exp_bias; | |
240 | else if (exponent == 0) | |
1c704f11 | 241 | exponent = 1 - fmt->exp_bias; |
d16aafd8 AC |
242 | |
243 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
0963b4bd | 244 | who cares. */ |
d16aafd8 AC |
245 | |
246 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
247 | increment the exponent by one to account for the integer bit. */ | |
248 | ||
249 | if (!special_exponent) | |
250 | { | |
251 | if (fmt->intbit == floatformat_intbit_no) | |
252 | dto = ldexp (1.0, exponent); | |
253 | else | |
254 | exponent++; | |
255 | } | |
256 | ||
257 | while (mant_bits_left > 0) | |
258 | { | |
259 | mant_bits = min (mant_bits_left, 32); | |
260 | ||
fcab3fb5 | 261 | mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 AC |
262 | |
263 | dto += ldexp ((double) mant, exponent - mant_bits); | |
264 | exponent -= mant_bits; | |
265 | mant_off += mant_bits; | |
266 | mant_bits_left -= mant_bits; | |
267 | } | |
268 | ||
269 | /* Negate it if negative. */ | |
fcab3fb5 | 270 | if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1)) |
d16aafd8 AC |
271 | dto = -dto; |
272 | *to = dto; | |
273 | } | |
274 | \f | |
d16aafd8 AC |
275 | /* Set a field which starts at START and is LEN bytes long. DATA and |
276 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
277 | static void | |
278 | put_field (unsigned char *data, enum floatformat_byteorders order, | |
279 | unsigned int total_len, unsigned int start, unsigned int len, | |
280 | unsigned long stuff_to_put) | |
281 | { | |
282 | unsigned int cur_byte; | |
283 | int cur_bitshift; | |
284 | ||
fcab3fb5 RE |
285 | /* Caller must byte-swap words before calling this routine. */ |
286 | gdb_assert (order == floatformat_little || order == floatformat_big); | |
287 | ||
d16aafd8 | 288 | /* Start at the least significant part of the field. */ |
fcab3fb5 | 289 | if (order == floatformat_little) |
d16aafd8 AC |
290 | { |
291 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
9a619af0 | 292 | |
d16aafd8 AC |
293 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) |
294 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
295 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
296 | - FLOATFORMAT_CHAR_BIT; | |
297 | } | |
298 | else | |
299 | { | |
300 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
301 | cur_bitshift = | |
302 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
303 | } | |
304 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
305 | { | |
306 | *(data + cur_byte) &= | |
307 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) | |
308 | << (-cur_bitshift)); | |
309 | *(data + cur_byte) |= | |
310 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
311 | } | |
312 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
fcab3fb5 | 313 | if (order == floatformat_little) |
d16aafd8 AC |
314 | ++cur_byte; |
315 | else | |
316 | --cur_byte; | |
317 | ||
318 | /* Move towards the most significant part of the field. */ | |
319 | while (cur_bitshift < len) | |
320 | { | |
321 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
322 | { | |
323 | /* This is the last byte. */ | |
324 | *(data + cur_byte) &= | |
325 | ~((1 << (len - cur_bitshift)) - 1); | |
326 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
327 | } | |
328 | else | |
329 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
330 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
331 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
fcab3fb5 | 332 | if (order == floatformat_little) |
d16aafd8 AC |
333 | ++cur_byte; |
334 | else | |
335 | --cur_byte; | |
336 | } | |
337 | } | |
338 | ||
339 | #ifdef HAVE_LONG_DOUBLE | |
340 | /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR. | |
341 | The range of the returned value is >= 0.5 and < 1.0. This is equivalent to | |
342 | frexp, but operates on the long double data type. */ | |
343 | ||
344 | static long double ldfrexp (long double value, int *eptr); | |
345 | ||
346 | static long double | |
347 | ldfrexp (long double value, int *eptr) | |
348 | { | |
349 | long double tmp; | |
350 | int exp; | |
351 | ||
3e43a32a MS |
352 | /* Unfortunately, there are no portable functions for extracting the |
353 | exponent of a long double, so we have to do it iteratively by | |
354 | multiplying or dividing by two until the fraction is between 0.5 | |
355 | and 1.0. */ | |
d16aafd8 AC |
356 | |
357 | if (value < 0.0l) | |
358 | value = -value; | |
359 | ||
360 | tmp = 1.0l; | |
361 | exp = 0; | |
362 | ||
363 | if (value >= tmp) /* Value >= 1.0 */ | |
364 | while (value >= tmp) | |
365 | { | |
366 | tmp *= 2.0l; | |
367 | exp++; | |
368 | } | |
369 | else if (value != 0.0l) /* Value < 1.0 and > 0.0 */ | |
370 | { | |
371 | while (value < tmp) | |
372 | { | |
373 | tmp /= 2.0l; | |
374 | exp--; | |
375 | } | |
376 | tmp *= 2.0l; | |
377 | exp++; | |
378 | } | |
379 | ||
380 | *eptr = exp; | |
381 | return value / tmp; | |
382 | } | |
383 | #endif /* HAVE_LONG_DOUBLE */ | |
384 | ||
385 | ||
0a3e99f6 MK |
386 | /* The converse: convert the DOUBLEST *FROM to an extended float and |
387 | store where TO points. Neither FROM nor TO have any alignment | |
d16aafd8 AC |
388 | restrictions. */ |
389 | ||
c422e771 AC |
390 | static void |
391 | convert_doublest_to_floatformat (CONST struct floatformat *fmt, | |
0a3e99f6 | 392 | const DOUBLEST *from, void *to) |
d16aafd8 AC |
393 | { |
394 | DOUBLEST dfrom; | |
395 | int exponent; | |
396 | DOUBLEST mant; | |
397 | unsigned int mant_bits, mant_off; | |
398 | int mant_bits_left; | |
399 | unsigned char *uto = (unsigned char *) to; | |
fcab3fb5 | 400 | enum floatformat_byteorders order = fmt->byteorder; |
0a3e99f6 | 401 | unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; |
fcab3fb5 | 402 | |
0a3e99f6 | 403 | if (order != floatformat_little) |
fcab3fb5 | 404 | order = floatformat_big; |
d16aafd8 | 405 | |
0a3e99f6 MK |
406 | if (order != fmt->byteorder) |
407 | uto = newto; | |
408 | ||
d16aafd8 AC |
409 | memcpy (&dfrom, from, sizeof (dfrom)); |
410 | memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) | |
411 | / FLOATFORMAT_CHAR_BIT); | |
b14d30e1 JM |
412 | |
413 | if (fmt->split_half) | |
414 | { | |
415 | /* Use static volatile to ensure that any excess precision is | |
416 | removed via storing in memory, and so the top half really is | |
417 | the result of converting to double. */ | |
418 | static volatile double dtop, dbot; | |
542a88d0 | 419 | DOUBLEST dtopnv, dbotnv; |
9a619af0 | 420 | |
b14d30e1 JM |
421 | dtop = (double) dfrom; |
422 | /* If the rounded top half is Inf, the bottom must be 0 not NaN | |
423 | or Inf. */ | |
424 | if (dtop + dtop == dtop && dtop != 0.0) | |
425 | dbot = 0.0; | |
426 | else | |
427 | dbot = (double) (dfrom - (DOUBLEST) dtop); | |
428 | dtopnv = dtop; | |
429 | dbotnv = dbot; | |
542a88d0 LM |
430 | floatformat_from_doublest (fmt->split_half, &dtopnv, uto); |
431 | floatformat_from_doublest (fmt->split_half, &dbotnv, | |
b14d30e1 JM |
432 | (uto |
433 | + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2)); | |
434 | return; | |
435 | } | |
436 | ||
d16aafd8 AC |
437 | if (dfrom == 0) |
438 | return; /* Result is zero */ | |
439 | if (dfrom != dfrom) /* Result is NaN */ | |
440 | { | |
441 | /* From is NaN */ | |
fcab3fb5 | 442 | put_field (uto, order, fmt->totalsize, fmt->exp_start, |
d16aafd8 | 443 | fmt->exp_len, fmt->exp_nan); |
0963b4bd | 444 | /* Be sure it's not infinity, but NaN value is irrel. */ |
fcab3fb5 | 445 | put_field (uto, order, fmt->totalsize, fmt->man_start, |
fbe12357 | 446 | fmt->man_len, 1); |
fcab3fb5 | 447 | goto finalize_byteorder; |
d16aafd8 AC |
448 | } |
449 | ||
450 | /* If negative, set the sign bit. */ | |
451 | if (dfrom < 0) | |
452 | { | |
fcab3fb5 | 453 | put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1); |
d16aafd8 AC |
454 | dfrom = -dfrom; |
455 | } | |
456 | ||
0963b4bd | 457 | if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */ |
d16aafd8 AC |
458 | { |
459 | /* Infinity exponent is same as NaN's. */ | |
fcab3fb5 | 460 | put_field (uto, order, fmt->totalsize, fmt->exp_start, |
d16aafd8 AC |
461 | fmt->exp_len, fmt->exp_nan); |
462 | /* Infinity mantissa is all zeroes. */ | |
fcab3fb5 | 463 | put_field (uto, order, fmt->totalsize, fmt->man_start, |
d16aafd8 | 464 | fmt->man_len, 0); |
fcab3fb5 | 465 | goto finalize_byteorder; |
d16aafd8 AC |
466 | } |
467 | ||
468 | #ifdef HAVE_LONG_DOUBLE | |
469 | mant = ldfrexp (dfrom, &exponent); | |
470 | #else | |
471 | mant = frexp (dfrom, &exponent); | |
472 | #endif | |
473 | ||
33d7655b JB |
474 | if (exponent + fmt->exp_bias <= 0) |
475 | { | |
476 | /* The value is too small to be expressed in the destination | |
477 | type (not enough bits in the exponent. Treat as 0. */ | |
478 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
479 | fmt->exp_len, 0); | |
480 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
481 | fmt->man_len, 0); | |
482 | goto finalize_byteorder; | |
483 | } | |
484 | ||
32560274 | 485 | if (exponent + fmt->exp_bias >= (1 << fmt->exp_len)) |
33d7655b JB |
486 | { |
487 | /* The value is too large to fit into the destination. | |
488 | Treat as infinity. */ | |
489 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
490 | fmt->exp_len, fmt->exp_nan); | |
491 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
492 | fmt->man_len, 0); | |
493 | goto finalize_byteorder; | |
494 | } | |
495 | ||
fcab3fb5 | 496 | put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, |
d16aafd8 AC |
497 | exponent + fmt->exp_bias - 1); |
498 | ||
499 | mant_bits_left = fmt->man_len; | |
500 | mant_off = fmt->man_start; | |
501 | while (mant_bits_left > 0) | |
502 | { | |
503 | unsigned long mant_long; | |
9a619af0 | 504 | |
d16aafd8 AC |
505 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; |
506 | ||
507 | mant *= 4294967296.0; | |
508 | mant_long = ((unsigned long) mant) & 0xffffffffL; | |
509 | mant -= mant_long; | |
510 | ||
511 | /* If the integer bit is implicit, then we need to discard it. | |
512 | If we are discarding a zero, we should be (but are not) creating | |
513 | a denormalized number which means adjusting the exponent | |
514 | (I think). */ | |
515 | if (mant_bits_left == fmt->man_len | |
516 | && fmt->intbit == floatformat_intbit_no) | |
517 | { | |
518 | mant_long <<= 1; | |
519 | mant_long &= 0xffffffffL; | |
06194148 JJ |
520 | /* If we are processing the top 32 mantissa bits of a doublest |
521 | so as to convert to a float value with implied integer bit, | |
522 | we will only be putting 31 of those 32 bits into the | |
523 | final value due to the discarding of the top bit. In the | |
524 | case of a small float value where the number of mantissa | |
525 | bits is less than 32, discarding the top bit does not alter | |
526 | the number of bits we will be adding to the result. */ | |
527 | if (mant_bits == 32) | |
528 | mant_bits -= 1; | |
d16aafd8 AC |
529 | } |
530 | ||
531 | if (mant_bits < 32) | |
532 | { | |
533 | /* The bits we want are in the most significant MANT_BITS bits of | |
534 | mant_long. Move them to the least significant. */ | |
535 | mant_long >>= 32 - mant_bits; | |
536 | } | |
537 | ||
fcab3fb5 | 538 | put_field (uto, order, fmt->totalsize, |
d16aafd8 AC |
539 | mant_off, mant_bits, mant_long); |
540 | mant_off += mant_bits; | |
541 | mant_bits_left -= mant_bits; | |
542 | } | |
fcab3fb5 RE |
543 | |
544 | finalize_byteorder: | |
545 | /* Do we need to byte-swap the words in the result? */ | |
546 | if (order != fmt->byteorder) | |
0a3e99f6 | 547 | floatformat_normalize_byteorder (fmt, newto, to); |
d16aafd8 AC |
548 | } |
549 | ||
550 | /* Check if VAL (which is assumed to be a floating point number whose | |
551 | format is described by FMT) is negative. */ | |
552 | ||
553 | int | |
108d6ead AC |
554 | floatformat_is_negative (const struct floatformat *fmt, |
555 | const bfd_byte *uval) | |
d16aafd8 | 556 | { |
fcab3fb5 RE |
557 | enum floatformat_byteorders order; |
558 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
559 | ||
069e84fd | 560 | gdb_assert (fmt != NULL); |
fcab3fb5 RE |
561 | gdb_assert (fmt->totalsize |
562 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
563 | ||
564 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
565 | ||
566 | if (order != fmt->byteorder) | |
567 | uval = newfrom; | |
568 | ||
569 | return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1); | |
d16aafd8 AC |
570 | } |
571 | ||
572 | /* Check if VAL is "not a number" (NaN) for FMT. */ | |
573 | ||
20389057 DJ |
574 | enum float_kind |
575 | floatformat_classify (const struct floatformat *fmt, | |
576 | const bfd_byte *uval) | |
d16aafd8 | 577 | { |
d16aafd8 AC |
578 | long exponent; |
579 | unsigned long mant; | |
580 | unsigned int mant_bits, mant_off; | |
581 | int mant_bits_left; | |
fcab3fb5 RE |
582 | enum floatformat_byteorders order; |
583 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
20389057 | 584 | int mant_zero; |
fcab3fb5 | 585 | |
069e84fd | 586 | gdb_assert (fmt != NULL); |
fcab3fb5 RE |
587 | gdb_assert (fmt->totalsize |
588 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
589 | ||
590 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
591 | ||
592 | if (order != fmt->byteorder) | |
593 | uval = newfrom; | |
069e84fd | 594 | |
fcab3fb5 RE |
595 | exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start, |
596 | fmt->exp_len); | |
d16aafd8 | 597 | |
d16aafd8 AC |
598 | mant_bits_left = fmt->man_len; |
599 | mant_off = fmt->man_start; | |
600 | ||
20389057 | 601 | mant_zero = 1; |
d16aafd8 AC |
602 | while (mant_bits_left > 0) |
603 | { | |
604 | mant_bits = min (mant_bits_left, 32); | |
605 | ||
fcab3fb5 | 606 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 AC |
607 | |
608 | /* If there is an explicit integer bit, mask it off. */ | |
609 | if (mant_off == fmt->man_start | |
610 | && fmt->intbit == floatformat_intbit_yes) | |
611 | mant &= ~(1 << (mant_bits - 1)); | |
612 | ||
613 | if (mant) | |
20389057 DJ |
614 | { |
615 | mant_zero = 0; | |
616 | break; | |
617 | } | |
d16aafd8 AC |
618 | |
619 | mant_off += mant_bits; | |
620 | mant_bits_left -= mant_bits; | |
621 | } | |
622 | ||
20389057 DJ |
623 | /* If exp_nan is not set, assume that inf, NaN, and subnormals are not |
624 | supported. */ | |
625 | if (! fmt->exp_nan) | |
626 | { | |
627 | if (mant_zero) | |
628 | return float_zero; | |
629 | else | |
630 | return float_normal; | |
631 | } | |
632 | ||
633 | if (exponent == 0 && !mant_zero) | |
634 | return float_subnormal; | |
635 | ||
636 | if (exponent == fmt->exp_nan) | |
637 | { | |
638 | if (mant_zero) | |
639 | return float_infinite; | |
640 | else | |
641 | return float_nan; | |
642 | } | |
643 | ||
644 | if (mant_zero) | |
645 | return float_zero; | |
646 | ||
647 | return float_normal; | |
d16aafd8 AC |
648 | } |
649 | ||
650 | /* Convert the mantissa of VAL (which is assumed to be a floating | |
651 | point number whose format is described by FMT) into a hexadecimal | |
652 | and store it in a static string. Return a pointer to that string. */ | |
653 | ||
108d6ead AC |
654 | const char * |
655 | floatformat_mantissa (const struct floatformat *fmt, | |
656 | const bfd_byte *val) | |
d16aafd8 AC |
657 | { |
658 | unsigned char *uval = (unsigned char *) val; | |
659 | unsigned long mant; | |
660 | unsigned int mant_bits, mant_off; | |
661 | int mant_bits_left; | |
662 | static char res[50]; | |
663 | char buf[9]; | |
27df76f3 | 664 | int len; |
fcab3fb5 RE |
665 | enum floatformat_byteorders order; |
666 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
667 | ||
668 | gdb_assert (fmt != NULL); | |
669 | gdb_assert (fmt->totalsize | |
670 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
671 | ||
672 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
673 | ||
674 | if (order != fmt->byteorder) | |
675 | uval = newfrom; | |
676 | ||
677 | if (! fmt->exp_nan) | |
678 | return 0; | |
d16aafd8 AC |
679 | |
680 | /* Make sure we have enough room to store the mantissa. */ | |
681 | gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); | |
682 | ||
683 | mant_off = fmt->man_start; | |
684 | mant_bits_left = fmt->man_len; | |
685 | mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; | |
686 | ||
fcab3fb5 | 687 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); |
d16aafd8 | 688 | |
27df76f3 | 689 | len = xsnprintf (res, sizeof res, "%lx", mant); |
d16aafd8 AC |
690 | |
691 | mant_off += mant_bits; | |
692 | mant_bits_left -= mant_bits; | |
27df76f3 | 693 | |
d16aafd8 AC |
694 | while (mant_bits_left > 0) |
695 | { | |
fcab3fb5 | 696 | mant = get_field (uval, order, fmt->totalsize, mant_off, 32); |
d16aafd8 | 697 | |
27df76f3 MK |
698 | xsnprintf (buf, sizeof buf, "%08lx", mant); |
699 | gdb_assert (len + strlen (buf) <= sizeof res); | |
d16aafd8 AC |
700 | strcat (res, buf); |
701 | ||
702 | mant_off += 32; | |
703 | mant_bits_left -= 32; | |
704 | } | |
705 | ||
706 | return res; | |
707 | } | |
708 | ||
d16aafd8 | 709 | \f |
c422e771 AC |
710 | /* Convert TO/FROM target to the hosts DOUBLEST floating-point format. |
711 | ||
712 | If the host and target formats agree, we just copy the raw data | |
713 | into the appropriate type of variable and return, letting the host | |
714 | increase precision as necessary. Otherwise, we call the conversion | |
715 | routine and let it do the dirty work. */ | |
716 | ||
c35f4ffc AC |
717 | static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT; |
718 | static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT; | |
3e43a32a MS |
719 | static const struct floatformat *host_long_double_format |
720 | = GDB_HOST_LONG_DOUBLE_FORMAT; | |
c422e771 AC |
721 | |
722 | void | |
723 | floatformat_to_doublest (const struct floatformat *fmt, | |
724 | const void *in, DOUBLEST *out) | |
725 | { | |
726 | gdb_assert (fmt != NULL); | |
727 | if (fmt == host_float_format) | |
728 | { | |
729 | float val; | |
9a619af0 | 730 | |
c422e771 AC |
731 | memcpy (&val, in, sizeof (val)); |
732 | *out = val; | |
733 | } | |
734 | else if (fmt == host_double_format) | |
735 | { | |
736 | double val; | |
9a619af0 | 737 | |
c422e771 AC |
738 | memcpy (&val, in, sizeof (val)); |
739 | *out = val; | |
740 | } | |
741 | else if (fmt == host_long_double_format) | |
742 | { | |
743 | long double val; | |
9a619af0 | 744 | |
c422e771 AC |
745 | memcpy (&val, in, sizeof (val)); |
746 | *out = val; | |
747 | } | |
748 | else | |
749 | convert_floatformat_to_doublest (fmt, in, out); | |
750 | } | |
751 | ||
752 | void | |
753 | floatformat_from_doublest (const struct floatformat *fmt, | |
754 | const DOUBLEST *in, void *out) | |
755 | { | |
756 | gdb_assert (fmt != NULL); | |
757 | if (fmt == host_float_format) | |
758 | { | |
759 | float val = *in; | |
9a619af0 | 760 | |
c422e771 AC |
761 | memcpy (out, &val, sizeof (val)); |
762 | } | |
763 | else if (fmt == host_double_format) | |
764 | { | |
765 | double val = *in; | |
9a619af0 | 766 | |
c422e771 AC |
767 | memcpy (out, &val, sizeof (val)); |
768 | } | |
769 | else if (fmt == host_long_double_format) | |
770 | { | |
771 | long double val = *in; | |
9a619af0 | 772 | |
c422e771 AC |
773 | memcpy (out, &val, sizeof (val)); |
774 | } | |
775 | else | |
776 | convert_doublest_to_floatformat (fmt, in, out); | |
777 | } | |
d16aafd8 | 778 | |
c422e771 | 779 | \f |
87ffba60 | 780 | /* Return a floating-point format for a floating-point variable of |
47b3f456 AC |
781 | length LEN. If no suitable floating-point format is found, an |
782 | error is thrown. | |
d16aafd8 | 783 | |
87ffba60 MK |
784 | We need this functionality since information about the |
785 | floating-point format of a type is not always available to GDB; the | |
786 | debug information typically only tells us the size of a | |
787 | floating-point type. | |
788 | ||
789 | FIXME: kettenis/2001-10-28: In many places, particularly in | |
790 | target-dependent code, the format of floating-point types is known, | |
791 | but not passed on by GDB. This should be fixed. */ | |
792 | ||
b9362cc7 | 793 | static const struct floatformat * |
50810684 | 794 | floatformat_from_length (struct gdbarch *gdbarch, int len) |
d16aafd8 | 795 | { |
47b3f456 | 796 | const struct floatformat *format; |
9a619af0 | 797 | |
f9e9243a UW |
798 | if (len * TARGET_CHAR_BIT == gdbarch_half_bit (gdbarch)) |
799 | format = gdbarch_half_format (gdbarch) | |
800 | [gdbarch_byte_order (gdbarch)]; | |
801 | else if (len * TARGET_CHAR_BIT == gdbarch_float_bit (gdbarch)) | |
50810684 UW |
802 | format = gdbarch_float_format (gdbarch) |
803 | [gdbarch_byte_order (gdbarch)]; | |
804 | else if (len * TARGET_CHAR_BIT == gdbarch_double_bit (gdbarch)) | |
805 | format = gdbarch_double_format (gdbarch) | |
806 | [gdbarch_byte_order (gdbarch)]; | |
807 | else if (len * TARGET_CHAR_BIT == gdbarch_long_double_bit (gdbarch)) | |
808 | format = gdbarch_long_double_format (gdbarch) | |
809 | [gdbarch_byte_order (gdbarch)]; | |
ddbfdd06 PM |
810 | /* On i386 the 'long double' type takes 96 bits, |
811 | while the real number of used bits is only 80, | |
0963b4bd | 812 | both in processor and in memory. |
ddbfdd06 | 813 | The code below accepts the real bit size. */ |
50810684 | 814 | else if ((gdbarch_long_double_format (gdbarch) != NULL) |
905e0470 PM |
815 | && (len * TARGET_CHAR_BIT |
816 | == gdbarch_long_double_format (gdbarch)[0]->totalsize)) | |
50810684 UW |
817 | format = gdbarch_long_double_format (gdbarch) |
818 | [gdbarch_byte_order (gdbarch)]; | |
47b3f456 AC |
819 | else |
820 | format = NULL; | |
821 | if (format == NULL) | |
8a3fe4f8 | 822 | error (_("Unrecognized %d-bit floating-point type."), |
9b0dea39 | 823 | len * TARGET_CHAR_BIT); |
47b3f456 | 824 | return format; |
87ffba60 MK |
825 | } |
826 | ||
c2f05ac9 AC |
827 | const struct floatformat * |
828 | floatformat_from_type (const struct type *type) | |
829 | { | |
50810684 | 830 | struct gdbarch *gdbarch = get_type_arch (type); |
9a619af0 | 831 | |
c2f05ac9 AC |
832 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT); |
833 | if (TYPE_FLOATFORMAT (type) != NULL) | |
50810684 | 834 | return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)]; |
c2f05ac9 | 835 | else |
50810684 | 836 | return floatformat_from_length (gdbarch, TYPE_LENGTH (type)); |
c2f05ac9 AC |
837 | } |
838 | ||
87ffba60 MK |
839 | /* Extract a floating-point number of type TYPE from a target-order |
840 | byte-stream at ADDR. Returns the value as type DOUBLEST. */ | |
96d2f608 AC |
841 | |
842 | DOUBLEST | |
843 | extract_typed_floating (const void *addr, const struct type *type) | |
844 | { | |
e035e373 | 845 | const struct floatformat *fmt = floatformat_from_type (type); |
96d2f608 | 846 | DOUBLEST retval; |
87ffba60 | 847 | |
e035e373 | 848 | floatformat_to_doublest (fmt, addr, &retval); |
96d2f608 AC |
849 | return retval; |
850 | } | |
851 | ||
87ffba60 MK |
852 | /* Store VAL as a floating-point number of type TYPE to a target-order |
853 | byte-stream at ADDR. */ | |
854 | ||
96d2f608 AC |
855 | void |
856 | store_typed_floating (void *addr, const struct type *type, DOUBLEST val) | |
857 | { | |
e035e373 | 858 | const struct floatformat *fmt = floatformat_from_type (type); |
87ffba60 MK |
859 | |
860 | /* FIXME: kettenis/2001-10-28: It is debatable whether we should | |
861 | zero out any remaining bytes in the target buffer when TYPE is | |
862 | longer than the actual underlying floating-point format. Perhaps | |
863 | we should store a fixed bitpattern in those remaining bytes, | |
864 | instead of zero, or perhaps we shouldn't touch those remaining | |
865 | bytes at all. | |
866 | ||
867 | NOTE: cagney/2001-10-28: With the way things currently work, it | |
868 | isn't a good idea to leave the end bits undefined. This is | |
869 | because GDB writes out the entire sizeof(<floating>) bits of the | |
870 | floating-point type even though the value might only be stored | |
871 | in, and the target processor may only refer to, the first N < | |
872 | TYPE_LENGTH (type) bits. If the end of the buffer wasn't | |
873 | initialized, GDB would write undefined data to the target. An | |
874 | errant program, refering to that undefined data, would then | |
43686d64 MK |
875 | become non-deterministic. |
876 | ||
877 | See also the function convert_typed_floating below. */ | |
96d2f608 | 878 | memset (addr, 0, TYPE_LENGTH (type)); |
87ffba60 | 879 | |
e035e373 | 880 | floatformat_from_doublest (fmt, &val, addr); |
96d2f608 | 881 | } |
43686d64 MK |
882 | |
883 | /* Convert a floating-point number of type FROM_TYPE from a | |
884 | target-order byte-stream at FROM to a floating-point number of type | |
885 | TO_TYPE, and store it to a target-order byte-stream at TO. */ | |
886 | ||
887 | void | |
888 | convert_typed_floating (const void *from, const struct type *from_type, | |
889 | void *to, const struct type *to_type) | |
890 | { | |
c2f05ac9 AC |
891 | const struct floatformat *from_fmt = floatformat_from_type (from_type); |
892 | const struct floatformat *to_fmt = floatformat_from_type (to_type); | |
43686d64 | 893 | |
43686d64 MK |
894 | if (from_fmt == NULL || to_fmt == NULL) |
895 | { | |
896 | /* If we don't know the floating-point format of FROM_TYPE or | |
897 | TO_TYPE, there's not much we can do. We might make the | |
898 | assumption that if the length of FROM_TYPE and TO_TYPE match, | |
899 | their floating-point format would match too, but that | |
900 | assumption might be wrong on targets that support | |
901 | floating-point types that only differ in endianness for | |
902 | example. So we warn instead, and zero out the target buffer. */ | |
8a3fe4f8 | 903 | warning (_("Can't convert floating-point number to desired type.")); |
43686d64 MK |
904 | memset (to, 0, TYPE_LENGTH (to_type)); |
905 | } | |
906 | else if (from_fmt == to_fmt) | |
907 | { | |
908 | /* We're in business. The floating-point format of FROM_TYPE | |
909 | and TO_TYPE match. However, even though the floating-point | |
910 | format matches, the length of the type might still be | |
911 | different. Make sure we don't overrun any buffers. See | |
912 | comment in store_typed_floating for a discussion about | |
913 | zeroing out remaining bytes in the target buffer. */ | |
914 | memset (to, 0, TYPE_LENGTH (to_type)); | |
915 | memcpy (to, from, min (TYPE_LENGTH (from_type), TYPE_LENGTH (to_type))); | |
916 | } | |
917 | else | |
918 | { | |
919 | /* The floating-point types don't match. The best we can do | |
938f5214 | 920 | (apart from simulating the target FPU) is converting to the |
43686d64 MK |
921 | widest floating-point type supported by the host, and then |
922 | again to the desired type. */ | |
923 | DOUBLEST d; | |
924 | ||
925 | floatformat_to_doublest (from_fmt, from, &d); | |
926 | floatformat_from_doublest (to_fmt, &d, to); | |
927 | } | |
928 | } | |
5ef2d0aa AC |
929 | |
930 | const struct floatformat *floatformat_ieee_single[BFD_ENDIAN_UNKNOWN]; | |
931 | const struct floatformat *floatformat_ieee_double[BFD_ENDIAN_UNKNOWN]; | |
49c54768 | 932 | const struct floatformat *floatformat_ieee_quad[BFD_ENDIAN_UNKNOWN]; |
5ef2d0aa AC |
933 | const struct floatformat *floatformat_arm_ext[BFD_ENDIAN_UNKNOWN]; |
934 | const struct floatformat *floatformat_ia64_spill[BFD_ENDIAN_UNKNOWN]; | |
5ef2d0aa AC |
935 | |
936 | extern void _initialize_doublest (void); | |
937 | ||
938 | extern void | |
939 | _initialize_doublest (void) | |
940 | { | |
941 | floatformat_ieee_single[BFD_ENDIAN_LITTLE] = &floatformat_ieee_single_little; | |
942 | floatformat_ieee_single[BFD_ENDIAN_BIG] = &floatformat_ieee_single_big; | |
943 | floatformat_ieee_double[BFD_ENDIAN_LITTLE] = &floatformat_ieee_double_little; | |
944 | floatformat_ieee_double[BFD_ENDIAN_BIG] = &floatformat_ieee_double_big; | |
3e43a32a MS |
945 | floatformat_arm_ext[BFD_ENDIAN_LITTLE] |
946 | = &floatformat_arm_ext_littlebyte_bigword; | |
5ef2d0aa AC |
947 | floatformat_arm_ext[BFD_ENDIAN_BIG] = &floatformat_arm_ext_big; |
948 | floatformat_ia64_spill[BFD_ENDIAN_LITTLE] = &floatformat_ia64_spill_little; | |
949 | floatformat_ia64_spill[BFD_ENDIAN_BIG] = &floatformat_ia64_spill_big; | |
49c54768 AC |
950 | floatformat_ieee_quad[BFD_ENDIAN_LITTLE] = &floatformat_ia64_quad_little; |
951 | floatformat_ieee_quad[BFD_ENDIAN_BIG] = &floatformat_ia64_quad_big; | |
5ef2d0aa | 952 | } |