]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* IEEE floating point support routines, for GDB, the GNU Debugger. |
f03aa80d | 2 | Copyright (C) 1991, 1994, 1999, 2000 Free Software Foundation, Inc. |
252b5132 RH |
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 | ||
1ea16ec5 | 20 | #include "ansidecl.h" |
252b5132 RH |
21 | #include "floatformat.h" |
22 | #include <math.h> /* ldexp */ | |
1ea16ec5 | 23 | #ifdef ANSI_PROTOTYPES |
252b5132 RH |
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 | { | |
f03aa80d AC |
40 | floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23, |
41 | floatformat_intbit_no, | |
42 | "floatformat_ieee_single_big" | |
252b5132 RH |
43 | }; |
44 | const struct floatformat floatformat_ieee_single_little = | |
45 | { | |
f03aa80d AC |
46 | floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23, |
47 | floatformat_intbit_no, | |
48 | "floatformat_ieee_single_little" | |
252b5132 RH |
49 | }; |
50 | const struct floatformat floatformat_ieee_double_big = | |
51 | { | |
f03aa80d AC |
52 | floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52, |
53 | floatformat_intbit_no, | |
54 | "floatformat_ieee_double_big" | |
252b5132 RH |
55 | }; |
56 | const struct floatformat floatformat_ieee_double_little = | |
57 | { | |
f03aa80d AC |
58 | floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52, |
59 | floatformat_intbit_no, | |
60 | "floatformat_ieee_double_little" | |
252b5132 RH |
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 | { | |
f03aa80d AC |
68 | floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52, |
69 | floatformat_intbit_no, | |
16bad250 | 70 | "floatformat_ieee_double_littlebyte_bigword" |
252b5132 RH |
71 | }; |
72 | ||
73 | const struct floatformat floatformat_i387_ext = | |
74 | { | |
75 | floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, | |
f03aa80d AC |
76 | floatformat_intbit_yes, |
77 | "floatformat_i387_ext" | |
252b5132 RH |
78 | }; |
79 | const struct floatformat floatformat_m68881_ext = | |
80 | { | |
81 | /* Note that the bits from 16 to 31 are unused. */ | |
f03aa80d AC |
82 | floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, |
83 | floatformat_intbit_yes, | |
84 | "floatformat_m68881_ext" | |
252b5132 RH |
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, | |
f03aa80d AC |
90 | floatformat_intbit_yes, |
91 | "floatformat_i960_ext" | |
252b5132 RH |
92 | }; |
93 | const struct floatformat floatformat_m88110_ext = | |
94 | { | |
eb828599 AC |
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 | { | |
252b5132 RH |
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, | |
f03aa80d | 104 | floatformat_intbit_no, |
eb828599 | 105 | "floatformat_m88110_ext_harris" |
252b5132 | 106 | }; |
eb828599 AC |
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 | }; | |
252b5132 RH |
145 | \f |
146 | static unsigned long get_field PARAMS ((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 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; | |
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. */ | |
08372f14 | 180 | while ((unsigned int) cur_bitshift < len) |
252b5132 RH |
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 | char *from; | |
211 | double *to; | |
212 | { | |
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 */ | |
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 | ||
08372f14 | 231 | special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan; |
252b5132 RH |
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 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; | |
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. */ | |
08372f14 | 307 | while ((unsigned int) cur_bitshift < len) |
252b5132 RH |
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 | 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). */ | |
08372f14 | 387 | if ((unsigned int) mant_bits_left == fmt->man_len |
252b5132 RH |
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 | ||
408 | #ifdef IEEE_DEBUG | |
409 | ||
410 | /* This is to be run on a host which uses IEEE floating point. */ | |
411 | ||
412 | void | |
413 | ieee_test (n) | |
414 | double n; | |
415 | { | |
416 | double result; | |
417 | char exten[16]; | |
418 | ||
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); | |
425 | ||
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); | |
430 | ||
431 | #if IEEE_DEBUG > 1 | |
432 | /* This is to be run on a host which uses 68881 format. */ | |
433 | { | |
434 | long double ex = *(long double *)exten; | |
435 | if (ex != n) | |
436 | printf ("Differ(from vs. extended): %.20g\n", n); | |
437 | } | |
438 | #endif | |
439 | } | |
440 | ||
441 | int | |
442 | main () | |
443 | { | |
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; | |
451 | } | |
452 | #endif |