1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 /* The order of the following headers is important for making sure
30 decNumber structure is large enough to hold decimal128 digits. */
32 #include "decimal128.h"
33 #include "decimal128Local.h"
34 #include "decimal64.h"
35 #include "decimal32.h"
36 #include "decNumber.h"
38 #ifndef WORDS_BIGENDIAN
39 #define WORDS_BIGENDIAN 0
42 /* Initialize R (a real with the decimal flag set) from DN. Can
43 utilize status passed in via CONTEXT, if a previous operation had
44 interesting status. */
47 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
49 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
52 if (decNumberIsNaN (dn
))
54 if (decNumberIsInfinite (dn
))
56 if (context
->status
& DEC_Overflow
)
58 if (decNumberIsNegative (dn
))
62 if (r
->cl
!= rvc_normal
)
65 decContextDefault (context
, DEC_INIT_DECIMAL128
);
68 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
71 /* Create decimal encoded R from string S. */
74 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
78 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
81 decNumberFromString (&dn
, s
, &set
);
83 /* It would be more efficient to store directly in decNumber format,
84 but that is impractical from current data structure size.
85 Encoding as a decimal128 is much more compact. */
86 decimal_from_decnumber (r
, &dn
, &set
);
89 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
92 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
95 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
104 decNumberFromString (dn
, "Infinity", &set
);
108 decNumberFromString (dn
, "snan", &set
);
110 decNumberFromString (dn
, "nan", &set
);
113 gcc_assert (r
->decimal
);
114 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
120 /* Fix up sign bit. */
121 if (r
->sign
!= decNumberIsNegative (dn
))
125 /* Encode a real into an IEEE 754 decimal32 type. */
128 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
129 long *buf
, const REAL_VALUE_TYPE
*r
)
136 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
139 decimal_to_decnumber (r
, &dn
);
140 decimal32FromNumber (&d32
, &dn
, &set
);
142 memcpy (&image
, d32
.bytes
, sizeof (int32_t));
146 /* Decode an IEEE 754 decimal32 type into a real. */
149 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
150 REAL_VALUE_TYPE
*r
, const long *buf
)
157 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
161 memcpy (&d32
.bytes
, &image
, sizeof (int32_t));
163 decimal32ToNumber (&d32
, &dn
);
164 decimal_from_decnumber (r
, &dn
, &set
);
167 /* Encode a real into an IEEE 754 decimal64 type. */
170 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
171 long *buf
, const REAL_VALUE_TYPE
*r
)
178 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
181 decimal_to_decnumber (r
, &dn
);
182 decimal64FromNumber (&d64
, &dn
, &set
);
184 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
186 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
188 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
193 memcpy (&image
, &d64
.bytes
[4], sizeof (int32_t));
195 memcpy (&image
, &d64
.bytes
[0], sizeof (int32_t));
200 /* Decode an IEEE 754 decimal64 type into a real. */
203 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
204 REAL_VALUE_TYPE
*r
, const long *buf
)
211 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
214 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
217 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
219 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
224 memcpy (&d64
.bytes
[0], &image
, sizeof (int32_t));
226 memcpy (&d64
.bytes
[4], &image
, sizeof (int32_t));
229 decimal64ToNumber (&d64
, &dn
);
230 decimal_from_decnumber (r
, &dn
, &set
);
233 /* Encode a real into an IEEE 754 decimal128 type. */
236 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
237 long *buf
, const REAL_VALUE_TYPE
*r
)
244 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
247 decimal_to_decnumber (r
, &dn
);
248 decimal128FromNumber (&d128
, &dn
, &set
);
250 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
252 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
254 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
256 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
258 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
263 memcpy (&image
, &d128
.bytes
[12], sizeof (int32_t));
265 memcpy (&image
, &d128
.bytes
[8], sizeof (int32_t));
267 memcpy (&image
, &d128
.bytes
[4], sizeof (int32_t));
269 memcpy (&image
, &d128
.bytes
[0], sizeof (int32_t));
274 /* Decode an IEEE 754 decimal128 type into a real. */
277 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
278 REAL_VALUE_TYPE
*r
, const long *buf
)
285 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
288 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
291 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
293 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
295 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
297 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
302 memcpy (&d128
.bytes
[0], &image
, sizeof (int32_t));
304 memcpy (&d128
.bytes
[4], &image
, sizeof (int32_t));
306 memcpy (&d128
.bytes
[8], &image
, sizeof (int32_t));
308 memcpy (&d128
.bytes
[12], &image
, sizeof (int32_t));
311 decimal128ToNumber (&d128
, &dn
);
312 decimal_from_decnumber (r
, &dn
, &set
);
315 /* Helper function to convert from a binary real internal
319 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
320 enum machine_mode mode
)
323 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
325 decimal128ToString (d128
, string
);
326 real_from_string3 (to
, string
, mode
);
330 /* Helper function to convert from a binary real internal
334 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
338 /* We convert to string, then to decNumber then to decimal128. */
339 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
340 decimal_real_from_string (to
, string
);
343 /* Helper function to real.c:do_compare() to handle decimal internal
344 representation including when one of the operands is still in the
345 binary internal representation. */
348 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
352 decNumber dn
, dn2
, dn3
;
353 REAL_VALUE_TYPE a1
, b1
;
355 /* If either operand is non-decimal, create temporary versions. */
358 decimal_from_binary (&a1
, a
);
363 decimal_from_binary (&b1
, b
);
367 /* Convert into decNumber form for comparison operation. */
368 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
370 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
371 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
373 /* Finally, do the comparison. */
374 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
376 /* Return the comparison result. */
377 if (decNumberIsNaN (&dn
))
379 else if (decNumberIsZero (&dn
))
381 else if (decNumberIsNegative (&dn
))
387 /* Helper to round_for_format, handling decimal float types. */
390 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
395 /* Real encoding occurs later. */
396 if (r
->cl
!= rvc_normal
)
399 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
401 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
403 if (fmt
== &decimal_quad_format
)
405 /* The internal format is already in this format. */
408 else if (fmt
== &decimal_single_format
)
411 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
414 decimal32FromNumber (&d32
, &dn
, &set
);
415 decimal32ToNumber (&d32
, &dn
);
417 else if (fmt
== &decimal_double_format
)
420 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
423 decimal64FromNumber (&d64
, &dn
, &set
);
424 decimal64ToNumber (&d64
, &dn
);
429 decimal_from_decnumber (r
, &dn
, &set
);
432 /* Extend or truncate to a new mode. Handles conversions between
433 binary and decimal types. */
436 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
437 const REAL_VALUE_TYPE
*a
)
439 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
441 if (a
->decimal
&& fmt
->b
== 10)
444 decimal_to_binary (r
, a
, mode
);
446 decimal_from_binary (r
, a
);
449 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
450 significant digits in the result, bounded by BUF_SIZE. If DIGITS
451 is 0, choose the maximum for the representation. If
452 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
453 DIGITS or CROP_TRAILING_ZEROS. */
456 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
458 size_t digits ATTRIBUTE_UNUSED
,
459 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
461 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
463 /* decimal128ToString requires space for at least 24 characters;
464 Require two more for suffix. */
465 gcc_assert (buf_size
>= 24);
466 decimal128ToString (d128
, str
);
470 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
471 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
477 decimal_to_decnumber (op0
, &dn2
);
478 decimal_to_decnumber (op1
, &dn3
);
480 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
484 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
486 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
488 decimal_from_decnumber (r
, &dn
, &set
);
490 /* Return true, if inexact. */
491 return (set
.status
& DEC_Inexact
);
494 /* Compute R = OP0 * OP1. */
497 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
498 const REAL_VALUE_TYPE
*op1
)
501 decNumber dn
, dn2
, dn3
;
503 decimal_to_decnumber (op0
, &dn2
);
504 decimal_to_decnumber (op1
, &dn3
);
506 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
509 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
510 decimal_from_decnumber (r
, &dn
, &set
);
512 /* Return true, if inexact. */
513 return (set
.status
& DEC_Inexact
);
516 /* Compute R = OP0 / OP1. */
519 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
520 const REAL_VALUE_TYPE
*op1
)
523 decNumber dn
, dn2
, dn3
;
525 decimal_to_decnumber (op0
, &dn2
);
526 decimal_to_decnumber (op1
, &dn3
);
528 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
531 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
532 decimal_from_decnumber (r
, &dn
, &set
);
534 /* Return true, if inexact. */
535 return (set
.status
& DEC_Inexact
);
538 /* Set R to A truncated to an integral value toward zero (decimal
542 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
547 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
549 set
.round
= DEC_ROUND_DOWN
;
550 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
552 decNumberToIntegralValue (&dn
, &dn2
, &set
);
553 decimal_from_decnumber (r
, &dn
, &set
);
556 /* Render decimal float value R as an integer. */
559 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
562 decNumber dn
, dn2
, dn3
;
566 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
568 set
.round
= DEC_ROUND_DOWN
;
569 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
571 decNumberToIntegralValue (&dn2
, &dn
, &set
);
572 decNumberZero (&dn3
);
573 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
575 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
577 decNumberToString (&dn
, string
);
578 real_from_string (&to
, string
);
579 return real_to_integer (&to
);
582 /* Likewise, but to an integer pair, HI+LOW. */
585 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
586 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 real_to_integer2 (plow
, phigh
, &to
);
609 /* Perform the decimal floating point operation described by CODE.
610 For a unary operation, OP1 will be NULL. This function returns
611 true if the result may be inexact due to loss of precision. */
614 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
615 const REAL_VALUE_TYPE
*op0
,
616 const REAL_VALUE_TYPE
*op1
)
618 REAL_VALUE_TYPE a
, b
;
620 /* If either operand is non-decimal, create temporaries. */
623 decimal_from_binary (&a
, op0
);
626 if (op1
&& !op1
->decimal
)
628 decimal_from_binary (&b
, op1
);
635 return decimal_do_add (r
, op0
, op1
, 0);
638 return decimal_do_add (r
, op0
, op1
, 1);
641 return decimal_do_multiply (r
, op0
, op1
);
644 return decimal_do_divide (r
, op0
, op1
);
647 if (op1
->cl
== rvc_nan
)
649 else if (real_compare (UNLT_EXPR
, op0
, op1
))
656 if (op1
->cl
== rvc_nan
)
658 else if (real_compare (LT_EXPR
, op0
, op1
))
668 decimal128FlipSign ((decimal128
*) r
->sig
);
669 /* Keep sign field in sync. */
677 /* Clear sign bit. */
678 decimal128ClearSign ((decimal128
*) r
->sig
);
679 /* Keep sign field in sync. */
685 decimal_do_fix_trunc (r
, op0
);
693 /* Fills R with the largest finite value representable in mode MODE.
694 If SIGN is nonzero, R is set to the most negative finite value. */
697 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
707 max
= "9.999999999999999E384";
710 max
= "9.999999999999999999999999999999999E6144";
716 decimal_real_from_string (r
, max
);
718 decimal128SetSign ((decimal128
*) r
->sig
, 1);