Target FP: Remove unused floating-point routines
[deliverable/binutils-gdb.git] / gdb / doublest.c
CommitLineData
d16aafd8 1/* Floating point routines for GDB, the GNU debugger.
f1908289 2
61baf725 3 Copyright (C) 1986-2017 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"
d16aafd8 29#include <math.h> /* ldexp */
325fac50 30#include <algorithm>
d16aafd8
AC
31
32/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
33 going to bother with trying to muck around with whether it is defined in
34 a system header, what we do if not, etc. */
35#define FLOATFORMAT_CHAR_BIT 8
36
fcab3fb5
RE
37/* The number of bytes that the largest floating-point type that we
38 can convert to doublest will need. */
39#define FLOATFORMAT_LARGEST_BYTES 16
40
d16aafd8
AC
41/* Extract a field which starts at START and is LEN bytes long. DATA and
42 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
43static unsigned long
108d6ead 44get_field (const bfd_byte *data, enum floatformat_byteorders order,
d16aafd8
AC
45 unsigned int total_len, unsigned int start, unsigned int len)
46{
47 unsigned long result;
48 unsigned int cur_byte;
49 int cur_bitshift;
50
fcab3fb5
RE
51 /* Caller must byte-swap words before calling this routine. */
52 gdb_assert (order == floatformat_little || order == floatformat_big);
53
d16aafd8 54 /* Start at the least significant part of the field. */
fcab3fb5 55 if (order == floatformat_little)
d16aafd8
AC
56 {
57 /* We start counting from the other end (i.e, from the high bytes
58 rather than the low bytes). As such, we need to be concerned
0963b4bd 59 with what happens if bit 0 doesn't start on a byte boundary.
d16aafd8
AC
60 I.e, we need to properly handle the case where total_len is
61 not evenly divisible by 8. So we compute ``excess'' which
62 represents the number of bits from the end of our starting
0963b4bd 63 byte needed to get to bit 0. */
d16aafd8 64 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
9a619af0 65
d16aafd8
AC
66 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
67 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
68 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
69 - FLOATFORMAT_CHAR_BIT;
70 }
71 else
72 {
73 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
74 cur_bitshift =
75 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
76 }
77 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
78 result = *(data + cur_byte) >> (-cur_bitshift);
79 else
80 result = 0;
81 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 82 if (order == floatformat_little)
d16aafd8
AC
83 ++cur_byte;
84 else
85 --cur_byte;
86
87 /* Move towards the most significant part of the field. */
88 while (cur_bitshift < len)
89 {
90 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
91 cur_bitshift += FLOATFORMAT_CHAR_BIT;
c35f4ffc
AC
92 switch (order)
93 {
94 case floatformat_little:
95 ++cur_byte;
96 break;
97 case floatformat_big:
98 --cur_byte;
99 break;
c35f4ffc 100 }
d16aafd8
AC
101 }
102 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
0963b4bd 103 /* Mask out bits which are not part of the field. */
d16aafd8
AC
104 result &= ((1UL << len) - 1);
105 return result;
106}
107
0a3e99f6
MK
108/* Normalize the byte order of FROM into TO. If no normalization is
109 needed then FMT->byteorder is returned and TO is not changed;
110 otherwise the format of the normalized form in TO is returned. */
111
fcab3fb5
RE
112static enum floatformat_byteorders
113floatformat_normalize_byteorder (const struct floatformat *fmt,
114 const void *from, void *to)
115{
116 const unsigned char *swapin;
117 unsigned char *swapout;
118 int words;
119
120 if (fmt->byteorder == floatformat_little
121 || fmt->byteorder == floatformat_big)
122 return fmt->byteorder;
123
fcab3fb5
RE
124 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
125 words >>= 2;
126
127 swapout = (unsigned char *)to;
128 swapin = (const unsigned char *)from;
129
0a3e99f6
MK
130 if (fmt->byteorder == floatformat_vax)
131 {
132 while (words-- > 0)
133 {
134 *swapout++ = swapin[1];
135 *swapout++ = swapin[0];
136 *swapout++ = swapin[3];
137 *swapout++ = swapin[2];
138 swapin += 4;
139 }
140 /* This may look weird, since VAX is little-endian, but it is
141 easier to translate to big-endian than to little-endian. */
142 return floatformat_big;
143 }
144 else
fcab3fb5 145 {
0a3e99f6
MK
146 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
147
148 while (words-- > 0)
149 {
150 *swapout++ = swapin[3];
151 *swapout++ = swapin[2];
152 *swapout++ = swapin[1];
153 *swapout++ = swapin[0];
154 swapin += 4;
155 }
156 return floatformat_big;
fcab3fb5 157 }
fcab3fb5
RE
158}
159
d16aafd8
AC
160/* Convert from FMT to a DOUBLEST.
161 FROM is the address of the extended float.
162 Store the DOUBLEST in *TO. */
163
c422e771
AC
164static void
165convert_floatformat_to_doublest (const struct floatformat *fmt,
166 const void *from,
167 DOUBLEST *to)
d16aafd8
AC
168{
169 unsigned char *ufrom = (unsigned char *) from;
170 DOUBLEST dto;
171 long exponent;
172 unsigned long mant;
173 unsigned int mant_bits, mant_off;
174 int mant_bits_left;
0963b4bd 175 int special_exponent; /* It's a NaN, denorm or zero. */
fcab3fb5
RE
176 enum floatformat_byteorders order;
177 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
20389057 178 enum float_kind kind;
fcab3fb5
RE
179
180 gdb_assert (fmt->totalsize
181 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
d16aafd8 182
20389057
DJ
183 /* For non-numbers, reuse libiberty's logic to find the correct
184 format. We do not lose any precision in this case by passing
185 through a double. */
9a3c8263 186 kind = floatformat_classify (fmt, (const bfd_byte *) from);
20389057
DJ
187 if (kind == float_infinite || kind == float_nan)
188 {
189 double dto;
9a619af0 190
f5aee5ee
AM
191 floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
192 from, &dto);
20389057
DJ
193 *to = (DOUBLEST) dto;
194 return;
195 }
196
fcab3fb5 197 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
d16aafd8 198
fcab3fb5
RE
199 if (order != fmt->byteorder)
200 ufrom = newfrom;
d16aafd8 201
b14d30e1
JM
202 if (fmt->split_half)
203 {
542a88d0 204 DOUBLEST dtop, dbot;
9a619af0 205
542a88d0 206 floatformat_to_doublest (fmt->split_half, ufrom, &dtop);
b14d30e1
JM
207 /* Preserve the sign of 0, which is the sign of the top
208 half. */
209 if (dtop == 0.0)
210 {
542a88d0 211 *to = dtop;
b14d30e1
JM
212 return;
213 }
542a88d0 214 floatformat_to_doublest (fmt->split_half,
b14d30e1
JM
215 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2,
216 &dbot);
542a88d0 217 *to = dtop + dbot;
b14d30e1
JM
218 return;
219 }
220
fcab3fb5
RE
221 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
222 fmt->exp_len);
d16aafd8
AC
223 /* Note that if exponent indicates a NaN, we can't really do anything useful
224 (not knowing if the host has NaN's, or how to build one). So it will
225 end up as an infinity or something close; that is OK. */
226
227 mant_bits_left = fmt->man_len;
228 mant_off = fmt->man_start;
229 dto = 0.0;
230
231 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
232
0963b4bd
MS
233 /* Don't bias NaNs. Use minimum exponent for denorms. For
234 simplicity, we don't check for zero as the exponent doesn't matter.
235 Note the cast to int; exp_bias is unsigned, so it's important to
236 make sure the operation is done in signed arithmetic. */
d16aafd8
AC
237 if (!special_exponent)
238 exponent -= fmt->exp_bias;
239 else if (exponent == 0)
1c704f11 240 exponent = 1 - fmt->exp_bias;
d16aafd8
AC
241
242 /* Build the result algebraically. Might go infinite, underflow, etc;
0963b4bd 243 who cares. */
d16aafd8
AC
244
245/* If this format uses a hidden bit, explicitly add it in now. Otherwise,
246 increment the exponent by one to account for the integer bit. */
247
248 if (!special_exponent)
249 {
250 if (fmt->intbit == floatformat_intbit_no)
251 dto = ldexp (1.0, exponent);
252 else
253 exponent++;
254 }
255
256 while (mant_bits_left > 0)
257 {
325fac50 258 mant_bits = std::min (mant_bits_left, 32);
d16aafd8 259
fcab3fb5 260 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8
AC
261
262 dto += ldexp ((double) mant, exponent - mant_bits);
263 exponent -= mant_bits;
264 mant_off += mant_bits;
265 mant_bits_left -= mant_bits;
266 }
267
268 /* Negate it if negative. */
fcab3fb5 269 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
d16aafd8
AC
270 dto = -dto;
271 *to = dto;
272}
273\f
d16aafd8
AC
274/* Set a field which starts at START and is LEN bytes long. DATA and
275 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
276static void
277put_field (unsigned char *data, enum floatformat_byteorders order,
278 unsigned int total_len, unsigned int start, unsigned int len,
279 unsigned long stuff_to_put)
280{
281 unsigned int cur_byte;
282 int cur_bitshift;
283
fcab3fb5
RE
284 /* Caller must byte-swap words before calling this routine. */
285 gdb_assert (order == floatformat_little || order == floatformat_big);
286
d16aafd8 287 /* Start at the least significant part of the field. */
fcab3fb5 288 if (order == floatformat_little)
d16aafd8
AC
289 {
290 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
9a619af0 291
d16aafd8
AC
292 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
293 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
294 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
295 - FLOATFORMAT_CHAR_BIT;
296 }
297 else
298 {
299 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
300 cur_bitshift =
301 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
302 }
303 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
304 {
305 *(data + cur_byte) &=
306 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
307 << (-cur_bitshift));
308 *(data + cur_byte) |=
309 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
310 }
311 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 312 if (order == floatformat_little)
d16aafd8
AC
313 ++cur_byte;
314 else
315 --cur_byte;
316
317 /* Move towards the most significant part of the field. */
318 while (cur_bitshift < len)
319 {
320 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
321 {
322 /* This is the last byte. */
323 *(data + cur_byte) &=
324 ~((1 << (len - cur_bitshift)) - 1);
325 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
326 }
327 else
328 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
329 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
330 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 331 if (order == floatformat_little)
d16aafd8
AC
332 ++cur_byte;
333 else
334 --cur_byte;
335 }
336}
337
0a3e99f6
MK
338/* The converse: convert the DOUBLEST *FROM to an extended float and
339 store where TO points. Neither FROM nor TO have any alignment
d16aafd8
AC
340 restrictions. */
341
c422e771 342static void
cc2f3c35 343convert_doublest_to_floatformat (const struct floatformat *fmt,
0a3e99f6 344 const DOUBLEST *from, void *to)
d16aafd8
AC
345{
346 DOUBLEST dfrom;
347 int exponent;
348 DOUBLEST mant;
349 unsigned int mant_bits, mant_off;
350 int mant_bits_left;
351 unsigned char *uto = (unsigned char *) to;
fcab3fb5 352 enum floatformat_byteorders order = fmt->byteorder;
0a3e99f6 353 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
fcab3fb5 354
0a3e99f6 355 if (order != floatformat_little)
fcab3fb5 356 order = floatformat_big;
d16aafd8 357
0a3e99f6
MK
358 if (order != fmt->byteorder)
359 uto = newto;
360
d16aafd8 361 memcpy (&dfrom, from, sizeof (dfrom));
e1ba3053 362 memset (uto, 0, floatformat_totalsize_bytes (fmt));
b14d30e1
JM
363
364 if (fmt->split_half)
365 {
366 /* Use static volatile to ensure that any excess precision is
367 removed via storing in memory, and so the top half really is
368 the result of converting to double. */
369 static volatile double dtop, dbot;
542a88d0 370 DOUBLEST dtopnv, dbotnv;
9a619af0 371
b14d30e1
JM
372 dtop = (double) dfrom;
373 /* If the rounded top half is Inf, the bottom must be 0 not NaN
374 or Inf. */
375 if (dtop + dtop == dtop && dtop != 0.0)
376 dbot = 0.0;
377 else
378 dbot = (double) (dfrom - (DOUBLEST) dtop);
379 dtopnv = dtop;
380 dbotnv = dbot;
542a88d0
LM
381 floatformat_from_doublest (fmt->split_half, &dtopnv, uto);
382 floatformat_from_doublest (fmt->split_half, &dbotnv,
b14d30e1
JM
383 (uto
384 + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2));
385 return;
386 }
387
d16aafd8 388 if (dfrom == 0)
67fa57cf 389 goto finalize_byteorder; /* Result is zero */
d16aafd8
AC
390 if (dfrom != dfrom) /* Result is NaN */
391 {
392 /* From is NaN */
fcab3fb5 393 put_field (uto, order, fmt->totalsize, fmt->exp_start,
d16aafd8 394 fmt->exp_len, fmt->exp_nan);
0963b4bd 395 /* Be sure it's not infinity, but NaN value is irrel. */
fcab3fb5 396 put_field (uto, order, fmt->totalsize, fmt->man_start,
fbe12357 397 fmt->man_len, 1);
fcab3fb5 398 goto finalize_byteorder;
d16aafd8
AC
399 }
400
401 /* If negative, set the sign bit. */
402 if (dfrom < 0)
403 {
fcab3fb5 404 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
d16aafd8
AC
405 dfrom = -dfrom;
406 }
407
0963b4bd 408 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */
d16aafd8
AC
409 {
410 /* Infinity exponent is same as NaN's. */
fcab3fb5 411 put_field (uto, order, fmt->totalsize, fmt->exp_start,
d16aafd8
AC
412 fmt->exp_len, fmt->exp_nan);
413 /* Infinity mantissa is all zeroes. */
fcab3fb5 414 put_field (uto, order, fmt->totalsize, fmt->man_start,
d16aafd8 415 fmt->man_len, 0);
fcab3fb5 416 goto finalize_byteorder;
d16aafd8
AC
417 }
418
419#ifdef HAVE_LONG_DOUBLE
85d3b769 420 mant = frexpl (dfrom, &exponent);
d16aafd8
AC
421#else
422 mant = frexp (dfrom, &exponent);
423#endif
424
33d7655b
JB
425 if (exponent + fmt->exp_bias <= 0)
426 {
427 /* The value is too small to be expressed in the destination
428 type (not enough bits in the exponent. Treat as 0. */
429 put_field (uto, order, fmt->totalsize, fmt->exp_start,
430 fmt->exp_len, 0);
431 put_field (uto, order, fmt->totalsize, fmt->man_start,
432 fmt->man_len, 0);
433 goto finalize_byteorder;
434 }
435
32560274 436 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
33d7655b
JB
437 {
438 /* The value is too large to fit into the destination.
439 Treat as infinity. */
440 put_field (uto, order, fmt->totalsize, fmt->exp_start,
441 fmt->exp_len, fmt->exp_nan);
442 put_field (uto, order, fmt->totalsize, fmt->man_start,
443 fmt->man_len, 0);
444 goto finalize_byteorder;
445 }
446
fcab3fb5 447 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
d16aafd8
AC
448 exponent + fmt->exp_bias - 1);
449
450 mant_bits_left = fmt->man_len;
451 mant_off = fmt->man_start;
452 while (mant_bits_left > 0)
453 {
454 unsigned long mant_long;
9a619af0 455
d16aafd8
AC
456 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
457
458 mant *= 4294967296.0;
459 mant_long = ((unsigned long) mant) & 0xffffffffL;
460 mant -= mant_long;
461
462 /* If the integer bit is implicit, then we need to discard it.
463 If we are discarding a zero, we should be (but are not) creating
464 a denormalized number which means adjusting the exponent
465 (I think). */
466 if (mant_bits_left == fmt->man_len
467 && fmt->intbit == floatformat_intbit_no)
468 {
469 mant_long <<= 1;
470 mant_long &= 0xffffffffL;
06194148
JJ
471 /* If we are processing the top 32 mantissa bits of a doublest
472 so as to convert to a float value with implied integer bit,
473 we will only be putting 31 of those 32 bits into the
474 final value due to the discarding of the top bit. In the
475 case of a small float value where the number of mantissa
476 bits is less than 32, discarding the top bit does not alter
477 the number of bits we will be adding to the result. */
478 if (mant_bits == 32)
479 mant_bits -= 1;
d16aafd8
AC
480 }
481
482 if (mant_bits < 32)
483 {
484 /* The bits we want are in the most significant MANT_BITS bits of
485 mant_long. Move them to the least significant. */
486 mant_long >>= 32 - mant_bits;
487 }
488
fcab3fb5 489 put_field (uto, order, fmt->totalsize,
d16aafd8
AC
490 mant_off, mant_bits, mant_long);
491 mant_off += mant_bits;
492 mant_bits_left -= mant_bits;
493 }
fcab3fb5
RE
494
495 finalize_byteorder:
496 /* Do we need to byte-swap the words in the result? */
497 if (order != fmt->byteorder)
0a3e99f6 498 floatformat_normalize_byteorder (fmt, newto, to);
d16aafd8
AC
499}
500
501/* Check if VAL (which is assumed to be a floating point number whose
502 format is described by FMT) is negative. */
503
504int
108d6ead
AC
505floatformat_is_negative (const struct floatformat *fmt,
506 const bfd_byte *uval)
d16aafd8 507{
fcab3fb5
RE
508 enum floatformat_byteorders order;
509 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
510
069e84fd 511 gdb_assert (fmt != NULL);
fcab3fb5
RE
512 gdb_assert (fmt->totalsize
513 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
514
f5aee5ee
AM
515 /* An IBM long double (a two element array of double) always takes the
516 sign of the first double. */
517 if (fmt->split_half)
518 fmt = fmt->split_half;
519
fcab3fb5
RE
520 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
521
522 if (order != fmt->byteorder)
523 uval = newfrom;
524
525 return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
d16aafd8
AC
526}
527
528/* Check if VAL is "not a number" (NaN) for FMT. */
529
20389057
DJ
530enum float_kind
531floatformat_classify (const struct floatformat *fmt,
532 const bfd_byte *uval)
d16aafd8 533{
d16aafd8
AC
534 long exponent;
535 unsigned long mant;
536 unsigned int mant_bits, mant_off;
537 int mant_bits_left;
fcab3fb5
RE
538 enum floatformat_byteorders order;
539 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
20389057 540 int mant_zero;
fcab3fb5 541
069e84fd 542 gdb_assert (fmt != NULL);
fcab3fb5
RE
543 gdb_assert (fmt->totalsize
544 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
545
f5aee5ee
AM
546 /* An IBM long double (a two element array of double) can be classified
547 by looking at the first double. inf and nan are specified as
548 ignoring the second double. zero and subnormal will always have
549 the second double 0.0 if the long double is correctly rounded. */
550 if (fmt->split_half)
551 fmt = fmt->split_half;
552
fcab3fb5
RE
553 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
554
555 if (order != fmt->byteorder)
556 uval = newfrom;
069e84fd 557
fcab3fb5
RE
558 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
559 fmt->exp_len);
d16aafd8 560
d16aafd8
AC
561 mant_bits_left = fmt->man_len;
562 mant_off = fmt->man_start;
563
20389057 564 mant_zero = 1;
d16aafd8
AC
565 while (mant_bits_left > 0)
566 {
325fac50 567 mant_bits = std::min (mant_bits_left, 32);
d16aafd8 568
fcab3fb5 569 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8
AC
570
571 /* If there is an explicit integer bit, mask it off. */
572 if (mant_off == fmt->man_start
573 && fmt->intbit == floatformat_intbit_yes)
574 mant &= ~(1 << (mant_bits - 1));
575
576 if (mant)
20389057
DJ
577 {
578 mant_zero = 0;
579 break;
580 }
d16aafd8
AC
581
582 mant_off += mant_bits;
583 mant_bits_left -= mant_bits;
584 }
585
20389057
DJ
586 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
587 supported. */
588 if (! fmt->exp_nan)
589 {
590 if (mant_zero)
591 return float_zero;
592 else
593 return float_normal;
594 }
595
70100014
UW
596 if (exponent == 0)
597 {
598 if (mant_zero)
599 return float_zero;
600 else
601 return float_subnormal;
602 }
20389057
DJ
603
604 if (exponent == fmt->exp_nan)
605 {
606 if (mant_zero)
607 return float_infinite;
608 else
609 return float_nan;
610 }
611
20389057 612 return float_normal;
d16aafd8
AC
613}
614
615/* Convert the mantissa of VAL (which is assumed to be a floating
616 point number whose format is described by FMT) into a hexadecimal
617 and store it in a static string. Return a pointer to that string. */
618
108d6ead
AC
619const char *
620floatformat_mantissa (const struct floatformat *fmt,
621 const bfd_byte *val)
d16aafd8
AC
622{
623 unsigned char *uval = (unsigned char *) val;
624 unsigned long mant;
625 unsigned int mant_bits, mant_off;
626 int mant_bits_left;
627 static char res[50];
628 char buf[9];
27df76f3 629 int len;
fcab3fb5
RE
630 enum floatformat_byteorders order;
631 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
632
633 gdb_assert (fmt != NULL);
634 gdb_assert (fmt->totalsize
635 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
636
f5aee5ee
AM
637 /* For IBM long double (a two element array of double), return the
638 mantissa of the first double. The problem with returning the
639 actual mantissa from both doubles is that there can be an
640 arbitrary number of implied 0's or 1's between the mantissas
641 of the first and second double. In any case, this function
642 is only used for dumping out nans, and a nan is specified to
643 ignore the value in the second double. */
644 if (fmt->split_half)
645 fmt = fmt->split_half;
646
fcab3fb5
RE
647 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
648
649 if (order != fmt->byteorder)
650 uval = newfrom;
651
652 if (! fmt->exp_nan)
653 return 0;
d16aafd8
AC
654
655 /* Make sure we have enough room to store the mantissa. */
656 gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
657
658 mant_off = fmt->man_start;
659 mant_bits_left = fmt->man_len;
660 mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
661
fcab3fb5 662 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8 663
27df76f3 664 len = xsnprintf (res, sizeof res, "%lx", mant);
d16aafd8
AC
665
666 mant_off += mant_bits;
667 mant_bits_left -= mant_bits;
27df76f3 668
d16aafd8
AC
669 while (mant_bits_left > 0)
670 {
fcab3fb5 671 mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
d16aafd8 672
27df76f3
MK
673 xsnprintf (buf, sizeof buf, "%08lx", mant);
674 gdb_assert (len + strlen (buf) <= sizeof res);
d16aafd8
AC
675 strcat (res, buf);
676
677 mant_off += 32;
678 mant_bits_left -= 32;
679 }
680
681 return res;
682}
683
fdf0cbc2
UW
684/* Return the precision of the floating point format FMT. */
685
686static int
687floatformat_precision (const struct floatformat *fmt)
688{
689 /* Assume the precision of and IBM long double is twice the precision
690 of the underlying double. This matches what GCC does. */
691 if (fmt->split_half)
692 return 2 * floatformat_precision (fmt->split_half);
693
694 /* Otherwise, the precision is the size of mantissa in bits,
695 including the implicit bit if present. */
696 int prec = fmt->man_len;
697 if (fmt->intbit == floatformat_intbit_no)
698 prec++;
699
700 return prec;
701}
702
d16aafd8 703\f
c422e771
AC
704/* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
705
706 If the host and target formats agree, we just copy the raw data
707 into the appropriate type of variable and return, letting the host
708 increase precision as necessary. Otherwise, we call the conversion
d7a87b5e
PA
709 routine and let it do the dirty work. Note that even if the target
710 and host floating-point formats match, the length of the types
711 might still be different, so the conversion routines must make sure
712 to not overrun any buffers. For example, on x86, long double is
713 the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
714 but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
715 64-bit, for alignment reasons. See comment in store_typed_floating
716 for a discussion about zeroing out remaining bytes in the target
717 buffer. */
c422e771 718
c35f4ffc
AC
719static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
720static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
3e43a32a
MS
721static const struct floatformat *host_long_double_format
722 = GDB_HOST_LONG_DOUBLE_FORMAT;
c422e771 723
b79497cb
PA
724/* See doublest.h. */
725
726size_t
727floatformat_totalsize_bytes (const struct floatformat *fmt)
728{
729 return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
730 / FLOATFORMAT_CHAR_BIT);
731}
732
c422e771
AC
733void
734floatformat_to_doublest (const struct floatformat *fmt,
735 const void *in, DOUBLEST *out)
736{
737 gdb_assert (fmt != NULL);
d7a87b5e 738
c422e771
AC
739 if (fmt == host_float_format)
740 {
d7a87b5e 741 float val = 0;
9a619af0 742
d7a87b5e 743 memcpy (&val, in, floatformat_totalsize_bytes (fmt));
c422e771
AC
744 *out = val;
745 }
746 else if (fmt == host_double_format)
747 {
d7a87b5e 748 double val = 0;
9a619af0 749
d7a87b5e 750 memcpy (&val, in, floatformat_totalsize_bytes (fmt));
c422e771
AC
751 *out = val;
752 }
753 else if (fmt == host_long_double_format)
754 {
d7a87b5e 755 long double val = 0;
9a619af0 756
d7a87b5e 757 memcpy (&val, in, floatformat_totalsize_bytes (fmt));
c422e771
AC
758 *out = val;
759 }
760 else
761 convert_floatformat_to_doublest (fmt, in, out);
762}
763
764void
765floatformat_from_doublest (const struct floatformat *fmt,
766 const DOUBLEST *in, void *out)
767{
768 gdb_assert (fmt != NULL);
d7a87b5e 769
c422e771
AC
770 if (fmt == host_float_format)
771 {
772 float val = *in;
9a619af0 773
d7a87b5e 774 memcpy (out, &val, floatformat_totalsize_bytes (fmt));
c422e771
AC
775 }
776 else if (fmt == host_double_format)
777 {
778 double val = *in;
9a619af0 779
d7a87b5e 780 memcpy (out, &val, floatformat_totalsize_bytes (fmt));
c422e771
AC
781 }
782 else if (fmt == host_long_double_format)
783 {
784 long double val = *in;
9a619af0 785
d7a87b5e 786 memcpy (out, &val, floatformat_totalsize_bytes (fmt));
c422e771
AC
787 }
788 else
789 convert_doublest_to_floatformat (fmt, in, out);
790}
d16aafd8 791
fdf0cbc2 792/* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
16e812b2 793 to a string, optionally using the print format FORMAT. */
fdf0cbc2
UW
794std::string
795floatformat_to_string (const struct floatformat *fmt,
16e812b2 796 const gdb_byte *in, const char *format)
fdf0cbc2 797{
16e812b2
UW
798 /* Unless we need to adhere to a specific format, provide special
799 output for certain cases. */
800 if (format == nullptr)
fdf0cbc2 801 {
16e812b2
UW
802 /* Detect invalid representations. */
803 if (!floatformat_is_valid (fmt, in))
804 return "<invalid float value>";
805
806 /* Handle NaN and Inf. */
807 enum float_kind kind = floatformat_classify (fmt, in);
808 if (kind == float_nan)
809 {
810 const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
811 const char *mantissa = floatformat_mantissa (fmt, in);
812 return string_printf ("%snan(0x%s)", sign, mantissa);
813 }
814 else if (kind == float_infinite)
815 {
816 const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
817 return string_printf ("%sinf", sign);
818 }
fdf0cbc2
UW
819 }
820
16e812b2
UW
821 /* Determine the format string to use on the host side. */
822 std::string host_format;
823 char conversion;
824
825 if (format == nullptr)
826 {
827 /* If no format was specified, print the number using a format string
828 where the precision is set to the DECIMAL_DIG value for the given
829 floating-point format. This value is computed as
fdf0cbc2
UW
830
831 ceil(1 + p * log10(b)),
832
16e812b2
UW
833 where p is the precision of the floating-point format in bits, and
834 b is the base (which is always 2 for the formats we support). */
835 const double log10_2 = .30102999566398119521;
836 double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2;
837 int decimal_dig = d_decimal_dig;
838 if (decimal_dig < d_decimal_dig)
839 decimal_dig++;
840
841 host_format = string_printf ("%%.%d", decimal_dig);
842 conversion = 'g';
843 }
844 else
845 {
846 /* Use the specified format, stripping out the conversion character
847 and length modifier, if present. */
848 size_t len = strlen (format);
849 gdb_assert (len > 1);
850 conversion = format[--len];
851 gdb_assert (conversion == 'e' || conversion == 'f' || conversion == 'g'
852 || conversion == 'E' || conversion == 'G');
853 if (format[len - 1] == 'L')
854 len--;
fdf0cbc2 855
16e812b2
UW
856 host_format = std::string (format, len);
857 }
858
859 /* Add the length modifier and conversion character appropriate for
860 handling the host DOUBLEST type. */
861#ifdef HAVE_LONG_DOUBLE
862 host_format += 'L';
863#endif
864 host_format += conversion;
fdf0cbc2
UW
865
866 DOUBLEST doub;
867 floatformat_to_doublest (fmt, in, &doub);
868 return string_printf (host_format.c_str (), doub);
869}
edd079d9
UW
870
871/* Parse string STRING into a target floating-number of format FMT and
872 store it as byte-stream ADDR. Return whether parsing succeeded. */
873bool
874floatformat_from_string (const struct floatformat *fmt, gdb_byte *out,
875 const std::string &in)
876{
877 DOUBLEST doub;
878 int n, num;
879#ifdef HAVE_LONG_DOUBLE
880 const char *scan_format = "%Lg%n";
881#else
882 const char *scan_format = "%lg%n";
883#endif
884 num = sscanf (in.c_str (), scan_format, &doub, &n);
885
886 /* The sscanf man page suggests not making any assumptions on the effect
887 of %n on the result, so we don't.
888 That is why we simply test num == 0. */
889 if (num == 0)
890 return false;
891
892 /* We only accept the whole string. */
893 if (in[n])
894 return false;
895
896 floatformat_from_doublest (fmt, &doub, out);
897 return true;
898}
This page took 1.174525 seconds and 4 git commands to generate.