* as.h (alloca): Don't declare if __GNUC__. Remove an old comment.
[binutils.git] / libiberty / floatformat.c
blob3cb78f51dde4c5df4e3f3336e66668373627f348
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 */
22 #ifdef __STDC__
23 #include <stddef.h>
24 extern void *memcpy (void *s1, const void *s2, size_t n);
25 extern void *memset (void *s, int c, size_t n);
26 #else
27 extern char *memcpy ();
28 extern char *memset ();
29 #endif
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_little"
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 #ifdef HARRIS_FLOAT_FORMAT
95 /* Harris uses raw format 128 bytes long, but the number is just an ieee
96 double, and the last 64 bits are wasted. */
97 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
98 floatformat_intbit_no,
99 "floatformat_m88110_ext(harris)"
100 #else
101 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
102 floatformat_intbit_yes,
103 "floatformat_m88110_ext"
104 #endif /* HARRIS_FLOAT_FORMAT */
106 const struct floatformat floatformat_arm_ext =
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"
114 static unsigned long get_field PARAMS ((unsigned char *,
115 enum floatformat_byteorders,
116 unsigned int,
117 unsigned int,
118 unsigned int));
120 /* Extract a field which starts at START and is LEN bytes long. DATA and
121 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
122 static unsigned long
123 get_field (data, order, total_len, start, len)
124 unsigned char *data;
125 enum floatformat_byteorders order;
126 unsigned int total_len;
127 unsigned int start;
128 unsigned int len;
130 unsigned long result;
131 unsigned int cur_byte;
132 int cur_bitshift;
134 /* Start at the least significant part of the field. */
135 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
136 if (order == floatformat_little)
137 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
138 cur_bitshift =
139 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
140 result = *(data + cur_byte) >> (-cur_bitshift);
141 cur_bitshift += FLOATFORMAT_CHAR_BIT;
142 if (order == floatformat_little)
143 ++cur_byte;
144 else
145 --cur_byte;
147 /* Move towards the most significant part of the field. */
148 while ((unsigned int) cur_bitshift < len)
150 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
151 /* This is the last byte; zero out the bits which are not part of
152 this field. */
153 result |=
154 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
155 << cur_bitshift;
156 else
157 result |= *(data + cur_byte) << cur_bitshift;
158 cur_bitshift += FLOATFORMAT_CHAR_BIT;
159 if (order == floatformat_little)
160 ++cur_byte;
161 else
162 --cur_byte;
164 return result;
167 #ifndef min
168 #define min(a, b) ((a) < (b) ? (a) : (b))
169 #endif
171 /* Convert from FMT to a double.
172 FROM is the address of the extended float.
173 Store the double in *TO. */
175 void
176 floatformat_to_double (fmt, from, to)
177 const struct floatformat *fmt;
178 char *from;
179 double *to;
181 unsigned char *ufrom = (unsigned char *)from;
182 double dto;
183 long exponent;
184 unsigned long mant;
185 unsigned int mant_bits, mant_off;
186 int mant_bits_left;
187 int special_exponent; /* It's a NaN, denorm or zero */
189 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
190 fmt->exp_start, fmt->exp_len);
191 /* Note that if exponent indicates a NaN, we can't really do anything useful
192 (not knowing if the host has NaN's, or how to build one). So it will
193 end up as an infinity or something close; that is OK. */
195 mant_bits_left = fmt->man_len;
196 mant_off = fmt->man_start;
197 dto = 0.0;
199 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
201 /* Don't bias zero's, denorms or NaNs. */
202 if (!special_exponent)
203 exponent -= fmt->exp_bias;
205 /* Build the result algebraically. Might go infinite, underflow, etc;
206 who cares. */
208 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
209 increment the exponent by one to account for the integer bit. */
211 if (!special_exponent)
213 if (fmt->intbit == floatformat_intbit_no)
214 dto = ldexp (1.0, exponent);
215 else
216 exponent++;
219 while (mant_bits_left > 0)
221 mant_bits = min (mant_bits_left, 32);
223 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
224 mant_off, mant_bits);
226 dto += ldexp ((double)mant, exponent - mant_bits);
227 exponent -= mant_bits;
228 mant_off += mant_bits;
229 mant_bits_left -= mant_bits;
232 /* Negate it if negative. */
233 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
234 dto = -dto;
235 *to = dto;
238 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
239 unsigned int,
240 unsigned int,
241 unsigned int,
242 unsigned long));
244 /* Set a field which starts at START and is LEN bytes long. DATA and
245 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
246 static void
247 put_field (data, order, total_len, start, len, stuff_to_put)
248 unsigned char *data;
249 enum floatformat_byteorders order;
250 unsigned int total_len;
251 unsigned int start;
252 unsigned int len;
253 unsigned long stuff_to_put;
255 unsigned int cur_byte;
256 int cur_bitshift;
258 /* Start at the least significant part of the field. */
259 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
260 if (order == floatformat_little)
261 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
262 cur_bitshift =
263 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
264 *(data + cur_byte) &=
265 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
266 *(data + cur_byte) |=
267 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
268 cur_bitshift += FLOATFORMAT_CHAR_BIT;
269 if (order == floatformat_little)
270 ++cur_byte;
271 else
272 --cur_byte;
274 /* Move towards the most significant part of the field. */
275 while ((unsigned int) cur_bitshift < len)
277 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
279 /* This is the last byte. */
280 *(data + cur_byte) &=
281 ~((1 << (len - cur_bitshift)) - 1);
282 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
284 else
285 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
286 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
287 cur_bitshift += FLOATFORMAT_CHAR_BIT;
288 if (order == floatformat_little)
289 ++cur_byte;
290 else
291 --cur_byte;
295 /* The converse: convert the double *FROM to an extended float
296 and store where TO points. Neither FROM nor TO have any alignment
297 restrictions. */
299 void
300 floatformat_from_double (fmt, from, to)
301 const struct floatformat *fmt;
302 double *from;
303 char *to;
305 double dfrom;
306 int exponent;
307 double mant;
308 unsigned int mant_bits, mant_off;
309 int mant_bits_left;
310 unsigned char *uto = (unsigned char *)to;
312 memcpy (&dfrom, from, sizeof (dfrom));
313 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
314 if (dfrom == 0)
315 return; /* Result is zero */
316 if (dfrom != dfrom)
318 /* From is NaN */
319 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
320 fmt->exp_len, fmt->exp_nan);
321 /* Be sure it's not infinity, but NaN value is irrel */
322 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
323 32, 1);
324 return;
327 /* If negative, set the sign bit. */
328 if (dfrom < 0)
330 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
331 dfrom = -dfrom;
334 /* How to tell an infinity from an ordinary number? FIXME-someday */
336 mant = frexp (dfrom, &exponent);
337 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
338 exponent + fmt->exp_bias - 1);
340 mant_bits_left = fmt->man_len;
341 mant_off = fmt->man_start;
342 while (mant_bits_left > 0)
344 unsigned long mant_long;
345 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
347 mant *= 4294967296.0;
348 mant_long = (unsigned long)mant;
349 mant -= mant_long;
351 /* If the integer bit is implicit, then we need to discard it.
352 If we are discarding a zero, we should be (but are not) creating
353 a denormalized number which means adjusting the exponent
354 (I think). */
355 if ((unsigned int) mant_bits_left == fmt->man_len
356 && fmt->intbit == floatformat_intbit_no)
358 mant_long &= 0x7fffffff;
359 mant_bits -= 1;
361 else if (mant_bits < 32)
363 /* The bits we want are in the most significant MANT_BITS bits of
364 mant_long. Move them to the least significant. */
365 mant_long >>= 32 - mant_bits;
368 put_field (uto, fmt->byteorder, fmt->totalsize,
369 mant_off, mant_bits, mant_long);
370 mant_off += mant_bits;
371 mant_bits_left -= mant_bits;
376 #ifdef IEEE_DEBUG
378 /* This is to be run on a host which uses IEEE floating point. */
380 void
381 ieee_test (n)
382 double n;
384 double result;
385 char exten[16];
387 floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
388 if (n != result)
389 printf ("Differ(to): %.20g -> %.20g\n", n, result);
390 floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
391 if (n != result)
392 printf ("Differ(from): %.20g -> %.20g\n", n, result);
394 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
395 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
396 if (n != result)
397 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
399 #if IEEE_DEBUG > 1
400 /* This is to be run on a host which uses 68881 format. */
402 long double ex = *(long double *)exten;
403 if (ex != n)
404 printf ("Differ(from vs. extended): %.20g\n", n);
406 #endif
410 main ()
412 ieee_test (0.5);
413 ieee_test (256.0);
414 ieee_test (0.12345);
415 ieee_test (234235.78907234);
416 ieee_test (-512.0);
417 ieee_test (-0.004321);
418 return 0;
420 #endif