1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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);
114 gcc_assert (r->decimal);
115 decimal128ToNumber ((const decimal128 *) r->sig, dn);
121 /* Fix up sign bit. */
122 if (r->sign != decNumberIsNegative (dn))
126 /* Encode a real into an IEEE 754 decimal32 type. */
129 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
130 long *buf, const REAL_VALUE_TYPE *r)
137 decContextDefault (&set, DEC_INIT_DECIMAL128);
140 decimal_to_decnumber (r, &dn);
141 decimal32FromNumber (&d32, &dn, &set);
143 memcpy (&image, d32.bytes, sizeof (int32_t));
147 /* Decode an IEEE 754 decimal32 type into a real. */
150 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
151 REAL_VALUE_TYPE *r, const long *buf)
158 decContextDefault (&set, DEC_INIT_DECIMAL128);
162 memcpy (&d32.bytes, &image, sizeof (int32_t));
164 decimal32ToNumber (&d32, &dn);
165 decimal_from_decnumber (r, &dn, &set);
168 /* Encode a real into an IEEE 754 decimal64 type. */
171 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
172 long *buf, const REAL_VALUE_TYPE *r)
179 decContextDefault (&set, DEC_INIT_DECIMAL128);
182 decimal_to_decnumber (r, &dn);
183 decimal64FromNumber (&d64, &dn, &set);
185 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
187 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
189 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
194 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
196 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
201 /* Decode an IEEE 754 decimal64 type into a real. */
204 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
205 REAL_VALUE_TYPE *r, const long *buf)
212 decContextDefault (&set, DEC_INIT_DECIMAL128);
215 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
218 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
220 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
225 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
227 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
230 decimal64ToNumber (&d64, &dn);
231 decimal_from_decnumber (r, &dn, &set);
234 /* Encode a real into an IEEE 754 decimal128 type. */
237 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
238 long *buf, const REAL_VALUE_TYPE *r)
245 decContextDefault (&set, DEC_INIT_DECIMAL128);
248 decimal_to_decnumber (r, &dn);
249 decimal128FromNumber (&d128, &dn, &set);
251 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
253 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
255 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
257 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
259 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
264 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
266 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
268 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
270 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
275 /* Decode an IEEE 754 decimal128 type into a real. */
278 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
279 REAL_VALUE_TYPE *r, const long *buf)
286 decContextDefault (&set, DEC_INIT_DECIMAL128);
289 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
292 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
294 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
296 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
298 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
303 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
305 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
307 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
309 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
312 decimal128ToNumber (&d128, &dn);
313 decimal_from_decnumber (r, &dn, &set);
316 /* Helper function to convert from a binary real internal
320 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
321 enum machine_mode mode)
324 const decimal128 *const d128 = (const decimal128 *) from->sig;
326 decimal128ToString (d128, string);
327 real_from_string3 (to, string, mode);
331 /* Helper function to convert from a binary real internal
335 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
339 /* We convert to string, then to decNumber then to decimal128. */
340 real_to_decimal (string, from, sizeof (string), 0, 1);
341 decimal_real_from_string (to, string);
344 /* Helper function to real.c:do_compare() to handle decimal internal
345 representation including when one of the operands is still in the
346 binary internal representation. */
349 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
353 decNumber dn, dn2, dn3;
354 REAL_VALUE_TYPE a1, b1;
356 /* If either operand is non-decimal, create temporary versions. */
359 decimal_from_binary (&a1, a);
364 decimal_from_binary (&b1, b);
368 /* Convert into decNumber form for comparison operation. */
369 decContextDefault (&set, DEC_INIT_DECIMAL128);
371 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
372 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
374 /* Finally, do the comparison. */
375 decNumberCompare (&dn, &dn2, &dn3, &set);
377 /* Return the comparison result. */
378 if (decNumberIsNaN (&dn))
380 else if (decNumberIsZero (&dn))
382 else if (decNumberIsNegative (&dn))
388 /* Helper to round_for_format, handling decimal float types. */
391 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
396 /* Real encoding occurs later. */
397 if (r->cl != rvc_normal)
400 decContextDefault (&set, DEC_INIT_DECIMAL128);
402 decimal128ToNumber ((decimal128 *) r->sig, &dn);
404 if (fmt == &decimal_quad_format)
406 /* The internal format is already in this format. */
409 else if (fmt == &decimal_single_format)
412 decContextDefault (&set, DEC_INIT_DECIMAL32);
415 decimal32FromNumber (&d32, &dn, &set);
416 decimal32ToNumber (&d32, &dn);
418 else if (fmt == &decimal_double_format)
421 decContextDefault (&set, DEC_INIT_DECIMAL64);
424 decimal64FromNumber (&d64, &dn, &set);
425 decimal64ToNumber (&d64, &dn);
430 decimal_from_decnumber (r, &dn, &set);
433 /* Extend or truncate to a new mode. Handles conversions between
434 binary and decimal types. */
437 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
438 const REAL_VALUE_TYPE *a)
440 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
442 if (a->decimal && fmt->b == 10)
445 decimal_to_binary (r, a, mode);
447 decimal_from_binary (r, a);
450 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
451 significant digits in the result, bounded by BUF_SIZE. If DIGITS
452 is 0, choose the maximum for the representation. If
453 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
454 DIGITS or CROP_TRAILING_ZEROS. */
457 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
459 size_t digits ATTRIBUTE_UNUSED,
460 int crop_trailing_zeros ATTRIBUTE_UNUSED)
462 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
464 /* decimal128ToString requires space for at least 24 characters;
465 Require two more for suffix. */
466 gcc_assert (buf_size >= 24);
467 decimal128ToString (d128, str);
471 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
472 const REAL_VALUE_TYPE *op1, int subtract_p)
478 decimal_to_decnumber (op0, &dn2);
479 decimal_to_decnumber (op1, &dn3);
481 decContextDefault (&set, DEC_INIT_DECIMAL128);
485 decNumberSubtract (&dn, &dn2, &dn3, &set);
487 decNumberAdd (&dn, &dn2, &dn3, &set);
489 decimal_from_decnumber (r, &dn, &set);
491 /* Return true, if inexact. */
492 return (set.status & DEC_Inexact);
495 /* Compute R = OP0 * OP1. */
498 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
499 const REAL_VALUE_TYPE *op1)
502 decNumber dn, dn2, dn3;
504 decimal_to_decnumber (op0, &dn2);
505 decimal_to_decnumber (op1, &dn3);
507 decContextDefault (&set, DEC_INIT_DECIMAL128);
510 decNumberMultiply (&dn, &dn2, &dn3, &set);
511 decimal_from_decnumber (r, &dn, &set);
513 /* Return true, if inexact. */
514 return (set.status & DEC_Inexact);
517 /* Compute R = OP0 / OP1. */
520 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
521 const REAL_VALUE_TYPE *op1)
524 decNumber dn, dn2, dn3;
526 decimal_to_decnumber (op0, &dn2);
527 decimal_to_decnumber (op1, &dn3);
529 decContextDefault (&set, DEC_INIT_DECIMAL128);
532 decNumberDivide (&dn, &dn2, &dn3, &set);
533 decimal_from_decnumber (r, &dn, &set);
535 /* Return true, if inexact. */
536 return (set.status & DEC_Inexact);
539 /* Set R to A truncated to an integral value toward zero (decimal
543 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
548 decContextDefault (&set, DEC_INIT_DECIMAL128);
550 set.round = DEC_ROUND_DOWN;
551 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
553 decNumberToIntegralValue (&dn, &dn2, &set);
554 decimal_from_decnumber (r, &dn, &set);
557 /* Render decimal float value R as an integer. */
560 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
563 decNumber dn, dn2, dn3;
567 decContextDefault (&set, DEC_INIT_DECIMAL128);
569 set.round = DEC_ROUND_DOWN;
570 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
572 decNumberToIntegralValue (&dn2, &dn, &set);
573 decNumberZero (&dn3);
574 decNumberRescale (&dn, &dn2, &dn3, &set);
576 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
578 decNumberToString (&dn, string);
579 real_from_string (&to, string);
580 return real_to_integer (&to);
583 /* Likewise, but to an integer pair, HI+LOW. */
586 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
587 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 real_to_integer2 (plow, phigh, &to);
610 /* Perform the decimal floating point operation described by CODE.
611 For a unary operation, OP1 will be NULL. This function returns
612 true if the result may be inexact due to loss of precision. */
615 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
616 const REAL_VALUE_TYPE *op0,
617 const REAL_VALUE_TYPE *op1)
619 REAL_VALUE_TYPE a, b;
621 /* If either operand is non-decimal, create temporaries. */
624 decimal_from_binary (&a, op0);
627 if (op1 && !op1->decimal)
629 decimal_from_binary (&b, op1);
636 return decimal_do_add (r, op0, op1, 0);
639 return decimal_do_add (r, op0, op1, 1);
642 return decimal_do_multiply (r, op0, op1);
645 return decimal_do_divide (r, op0, op1);
648 if (op1->cl == rvc_nan)
650 else if (real_compare (UNLT_EXPR, op0, op1))
657 if (op1->cl == rvc_nan)
659 else if (real_compare (LT_EXPR, op0, op1))
669 decimal128FlipSign ((decimal128 *) r->sig);
670 /* Keep sign field in sync. */
678 /* Clear sign bit. */
679 decimal128ClearSign ((decimal128 *) r->sig);
680 /* Keep sign field in sync. */
686 decimal_do_fix_trunc (r, op0);
694 /* Fills R with the largest finite value representable in mode MODE.
695 If SIGN is nonzero, R is set to the most negative finite value. */
698 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
708 max = "9.999999999999999E384";
711 max = "9.999999999999999999999999999999999E6144";
717 decimal_real_from_string (r, max);
719 decimal128SetSign ((decimal128 *) r->sig, 1);