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