]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/floatformat.c
[Ada] Improved support for aspect alignment in CCG
[thirdparty/gcc.git] / libiberty / floatformat.c
CommitLineData
6599da04 1/* IEEE floating point support routines, for GDB, the GNU Debugger.
8d9254fc 2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
6599da04
JM
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
ee58dffd 18Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
6599da04 19
bee6ab3e 20/* This is needed to pick up the NAN macro on some systems. */
f50f17e6 21#ifndef _GNU_SOURCE
bee6ab3e 22#define _GNU_SOURCE
f50f17e6 23#endif
bee6ab3e
ILT
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <math.h>
30
31#ifdef HAVE_STRING_H
32#include <string.h>
33#endif
34
12c61ac3
RS
35/* On some platforms, <float.h> provides DBL_QNAN. */
36#ifdef STDC_HEADERS
37#include <float.h>
38#endif
39
c9ac9147 40#include "ansidecl.h"
bee6ab3e 41#include "libiberty.h"
6599da04 42#include "floatformat.h"
bee6ab3e
ILT
43
44#ifndef INFINITY
45#ifdef HUGE_VAL
46#define INFINITY HUGE_VAL
6599da04 47#else
bee6ab3e
ILT
48#define INFINITY (1.0 / 0.0)
49#endif
50#endif
51
52#ifndef NAN
12c61ac3
RS
53#ifdef DBL_QNAN
54#define NAN DBL_QNAN
55#else
bee6ab3e 56#define NAN (0.0 / 0.0)
6599da04 57#endif
12c61ac3 58#endif
6599da04 59
da59326f 60static int mant_bits_set (const struct floatformat *, const unsigned char *);
6da879de
GDR
61static unsigned long get_field (const unsigned char *,
62 enum floatformat_byteorders,
63 unsigned int,
64 unsigned int,
65 unsigned int);
66static int floatformat_always_valid (const struct floatformat *fmt,
e89b6c1c 67 const void *from);
83c07342
AC
68
69static int
6da879de 70floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
e89b6c1c 71 const void *from ATTRIBUTE_UNUSED)
83c07342
AC
72{
73 return 1;
74}
75
6599da04
JM
76/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
77 going to bother with trying to muck around with whether it is defined in
78 a system header, what we do if not, etc. */
79#define FLOATFORMAT_CHAR_BIT 8
80
d414dde9
KW
81/* floatformats for IEEE half, single and double, big and little endian. */
82const struct floatformat floatformat_ieee_half_big =
83{
84 floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
85 floatformat_intbit_no,
86 "floatformat_ieee_half_big",
87 floatformat_always_valid,
88 NULL
89};
90const struct floatformat floatformat_ieee_half_little =
91{
92 floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
93 floatformat_intbit_no,
94 "floatformat_ieee_half_little",
95 floatformat_always_valid,
96 NULL
97};
6599da04
JM
98const struct floatformat floatformat_ieee_single_big =
99{
0b72c3df
AC
100 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
101 floatformat_intbit_no,
83c07342 102 "floatformat_ieee_single_big",
da59326f
JM
103 floatformat_always_valid,
104 NULL
6599da04
JM
105};
106const struct floatformat floatformat_ieee_single_little =
107{
0b72c3df
AC
108 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
109 floatformat_intbit_no,
83c07342 110 "floatformat_ieee_single_little",
da59326f
JM
111 floatformat_always_valid,
112 NULL
6599da04
JM
113};
114const struct floatformat floatformat_ieee_double_big =
115{
0b72c3df
AC
116 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
117 floatformat_intbit_no,
83c07342 118 "floatformat_ieee_double_big",
da59326f
JM
119 floatformat_always_valid,
120 NULL
6599da04
JM
121};
122const struct floatformat floatformat_ieee_double_little =
123{
0b72c3df
AC
124 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
125 floatformat_intbit_no,
83c07342 126 "floatformat_ieee_double_little",
da59326f
JM
127 floatformat_always_valid,
128 NULL
6599da04
JM
129};
130
131/* floatformat for IEEE double, little endian byte order, with big endian word
132 ordering, as on the ARM. */
133
134const struct floatformat floatformat_ieee_double_littlebyte_bigword =
135{
0b72c3df
AC
136 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
137 floatformat_intbit_no,
83c07342 138 "floatformat_ieee_double_littlebyte_bigword",
da59326f
JM
139 floatformat_always_valid,
140 NULL
6599da04
JM
141};
142
0432a5de
MK
143/* floatformat for VAX. Not quite IEEE, but close enough. */
144
145const struct floatformat floatformat_vax_f =
146{
147 floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
148 floatformat_intbit_no,
149 "floatformat_vax_f",
da59326f
JM
150 floatformat_always_valid,
151 NULL
0432a5de
MK
152};
153const struct floatformat floatformat_vax_d =
154{
155 floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
156 floatformat_intbit_no,
157 "floatformat_vax_d",
da59326f
JM
158 floatformat_always_valid,
159 NULL
0432a5de
MK
160};
161const struct floatformat floatformat_vax_g =
162{
163 floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
164 floatformat_intbit_no,
165 "floatformat_vax_g",
da59326f
JM
166 floatformat_always_valid,
167 NULL
0432a5de
MK
168};
169
e89b6c1c
MK
170static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
171 const void *from);
83c07342
AC
172
173static int
e89b6c1c 174floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
83c07342
AC
175{
176 /* In the i387 double-extended format, if the exponent is all ones,
177 then the integer bit must be set. If the exponent is neither 0
178 nor ~0, the intbit must also be set. Only if the exponent is
179 zero can it be zero, and then it must be zero. */
180 unsigned long exponent, int_bit;
573b07c7 181 const unsigned char *ufrom = (const unsigned char *) from;
e89b6c1c 182
83c07342
AC
183 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
184 fmt->exp_start, fmt->exp_len);
185 int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
186 fmt->man_start, 1);
e89b6c1c 187
83c07342
AC
188 if ((exponent == 0) != (int_bit == 0))
189 return 0;
190 else
191 return 1;
192}
193
6599da04
JM
194const struct floatformat floatformat_i387_ext =
195{
196 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
0b72c3df 197 floatformat_intbit_yes,
83c07342 198 "floatformat_i387_ext",
da59326f
JM
199 floatformat_i387_ext_is_valid,
200 NULL
6599da04
JM
201};
202const struct floatformat floatformat_m68881_ext =
203{
204 /* Note that the bits from 16 to 31 are unused. */
0b72c3df
AC
205 floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
206 floatformat_intbit_yes,
83c07342 207 "floatformat_m68881_ext",
da59326f
JM
208 floatformat_always_valid,
209 NULL
6599da04
JM
210};
211const struct floatformat floatformat_i960_ext =
212{
213 /* Note that the bits from 0 to 15 are unused. */
214 floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
0b72c3df 215 floatformat_intbit_yes,
83c07342 216 "floatformat_i960_ext",
da59326f
JM
217 floatformat_always_valid,
218 NULL
6599da04
JM
219};
220const struct floatformat floatformat_m88110_ext =
221{
0310e5ac
AC
222 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
223 floatformat_intbit_yes,
83c07342 224 "floatformat_m88110_ext",
da59326f
JM
225 floatformat_always_valid,
226 NULL
0310e5ac
AC
227};
228const struct floatformat floatformat_m88110_harris_ext =
229{
6599da04
JM
230 /* Harris uses raw format 128 bytes long, but the number is just an ieee
231 double, and the last 64 bits are wasted. */
232 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
0b72c3df 233 floatformat_intbit_no,
83c07342 234 "floatformat_m88110_ext_harris",
da59326f
JM
235 floatformat_always_valid,
236 NULL
6599da04 237};
0310e5ac
AC
238const struct floatformat floatformat_arm_ext_big =
239{
240 /* Bits 1 to 16 are unused. */
241 floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
242 floatformat_intbit_yes,
83c07342 243 "floatformat_arm_ext_big",
da59326f
JM
244 floatformat_always_valid,
245 NULL
0310e5ac
AC
246};
247const struct floatformat floatformat_arm_ext_littlebyte_bigword =
248{
249 /* Bits 1 to 16 are unused. */
250 floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
251 floatformat_intbit_yes,
83c07342 252 "floatformat_arm_ext_littlebyte_bigword",
da59326f
JM
253 floatformat_always_valid,
254 NULL
0310e5ac
AC
255};
256const struct floatformat floatformat_ia64_spill_big =
257{
258 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
259 floatformat_intbit_yes,
83c07342 260 "floatformat_ia64_spill_big",
da59326f
JM
261 floatformat_always_valid,
262 NULL
0310e5ac
AC
263};
264const struct floatformat floatformat_ia64_spill_little =
265{
266 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
267 floatformat_intbit_yes,
83c07342 268 "floatformat_ia64_spill_little",
da59326f
JM
269 floatformat_always_valid,
270 NULL
0310e5ac
AC
271};
272const struct floatformat floatformat_ia64_quad_big =
273{
274 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
275 floatformat_intbit_no,
83c07342 276 "floatformat_ia64_quad_big",
da59326f
JM
277 floatformat_always_valid,
278 NULL
0310e5ac
AC
279};
280const struct floatformat floatformat_ia64_quad_little =
281{
282 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
283 floatformat_intbit_no,
83c07342 284 "floatformat_ia64_quad_little",
da59326f
JM
285 floatformat_always_valid,
286 NULL
287};
288
289static int
290floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
291 const void *from)
292{
293 const unsigned char *ufrom = (const unsigned char *) from;
294 const struct floatformat *hfmt = fmt->split_half;
295 long top_exp, bot_exp;
296 int top_nan = 0;
297
298 top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
299 hfmt->exp_start, hfmt->exp_len);
300 bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
301 hfmt->exp_start, hfmt->exp_len);
302
983a6f8d 303 if ((unsigned long) top_exp == hfmt->exp_nan)
da59326f
JM
304 top_nan = mant_bits_set (hfmt, ufrom);
305
306 /* A NaN is valid with any low part. */
307 if (top_nan)
308 return 1;
309
310 /* An infinity, zero or denormal requires low part 0 (positive or
311 negative). */
983a6f8d 312 if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
da59326f 313 {
da59326f
JM
314 if (bot_exp != 0)
315 return 0;
316
317 return !mant_bits_set (hfmt, ufrom + 8);
318 }
319
320 /* The top part is now a finite normal value. The long double value
321 is the sum of the two parts, and the top part must equal the
322 result of rounding the long double value to nearest double. Thus
323 the bottom part must be <= 0.5ulp of the top part in absolute
324 value, and if it is < 0.5ulp then the long double is definitely
325 valid. */
326 if (bot_exp < top_exp - 53)
327 return 1;
328 if (bot_exp > top_exp - 53 && bot_exp != 0)
329 return 0;
330 if (bot_exp == 0)
331 {
332 /* The bottom part is 0 or denormal. Determine which, and if
333 denormal the first two set bits. */
334 int first_bit = -1, second_bit = -1, cur_bit;
983a6f8d 335 for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
da59326f
JM
336 if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
337 hfmt->man_start + cur_bit, 1))
338 {
339 if (first_bit == -1)
340 first_bit = cur_bit;
341 else
342 {
343 second_bit = cur_bit;
344 break;
345 }
346 }
347 /* Bottom part 0 is OK. */
348 if (first_bit == -1)
349 return 1;
350 /* The real exponent of the bottom part is -first_bit. */
351 if (-first_bit < top_exp - 53)
352 return 1;
353 if (-first_bit > top_exp - 53)
354 return 0;
355 /* The bottom part is at least 0.5ulp of the top part. For this
356 to be OK, the bottom part must be exactly 0.5ulp (i.e. no
357 more bits set) and the top part must have last bit 0. */
358 if (second_bit != -1)
359 return 0;
360 return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
361 hfmt->man_start + hfmt->man_len - 1, 1);
362 }
363 else
364 {
365 /* The bottom part is at least 0.5ulp of the top part. For this
366 to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
367 set) and the top part must have last bit 0. */
368 if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
369 hfmt->man_start + hfmt->man_len - 1, 1))
370 return 0;
371 return !mant_bits_set (hfmt, ufrom + 8);
372 }
373}
374
a98cbc36 375const struct floatformat floatformat_ibm_long_double_big =
da59326f
JM
376{
377 floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
378 floatformat_intbit_no,
a98cbc36 379 "floatformat_ibm_long_double_big",
983a6f8d 380 floatformat_ibm_long_double_is_valid,
da59326f 381 &floatformat_ieee_double_big
0310e5ac 382};
a98cbc36
AM
383
384const struct floatformat floatformat_ibm_long_double_little =
385{
386 floatformat_little, 128, 0, 1, 11, 1023, 2047, 12, 52,
387 floatformat_intbit_no,
388 "floatformat_ibm_long_double_little",
389 floatformat_ibm_long_double_is_valid,
390 &floatformat_ieee_double_little
391};
6599da04 392\f
36b82060
JB
393
394#ifndef min
395#define min(a, b) ((a) < (b) ? (a) : (b))
396#endif
397
da59326f
JM
398/* Return 1 if any bits are explicitly set in the mantissa of UFROM,
399 format FMT, 0 otherwise. */
400static int
401mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
402{
403 unsigned int mant_bits, mant_off;
404 int mant_bits_left;
405
406 mant_off = fmt->man_start;
407 mant_bits_left = fmt->man_len;
408 while (mant_bits_left > 0)
409 {
410 mant_bits = min (mant_bits_left, 32);
411
412 if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
413 mant_off, mant_bits) != 0)
414 return 1;
415
416 mant_off += mant_bits;
417 mant_bits_left -= mant_bits;
418 }
419 return 0;
420}
421
9c8860c3 422/* Extract a field which starts at START and is LEN bits long. DATA and
6599da04
JM
423 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
424static unsigned long
6da879de
GDR
425get_field (const unsigned char *data, enum floatformat_byteorders order,
426 unsigned int total_len, unsigned int start, unsigned int len)
6599da04 427{
36b82060 428 unsigned long result = 0;
6599da04 429 unsigned int cur_byte;
36b82060
JB
430 int lo_bit, hi_bit, cur_bitshift = 0;
431 int nextbyte = (order == floatformat_little) ? 1 : -1;
432
433 /* Start is in big-endian bit order! Fix that first. */
434 start = total_len - (start + len);
6599da04
JM
435
436 /* Start at the least significant part of the field. */
6599da04 437 if (order == floatformat_little)
36b82060 438 cur_byte = start / FLOATFORMAT_CHAR_BIT;
6599da04 439 else
36b82060 440 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
6599da04 441
36b82060
JB
442 lo_bit = start % FLOATFORMAT_CHAR_BIT;
443 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
444
445 do
6599da04 446 {
36b82060
JB
447 unsigned int shifted = *(data + cur_byte) >> lo_bit;
448 unsigned int bits = hi_bit - lo_bit;
449 unsigned int mask = (1 << bits) - 1;
450 result |= (shifted & mask) << cur_bitshift;
451 len -= bits;
452 cur_bitshift += bits;
453 cur_byte += nextbyte;
454 lo_bit = 0;
455 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
6599da04 456 }
36b82060
JB
457 while (len != 0);
458
6599da04
JM
459 return result;
460}
461
6599da04
JM
462/* Convert from FMT to a double.
463 FROM is the address of the extended float.
464 Store the double in *TO. */
465
466void
6da879de 467floatformat_to_double (const struct floatformat *fmt,
e89b6c1c 468 const void *from, double *to)
6599da04 469{
573b07c7 470 const unsigned char *ufrom = (const unsigned char *) from;
6599da04
JM
471 double dto;
472 long exponent;
473 unsigned long mant;
474 unsigned int mant_bits, mant_off;
475 int mant_bits_left;
6599da04 476
da59326f
JM
477 /* Split values are not handled specially, since the top half has
478 the correctly rounded double value (in the only supported case of
479 split values). */
480
6599da04
JM
481 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
482 fmt->exp_start, fmt->exp_len);
bee6ab3e
ILT
483
484 /* If the exponent indicates a NaN, we don't have information to
485 decide what to do. So we handle it like IEEE, except that we
486 don't try to preserve the type of NaN. FIXME. */
487 if ((unsigned long) exponent == fmt->exp_nan)
488 {
da59326f 489 int nan = mant_bits_set (fmt, ufrom);
bee6ab3e 490
72f93778 491 /* On certain systems (such as GNU/Linux), the use of the
adcb167e 492 INFINITY macro below may generate a warning that cannot be
72f93778
BE
493 silenced due to a bug in GCC (PR preprocessor/11931). The
494 preprocessor fails to recognise the __extension__ keyword in
495 conjunction with the GNU/C99 extension for hexadecimal
496 floating point constants and will issue a warning when
497 compiling with -pedantic. */
bee6ab3e
ILT
498 if (nan)
499 dto = NAN;
500 else
501 dto = INFINITY;
502
503 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
504 dto = -dto;
505
506 *to = dto;
507
508 return;
509 }
6599da04
JM
510
511 mant_bits_left = fmt->man_len;
512 mant_off = fmt->man_start;
513 dto = 0.0;
514
6599da04
JM
515 /* Build the result algebraically. Might go infinite, underflow, etc;
516 who cares. */
517
c9ac76ae
AS
518 /* For denorms use minimum exponent. */
519 if (exponent == 0)
520 exponent = 1 - fmt->exp_bias;
521 else
087aa398 522 {
c9ac76ae
AS
523 exponent -= fmt->exp_bias;
524
525 /* If this format uses a hidden bit, explicitly add it in now.
526 Otherwise, increment the exponent by one to account for the
527 integer bit. */
528
087aa398
KG
529 if (fmt->intbit == floatformat_intbit_no)
530 dto = ldexp (1.0, exponent);
531 else
532 exponent++;
533 }
6599da04
JM
534
535 while (mant_bits_left > 0)
536 {
537 mant_bits = min (mant_bits_left, 32);
538
539 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
540 mant_off, mant_bits);
541
c9ac76ae
AS
542 dto += ldexp ((double) mant, exponent - mant_bits);
543 exponent -= mant_bits;
6599da04
JM
544 mant_off += mant_bits;
545 mant_bits_left -= mant_bits;
546 }
547
548 /* Negate it if negative. */
549 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
550 dto = -dto;
551 *to = dto;
552}
553\f
6da879de
GDR
554static void put_field (unsigned char *, enum floatformat_byteorders,
555 unsigned int,
556 unsigned int,
557 unsigned int,
558 unsigned long);
6599da04 559
9c8860c3 560/* Set a field which starts at START and is LEN bits long. DATA and
6599da04
JM
561 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
562static void
6da879de
GDR
563put_field (unsigned char *data, enum floatformat_byteorders order,
564 unsigned int total_len, unsigned int start, unsigned int len,
565 unsigned long stuff_to_put)
6599da04
JM
566{
567 unsigned int cur_byte;
36b82060
JB
568 int lo_bit, hi_bit;
569 int nextbyte = (order == floatformat_little) ? 1 : -1;
570
571 /* Start is in big-endian bit order! Fix that first. */
572 start = total_len - (start + len);
6599da04
JM
573
574 /* Start at the least significant part of the field. */
6599da04 575 if (order == floatformat_little)
36b82060 576 cur_byte = start / FLOATFORMAT_CHAR_BIT;
6599da04 577 else
36b82060 578 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
6599da04 579
36b82060
JB
580 lo_bit = start % FLOATFORMAT_CHAR_BIT;
581 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
582
583 do
6599da04 584 {
36b82060
JB
585 unsigned char *byte_ptr = data + cur_byte;
586 unsigned int bits = hi_bit - lo_bit;
587 unsigned int mask = ((1 << bits) - 1) << lo_bit;
588 *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
589 stuff_to_put >>= bits;
590 len -= bits;
591 cur_byte += nextbyte;
592 lo_bit = 0;
593 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
6599da04 594 }
36b82060 595 while (len != 0);
6599da04
JM
596}
597
598/* The converse: convert the double *FROM to an extended float
599 and store where TO points. Neither FROM nor TO have any alignment
600 restrictions. */
601
602void
6da879de 603floatformat_from_double (const struct floatformat *fmt,
e89b6c1c 604 const double *from, void *to)
6599da04
JM
605{
606 double dfrom;
607 int exponent;
608 double mant;
609 unsigned int mant_bits, mant_off;
610 int mant_bits_left;
573b07c7 611 unsigned char *uto = (unsigned char *) to;
6599da04 612
bee6ab3e 613 dfrom = *from;
6599da04 614 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
bee6ab3e 615
da59326f
JM
616 /* Split values are not handled specially, since a bottom half of
617 zero is correct for any value representable as double (in the
618 only supported case of split values). */
619
bee6ab3e
ILT
620 /* If negative, set the sign bit. */
621 if (dfrom < 0)
622 {
623 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
624 dfrom = -dfrom;
625 }
626
6599da04 627 if (dfrom == 0)
bee6ab3e
ILT
628 {
629 /* 0.0. */
630 return;
631 }
632
6599da04
JM
633 if (dfrom != dfrom)
634 {
bee6ab3e 635 /* NaN. */
6599da04
JM
636 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
637 fmt->exp_len, fmt->exp_nan);
bee6ab3e 638 /* Be sure it's not infinity, but NaN value is irrelevant. */
6599da04
JM
639 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
640 32, 1);
641 return;
642 }
643
bee6ab3e 644 if (dfrom + dfrom == dfrom)
6599da04 645 {
bee6ab3e
ILT
646 /* This can only happen for an infinite value (or zero, which we
647 already handled above). */
648 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
649 fmt->exp_len, fmt->exp_nan);
650 return;
6599da04
JM
651 }
652
6599da04 653 mant = frexp (dfrom, &exponent);
bee6ab3e
ILT
654 if (exponent + fmt->exp_bias - 1 > 0)
655 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
656 fmt->exp_len, exponent + fmt->exp_bias - 1);
657 else
658 {
659 /* Handle a denormalized number. FIXME: What should we do for
660 non-IEEE formats? */
661 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
662 fmt->exp_len, 0);
663 mant = ldexp (mant, exponent + fmt->exp_bias - 1);
664 }
6599da04
JM
665
666 mant_bits_left = fmt->man_len;
667 mant_off = fmt->man_start;
668 while (mant_bits_left > 0)
669 {
670 unsigned long mant_long;
671 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
672
673 mant *= 4294967296.0;
674 mant_long = (unsigned long)mant;
675 mant -= mant_long;
676
bee6ab3e
ILT
677 /* If the integer bit is implicit, and we are not creating a
678 denormalized number, then we need to discard it. */
5ad5a984 679 if ((unsigned int) mant_bits_left == fmt->man_len
bee6ab3e
ILT
680 && fmt->intbit == floatformat_intbit_no
681 && exponent + fmt->exp_bias - 1 > 0)
6599da04
JM
682 {
683 mant_long &= 0x7fffffff;
684 mant_bits -= 1;
685 }
686 else if (mant_bits < 32)
687 {
688 /* The bits we want are in the most significant MANT_BITS bits of
689 mant_long. Move them to the least significant. */
690 mant_long >>= 32 - mant_bits;
691 }
692
693 put_field (uto, fmt->byteorder, fmt->totalsize,
694 mant_off, mant_bits, mant_long);
695 mant_off += mant_bits;
696 mant_bits_left -= mant_bits;
697 }
698}
699
9c8860c3
DJ
700/* Return non-zero iff the data at FROM is a valid number in format FMT. */
701
702int
e89b6c1c 703floatformat_is_valid (const struct floatformat *fmt, const void *from)
9c8860c3 704{
83c07342 705 return fmt->is_valid (fmt, from);
9c8860c3
DJ
706}
707
6599da04
JM
708
709#ifdef IEEE_DEBUG
710
bee6ab3e
ILT
711#include <stdio.h>
712
6599da04
JM
713/* This is to be run on a host which uses IEEE floating point. */
714
715void
6da879de 716ieee_test (double n)
6599da04
JM
717{
718 double result;
6599da04 719
e89b6c1c 720 floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
bee6ab3e
ILT
721 if ((n != result && (! isnan (n) || ! isnan (result)))
722 || (n < 0 && result >= 0)
723 || (n >= 0 && result < 0))
6599da04 724 printf ("Differ(to): %.20g -> %.20g\n", n, result);
bee6ab3e 725
e89b6c1c 726 floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
bee6ab3e
ILT
727 if ((n != result && (! isnan (n) || ! isnan (result)))
728 || (n < 0 && result >= 0)
729 || (n >= 0 && result < 0))
6599da04
JM
730 printf ("Differ(from): %.20g -> %.20g\n", n, result);
731
bee6ab3e
ILT
732#if 0
733 {
734 char exten[16];
735
736 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
737 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
738 if (n != result)
739 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
740 }
741#endif
6599da04
JM
742
743#if IEEE_DEBUG > 1
744 /* This is to be run on a host which uses 68881 format. */
745 {
746 long double ex = *(long double *)exten;
747 if (ex != n)
748 printf ("Differ(from vs. extended): %.20g\n", n);
749 }
750#endif
751}
752
753int
6da879de 754main (void)
6599da04 755{
bee6ab3e 756 ieee_test (0.0);
6599da04 757 ieee_test (0.5);
c9ac76ae 758 ieee_test (1.1);
6599da04
JM
759 ieee_test (256.0);
760 ieee_test (0.12345);
761 ieee_test (234235.78907234);
762 ieee_test (-512.0);
763 ieee_test (-0.004321);
bee6ab3e
ILT
764 ieee_test (1.2E-70);
765 ieee_test (1.2E-316);
766 ieee_test (4.9406564584124654E-324);
767 ieee_test (- 4.9406564584124654E-324);
768 ieee_test (- 0.0);
769 ieee_test (- INFINITY);
770 ieee_test (- NAN);
771 ieee_test (INFINITY);
772 ieee_test (NAN);
6599da04
JM
773 return 0;
774}
775#endif