2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dfp.c
blob6c8a6c7f2a176cdc4ac897899597c32cb2708c88
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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "dfp.h"
31 /* The order of the following headers is important for making sure
32 decNumber structure is large enough to hold decimal128 digits. */
34 #include "decimal128.h"
35 #include "decimal128Local.h"
36 #include "decimal64.h"
37 #include "decimal32.h"
38 #include "decNumber.h"
40 #ifndef WORDS_BIGENDIAN
41 #define WORDS_BIGENDIAN 0
42 #endif
44 /* Initialize R (a real with the decimal flag set) from DN. Can
45 utilize status passed in via CONTEXT, if a previous operation had
46 interesting status. */
48 static void
49 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
51 memset (r, 0, sizeof (REAL_VALUE_TYPE));
53 r->cl = rvc_normal;
54 if (decNumberIsNaN (dn))
55 r->cl = rvc_nan;
56 if (decNumberIsInfinite (dn))
57 r->cl = rvc_inf;
58 if (context->status & DEC_Overflow)
59 r->cl = rvc_inf;
60 if (decNumberIsNegative (dn))
61 r->sign = 1;
62 r->decimal = 1;
64 if (r->cl != rvc_normal)
65 return;
67 decContextDefault (context, DEC_INIT_DECIMAL128);
68 context->traps = 0;
70 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
73 /* Create decimal encoded R from string S. */
75 void
76 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
78 decNumber dn;
79 decContext set;
80 decContextDefault (&set, DEC_INIT_DECIMAL128);
81 set.traps = 0;
83 decNumberFromString (&dn, s, &set);
85 /* It would be more efficient to store directly in decNumber format,
86 but that is impractical from current data structure size.
87 Encoding as a decimal128 is much more compact. */
88 decimal_from_decnumber (r, &dn, &set);
91 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
93 static void
94 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
96 decContext set;
97 decContextDefault (&set, DEC_INIT_DECIMAL128);
98 set.traps = 0;
100 switch (r->cl)
102 case rvc_zero:
103 decNumberZero (dn);
104 break;
105 case rvc_inf:
106 decNumberFromString (dn, "Infinity", &set);
107 break;
108 case rvc_nan:
109 if (r->signalling)
110 decNumberFromString (dn, "snan", &set);
111 else
112 decNumberFromString (dn, "nan", &set);
113 break;
114 case rvc_normal:
115 if (!r->decimal)
117 /* dconst{1,2,m1,half} are used in various places in
118 the middle-end and optimizers, allow them here
119 as an exception by converting them to decimal. */
120 if (memcmp (r, &dconst1, sizeof (*r)) == 0)
122 decNumberFromString (dn, "1", &set);
123 break;
125 if (memcmp (r, &dconst2, sizeof (*r)) == 0)
127 decNumberFromString (dn, "2", &set);
128 break;
130 if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
132 decNumberFromString (dn, "-1", &set);
133 break;
135 if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
137 decNumberFromString (dn, "0.5", &set);
138 break;
140 gcc_unreachable ();
142 decimal128ToNumber ((const decimal128 *) r->sig, dn);
143 break;
144 default:
145 gcc_unreachable ();
148 /* Fix up sign bit. */
149 if (r->sign != decNumberIsNegative (dn))
150 dn->bits ^= DECNEG;
153 /* Encode a real into an IEEE 754 decimal32 type. */
155 void
156 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
157 long *buf, const REAL_VALUE_TYPE *r)
159 decNumber dn;
160 decimal32 d32;
161 decContext set;
162 int32_t image;
164 decContextDefault (&set, DEC_INIT_DECIMAL128);
165 set.traps = 0;
167 decimal_to_decnumber (r, &dn);
168 decimal32FromNumber (&d32, &dn, &set);
170 memcpy (&image, d32.bytes, sizeof (int32_t));
171 buf[0] = image;
174 /* Decode an IEEE 754 decimal32 type into a real. */
176 void
177 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
178 REAL_VALUE_TYPE *r, const long *buf)
180 decNumber dn;
181 decimal32 d32;
182 decContext set;
183 int32_t image;
185 decContextDefault (&set, DEC_INIT_DECIMAL128);
186 set.traps = 0;
188 image = buf[0];
189 memcpy (&d32.bytes, &image, sizeof (int32_t));
191 decimal32ToNumber (&d32, &dn);
192 decimal_from_decnumber (r, &dn, &set);
195 /* Encode a real into an IEEE 754 decimal64 type. */
197 void
198 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
199 long *buf, const REAL_VALUE_TYPE *r)
201 decNumber dn;
202 decimal64 d64;
203 decContext set;
204 int32_t image;
206 decContextDefault (&set, DEC_INIT_DECIMAL128);
207 set.traps = 0;
209 decimal_to_decnumber (r, &dn);
210 decimal64FromNumber (&d64, &dn, &set);
212 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
214 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
215 buf[0] = image;
216 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
217 buf[1] = image;
219 else
221 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
222 buf[0] = image;
223 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
224 buf[1] = image;
228 /* Decode an IEEE 754 decimal64 type into a real. */
230 void
231 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
232 REAL_VALUE_TYPE *r, const long *buf)
234 decNumber dn;
235 decimal64 d64;
236 decContext set;
237 int32_t image;
239 decContextDefault (&set, DEC_INIT_DECIMAL128);
240 set.traps = 0;
242 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
244 image = buf[0];
245 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
246 image = buf[1];
247 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
249 else
251 image = buf[1];
252 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
253 image = buf[0];
254 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
257 decimal64ToNumber (&d64, &dn);
258 decimal_from_decnumber (r, &dn, &set);
261 /* Encode a real into an IEEE 754 decimal128 type. */
263 void
264 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
265 long *buf, const REAL_VALUE_TYPE *r)
267 decNumber dn;
268 decContext set;
269 decimal128 d128;
270 int32_t image;
272 decContextDefault (&set, DEC_INIT_DECIMAL128);
273 set.traps = 0;
275 decimal_to_decnumber (r, &dn);
276 decimal128FromNumber (&d128, &dn, &set);
278 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
280 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
281 buf[0] = image;
282 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
283 buf[1] = image;
284 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
285 buf[2] = image;
286 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
287 buf[3] = image;
289 else
291 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
292 buf[0] = image;
293 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
294 buf[1] = image;
295 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
296 buf[2] = image;
297 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
298 buf[3] = image;
302 /* Decode an IEEE 754 decimal128 type into a real. */
304 void
305 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
306 REAL_VALUE_TYPE *r, const long *buf)
308 decNumber dn;
309 decimal128 d128;
310 decContext set;
311 int32_t image;
313 decContextDefault (&set, DEC_INIT_DECIMAL128);
314 set.traps = 0;
316 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
318 image = buf[0];
319 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
320 image = buf[1];
321 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
322 image = buf[2];
323 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
324 image = buf[3];
325 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
327 else
329 image = buf[3];
330 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
331 image = buf[2];
332 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
333 image = buf[1];
334 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
335 image = buf[0];
336 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
339 decimal128ToNumber (&d128, &dn);
340 decimal_from_decnumber (r, &dn, &set);
343 /* Helper function to convert from a binary real internal
344 representation. */
346 static void
347 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
348 machine_mode mode)
350 char string[256];
351 const decimal128 *const d128 = (const decimal128 *) from->sig;
353 decimal128ToString (d128, string);
354 real_from_string3 (to, string, mode);
358 /* Helper function to convert from a binary real internal
359 representation. */
361 static void
362 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
364 char string[256];
366 /* We convert to string, then to decNumber then to decimal128. */
367 real_to_decimal (string, from, sizeof (string), 0, 1);
368 decimal_real_from_string (to, string);
371 /* Helper function to real.c:do_compare() to handle decimal internal
372 representation including when one of the operands is still in the
373 binary internal representation. */
376 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
377 int nan_result)
379 decContext set;
380 decNumber dn, dn2, dn3;
381 REAL_VALUE_TYPE a1, b1;
383 /* If either operand is non-decimal, create temporary versions. */
384 if (!a->decimal)
386 decimal_from_binary (&a1, a);
387 a = &a1;
389 if (!b->decimal)
391 decimal_from_binary (&b1, b);
392 b = &b1;
395 /* Convert into decNumber form for comparison operation. */
396 decContextDefault (&set, DEC_INIT_DECIMAL128);
397 set.traps = 0;
398 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
399 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
401 /* Finally, do the comparison. */
402 decNumberCompare (&dn, &dn2, &dn3, &set);
404 /* Return the comparison result. */
405 if (decNumberIsNaN (&dn))
406 return nan_result;
407 else if (decNumberIsZero (&dn))
408 return 0;
409 else if (decNumberIsNegative (&dn))
410 return -1;
411 else
412 return 1;
415 /* Helper to round_for_format, handling decimal float types. */
417 void
418 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
420 decNumber dn;
421 decContext set;
423 /* Real encoding occurs later. */
424 if (r->cl != rvc_normal)
425 return;
427 decContextDefault (&set, DEC_INIT_DECIMAL128);
428 set.traps = 0;
429 decimal128ToNumber ((decimal128 *) r->sig, &dn);
431 if (fmt == &decimal_quad_format)
433 /* The internal format is already in this format. */
434 return;
436 else if (fmt == &decimal_single_format)
438 decimal32 d32;
439 decContextDefault (&set, DEC_INIT_DECIMAL32);
440 set.traps = 0;
442 decimal32FromNumber (&d32, &dn, &set);
443 decimal32ToNumber (&d32, &dn);
445 else if (fmt == &decimal_double_format)
447 decimal64 d64;
448 decContextDefault (&set, DEC_INIT_DECIMAL64);
449 set.traps = 0;
451 decimal64FromNumber (&d64, &dn, &set);
452 decimal64ToNumber (&d64, &dn);
454 else
455 gcc_unreachable ();
457 decimal_from_decnumber (r, &dn, &set);
460 /* Extend or truncate to a new mode. Handles conversions between
461 binary and decimal types. */
463 void
464 decimal_real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
465 const REAL_VALUE_TYPE *a)
467 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
469 if (a->decimal && fmt->b == 10)
470 return;
471 if (a->decimal)
472 decimal_to_binary (r, a, mode);
473 else
474 decimal_from_binary (r, a);
477 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
478 significant digits in the result, bounded by BUF_SIZE. If DIGITS
479 is 0, choose the maximum for the representation. If
480 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
481 DIGITS or CROP_TRAILING_ZEROS. */
483 void
484 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
485 size_t buf_size,
486 size_t digits ATTRIBUTE_UNUSED,
487 int crop_trailing_zeros ATTRIBUTE_UNUSED)
489 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
491 /* decimal128ToString requires space for at least 24 characters;
492 Require two more for suffix. */
493 gcc_assert (buf_size >= 24);
494 decimal128ToString (d128, str);
497 static bool
498 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
499 const REAL_VALUE_TYPE *op1, int subtract_p)
501 decNumber dn;
502 decContext set;
503 decNumber dn2, dn3;
505 decimal_to_decnumber (op0, &dn2);
506 decimal_to_decnumber (op1, &dn3);
508 decContextDefault (&set, DEC_INIT_DECIMAL128);
509 set.traps = 0;
511 if (subtract_p)
512 decNumberSubtract (&dn, &dn2, &dn3, &set);
513 else
514 decNumberAdd (&dn, &dn2, &dn3, &set);
516 decimal_from_decnumber (r, &dn, &set);
518 /* Return true, if inexact. */
519 return (set.status & DEC_Inexact);
522 /* Compute R = OP0 * OP1. */
524 static bool
525 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
526 const REAL_VALUE_TYPE *op1)
528 decContext set;
529 decNumber dn, dn2, dn3;
531 decimal_to_decnumber (op0, &dn2);
532 decimal_to_decnumber (op1, &dn3);
534 decContextDefault (&set, DEC_INIT_DECIMAL128);
535 set.traps = 0;
537 decNumberMultiply (&dn, &dn2, &dn3, &set);
538 decimal_from_decnumber (r, &dn, &set);
540 /* Return true, if inexact. */
541 return (set.status & DEC_Inexact);
544 /* Compute R = OP0 / OP1. */
546 static bool
547 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
548 const REAL_VALUE_TYPE *op1)
550 decContext set;
551 decNumber dn, dn2, dn3;
553 decimal_to_decnumber (op0, &dn2);
554 decimal_to_decnumber (op1, &dn3);
556 decContextDefault (&set, DEC_INIT_DECIMAL128);
557 set.traps = 0;
559 decNumberDivide (&dn, &dn2, &dn3, &set);
560 decimal_from_decnumber (r, &dn, &set);
562 /* Return true, if inexact. */
563 return (set.status & DEC_Inexact);
566 /* Set R to A truncated to an integral value toward zero (decimal
567 floating point). */
569 void
570 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
572 decNumber dn, dn2;
573 decContext set;
575 decContextDefault (&set, DEC_INIT_DECIMAL128);
576 set.traps = 0;
577 set.round = DEC_ROUND_DOWN;
578 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
580 decNumberToIntegralValue (&dn, &dn2, &set);
581 decimal_from_decnumber (r, &dn, &set);
584 /* Render decimal float value R as an integer. */
586 HOST_WIDE_INT
587 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
589 decContext set;
590 decNumber dn, dn2, dn3;
591 REAL_VALUE_TYPE to;
592 char string[256];
594 decContextDefault (&set, DEC_INIT_DECIMAL128);
595 set.traps = 0;
596 set.round = DEC_ROUND_DOWN;
597 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
599 decNumberToIntegralValue (&dn2, &dn, &set);
600 decNumberZero (&dn3);
601 decNumberRescale (&dn, &dn2, &dn3, &set);
603 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
604 function. */
605 decNumberToString (&dn, string);
606 real_from_string (&to, string);
607 return real_to_integer (&to);
610 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
611 value does not fit. */
613 wide_int
614 decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
616 decContext set;
617 decNumber dn, dn2, dn3;
618 REAL_VALUE_TYPE to;
619 char string[256];
621 decContextDefault (&set, DEC_INIT_DECIMAL128);
622 set.traps = 0;
623 set.round = DEC_ROUND_DOWN;
624 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
626 decNumberToIntegralValue (&dn2, &dn, &set);
627 decNumberZero (&dn3);
628 decNumberRescale (&dn, &dn2, &dn3, &set);
630 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
631 function. */
632 decNumberToString (&dn, string);
633 real_from_string (&to, string);
634 return real_to_integer (&to, fail, precision);
637 /* Perform the decimal floating point operation described by CODE.
638 For a unary operation, OP1 will be NULL. This function returns
639 true if the result may be inexact due to loss of precision. */
641 bool
642 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
643 const REAL_VALUE_TYPE *op0,
644 const REAL_VALUE_TYPE *op1)
646 REAL_VALUE_TYPE a, b;
648 /* If either operand is non-decimal, create temporaries. */
649 if (!op0->decimal)
651 decimal_from_binary (&a, op0);
652 op0 = &a;
654 if (op1 && !op1->decimal)
656 decimal_from_binary (&b, op1);
657 op1 = &b;
660 switch (code)
662 case PLUS_EXPR:
663 return decimal_do_add (r, op0, op1, 0);
665 case MINUS_EXPR:
666 return decimal_do_add (r, op0, op1, 1);
668 case MULT_EXPR:
669 return decimal_do_multiply (r, op0, op1);
671 case RDIV_EXPR:
672 return decimal_do_divide (r, op0, op1);
674 case MIN_EXPR:
675 if (op1->cl == rvc_nan)
676 *r = *op1;
677 else if (real_compare (UNLT_EXPR, op0, op1))
678 *r = *op0;
679 else
680 *r = *op1;
681 return false;
683 case MAX_EXPR:
684 if (op1->cl == rvc_nan)
685 *r = *op1;
686 else if (real_compare (LT_EXPR, op0, op1))
687 *r = *op1;
688 else
689 *r = *op0;
690 return false;
692 case NEGATE_EXPR:
694 *r = *op0;
695 /* Flip sign bit. */
696 decimal128FlipSign ((decimal128 *) r->sig);
697 /* Keep sign field in sync. */
698 r->sign ^= 1;
700 return false;
702 case ABS_EXPR:
704 *r = *op0;
705 /* Clear sign bit. */
706 decimal128ClearSign ((decimal128 *) r->sig);
707 /* Keep sign field in sync. */
708 r->sign = 0;
710 return false;
712 case FIX_TRUNC_EXPR:
713 decimal_do_fix_trunc (r, op0);
714 return false;
716 default:
717 gcc_unreachable ();
721 /* Fills R with the largest finite value representable in mode MODE.
722 If SIGN is nonzero, R is set to the most negative finite value. */
724 void
725 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
727 const char *max;
729 switch (mode)
731 case SDmode:
732 max = "9.999999E96";
733 break;
734 case DDmode:
735 max = "9.999999999999999E384";
736 break;
737 case TDmode:
738 max = "9.999999999999999999999999999999999E6144";
739 break;
740 default:
741 gcc_unreachable ();
744 decimal_real_from_string (r, max);
745 if (sign)
746 decimal128SetSign ((decimal128 *) r->sig, 1);