1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994, 1999 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, floatformat_intbit_no
41 const struct floatformat floatformat_ieee_single_little
=
43 floatformat_little
, 32, 0, 1, 8, 127, 255, 9, 23, floatformat_intbit_no
45 const struct floatformat floatformat_ieee_double_big
=
47 floatformat_big
, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no
49 const struct floatformat floatformat_ieee_double_little
=
51 floatformat_little
, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no
54 /* floatformat for IEEE double, little endian byte order, with big endian word
55 ordering, as on the ARM. */
57 const struct floatformat floatformat_ieee_double_littlebyte_bigword
=
59 floatformat_littlebyte_bigword
, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no
62 const struct floatformat floatformat_i387_ext
=
64 floatformat_little
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
65 floatformat_intbit_yes
67 const struct floatformat floatformat_m68881_ext
=
69 /* Note that the bits from 16 to 31 are unused. */
70 floatformat_big
, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, floatformat_intbit_yes
72 const struct floatformat floatformat_i960_ext
=
74 /* Note that the bits from 0 to 15 are unused. */
75 floatformat_little
, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
76 floatformat_intbit_yes
78 const struct floatformat floatformat_m88110_ext
=
80 #ifdef HARRIS_FLOAT_FORMAT
81 /* Harris uses raw format 128 bytes long, but the number is just an ieee
82 double, and the last 64 bits are wasted. */
83 floatformat_big
,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
86 floatformat_big
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
87 floatformat_intbit_yes
88 #endif /* HARRIS_FLOAT_FORMAT */
90 const struct floatformat floatformat_arm_ext
=
92 /* Bits 1 to 16 are unused. */
93 floatformat_big
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
94 floatformat_intbit_yes
97 static unsigned long get_field
PARAMS ((unsigned char *,
98 enum floatformat_byteorders
,
103 /* Extract a field which starts at START and is LEN bytes long. DATA and
104 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
106 get_field (data
, order
, total_len
, start
, len
)
108 enum floatformat_byteorders order
;
109 unsigned int total_len
;
113 unsigned long result
;
114 unsigned int cur_byte
;
117 /* Start at the least significant part of the field. */
118 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
119 if (order
== floatformat_little
)
120 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
122 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
123 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
124 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
125 if (order
== floatformat_little
)
130 /* Move towards the most significant part of the field. */
131 while ((unsigned int) cur_bitshift
< len
)
133 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
134 /* This is the last byte; zero out the bits which are not part of
137 (*(data
+ cur_byte
) & ((1 << (len
- cur_bitshift
)) - 1))
140 result
|= *(data
+ cur_byte
) << cur_bitshift
;
141 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
142 if (order
== floatformat_little
)
151 #define min(a, b) ((a) < (b) ? (a) : (b))
154 /* Convert from FMT to a double.
155 FROM is the address of the extended float.
156 Store the double in *TO. */
159 floatformat_to_double (fmt
, from
, to
)
160 const struct floatformat
*fmt
;
164 unsigned char *ufrom
= (unsigned char *)from
;
168 unsigned int mant_bits
, mant_off
;
170 int special_exponent
; /* It's a NaN, denorm or zero */
172 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
173 fmt
->exp_start
, fmt
->exp_len
);
174 /* Note that if exponent indicates a NaN, we can't really do anything useful
175 (not knowing if the host has NaN's, or how to build one). So it will
176 end up as an infinity or something close; that is OK. */
178 mant_bits_left
= fmt
->man_len
;
179 mant_off
= fmt
->man_start
;
182 special_exponent
= exponent
== 0 || (unsigned long) exponent
== fmt
->exp_nan
;
184 /* Don't bias zero's, denorms or NaNs. */
185 if (!special_exponent
)
186 exponent
-= fmt
->exp_bias
;
188 /* Build the result algebraically. Might go infinite, underflow, etc;
191 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
192 increment the exponent by one to account for the integer bit. */
194 if (!special_exponent
)
196 if (fmt
->intbit
== floatformat_intbit_no
)
197 dto
= ldexp (1.0, exponent
);
202 while (mant_bits_left
> 0)
204 mant_bits
= min (mant_bits_left
, 32);
206 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
207 mant_off
, mant_bits
);
209 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
210 exponent
-= mant_bits
;
211 mant_off
+= mant_bits
;
212 mant_bits_left
-= mant_bits
;
215 /* Negate it if negative. */
216 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
221 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
227 /* Set a field which starts at START and is LEN bytes long. DATA and
228 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
230 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
232 enum floatformat_byteorders order
;
233 unsigned int total_len
;
236 unsigned long stuff_to_put
;
238 unsigned int cur_byte
;
241 /* Start at the least significant part of the field. */
242 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
243 if (order
== floatformat_little
)
244 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
246 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
247 *(data
+ cur_byte
) &=
248 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
249 *(data
+ cur_byte
) |=
250 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
251 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
252 if (order
== floatformat_little
)
257 /* Move towards the most significant part of the field. */
258 while ((unsigned int) cur_bitshift
< len
)
260 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
262 /* This is the last byte. */
263 *(data
+ cur_byte
) &=
264 ~((1 << (len
- cur_bitshift
)) - 1);
265 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
268 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
269 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
270 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
271 if (order
== floatformat_little
)
278 /* The converse: convert the double *FROM to an extended float
279 and store where TO points. Neither FROM nor TO have any alignment
283 floatformat_from_double (fmt
, from
, to
)
284 const struct floatformat
*fmt
;
291 unsigned int mant_bits
, mant_off
;
293 unsigned char *uto
= (unsigned char *)to
;
295 memcpy (&dfrom
, from
, sizeof (dfrom
));
296 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
298 return; /* Result is zero */
302 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
303 fmt
->exp_len
, fmt
->exp_nan
);
304 /* Be sure it's not infinity, but NaN value is irrel */
305 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
310 /* If negative, set the sign bit. */
313 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
317 /* How to tell an infinity from an ordinary number? FIXME-someday */
319 mant
= frexp (dfrom
, &exponent
);
320 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
321 exponent
+ fmt
->exp_bias
- 1);
323 mant_bits_left
= fmt
->man_len
;
324 mant_off
= fmt
->man_start
;
325 while (mant_bits_left
> 0)
327 unsigned long mant_long
;
328 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
330 mant
*= 4294967296.0;
331 mant_long
= (unsigned long)mant
;
334 /* If the integer bit is implicit, then we need to discard it.
335 If we are discarding a zero, we should be (but are not) creating
336 a denormalized number which means adjusting the exponent
338 if ((unsigned int) mant_bits_left
== fmt
->man_len
339 && fmt
->intbit
== floatformat_intbit_no
)
341 mant_long
&= 0x7fffffff;
344 else if (mant_bits
< 32)
346 /* The bits we want are in the most significant MANT_BITS bits of
347 mant_long. Move them to the least significant. */
348 mant_long
>>= 32 - mant_bits
;
351 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
352 mant_off
, mant_bits
, mant_long
);
353 mant_off
+= mant_bits
;
354 mant_bits_left
-= mant_bits
;
361 /* This is to be run on a host which uses IEEE floating point. */
370 floatformat_to_double (&floatformat_ieee_double_big
, &n
, &result
);
372 printf ("Differ(to): %.20g -> %.20g\n", n
, result
);
373 floatformat_from_double (&floatformat_ieee_double_big
, &n
, &result
);
375 printf ("Differ(from): %.20g -> %.20g\n", n
, result
);
377 floatformat_from_double (&floatformat_m68881_ext
, &n
, exten
);
378 floatformat_to_double (&floatformat_m68881_ext
, exten
, &result
);
380 printf ("Differ(to+from): %.20g -> %.20g\n", n
, result
);
383 /* This is to be run on a host which uses 68881 format. */
385 long double ex
= *(long double *)exten
;
387 printf ("Differ(from vs. extended): %.20g\n", n
);
398 ieee_test (234235.78907234);
400 ieee_test (-0.004321);