1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software 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
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
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/>. */
23 #include "coretypes.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
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. */
47 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
49 memset (r, 0, sizeof (REAL_VALUE_TYPE));
52 if (decNumberIsNaN (dn))
54 if (decNumberIsInfinite (dn))
56 if (context->status & DEC_Overflow)
58 if (decNumberIsNegative (dn))
62 if (r->cl != rvc_normal)
65 decContextDefault (context, DEC_INIT_DECIMAL128);
68 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
71 /* Create decimal encoded R from string S. */
74 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
78 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
92 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
95 decContextDefault (&set, DEC_INIT_DECIMAL128);
104 decNumberFromString (dn, "Infinity", &set);
108 decNumberFromString (dn, "snan", &set);
110 decNumberFromString (dn, "nan", &set);
115 /* dconst{1,2,m1,half} are used in various places in
116 the middle-end and optimizers, allow them here
117 as an exception by converting them to decimal. */
118 if (memcmp (r, &dconst1, sizeof (*r)) == 0)
120 decNumberFromString (dn, "1", &set);
123 if (memcmp (r, &dconst2, sizeof (*r)) == 0)
125 decNumberFromString (dn, "2", &set);
128 if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
130 decNumberFromString (dn, "-1", &set);
133 if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
135 decNumberFromString (dn, "0.5", &set);
140 decimal128ToNumber ((const decimal128 *) r->sig, dn);
146 /* Fix up sign bit. */
147 if (r->sign != decNumberIsNegative (dn))
151 /* Encode a real into an IEEE 754 decimal32 type. */
154 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
155 long *buf, const REAL_VALUE_TYPE *r)
162 decContextDefault (&set, DEC_INIT_DECIMAL128);
165 decimal_to_decnumber (r, &dn);
166 decimal32FromNumber (&d32, &dn, &set);
168 memcpy (&image, d32.bytes, sizeof (int32_t));
172 /* Decode an IEEE 754 decimal32 type into a real. */
175 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
176 REAL_VALUE_TYPE *r, const long *buf)
183 decContextDefault (&set, DEC_INIT_DECIMAL128);
187 memcpy (&d32.bytes, &image, sizeof (int32_t));
189 decimal32ToNumber (&d32, &dn);
190 decimal_from_decnumber (r, &dn, &set);
193 /* Encode a real into an IEEE 754 decimal64 type. */
196 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
197 long *buf, const REAL_VALUE_TYPE *r)
204 decContextDefault (&set, DEC_INIT_DECIMAL128);
207 decimal_to_decnumber (r, &dn);
208 decimal64FromNumber (&d64, &dn, &set);
210 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
212 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
214 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
219 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
221 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
226 /* Decode an IEEE 754 decimal64 type into a real. */
229 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
230 REAL_VALUE_TYPE *r, const long *buf)
237 decContextDefault (&set, DEC_INIT_DECIMAL128);
240 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
243 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
245 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
250 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
252 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
255 decimal64ToNumber (&d64, &dn);
256 decimal_from_decnumber (r, &dn, &set);
259 /* Encode a real into an IEEE 754 decimal128 type. */
262 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
263 long *buf, const REAL_VALUE_TYPE *r)
270 decContextDefault (&set, DEC_INIT_DECIMAL128);
273 decimal_to_decnumber (r, &dn);
274 decimal128FromNumber (&d128, &dn, &set);
276 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
278 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
280 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
282 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
284 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
289 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
291 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
293 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
295 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
300 /* Decode an IEEE 754 decimal128 type into a real. */
303 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
304 REAL_VALUE_TYPE *r, const long *buf)
311 decContextDefault (&set, DEC_INIT_DECIMAL128);
314 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
317 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
319 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
321 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
323 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
328 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
330 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
332 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
334 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
337 decimal128ToNumber (&d128, &dn);
338 decimal_from_decnumber (r, &dn, &set);
341 /* Helper function to convert from a binary real internal
345 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
346 enum machine_mode mode)
349 const decimal128 *const d128 = (const decimal128 *) from->sig;
351 decimal128ToString (d128, string);
352 real_from_string3 (to, string, mode);
356 /* Helper function to convert from a binary real internal
360 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
364 /* We convert to string, then to decNumber then to decimal128. */
365 real_to_decimal (string, from, sizeof (string), 0, 1);
366 decimal_real_from_string (to, string);
369 /* Helper function to real.c:do_compare() to handle decimal internal
370 representation including when one of the operands is still in the
371 binary internal representation. */
374 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
378 decNumber dn, dn2, dn3;
379 REAL_VALUE_TYPE a1, b1;
381 /* If either operand is non-decimal, create temporary versions. */
384 decimal_from_binary (&a1, a);
389 decimal_from_binary (&b1, b);
393 /* Convert into decNumber form for comparison operation. */
394 decContextDefault (&set, DEC_INIT_DECIMAL128);
396 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
397 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
399 /* Finally, do the comparison. */
400 decNumberCompare (&dn, &dn2, &dn3, &set);
402 /* Return the comparison result. */
403 if (decNumberIsNaN (&dn))
405 else if (decNumberIsZero (&dn))
407 else if (decNumberIsNegative (&dn))
413 /* Helper to round_for_format, handling decimal float types. */
416 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
421 /* Real encoding occurs later. */
422 if (r->cl != rvc_normal)
425 decContextDefault (&set, DEC_INIT_DECIMAL128);
427 decimal128ToNumber ((decimal128 *) r->sig, &dn);
429 if (fmt == &decimal_quad_format)
431 /* The internal format is already in this format. */
434 else if (fmt == &decimal_single_format)
437 decContextDefault (&set, DEC_INIT_DECIMAL32);
440 decimal32FromNumber (&d32, &dn, &set);
441 decimal32ToNumber (&d32, &dn);
443 else if (fmt == &decimal_double_format)
446 decContextDefault (&set, DEC_INIT_DECIMAL64);
449 decimal64FromNumber (&d64, &dn, &set);
450 decimal64ToNumber (&d64, &dn);
455 decimal_from_decnumber (r, &dn, &set);
458 /* Extend or truncate to a new mode. Handles conversions between
459 binary and decimal types. */
462 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
463 const REAL_VALUE_TYPE *a)
465 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
467 if (a->decimal && fmt->b == 10)
470 decimal_to_binary (r, a, mode);
472 decimal_from_binary (r, a);
475 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
476 significant digits in the result, bounded by BUF_SIZE. If DIGITS
477 is 0, choose the maximum for the representation. If
478 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
479 DIGITS or CROP_TRAILING_ZEROS. */
482 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
484 size_t digits ATTRIBUTE_UNUSED,
485 int crop_trailing_zeros ATTRIBUTE_UNUSED)
487 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
489 /* decimal128ToString requires space for at least 24 characters;
490 Require two more for suffix. */
491 gcc_assert (buf_size >= 24);
492 decimal128ToString (d128, str);
496 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
497 const REAL_VALUE_TYPE *op1, int subtract_p)
503 decimal_to_decnumber (op0, &dn2);
504 decimal_to_decnumber (op1, &dn3);
506 decContextDefault (&set, DEC_INIT_DECIMAL128);
510 decNumberSubtract (&dn, &dn2, &dn3, &set);
512 decNumberAdd (&dn, &dn2, &dn3, &set);
514 decimal_from_decnumber (r, &dn, &set);
516 /* Return true, if inexact. */
517 return (set.status & DEC_Inexact);
520 /* Compute R = OP0 * OP1. */
523 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
524 const REAL_VALUE_TYPE *op1)
527 decNumber dn, dn2, dn3;
529 decimal_to_decnumber (op0, &dn2);
530 decimal_to_decnumber (op1, &dn3);
532 decContextDefault (&set, DEC_INIT_DECIMAL128);
535 decNumberMultiply (&dn, &dn2, &dn3, &set);
536 decimal_from_decnumber (r, &dn, &set);
538 /* Return true, if inexact. */
539 return (set.status & DEC_Inexact);
542 /* Compute R = OP0 / OP1. */
545 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
546 const REAL_VALUE_TYPE *op1)
549 decNumber dn, dn2, dn3;
551 decimal_to_decnumber (op0, &dn2);
552 decimal_to_decnumber (op1, &dn3);
554 decContextDefault (&set, DEC_INIT_DECIMAL128);
557 decNumberDivide (&dn, &dn2, &dn3, &set);
558 decimal_from_decnumber (r, &dn, &set);
560 /* Return true, if inexact. */
561 return (set.status & DEC_Inexact);
564 /* Set R to A truncated to an integral value toward zero (decimal
568 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
573 decContextDefault (&set, DEC_INIT_DECIMAL128);
575 set.round = DEC_ROUND_DOWN;
576 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
578 decNumberToIntegralValue (&dn, &dn2, &set);
579 decimal_from_decnumber (r, &dn, &set);
582 /* Render decimal float value R as an integer. */
585 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
588 decNumber dn, dn2, dn3;
592 decContextDefault (&set, DEC_INIT_DECIMAL128);
594 set.round = DEC_ROUND_DOWN;
595 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
597 decNumberToIntegralValue (&dn2, &dn, &set);
598 decNumberZero (&dn3);
599 decNumberRescale (&dn, &dn2, &dn3, &set);
601 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
603 decNumberToString (&dn, string);
604 real_from_string (&to, string);
605 return real_to_integer (&to);
608 /* Likewise, but to an integer pair, HI+LOW. */
611 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
612 const REAL_VALUE_TYPE *r)
615 decNumber dn, dn2, dn3;
619 decContextDefault (&set, DEC_INIT_DECIMAL128);
621 set.round = DEC_ROUND_DOWN;
622 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
624 decNumberToIntegralValue (&dn2, &dn, &set);
625 decNumberZero (&dn3);
626 decNumberRescale (&dn, &dn2, &dn3, &set);
628 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
630 decNumberToString (&dn, string);
631 real_from_string (&to, string);
632 real_to_integer2 (plow, phigh, &to);
635 /* Perform the decimal floating point operation described by CODE.
636 For a unary operation, OP1 will be NULL. This function returns
637 true if the result may be inexact due to loss of precision. */
640 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
641 const REAL_VALUE_TYPE *op0,
642 const REAL_VALUE_TYPE *op1)
644 REAL_VALUE_TYPE a, b;
646 /* If either operand is non-decimal, create temporaries. */
649 decimal_from_binary (&a, op0);
652 if (op1 && !op1->decimal)
654 decimal_from_binary (&b, op1);
661 return decimal_do_add (r, op0, op1, 0);
664 return decimal_do_add (r, op0, op1, 1);
667 return decimal_do_multiply (r, op0, op1);
670 return decimal_do_divide (r, op0, op1);
673 if (op1->cl == rvc_nan)
675 else if (real_compare (UNLT_EXPR, op0, op1))
682 if (op1->cl == rvc_nan)
684 else if (real_compare (LT_EXPR, op0, op1))
694 decimal128FlipSign ((decimal128 *) r->sig);
695 /* Keep sign field in sync. */
703 /* Clear sign bit. */
704 decimal128ClearSign ((decimal128 *) r->sig);
705 /* Keep sign field in sync. */
711 decimal_do_fix_trunc (r, op0);
719 /* Fills R with the largest finite value representable in mode MODE.
720 If SIGN is nonzero, R is set to the most negative finite value. */
723 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
733 max = "9.999999999999999E384";
736 max = "9.999999999999999999999999999999999E6144";
742 decimal_real_from_string (r, max);
744 decimal128SetSign ((decimal128 *) r->sig, 1);