1 /* Decimal floating point support.
2 Copyright (C) 2005-2013 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"
28 /* The order of the following headers is important for making sure
29 decNumber structure is large enough to hold decimal128 digits. */
31 #include "decimal128.h"
32 #include "decimal128Local.h"
33 #include "decimal64.h"
34 #include "decimal32.h"
35 #include "decNumber.h"
37 #ifndef WORDS_BIGENDIAN
38 #define WORDS_BIGENDIAN 0
41 /* Initialize R (a real with the decimal flag set) from DN. Can
42 utilize status passed in via CONTEXT, if a previous operation had
43 interesting status. */
46 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
48 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
51 if (decNumberIsNaN (dn
))
53 if (decNumberIsInfinite (dn
))
55 if (context
->status
& DEC_Overflow
)
57 if (decNumberIsNegative (dn
))
61 if (r
->cl
!= rvc_normal
)
64 decContextDefault (context
, DEC_INIT_DECIMAL128
);
67 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
70 /* Create decimal encoded R from string S. */
73 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
77 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
80 decNumberFromString (&dn
, s
, &set
);
82 /* It would be more efficient to store directly in decNumber format,
83 but that is impractical from current data structure size.
84 Encoding as a decimal128 is much more compact. */
85 decimal_from_decnumber (r
, &dn
, &set
);
88 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
91 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
94 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
103 decNumberFromString (dn
, "Infinity", &set
);
107 decNumberFromString (dn
, "snan", &set
);
109 decNumberFromString (dn
, "nan", &set
);
114 /* dconst{1,2,m1,half} are used in various places in
115 the middle-end and optimizers, allow them here
116 as an exception by converting them to decimal. */
117 if (memcmp (r
, &dconst1
, sizeof (*r
)) == 0)
119 decNumberFromString (dn
, "1", &set
);
122 if (memcmp (r
, &dconst2
, sizeof (*r
)) == 0)
124 decNumberFromString (dn
, "2", &set
);
127 if (memcmp (r
, &dconstm1
, sizeof (*r
)) == 0)
129 decNumberFromString (dn
, "-1", &set
);
132 if (memcmp (r
, &dconsthalf
, sizeof (*r
)) == 0)
134 decNumberFromString (dn
, "0.5", &set
);
139 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
145 /* Fix up sign bit. */
146 if (r
->sign
!= decNumberIsNegative (dn
))
150 /* Encode a real into an IEEE 754 decimal32 type. */
153 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
154 long *buf
, const REAL_VALUE_TYPE
*r
)
161 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
164 decimal_to_decnumber (r
, &dn
);
165 decimal32FromNumber (&d32
, &dn
, &set
);
167 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
171 /* Decode an IEEE 754 decimal32 type into a real. */
174 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
175 REAL_VALUE_TYPE
*r
, const long *buf
)
182 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
186 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
188 decimal32ToNumber (&d32
, &dn
);
189 decimal_from_decnumber (r
, &dn
, &set
);
192 /* Encode a real into an IEEE 754 decimal64 type. */
195 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
196 long *buf
, const REAL_VALUE_TYPE
*r
)
203 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
206 decimal_to_decnumber (r
, &dn
);
207 decimal64FromNumber (&d64
, &dn
, &set
);
209 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
211 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
213 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
218 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
220 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
225 /* Decode an IEEE 754 decimal64 type into a real. */
228 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
229 REAL_VALUE_TYPE
*r
, const long *buf
)
236 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
239 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
242 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
244 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
249 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
251 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
254 decimal64ToNumber (&d64
, &dn
);
255 decimal_from_decnumber (r
, &dn
, &set
);
258 /* Encode a real into an IEEE 754 decimal128 type. */
261 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
262 long *buf
, const REAL_VALUE_TYPE
*r
)
269 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
272 decimal_to_decnumber (r
, &dn
);
273 decimal128FromNumber (&d128
, &dn
, &set
);
275 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
277 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
279 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
281 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
283 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
288 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
290 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
292 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
294 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
299 /* Decode an IEEE 754 decimal128 type into a real. */
302 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
303 REAL_VALUE_TYPE
*r
, const long *buf
)
310 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
313 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
316 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
318 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
320 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
322 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
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));
336 decimal128ToNumber (&d128
, &dn
);
337 decimal_from_decnumber (r
, &dn
, &set
);
340 /* Helper function to convert from a binary real internal
344 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
345 enum machine_mode mode
)
348 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
350 decimal128ToString (d128
, string
);
351 real_from_string3 (to
, string
, mode
);
355 /* Helper function to convert from a binary real internal
359 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
363 /* We convert to string, then to decNumber then to decimal128. */
364 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
365 decimal_real_from_string (to
, string
);
368 /* Helper function to real.c:do_compare() to handle decimal internal
369 representation including when one of the operands is still in the
370 binary internal representation. */
373 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
377 decNumber dn
, dn2
, dn3
;
378 REAL_VALUE_TYPE a1
, b1
;
380 /* If either operand is non-decimal, create temporary versions. */
383 decimal_from_binary (&a1
, a
);
388 decimal_from_binary (&b1
, b
);
392 /* Convert into decNumber form for comparison operation. */
393 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
395 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
396 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
398 /* Finally, do the comparison. */
399 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
401 /* Return the comparison result. */
402 if (decNumberIsNaN (&dn
))
404 else if (decNumberIsZero (&dn
))
406 else if (decNumberIsNegative (&dn
))
412 /* Helper to round_for_format, handling decimal float types. */
415 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
420 /* Real encoding occurs later. */
421 if (r
->cl
!= rvc_normal
)
424 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
426 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
428 if (fmt
== &decimal_quad_format
)
430 /* The internal format is already in this format. */
433 else if (fmt
== &decimal_single_format
)
436 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
439 decimal32FromNumber (&d32
, &dn
, &set
);
440 decimal32ToNumber (&d32
, &dn
);
442 else if (fmt
== &decimal_double_format
)
445 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
448 decimal64FromNumber (&d64
, &dn
, &set
);
449 decimal64ToNumber (&d64
, &dn
);
454 decimal_from_decnumber (r
, &dn
, &set
);
457 /* Extend or truncate to a new mode. Handles conversions between
458 binary and decimal types. */
461 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
462 const REAL_VALUE_TYPE
*a
)
464 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
466 if (a
->decimal
&& fmt
->b
== 10)
469 decimal_to_binary (r
, a
, mode
);
471 decimal_from_binary (r
, a
);
474 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
475 significant digits in the result, bounded by BUF_SIZE. If DIGITS
476 is 0, choose the maximum for the representation. If
477 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
478 DIGITS or CROP_TRAILING_ZEROS. */
481 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
483 size_t digits ATTRIBUTE_UNUSED
,
484 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
486 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
488 /* decimal128ToString requires space for at least 24 characters;
489 Require two more for suffix. */
490 gcc_assert (buf_size
>= 24);
491 decimal128ToString (d128
, str
);
495 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
496 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
502 decimal_to_decnumber (op0
, &dn2
);
503 decimal_to_decnumber (op1
, &dn3
);
505 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
509 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
511 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
513 decimal_from_decnumber (r
, &dn
, &set
);
515 /* Return true, if inexact. */
516 return (set
.status
& DEC_Inexact
);
519 /* Compute R = OP0 * OP1. */
522 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
523 const REAL_VALUE_TYPE
*op1
)
526 decNumber dn
, dn2
, dn3
;
528 decimal_to_decnumber (op0
, &dn2
);
529 decimal_to_decnumber (op1
, &dn3
);
531 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
534 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
535 decimal_from_decnumber (r
, &dn
, &set
);
537 /* Return true, if inexact. */
538 return (set
.status
& DEC_Inexact
);
541 /* Compute R = OP0 / OP1. */
544 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
545 const REAL_VALUE_TYPE
*op1
)
548 decNumber dn
, dn2
, dn3
;
550 decimal_to_decnumber (op0
, &dn2
);
551 decimal_to_decnumber (op1
, &dn3
);
553 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
556 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
557 decimal_from_decnumber (r
, &dn
, &set
);
559 /* Return true, if inexact. */
560 return (set
.status
& DEC_Inexact
);
563 /* Set R to A truncated to an integral value toward zero (decimal
567 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
572 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
574 set
.round
= DEC_ROUND_DOWN
;
575 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
577 decNumberToIntegralValue (&dn
, &dn2
, &set
);
578 decimal_from_decnumber (r
, &dn
, &set
);
581 /* Render decimal float value R as an integer. */
584 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
587 decNumber dn
, dn2
, dn3
;
591 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
593 set
.round
= DEC_ROUND_DOWN
;
594 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
596 decNumberToIntegralValue (&dn2
, &dn
, &set
);
597 decNumberZero (&dn3
);
598 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
600 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
602 decNumberToString (&dn
, string
);
603 real_from_string (&to
, string
);
604 return real_to_integer (&to
);
607 /* Likewise, but to an integer pair, HI+LOW. */
610 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
611 const REAL_VALUE_TYPE
*r
)
614 decNumber dn
, dn2
, dn3
;
618 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
620 set
.round
= DEC_ROUND_DOWN
;
621 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
623 decNumberToIntegralValue (&dn2
, &dn
, &set
);
624 decNumberZero (&dn3
);
625 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
627 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
629 decNumberToString (&dn
, string
);
630 real_from_string (&to
, string
);
631 real_to_integer2 (plow
, phigh
, &to
);
634 /* Perform the decimal floating point operation described by CODE.
635 For a unary operation, OP1 will be NULL. This function returns
636 true if the result may be inexact due to loss of precision. */
639 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
640 const REAL_VALUE_TYPE
*op0
,
641 const REAL_VALUE_TYPE
*op1
)
643 REAL_VALUE_TYPE a
, b
;
645 /* If either operand is non-decimal, create temporaries. */
648 decimal_from_binary (&a
, op0
);
651 if (op1
&& !op1
->decimal
)
653 decimal_from_binary (&b
, op1
);
660 return decimal_do_add (r
, op0
, op1
, 0);
663 return decimal_do_add (r
, op0
, op1
, 1);
666 return decimal_do_multiply (r
, op0
, op1
);
669 return decimal_do_divide (r
, op0
, op1
);
672 if (op1
->cl
== rvc_nan
)
674 else if (real_compare (UNLT_EXPR
, op0
, op1
))
681 if (op1
->cl
== rvc_nan
)
683 else if (real_compare (LT_EXPR
, op0
, op1
))
693 decimal128FlipSign ((decimal128
*) r
->sig
);
694 /* Keep sign field in sync. */
702 /* Clear sign bit. */
703 decimal128ClearSign ((decimal128
*) r
->sig
);
704 /* Keep sign field in sync. */
710 decimal_do_fix_trunc (r
, op0
);
718 /* Fills R with the largest finite value representable in mode MODE.
719 If SIGN is nonzero, R is set to the most negative finite value. */
722 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
732 max
= "9.999999999999999E384";
735 max
= "9.999999999999999999999999999999999E6144";
741 decimal_real_from_string (r
, max
);
743 decimal128SetSign ((decimal128
*) r
->sig
, 1);