1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994 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 (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 || 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
)
195 if (fmt
->intbit
== floatformat_intbit_no
)
196 dto
= ldexp (1.0, exponent
);
200 while (mant_bits_left
> 0)
202 mant_bits
= min (mant_bits_left
, 32);
204 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
205 mant_off
, mant_bits
);
207 dto
+= ldexp ((double)mant
, exponent
- mant_bits
);
208 exponent
-= mant_bits
;
209 mant_off
+= mant_bits
;
210 mant_bits_left
-= mant_bits
;
213 /* Negate it if negative. */
214 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
219 static void put_field
PARAMS ((unsigned char *, enum floatformat_byteorders
,
225 /* Set a field which starts at START and is LEN bytes long. DATA and
226 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
228 put_field (data
, order
, total_len
, start
, len
, stuff_to_put
)
230 enum floatformat_byteorders order
;
231 unsigned int total_len
;
234 unsigned long stuff_to_put
;
236 unsigned int cur_byte
;
239 /* Start at the least significant part of the field. */
240 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
241 if (order
== floatformat_little
)
242 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
) - cur_byte
- 1;
244 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
245 *(data
+ cur_byte
) &=
246 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1) << (-cur_bitshift
));
247 *(data
+ cur_byte
) |=
248 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
249 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
250 if (order
== floatformat_little
)
255 /* Move towards the most significant part of the field. */
256 while (cur_bitshift
< len
)
258 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
260 /* This is the last byte. */
261 *(data
+ cur_byte
) &=
262 ~((1 << (len
- cur_bitshift
)) - 1);
263 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
266 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
267 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
268 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
269 if (order
== floatformat_little
)
276 /* The converse: convert the double *FROM to an extended float
277 and store where TO points. Neither FROM nor TO have any alignment
281 floatformat_from_double (fmt
, from
, to
)
282 CONST
struct floatformat
*fmt
;
289 unsigned int mant_bits
, mant_off
;
291 unsigned char *uto
= (unsigned char *)to
;
293 memcpy (&dfrom
, from
, sizeof (dfrom
));
294 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
296 return; /* Result is zero */
300 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
301 fmt
->exp_len
, fmt
->exp_nan
);
302 /* Be sure it's not infinity, but NaN value is irrel */
303 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
308 /* If negative, set the sign bit. */
311 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
315 /* How to tell an infinity from an ordinary number? FIXME-someday */
317 mant
= frexp (dfrom
, &exponent
);
318 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
319 exponent
+ fmt
->exp_bias
- 1);
321 mant_bits_left
= fmt
->man_len
;
322 mant_off
= fmt
->man_start
;
323 while (mant_bits_left
> 0)
325 unsigned long mant_long
;
326 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
328 mant
*= 4294967296.0;
329 mant_long
= (unsigned long)mant
;
332 /* If the integer bit is implicit, then we need to discard it.
333 If we are discarding a zero, we should be (but are not) creating
334 a denormalized number which means adjusting the exponent
336 if (mant_bits_left
== fmt
->man_len
337 && fmt
->intbit
== floatformat_intbit_no
)
339 mant_long
&= 0x7fffffff;
342 else if (mant_bits
< 32)
344 /* The bits we want are in the most significant MANT_BITS bits of
345 mant_long. Move them to the least significant. */
346 mant_long
>>= 32 - mant_bits
;
349 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
350 mant_off
, mant_bits
, mant_long
);
351 mant_off
+= mant_bits
;
352 mant_bits_left
-= mant_bits
;
359 /* This is to be run on a host which uses IEEE floating point. */
368 floatformat_to_double (&floatformat_ieee_double_big
, &n
, &result
);
370 printf ("Differ(to): %.20g -> %.20g\n", n
, result
);
371 floatformat_from_double (&floatformat_ieee_double_big
, &n
, &result
);
373 printf ("Differ(from): %.20g -> %.20g\n", n
, result
);
375 floatformat_from_double (&floatformat_m68881_ext
, &n
, exten
);
376 floatformat_to_double (&floatformat_m68881_ext
, exten
, &result
);
378 printf ("Differ(to+from): %.20g -> %.20g\n", n
, result
);
381 /* This is to be run on a host which uses 68881 format. */
383 long double ex
= *(long double *)exten
;
385 printf ("Differ(from vs. extended): %.20g\n", n
);
396 ieee_test (234235.78907234);
398 ieee_test (-0.004321);