PR tree-optimization/66718
[official-gcc.git] / gcc / dfp.c
blob7c9a86f81a723cada35c7a57b8426126a85458bf
1 /* Decimal floating point support.
2 Copyright (C) 2005-2015 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 "alias.h"
25 #include "symtab.h"
26 #include "tree.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 if (!r->decimal)
116 /* dconst{1,2,m1,half} are used in various places in
117 the middle-end and optimizers, allow them here
118 as an exception by converting them to decimal. */
119 if (memcmp (r, &dconst1, sizeof (*r)) == 0)
121 decNumberFromString (dn, "1", &set);
122 break;
124 if (memcmp (r, &dconst2, sizeof (*r)) == 0)
126 decNumberFromString (dn, "2", &set);
127 break;
129 if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
131 decNumberFromString (dn, "-1", &set);
132 break;
134 if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
136 decNumberFromString (dn, "0.5", &set);
137 break;
139 gcc_unreachable ();
141 decimal128ToNumber ((const decimal128 *) r->sig, dn);
142 break;
143 default:
144 gcc_unreachable ();
147 /* Fix up sign bit. */
148 if (r->sign != decNumberIsNegative (dn))
149 dn->bits ^= DECNEG;
152 /* Encode a real into an IEEE 754 decimal32 type. */
154 void
155 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
156 long *buf, const REAL_VALUE_TYPE *r)
158 decNumber dn;
159 decimal32 d32;
160 decContext set;
161 int32_t image;
163 decContextDefault (&set, DEC_INIT_DECIMAL128);
164 set.traps = 0;
166 decimal_to_decnumber (r, &dn);
167 decimal32FromNumber (&d32, &dn, &set);
169 memcpy (&image, d32.bytes, sizeof (int32_t));
170 buf[0] = image;
173 /* Decode an IEEE 754 decimal32 type into a real. */
175 void
176 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
177 REAL_VALUE_TYPE *r, const long *buf)
179 decNumber dn;
180 decimal32 d32;
181 decContext set;
182 int32_t image;
184 decContextDefault (&set, DEC_INIT_DECIMAL128);
185 set.traps = 0;
187 image = buf[0];
188 memcpy (&d32.bytes, &image, sizeof (int32_t));
190 decimal32ToNumber (&d32, &dn);
191 decimal_from_decnumber (r, &dn, &set);
194 /* Encode a real into an IEEE 754 decimal64 type. */
196 void
197 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
198 long *buf, const REAL_VALUE_TYPE *r)
200 decNumber dn;
201 decimal64 d64;
202 decContext set;
203 int32_t image;
205 decContextDefault (&set, DEC_INIT_DECIMAL128);
206 set.traps = 0;
208 decimal_to_decnumber (r, &dn);
209 decimal64FromNumber (&d64, &dn, &set);
211 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
213 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
214 buf[0] = image;
215 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
216 buf[1] = image;
218 else
220 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
221 buf[0] = image;
222 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
223 buf[1] = image;
227 /* Decode an IEEE 754 decimal64 type into a real. */
229 void
230 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
231 REAL_VALUE_TYPE *r, const long *buf)
233 decNumber dn;
234 decimal64 d64;
235 decContext set;
236 int32_t image;
238 decContextDefault (&set, DEC_INIT_DECIMAL128);
239 set.traps = 0;
241 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
243 image = buf[0];
244 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
245 image = buf[1];
246 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
248 else
250 image = buf[1];
251 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
252 image = buf[0];
253 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
256 decimal64ToNumber (&d64, &dn);
257 decimal_from_decnumber (r, &dn, &set);
260 /* Encode a real into an IEEE 754 decimal128 type. */
262 void
263 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
264 long *buf, const REAL_VALUE_TYPE *r)
266 decNumber dn;
267 decContext set;
268 decimal128 d128;
269 int32_t image;
271 decContextDefault (&set, DEC_INIT_DECIMAL128);
272 set.traps = 0;
274 decimal_to_decnumber (r, &dn);
275 decimal128FromNumber (&d128, &dn, &set);
277 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
279 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
280 buf[0] = image;
281 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
282 buf[1] = image;
283 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
284 buf[2] = image;
285 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
286 buf[3] = image;
288 else
290 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
291 buf[0] = image;
292 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
293 buf[1] = image;
294 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
295 buf[2] = image;
296 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
297 buf[3] = image;
301 /* Decode an IEEE 754 decimal128 type into a real. */
303 void
304 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
305 REAL_VALUE_TYPE *r, const long *buf)
307 decNumber dn;
308 decimal128 d128;
309 decContext set;
310 int32_t image;
312 decContextDefault (&set, DEC_INIT_DECIMAL128);
313 set.traps = 0;
315 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
317 image = buf[0];
318 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
319 image = buf[1];
320 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
321 image = buf[2];
322 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
323 image = buf[3];
324 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
326 else
328 image = buf[3];
329 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
330 image = buf[2];
331 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
332 image = buf[1];
333 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
334 image = buf[0];
335 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
338 decimal128ToNumber (&d128, &dn);
339 decimal_from_decnumber (r, &dn, &set);
342 /* Helper function to convert from a binary real internal
343 representation. */
345 static void
346 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
347 machine_mode mode)
349 char string[256];
350 const decimal128 *const d128 = (const decimal128 *) from->sig;
352 decimal128ToString (d128, string);
353 real_from_string3 (to, string, mode);
357 /* Helper function to convert from a binary real internal
358 representation. */
360 static void
361 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
363 char string[256];
365 /* We convert to string, then to decNumber then to decimal128. */
366 real_to_decimal (string, from, sizeof (string), 0, 1);
367 decimal_real_from_string (to, string);
370 /* Helper function to real.c:do_compare() to handle decimal internal
371 representation including when one of the operands is still in the
372 binary internal representation. */
375 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
376 int nan_result)
378 decContext set;
379 decNumber dn, dn2, dn3;
380 REAL_VALUE_TYPE a1, b1;
382 /* If either operand is non-decimal, create temporary versions. */
383 if (!a->decimal)
385 decimal_from_binary (&a1, a);
386 a = &a1;
388 if (!b->decimal)
390 decimal_from_binary (&b1, b);
391 b = &b1;
394 /* Convert into decNumber form for comparison operation. */
395 decContextDefault (&set, DEC_INIT_DECIMAL128);
396 set.traps = 0;
397 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
398 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
400 /* Finally, do the comparison. */
401 decNumberCompare (&dn, &dn2, &dn3, &set);
403 /* Return the comparison result. */
404 if (decNumberIsNaN (&dn))
405 return nan_result;
406 else if (decNumberIsZero (&dn))
407 return 0;
408 else if (decNumberIsNegative (&dn))
409 return -1;
410 else
411 return 1;
414 /* Helper to round_for_format, handling decimal float types. */
416 void
417 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
419 decNumber dn;
420 decContext set;
422 /* Real encoding occurs later. */
423 if (r->cl != rvc_normal)
424 return;
426 decContextDefault (&set, DEC_INIT_DECIMAL128);
427 set.traps = 0;
428 decimal128ToNumber ((decimal128 *) r->sig, &dn);
430 if (fmt == &decimal_quad_format)
432 /* The internal format is already in this format. */
433 return;
435 else if (fmt == &decimal_single_format)
437 decimal32 d32;
438 decContextDefault (&set, DEC_INIT_DECIMAL32);
439 set.traps = 0;
441 decimal32FromNumber (&d32, &dn, &set);
442 decimal32ToNumber (&d32, &dn);
444 else if (fmt == &decimal_double_format)
446 decimal64 d64;
447 decContextDefault (&set, DEC_INIT_DECIMAL64);
448 set.traps = 0;
450 decimal64FromNumber (&d64, &dn, &set);
451 decimal64ToNumber (&d64, &dn);
453 else
454 gcc_unreachable ();
456 decimal_from_decnumber (r, &dn, &set);
459 /* Extend or truncate to a new mode. Handles conversions between
460 binary and decimal types. */
462 void
463 decimal_real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
464 const REAL_VALUE_TYPE *a)
466 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
468 if (a->decimal && fmt->b == 10)
469 return;
470 if (a->decimal)
471 decimal_to_binary (r, a, mode);
472 else
473 decimal_from_binary (r, a);
476 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
477 significant digits in the result, bounded by BUF_SIZE. If DIGITS
478 is 0, choose the maximum for the representation. If
479 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
480 DIGITS or CROP_TRAILING_ZEROS. */
482 void
483 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
484 size_t buf_size,
485 size_t digits ATTRIBUTE_UNUSED,
486 int crop_trailing_zeros ATTRIBUTE_UNUSED)
488 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
490 /* decimal128ToString requires space for at least 24 characters;
491 Require two more for suffix. */
492 gcc_assert (buf_size >= 24);
493 decimal128ToString (d128, str);
496 static bool
497 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
498 const REAL_VALUE_TYPE *op1, int subtract_p)
500 decNumber dn;
501 decContext set;
502 decNumber dn2, dn3;
504 decimal_to_decnumber (op0, &dn2);
505 decimal_to_decnumber (op1, &dn3);
507 decContextDefault (&set, DEC_INIT_DECIMAL128);
508 set.traps = 0;
510 if (subtract_p)
511 decNumberSubtract (&dn, &dn2, &dn3, &set);
512 else
513 decNumberAdd (&dn, &dn2, &dn3, &set);
515 decimal_from_decnumber (r, &dn, &set);
517 /* Return true, if inexact. */
518 return (set.status & DEC_Inexact);
521 /* Compute R = OP0 * OP1. */
523 static bool
524 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
525 const REAL_VALUE_TYPE *op1)
527 decContext set;
528 decNumber dn, dn2, dn3;
530 decimal_to_decnumber (op0, &dn2);
531 decimal_to_decnumber (op1, &dn3);
533 decContextDefault (&set, DEC_INIT_DECIMAL128);
534 set.traps = 0;
536 decNumberMultiply (&dn, &dn2, &dn3, &set);
537 decimal_from_decnumber (r, &dn, &set);
539 /* Return true, if inexact. */
540 return (set.status & DEC_Inexact);
543 /* Compute R = OP0 / OP1. */
545 static bool
546 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
547 const REAL_VALUE_TYPE *op1)
549 decContext set;
550 decNumber dn, dn2, dn3;
552 decimal_to_decnumber (op0, &dn2);
553 decimal_to_decnumber (op1, &dn3);
555 decContextDefault (&set, DEC_INIT_DECIMAL128);
556 set.traps = 0;
558 decNumberDivide (&dn, &dn2, &dn3, &set);
559 decimal_from_decnumber (r, &dn, &set);
561 /* Return true, if inexact. */
562 return (set.status & DEC_Inexact);
565 /* Set R to A truncated to an integral value toward zero (decimal
566 floating point). */
568 void
569 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
571 decNumber dn, dn2;
572 decContext set;
574 decContextDefault (&set, DEC_INIT_DECIMAL128);
575 set.traps = 0;
576 set.round = DEC_ROUND_DOWN;
577 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
579 decNumberToIntegralValue (&dn, &dn2, &set);
580 decimal_from_decnumber (r, &dn, &set);
583 /* Render decimal float value R as an integer. */
585 HOST_WIDE_INT
586 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
588 decContext set;
589 decNumber dn, dn2, dn3;
590 REAL_VALUE_TYPE to;
591 char string[256];
593 decContextDefault (&set, DEC_INIT_DECIMAL128);
594 set.traps = 0;
595 set.round = DEC_ROUND_DOWN;
596 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
598 decNumberToIntegralValue (&dn2, &dn, &set);
599 decNumberZero (&dn3);
600 decNumberRescale (&dn, &dn2, &dn3, &set);
602 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
603 function. */
604 decNumberToString (&dn, string);
605 real_from_string (&to, string);
606 return real_to_integer (&to);
609 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
610 value does not fit. */
612 wide_int
613 decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
615 decContext set;
616 decNumber dn, dn2, dn3;
617 REAL_VALUE_TYPE to;
618 char string[256];
620 decContextDefault (&set, DEC_INIT_DECIMAL128);
621 set.traps = 0;
622 set.round = DEC_ROUND_DOWN;
623 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
625 decNumberToIntegralValue (&dn2, &dn, &set);
626 decNumberZero (&dn3);
627 decNumberRescale (&dn, &dn2, &dn3, &set);
629 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
630 function. */
631 decNumberToString (&dn, string);
632 real_from_string (&to, string);
633 return real_to_integer (&to, fail, precision);
636 /* Perform the decimal floating point operation described by CODE.
637 For a unary operation, OP1 will be NULL. This function returns
638 true if the result may be inexact due to loss of precision. */
640 bool
641 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
642 const REAL_VALUE_TYPE *op0,
643 const REAL_VALUE_TYPE *op1)
645 REAL_VALUE_TYPE a, b;
647 /* If either operand is non-decimal, create temporaries. */
648 if (!op0->decimal)
650 decimal_from_binary (&a, op0);
651 op0 = &a;
653 if (op1 && !op1->decimal)
655 decimal_from_binary (&b, op1);
656 op1 = &b;
659 switch (code)
661 case PLUS_EXPR:
662 return decimal_do_add (r, op0, op1, 0);
664 case MINUS_EXPR:
665 return decimal_do_add (r, op0, op1, 1);
667 case MULT_EXPR:
668 return decimal_do_multiply (r, op0, op1);
670 case RDIV_EXPR:
671 return decimal_do_divide (r, op0, op1);
673 case MIN_EXPR:
674 if (op1->cl == rvc_nan)
675 *r = *op1;
676 else if (real_compare (UNLT_EXPR, op0, op1))
677 *r = *op0;
678 else
679 *r = *op1;
680 return false;
682 case MAX_EXPR:
683 if (op1->cl == rvc_nan)
684 *r = *op1;
685 else if (real_compare (LT_EXPR, op0, op1))
686 *r = *op1;
687 else
688 *r = *op0;
689 return false;
691 case NEGATE_EXPR:
693 *r = *op0;
694 /* Flip sign bit. */
695 decimal128FlipSign ((decimal128 *) r->sig);
696 /* Keep sign field in sync. */
697 r->sign ^= 1;
699 return false;
701 case ABS_EXPR:
703 *r = *op0;
704 /* Clear sign bit. */
705 decimal128ClearSign ((decimal128 *) r->sig);
706 /* Keep sign field in sync. */
707 r->sign = 0;
709 return false;
711 case FIX_TRUNC_EXPR:
712 decimal_do_fix_trunc (r, op0);
713 return false;
715 default:
716 gcc_unreachable ();
720 /* Fills R with the largest finite value representable in mode MODE.
721 If SIGN is nonzero, R is set to the most negative finite value. */
723 void
724 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
726 const char *max;
728 switch (mode)
730 case SDmode:
731 max = "9.999999E96";
732 break;
733 case DDmode:
734 max = "9.999999999999999E384";
735 break;
736 case TDmode:
737 max = "9.999999999999999999999999999999999E6144";
738 break;
739 default:
740 gcc_unreachable ();
743 decimal_real_from_string (r, max);
744 if (sign)
745 decimal128SetSign ((decimal128 *) r->sig, 1);