2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / dfp.c
blob4f2abb195dc0b53e46ec39ddb60243b44ec41ea6
1 /* Decimal floating point support.
2 Copyright (C) 2005-2014 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 "tm_p.h"
26 #include "dfp.h"
28 /* The order of the following headers is important for making sure
29 decNumber structure is large enough to hold decimal128 digits. */
31 #include "decimal128.h"
32 #include "decimal128Local.h"
33 #include "decimal64.h"
34 #include "decimal32.h"
35 #include "decNumber.h"
37 #ifndef WORDS_BIGENDIAN
38 #define WORDS_BIGENDIAN 0
39 #endif
41 /* Initialize R (a real with the decimal flag set) from DN. Can
42 utilize status passed in via CONTEXT, if a previous operation had
43 interesting status. */
45 static void
46 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
48 memset (r, 0, sizeof (REAL_VALUE_TYPE));
50 r->cl = rvc_normal;
51 if (decNumberIsNaN (dn))
52 r->cl = rvc_nan;
53 if (decNumberIsInfinite (dn))
54 r->cl = rvc_inf;
55 if (context->status & DEC_Overflow)
56 r->cl = rvc_inf;
57 if (decNumberIsNegative (dn))
58 r->sign = 1;
59 r->decimal = 1;
61 if (r->cl != rvc_normal)
62 return;
64 decContextDefault (context, DEC_INIT_DECIMAL128);
65 context->traps = 0;
67 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
70 /* Create decimal encoded R from string S. */
72 void
73 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
75 decNumber dn;
76 decContext set;
77 decContextDefault (&set, DEC_INIT_DECIMAL128);
78 set.traps = 0;
80 decNumberFromString (&dn, s, &set);
82 /* It would be more efficient to store directly in decNumber format,
83 but that is impractical from current data structure size.
84 Encoding as a decimal128 is much more compact. */
85 decimal_from_decnumber (r, &dn, &set);
88 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
90 static void
91 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
93 decContext set;
94 decContextDefault (&set, DEC_INIT_DECIMAL128);
95 set.traps = 0;
97 switch (r->cl)
99 case rvc_zero:
100 decNumberZero (dn);
101 break;
102 case rvc_inf:
103 decNumberFromString (dn, "Infinity", &set);
104 break;
105 case rvc_nan:
106 if (r->signalling)
107 decNumberFromString (dn, "snan", &set);
108 else
109 decNumberFromString (dn, "nan", &set);
110 break;
111 case rvc_normal:
112 if (!r->decimal)
114 /* dconst{1,2,m1,half} are used in various places in
115 the middle-end and optimizers, allow them here
116 as an exception by converting them to decimal. */
117 if (memcmp (r, &dconst1, sizeof (*r)) == 0)
119 decNumberFromString (dn, "1", &set);
120 break;
122 if (memcmp (r, &dconst2, sizeof (*r)) == 0)
124 decNumberFromString (dn, "2", &set);
125 break;
127 if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
129 decNumberFromString (dn, "-1", &set);
130 break;
132 if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
134 decNumberFromString (dn, "0.5", &set);
135 break;
137 gcc_unreachable ();
139 decimal128ToNumber ((const decimal128 *) r->sig, dn);
140 break;
141 default:
142 gcc_unreachable ();
145 /* Fix up sign bit. */
146 if (r->sign != decNumberIsNegative (dn))
147 dn->bits ^= DECNEG;
150 /* Encode a real into an IEEE 754 decimal32 type. */
152 void
153 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
154 long *buf, const REAL_VALUE_TYPE *r)
156 decNumber dn;
157 decimal32 d32;
158 decContext set;
159 int32_t image;
161 decContextDefault (&set, DEC_INIT_DECIMAL128);
162 set.traps = 0;
164 decimal_to_decnumber (r, &dn);
165 decimal32FromNumber (&d32, &dn, &set);
167 memcpy (&image, d32.bytes, sizeof (int32_t));
168 buf[0] = image;
171 /* Decode an IEEE 754 decimal32 type into a real. */
173 void
174 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
175 REAL_VALUE_TYPE *r, const long *buf)
177 decNumber dn;
178 decimal32 d32;
179 decContext set;
180 int32_t image;
182 decContextDefault (&set, DEC_INIT_DECIMAL128);
183 set.traps = 0;
185 image = buf[0];
186 memcpy (&d32.bytes, &image, sizeof (int32_t));
188 decimal32ToNumber (&d32, &dn);
189 decimal_from_decnumber (r, &dn, &set);
192 /* Encode a real into an IEEE 754 decimal64 type. */
194 void
195 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
196 long *buf, const REAL_VALUE_TYPE *r)
198 decNumber dn;
199 decimal64 d64;
200 decContext set;
201 int32_t image;
203 decContextDefault (&set, DEC_INIT_DECIMAL128);
204 set.traps = 0;
206 decimal_to_decnumber (r, &dn);
207 decimal64FromNumber (&d64, &dn, &set);
209 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
211 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
212 buf[0] = image;
213 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
214 buf[1] = image;
216 else
218 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
219 buf[0] = image;
220 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
221 buf[1] = image;
225 /* Decode an IEEE 754 decimal64 type into a real. */
227 void
228 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
229 REAL_VALUE_TYPE *r, const long *buf)
231 decNumber dn;
232 decimal64 d64;
233 decContext set;
234 int32_t image;
236 decContextDefault (&set, DEC_INIT_DECIMAL128);
237 set.traps = 0;
239 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
241 image = buf[0];
242 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
243 image = buf[1];
244 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
246 else
248 image = buf[1];
249 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
250 image = buf[0];
251 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
254 decimal64ToNumber (&d64, &dn);
255 decimal_from_decnumber (r, &dn, &set);
258 /* Encode a real into an IEEE 754 decimal128 type. */
260 void
261 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
262 long *buf, const REAL_VALUE_TYPE *r)
264 decNumber dn;
265 decContext set;
266 decimal128 d128;
267 int32_t image;
269 decContextDefault (&set, DEC_INIT_DECIMAL128);
270 set.traps = 0;
272 decimal_to_decnumber (r, &dn);
273 decimal128FromNumber (&d128, &dn, &set);
275 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
277 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
278 buf[0] = image;
279 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
280 buf[1] = image;
281 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
282 buf[2] = image;
283 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
284 buf[3] = image;
286 else
288 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
289 buf[0] = image;
290 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
291 buf[1] = image;
292 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
293 buf[2] = image;
294 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
295 buf[3] = image;
299 /* Decode an IEEE 754 decimal128 type into a real. */
301 void
302 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
303 REAL_VALUE_TYPE *r, const long *buf)
305 decNumber dn;
306 decimal128 d128;
307 decContext set;
308 int32_t image;
310 decContextDefault (&set, DEC_INIT_DECIMAL128);
311 set.traps = 0;
313 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
315 image = buf[0];
316 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
317 image = buf[1];
318 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
319 image = buf[2];
320 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
321 image = buf[3];
322 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
324 else
326 image = buf[3];
327 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
328 image = buf[2];
329 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
330 image = buf[1];
331 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
332 image = buf[0];
333 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
336 decimal128ToNumber (&d128, &dn);
337 decimal_from_decnumber (r, &dn, &set);
340 /* Helper function to convert from a binary real internal
341 representation. */
343 static void
344 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
345 enum machine_mode mode)
347 char string[256];
348 const decimal128 *const d128 = (const decimal128 *) from->sig;
350 decimal128ToString (d128, string);
351 real_from_string3 (to, string, mode);
355 /* Helper function to convert from a binary real internal
356 representation. */
358 static void
359 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
361 char string[256];
363 /* We convert to string, then to decNumber then to decimal128. */
364 real_to_decimal (string, from, sizeof (string), 0, 1);
365 decimal_real_from_string (to, string);
368 /* Helper function to real.c:do_compare() to handle decimal internal
369 representation including when one of the operands is still in the
370 binary internal representation. */
373 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
374 int nan_result)
376 decContext set;
377 decNumber dn, dn2, dn3;
378 REAL_VALUE_TYPE a1, b1;
380 /* If either operand is non-decimal, create temporary versions. */
381 if (!a->decimal)
383 decimal_from_binary (&a1, a);
384 a = &a1;
386 if (!b->decimal)
388 decimal_from_binary (&b1, b);
389 b = &b1;
392 /* Convert into decNumber form for comparison operation. */
393 decContextDefault (&set, DEC_INIT_DECIMAL128);
394 set.traps = 0;
395 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
396 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
398 /* Finally, do the comparison. */
399 decNumberCompare (&dn, &dn2, &dn3, &set);
401 /* Return the comparison result. */
402 if (decNumberIsNaN (&dn))
403 return nan_result;
404 else if (decNumberIsZero (&dn))
405 return 0;
406 else if (decNumberIsNegative (&dn))
407 return -1;
408 else
409 return 1;
412 /* Helper to round_for_format, handling decimal float types. */
414 void
415 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
417 decNumber dn;
418 decContext set;
420 /* Real encoding occurs later. */
421 if (r->cl != rvc_normal)
422 return;
424 decContextDefault (&set, DEC_INIT_DECIMAL128);
425 set.traps = 0;
426 decimal128ToNumber ((decimal128 *) r->sig, &dn);
428 if (fmt == &decimal_quad_format)
430 /* The internal format is already in this format. */
431 return;
433 else if (fmt == &decimal_single_format)
435 decimal32 d32;
436 decContextDefault (&set, DEC_INIT_DECIMAL32);
437 set.traps = 0;
439 decimal32FromNumber (&d32, &dn, &set);
440 decimal32ToNumber (&d32, &dn);
442 else if (fmt == &decimal_double_format)
444 decimal64 d64;
445 decContextDefault (&set, DEC_INIT_DECIMAL64);
446 set.traps = 0;
448 decimal64FromNumber (&d64, &dn, &set);
449 decimal64ToNumber (&d64, &dn);
451 else
452 gcc_unreachable ();
454 decimal_from_decnumber (r, &dn, &set);
457 /* Extend or truncate to a new mode. Handles conversions between
458 binary and decimal types. */
460 void
461 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
462 const REAL_VALUE_TYPE *a)
464 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
466 if (a->decimal && fmt->b == 10)
467 return;
468 if (a->decimal)
469 decimal_to_binary (r, a, mode);
470 else
471 decimal_from_binary (r, a);
474 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
475 significant digits in the result, bounded by BUF_SIZE. If DIGITS
476 is 0, choose the maximum for the representation. If
477 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
478 DIGITS or CROP_TRAILING_ZEROS. */
480 void
481 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
482 size_t buf_size,
483 size_t digits ATTRIBUTE_UNUSED,
484 int crop_trailing_zeros ATTRIBUTE_UNUSED)
486 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
488 /* decimal128ToString requires space for at least 24 characters;
489 Require two more for suffix. */
490 gcc_assert (buf_size >= 24);
491 decimal128ToString (d128, str);
494 static bool
495 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
496 const REAL_VALUE_TYPE *op1, int subtract_p)
498 decNumber dn;
499 decContext set;
500 decNumber dn2, dn3;
502 decimal_to_decnumber (op0, &dn2);
503 decimal_to_decnumber (op1, &dn3);
505 decContextDefault (&set, DEC_INIT_DECIMAL128);
506 set.traps = 0;
508 if (subtract_p)
509 decNumberSubtract (&dn, &dn2, &dn3, &set);
510 else
511 decNumberAdd (&dn, &dn2, &dn3, &set);
513 decimal_from_decnumber (r, &dn, &set);
515 /* Return true, if inexact. */
516 return (set.status & DEC_Inexact);
519 /* Compute R = OP0 * OP1. */
521 static bool
522 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
523 const REAL_VALUE_TYPE *op1)
525 decContext set;
526 decNumber dn, dn2, dn3;
528 decimal_to_decnumber (op0, &dn2);
529 decimal_to_decnumber (op1, &dn3);
531 decContextDefault (&set, DEC_INIT_DECIMAL128);
532 set.traps = 0;
534 decNumberMultiply (&dn, &dn2, &dn3, &set);
535 decimal_from_decnumber (r, &dn, &set);
537 /* Return true, if inexact. */
538 return (set.status & DEC_Inexact);
541 /* Compute R = OP0 / OP1. */
543 static bool
544 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
545 const REAL_VALUE_TYPE *op1)
547 decContext set;
548 decNumber dn, dn2, dn3;
550 decimal_to_decnumber (op0, &dn2);
551 decimal_to_decnumber (op1, &dn3);
553 decContextDefault (&set, DEC_INIT_DECIMAL128);
554 set.traps = 0;
556 decNumberDivide (&dn, &dn2, &dn3, &set);
557 decimal_from_decnumber (r, &dn, &set);
559 /* Return true, if inexact. */
560 return (set.status & DEC_Inexact);
563 /* Set R to A truncated to an integral value toward zero (decimal
564 floating point). */
566 void
567 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
569 decNumber dn, dn2;
570 decContext set;
572 decContextDefault (&set, DEC_INIT_DECIMAL128);
573 set.traps = 0;
574 set.round = DEC_ROUND_DOWN;
575 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
577 decNumberToIntegralValue (&dn, &dn2, &set);
578 decimal_from_decnumber (r, &dn, &set);
581 /* Render decimal float value R as an integer. */
583 HOST_WIDE_INT
584 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
586 decContext set;
587 decNumber dn, dn2, dn3;
588 REAL_VALUE_TYPE to;
589 char string[256];
591 decContextDefault (&set, DEC_INIT_DECIMAL128);
592 set.traps = 0;
593 set.round = DEC_ROUND_DOWN;
594 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
596 decNumberToIntegralValue (&dn2, &dn, &set);
597 decNumberZero (&dn3);
598 decNumberRescale (&dn, &dn2, &dn3, &set);
600 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
601 function. */
602 decNumberToString (&dn, string);
603 real_from_string (&to, string);
604 return real_to_integer (&to);
607 /* Likewise, but to an integer pair, HI+LOW. */
609 void
610 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
611 const REAL_VALUE_TYPE *r)
613 decContext set;
614 decNumber dn, dn2, dn3;
615 REAL_VALUE_TYPE to;
616 char string[256];
618 decContextDefault (&set, DEC_INIT_DECIMAL128);
619 set.traps = 0;
620 set.round = DEC_ROUND_DOWN;
621 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
623 decNumberToIntegralValue (&dn2, &dn, &set);
624 decNumberZero (&dn3);
625 decNumberRescale (&dn, &dn2, &dn3, &set);
627 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
628 function. */
629 decNumberToString (&dn, string);
630 real_from_string (&to, string);
631 real_to_integer2 (plow, phigh, &to);
634 /* Perform the decimal floating point operation described by CODE.
635 For a unary operation, OP1 will be NULL. This function returns
636 true if the result may be inexact due to loss of precision. */
638 bool
639 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
640 const REAL_VALUE_TYPE *op0,
641 const REAL_VALUE_TYPE *op1)
643 REAL_VALUE_TYPE a, b;
645 /* If either operand is non-decimal, create temporaries. */
646 if (!op0->decimal)
648 decimal_from_binary (&a, op0);
649 op0 = &a;
651 if (op1 && !op1->decimal)
653 decimal_from_binary (&b, op1);
654 op1 = &b;
657 switch (code)
659 case PLUS_EXPR:
660 return decimal_do_add (r, op0, op1, 0);
662 case MINUS_EXPR:
663 return decimal_do_add (r, op0, op1, 1);
665 case MULT_EXPR:
666 return decimal_do_multiply (r, op0, op1);
668 case RDIV_EXPR:
669 return decimal_do_divide (r, op0, op1);
671 case MIN_EXPR:
672 if (op1->cl == rvc_nan)
673 *r = *op1;
674 else if (real_compare (UNLT_EXPR, op0, op1))
675 *r = *op0;
676 else
677 *r = *op1;
678 return false;
680 case MAX_EXPR:
681 if (op1->cl == rvc_nan)
682 *r = *op1;
683 else if (real_compare (LT_EXPR, op0, op1))
684 *r = *op1;
685 else
686 *r = *op0;
687 return false;
689 case NEGATE_EXPR:
691 *r = *op0;
692 /* Flip sign bit. */
693 decimal128FlipSign ((decimal128 *) r->sig);
694 /* Keep sign field in sync. */
695 r->sign ^= 1;
697 return false;
699 case ABS_EXPR:
701 *r = *op0;
702 /* Clear sign bit. */
703 decimal128ClearSign ((decimal128 *) r->sig);
704 /* Keep sign field in sync. */
705 r->sign = 0;
707 return false;
709 case FIX_TRUNC_EXPR:
710 decimal_do_fix_trunc (r, op0);
711 return false;
713 default:
714 gcc_unreachable ();
718 /* Fills R with the largest finite value representable in mode MODE.
719 If SIGN is nonzero, R is set to the most negative finite value. */
721 void
722 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
724 const char *max;
726 switch (mode)
728 case SDmode:
729 max = "9.999999E96";
730 break;
731 case DDmode:
732 max = "9.999999999999999E384";
733 break;
734 case TDmode:
735 max = "9.999999999999999999999999999999999E6144";
736 break;
737 default:
738 gcc_unreachable ();
741 decimal_real_from_string (r, max);
742 if (sign)
743 decimal128SetSign ((decimal128 *) r->sig, 1);