Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* IEEE floating point support routines, for GDB, the GNU Debugger. |
3f2aacaf | 2 | Copyright (C) 1991, 1994, 1999, 2000, 2003 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 | |
979c05d3 | 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
252b5132 | 19 | |
b52927b7 DD |
20 | /* This is needed to pick up the NAN macro on some systems. */ |
21 | #define _GNU_SOURCE | |
22 | ||
23 | #ifdef HAVE_CONFIG_H | |
24 | #include "config.h" | |
25 | #endif | |
26 | ||
27 | #include <math.h> | |
28 | ||
29 | #ifdef HAVE_STRING_H | |
30 | #include <string.h> | |
31 | #endif | |
32 | ||
1ea16ec5 | 33 | #include "ansidecl.h" |
b52927b7 | 34 | #include "libiberty.h" |
252b5132 | 35 | #include "floatformat.h" |
b52927b7 DD |
36 | |
37 | #ifndef INFINITY | |
38 | #ifdef HUGE_VAL | |
39 | #define INFINITY HUGE_VAL | |
252b5132 | 40 | #else |
b52927b7 DD |
41 | #define INFINITY (1.0 / 0.0) |
42 | #endif | |
43 | #endif | |
44 | ||
45 | #ifndef NAN | |
46 | #define NAN (0.0 / 0.0) | |
252b5132 RH |
47 | #endif |
48 | ||
49b1fae4 DD |
49 | static unsigned long get_field (const unsigned char *, |
50 | enum floatformat_byteorders, | |
51 | unsigned int, | |
52 | unsigned int, | |
53 | unsigned int); | |
54 | static int floatformat_always_valid (const struct floatformat *fmt, | |
55 | const char *from); | |
5324d185 AC |
56 | |
57 | static int | |
49b1fae4 DD |
58 | floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED, |
59 | const char *from ATTRIBUTE_UNUSED) | |
5324d185 AC |
60 | { |
61 | return 1; | |
62 | } | |
63 | ||
252b5132 RH |
64 | /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not |
65 | going to bother with trying to muck around with whether it is defined in | |
66 | a system header, what we do if not, etc. */ | |
67 | #define FLOATFORMAT_CHAR_BIT 8 | |
68 | ||
69 | /* floatformats for IEEE single and double, big and little endian. */ | |
70 | const struct floatformat floatformat_ieee_single_big = | |
71 | { | |
f03aa80d AC |
72 | floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23, |
73 | floatformat_intbit_no, | |
5324d185 AC |
74 | "floatformat_ieee_single_big", |
75 | floatformat_always_valid | |
252b5132 RH |
76 | }; |
77 | const struct floatformat floatformat_ieee_single_little = | |
78 | { | |
f03aa80d AC |
79 | floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23, |
80 | floatformat_intbit_no, | |
5324d185 AC |
81 | "floatformat_ieee_single_little", |
82 | floatformat_always_valid | |
252b5132 RH |
83 | }; |
84 | const struct floatformat floatformat_ieee_double_big = | |
85 | { | |
f03aa80d AC |
86 | floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52, |
87 | floatformat_intbit_no, | |
5324d185 AC |
88 | "floatformat_ieee_double_big", |
89 | floatformat_always_valid | |
252b5132 RH |
90 | }; |
91 | const struct floatformat floatformat_ieee_double_little = | |
92 | { | |
f03aa80d AC |
93 | floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52, |
94 | floatformat_intbit_no, | |
5324d185 AC |
95 | "floatformat_ieee_double_little", |
96 | floatformat_always_valid | |
252b5132 RH |
97 | }; |
98 | ||
99 | /* floatformat for IEEE double, little endian byte order, with big endian word | |
100 | ordering, as on the ARM. */ | |
101 | ||
102 | const struct floatformat floatformat_ieee_double_littlebyte_bigword = | |
103 | { | |
f03aa80d AC |
104 | floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52, |
105 | floatformat_intbit_no, | |
5324d185 AC |
106 | "floatformat_ieee_double_littlebyte_bigword", |
107 | floatformat_always_valid | |
252b5132 RH |
108 | }; |
109 | ||
49b1fae4 | 110 | static int floatformat_i387_ext_is_valid (const struct floatformat *fmt, const char *from); |
5324d185 AC |
111 | |
112 | static int | |
49b1fae4 | 113 | floatformat_i387_ext_is_valid (const struct floatformat *fmt, const char *from) |
5324d185 AC |
114 | { |
115 | /* In the i387 double-extended format, if the exponent is all ones, | |
116 | then the integer bit must be set. If the exponent is neither 0 | |
117 | nor ~0, the intbit must also be set. Only if the exponent is | |
118 | zero can it be zero, and then it must be zero. */ | |
119 | unsigned long exponent, int_bit; | |
120 | const unsigned char *ufrom = (const unsigned char *) from; | |
121 | ||
122 | exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
123 | fmt->exp_start, fmt->exp_len); | |
124 | int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
125 | fmt->man_start, 1); | |
126 | ||
127 | if ((exponent == 0) != (int_bit == 0)) | |
128 | return 0; | |
129 | else | |
130 | return 1; | |
131 | } | |
132 | ||
252b5132 RH |
133 | const struct floatformat floatformat_i387_ext = |
134 | { | |
135 | floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, | |
f03aa80d | 136 | floatformat_intbit_yes, |
5324d185 AC |
137 | "floatformat_i387_ext", |
138 | floatformat_i387_ext_is_valid | |
252b5132 RH |
139 | }; |
140 | const struct floatformat floatformat_m68881_ext = | |
141 | { | |
142 | /* Note that the bits from 16 to 31 are unused. */ | |
f03aa80d AC |
143 | floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, |
144 | floatformat_intbit_yes, | |
5324d185 AC |
145 | "floatformat_m68881_ext", |
146 | floatformat_always_valid | |
252b5132 RH |
147 | }; |
148 | const struct floatformat floatformat_i960_ext = | |
149 | { | |
150 | /* Note that the bits from 0 to 15 are unused. */ | |
151 | floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64, | |
f03aa80d | 152 | floatformat_intbit_yes, |
5324d185 AC |
153 | "floatformat_i960_ext", |
154 | floatformat_always_valid | |
252b5132 RH |
155 | }; |
156 | const struct floatformat floatformat_m88110_ext = | |
157 | { | |
eb828599 AC |
158 | floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, |
159 | floatformat_intbit_yes, | |
5324d185 AC |
160 | "floatformat_m88110_ext", |
161 | floatformat_always_valid | |
eb828599 AC |
162 | }; |
163 | const struct floatformat floatformat_m88110_harris_ext = | |
164 | { | |
252b5132 RH |
165 | /* Harris uses raw format 128 bytes long, but the number is just an ieee |
166 | double, and the last 64 bits are wasted. */ | |
167 | floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52, | |
f03aa80d | 168 | floatformat_intbit_no, |
5324d185 AC |
169 | "floatformat_m88110_ext_harris", |
170 | floatformat_always_valid | |
252b5132 | 171 | }; |
eb828599 AC |
172 | const struct floatformat floatformat_arm_ext_big = |
173 | { | |
174 | /* Bits 1 to 16 are unused. */ | |
175 | floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64, | |
176 | floatformat_intbit_yes, | |
5324d185 AC |
177 | "floatformat_arm_ext_big", |
178 | floatformat_always_valid | |
eb828599 AC |
179 | }; |
180 | const struct floatformat floatformat_arm_ext_littlebyte_bigword = | |
181 | { | |
182 | /* Bits 1 to 16 are unused. */ | |
183 | floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64, | |
184 | floatformat_intbit_yes, | |
5324d185 AC |
185 | "floatformat_arm_ext_littlebyte_bigword", |
186 | floatformat_always_valid | |
eb828599 AC |
187 | }; |
188 | const struct floatformat floatformat_ia64_spill_big = | |
189 | { | |
190 | floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64, | |
191 | floatformat_intbit_yes, | |
5324d185 AC |
192 | "floatformat_ia64_spill_big", |
193 | floatformat_always_valid | |
eb828599 AC |
194 | }; |
195 | const struct floatformat floatformat_ia64_spill_little = | |
196 | { | |
197 | floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64, | |
198 | floatformat_intbit_yes, | |
5324d185 AC |
199 | "floatformat_ia64_spill_little", |
200 | floatformat_always_valid | |
eb828599 AC |
201 | }; |
202 | const struct floatformat floatformat_ia64_quad_big = | |
203 | { | |
204 | floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112, | |
205 | floatformat_intbit_no, | |
5324d185 AC |
206 | "floatformat_ia64_quad_big", |
207 | floatformat_always_valid | |
eb828599 AC |
208 | }; |
209 | const struct floatformat floatformat_ia64_quad_little = | |
210 | { | |
211 | floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112, | |
212 | floatformat_intbit_no, | |
5324d185 AC |
213 | "floatformat_ia64_quad_little", |
214 | floatformat_always_valid | |
eb828599 | 215 | }; |
252b5132 | 216 | \f |
3f2aacaf | 217 | /* Extract a field which starts at START and is LEN bits long. DATA and |
252b5132 RH |
218 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ |
219 | static unsigned long | |
49b1fae4 DD |
220 | get_field (const unsigned char *data, enum floatformat_byteorders order, |
221 | unsigned int total_len, unsigned int start, unsigned int len) | |
252b5132 RH |
222 | { |
223 | unsigned long result; | |
224 | unsigned int cur_byte; | |
225 | int cur_bitshift; | |
226 | ||
227 | /* Start at the least significant part of the field. */ | |
228 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
229 | if (order == floatformat_little) | |
230 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; | |
231 | cur_bitshift = | |
232 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
233 | result = *(data + cur_byte) >> (-cur_bitshift); | |
234 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
235 | if (order == floatformat_little) | |
236 | ++cur_byte; | |
237 | else | |
238 | --cur_byte; | |
239 | ||
240 | /* Move towards the most significant part of the field. */ | |
08372f14 | 241 | while ((unsigned int) cur_bitshift < len) |
252b5132 RH |
242 | { |
243 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
244 | /* This is the last byte; zero out the bits which are not part of | |
245 | this field. */ | |
246 | result |= | |
247 | (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1)) | |
248 | << cur_bitshift; | |
249 | else | |
250 | result |= *(data + cur_byte) << cur_bitshift; | |
251 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
252 | if (order == floatformat_little) | |
253 | ++cur_byte; | |
254 | else | |
255 | --cur_byte; | |
256 | } | |
257 | return result; | |
258 | } | |
259 | ||
260 | #ifndef min | |
261 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
262 | #endif | |
263 | ||
264 | /* Convert from FMT to a double. | |
265 | FROM is the address of the extended float. | |
266 | Store the double in *TO. */ | |
267 | ||
268 | void | |
49b1fae4 DD |
269 | floatformat_to_double (const struct floatformat *fmt, |
270 | const char *from, double *to) | |
252b5132 | 271 | { |
34f4a113 | 272 | const unsigned char *ufrom = (const unsigned char *)from; |
252b5132 RH |
273 | double dto; |
274 | long exponent; | |
275 | unsigned long mant; | |
276 | unsigned int mant_bits, mant_off; | |
277 | int mant_bits_left; | |
278 | int special_exponent; /* It's a NaN, denorm or zero */ | |
279 | ||
280 | exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
281 | fmt->exp_start, fmt->exp_len); | |
b52927b7 DD |
282 | |
283 | /* If the exponent indicates a NaN, we don't have information to | |
284 | decide what to do. So we handle it like IEEE, except that we | |
285 | don't try to preserve the type of NaN. FIXME. */ | |
286 | if ((unsigned long) exponent == fmt->exp_nan) | |
287 | { | |
288 | int nan; | |
289 | ||
290 | mant_off = fmt->man_start; | |
291 | mant_bits_left = fmt->man_len; | |
292 | nan = 0; | |
293 | while (mant_bits_left > 0) | |
294 | { | |
295 | mant_bits = min (mant_bits_left, 32); | |
296 | ||
297 | if (get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
298 | mant_off, mant_bits) != 0) | |
299 | { | |
300 | /* This is a NaN. */ | |
301 | nan = 1; | |
302 | break; | |
303 | } | |
304 | ||
305 | mant_off += mant_bits; | |
306 | mant_bits_left -= mant_bits; | |
307 | } | |
308 | ||
f2942ea4 DD |
309 | /* On certain systems (such as GNU/Linux), the use of the |
310 | INFINITY macro below may generate a warning that can not be | |
311 | silenced due to a bug in GCC (PR preprocessor/11931). The | |
312 | preprocessor fails to recognise the __extension__ keyword in | |
313 | conjunction with the GNU/C99 extension for hexadecimal | |
314 | floating point constants and will issue a warning when | |
315 | compiling with -pedantic. */ | |
b52927b7 DD |
316 | if (nan) |
317 | dto = NAN; | |
318 | else | |
319 | dto = INFINITY; | |
320 | ||
321 | if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) | |
322 | dto = -dto; | |
323 | ||
324 | *to = dto; | |
325 | ||
326 | return; | |
327 | } | |
252b5132 RH |
328 | |
329 | mant_bits_left = fmt->man_len; | |
330 | mant_off = fmt->man_start; | |
331 | dto = 0.0; | |
332 | ||
08372f14 | 333 | special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan; |
252b5132 RH |
334 | |
335 | /* Don't bias zero's, denorms or NaNs. */ | |
336 | if (!special_exponent) | |
337 | exponent -= fmt->exp_bias; | |
338 | ||
339 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
340 | who cares. */ | |
341 | ||
342 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
343 | increment the exponent by one to account for the integer bit. */ | |
344 | ||
345 | if (!special_exponent) | |
346 | { | |
347 | if (fmt->intbit == floatformat_intbit_no) | |
348 | dto = ldexp (1.0, exponent); | |
349 | else | |
350 | exponent++; | |
351 | } | |
352 | ||
353 | while (mant_bits_left > 0) | |
354 | { | |
355 | mant_bits = min (mant_bits_left, 32); | |
356 | ||
357 | mant = get_field (ufrom, fmt->byteorder, fmt->totalsize, | |
358 | mant_off, mant_bits); | |
359 | ||
b52927b7 DD |
360 | /* Handle denormalized numbers. FIXME: What should we do for |
361 | non-IEEE formats? */ | |
362 | if (exponent == 0 && mant != 0) | |
363 | dto += ldexp ((double)mant, | |
364 | (- fmt->exp_bias | |
365 | - mant_bits | |
366 | - (mant_off - fmt->man_start) | |
367 | + 1)); | |
368 | else | |
369 | dto += ldexp ((double)mant, exponent - mant_bits); | |
370 | if (exponent != 0) | |
371 | exponent -= mant_bits; | |
252b5132 RH |
372 | mant_off += mant_bits; |
373 | mant_bits_left -= mant_bits; | |
374 | } | |
375 | ||
376 | /* Negate it if negative. */ | |
377 | if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) | |
378 | dto = -dto; | |
379 | *to = dto; | |
380 | } | |
381 | \f | |
49b1fae4 DD |
382 | static void put_field (unsigned char *, enum floatformat_byteorders, |
383 | unsigned int, | |
384 | unsigned int, | |
385 | unsigned int, | |
386 | unsigned long); | |
252b5132 | 387 | |
3f2aacaf | 388 | /* Set a field which starts at START and is LEN bits long. DATA and |
252b5132 RH |
389 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ |
390 | static void | |
49b1fae4 DD |
391 | put_field (unsigned char *data, enum floatformat_byteorders order, |
392 | unsigned int total_len, unsigned int start, unsigned int len, | |
393 | unsigned long stuff_to_put) | |
252b5132 RH |
394 | { |
395 | unsigned int cur_byte; | |
396 | int cur_bitshift; | |
397 | ||
398 | /* Start at the least significant part of the field. */ | |
399 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
400 | if (order == floatformat_little) | |
401 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1; | |
402 | cur_bitshift = | |
403 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
404 | *(data + cur_byte) &= | |
405 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift)); | |
406 | *(data + cur_byte) |= | |
407 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
408 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
409 | if (order == floatformat_little) | |
410 | ++cur_byte; | |
411 | else | |
412 | --cur_byte; | |
413 | ||
414 | /* Move towards the most significant part of the field. */ | |
08372f14 | 415 | while ((unsigned int) cur_bitshift < len) |
252b5132 RH |
416 | { |
417 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
418 | { | |
419 | /* This is the last byte. */ | |
420 | *(data + cur_byte) &= | |
421 | ~((1 << (len - cur_bitshift)) - 1); | |
422 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
423 | } | |
424 | else | |
425 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
426 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
427 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
428 | if (order == floatformat_little) | |
429 | ++cur_byte; | |
430 | else | |
431 | --cur_byte; | |
432 | } | |
433 | } | |
434 | ||
435 | /* The converse: convert the double *FROM to an extended float | |
436 | and store where TO points. Neither FROM nor TO have any alignment | |
437 | restrictions. */ | |
438 | ||
439 | void | |
49b1fae4 DD |
440 | floatformat_from_double (const struct floatformat *fmt, |
441 | const double *from, char *to) | |
252b5132 RH |
442 | { |
443 | double dfrom; | |
444 | int exponent; | |
445 | double mant; | |
446 | unsigned int mant_bits, mant_off; | |
447 | int mant_bits_left; | |
448 | unsigned char *uto = (unsigned char *)to; | |
449 | ||
b52927b7 | 450 | dfrom = *from; |
252b5132 | 451 | memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT); |
b52927b7 DD |
452 | |
453 | /* If negative, set the sign bit. */ | |
454 | if (dfrom < 0) | |
455 | { | |
456 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); | |
457 | dfrom = -dfrom; | |
458 | } | |
459 | ||
252b5132 | 460 | if (dfrom == 0) |
b52927b7 DD |
461 | { |
462 | /* 0.0. */ | |
463 | return; | |
464 | } | |
465 | ||
252b5132 RH |
466 | if (dfrom != dfrom) |
467 | { | |
b52927b7 | 468 | /* NaN. */ |
252b5132 RH |
469 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, |
470 | fmt->exp_len, fmt->exp_nan); | |
b52927b7 | 471 | /* Be sure it's not infinity, but NaN value is irrelevant. */ |
252b5132 RH |
472 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, |
473 | 32, 1); | |
474 | return; | |
475 | } | |
476 | ||
b52927b7 | 477 | if (dfrom + dfrom == dfrom) |
252b5132 | 478 | { |
b52927b7 DD |
479 | /* This can only happen for an infinite value (or zero, which we |
480 | already handled above). */ | |
481 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
482 | fmt->exp_len, fmt->exp_nan); | |
483 | return; | |
252b5132 RH |
484 | } |
485 | ||
252b5132 | 486 | mant = frexp (dfrom, &exponent); |
b52927b7 DD |
487 | if (exponent + fmt->exp_bias - 1 > 0) |
488 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
489 | fmt->exp_len, exponent + fmt->exp_bias - 1); | |
490 | else | |
491 | { | |
492 | /* Handle a denormalized number. FIXME: What should we do for | |
493 | non-IEEE formats? */ | |
494 | put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, | |
495 | fmt->exp_len, 0); | |
496 | mant = ldexp (mant, exponent + fmt->exp_bias - 1); | |
497 | } | |
252b5132 RH |
498 | |
499 | mant_bits_left = fmt->man_len; | |
500 | mant_off = fmt->man_start; | |
501 | while (mant_bits_left > 0) | |
502 | { | |
503 | unsigned long mant_long; | |
504 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; | |
505 | ||
506 | mant *= 4294967296.0; | |
507 | mant_long = (unsigned long)mant; | |
508 | mant -= mant_long; | |
509 | ||
b52927b7 DD |
510 | /* If the integer bit is implicit, and we are not creating a |
511 | denormalized number, then we need to discard it. */ | |
08372f14 | 512 | if ((unsigned int) mant_bits_left == fmt->man_len |
b52927b7 DD |
513 | && fmt->intbit == floatformat_intbit_no |
514 | && exponent + fmt->exp_bias - 1 > 0) | |
252b5132 RH |
515 | { |
516 | mant_long &= 0x7fffffff; | |
517 | mant_bits -= 1; | |
518 | } | |
519 | else if (mant_bits < 32) | |
520 | { | |
521 | /* The bits we want are in the most significant MANT_BITS bits of | |
522 | mant_long. Move them to the least significant. */ | |
523 | mant_long >>= 32 - mant_bits; | |
524 | } | |
525 | ||
526 | put_field (uto, fmt->byteorder, fmt->totalsize, | |
527 | mant_off, mant_bits, mant_long); | |
528 | mant_off += mant_bits; | |
529 | mant_bits_left -= mant_bits; | |
530 | } | |
531 | } | |
532 | ||
3f2aacaf DJ |
533 | /* Return non-zero iff the data at FROM is a valid number in format FMT. */ |
534 | ||
535 | int | |
49b1fae4 | 536 | floatformat_is_valid (const struct floatformat *fmt, const char *from) |
3f2aacaf | 537 | { |
5324d185 | 538 | return fmt->is_valid (fmt, from); |
3f2aacaf DJ |
539 | } |
540 | ||
252b5132 RH |
541 | |
542 | #ifdef IEEE_DEBUG | |
543 | ||
b52927b7 DD |
544 | #include <stdio.h> |
545 | ||
252b5132 RH |
546 | /* This is to be run on a host which uses IEEE floating point. */ |
547 | ||
548 | void | |
49b1fae4 | 549 | ieee_test (double n) |
252b5132 RH |
550 | { |
551 | double result; | |
252b5132 | 552 | |
b52927b7 DD |
553 | floatformat_to_double (&floatformat_ieee_double_little, (char *) &n, |
554 | &result); | |
555 | if ((n != result && (! isnan (n) || ! isnan (result))) | |
556 | || (n < 0 && result >= 0) | |
557 | || (n >= 0 && result < 0)) | |
252b5132 | 558 | printf ("Differ(to): %.20g -> %.20g\n", n, result); |
b52927b7 DD |
559 | |
560 | floatformat_from_double (&floatformat_ieee_double_little, &n, | |
561 | (char *) &result); | |
562 | if ((n != result && (! isnan (n) || ! isnan (result))) | |
563 | || (n < 0 && result >= 0) | |
564 | || (n >= 0 && result < 0)) | |
252b5132 RH |
565 | printf ("Differ(from): %.20g -> %.20g\n", n, result); |
566 | ||
b52927b7 DD |
567 | #if 0 |
568 | { | |
569 | char exten[16]; | |
570 | ||
571 | floatformat_from_double (&floatformat_m68881_ext, &n, exten); | |
572 | floatformat_to_double (&floatformat_m68881_ext, exten, &result); | |
573 | if (n != result) | |
574 | printf ("Differ(to+from): %.20g -> %.20g\n", n, result); | |
575 | } | |
576 | #endif | |
252b5132 RH |
577 | |
578 | #if IEEE_DEBUG > 1 | |
579 | /* This is to be run on a host which uses 68881 format. */ | |
580 | { | |
581 | long double ex = *(long double *)exten; | |
582 | if (ex != n) | |
583 | printf ("Differ(from vs. extended): %.20g\n", n); | |
584 | } | |
585 | #endif | |
586 | } | |
587 | ||
588 | int | |
49b1fae4 | 589 | main (void) |
252b5132 | 590 | { |
b52927b7 | 591 | ieee_test (0.0); |
252b5132 RH |
592 | ieee_test (0.5); |
593 | ieee_test (256.0); | |
594 | ieee_test (0.12345); | |
595 | ieee_test (234235.78907234); | |
596 | ieee_test (-512.0); | |
597 | ieee_test (-0.004321); | |
b52927b7 DD |
598 | ieee_test (1.2E-70); |
599 | ieee_test (1.2E-316); | |
600 | ieee_test (4.9406564584124654E-324); | |
601 | ieee_test (- 4.9406564584124654E-324); | |
602 | ieee_test (- 0.0); | |
603 | ieee_test (- INFINITY); | |
604 | ieee_test (- NAN); | |
605 | ieee_test (INFINITY); | |
606 | ieee_test (NAN); | |
252b5132 RH |
607 | return 0; |
608 | } | |
609 | #endif |