1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994, 1999, 2000, 2003 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* This is needed to pick up the NAN macro on some systems. */
34 #include "libiberty.h"
35 #include "floatformat.h"
39 #define INFINITY HUGE_VAL
41 #define INFINITY (1.0 / 0.0)
46 #define NAN (0.0 / 0.0)
49 static unsigned long get_field
PARAMS ((const unsigned char *,
50 enum floatformat_byteorders
,
54 static int floatformat_always_valid
PARAMS ((const struct floatformat
*fmt
,
58 floatformat_always_valid (fmt
, from
)
59 const struct floatformat
*fmt ATTRIBUTE_UNUSED
;
60 const char *from ATTRIBUTE_UNUSED
;
65 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
66 going to bother with trying to muck around with whether it is defined in
67 a system header, what we do if not, etc. */
68 #define FLOATFORMAT_CHAR_BIT 8
70 /* floatformats for IEEE single and double, big and little endian. */
71 const struct floatformat floatformat_ieee_single_big
=
73 floatformat_big
, 32, 0, 1, 8, 127, 255, 9, 23,
74 floatformat_intbit_no
,
75 "floatformat_ieee_single_big",
76 floatformat_always_valid
78 const struct floatformat floatformat_ieee_single_little
=
80 floatformat_little
, 32, 0, 1, 8, 127, 255, 9, 23,
81 floatformat_intbit_no
,
82 "floatformat_ieee_single_little",
83 floatformat_always_valid
85 const struct floatformat floatformat_ieee_double_big
=
87 floatformat_big
, 64, 0, 1, 11, 1023, 2047, 12, 52,
88 floatformat_intbit_no
,
89 "floatformat_ieee_double_big",
90 floatformat_always_valid
92 const struct floatformat floatformat_ieee_double_little
=
94 floatformat_little
, 64, 0, 1, 11, 1023, 2047, 12, 52,
95 floatformat_intbit_no
,
96 "floatformat_ieee_double_little",
97 floatformat_always_valid
100 /* floatformat for IEEE double, little endian byte order, with big endian word
101 ordering, as on the ARM. */
103 const struct floatformat floatformat_ieee_double_littlebyte_bigword
=
105 floatformat_littlebyte_bigword
, 64, 0, 1, 11, 1023, 2047, 12, 52,
106 floatformat_intbit_no
,
107 "floatformat_ieee_double_littlebyte_bigword",
108 floatformat_always_valid
111 static int floatformat_i387_ext_is_valid
PARAMS ((const struct floatformat
*fmt
, const char *from
));
114 floatformat_i387_ext_is_valid (fmt
, from
)
115 const struct floatformat
*fmt
;
118 /* In the i387 double-extended format, if the exponent is all ones,
119 then the integer bit must be set. If the exponent is neither 0
120 nor ~0, the intbit must also be set. Only if the exponent is
121 zero can it be zero, and then it must be zero. */
122 unsigned long exponent
, int_bit
;
123 const unsigned char *ufrom
= (const unsigned char *) from
;
125 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
126 fmt
->exp_start
, fmt
->exp_len
);
127 int_bit
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
130 if ((exponent
== 0) != (int_bit
== 0))
136 const struct floatformat floatformat_i387_ext
=
138 floatformat_little
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
139 floatformat_intbit_yes
,
140 "floatformat_i387_ext",
141 floatformat_i387_ext_is_valid
143 const struct floatformat floatformat_m68881_ext
=
145 /* Note that the bits from 16 to 31 are unused. */
146 floatformat_big
, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
147 floatformat_intbit_yes
,
148 "floatformat_m68881_ext",
149 floatformat_always_valid
151 const struct floatformat floatformat_i960_ext
=
153 /* Note that the bits from 0 to 15 are unused. */
154 floatformat_little
, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
155 floatformat_intbit_yes
,
156 "floatformat_i960_ext",
157 floatformat_always_valid
159 const struct floatformat floatformat_m88110_ext
=
161 floatformat_big
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
162 floatformat_intbit_yes
,
163 "floatformat_m88110_ext",
164 floatformat_always_valid
166 const struct floatformat floatformat_m88110_harris_ext
=
168 /* Harris uses raw format 128 bytes long, but the number is just an ieee
169 double, and the last 64 bits are wasted. */
170 floatformat_big
,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
171 floatformat_intbit_no
,
172 "floatformat_m88110_ext_harris",
173 floatformat_always_valid
175 const struct floatformat floatformat_arm_ext_big
=
177 /* Bits 1 to 16 are unused. */
178 floatformat_big
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
179 floatformat_intbit_yes
,
180 "floatformat_arm_ext_big",
181 floatformat_always_valid
183 const struct floatformat floatformat_arm_ext_littlebyte_bigword
=
185 /* Bits 1 to 16 are unused. */
186 floatformat_littlebyte_bigword
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
187 floatformat_intbit_yes
,
188 "floatformat_arm_ext_littlebyte_bigword",
189 floatformat_always_valid
191 const struct floatformat floatformat_ia64_spill_big
=
193 floatformat_big
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
194 floatformat_intbit_yes
,
195 "floatformat_ia64_spill_big",
196 floatformat_always_valid
198 const struct floatformat floatformat_ia64_spill_little
=
200 floatformat_little
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
201 floatformat_intbit_yes
,
202 "floatformat_ia64_spill_little",
203 floatformat_always_valid
205 const struct floatformat floatformat_ia64_quad_big
=
207 floatformat_big
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
208 floatformat_intbit_no
,
209 "floatformat_ia64_quad_big",
210 floatformat_always_valid
212 const struct floatformat floatformat_ia64_quad_little
=
214 floatformat_little
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
215 floatformat_intbit_no
,
216 "floatformat_ia64_quad_little",
217 floatformat_always_valid
220 /* Extract a field which starts at START and is LEN bits long. DATA and
221 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
223 get_field (data
, order
, total_len
, start
, len
)
224 const unsigned char *data
;
225 enum floatformat_byteorders order
;
226 unsigned int total_len
;
230 unsigned long result
;
231 unsigned int cur_byte
;
234 /* Start at the least significant part of the field. */
235 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
236 if (order
== floatformat_little
)
237 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
239 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
240 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
241 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
242 if (order
== floatformat_little
)
247 /* Move towards the most significant part of the field. */
248 while ((unsigned int) cur_bitshift
< len
)
250 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
251 /* This is the last byte; zero out the bits which are not part of
254 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
257 result
|= *(data
+ cur_byte
) << cur_bitshift
;
258 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
259 if (order
== floatformat_little
)
268 #define min(a, b) ((a) < (b) ? (a) : (b))
271 /* Convert from FMT to a double.
272 FROM is the address of the extended float.
273 Store the double in *TO. */
276 floatformat_to_double (fmt
, from
, to
)
277 const struct floatformat
*fmt
;
281 const unsigned char *ufrom
= (const unsigned char *)from
;
285 unsigned int mant_bits
, mant_off
;
287 int special_exponent
; /* It's a NaN, denorm or zero */
289 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
290 fmt
->exp_start
, fmt
->exp_len
);
292 /* If the exponent indicates a NaN, we don't have information to
293 decide what to do. So we handle it like IEEE, except that we
294 don't try to preserve the type of NaN. FIXME. */
295 if ((unsigned long) exponent
== fmt
->exp_nan
)
299 mant_off
= fmt
->man_start
;
300 mant_bits_left
= fmt
->man_len
;
302 while (mant_bits_left
> 0)
304 mant_bits
= min (mant_bits_left
, 32);
306 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
307 mant_off
, mant_bits
) != 0)
314 mant_off
+= mant_bits
;
315 mant_bits_left
-= mant_bits
;
323 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
331 mant_bits_left
= fmt
->man_len
;
332 mant_off
= fmt
->man_start
;
335 special_exponent
= exponent
== 0 || (unsigned long) exponent
== fmt
->exp_nan
;
337 /* Don't bias zero's, denorms or NaNs. */
338 if (!special_exponent
)
339 exponent
-= fmt
->exp_bias
;
341 /* Build the result algebraically. Might go infinite, underflow, etc;
344 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
345 increment the exponent by one to account for the integer bit. */
347 if (!special_exponent
)
349 if (fmt
->intbit
== floatformat_intbit_no
)
350 dto
= ldexp (1.0, exponent
);
355 while (mant_bits_left
> 0)
357 mant_bits
= min (mant_bits_left
, 32);
359 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
360 mant_off
, mant_bits
);
362 /* Handle denormalized numbers. FIXME: What should we do for
364 if (exponent
== 0 && mant
!= 0)
365 dto
+= ldexp ((double)mant
,
368 - (mant_off
- fmt
->man_start
)
371 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
373 exponent
-= mant_bits
;
374 mant_off
+= mant_bits
;
375 mant_bits_left
-= mant_bits
;
378 /* Negate it if negative. */
379 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
384 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
390 /* Set a field which starts at START and is LEN bits long. DATA and
391 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
393 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
395 enum floatformat_byteorders order
;
396 unsigned int total_len
;
399 unsigned long stuff_to_put
;
401 unsigned int cur_byte
;
404 /* Start at the least significant part of the field. */
405 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
406 if (order
== floatformat_little
)
407 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
409 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
410 *(data
+ cur_byte
) &=
411 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
412 *(data
+ cur_byte
) |=
413 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
414 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
415 if (order
== floatformat_little
)
420 /* Move towards the most significant part of the field. */
421 while ((unsigned int) cur_bitshift
< len
)
423 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
425 /* This is the last byte. */
426 *(data
+ cur_byte
) &=
427 ~((1 << (len
- cur_bitshift
)) - 1);
428 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
431 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
432 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
433 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
434 if (order
== floatformat_little
)
441 /* The converse: convert the double *FROM to an extended float
442 and store where TO points. Neither FROM nor TO have any alignment
446 floatformat_from_double (fmt
, from
, to
)
447 const struct floatformat
*fmt
;
454 unsigned int mant_bits
, mant_off
;
456 unsigned char *uto
= (unsigned char *)to
;
459 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
461 /* If negative, set the sign bit. */
464 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
477 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
478 fmt
->exp_len
, fmt
->exp_nan
);
479 /* Be sure it's not infinity, but NaN value is irrelevant. */
480 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
485 if (dfrom
+ dfrom
== dfrom
)
487 /* This can only happen for an infinite value (or zero, which we
488 already handled above). */
489 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
490 fmt
->exp_len
, fmt
->exp_nan
);
494 mant
= frexp (dfrom
, &exponent
);
495 if (exponent
+ fmt
->exp_bias
- 1 > 0)
496 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
497 fmt
->exp_len
, exponent
+ fmt
->exp_bias
- 1);
500 /* Handle a denormalized number. FIXME: What should we do for
502 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
504 mant
= ldexp (mant
, exponent
+ fmt
->exp_bias
- 1);
507 mant_bits_left
= fmt
->man_len
;
508 mant_off
= fmt
->man_start
;
509 while (mant_bits_left
> 0)
511 unsigned long mant_long
;
512 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
514 mant
*= 4294967296.0;
515 mant_long
= (unsigned long)mant
;
518 /* If the integer bit is implicit, and we are not creating a
519 denormalized number, then we need to discard it. */
520 if ((unsigned int) mant_bits_left
== fmt
->man_len
521 && fmt
->intbit
== floatformat_intbit_no
522 && exponent
+ fmt
->exp_bias
- 1 > 0)
524 mant_long
&= 0x7fffffff;
527 else if (mant_bits
< 32)
529 /* The bits we want are in the most significant MANT_BITS bits of
530 mant_long. Move them to the least significant. */
531 mant_long
>>= 32 - mant_bits
;
534 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
535 mant_off
, mant_bits
, mant_long
);
536 mant_off
+= mant_bits
;
537 mant_bits_left
-= mant_bits
;
541 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
544 floatformat_is_valid (fmt
, from
)
545 const struct floatformat
*fmt
;
548 return fmt
->is_valid (fmt
, from
);
556 /* This is to be run on a host which uses IEEE floating point. */
564 floatformat_to_double (&floatformat_ieee_double_little
, (char *) &n
,
566 if ((n
!= result
&& (! isnan (n
) || ! isnan (result
)))
567 || (n
< 0 && result
>= 0)
568 || (n
>= 0 && result
< 0))
569 printf ("Differ(to): %.20g -> %.20g\n", n
, result
);
571 floatformat_from_double (&floatformat_ieee_double_little
, &n
,
573 if ((n
!= result
&& (! isnan (n
) || ! isnan (result
)))
574 || (n
< 0 && result
>= 0)
575 || (n
>= 0 && result
< 0))
576 printf ("Differ(from): %.20g -> %.20g\n", n
, result
);
582 floatformat_from_double (&floatformat_m68881_ext
, &n
, exten
);
583 floatformat_to_double (&floatformat_m68881_ext
, exten
, &result
);
585 printf ("Differ(to+from): %.20g -> %.20g\n", n
, result
);
590 /* This is to be run on a host which uses 68881 format. */
592 long double ex
= *(long double *)exten
;
594 printf ("Differ(from vs. extended): %.20g\n", n
);
606 ieee_test (234235.78907234);
608 ieee_test (-0.004321);
610 ieee_test (1.2E-316);
611 ieee_test (4.9406564584124654E-324);
612 ieee_test (- 4.9406564584124654E-324);
614 ieee_test (- INFINITY
);
616 ieee_test (INFINITY
);