PR tree-optimization/48734
[official-gcc.git] / gcc / dfp.c
blob5a18db9f52fb5b1da93a1fdbb0dd6a1c4995daf9
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "dfp.h"
29 /* The order of the following headers is important for making sure
30 decNumber structure is large enough to hold decimal128 digits. */
32 #include "decimal128.h"
33 #include "decimal128Local.h"
34 #include "decimal64.h"
35 #include "decimal32.h"
36 #include "decNumber.h"
38 #ifndef WORDS_BIGENDIAN
39 #define WORDS_BIGENDIAN 0
40 #endif
42 /* Initialize R (a real with the decimal flag set) from DN. Can
43 utilize status passed in via CONTEXT, if a previous operation had
44 interesting status. */
46 static void
47 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
49 memset (r, 0, sizeof (REAL_VALUE_TYPE));
51 r->cl = rvc_normal;
52 if (decNumberIsNaN (dn))
53 r->cl = rvc_nan;
54 if (decNumberIsInfinite (dn))
55 r->cl = rvc_inf;
56 if (context->status & DEC_Overflow)
57 r->cl = rvc_inf;
58 if (decNumberIsNegative (dn))
59 r->sign = 1;
60 r->decimal = 1;
62 if (r->cl != rvc_normal)
63 return;
65 decContextDefault (context, DEC_INIT_DECIMAL128);
66 context->traps = 0;
68 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
71 /* Create decimal encoded R from string S. */
73 void
74 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
76 decNumber dn;
77 decContext set;
78 decContextDefault (&set, DEC_INIT_DECIMAL128);
79 set.traps = 0;
81 decNumberFromString (&dn, s, &set);
83 /* It would be more efficient to store directly in decNumber format,
84 but that is impractical from current data structure size.
85 Encoding as a decimal128 is much more compact. */
86 decimal_from_decnumber (r, &dn, &set);
89 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
91 static void
92 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
94 decContext set;
95 decContextDefault (&set, DEC_INIT_DECIMAL128);
96 set.traps = 0;
98 switch (r->cl)
100 case rvc_zero:
101 decNumberZero (dn);
102 break;
103 case rvc_inf:
104 decNumberFromString (dn, "Infinity", &set);
105 break;
106 case rvc_nan:
107 if (r->signalling)
108 decNumberFromString (dn, "snan", &set);
109 else
110 decNumberFromString (dn, "nan", &set);
111 break;
112 case rvc_normal:
113 gcc_assert (r->decimal);
114 decimal128ToNumber ((const decimal128 *) r->sig, dn);
115 break;
116 default:
117 gcc_unreachable ();
120 /* Fix up sign bit. */
121 if (r->sign != decNumberIsNegative (dn))
122 dn->bits ^= DECNEG;
125 /* Encode a real into an IEEE 754 decimal32 type. */
127 void
128 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
129 long *buf, const REAL_VALUE_TYPE *r)
131 decNumber dn;
132 decimal32 d32;
133 decContext set;
134 int32_t image;
136 decContextDefault (&set, DEC_INIT_DECIMAL128);
137 set.traps = 0;
139 decimal_to_decnumber (r, &dn);
140 decimal32FromNumber (&d32, &dn, &set);
142 memcpy (&image, d32.bytes, sizeof (int32_t));
143 buf[0] = image;
146 /* Decode an IEEE 754 decimal32 type into a real. */
148 void
149 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
150 REAL_VALUE_TYPE *r, const long *buf)
152 decNumber dn;
153 decimal32 d32;
154 decContext set;
155 int32_t image;
157 decContextDefault (&set, DEC_INIT_DECIMAL128);
158 set.traps = 0;
160 image = buf[0];
161 memcpy (&d32.bytes, &image, sizeof (int32_t));
163 decimal32ToNumber (&d32, &dn);
164 decimal_from_decnumber (r, &dn, &set);
167 /* Encode a real into an IEEE 754 decimal64 type. */
169 void
170 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
171 long *buf, const REAL_VALUE_TYPE *r)
173 decNumber dn;
174 decimal64 d64;
175 decContext set;
176 int32_t image;
178 decContextDefault (&set, DEC_INIT_DECIMAL128);
179 set.traps = 0;
181 decimal_to_decnumber (r, &dn);
182 decimal64FromNumber (&d64, &dn, &set);
184 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
186 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
187 buf[0] = image;
188 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
189 buf[1] = image;
191 else
193 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
194 buf[0] = image;
195 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
196 buf[1] = image;
200 /* Decode an IEEE 754 decimal64 type into a real. */
202 void
203 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
204 REAL_VALUE_TYPE *r, const long *buf)
206 decNumber dn;
207 decimal64 d64;
208 decContext set;
209 int32_t image;
211 decContextDefault (&set, DEC_INIT_DECIMAL128);
212 set.traps = 0;
214 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
216 image = buf[0];
217 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
218 image = buf[1];
219 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
221 else
223 image = buf[1];
224 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
225 image = buf[0];
226 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
229 decimal64ToNumber (&d64, &dn);
230 decimal_from_decnumber (r, &dn, &set);
233 /* Encode a real into an IEEE 754 decimal128 type. */
235 void
236 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
237 long *buf, const REAL_VALUE_TYPE *r)
239 decNumber dn;
240 decContext set;
241 decimal128 d128;
242 int32_t image;
244 decContextDefault (&set, DEC_INIT_DECIMAL128);
245 set.traps = 0;
247 decimal_to_decnumber (r, &dn);
248 decimal128FromNumber (&d128, &dn, &set);
250 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
252 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
253 buf[0] = image;
254 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
255 buf[1] = image;
256 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
257 buf[2] = image;
258 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
259 buf[3] = image;
261 else
263 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
264 buf[0] = image;
265 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
266 buf[1] = image;
267 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
268 buf[2] = image;
269 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
270 buf[3] = image;
274 /* Decode an IEEE 754 decimal128 type into a real. */
276 void
277 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
278 REAL_VALUE_TYPE *r, const long *buf)
280 decNumber dn;
281 decimal128 d128;
282 decContext set;
283 int32_t image;
285 decContextDefault (&set, DEC_INIT_DECIMAL128);
286 set.traps = 0;
288 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
290 image = buf[0];
291 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
292 image = buf[1];
293 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
294 image = buf[2];
295 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
296 image = buf[3];
297 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
299 else
301 image = buf[3];
302 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
303 image = buf[2];
304 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
305 image = buf[1];
306 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
307 image = buf[0];
308 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
311 decimal128ToNumber (&d128, &dn);
312 decimal_from_decnumber (r, &dn, &set);
315 /* Helper function to convert from a binary real internal
316 representation. */
318 static void
319 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
320 enum machine_mode mode)
322 char string[256];
323 const decimal128 *const d128 = (const decimal128 *) from->sig;
325 decimal128ToString (d128, string);
326 real_from_string3 (to, string, mode);
330 /* Helper function to convert from a binary real internal
331 representation. */
333 static void
334 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
336 char string[256];
338 /* We convert to string, then to decNumber then to decimal128. */
339 real_to_decimal (string, from, sizeof (string), 0, 1);
340 decimal_real_from_string (to, string);
343 /* Helper function to real.c:do_compare() to handle decimal internal
344 representation including when one of the operands is still in the
345 binary internal representation. */
348 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
349 int nan_result)
351 decContext set;
352 decNumber dn, dn2, dn3;
353 REAL_VALUE_TYPE a1, b1;
355 /* If either operand is non-decimal, create temporary versions. */
356 if (!a->decimal)
358 decimal_from_binary (&a1, a);
359 a = &a1;
361 if (!b->decimal)
363 decimal_from_binary (&b1, b);
364 b = &b1;
367 /* Convert into decNumber form for comparison operation. */
368 decContextDefault (&set, DEC_INIT_DECIMAL128);
369 set.traps = 0;
370 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
371 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
373 /* Finally, do the comparison. */
374 decNumberCompare (&dn, &dn2, &dn3, &set);
376 /* Return the comparison result. */
377 if (decNumberIsNaN (&dn))
378 return nan_result;
379 else if (decNumberIsZero (&dn))
380 return 0;
381 else if (decNumberIsNegative (&dn))
382 return -1;
383 else
384 return 1;
387 /* Helper to round_for_format, handling decimal float types. */
389 void
390 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
392 decNumber dn;
393 decContext set;
395 /* Real encoding occurs later. */
396 if (r->cl != rvc_normal)
397 return;
399 decContextDefault (&set, DEC_INIT_DECIMAL128);
400 set.traps = 0;
401 decimal128ToNumber ((decimal128 *) r->sig, &dn);
403 if (fmt == &decimal_quad_format)
405 /* The internal format is already in this format. */
406 return;
408 else if (fmt == &decimal_single_format)
410 decimal32 d32;
411 decContextDefault (&set, DEC_INIT_DECIMAL32);
412 set.traps = 0;
414 decimal32FromNumber (&d32, &dn, &set);
415 decimal32ToNumber (&d32, &dn);
417 else if (fmt == &decimal_double_format)
419 decimal64 d64;
420 decContextDefault (&set, DEC_INIT_DECIMAL64);
421 set.traps = 0;
423 decimal64FromNumber (&d64, &dn, &set);
424 decimal64ToNumber (&d64, &dn);
426 else
427 gcc_unreachable ();
429 decimal_from_decnumber (r, &dn, &set);
432 /* Extend or truncate to a new mode. Handles conversions between
433 binary and decimal types. */
435 void
436 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
437 const REAL_VALUE_TYPE *a)
439 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
441 if (a->decimal && fmt->b == 10)
442 return;
443 if (a->decimal)
444 decimal_to_binary (r, a, mode);
445 else
446 decimal_from_binary (r, a);
449 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
450 significant digits in the result, bounded by BUF_SIZE. If DIGITS
451 is 0, choose the maximum for the representation. If
452 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
453 DIGITS or CROP_TRAILING_ZEROS. */
455 void
456 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
457 size_t buf_size,
458 size_t digits ATTRIBUTE_UNUSED,
459 int crop_trailing_zeros ATTRIBUTE_UNUSED)
461 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
463 /* decimal128ToString requires space for at least 24 characters;
464 Require two more for suffix. */
465 gcc_assert (buf_size >= 24);
466 decimal128ToString (d128, str);
469 static bool
470 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
471 const REAL_VALUE_TYPE *op1, int subtract_p)
473 decNumber dn;
474 decContext set;
475 decNumber dn2, dn3;
477 decimal_to_decnumber (op0, &dn2);
478 decimal_to_decnumber (op1, &dn3);
480 decContextDefault (&set, DEC_INIT_DECIMAL128);
481 set.traps = 0;
483 if (subtract_p)
484 decNumberSubtract (&dn, &dn2, &dn3, &set);
485 else
486 decNumberAdd (&dn, &dn2, &dn3, &set);
488 decimal_from_decnumber (r, &dn, &set);
490 /* Return true, if inexact. */
491 return (set.status & DEC_Inexact);
494 /* Compute R = OP0 * OP1. */
496 static bool
497 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
498 const REAL_VALUE_TYPE *op1)
500 decContext set;
501 decNumber dn, dn2, dn3;
503 decimal_to_decnumber (op0, &dn2);
504 decimal_to_decnumber (op1, &dn3);
506 decContextDefault (&set, DEC_INIT_DECIMAL128);
507 set.traps = 0;
509 decNumberMultiply (&dn, &dn2, &dn3, &set);
510 decimal_from_decnumber (r, &dn, &set);
512 /* Return true, if inexact. */
513 return (set.status & DEC_Inexact);
516 /* Compute R = OP0 / OP1. */
518 static bool
519 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
520 const REAL_VALUE_TYPE *op1)
522 decContext set;
523 decNumber dn, dn2, dn3;
525 decimal_to_decnumber (op0, &dn2);
526 decimal_to_decnumber (op1, &dn3);
528 decContextDefault (&set, DEC_INIT_DECIMAL128);
529 set.traps = 0;
531 decNumberDivide (&dn, &dn2, &dn3, &set);
532 decimal_from_decnumber (r, &dn, &set);
534 /* Return true, if inexact. */
535 return (set.status & DEC_Inexact);
538 /* Set R to A truncated to an integral value toward zero (decimal
539 floating point). */
541 void
542 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
544 decNumber dn, dn2;
545 decContext set;
547 decContextDefault (&set, DEC_INIT_DECIMAL128);
548 set.traps = 0;
549 set.round = DEC_ROUND_DOWN;
550 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
552 decNumberToIntegralValue (&dn, &dn2, &set);
553 decimal_from_decnumber (r, &dn, &set);
556 /* Render decimal float value R as an integer. */
558 HOST_WIDE_INT
559 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
561 decContext set;
562 decNumber dn, dn2, dn3;
563 REAL_VALUE_TYPE to;
564 char string[256];
566 decContextDefault (&set, DEC_INIT_DECIMAL128);
567 set.traps = 0;
568 set.round = DEC_ROUND_DOWN;
569 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
571 decNumberToIntegralValue (&dn2, &dn, &set);
572 decNumberZero (&dn3);
573 decNumberRescale (&dn, &dn2, &dn3, &set);
575 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
576 function. */
577 decNumberToString (&dn, string);
578 real_from_string (&to, string);
579 return real_to_integer (&to);
582 /* Likewise, but to an integer pair, HI+LOW. */
584 void
585 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
586 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 real_to_integer2 (plow, phigh, &to);
609 /* Perform the decimal floating point operation described by CODE.
610 For a unary operation, OP1 will be NULL. This function returns
611 true if the result may be inexact due to loss of precision. */
613 bool
614 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
615 const REAL_VALUE_TYPE *op0,
616 const REAL_VALUE_TYPE *op1)
618 REAL_VALUE_TYPE a, b;
620 /* If either operand is non-decimal, create temporaries. */
621 if (!op0->decimal)
623 decimal_from_binary (&a, op0);
624 op0 = &a;
626 if (op1 && !op1->decimal)
628 decimal_from_binary (&b, op1);
629 op1 = &b;
632 switch (code)
634 case PLUS_EXPR:
635 return decimal_do_add (r, op0, op1, 0);
637 case MINUS_EXPR:
638 return decimal_do_add (r, op0, op1, 1);
640 case MULT_EXPR:
641 return decimal_do_multiply (r, op0, op1);
643 case RDIV_EXPR:
644 return decimal_do_divide (r, op0, op1);
646 case MIN_EXPR:
647 if (op1->cl == rvc_nan)
648 *r = *op1;
649 else if (real_compare (UNLT_EXPR, op0, op1))
650 *r = *op0;
651 else
652 *r = *op1;
653 return false;
655 case MAX_EXPR:
656 if (op1->cl == rvc_nan)
657 *r = *op1;
658 else if (real_compare (LT_EXPR, op0, op1))
659 *r = *op1;
660 else
661 *r = *op0;
662 return false;
664 case NEGATE_EXPR:
666 *r = *op0;
667 /* Flip sign bit. */
668 decimal128FlipSign ((decimal128 *) r->sig);
669 /* Keep sign field in sync. */
670 r->sign ^= 1;
672 return false;
674 case ABS_EXPR:
676 *r = *op0;
677 /* Clear sign bit. */
678 decimal128ClearSign ((decimal128 *) r->sig);
679 /* Keep sign field in sync. */
680 r->sign = 0;
682 return false;
684 case FIX_TRUNC_EXPR:
685 decimal_do_fix_trunc (r, op0);
686 return false;
688 default:
689 gcc_unreachable ();
693 /* Fills R with the largest finite value representable in mode MODE.
694 If SIGN is nonzero, R is set to the most negative finite value. */
696 void
697 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
699 const char *max;
701 switch (mode)
703 case SDmode:
704 max = "9.999999E96";
705 break;
706 case DDmode:
707 max = "9.999999999999999E384";
708 break;
709 case TDmode:
710 max = "9.999999999999999999999999999999999E6144";
711 break;
712 default:
713 gcc_unreachable ();
716 decimal_real_from_string (r, max);
717 if (sign)
718 decimal128SetSign ((decimal128 *) r->sig, 1);