]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/floatformat.c
pexecute.c (pexecute): Cast string litrals to char *.
[thirdparty/gcc.git] / libiberty / floatformat.c
CommitLineData
6599da04 1/* IEEE floating point support routines, for GDB, the GNU Debugger.
9c8860c3 2 Copyright (C) 1991, 1994, 1999, 2000, 2003 Free Software Foundation, Inc.
6599da04
JM
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
ee58dffd 18Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
6599da04 19
bee6ab3e
ILT
20/* This is needed to pick up the NAN macro on some systems. */
21#define _GNU_SOURCE
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <math.h>
28
29#ifdef HAVE_STRING_H
30#include <string.h>
31#endif
32
c9ac9147 33#include "ansidecl.h"
bee6ab3e 34#include "libiberty.h"
6599da04 35#include "floatformat.h"
bee6ab3e
ILT
36
37#ifndef INFINITY
38#ifdef HUGE_VAL
39#define INFINITY HUGE_VAL
6599da04 40#else
bee6ab3e
ILT
41#define INFINITY (1.0 / 0.0)
42#endif
43#endif
44
45#ifndef NAN
46#define NAN (0.0 / 0.0)
6599da04
JM
47#endif
48
6da879de
GDR
49static unsigned long get_field (const unsigned char *,
50 enum floatformat_byteorders,
51 unsigned int,
52 unsigned int,
53 unsigned int);
54static int floatformat_always_valid (const struct floatformat *fmt,
55 const char *from);
83c07342
AC
56
57static int
6da879de
GDR
58floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
59 const char *from ATTRIBUTE_UNUSED)
83c07342
AC
60{
61 return 1;
62}
63
6599da04
JM
64/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
65 going to bother with trying to muck around with whether it is defined in
66 a system header, what we do if not, etc. */
67#define FLOATFORMAT_CHAR_BIT 8
68
69/* floatformats for IEEE single and double, big and little endian. */
70const struct floatformat floatformat_ieee_single_big =
71{
0b72c3df
AC
72 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
73 floatformat_intbit_no,
83c07342
AC
74 "floatformat_ieee_single_big",
75 floatformat_always_valid
6599da04
JM
76};
77const struct floatformat floatformat_ieee_single_little =
78{
0b72c3df
AC
79 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
80 floatformat_intbit_no,
83c07342
AC
81 "floatformat_ieee_single_little",
82 floatformat_always_valid
6599da04
JM
83};
84const struct floatformat floatformat_ieee_double_big =
85{
0b72c3df
AC
86 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
87 floatformat_intbit_no,
83c07342
AC
88 "floatformat_ieee_double_big",
89 floatformat_always_valid
6599da04
JM
90};
91const struct floatformat floatformat_ieee_double_little =
92{
0b72c3df
AC
93 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
94 floatformat_intbit_no,
83c07342
AC
95 "floatformat_ieee_double_little",
96 floatformat_always_valid
6599da04
JM
97};
98
99/* floatformat for IEEE double, little endian byte order, with big endian word
100 ordering, as on the ARM. */
101
102const struct floatformat floatformat_ieee_double_littlebyte_bigword =
103{
0b72c3df
AC
104 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
105 floatformat_intbit_no,
83c07342
AC
106 "floatformat_ieee_double_littlebyte_bigword",
107 floatformat_always_valid
6599da04
JM
108};
109
6da879de 110static int floatformat_i387_ext_is_valid (const struct floatformat *fmt, const char *from);
83c07342
AC
111
112static int
6da879de 113floatformat_i387_ext_is_valid (const struct floatformat *fmt, const char *from)
83c07342
AC
114{
115 /* In the i387 double-extended format, if the exponent is all ones,
116 then the integer bit must be set. If the exponent is neither 0
117 nor ~0, the intbit must also be set. Only if the exponent is
118 zero can it be zero, and then it must be zero. */
119 unsigned long exponent, int_bit;
120 const unsigned char *ufrom = (const unsigned char *) from;
121
122 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
123 fmt->exp_start, fmt->exp_len);
124 int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
125 fmt->man_start, 1);
126
127 if ((exponent == 0) != (int_bit == 0))
128 return 0;
129 else
130 return 1;
131}
132
6599da04
JM
133const struct floatformat floatformat_i387_ext =
134{
135 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
0b72c3df 136 floatformat_intbit_yes,
83c07342
AC
137 "floatformat_i387_ext",
138 floatformat_i387_ext_is_valid
6599da04
JM
139};
140const struct floatformat floatformat_m68881_ext =
141{
142 /* Note that the bits from 16 to 31 are unused. */
0b72c3df
AC
143 floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
144 floatformat_intbit_yes,
83c07342
AC
145 "floatformat_m68881_ext",
146 floatformat_always_valid
6599da04
JM
147};
148const struct floatformat floatformat_i960_ext =
149{
150 /* Note that the bits from 0 to 15 are unused. */
151 floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
0b72c3df 152 floatformat_intbit_yes,
83c07342
AC
153 "floatformat_i960_ext",
154 floatformat_always_valid
6599da04
JM
155};
156const struct floatformat floatformat_m88110_ext =
157{
0310e5ac
AC
158 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
159 floatformat_intbit_yes,
83c07342
AC
160 "floatformat_m88110_ext",
161 floatformat_always_valid
0310e5ac
AC
162};
163const struct floatformat floatformat_m88110_harris_ext =
164{
6599da04
JM
165 /* Harris uses raw format 128 bytes long, but the number is just an ieee
166 double, and the last 64 bits are wasted. */
167 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
0b72c3df 168 floatformat_intbit_no,
83c07342
AC
169 "floatformat_m88110_ext_harris",
170 floatformat_always_valid
6599da04 171};
0310e5ac
AC
172const struct floatformat floatformat_arm_ext_big =
173{
174 /* Bits 1 to 16 are unused. */
175 floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
176 floatformat_intbit_yes,
83c07342
AC
177 "floatformat_arm_ext_big",
178 floatformat_always_valid
0310e5ac
AC
179};
180const struct floatformat floatformat_arm_ext_littlebyte_bigword =
181{
182 /* Bits 1 to 16 are unused. */
183 floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
184 floatformat_intbit_yes,
83c07342
AC
185 "floatformat_arm_ext_littlebyte_bigword",
186 floatformat_always_valid
0310e5ac
AC
187};
188const struct floatformat floatformat_ia64_spill_big =
189{
190 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
191 floatformat_intbit_yes,
83c07342
AC
192 "floatformat_ia64_spill_big",
193 floatformat_always_valid
0310e5ac
AC
194};
195const struct floatformat floatformat_ia64_spill_little =
196{
197 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
198 floatformat_intbit_yes,
83c07342
AC
199 "floatformat_ia64_spill_little",
200 floatformat_always_valid
0310e5ac
AC
201};
202const struct floatformat floatformat_ia64_quad_big =
203{
204 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
205 floatformat_intbit_no,
83c07342
AC
206 "floatformat_ia64_quad_big",
207 floatformat_always_valid
0310e5ac
AC
208};
209const struct floatformat floatformat_ia64_quad_little =
210{
211 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
212 floatformat_intbit_no,
83c07342
AC
213 "floatformat_ia64_quad_little",
214 floatformat_always_valid
0310e5ac 215};
6599da04 216\f
9c8860c3 217/* Extract a field which starts at START and is LEN bits long. DATA and
6599da04
JM
218 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
219static unsigned long
6da879de
GDR
220get_field (const unsigned char *data, enum floatformat_byteorders order,
221 unsigned int total_len, unsigned int start, unsigned int len)
6599da04
JM
222{
223 unsigned long result;
224 unsigned int cur_byte;
225 int cur_bitshift;
226
227 /* Start at the least significant part of the field. */
228 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
229 if (order == floatformat_little)
230 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
231 cur_bitshift =
232 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
233 result = *(data + cur_byte) >> (-cur_bitshift);
234 cur_bitshift += FLOATFORMAT_CHAR_BIT;
235 if (order == floatformat_little)
236 ++cur_byte;
237 else
238 --cur_byte;
239
240 /* Move towards the most significant part of the field. */
5ad5a984 241 while ((unsigned int) cur_bitshift < len)
6599da04
JM
242 {
243 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
244 /* This is the last byte; zero out the bits which are not part of
245 this field. */
246 result |=
247 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
248 << cur_bitshift;
249 else
250 result |= *(data + cur_byte) << cur_bitshift;
251 cur_bitshift += FLOATFORMAT_CHAR_BIT;
252 if (order == floatformat_little)
253 ++cur_byte;
254 else
255 --cur_byte;
256 }
257 return result;
258}
259
260#ifndef min
261#define min(a, b) ((a) < (b) ? (a) : (b))
262#endif
263
264/* Convert from FMT to a double.
265 FROM is the address of the extended float.
266 Store the double in *TO. */
267
268void
6da879de
GDR
269floatformat_to_double (const struct floatformat *fmt,
270 const char *from, double *to)
6599da04 271{
c9fbef12 272 const unsigned char *ufrom = (const unsigned char *)from;
6599da04
JM
273 double dto;
274 long exponent;
275 unsigned long mant;
276 unsigned int mant_bits, mant_off;
277 int mant_bits_left;
278 int special_exponent; /* It's a NaN, denorm or zero */
279
280 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
281 fmt->exp_start, fmt->exp_len);
bee6ab3e
ILT
282
283 /* If the exponent indicates a NaN, we don't have information to
284 decide what to do. So we handle it like IEEE, except that we
285 don't try to preserve the type of NaN. FIXME. */
286 if ((unsigned long) exponent == fmt->exp_nan)
287 {
288 int nan;
289
290 mant_off = fmt->man_start;
291 mant_bits_left = fmt->man_len;
292 nan = 0;
293 while (mant_bits_left > 0)
294 {
295 mant_bits = min (mant_bits_left, 32);
296
297 if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
298 mant_off, mant_bits) != 0)
299 {
300 /* This is a NaN. */
301 nan = 1;
302 break;
303 }
304
305 mant_off += mant_bits;
306 mant_bits_left -= mant_bits;
307 }
308
309 if (nan)
310 dto = NAN;
311 else
312 dto = INFINITY;
313
314 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
315 dto = -dto;
316
317 *to = dto;
318
319 return;
320 }
6599da04
JM
321
322 mant_bits_left = fmt->man_len;
323 mant_off = fmt->man_start;
324 dto = 0.0;
325
5ad5a984 326 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
6599da04
JM
327
328 /* Don't bias zero's, denorms or NaNs. */
329 if (!special_exponent)
330 exponent -= fmt->exp_bias;
331
332 /* Build the result algebraically. Might go infinite, underflow, etc;
333 who cares. */
334
335 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
336 increment the exponent by one to account for the integer bit. */
337
338 if (!special_exponent)
087aa398
KG
339 {
340 if (fmt->intbit == floatformat_intbit_no)
341 dto = ldexp (1.0, exponent);
342 else
343 exponent++;
344 }
6599da04
JM
345
346 while (mant_bits_left > 0)
347 {
348 mant_bits = min (mant_bits_left, 32);
349
350 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
351 mant_off, mant_bits);
352
bee6ab3e
ILT
353 /* Handle denormalized numbers. FIXME: What should we do for
354 non-IEEE formats? */
355 if (exponent == 0 && mant != 0)
356 dto += ldexp ((double)mant,
357 (- fmt->exp_bias
358 - mant_bits
359 - (mant_off - fmt->man_start)
360 + 1));
361 else
362 dto += ldexp ((double)mant, exponent - mant_bits);
363 if (exponent != 0)
364 exponent -= mant_bits;
6599da04
JM
365 mant_off += mant_bits;
366 mant_bits_left -= mant_bits;
367 }
368
369 /* Negate it if negative. */
370 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
371 dto = -dto;
372 *to = dto;
373}
374\f
6da879de
GDR
375static void put_field (unsigned char *, enum floatformat_byteorders,
376 unsigned int,
377 unsigned int,
378 unsigned int,
379 unsigned long);
6599da04 380
9c8860c3 381/* Set a field which starts at START and is LEN bits long. DATA and
6599da04
JM
382 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
383static void
6da879de
GDR
384put_field (unsigned char *data, enum floatformat_byteorders order,
385 unsigned int total_len, unsigned int start, unsigned int len,
386 unsigned long stuff_to_put)
6599da04
JM
387{
388 unsigned int cur_byte;
389 int cur_bitshift;
390
391 /* Start at the least significant part of the field. */
392 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
393 if (order == floatformat_little)
394 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
395 cur_bitshift =
396 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
397 *(data + cur_byte) &=
398 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
399 *(data + cur_byte) |=
400 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
401 cur_bitshift += FLOATFORMAT_CHAR_BIT;
402 if (order == floatformat_little)
403 ++cur_byte;
404 else
405 --cur_byte;
406
407 /* Move towards the most significant part of the field. */
5ad5a984 408 while ((unsigned int) cur_bitshift < len)
6599da04
JM
409 {
410 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
411 {
412 /* This is the last byte. */
413 *(data + cur_byte) &=
414 ~((1 << (len - cur_bitshift)) - 1);
415 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
416 }
417 else
418 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
419 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
420 cur_bitshift += FLOATFORMAT_CHAR_BIT;
421 if (order == floatformat_little)
422 ++cur_byte;
423 else
424 --cur_byte;
425 }
426}
427
428/* The converse: convert the double *FROM to an extended float
429 and store where TO points. Neither FROM nor TO have any alignment
430 restrictions. */
431
432void
6da879de
GDR
433floatformat_from_double (const struct floatformat *fmt,
434 const double *from, char *to)
6599da04
JM
435{
436 double dfrom;
437 int exponent;
438 double mant;
439 unsigned int mant_bits, mant_off;
440 int mant_bits_left;
441 unsigned char *uto = (unsigned char *)to;
442
bee6ab3e 443 dfrom = *from;
6599da04 444 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
bee6ab3e
ILT
445
446 /* If negative, set the sign bit. */
447 if (dfrom < 0)
448 {
449 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
450 dfrom = -dfrom;
451 }
452
6599da04 453 if (dfrom == 0)
bee6ab3e
ILT
454 {
455 /* 0.0. */
456 return;
457 }
458
6599da04
JM
459 if (dfrom != dfrom)
460 {
bee6ab3e 461 /* NaN. */
6599da04
JM
462 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
463 fmt->exp_len, fmt->exp_nan);
bee6ab3e 464 /* Be sure it's not infinity, but NaN value is irrelevant. */
6599da04
JM
465 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
466 32, 1);
467 return;
468 }
469
bee6ab3e 470 if (dfrom + dfrom == dfrom)
6599da04 471 {
bee6ab3e
ILT
472 /* This can only happen for an infinite value (or zero, which we
473 already handled above). */
474 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
475 fmt->exp_len, fmt->exp_nan);
476 return;
6599da04
JM
477 }
478
6599da04 479 mant = frexp (dfrom, &exponent);
bee6ab3e
ILT
480 if (exponent + fmt->exp_bias - 1 > 0)
481 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
482 fmt->exp_len, exponent + fmt->exp_bias - 1);
483 else
484 {
485 /* Handle a denormalized number. FIXME: What should we do for
486 non-IEEE formats? */
487 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
488 fmt->exp_len, 0);
489 mant = ldexp (mant, exponent + fmt->exp_bias - 1);
490 }
6599da04
JM
491
492 mant_bits_left = fmt->man_len;
493 mant_off = fmt->man_start;
494 while (mant_bits_left > 0)
495 {
496 unsigned long mant_long;
497 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
498
499 mant *= 4294967296.0;
500 mant_long = (unsigned long)mant;
501 mant -= mant_long;
502
bee6ab3e
ILT
503 /* If the integer bit is implicit, and we are not creating a
504 denormalized number, then we need to discard it. */
5ad5a984 505 if ((unsigned int) mant_bits_left == fmt->man_len
bee6ab3e
ILT
506 && fmt->intbit == floatformat_intbit_no
507 && exponent + fmt->exp_bias - 1 > 0)
6599da04
JM
508 {
509 mant_long &= 0x7fffffff;
510 mant_bits -= 1;
511 }
512 else if (mant_bits < 32)
513 {
514 /* The bits we want are in the most significant MANT_BITS bits of
515 mant_long. Move them to the least significant. */
516 mant_long >>= 32 - mant_bits;
517 }
518
519 put_field (uto, fmt->byteorder, fmt->totalsize,
520 mant_off, mant_bits, mant_long);
521 mant_off += mant_bits;
522 mant_bits_left -= mant_bits;
523 }
524}
525
9c8860c3
DJ
526/* Return non-zero iff the data at FROM is a valid number in format FMT. */
527
528int
6da879de 529floatformat_is_valid (const struct floatformat *fmt, const char *from)
9c8860c3 530{
83c07342 531 return fmt->is_valid (fmt, from);
9c8860c3
DJ
532}
533
6599da04
JM
534
535#ifdef IEEE_DEBUG
536
bee6ab3e
ILT
537#include <stdio.h>
538
6599da04
JM
539/* This is to be run on a host which uses IEEE floating point. */
540
541void
6da879de 542ieee_test (double n)
6599da04
JM
543{
544 double result;
6599da04 545
bee6ab3e
ILT
546 floatformat_to_double (&floatformat_ieee_double_little, (char *) &n,
547 &result);
548 if ((n != result && (! isnan (n) || ! isnan (result)))
549 || (n < 0 && result >= 0)
550 || (n >= 0 && result < 0))
6599da04 551 printf ("Differ(to): %.20g -> %.20g\n", n, result);
bee6ab3e
ILT
552
553 floatformat_from_double (&floatformat_ieee_double_little, &n,
554 (char *) &result);
555 if ((n != result && (! isnan (n) || ! isnan (result)))
556 || (n < 0 && result >= 0)
557 || (n >= 0 && result < 0))
6599da04
JM
558 printf ("Differ(from): %.20g -> %.20g\n", n, result);
559
bee6ab3e
ILT
560#if 0
561 {
562 char exten[16];
563
564 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
565 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
566 if (n != result)
567 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
568 }
569#endif
6599da04
JM
570
571#if IEEE_DEBUG > 1
572 /* This is to be run on a host which uses 68881 format. */
573 {
574 long double ex = *(long double *)exten;
575 if (ex != n)
576 printf ("Differ(from vs. extended): %.20g\n", n);
577 }
578#endif
579}
580
581int
6da879de 582main (void)
6599da04 583{
bee6ab3e 584 ieee_test (0.0);
6599da04
JM
585 ieee_test (0.5);
586 ieee_test (256.0);
587 ieee_test (0.12345);
588 ieee_test (234235.78907234);
589 ieee_test (-512.0);
590 ieee_test (-0.004321);
bee6ab3e
ILT
591 ieee_test (1.2E-70);
592 ieee_test (1.2E-316);
593 ieee_test (4.9406564584124654E-324);
594 ieee_test (- 4.9406564584124654E-324);
595 ieee_test (- 0.0);
596 ieee_test (- INFINITY);
597 ieee_test (- NAN);
598 ieee_test (INFINITY);
599 ieee_test (NAN);
6599da04
JM
600 return 0;
601}
602#endif