Index: include/ChangeLog
[deliverable/binutils-gdb.git] / libiberty / floatformat.c
1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994, 1999, 2000, 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "ansidecl.h"
21 #include "floatformat.h"
22 #include <math.h> /* ldexp */
23 #ifdef ANSI_PROTOTYPES
24 #include <stddef.h>
25 extern void *memcpy (void *s1, const void *s2, size_t n);
26 extern void *memset (void *s, int c, size_t n);
27 #else
28 extern char *memcpy ();
29 extern char *memset ();
30 #endif
31
32 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
33 going to bother with trying to muck around with whether it is defined in
34 a system header, what we do if not, etc. */
35 #define FLOATFORMAT_CHAR_BIT 8
36
37 /* floatformats for IEEE single and double, big and little endian. */
38 const struct floatformat floatformat_ieee_single_big =
39 {
40 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
41 floatformat_intbit_no,
42 "floatformat_ieee_single_big"
43 };
44 const struct floatformat floatformat_ieee_single_little =
45 {
46 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
47 floatformat_intbit_no,
48 "floatformat_ieee_single_little"
49 };
50 const struct floatformat floatformat_ieee_double_big =
51 {
52 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
53 floatformat_intbit_no,
54 "floatformat_ieee_double_big"
55 };
56 const struct floatformat floatformat_ieee_double_little =
57 {
58 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
59 floatformat_intbit_no,
60 "floatformat_ieee_double_little"
61 };
62
63 /* floatformat for IEEE double, little endian byte order, with big endian word
64 ordering, as on the ARM. */
65
66 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
67 {
68 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
69 floatformat_intbit_no,
70 "floatformat_ieee_double_littlebyte_bigword"
71 };
72
73 const struct floatformat floatformat_i387_ext =
74 {
75 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
76 floatformat_intbit_yes,
77 "floatformat_i387_ext"
78 };
79 const struct floatformat floatformat_m68881_ext =
80 {
81 /* Note that the bits from 16 to 31 are unused. */
82 floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
83 floatformat_intbit_yes,
84 "floatformat_m68881_ext"
85 };
86 const struct floatformat floatformat_i960_ext =
87 {
88 /* Note that the bits from 0 to 15 are unused. */
89 floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
90 floatformat_intbit_yes,
91 "floatformat_i960_ext"
92 };
93 const struct floatformat floatformat_m88110_ext =
94 {
95 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
96 floatformat_intbit_yes,
97 "floatformat_m88110_ext"
98 };
99 const struct floatformat floatformat_m88110_harris_ext =
100 {
101 /* Harris uses raw format 128 bytes long, but the number is just an ieee
102 double, and the last 64 bits are wasted. */
103 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
104 floatformat_intbit_no,
105 "floatformat_m88110_ext_harris"
106 };
107 const struct floatformat floatformat_arm_ext_big =
108 {
109 /* Bits 1 to 16 are unused. */
110 floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
111 floatformat_intbit_yes,
112 "floatformat_arm_ext_big"
113 };
114 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
115 {
116 /* Bits 1 to 16 are unused. */
117 floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
118 floatformat_intbit_yes,
119 "floatformat_arm_ext_littlebyte_bigword"
120 };
121 const struct floatformat floatformat_ia64_spill_big =
122 {
123 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
124 floatformat_intbit_yes,
125 "floatformat_ia64_spill_big"
126 };
127 const struct floatformat floatformat_ia64_spill_little =
128 {
129 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
130 floatformat_intbit_yes,
131 "floatformat_ia64_spill_little"
132 };
133 const struct floatformat floatformat_ia64_quad_big =
134 {
135 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
136 floatformat_intbit_no,
137 "floatformat_ia64_quad_big"
138 };
139 const struct floatformat floatformat_ia64_quad_little =
140 {
141 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
142 floatformat_intbit_no,
143 "floatformat_ia64_quad_little"
144 };
145 \f
146 static unsigned long get_field PARAMS ((const unsigned char *,
147 enum floatformat_byteorders,
148 unsigned int,
149 unsigned int,
150 unsigned int));
151
152 /* Extract a field which starts at START and is LEN bits long. DATA and
153 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
154 static unsigned long
155 get_field (data, order, total_len, start, len)
156 const unsigned char *data;
157 enum floatformat_byteorders order;
158 unsigned int total_len;
159 unsigned int start;
160 unsigned int len;
161 {
162 unsigned long result;
163 unsigned int cur_byte;
164 int cur_bitshift;
165
166 /* Start at the least significant part of the field. */
167 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
168 if (order == floatformat_little)
169 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
170 cur_bitshift =
171 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
172 result = *(data + cur_byte) >> (-cur_bitshift);
173 cur_bitshift += FLOATFORMAT_CHAR_BIT;
174 if (order == floatformat_little)
175 ++cur_byte;
176 else
177 --cur_byte;
178
179 /* Move towards the most significant part of the field. */
180 while ((unsigned int) cur_bitshift < len)
181 {
182 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
183 /* This is the last byte; zero out the bits which are not part of
184 this field. */
185 result |=
186 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
187 << cur_bitshift;
188 else
189 result |= *(data + cur_byte) << cur_bitshift;
190 cur_bitshift += FLOATFORMAT_CHAR_BIT;
191 if (order == floatformat_little)
192 ++cur_byte;
193 else
194 --cur_byte;
195 }
196 return result;
197 }
198
199 #ifndef min
200 #define min(a, b) ((a) < (b) ? (a) : (b))
201 #endif
202
203 /* Convert from FMT to a double.
204 FROM is the address of the extended float.
205 Store the double in *TO. */
206
207 void
208 floatformat_to_double (fmt, from, to)
209 const struct floatformat *fmt;
210 const char *from;
211 double *to;
212 {
213 const unsigned char *ufrom = (const unsigned char *)from;
214 double dto;
215 long exponent;
216 unsigned long mant;
217 unsigned int mant_bits, mant_off;
218 int mant_bits_left;
219 int special_exponent; /* It's a NaN, denorm or zero */
220
221 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
222 fmt->exp_start, fmt->exp_len);
223 /* Note that if exponent indicates a NaN, we can't really do anything useful
224 (not knowing if the host has NaN's, or how to build one). So it will
225 end up as an infinity or something close; that is OK. */
226
227 mant_bits_left = fmt->man_len;
228 mant_off = fmt->man_start;
229 dto = 0.0;
230
231 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
232
233 /* Don't bias zero's, denorms or NaNs. */
234 if (!special_exponent)
235 exponent -= fmt->exp_bias;
236
237 /* Build the result algebraically. Might go infinite, underflow, etc;
238 who cares. */
239
240 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
241 increment the exponent by one to account for the integer bit. */
242
243 if (!special_exponent)
244 {
245 if (fmt->intbit == floatformat_intbit_no)
246 dto = ldexp (1.0, exponent);
247 else
248 exponent++;
249 }
250
251 while (mant_bits_left > 0)
252 {
253 mant_bits = min (mant_bits_left, 32);
254
255 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
256 mant_off, mant_bits);
257
258 dto += ldexp ((double)mant, exponent - mant_bits);
259 exponent -= mant_bits;
260 mant_off += mant_bits;
261 mant_bits_left -= mant_bits;
262 }
263
264 /* Negate it if negative. */
265 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
266 dto = -dto;
267 *to = dto;
268 }
269 \f
270 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
271 unsigned int,
272 unsigned int,
273 unsigned int,
274 unsigned long));
275
276 /* Set a field which starts at START and is LEN bits long. DATA and
277 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
278 static void
279 put_field (data, order, total_len, start, len, stuff_to_put)
280 unsigned char *data;
281 enum floatformat_byteorders order;
282 unsigned int total_len;
283 unsigned int start;
284 unsigned int len;
285 unsigned long stuff_to_put;
286 {
287 unsigned int cur_byte;
288 int cur_bitshift;
289
290 /* Start at the least significant part of the field. */
291 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
292 if (order == floatformat_little)
293 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
294 cur_bitshift =
295 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
296 *(data + cur_byte) &=
297 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
298 *(data + cur_byte) |=
299 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
300 cur_bitshift += FLOATFORMAT_CHAR_BIT;
301 if (order == floatformat_little)
302 ++cur_byte;
303 else
304 --cur_byte;
305
306 /* Move towards the most significant part of the field. */
307 while ((unsigned int) cur_bitshift < len)
308 {
309 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
310 {
311 /* This is the last byte. */
312 *(data + cur_byte) &=
313 ~((1 << (len - cur_bitshift)) - 1);
314 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
315 }
316 else
317 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
318 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
319 cur_bitshift += FLOATFORMAT_CHAR_BIT;
320 if (order == floatformat_little)
321 ++cur_byte;
322 else
323 --cur_byte;
324 }
325 }
326
327 /* The converse: convert the double *FROM to an extended float
328 and store where TO points. Neither FROM nor TO have any alignment
329 restrictions. */
330
331 void
332 floatformat_from_double (fmt, from, to)
333 const struct floatformat *fmt;
334 const double *from;
335 char *to;
336 {
337 double dfrom;
338 int exponent;
339 double mant;
340 unsigned int mant_bits, mant_off;
341 int mant_bits_left;
342 unsigned char *uto = (unsigned char *)to;
343
344 memcpy (&dfrom, from, sizeof (dfrom));
345 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
346 if (dfrom == 0)
347 return; /* Result is zero */
348 if (dfrom != dfrom)
349 {
350 /* From is NaN */
351 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
352 fmt->exp_len, fmt->exp_nan);
353 /* Be sure it's not infinity, but NaN value is irrel */
354 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
355 32, 1);
356 return;
357 }
358
359 /* If negative, set the sign bit. */
360 if (dfrom < 0)
361 {
362 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
363 dfrom = -dfrom;
364 }
365
366 /* How to tell an infinity from an ordinary number? FIXME-someday */
367
368 mant = frexp (dfrom, &exponent);
369 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
370 exponent + fmt->exp_bias - 1);
371
372 mant_bits_left = fmt->man_len;
373 mant_off = fmt->man_start;
374 while (mant_bits_left > 0)
375 {
376 unsigned long mant_long;
377 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
378
379 mant *= 4294967296.0;
380 mant_long = (unsigned long)mant;
381 mant -= mant_long;
382
383 /* If the integer bit is implicit, then we need to discard it.
384 If we are discarding a zero, we should be (but are not) creating
385 a denormalized number which means adjusting the exponent
386 (I think). */
387 if ((unsigned int) mant_bits_left == fmt->man_len
388 && fmt->intbit == floatformat_intbit_no)
389 {
390 mant_long &= 0x7fffffff;
391 mant_bits -= 1;
392 }
393 else if (mant_bits < 32)
394 {
395 /* The bits we want are in the most significant MANT_BITS bits of
396 mant_long. Move them to the least significant. */
397 mant_long >>= 32 - mant_bits;
398 }
399
400 put_field (uto, fmt->byteorder, fmt->totalsize,
401 mant_off, mant_bits, mant_long);
402 mant_off += mant_bits;
403 mant_bits_left -= mant_bits;
404 }
405 }
406
407 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
408
409 int
410 floatformat_is_valid (fmt, from)
411 const struct floatformat *fmt;
412 const char *from;
413 {
414 if (fmt == &floatformat_i387_ext)
415 {
416 /* In the i387 double-extended format, if the exponent is all
417 ones, then the integer bit must be set. If the exponent
418 is neither 0 nor ~0, the intbit must also be set. Only
419 if the exponent is zero can it be zero, and then it must
420 be zero. */
421 unsigned long exponent, int_bit;
422 const unsigned char *ufrom = (const unsigned char *) from;
423
424 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
425 fmt->exp_start, fmt->exp_len);
426 int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
427 fmt->man_start, 1);
428
429 if ((exponent == 0) != (int_bit == 0))
430 return 0;
431 else
432 return 1;
433 }
434
435 /* Other formats with invalid representations should be added
436 here. */
437 return 1;
438 }
439
440
441 #ifdef IEEE_DEBUG
442
443 /* This is to be run on a host which uses IEEE floating point. */
444
445 void
446 ieee_test (n)
447 double n;
448 {
449 double result;
450 char exten[16];
451
452 floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
453 if (n != result)
454 printf ("Differ(to): %.20g -> %.20g\n", n, result);
455 floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
456 if (n != result)
457 printf ("Differ(from): %.20g -> %.20g\n", n, result);
458
459 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
460 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
461 if (n != result)
462 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
463
464 #if IEEE_DEBUG > 1
465 /* This is to be run on a host which uses 68881 format. */
466 {
467 long double ex = *(long double *)exten;
468 if (ex != n)
469 printf ("Differ(from vs. extended): %.20g\n", n);
470 }
471 #endif
472 }
473
474 int
475 main ()
476 {
477 ieee_test (0.5);
478 ieee_test (256.0);
479 ieee_test (0.12345);
480 ieee_test (234235.78907234);
481 ieee_test (-512.0);
482 ieee_test (-0.004321);
483 return 0;
484 }
485 #endif
This page took 0.044206 seconds and 5 git commands to generate.