2008-09-24 Michael J. Eager <eager@eagercon.com>
[official-gcc.git] / gcc / dfp.c
blob0485430305fd8bdac7cfa9c6ceed7128d559fe40
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "toplev.h"
26 #include "real.h"
27 #include "tm_p.h"
28 #include "dfp.h"
30 /* The order of the following headers is important for making sure
31 decNumber structure is large enough to hold decimal128 digits. */
33 #include "decimal128.h"
34 #include "decimal128Local.h"
35 #include "decimal64.h"
36 #include "decimal32.h"
37 #include "decNumber.h"
39 #ifndef WORDS_BIGENDIAN
40 #define WORDS_BIGENDIAN 0
41 #endif
43 /* Initialize R (a real with the decimal flag set) from DN. Can
44 utilize status passed in via CONTEXT, if a previous operation had
45 interesting status. */
47 static void
48 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
50 memset (r, 0, sizeof (REAL_VALUE_TYPE));
52 r->cl = rvc_normal;
53 if (decNumberIsNaN (dn))
54 r->cl = rvc_nan;
55 if (decNumberIsInfinite (dn))
56 r->cl = rvc_inf;
57 if (context->status & DEC_Overflow)
58 r->cl = rvc_inf;
59 if (decNumberIsNegative (dn))
60 r->sign = 1;
61 r->decimal = 1;
63 if (r->cl != rvc_normal)
64 return;
66 decContextDefault (context, DEC_INIT_DECIMAL128);
67 context->traps = 0;
69 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
72 /* Create decimal encoded R from string S. */
74 void
75 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
77 decNumber dn;
78 decContext set;
79 decContextDefault (&set, DEC_INIT_DECIMAL128);
80 set.traps = 0;
82 decNumberFromString (&dn, s, &set);
84 /* It would be more efficient to store directly in decNumber format,
85 but that is impractical from current data structure size.
86 Encoding as a decimal128 is much more compact. */
87 decimal_from_decnumber (r, &dn, &set);
90 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
92 static void
93 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
95 decContext set;
96 decContextDefault (&set, DEC_INIT_DECIMAL128);
97 set.traps = 0;
99 switch (r->cl)
101 case rvc_zero:
102 decNumberZero (dn);
103 break;
104 case rvc_inf:
105 decNumberFromString (dn, "Infinity", &set);
106 break;
107 case rvc_nan:
108 if (r->signalling)
109 decNumberFromString (dn, "snan", &set);
110 else
111 decNumberFromString (dn, "nan", &set);
112 break;
113 case rvc_normal:
114 gcc_assert (r->decimal);
115 decimal128ToNumber ((const decimal128 *) r->sig, dn);
116 break;
117 default:
118 gcc_unreachable ();
121 /* Fix up sign bit. */
122 if (r->sign != decNumberIsNegative (dn))
123 dn->bits ^= DECNEG;
126 /* Encode a real into an IEEE 754 decimal32 type. */
128 void
129 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
130 long *buf, const REAL_VALUE_TYPE *r)
132 decNumber dn;
133 decimal32 d32;
134 decContext set;
136 decContextDefault (&set, DEC_INIT_DECIMAL128);
137 set.traps = 0;
139 decimal_to_decnumber (r, &dn);
140 decimal32FromNumber (&d32, &dn, &set);
142 buf[0] = *(uint32_t *) d32.bytes;
145 /* Decode an IEEE 754 decimal32 type into a real. */
147 void
148 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
149 REAL_VALUE_TYPE *r, const long *buf)
151 decNumber dn;
152 decimal32 d32;
153 decContext set;
155 decContextDefault (&set, DEC_INIT_DECIMAL128);
156 set.traps = 0;
158 *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
160 decimal32ToNumber (&d32, &dn);
161 decimal_from_decnumber (r, &dn, &set);
164 /* Encode a real into an IEEE 754 decimal64 type. */
166 void
167 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
168 long *buf, const REAL_VALUE_TYPE *r)
170 decNumber dn;
171 decimal64 d64;
172 decContext set;
174 decContextDefault (&set, DEC_INIT_DECIMAL128);
175 set.traps = 0;
177 decimal_to_decnumber (r, &dn);
178 decimal64FromNumber (&d64, &dn, &set);
180 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
182 buf[0] = *(uint32_t *) &d64.bytes[0];
183 buf[1] = *(uint32_t *) &d64.bytes[4];
185 else
187 buf[0] = *(uint32_t *) &d64.bytes[4];
188 buf[1] = *(uint32_t *) &d64.bytes[0];
192 /* Decode an IEEE 754 decimal64 type into a real. */
194 void
195 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
196 REAL_VALUE_TYPE *r, const long *buf)
198 decNumber dn;
199 decimal64 d64;
200 decContext set;
202 decContextDefault (&set, DEC_INIT_DECIMAL128);
203 set.traps = 0;
205 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
207 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
208 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
210 else
212 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[1];
213 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[0];
216 decimal64ToNumber (&d64, &dn);
217 decimal_from_decnumber (r, &dn, &set);
220 /* Encode a real into an IEEE 754 decimal128 type. */
222 void
223 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
224 long *buf, const REAL_VALUE_TYPE *r)
226 decNumber dn;
227 decContext set;
228 decimal128 d128;
230 decContextDefault (&set, DEC_INIT_DECIMAL128);
231 set.traps = 0;
233 decimal_to_decnumber (r, &dn);
234 decimal128FromNumber (&d128, &dn, &set);
236 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
238 buf[0] = *(uint32_t *) &d128.bytes[0];
239 buf[1] = *(uint32_t *) &d128.bytes[4];
240 buf[2] = *(uint32_t *) &d128.bytes[8];
241 buf[3] = *(uint32_t *) &d128.bytes[12];
243 else
245 buf[0] = *(uint32_t *) &d128.bytes[12];
246 buf[1] = *(uint32_t *) &d128.bytes[8];
247 buf[2] = *(uint32_t *) &d128.bytes[4];
248 buf[3] = *(uint32_t *) &d128.bytes[0];
252 /* Decode an IEEE 754 decimal128 type into a real. */
254 void
255 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
256 REAL_VALUE_TYPE *r, const long *buf)
258 decNumber dn;
259 decimal128 d128;
260 decContext set;
262 decContextDefault (&set, DEC_INIT_DECIMAL128);
263 set.traps = 0;
265 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
267 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
268 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
269 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
270 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
272 else
274 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[3];
275 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[2];
276 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[1];
277 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[0];
280 decimal128ToNumber (&d128, &dn);
281 decimal_from_decnumber (r, &dn, &set);
284 /* Helper function to convert from a binary real internal
285 representation. */
287 static void
288 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
289 enum machine_mode mode)
291 char string[256];
292 const decimal128 *const d128 = (const decimal128 *) from->sig;
294 decimal128ToString (d128, string);
295 real_from_string3 (to, string, mode);
299 /* Helper function to convert from a binary real internal
300 representation. */
302 static void
303 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
305 char string[256];
307 /* We convert to string, then to decNumber then to decimal128. */
308 real_to_decimal (string, from, sizeof (string), 0, 1);
309 decimal_real_from_string (to, string);
312 /* Helper function to real.c:do_compare() to handle decimal internal
313 representation including when one of the operands is still in the
314 binary internal representation. */
317 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
318 int nan_result)
320 decContext set;
321 decNumber dn, dn2, dn3;
322 REAL_VALUE_TYPE a1, b1;
324 /* If either operand is non-decimal, create temporary versions. */
325 if (!a->decimal)
327 decimal_from_binary (&a1, a);
328 a = &a1;
330 if (!b->decimal)
332 decimal_from_binary (&b1, b);
333 b = &b1;
336 /* Convert into decNumber form for comparison operation. */
337 decContextDefault (&set, DEC_INIT_DECIMAL128);
338 set.traps = 0;
339 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
340 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
342 /* Finally, do the comparison. */
343 decNumberCompare (&dn, &dn2, &dn3, &set);
345 /* Return the comparison result. */
346 if (decNumberIsNaN (&dn))
347 return nan_result;
348 else if (decNumberIsZero (&dn))
349 return 0;
350 else if (decNumberIsNegative (&dn))
351 return -1;
352 else
353 return 1;
356 /* Helper to round_for_format, handling decimal float types. */
358 void
359 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
361 decNumber dn;
362 decContext set;
364 /* Real encoding occurs later. */
365 if (r->cl != rvc_normal)
366 return;
368 decContextDefault (&set, DEC_INIT_DECIMAL128);
369 set.traps = 0;
370 decimal128ToNumber ((decimal128 *) r->sig, &dn);
372 if (fmt == &decimal_quad_format)
374 /* The internal format is already in this format. */
375 return;
377 else if (fmt == &decimal_single_format)
379 decimal32 d32;
380 decContextDefault (&set, DEC_INIT_DECIMAL32);
381 set.traps = 0;
383 decimal32FromNumber (&d32, &dn, &set);
384 decimal32ToNumber (&d32, &dn);
386 else if (fmt == &decimal_double_format)
388 decimal64 d64;
389 decContextDefault (&set, DEC_INIT_DECIMAL64);
390 set.traps = 0;
392 decimal64FromNumber (&d64, &dn, &set);
393 decimal64ToNumber (&d64, &dn);
395 else
396 gcc_unreachable ();
398 decimal_from_decnumber (r, &dn, &set);
401 /* Extend or truncate to a new mode. Handles conversions between
402 binary and decimal types. */
404 void
405 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
406 const REAL_VALUE_TYPE *a)
408 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
410 if (a->decimal && fmt->b == 10)
411 return;
412 if (a->decimal)
413 decimal_to_binary (r, a, mode);
414 else
415 decimal_from_binary (r, a);
418 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
419 significant digits in the result, bounded by BUF_SIZE. If DIGITS
420 is 0, choose the maximum for the representation. If
421 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
422 DIGITS or CROP_TRAILING_ZEROS. */
424 void
425 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
426 size_t buf_size,
427 size_t digits ATTRIBUTE_UNUSED,
428 int crop_trailing_zeros ATTRIBUTE_UNUSED)
430 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
432 /* decimal128ToString requires space for at least 24 characters;
433 Require two more for suffix. */
434 gcc_assert (buf_size >= 24);
435 decimal128ToString (d128, str);
438 static bool
439 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
440 const REAL_VALUE_TYPE *op1, int subtract_p)
442 decNumber dn;
443 decContext set;
444 decNumber dn2, dn3;
446 decimal_to_decnumber (op0, &dn2);
447 decimal_to_decnumber (op1, &dn3);
449 decContextDefault (&set, DEC_INIT_DECIMAL128);
450 set.traps = 0;
452 if (subtract_p)
453 decNumberSubtract (&dn, &dn2, &dn3, &set);
454 else
455 decNumberAdd (&dn, &dn2, &dn3, &set);
457 decimal_from_decnumber (r, &dn, &set);
459 /* Return true, if inexact. */
460 return (set.status & DEC_Inexact);
463 /* Compute R = OP0 * OP1. */
465 static bool
466 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
467 const REAL_VALUE_TYPE *op1)
469 decContext set;
470 decNumber dn, dn2, dn3;
472 decimal_to_decnumber (op0, &dn2);
473 decimal_to_decnumber (op1, &dn3);
475 decContextDefault (&set, DEC_INIT_DECIMAL128);
476 set.traps = 0;
478 decNumberMultiply (&dn, &dn2, &dn3, &set);
479 decimal_from_decnumber (r, &dn, &set);
481 /* Return true, if inexact. */
482 return (set.status & DEC_Inexact);
485 /* Compute R = OP0 / OP1. */
487 static bool
488 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
489 const REAL_VALUE_TYPE *op1)
491 decContext set;
492 decNumber dn, dn2, dn3;
494 decimal_to_decnumber (op0, &dn2);
495 decimal_to_decnumber (op1, &dn3);
497 decContextDefault (&set, DEC_INIT_DECIMAL128);
498 set.traps = 0;
500 decNumberDivide (&dn, &dn2, &dn3, &set);
501 decimal_from_decnumber (r, &dn, &set);
503 /* Return true, if inexact. */
504 return (set.status & DEC_Inexact);
507 /* Set R to A truncated to an integral value toward zero (decimal
508 floating point). */
510 void
511 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
513 decNumber dn, dn2;
514 decContext set;
516 decContextDefault (&set, DEC_INIT_DECIMAL128);
517 set.traps = 0;
518 set.round = DEC_ROUND_DOWN;
519 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
521 decNumberToIntegralValue (&dn, &dn2, &set);
522 decimal_from_decnumber (r, &dn, &set);
525 /* Render decimal float value R as an integer. */
527 HOST_WIDE_INT
528 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
530 decContext set;
531 decNumber dn, dn2, dn3;
532 REAL_VALUE_TYPE to;
533 char string[256];
535 decContextDefault (&set, DEC_INIT_DECIMAL128);
536 set.traps = 0;
537 set.round = DEC_ROUND_DOWN;
538 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
540 decNumberToIntegralValue (&dn2, &dn, &set);
541 decNumberZero (&dn3);
542 decNumberRescale (&dn, &dn2, &dn3, &set);
544 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
545 function. */
546 decNumberToString (&dn, string);
547 real_from_string (&to, string);
548 return real_to_integer (&to);
551 /* Likewise, but to an integer pair, HI+LOW. */
553 void
554 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
555 const REAL_VALUE_TYPE *r)
557 decContext set;
558 decNumber dn, dn2, dn3;
559 REAL_VALUE_TYPE to;
560 char string[256];
562 decContextDefault (&set, DEC_INIT_DECIMAL128);
563 set.traps = 0;
564 set.round = DEC_ROUND_DOWN;
565 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
567 decNumberToIntegralValue (&dn2, &dn, &set);
568 decNumberZero (&dn3);
569 decNumberRescale (&dn, &dn2, &dn3, &set);
571 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
572 function. */
573 decNumberToString (&dn, string);
574 real_from_string (&to, string);
575 real_to_integer2 (plow, phigh, &to);
578 /* Perform the decimal floating point operation described by CODE.
579 For a unary operation, OP1 will be NULL. This function returns
580 true if the result may be inexact due to loss of precision. */
582 bool
583 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
584 const REAL_VALUE_TYPE *op0,
585 const REAL_VALUE_TYPE *op1)
587 REAL_VALUE_TYPE a, b;
589 /* If either operand is non-decimal, create temporaries. */
590 if (!op0->decimal)
592 decimal_from_binary (&a, op0);
593 op0 = &a;
595 if (op1 && !op1->decimal)
597 decimal_from_binary (&b, op1);
598 op1 = &b;
601 switch (code)
603 case PLUS_EXPR:
604 return decimal_do_add (r, op0, op1, 0);
606 case MINUS_EXPR:
607 return decimal_do_add (r, op0, op1, 1);
609 case MULT_EXPR:
610 return decimal_do_multiply (r, op0, op1);
612 case RDIV_EXPR:
613 return decimal_do_divide (r, op0, op1);
615 case MIN_EXPR:
616 if (op1->cl == rvc_nan)
617 *r = *op1;
618 else if (real_compare (UNLT_EXPR, op0, op1))
619 *r = *op0;
620 else
621 *r = *op1;
622 return false;
624 case MAX_EXPR:
625 if (op1->cl == rvc_nan)
626 *r = *op1;
627 else if (real_compare (LT_EXPR, op0, op1))
628 *r = *op1;
629 else
630 *r = *op0;
631 return false;
633 case NEGATE_EXPR:
635 *r = *op0;
636 /* Flip sign bit. */
637 decimal128FlipSign ((decimal128 *) r->sig);
638 /* Keep sign field in sync. */
639 r->sign ^= 1;
641 return false;
643 case ABS_EXPR:
645 *r = *op0;
646 /* Clear sign bit. */
647 decimal128ClearSign ((decimal128 *) r->sig);
648 /* Keep sign field in sync. */
649 r->sign = 0;
651 return false;
653 case FIX_TRUNC_EXPR:
654 decimal_do_fix_trunc (r, op0);
655 return false;
657 default:
658 gcc_unreachable ();
662 /* Fills R with the largest finite value representable in mode MODE.
663 If SIGN is nonzero, R is set to the most negative finite value. */
665 void
666 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
668 const char *max;
670 switch (mode)
672 case SDmode:
673 max = "9.999999E96";
674 break;
675 case DDmode:
676 max = "9.999999999999999E384";
677 break;
678 case TDmode:
679 max = "9.999999999999999999999999999999999E6144";
680 break;
681 default:
682 gcc_unreachable ();
685 decimal_real_from_string (r, max);
686 if (sign)
687 decimal128SetSign ((decimal128 *) r->sig, 1);