* ld-selective/3.cc (start): New function.
[binutils.git] / libiberty / floatformat.c
blobd69024f739026dceaa6f37b3832ba9461e7a8206
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 "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
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
37 /* floatformats for IEEE single and double, big and little endian. */
38 const struct floatformat floatformat_ieee_single_big =
40 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
41 floatformat_intbit_no,
42 "floatformat_ieee_single_big"
44 const struct floatformat floatformat_ieee_single_little =
46 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
47 floatformat_intbit_no,
48 "floatformat_ieee_single_little"
50 const struct floatformat floatformat_ieee_double_big =
52 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
53 floatformat_intbit_no,
54 "floatformat_ieee_double_big"
56 const struct floatformat floatformat_ieee_double_little =
58 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
59 floatformat_intbit_no,
60 "floatformat_ieee_double_little"
63 /* floatformat for IEEE double, little endian byte order, with big endian word
64 ordering, as on the ARM. */
66 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
68 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
69 floatformat_intbit_no,
70 "floatformat_ieee_double_littlebyte_bigword"
73 const struct floatformat floatformat_i387_ext =
75 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
76 floatformat_intbit_yes,
77 "floatformat_i387_ext"
79 const struct floatformat floatformat_m68881_ext =
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"
86 const struct floatformat floatformat_i960_ext =
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"
93 const struct floatformat floatformat_m88110_ext =
95 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
96 floatformat_intbit_yes,
97 "floatformat_m88110_ext"
99 const struct floatformat floatformat_m88110_harris_ext =
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"
107 const struct floatformat floatformat_arm_ext_big =
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"
114 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
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"
121 const struct floatformat floatformat_ia64_spill_big =
123 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
124 floatformat_intbit_yes,
125 "floatformat_ia64_spill_big"
127 const struct floatformat floatformat_ia64_spill_little =
129 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
130 floatformat_intbit_yes,
131 "floatformat_ia64_spill_little"
133 const struct floatformat floatformat_ia64_quad_big =
135 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
136 floatformat_intbit_no,
137 "floatformat_ia64_quad_big"
139 const struct floatformat floatformat_ia64_quad_little =
141 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
142 floatformat_intbit_no,
143 "floatformat_ia64_quad_little"
146 static unsigned long get_field PARAMS ((unsigned char *,
147 enum floatformat_byteorders,
148 unsigned int,
149 unsigned int,
150 unsigned int));
152 /* Extract a field which starts at START and is LEN bytes 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 unsigned char *data;
157 enum floatformat_byteorders order;
158 unsigned int total_len;
159 unsigned int start;
160 unsigned int len;
162 unsigned long result;
163 unsigned int cur_byte;
164 int cur_bitshift;
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;
179 /* Move towards the most significant part of the field. */
180 while ((unsigned int) cur_bitshift < len)
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;
196 return result;
199 #ifndef min
200 #define min(a, b) ((a) < (b) ? (a) : (b))
201 #endif
203 /* Convert from FMT to a double.
204 FROM is the address of the extended float.
205 Store the double in *TO. */
207 void
208 floatformat_to_double (fmt, from, to)
209 const struct floatformat *fmt;
210 char *from;
211 double *to;
213 unsigned char *ufrom = (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 */
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. */
227 mant_bits_left = fmt->man_len;
228 mant_off = fmt->man_start;
229 dto = 0.0;
231 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
233 /* Don't bias zero's, denorms or NaNs. */
234 if (!special_exponent)
235 exponent -= fmt->exp_bias;
237 /* Build the result algebraically. Might go infinite, underflow, etc;
238 who cares. */
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. */
243 if (!special_exponent)
245 if (fmt->intbit == floatformat_intbit_no)
246 dto = ldexp (1.0, exponent);
247 else
248 exponent++;
251 while (mant_bits_left > 0)
253 mant_bits = min (mant_bits_left, 32);
255 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
256 mant_off, mant_bits);
258 dto += ldexp ((double)mant, exponent - mant_bits);
259 exponent -= mant_bits;
260 mant_off += mant_bits;
261 mant_bits_left -= mant_bits;
264 /* Negate it if negative. */
265 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
266 dto = -dto;
267 *to = dto;
270 static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
271 unsigned int,
272 unsigned int,
273 unsigned int,
274 unsigned long));
276 /* Set a field which starts at START and is LEN bytes 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;
287 unsigned int cur_byte;
288 int cur_bitshift;
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;
306 /* Move towards the most significant part of the field. */
307 while ((unsigned int) cur_bitshift < len)
309 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
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);
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;
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. */
331 void
332 floatformat_from_double (fmt, from, to)
333 const struct floatformat *fmt;
334 double *from;
335 char *to;
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;
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)
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;
359 /* If negative, set the sign bit. */
360 if (dfrom < 0)
362 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
363 dfrom = -dfrom;
366 /* How to tell an infinity from an ordinary number? FIXME-someday */
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);
372 mant_bits_left = fmt->man_len;
373 mant_off = fmt->man_start;
374 while (mant_bits_left > 0)
376 unsigned long mant_long;
377 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
379 mant *= 4294967296.0;
380 mant_long = (unsigned long)mant;
381 mant -= mant_long;
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)
390 mant_long &= 0x7fffffff;
391 mant_bits -= 1;
393 else if (mant_bits < 32)
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;
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;
408 #ifdef IEEE_DEBUG
410 /* This is to be run on a host which uses IEEE floating point. */
412 void
413 ieee_test (n)
414 double n;
416 double result;
417 char exten[16];
419 floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
420 if (n != result)
421 printf ("Differ(to): %.20g -> %.20g\n", n, result);
422 floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
423 if (n != result)
424 printf ("Differ(from): %.20g -> %.20g\n", n, result);
426 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
427 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
428 if (n != result)
429 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
431 #if IEEE_DEBUG > 1
432 /* This is to be run on a host which uses 68881 format. */
434 long double ex = *(long double *)exten;
435 if (ex != n)
436 printf ("Differ(from vs. extended): %.20g\n", n);
438 #endif
442 main ()
444 ieee_test (0.5);
445 ieee_test (256.0);
446 ieee_test (0.12345);
447 ieee_test (234235.78907234);
448 ieee_test (-512.0);
449 ieee_test (-0.004321);
450 return 0;
452 #endif