2010-04-19 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / dfp.c
blob90ca2a2a4f2926f3ced3598af446fecd0babb6f8
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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 "toplev.h"
27 #include "real.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 gcc_assert (r->decimal);
116 decimal128ToNumber ((const decimal128 *) r->sig, dn);
117 break;
118 default:
119 gcc_unreachable ();
122 /* Fix up sign bit. */
123 if (r->sign != decNumberIsNegative (dn))
124 dn->bits ^= DECNEG;
127 /* Encode a real into an IEEE 754 decimal32 type. */
129 void
130 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
131 long *buf, const REAL_VALUE_TYPE *r)
133 decNumber dn;
134 decimal32 d32;
135 decContext set;
136 int32_t image;
138 decContextDefault (&set, DEC_INIT_DECIMAL128);
139 set.traps = 0;
141 decimal_to_decnumber (r, &dn);
142 decimal32FromNumber (&d32, &dn, &set);
144 memcpy (&image, d32.bytes, sizeof (int32_t));
145 buf[0] = image;
148 /* Decode an IEEE 754 decimal32 type into a real. */
150 void
151 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
152 REAL_VALUE_TYPE *r, const long *buf)
154 decNumber dn;
155 decimal32 d32;
156 decContext set;
157 int32_t image;
159 decContextDefault (&set, DEC_INIT_DECIMAL128);
160 set.traps = 0;
162 image = buf[0];
163 memcpy (&d32.bytes, &image, sizeof (int32_t));
165 decimal32ToNumber (&d32, &dn);
166 decimal_from_decnumber (r, &dn, &set);
169 /* Encode a real into an IEEE 754 decimal64 type. */
171 void
172 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
173 long *buf, const REAL_VALUE_TYPE *r)
175 decNumber dn;
176 decimal64 d64;
177 decContext set;
178 int32_t image;
180 decContextDefault (&set, DEC_INIT_DECIMAL128);
181 set.traps = 0;
183 decimal_to_decnumber (r, &dn);
184 decimal64FromNumber (&d64, &dn, &set);
186 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
188 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
189 buf[0] = image;
190 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
191 buf[1] = image;
193 else
195 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
196 buf[0] = image;
197 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
198 buf[1] = image;
202 /* Decode an IEEE 754 decimal64 type into a real. */
204 void
205 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
206 REAL_VALUE_TYPE *r, const long *buf)
208 decNumber dn;
209 decimal64 d64;
210 decContext set;
211 int32_t image;
213 decContextDefault (&set, DEC_INIT_DECIMAL128);
214 set.traps = 0;
216 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
218 image = buf[0];
219 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
220 image = buf[1];
221 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
223 else
225 image = buf[1];
226 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
227 image = buf[0];
228 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
231 decimal64ToNumber (&d64, &dn);
232 decimal_from_decnumber (r, &dn, &set);
235 /* Encode a real into an IEEE 754 decimal128 type. */
237 void
238 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
239 long *buf, const REAL_VALUE_TYPE *r)
241 decNumber dn;
242 decContext set;
243 decimal128 d128;
244 int32_t image;
246 decContextDefault (&set, DEC_INIT_DECIMAL128);
247 set.traps = 0;
249 decimal_to_decnumber (r, &dn);
250 decimal128FromNumber (&d128, &dn, &set);
252 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
254 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
255 buf[0] = image;
256 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
257 buf[1] = image;
258 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
259 buf[2] = image;
260 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
261 buf[3] = image;
263 else
265 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
266 buf[0] = image;
267 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
268 buf[1] = image;
269 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
270 buf[2] = image;
271 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
272 buf[3] = image;
276 /* Decode an IEEE 754 decimal128 type into a real. */
278 void
279 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
280 REAL_VALUE_TYPE *r, const long *buf)
282 decNumber dn;
283 decimal128 d128;
284 decContext set;
285 int32_t image;
287 decContextDefault (&set, DEC_INIT_DECIMAL128);
288 set.traps = 0;
290 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
292 image = buf[0];
293 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
294 image = buf[1];
295 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
296 image = buf[2];
297 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
298 image = buf[3];
299 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
301 else
303 image = buf[3];
304 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
305 image = buf[2];
306 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
307 image = buf[1];
308 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
309 image = buf[0];
310 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
313 decimal128ToNumber (&d128, &dn);
314 decimal_from_decnumber (r, &dn, &set);
317 /* Helper function to convert from a binary real internal
318 representation. */
320 static void
321 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
322 enum machine_mode mode)
324 char string[256];
325 const decimal128 *const d128 = (const decimal128 *) from->sig;
327 decimal128ToString (d128, string);
328 real_from_string3 (to, string, mode);
332 /* Helper function to convert from a binary real internal
333 representation. */
335 static void
336 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
338 char string[256];
340 /* We convert to string, then to decNumber then to decimal128. */
341 real_to_decimal (string, from, sizeof (string), 0, 1);
342 decimal_real_from_string (to, string);
345 /* Helper function to real.c:do_compare() to handle decimal internal
346 representation including when one of the operands is still in the
347 binary internal representation. */
350 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
351 int nan_result)
353 decContext set;
354 decNumber dn, dn2, dn3;
355 REAL_VALUE_TYPE a1, b1;
357 /* If either operand is non-decimal, create temporary versions. */
358 if (!a->decimal)
360 decimal_from_binary (&a1, a);
361 a = &a1;
363 if (!b->decimal)
365 decimal_from_binary (&b1, b);
366 b = &b1;
369 /* Convert into decNumber form for comparison operation. */
370 decContextDefault (&set, DEC_INIT_DECIMAL128);
371 set.traps = 0;
372 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
373 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
375 /* Finally, do the comparison. */
376 decNumberCompare (&dn, &dn2, &dn3, &set);
378 /* Return the comparison result. */
379 if (decNumberIsNaN (&dn))
380 return nan_result;
381 else if (decNumberIsZero (&dn))
382 return 0;
383 else if (decNumberIsNegative (&dn))
384 return -1;
385 else
386 return 1;
389 /* Helper to round_for_format, handling decimal float types. */
391 void
392 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
394 decNumber dn;
395 decContext set;
397 /* Real encoding occurs later. */
398 if (r->cl != rvc_normal)
399 return;
401 decContextDefault (&set, DEC_INIT_DECIMAL128);
402 set.traps = 0;
403 decimal128ToNumber ((decimal128 *) r->sig, &dn);
405 if (fmt == &decimal_quad_format)
407 /* The internal format is already in this format. */
408 return;
410 else if (fmt == &decimal_single_format)
412 decimal32 d32;
413 decContextDefault (&set, DEC_INIT_DECIMAL32);
414 set.traps = 0;
416 decimal32FromNumber (&d32, &dn, &set);
417 decimal32ToNumber (&d32, &dn);
419 else if (fmt == &decimal_double_format)
421 decimal64 d64;
422 decContextDefault (&set, DEC_INIT_DECIMAL64);
423 set.traps = 0;
425 decimal64FromNumber (&d64, &dn, &set);
426 decimal64ToNumber (&d64, &dn);
428 else
429 gcc_unreachable ();
431 decimal_from_decnumber (r, &dn, &set);
434 /* Extend or truncate to a new mode. Handles conversions between
435 binary and decimal types. */
437 void
438 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
439 const REAL_VALUE_TYPE *a)
441 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
443 if (a->decimal && fmt->b == 10)
444 return;
445 if (a->decimal)
446 decimal_to_binary (r, a, mode);
447 else
448 decimal_from_binary (r, a);
451 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
452 significant digits in the result, bounded by BUF_SIZE. If DIGITS
453 is 0, choose the maximum for the representation. If
454 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
455 DIGITS or CROP_TRAILING_ZEROS. */
457 void
458 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
459 size_t buf_size,
460 size_t digits ATTRIBUTE_UNUSED,
461 int crop_trailing_zeros ATTRIBUTE_UNUSED)
463 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
465 /* decimal128ToString requires space for at least 24 characters;
466 Require two more for suffix. */
467 gcc_assert (buf_size >= 24);
468 decimal128ToString (d128, str);
471 static bool
472 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
473 const REAL_VALUE_TYPE *op1, int subtract_p)
475 decNumber dn;
476 decContext set;
477 decNumber dn2, dn3;
479 decimal_to_decnumber (op0, &dn2);
480 decimal_to_decnumber (op1, &dn3);
482 decContextDefault (&set, DEC_INIT_DECIMAL128);
483 set.traps = 0;
485 if (subtract_p)
486 decNumberSubtract (&dn, &dn2, &dn3, &set);
487 else
488 decNumberAdd (&dn, &dn2, &dn3, &set);
490 decimal_from_decnumber (r, &dn, &set);
492 /* Return true, if inexact. */
493 return (set.status & DEC_Inexact);
496 /* Compute R = OP0 * OP1. */
498 static bool
499 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
500 const REAL_VALUE_TYPE *op1)
502 decContext set;
503 decNumber dn, 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 decNumberMultiply (&dn, &dn2, &dn3, &set);
512 decimal_from_decnumber (r, &dn, &set);
514 /* Return true, if inexact. */
515 return (set.status & DEC_Inexact);
518 /* Compute R = OP0 / OP1. */
520 static bool
521 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
522 const REAL_VALUE_TYPE *op1)
524 decContext set;
525 decNumber dn, dn2, dn3;
527 decimal_to_decnumber (op0, &dn2);
528 decimal_to_decnumber (op1, &dn3);
530 decContextDefault (&set, DEC_INIT_DECIMAL128);
531 set.traps = 0;
533 decNumberDivide (&dn, &dn2, &dn3, &set);
534 decimal_from_decnumber (r, &dn, &set);
536 /* Return true, if inexact. */
537 return (set.status & DEC_Inexact);
540 /* Set R to A truncated to an integral value toward zero (decimal
541 floating point). */
543 void
544 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
546 decNumber dn, dn2;
547 decContext set;
549 decContextDefault (&set, DEC_INIT_DECIMAL128);
550 set.traps = 0;
551 set.round = DEC_ROUND_DOWN;
552 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
554 decNumberToIntegralValue (&dn, &dn2, &set);
555 decimal_from_decnumber (r, &dn, &set);
558 /* Render decimal float value R as an integer. */
560 HOST_WIDE_INT
561 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
563 decContext set;
564 decNumber dn, dn2, dn3;
565 REAL_VALUE_TYPE to;
566 char string[256];
568 decContextDefault (&set, DEC_INIT_DECIMAL128);
569 set.traps = 0;
570 set.round = DEC_ROUND_DOWN;
571 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
573 decNumberToIntegralValue (&dn2, &dn, &set);
574 decNumberZero (&dn3);
575 decNumberRescale (&dn, &dn2, &dn3, &set);
577 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
578 function. */
579 decNumberToString (&dn, string);
580 real_from_string (&to, string);
581 return real_to_integer (&to);
584 /* Likewise, but to an integer pair, HI+LOW. */
586 void
587 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
588 const REAL_VALUE_TYPE *r)
590 decContext set;
591 decNumber dn, dn2, dn3;
592 REAL_VALUE_TYPE to;
593 char string[256];
595 decContextDefault (&set, DEC_INIT_DECIMAL128);
596 set.traps = 0;
597 set.round = DEC_ROUND_DOWN;
598 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
600 decNumberToIntegralValue (&dn2, &dn, &set);
601 decNumberZero (&dn3);
602 decNumberRescale (&dn, &dn2, &dn3, &set);
604 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
605 function. */
606 decNumberToString (&dn, string);
607 real_from_string (&to, string);
608 real_to_integer2 (plow, phigh, &to);
611 /* Perform the decimal floating point operation described by CODE.
612 For a unary operation, OP1 will be NULL. This function returns
613 true if the result may be inexact due to loss of precision. */
615 bool
616 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
617 const REAL_VALUE_TYPE *op0,
618 const REAL_VALUE_TYPE *op1)
620 REAL_VALUE_TYPE a, b;
622 /* If either operand is non-decimal, create temporaries. */
623 if (!op0->decimal)
625 decimal_from_binary (&a, op0);
626 op0 = &a;
628 if (op1 && !op1->decimal)
630 decimal_from_binary (&b, op1);
631 op1 = &b;
634 switch (code)
636 case PLUS_EXPR:
637 return decimal_do_add (r, op0, op1, 0);
639 case MINUS_EXPR:
640 return decimal_do_add (r, op0, op1, 1);
642 case MULT_EXPR:
643 return decimal_do_multiply (r, op0, op1);
645 case RDIV_EXPR:
646 return decimal_do_divide (r, op0, op1);
648 case MIN_EXPR:
649 if (op1->cl == rvc_nan)
650 *r = *op1;
651 else if (real_compare (UNLT_EXPR, op0, op1))
652 *r = *op0;
653 else
654 *r = *op1;
655 return false;
657 case MAX_EXPR:
658 if (op1->cl == rvc_nan)
659 *r = *op1;
660 else if (real_compare (LT_EXPR, op0, op1))
661 *r = *op1;
662 else
663 *r = *op0;
664 return false;
666 case NEGATE_EXPR:
668 *r = *op0;
669 /* Flip sign bit. */
670 decimal128FlipSign ((decimal128 *) r->sig);
671 /* Keep sign field in sync. */
672 r->sign ^= 1;
674 return false;
676 case ABS_EXPR:
678 *r = *op0;
679 /* Clear sign bit. */
680 decimal128ClearSign ((decimal128 *) r->sig);
681 /* Keep sign field in sync. */
682 r->sign = 0;
684 return false;
686 case FIX_TRUNC_EXPR:
687 decimal_do_fix_trunc (r, op0);
688 return false;
690 default:
691 gcc_unreachable ();
695 /* Fills R with the largest finite value representable in mode MODE.
696 If SIGN is nonzero, R is set to the most negative finite value. */
698 void
699 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
701 const char *max;
703 switch (mode)
705 case SDmode:
706 max = "9.999999E96";
707 break;
708 case DDmode:
709 max = "9.999999999999999E384";
710 break;
711 case TDmode:
712 max = "9.999999999999999999999999999999999E6144";
713 break;
714 default:
715 gcc_unreachable ();
718 decimal_real_from_string (r, max);
719 if (sign)
720 decimal128SetSign ((decimal128 *) r->sig, 1);