1 /* Decimal floating point support.
2 Copyright (C) 2005-2023 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 /* The order of the following headers is important for making sure
28 decNumber structure is large enough to hold decimal128 digits. */
30 #include "decimal128.h"
31 #include "decimal64.h"
32 #include "decimal32.h"
34 #ifndef WORDS_BIGENDIAN
35 #define WORDS_BIGENDIAN 0
38 /* Initialize R (a real with the decimal flag set) from DN. Can
39 utilize status passed in via CONTEXT, if a previous operation had
40 interesting status. */
43 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
45 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
48 if (decNumberIsNaN (dn
))
50 if (decNumberIsInfinite (dn
))
52 if (context
->status
& DEC_Overflow
)
54 if (decNumberIsNegative (dn
))
58 if (r
->cl
!= rvc_normal
)
61 decContextDefault (context
, DEC_INIT_DECIMAL128
);
64 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
67 /* Create decimal encoded R from string S. */
70 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
74 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
77 decNumberFromString (&dn
, s
, &set
);
79 /* It would be more efficient to store directly in decNumber format,
80 but that is impractical from current data structure size.
81 Encoding as a decimal128 is much more compact. */
82 decimal_from_decnumber (r
, &dn
, &set
);
85 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
88 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
91 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
100 decNumberFromString (dn
, "Infinity", &set
);
104 decNumberFromString (dn
, "snan", &set
);
106 decNumberFromString (dn
, "nan", &set
);
111 /* dconst{1,2,m1,half} are used in various places in
112 the middle-end and optimizers, allow them here
113 as an exception by converting them to decimal. */
114 if (memcmp (r
, &dconst1
, sizeof (*r
)) == 0)
116 decNumberFromString (dn
, "1", &set
);
119 if (memcmp (r
, &dconst2
, sizeof (*r
)) == 0)
121 decNumberFromString (dn
, "2", &set
);
124 if (memcmp (r
, &dconstm1
, sizeof (*r
)) == 0)
126 decNumberFromString (dn
, "-1", &set
);
129 if (memcmp (r
, &dconsthalf
, sizeof (*r
)) == 0)
131 decNumberFromString (dn
, "0.5", &set
);
136 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
142 /* Fix up sign bit. */
143 if (r
->sign
!= decNumberIsNegative (dn
))
147 /* Encode a real into an IEEE 754 decimal32 type. */
150 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
151 long *buf
, const REAL_VALUE_TYPE
*r
)
158 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
161 decimal_to_decnumber (r
, &dn
);
162 decimal32FromNumber (&d32
, &dn
, &set
);
164 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
168 /* Decode an IEEE 754 decimal32 type into a real. */
171 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
172 REAL_VALUE_TYPE
*r
, const long *buf
)
179 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
183 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
185 decimal32ToNumber (&d32
, &dn
);
186 decimal_from_decnumber (r
, &dn
, &set
);
189 /* Encode a real into an IEEE 754 decimal64 type. */
192 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
193 long *buf
, const REAL_VALUE_TYPE
*r
)
200 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
203 decimal_to_decnumber (r
, &dn
);
204 decimal64FromNumber (&d64
, &dn
, &set
);
206 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
208 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
210 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
215 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
217 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
222 /* Decode an IEEE 754 decimal64 type into a real. */
225 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
226 REAL_VALUE_TYPE
*r
, const long *buf
)
233 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
236 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
239 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
241 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
246 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
248 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
251 decimal64ToNumber (&d64
, &dn
);
252 decimal_from_decnumber (r
, &dn
, &set
);
255 /* Encode a real into an IEEE 754 decimal128 type. */
258 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
259 long *buf
, const REAL_VALUE_TYPE
*r
)
266 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
269 decimal_to_decnumber (r
, &dn
);
270 decimal128FromNumber (&d128
, &dn
, &set
);
272 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
274 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
276 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
278 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
280 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
285 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
287 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
289 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
291 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
296 /* Decode an IEEE 754 decimal128 type into a real. */
299 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
300 REAL_VALUE_TYPE
*r
, const long *buf
)
307 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
310 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
313 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
315 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
317 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
319 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
324 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
326 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
328 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
330 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
333 decimal128ToNumber (&d128
, &dn
);
334 decimal_from_decnumber (r
, &dn
, &set
);
337 /* Helper function to convert from a binary real internal
341 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
342 const real_format
*fmt
)
345 if (from
->cl
== rvc_normal
)
347 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
348 decimal128ToString (d128
, string
);
351 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
352 real_from_string3 (to
, string
, fmt
);
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
);
367 /* When a canonical NaN is originally created, it is not marked as
368 decimal. Ensure the result of converting to another decimal type
369 (which passes through this function) is also marked as
371 if (from
->cl
== rvc_nan
&& from
->canonical
)
375 /* Helper function to real.cc:do_compare() to handle decimal internal
376 representation including when one of the operands is still in the
377 binary internal representation. */
380 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
384 decNumber dn
, dn2
, dn3
;
385 REAL_VALUE_TYPE a1
, b1
;
387 /* If either operand is non-decimal, create temporary versions. */
390 decimal_from_binary (&a1
, a
);
395 decimal_from_binary (&b1
, b
);
399 /* Convert into decNumber form for comparison operation. */
400 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
402 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
403 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
405 /* Finally, do the comparison. */
406 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
408 /* Return the comparison result. */
409 if (decNumberIsNaN (&dn
))
411 else if (decNumberIsZero (&dn
))
413 else if (decNumberIsNegative (&dn
))
419 /* Helper to round_for_format, handling decimal float types. */
422 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
427 /* Real encoding occurs later. */
428 if (r
->cl
!= rvc_normal
)
431 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
433 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
435 if (fmt
== &decimal_quad_format
)
437 /* The internal format is already in this format. */
440 else if (fmt
== &decimal_single_format
)
443 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
446 decimal32FromNumber (&d32
, &dn
, &set
);
447 decimal32ToNumber (&d32
, &dn
);
449 else if (fmt
== &decimal_double_format
)
452 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
455 decimal64FromNumber (&d64
, &dn
, &set
);
456 decimal64ToNumber (&d64
, &dn
);
461 decimal_from_decnumber (r
, &dn
, &set
);
464 /* Extend or truncate to a new mode. Handles conversions between
465 binary and decimal types. */
468 decimal_real_convert (REAL_VALUE_TYPE
*r
, const real_format
*fmt
,
469 const REAL_VALUE_TYPE
*a
)
471 if (a
->decimal
&& fmt
->b
== 10)
474 decimal_to_binary (r
, a
, fmt
);
476 decimal_from_binary (r
, a
);
479 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
480 significant digits in the result, bounded by BUF_SIZE. If DIGITS
481 is 0, choose the maximum for the representation. If
482 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
483 DIGITS or CROP_TRAILING_ZEROS. */
486 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
488 size_t digits ATTRIBUTE_UNUSED
,
489 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
491 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
493 /* decimal128ToString requires space for at least 24 characters;
494 Require two more for suffix. */
495 gcc_assert (buf_size
>= 24);
496 decimal128ToString (d128
, str
);
500 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
501 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
507 decimal_to_decnumber (op0
, &dn2
);
508 decimal_to_decnumber (op1
, &dn3
);
510 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
514 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
516 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
518 decimal_from_decnumber (r
, &dn
, &set
);
520 /* Return true, if inexact. */
521 return (set
.status
& DEC_Inexact
);
524 /* Compute R = OP0 * OP1. */
527 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
528 const REAL_VALUE_TYPE
*op1
)
531 decNumber dn
, dn2
, dn3
;
533 decimal_to_decnumber (op0
, &dn2
);
534 decimal_to_decnumber (op1
, &dn3
);
536 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
539 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
540 decimal_from_decnumber (r
, &dn
, &set
);
542 /* Return true, if inexact. */
543 return (set
.status
& DEC_Inexact
);
546 /* Compute R = OP0 / OP1. */
549 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
550 const REAL_VALUE_TYPE
*op1
)
553 decNumber dn
, dn2
, dn3
;
555 decimal_to_decnumber (op0
, &dn2
);
556 decimal_to_decnumber (op1
, &dn3
);
558 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
561 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
562 decimal_from_decnumber (r
, &dn
, &set
);
564 /* Return true, if inexact. */
565 return (set
.status
& DEC_Inexact
);
568 /* Set R to A truncated to an integral value toward zero (decimal
572 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
577 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
579 set
.round
= DEC_ROUND_DOWN
;
580 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
582 decNumberToIntegralValue (&dn
, &dn2
, &set
);
583 decimal_from_decnumber (r
, &dn
, &set
);
586 /* Render decimal float value R as an integer. */
589 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
592 decNumber dn
, dn2
, dn3
;
596 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
598 set
.round
= DEC_ROUND_DOWN
;
599 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
601 decNumberToIntegralValue (&dn2
, &dn
, &set
);
602 decNumberZero (&dn3
);
603 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
605 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
607 decNumberToString (&dn
, string
);
608 real_from_string (&to
, string
);
609 return real_to_integer (&to
);
612 /* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
613 value does not fit. */
616 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
, bool *fail
, int precision
)
619 decNumber dn
, dn2
, dn3
;
623 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
625 set
.round
= DEC_ROUND_DOWN
;
626 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
628 decNumberToIntegralValue (&dn2
, &dn
, &set
);
629 decNumberZero (&dn3
);
630 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
632 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
634 decNumberToString (&dn
, string
);
635 real_from_string (&to
, string
);
636 return real_to_integer (&to
, fail
, precision
);
639 /* Perform the decimal floating point operation described by CODE.
640 For a unary operation, OP1 will be NULL. This function returns
641 true if the result may be inexact due to loss of precision. */
644 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
645 const REAL_VALUE_TYPE
*op0
,
646 const REAL_VALUE_TYPE
*op1
)
648 REAL_VALUE_TYPE a
, b
;
650 /* If either operand is non-decimal, create temporaries. */
653 decimal_from_binary (&a
, op0
);
656 if (op1
&& !op1
->decimal
)
658 decimal_from_binary (&b
, op1
);
665 return decimal_do_add (r
, op0
, op1
, 0);
668 return decimal_do_add (r
, op0
, op1
, 1);
671 return decimal_do_multiply (r
, op0
, op1
);
674 return decimal_do_divide (r
, op0
, op1
);
677 if (op1
->cl
== rvc_nan
)
679 else if (real_compare (UNLT_EXPR
, op0
, op1
))
686 if (op1
->cl
== rvc_nan
)
688 else if (real_compare (LT_EXPR
, op0
, op1
))
698 decimal128FlipSign ((decimal128
*) r
->sig
);
699 /* Keep sign field in sync. */
707 /* Clear sign bit. */
708 decimal128ClearSign ((decimal128
*) r
->sig
);
709 /* Keep sign field in sync. */
715 decimal_do_fix_trunc (r
, op0
);
723 /* Fills R with the largest finite value representable in mode MODE.
724 If SIGN is nonzero, R is set to the most negative finite value. */
727 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, machine_mode mode
)
737 max
= "9.999999999999999E384";
740 max
= "9.999999999999999999999999999999999E6144";
746 decimal_real_from_string (r
, max
);
748 decimal128SetSign ((decimal128
*) r
->sig
, 1);