1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994, 1999, 2000 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 #include "floatformat.h"
21 #include <math.h> /* ldexp */
24 extern void *memcpy (void *s1
, const void *s2
, size_t n
);
25 extern void *memset (void *s
, int c
, size_t n
);
27 extern char *memcpy ();
28 extern char *memset ();
31 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
32 going to bother with trying to muck around with whether it is defined in
33 a system header, what we do if not, etc. */
34 #define FLOATFORMAT_CHAR_BIT 8
36 /* floatformats for IEEE single and double, big and little endian. */
37 const struct floatformat floatformat_ieee_single_big
=
39 floatformat_big
, 32, 0, 1, 8, 127, 255, 9, 23,
40 floatformat_intbit_no
,
41 "floatformat_ieee_single_big"
43 const struct floatformat floatformat_ieee_single_little
=
45 floatformat_little
, 32, 0, 1, 8, 127, 255, 9, 23,
46 floatformat_intbit_no
,
47 "floatformat_ieee_single_little"
49 const struct floatformat floatformat_ieee_double_big
=
51 floatformat_big
, 64, 0, 1, 11, 1023, 2047, 12, 52,
52 floatformat_intbit_no
,
53 "floatformat_ieee_double_big"
55 const struct floatformat floatformat_ieee_double_little
=
57 floatformat_little
, 64, 0, 1, 11, 1023, 2047, 12, 52,
58 floatformat_intbit_no
,
59 "floatformat_ieee_double_little"
62 /* floatformat for IEEE double, little endian byte order, with big endian word
63 ordering, as on the ARM. */
65 const struct floatformat floatformat_ieee_double_littlebyte_bigword
=
67 floatformat_littlebyte_bigword
, 64, 0, 1, 11, 1023, 2047, 12, 52,
68 floatformat_intbit_no
,
69 "floatformat_ieee_double_littlebyte_bigword"
72 const struct floatformat floatformat_i387_ext
=
74 floatformat_little
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
75 floatformat_intbit_yes
,
76 "floatformat_i387_ext"
78 const struct floatformat floatformat_m68881_ext
=
80 /* Note that the bits from 16 to 31 are unused. */
81 floatformat_big
, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
82 floatformat_intbit_yes
,
83 "floatformat_m68881_ext"
85 const struct floatformat floatformat_i960_ext
=
87 /* Note that the bits from 0 to 15 are unused. */
88 floatformat_little
, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
89 floatformat_intbit_yes
,
90 "floatformat_i960_ext"
92 const struct floatformat floatformat_m88110_ext
=
94 floatformat_big
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
95 floatformat_intbit_yes
,
96 "floatformat_m88110_ext"
98 const struct floatformat floatformat_m88110_harris_ext
=
100 /* Harris uses raw format 128 bytes long, but the number is just an ieee
101 double, and the last 64 bits are wasted. */
102 floatformat_big
,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
103 floatformat_intbit_no
,
104 "floatformat_m88110_ext_harris"
106 const struct floatformat floatformat_arm_ext_big
=
108 /* Bits 1 to 16 are unused. */
109 floatformat_big
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
110 floatformat_intbit_yes
,
111 "floatformat_arm_ext_big"
113 const struct floatformat floatformat_arm_ext_littlebyte_bigword
=
115 /* Bits 1 to 16 are unused. */
116 floatformat_littlebyte_bigword
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
117 floatformat_intbit_yes
,
118 "floatformat_arm_ext_littlebyte_bigword"
120 const struct floatformat floatformat_ia64_spill_big
=
122 floatformat_big
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
123 floatformat_intbit_yes
,
124 "floatformat_ia64_spill_big"
126 const struct floatformat floatformat_ia64_spill_little
=
128 floatformat_little
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
129 floatformat_intbit_yes
,
130 "floatformat_ia64_spill_little"
132 const struct floatformat floatformat_ia64_quad_big
=
134 floatformat_big
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
135 floatformat_intbit_no
,
136 "floatformat_ia64_quad_big"
138 const struct floatformat floatformat_ia64_quad_little
=
140 floatformat_little
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
141 floatformat_intbit_no
,
142 "floatformat_ia64_quad_little"
145 static unsigned long get_field
PARAMS ((unsigned char *,
146 enum floatformat_byteorders
,
151 /* Extract a field which starts at START and is LEN bytes long. DATA and
152 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
154 get_field (data
, order
, total_len
, start
, len
)
156 enum floatformat_byteorders order
;
157 unsigned int total_len
;
161 unsigned long result
;
162 unsigned int cur_byte
;
165 /* Start at the least significant part of the field. */
166 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
167 if (order
== floatformat_little
)
168 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
170 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
171 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
172 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
173 if (order
== floatformat_little
)
178 /* Move towards the most significant part of the field. */
179 while ((unsigned int) cur_bitshift
< len
)
181 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
182 /* This is the last byte; zero out the bits which are not part of
185 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
188 result
|= *(data
+ cur_byte
) << cur_bitshift
;
189 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
190 if (order
== floatformat_little
)
199 #define min(a, b) ((a) < (b) ? (a) : (b))
202 /* Convert from FMT to a double.
203 FROM is the address of the extended float.
204 Store the double in *TO. */
207 floatformat_to_double (fmt
, from
, to
)
208 const struct floatformat
*fmt
;
212 unsigned char *ufrom
= (unsigned char *)from
;
216 unsigned int mant_bits
, mant_off
;
218 int special_exponent
; /* It's a NaN, denorm or zero */
220 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
221 fmt
->exp_start
, fmt
->exp_len
);
222 /* Note that if exponent indicates a NaN, we can't really do anything useful
223 (not knowing if the host has NaN's, or how to build one). So it will
224 end up as an infinity or something close; that is OK. */
226 mant_bits_left
= fmt
->man_len
;
227 mant_off
= fmt
->man_start
;
230 special_exponent
= exponent
== 0 || (unsigned long) exponent
== fmt
->exp_nan
;
232 /* Don't bias zero's, denorms or NaNs. */
233 if (!special_exponent
)
234 exponent
-= fmt
->exp_bias
;
236 /* Build the result algebraically. Might go infinite, underflow, etc;
239 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
240 increment the exponent by one to account for the integer bit. */
242 if (!special_exponent
)
244 if (fmt
->intbit
== floatformat_intbit_no
)
245 dto
= ldexp (1.0, exponent
);
250 while (mant_bits_left
> 0)
252 mant_bits
= min (mant_bits_left
, 32);
254 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
255 mant_off
, mant_bits
);
257 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
258 exponent
-= mant_bits
;
259 mant_off
+= mant_bits
;
260 mant_bits_left
-= mant_bits
;
263 /* Negate it if negative. */
264 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
269 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
275 /* Set a field which starts at START and is LEN bytes long. DATA and
276 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
278 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
280 enum floatformat_byteorders order
;
281 unsigned int total_len
;
284 unsigned long stuff_to_put
;
286 unsigned int cur_byte
;
289 /* Start at the least significant part of the field. */
290 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
291 if (order
== floatformat_little
)
292 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
294 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
295 *(data
+ cur_byte
) &=
296 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
297 *(data
+ cur_byte
) |=
298 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
299 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
300 if (order
== floatformat_little
)
305 /* Move towards the most significant part of the field. */
306 while ((unsigned int) cur_bitshift
< len
)
308 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
310 /* This is the last byte. */
311 *(data
+ cur_byte
) &=
312 ~((1 << (len
- cur_bitshift
)) - 1);
313 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
316 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
317 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
318 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
319 if (order
== floatformat_little
)
326 /* The converse: convert the double *FROM to an extended float
327 and store where TO points. Neither FROM nor TO have any alignment
331 floatformat_from_double (fmt
, from
, to
)
332 const struct floatformat
*fmt
;
339 unsigned int mant_bits
, mant_off
;
341 unsigned char *uto
= (unsigned char *)to
;
343 memcpy (&dfrom
, from
, sizeof (dfrom
));
344 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
346 return; /* Result is zero */
350 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
351 fmt
->exp_len
, fmt
->exp_nan
);
352 /* Be sure it's not infinity, but NaN value is irrel */
353 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
358 /* If negative, set the sign bit. */
361 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
365 /* How to tell an infinity from an ordinary number? FIXME-someday */
367 mant
= frexp (dfrom
, &exponent
);
368 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
369 exponent
+ fmt
->exp_bias
- 1);
371 mant_bits_left
= fmt
->man_len
;
372 mant_off
= fmt
->man_start
;
373 while (mant_bits_left
> 0)
375 unsigned long mant_long
;
376 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
378 mant
*= 4294967296.0;
379 mant_long
= (unsigned long)mant
;
382 /* If the integer bit is implicit, then we need to discard it.
383 If we are discarding a zero, we should be (but are not) creating
384 a denormalized number which means adjusting the exponent
386 if ((unsigned int) mant_bits_left
== fmt
->man_len
387 && fmt
->intbit
== floatformat_intbit_no
)
389 mant_long
&= 0x7fffffff;
392 else if (mant_bits
< 32)
394 /* The bits we want are in the most significant MANT_BITS bits of
395 mant_long. Move them to the least significant. */
396 mant_long
>>= 32 - mant_bits
;
399 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
400 mant_off
, mant_bits
, mant_long
);
401 mant_off
+= mant_bits
;
402 mant_bits_left
-= mant_bits
;
409 /* This is to be run on a host which uses IEEE floating point. */
418 floatformat_to_double (&floatformat_ieee_double_big
, &n
, &result
);
420 printf ("Differ(to): %.20g -> %.20g\n", n
, result
);
421 floatformat_from_double (&floatformat_ieee_double_big
, &n
, &result
);
423 printf ("Differ(from): %.20g -> %.20g\n", n
, result
);
425 floatformat_from_double (&floatformat_m68881_ext
, &n
, exten
);
426 floatformat_to_double (&floatformat_m68881_ext
, exten
, &result
);
428 printf ("Differ(to+from): %.20g -> %.20g\n", n
, result
);
431 /* This is to be run on a host which uses 68881 format. */
433 long double ex
= *(long double *)exten
;
435 printf ("Differ(from vs. extended): %.20g\n", n
);
446 ieee_test (234235.78907234);
448 ieee_test (-0.004321);