source: trunk/src/binutils/libiberty/floatformat.c@ 10

Last change on this file since 10 was 10, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.1 KB
Line 
1/* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991, 1994, 1999, 2000 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20#include "floatformat.h"
21#include <math.h> /* ldexp */
22#ifdef __STDC__
23#include <stddef.h>
24extern void *memcpy (void *s1, const void *s2, size_t n);
25extern void *memset (void *s, int c, size_t n);
26#else
27extern char *memcpy ();
28extern char *memset ();
29#endif
30
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
35
36/* floatformats for IEEE single and double, big and little endian. */
37const struct floatformat floatformat_ieee_single_big =
38{
39 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
40 floatformat_intbit_no,
41 "floatformat_ieee_single_big"
42};
43const struct floatformat floatformat_ieee_single_little =
44{
45 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
46 floatformat_intbit_no,
47 "floatformat_ieee_single_little"
48};
49const struct floatformat floatformat_ieee_double_big =
50{
51 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
52 floatformat_intbit_no,
53 "floatformat_ieee_double_big"
54};
55const struct floatformat floatformat_ieee_double_little =
56{
57 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
58 floatformat_intbit_no,
59 "floatformat_ieee_double_little"
60};
61
62/* floatformat for IEEE double, little endian byte order, with big endian word
63 ordering, as on the ARM. */
64
65const struct floatformat floatformat_ieee_double_littlebyte_bigword =
66{
67 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
68 floatformat_intbit_no,
69 "floatformat_ieee_double_little"
70};
71
72const struct floatformat floatformat_i387_ext =
73{
74 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
75 floatformat_intbit_yes,
76 "floatformat_i387_ext"
77};
78const struct floatformat floatformat_m68881_ext =
79{
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"
84};
85const struct floatformat floatformat_i960_ext =
86{
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"
91};
92const struct floatformat floatformat_m88110_ext =
93{
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 */
105};
106const struct floatformat floatformat_arm_ext =
107{
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"
112};
113
114
115static unsigned long get_field PARAMS ((unsigned char *,
116 enum floatformat_byteorders,
117 unsigned int,
118 unsigned int,
119 unsigned int));
120
121/* Extract a field which starts at START and is LEN bytes long. DATA and
122 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
123static unsigned long
124get_field (data, order, total_len, start, len)
125 unsigned char *data;
126 enum floatformat_byteorders order;
127 unsigned int total_len;
128 unsigned int start;
129 unsigned int len;
130{
131 unsigned long result;
132 unsigned int cur_byte;
133 int cur_bitshift;
134
135 /* Start at the least significant part of the field. */
136 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
137 if (order == floatformat_little)
138 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
139 cur_bitshift =
140 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
141 result = *(data + cur_byte) >> (-cur_bitshift);
142 cur_bitshift += FLOATFORMAT_CHAR_BIT;
143 if (order == floatformat_little)
144 ++cur_byte;
145 else
146 --cur_byte;
147
148 /* Move towards the most significant part of the field. */
149 while ((unsigned int) cur_bitshift < len)
150 {
151 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
152 /* This is the last byte; zero out the bits which are not part of
153 this field. */
154 result |=
155 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
156 << cur_bitshift;
157 else
158 result |= *(data + cur_byte) << cur_bitshift;
159 cur_bitshift += FLOATFORMAT_CHAR_BIT;
160 if (order == floatformat_little)
161 ++cur_byte;
162 else
163 --cur_byte;
164 }
165 return result;
166}
167
168#ifndef min
169#define min(a, b) ((a) < (b) ? (a) : (b))
170#endif
171
172/* Convert from FMT to a double.
173 FROM is the address of the extended float.
174 Store the double in *TO. */
175
176void
177floatformat_to_double (fmt, from, to)
178 const struct floatformat *fmt;
179 char *from;
180 double *to;
181{
182 unsigned char *ufrom = (unsigned char *)from;
183 double dto;
184 long exponent;
185 unsigned long mant;
186 unsigned int mant_bits, mant_off;
187 int mant_bits_left;
188 int special_exponent; /* It's a NaN, denorm or zero */
189
190 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
191 fmt->exp_start, fmt->exp_len);
192 /* Note that if exponent indicates a NaN, we can't really do anything useful
193 (not knowing if the host has NaN's, or how to build one). So it will
194 end up as an infinity or something close; that is OK. */
195
196 mant_bits_left = fmt->man_len;
197 mant_off = fmt->man_start;
198 dto = 0.0;
199
200 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
201
202 /* Don't bias zero's, denorms or NaNs. */
203 if (!special_exponent)
204 exponent -= fmt->exp_bias;
205
206 /* Build the result algebraically. Might go infinite, underflow, etc;
207 who cares. */
208
209 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
210 increment the exponent by one to account for the integer bit. */
211
212 if (!special_exponent)
213 {
214 if (fmt->intbit == floatformat_intbit_no)
215 dto = ldexp (1.0, exponent);
216 else
217 exponent++;
218 }
219
220 while (mant_bits_left > 0)
221 {
222 mant_bits = min (mant_bits_left, 32);
223
224 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
225 mant_off, mant_bits);
226
227 dto += ldexp ((double)mant, exponent - mant_bits);
228 exponent -= mant_bits;
229 mant_off += mant_bits;
230 mant_bits_left -= mant_bits;
231 }
232
233 /* Negate it if negative. */
234 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
235 dto = -dto;
236 *to = dto;
237}
238
239
240static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
241 unsigned int,
242 unsigned int,
243 unsigned int,
244 unsigned long));
245
246/* Set a field which starts at START and is LEN bytes long. DATA and
247 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
248static void
249put_field (data, order, total_len, start, len, stuff_to_put)
250 unsigned char *data;
251 enum floatformat_byteorders order;
252 unsigned int total_len;
253 unsigned int start;
254 unsigned int len;
255 unsigned long stuff_to_put;
256{
257 unsigned int cur_byte;
258 int cur_bitshift;
259
260 /* Start at the least significant part of the field. */
261 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
262 if (order == floatformat_little)
263 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
264 cur_bitshift =
265 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
266 *(data + cur_byte) &=
267 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
268 *(data + cur_byte) |=
269 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
270 cur_bitshift += FLOATFORMAT_CHAR_BIT;
271 if (order == floatformat_little)
272 ++cur_byte;
273 else
274 --cur_byte;
275
276 /* Move towards the most significant part of the field. */
277 while ((unsigned int) cur_bitshift < len)
278 {
279 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
280 {
281 /* This is the last byte. */
282 *(data + cur_byte) &=
283 ~((1 << (len - cur_bitshift)) - 1);
284 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
285 }
286 else
287 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
288 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
289 cur_bitshift += FLOATFORMAT_CHAR_BIT;
290 if (order == floatformat_little)
291 ++cur_byte;
292 else
293 --cur_byte;
294 }
295}
296
297/* The converse: convert the double *FROM to an extended float
298 and store where TO points. Neither FROM nor TO have any alignment
299 restrictions. */
300
301void
302floatformat_from_double (fmt, from, to)
303 const struct floatformat *fmt;
304 double *from;
305 char *to;
306{
307 double dfrom;
308 int exponent;
309 double mant;
310 unsigned int mant_bits, mant_off;
311 int mant_bits_left;
312 unsigned char *uto = (unsigned char *)to;
313
314 memcpy (&dfrom, from, sizeof (dfrom));
315 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
316 if (dfrom == 0)
317 return; /* Result is zero */
318 if (dfrom != dfrom)
319 {
320 /* From is NaN */
321 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
322 fmt->exp_len, fmt->exp_nan);
323 /* Be sure it's not infinity, but NaN value is irrel */
324 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
325 32, 1);
326 return;
327 }
328
329 /* If negative, set the sign bit. */
330 if (dfrom < 0)
331 {
332 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
333 dfrom = -dfrom;
334 }
335
336 /* How to tell an infinity from an ordinary number? FIXME-someday */
337
338 mant = frexp (dfrom, &exponent);
339 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
340 exponent + fmt->exp_bias - 1);
341
342 mant_bits_left = fmt->man_len;
343 mant_off = fmt->man_start;
344 while (mant_bits_left > 0)
345 {
346 unsigned long mant_long;
347 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
348
349 mant *= 4294967296.0;
350 mant_long = (unsigned long)mant;
351 mant -= mant_long;
352
353 /* If the integer bit is implicit, then we need to discard it.
354 If we are discarding a zero, we should be (but are not) creating
355 a denormalized number which means adjusting the exponent
356 (I think). */
357 if ((unsigned int) mant_bits_left == fmt->man_len
358 && fmt->intbit == floatformat_intbit_no)
359 {
360 mant_long &= 0x7fffffff;
361 mant_bits -= 1;
362 }
363 else if (mant_bits < 32)
364 {
365 /* The bits we want are in the most significant MANT_BITS bits of
366 mant_long. Move them to the least significant. */
367 mant_long >>= 32 - mant_bits;
368 }
369
370 put_field (uto, fmt->byteorder, fmt->totalsize,
371 mant_off, mant_bits, mant_long);
372 mant_off += mant_bits;
373 mant_bits_left -= mant_bits;
374 }
375}
376
377
378#ifdef IEEE_DEBUG
379
380/* This is to be run on a host which uses IEEE floating point. */
381
382void
383ieee_test (n)
384 double n;
385{
386 double result;
387 char exten[16];
388
389 floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
390 if (n != result)
391 printf ("Differ(to): %.20g -> %.20g\n", n, result);
392 floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
393 if (n != result)
394 printf ("Differ(from): %.20g -> %.20g\n", n, result);
395
396 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
397 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
398 if (n != result)
399 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
400
401#if IEEE_DEBUG > 1
402 /* This is to be run on a host which uses 68881 format. */
403 {
404 long double ex = *(long double *)exten;
405 if (ex != n)
406 printf ("Differ(from vs. extended): %.20g\n", n);
407 }
408#endif
409}
410
411int
412main ()
413{
414 ieee_test (0.5);
415 ieee_test (256.0);
416 ieee_test (0.12345);
417 ieee_test (234235.78907234);
418 ieee_test (-512.0);
419 ieee_test (-0.004321);
420 return 0;
421}
422#endif
Note: See TracBrowser for help on using the repository browser.