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"
30 /* The order of the following headers is important for making sure
31 decNumber structure is large enough to hold decimal128 digits. */
33 #include "decimal128.h"
34 #include "decimal128Local.h"
35 #include "decimal64.h"
36 #include "decimal32.h"
37 #include "decNumber.h"
39 #ifndef WORDS_BIGENDIAN
40 #define WORDS_BIGENDIAN 0
43 /* Initialize R (a real with the decimal flag set) from DN. Can
44 utilize status passed in via CONTEXT, if a previous operation had
45 interesting status. */
48 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
50 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
53 if (decNumberIsNaN (dn
))
55 if (decNumberIsInfinite (dn
))
57 if (context
->status
& DEC_Overflow
)
59 if (decNumberIsNegative (dn
))
63 if (r
->cl
!= rvc_normal
)
66 decContextDefault (context
, DEC_INIT_DECIMAL128
);
69 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
72 /* Create decimal encoded R from string S. */
75 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
79 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
82 decNumberFromString (&dn
, s
, &set
);
84 /* It would be more efficient to store directly in decNumber format,
85 but that is impractical from current data structure size.
86 Encoding as a decimal128 is much more compact. */
87 decimal_from_decnumber (r
, &dn
, &set
);
90 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
93 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
96 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
105 decNumberFromString (dn
, "Infinity", &set
);
109 decNumberFromString (dn
, "snan", &set
);
111 decNumberFromString (dn
, "nan", &set
);
116 /* dconst{1,2,m1,half} are used in various places in
117 the middle-end and optimizers, allow them here
118 as an exception by converting them to decimal. */
119 if (memcmp (r
, &dconst1
, sizeof (*r
)) == 0)
121 decNumberFromString (dn
, "1", &set
);
124 if (memcmp (r
, &dconst2
, sizeof (*r
)) == 0)
126 decNumberFromString (dn
, "2", &set
);
129 if (memcmp (r
, &dconstm1
, sizeof (*r
)) == 0)
131 decNumberFromString (dn
, "-1", &set
);
134 if (memcmp (r
, &dconsthalf
, sizeof (*r
)) == 0)
136 decNumberFromString (dn
, "0.5", &set
);
141 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
147 /* Fix up sign bit. */
148 if (r
->sign
!= decNumberIsNegative (dn
))
152 /* Encode a real into an IEEE 754 decimal32 type. */
155 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
156 long *buf
, const REAL_VALUE_TYPE
*r
)
163 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
166 decimal_to_decnumber (r
, &dn
);
167 decimal32FromNumber (&d32
, &dn
, &set
);
169 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
173 /* Decode an IEEE 754 decimal32 type into a real. */
176 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
177 REAL_VALUE_TYPE
*r
, const long *buf
)
184 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
188 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
190 decimal32ToNumber (&d32
, &dn
);
191 decimal_from_decnumber (r
, &dn
, &set
);
194 /* Encode a real into an IEEE 754 decimal64 type. */
197 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
198 long *buf
, const REAL_VALUE_TYPE
*r
)
205 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
208 decimal_to_decnumber (r
, &dn
);
209 decimal64FromNumber (&d64
, &dn
, &set
);
211 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
213 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
215 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
220 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
222 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
227 /* Decode an IEEE 754 decimal64 type into a real. */
230 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
231 REAL_VALUE_TYPE
*r
, const long *buf
)
238 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
241 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
244 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
246 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
251 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
253 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
256 decimal64ToNumber (&d64
, &dn
);
257 decimal_from_decnumber (r
, &dn
, &set
);
260 /* Encode a real into an IEEE 754 decimal128 type. */
263 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
264 long *buf
, const REAL_VALUE_TYPE
*r
)
271 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
274 decimal_to_decnumber (r
, &dn
);
275 decimal128FromNumber (&d128
, &dn
, &set
);
277 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
279 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
281 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
283 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
285 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
290 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
292 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
294 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
296 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
301 /* Decode an IEEE 754 decimal128 type into a real. */
304 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
305 REAL_VALUE_TYPE
*r
, const long *buf
)
312 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
315 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
318 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
320 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
322 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
324 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
329 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
331 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
333 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
335 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
338 decimal128ToNumber (&d128
, &dn
);
339 decimal_from_decnumber (r
, &dn
, &set
);
342 /* Helper function to convert from a binary real internal
346 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
350 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
352 decimal128ToString (d128
, string
);
353 real_from_string3 (to
, string
, mode
);
357 /* Helper function to convert from a binary real internal
361 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
365 /* We convert to string, then to decNumber then to decimal128. */
366 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
367 decimal_real_from_string (to
, string
);
370 /* Helper function to real.c:do_compare() to handle decimal internal
371 representation including when one of the operands is still in the
372 binary internal representation. */
375 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
379 decNumber dn
, dn2
, dn3
;
380 REAL_VALUE_TYPE a1
, b1
;
382 /* If either operand is non-decimal, create temporary versions. */
385 decimal_from_binary (&a1
, a
);
390 decimal_from_binary (&b1
, b
);
394 /* Convert into decNumber form for comparison operation. */
395 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
397 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
398 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
400 /* Finally, do the comparison. */
401 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
403 /* Return the comparison result. */
404 if (decNumberIsNaN (&dn
))
406 else if (decNumberIsZero (&dn
))
408 else if (decNumberIsNegative (&dn
))
414 /* Helper to round_for_format, handling decimal float types. */
417 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
422 /* Real encoding occurs later. */
423 if (r
->cl
!= rvc_normal
)
426 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
428 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
430 if (fmt
== &decimal_quad_format
)
432 /* The internal format is already in this format. */
435 else if (fmt
== &decimal_single_format
)
438 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
441 decimal32FromNumber (&d32
, &dn
, &set
);
442 decimal32ToNumber (&d32
, &dn
);
444 else if (fmt
== &decimal_double_format
)
447 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
450 decimal64FromNumber (&d64
, &dn
, &set
);
451 decimal64ToNumber (&d64
, &dn
);
456 decimal_from_decnumber (r
, &dn
, &set
);
459 /* Extend or truncate to a new mode. Handles conversions between
460 binary and decimal types. */
463 decimal_real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
464 const REAL_VALUE_TYPE
*a
)
466 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
468 if (a
->decimal
&& fmt
->b
== 10)
471 decimal_to_binary (r
, a
, mode
);
473 decimal_from_binary (r
, a
);
476 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
477 significant digits in the result, bounded by BUF_SIZE. If DIGITS
478 is 0, choose the maximum for the representation. If
479 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
480 DIGITS or CROP_TRAILING_ZEROS. */
483 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
485 size_t digits ATTRIBUTE_UNUSED
,
486 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
488 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
490 /* decimal128ToString requires space for at least 24 characters;
491 Require two more for suffix. */
492 gcc_assert (buf_size
>= 24);
493 decimal128ToString (d128
, str
);
497 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
498 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
504 decimal_to_decnumber (op0
, &dn2
);
505 decimal_to_decnumber (op1
, &dn3
);
507 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
511 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
513 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
515 decimal_from_decnumber (r
, &dn
, &set
);
517 /* Return true, if inexact. */
518 return (set
.status
& DEC_Inexact
);
521 /* Compute R = OP0 * OP1. */
524 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
525 const REAL_VALUE_TYPE
*op1
)
528 decNumber dn
, dn2
, dn3
;
530 decimal_to_decnumber (op0
, &dn2
);
531 decimal_to_decnumber (op1
, &dn3
);
533 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
536 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
537 decimal_from_decnumber (r
, &dn
, &set
);
539 /* Return true, if inexact. */
540 return (set
.status
& DEC_Inexact
);
543 /* Compute R = OP0 / OP1. */
546 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
547 const REAL_VALUE_TYPE
*op1
)
550 decNumber dn
, dn2
, dn3
;
552 decimal_to_decnumber (op0
, &dn2
);
553 decimal_to_decnumber (op1
, &dn3
);
555 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
558 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
559 decimal_from_decnumber (r
, &dn
, &set
);
561 /* Return true, if inexact. */
562 return (set
.status
& DEC_Inexact
);
565 /* Set R to A truncated to an integral value toward zero (decimal
569 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
574 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
576 set
.round
= DEC_ROUND_DOWN
;
577 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
579 decNumberToIntegralValue (&dn
, &dn2
, &set
);
580 decimal_from_decnumber (r
, &dn
, &set
);
583 /* Render decimal float value R as an integer. */
586 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
589 decNumber dn
, dn2
, dn3
;
593 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
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
604 decNumberToString (&dn
, string
);
605 real_from_string (&to
, string
);
606 return real_to_integer (&to
);
609 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
610 value does not fit. */
613 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
616 decNumber dn
, dn2
, dn3
;
620 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
622 set
.round
= DEC_ROUND_DOWN
;
623 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
625 decNumberToIntegralValue (&dn2
, &dn
, &set
);
626 decNumberZero (&dn3
);
627 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
629 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
631 decNumberToString (&dn
, string
);
632 real_from_string (&to
, string
);
633 return real_to_integer (&to
, fail
, precision
);
636 /* Perform the decimal floating point operation described by CODE.
637 For a unary operation, OP1 will be NULL. This function returns
638 true if the result may be inexact due to loss of precision. */
641 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
642 const REAL_VALUE_TYPE
*op0
,
643 const REAL_VALUE_TYPE
*op1
)
645 REAL_VALUE_TYPE a
, b
;
647 /* If either operand is non-decimal, create temporaries. */
650 decimal_from_binary (&a
, op0
);
653 if (op1
&& !op1
->decimal
)
655 decimal_from_binary (&b
, op1
);
662 return decimal_do_add (r
, op0
, op1
, 0);
665 return decimal_do_add (r
, op0
, op1
, 1);
668 return decimal_do_multiply (r
, op0
, op1
);
671 return decimal_do_divide (r
, op0
, op1
);
674 if (op1
->cl
== rvc_nan
)
676 else if (real_compare (UNLT_EXPR
, op0
, op1
))
683 if (op1
->cl
== rvc_nan
)
685 else if (real_compare (LT_EXPR
, op0
, op1
))
695 decimal128FlipSign ((decimal128
*) r
->sig
);
696 /* Keep sign field in sync. */
704 /* Clear sign bit. */
705 decimal128ClearSign ((decimal128
*) r
->sig
);
706 /* Keep sign field in sync. */
712 decimal_do_fix_trunc (r
, op0
);
720 /* Fills R with the largest finite value representable in mode MODE.
721 If SIGN is nonzero, R is set to the most negative finite value. */
724 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
734 max
= "9.999999999999999E384";
737 max
= "9.999999999999999999999999999999999E6144";
743 decimal_real_from_string (r
, max
);
745 decimal128SetSign ((decimal128
*) r
->sig
, 1);