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