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
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
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/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
39 /* The order of the following headers is important for making sure
40 decNumber structure is large enough to hold decimal128 digits. */
42 #include "decimal128.h"
43 #include "decimal128Local.h"
44 #include "decimal64.h"
45 #include "decimal32.h"
46 #include "decNumber.h"
48 #ifndef WORDS_BIGENDIAN
49 #define WORDS_BIGENDIAN 0
52 /* Initialize R (a real with the decimal flag set) from DN. Can
53 utilize status passed in via CONTEXT, if a previous operation had
54 interesting status. */
57 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
59 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
62 if (decNumberIsNaN (dn
))
64 if (decNumberIsInfinite (dn
))
66 if (context
->status
& DEC_Overflow
)
68 if (decNumberIsNegative (dn
))
72 if (r
->cl
!= rvc_normal
)
75 decContextDefault (context
, DEC_INIT_DECIMAL128
);
78 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
81 /* Create decimal encoded R from string S. */
84 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
88 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
91 decNumberFromString (&dn
, s
, &set
);
93 /* It would be more efficient to store directly in decNumber format,
94 but that is impractical from current data structure size.
95 Encoding as a decimal128 is much more compact. */
96 decimal_from_decnumber (r
, &dn
, &set
);
99 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
102 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
105 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
114 decNumberFromString (dn
, "Infinity", &set
);
118 decNumberFromString (dn
, "snan", &set
);
120 decNumberFromString (dn
, "nan", &set
);
125 /* dconst{1,2,m1,half} are used in various places in
126 the middle-end and optimizers, allow them here
127 as an exception by converting them to decimal. */
128 if (memcmp (r
, &dconst1
, sizeof (*r
)) == 0)
130 decNumberFromString (dn
, "1", &set
);
133 if (memcmp (r
, &dconst2
, sizeof (*r
)) == 0)
135 decNumberFromString (dn
, "2", &set
);
138 if (memcmp (r
, &dconstm1
, sizeof (*r
)) == 0)
140 decNumberFromString (dn
, "-1", &set
);
143 if (memcmp (r
, &dconsthalf
, sizeof (*r
)) == 0)
145 decNumberFromString (dn
, "0.5", &set
);
150 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
156 /* Fix up sign bit. */
157 if (r
->sign
!= decNumberIsNegative (dn
))
161 /* Encode a real into an IEEE 754 decimal32 type. */
164 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
165 long *buf
, const REAL_VALUE_TYPE
*r
)
172 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
175 decimal_to_decnumber (r
, &dn
);
176 decimal32FromNumber (&d32
, &dn
, &set
);
178 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
182 /* Decode an IEEE 754 decimal32 type into a real. */
185 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
186 REAL_VALUE_TYPE
*r
, const long *buf
)
193 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
197 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
199 decimal32ToNumber (&d32
, &dn
);
200 decimal_from_decnumber (r
, &dn
, &set
);
203 /* Encode a real into an IEEE 754 decimal64 type. */
206 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
207 long *buf
, const REAL_VALUE_TYPE
*r
)
214 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
217 decimal_to_decnumber (r
, &dn
);
218 decimal64FromNumber (&d64
, &dn
, &set
);
220 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
222 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
224 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
229 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
231 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
236 /* Decode an IEEE 754 decimal64 type into a real. */
239 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
240 REAL_VALUE_TYPE
*r
, const long *buf
)
247 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
250 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
253 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
255 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
260 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
262 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
265 decimal64ToNumber (&d64
, &dn
);
266 decimal_from_decnumber (r
, &dn
, &set
);
269 /* Encode a real into an IEEE 754 decimal128 type. */
272 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
273 long *buf
, const REAL_VALUE_TYPE
*r
)
280 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
283 decimal_to_decnumber (r
, &dn
);
284 decimal128FromNumber (&d128
, &dn
, &set
);
286 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
288 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
290 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
292 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
294 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
299 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
301 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
303 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
305 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
310 /* Decode an IEEE 754 decimal128 type into a real. */
313 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
314 REAL_VALUE_TYPE
*r
, const long *buf
)
321 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
324 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
327 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
329 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
331 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
333 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
338 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
340 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
342 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
344 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
347 decimal128ToNumber (&d128
, &dn
);
348 decimal_from_decnumber (r
, &dn
, &set
);
351 /* Helper function to convert from a binary real internal
355 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
359 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
361 decimal128ToString (d128
, string
);
362 real_from_string3 (to
, string
, mode
);
366 /* Helper function to convert from a binary real internal
370 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
374 /* We convert to string, then to decNumber then to decimal128. */
375 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
376 decimal_real_from_string (to
, string
);
379 /* Helper function to real.c:do_compare() to handle decimal internal
380 representation including when one of the operands is still in the
381 binary internal representation. */
384 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
388 decNumber dn
, dn2
, dn3
;
389 REAL_VALUE_TYPE a1
, b1
;
391 /* If either operand is non-decimal, create temporary versions. */
394 decimal_from_binary (&a1
, a
);
399 decimal_from_binary (&b1
, b
);
403 /* Convert into decNumber form for comparison operation. */
404 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
406 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
407 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
409 /* Finally, do the comparison. */
410 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
412 /* Return the comparison result. */
413 if (decNumberIsNaN (&dn
))
415 else if (decNumberIsZero (&dn
))
417 else if (decNumberIsNegative (&dn
))
423 /* Helper to round_for_format, handling decimal float types. */
426 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
431 /* Real encoding occurs later. */
432 if (r
->cl
!= rvc_normal
)
435 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
437 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
439 if (fmt
== &decimal_quad_format
)
441 /* The internal format is already in this format. */
444 else if (fmt
== &decimal_single_format
)
447 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
450 decimal32FromNumber (&d32
, &dn
, &set
);
451 decimal32ToNumber (&d32
, &dn
);
453 else if (fmt
== &decimal_double_format
)
456 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
459 decimal64FromNumber (&d64
, &dn
, &set
);
460 decimal64ToNumber (&d64
, &dn
);
465 decimal_from_decnumber (r
, &dn
, &set
);
468 /* Extend or truncate to a new mode. Handles conversions between
469 binary and decimal types. */
472 decimal_real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
473 const REAL_VALUE_TYPE
*a
)
475 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
477 if (a
->decimal
&& fmt
->b
== 10)
480 decimal_to_binary (r
, a
, mode
);
482 decimal_from_binary (r
, a
);
485 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
486 significant digits in the result, bounded by BUF_SIZE. If DIGITS
487 is 0, choose the maximum for the representation. If
488 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
489 DIGITS or CROP_TRAILING_ZEROS. */
492 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
494 size_t digits ATTRIBUTE_UNUSED
,
495 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
497 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
499 /* decimal128ToString requires space for at least 24 characters;
500 Require two more for suffix. */
501 gcc_assert (buf_size
>= 24);
502 decimal128ToString (d128
, str
);
506 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
507 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
513 decimal_to_decnumber (op0
, &dn2
);
514 decimal_to_decnumber (op1
, &dn3
);
516 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
520 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
522 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
524 decimal_from_decnumber (r
, &dn
, &set
);
526 /* Return true, if inexact. */
527 return (set
.status
& DEC_Inexact
);
530 /* Compute R = OP0 * OP1. */
533 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
534 const REAL_VALUE_TYPE
*op1
)
537 decNumber dn
, dn2
, dn3
;
539 decimal_to_decnumber (op0
, &dn2
);
540 decimal_to_decnumber (op1
, &dn3
);
542 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
545 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
546 decimal_from_decnumber (r
, &dn
, &set
);
548 /* Return true, if inexact. */
549 return (set
.status
& DEC_Inexact
);
552 /* Compute R = OP0 / OP1. */
555 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
556 const REAL_VALUE_TYPE
*op1
)
559 decNumber dn
, dn2
, dn3
;
561 decimal_to_decnumber (op0
, &dn2
);
562 decimal_to_decnumber (op1
, &dn3
);
564 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
567 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
568 decimal_from_decnumber (r
, &dn
, &set
);
570 /* Return true, if inexact. */
571 return (set
.status
& DEC_Inexact
);
574 /* Set R to A truncated to an integral value toward zero (decimal
578 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
583 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
585 set
.round
= DEC_ROUND_DOWN
;
586 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
588 decNumberToIntegralValue (&dn
, &dn2
, &set
);
589 decimal_from_decnumber (r
, &dn
, &set
);
592 /* Render decimal float value R as an integer. */
595 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
598 decNumber dn
, dn2
, dn3
;
602 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
604 set
.round
= DEC_ROUND_DOWN
;
605 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
607 decNumberToIntegralValue (&dn2
, &dn
, &set
);
608 decNumberZero (&dn3
);
609 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
611 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
613 decNumberToString (&dn
, string
);
614 real_from_string (&to
, string
);
615 return real_to_integer (&to
);
618 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
619 value does not fit. */
622 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
625 decNumber dn
, dn2
, dn3
;
629 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
631 set
.round
= DEC_ROUND_DOWN
;
632 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
634 decNumberToIntegralValue (&dn2
, &dn
, &set
);
635 decNumberZero (&dn3
);
636 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
638 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
640 decNumberToString (&dn
, string
);
641 real_from_string (&to
, string
);
642 return real_to_integer (&to
, fail
, precision
);
645 /* Perform the decimal floating point operation described by CODE.
646 For a unary operation, OP1 will be NULL. This function returns
647 true if the result may be inexact due to loss of precision. */
650 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
651 const REAL_VALUE_TYPE
*op0
,
652 const REAL_VALUE_TYPE
*op1
)
654 REAL_VALUE_TYPE a
, b
;
656 /* If either operand is non-decimal, create temporaries. */
659 decimal_from_binary (&a
, op0
);
662 if (op1
&& !op1
->decimal
)
664 decimal_from_binary (&b
, op1
);
671 return decimal_do_add (r
, op0
, op1
, 0);
674 return decimal_do_add (r
, op0
, op1
, 1);
677 return decimal_do_multiply (r
, op0
, op1
);
680 return decimal_do_divide (r
, op0
, op1
);
683 if (op1
->cl
== rvc_nan
)
685 else if (real_compare (UNLT_EXPR
, op0
, op1
))
692 if (op1
->cl
== rvc_nan
)
694 else if (real_compare (LT_EXPR
, op0
, op1
))
704 decimal128FlipSign ((decimal128
*) r
->sig
);
705 /* Keep sign field in sync. */
713 /* Clear sign bit. */
714 decimal128ClearSign ((decimal128
*) r
->sig
);
715 /* Keep sign field in sync. */
721 decimal_do_fix_trunc (r
, op0
);
729 /* Fills R with the largest finite value representable in mode MODE.
730 If SIGN is nonzero, R is set to the most negative finite value. */
733 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
743 max
= "9.999999999999999E384";
746 max
= "9.999999999999999999999999999999999E6144";
752 decimal_real_from_string (r
, max
);
754 decimal128SetSign ((decimal128
*) r
->sig
, 1);