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"
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
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. */
49 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
51 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
54 if (decNumberIsNaN (dn
))
56 if (decNumberIsInfinite (dn
))
58 if (context
->status
& DEC_Overflow
)
60 if (decNumberIsNegative (dn
))
64 if (r
->cl
!= rvc_normal
)
67 decContextDefault (context
, DEC_INIT_DECIMAL128
);
70 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
73 /* Create decimal encoded R from string S. */
76 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
80 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
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. */
94 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
97 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
106 decNumberFromString (dn
, "Infinity", &set
);
110 decNumberFromString (dn
, "snan", &set
);
112 decNumberFromString (dn
, "nan", &set
);
117 /* dconst{1,2,m1,half} are used in various places in
118 the middle-end and optimizers, allow them here
119 as an exception by converting them to decimal. */
120 if (memcmp (r
, &dconst1
, sizeof (*r
)) == 0)
122 decNumberFromString (dn
, "1", &set
);
125 if (memcmp (r
, &dconst2
, sizeof (*r
)) == 0)
127 decNumberFromString (dn
, "2", &set
);
130 if (memcmp (r
, &dconstm1
, sizeof (*r
)) == 0)
132 decNumberFromString (dn
, "-1", &set
);
135 if (memcmp (r
, &dconsthalf
, sizeof (*r
)) == 0)
137 decNumberFromString (dn
, "0.5", &set
);
142 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
148 /* Fix up sign bit. */
149 if (r
->sign
!= decNumberIsNegative (dn
))
153 /* Encode a real into an IEEE 754 decimal32 type. */
156 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
157 long *buf
, const REAL_VALUE_TYPE
*r
)
164 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
167 decimal_to_decnumber (r
, &dn
);
168 decimal32FromNumber (&d32
, &dn
, &set
);
170 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
174 /* Decode an IEEE 754 decimal32 type into a real. */
177 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
178 REAL_VALUE_TYPE
*r
, const long *buf
)
185 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
189 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
191 decimal32ToNumber (&d32
, &dn
);
192 decimal_from_decnumber (r
, &dn
, &set
);
195 /* Encode a real into an IEEE 754 decimal64 type. */
198 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
199 long *buf
, const REAL_VALUE_TYPE
*r
)
206 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
209 decimal_to_decnumber (r
, &dn
);
210 decimal64FromNumber (&d64
, &dn
, &set
);
212 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
214 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
216 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
221 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
223 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
228 /* Decode an IEEE 754 decimal64 type into a real. */
231 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
232 REAL_VALUE_TYPE
*r
, const long *buf
)
239 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
242 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
245 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
247 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
252 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
254 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
257 decimal64ToNumber (&d64
, &dn
);
258 decimal_from_decnumber (r
, &dn
, &set
);
261 /* Encode a real into an IEEE 754 decimal128 type. */
264 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
265 long *buf
, const REAL_VALUE_TYPE
*r
)
272 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
275 decimal_to_decnumber (r
, &dn
);
276 decimal128FromNumber (&d128
, &dn
, &set
);
278 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
280 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
282 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
284 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
286 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
291 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
293 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
295 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
297 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
302 /* Decode an IEEE 754 decimal128 type into a real. */
305 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
306 REAL_VALUE_TYPE
*r
, const long *buf
)
313 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
316 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
319 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
321 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
323 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
325 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
330 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
332 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
334 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
336 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
339 decimal128ToNumber (&d128
, &dn
);
340 decimal_from_decnumber (r
, &dn
, &set
);
343 /* Helper function to convert from a binary real internal
347 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
351 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
353 decimal128ToString (d128
, string
);
354 real_from_string3 (to
, string
, mode
);
358 /* Helper function to convert from a binary real internal
362 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
366 /* We convert to string, then to decNumber then to decimal128. */
367 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
368 decimal_real_from_string (to
, string
);
371 /* Helper function to real.c:do_compare() to handle decimal internal
372 representation including when one of the operands is still in the
373 binary internal representation. */
376 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
380 decNumber dn
, dn2
, dn3
;
381 REAL_VALUE_TYPE a1
, b1
;
383 /* If either operand is non-decimal, create temporary versions. */
386 decimal_from_binary (&a1
, a
);
391 decimal_from_binary (&b1
, b
);
395 /* Convert into decNumber form for comparison operation. */
396 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
398 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
399 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
401 /* Finally, do the comparison. */
402 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
404 /* Return the comparison result. */
405 if (decNumberIsNaN (&dn
))
407 else if (decNumberIsZero (&dn
))
409 else if (decNumberIsNegative (&dn
))
415 /* Helper to round_for_format, handling decimal float types. */
418 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
423 /* Real encoding occurs later. */
424 if (r
->cl
!= rvc_normal
)
427 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
429 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
431 if (fmt
== &decimal_quad_format
)
433 /* The internal format is already in this format. */
436 else if (fmt
== &decimal_single_format
)
439 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
442 decimal32FromNumber (&d32
, &dn
, &set
);
443 decimal32ToNumber (&d32
, &dn
);
445 else if (fmt
== &decimal_double_format
)
448 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
451 decimal64FromNumber (&d64
, &dn
, &set
);
452 decimal64ToNumber (&d64
, &dn
);
457 decimal_from_decnumber (r
, &dn
, &set
);
460 /* Extend or truncate to a new mode. Handles conversions between
461 binary and decimal types. */
464 decimal_real_convert (REAL_VALUE_TYPE
*r
, machine_mode mode
,
465 const REAL_VALUE_TYPE
*a
)
467 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
469 if (a
->decimal
&& fmt
->b
== 10)
472 decimal_to_binary (r
, a
, mode
);
474 decimal_from_binary (r
, a
);
477 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
478 significant digits in the result, bounded by BUF_SIZE. If DIGITS
479 is 0, choose the maximum for the representation. If
480 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
481 DIGITS or CROP_TRAILING_ZEROS. */
484 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
486 size_t digits ATTRIBUTE_UNUSED
,
487 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
489 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
491 /* decimal128ToString requires space for at least 24 characters;
492 Require two more for suffix. */
493 gcc_assert (buf_size
>= 24);
494 decimal128ToString (d128
, str
);
498 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
499 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
505 decimal_to_decnumber (op0
, &dn2
);
506 decimal_to_decnumber (op1
, &dn3
);
508 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
512 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
514 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
516 decimal_from_decnumber (r
, &dn
, &set
);
518 /* Return true, if inexact. */
519 return (set
.status
& DEC_Inexact
);
522 /* Compute R = OP0 * OP1. */
525 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
526 const REAL_VALUE_TYPE
*op1
)
529 decNumber dn
, dn2
, dn3
;
531 decimal_to_decnumber (op0
, &dn2
);
532 decimal_to_decnumber (op1
, &dn3
);
534 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
537 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
538 decimal_from_decnumber (r
, &dn
, &set
);
540 /* Return true, if inexact. */
541 return (set
.status
& DEC_Inexact
);
544 /* Compute R = OP0 / OP1. */
547 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
548 const REAL_VALUE_TYPE
*op1
)
551 decNumber dn
, dn2
, dn3
;
553 decimal_to_decnumber (op0
, &dn2
);
554 decimal_to_decnumber (op1
, &dn3
);
556 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
559 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
560 decimal_from_decnumber (r
, &dn
, &set
);
562 /* Return true, if inexact. */
563 return (set
.status
& DEC_Inexact
);
566 /* Set R to A truncated to an integral value toward zero (decimal
570 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
575 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
577 set
.round
= DEC_ROUND_DOWN
;
578 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
580 decNumberToIntegralValue (&dn
, &dn2
, &set
);
581 decimal_from_decnumber (r
, &dn
, &set
);
584 /* Render decimal float value R as an integer. */
587 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
590 decNumber dn
, dn2
, dn3
;
594 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
596 set
.round
= DEC_ROUND_DOWN
;
597 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
599 decNumberToIntegralValue (&dn2
, &dn
, &set
);
600 decNumberZero (&dn3
);
601 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
603 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
605 decNumberToString (&dn
, string
);
606 real_from_string (&to
, string
);
607 return real_to_integer (&to
);
610 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
611 value does not fit. */
614 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
617 decNumber dn
, dn2
, dn3
;
621 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
623 set
.round
= DEC_ROUND_DOWN
;
624 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
626 decNumberToIntegralValue (&dn2
, &dn
, &set
);
627 decNumberZero (&dn3
);
628 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
630 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
632 decNumberToString (&dn
, string
);
633 real_from_string (&to
, string
);
634 return real_to_integer (&to
, fail
, precision
);
637 /* Perform the decimal floating point operation described by CODE.
638 For a unary operation, OP1 will be NULL. This function returns
639 true if the result may be inexact due to loss of precision. */
642 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
643 const REAL_VALUE_TYPE
*op0
,
644 const REAL_VALUE_TYPE
*op1
)
646 REAL_VALUE_TYPE a
, b
;
648 /* If either operand is non-decimal, create temporaries. */
651 decimal_from_binary (&a
, op0
);
654 if (op1
&& !op1
->decimal
)
656 decimal_from_binary (&b
, op1
);
663 return decimal_do_add (r
, op0
, op1
, 0);
666 return decimal_do_add (r
, op0
, op1
, 1);
669 return decimal_do_multiply (r
, op0
, op1
);
672 return decimal_do_divide (r
, op0
, op1
);
675 if (op1
->cl
== rvc_nan
)
677 else if (real_compare (UNLT_EXPR
, op0
, op1
))
684 if (op1
->cl
== rvc_nan
)
686 else if (real_compare (LT_EXPR
, op0
, op1
))
696 decimal128FlipSign ((decimal128
*) r
->sig
);
697 /* Keep sign field in sync. */
705 /* Clear sign bit. */
706 decimal128ClearSign ((decimal128
*) r
->sig
);
707 /* Keep sign field in sync. */
713 decimal_do_fix_trunc (r
, op0
);
721 /* Fills R with the largest finite value representable in mode MODE.
722 If SIGN is nonzero, R is set to the most negative finite value. */
725 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
735 max
= "9.999999999999999E384";
738 max
= "9.999999999999999999999999999999999E6144";
744 decimal_real_from_string (r
, max
);
746 decimal128SetSign ((decimal128
*) r
->sig
, 1);