1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 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"
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);
115 gcc_assert (r->decimal);
116 decimal128ToNumber ((const decimal128 *) r->sig, dn);
122 /* Fix up sign bit. */
123 if (r->sign != decNumberIsNegative (dn))
127 /* Encode a real into an IEEE 754 decimal32 type. */
130 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
131 long *buf, const REAL_VALUE_TYPE *r)
138 decContextDefault (&set, DEC_INIT_DECIMAL128);
141 decimal_to_decnumber (r, &dn);
142 decimal32FromNumber (&d32, &dn, &set);
144 memcpy (&image, d32.bytes, sizeof (int32_t));
148 /* Decode an IEEE 754 decimal32 type into a real. */
151 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
152 REAL_VALUE_TYPE *r, const long *buf)
159 decContextDefault (&set, DEC_INIT_DECIMAL128);
163 memcpy (&d32.bytes, &image, sizeof (int32_t));
165 decimal32ToNumber (&d32, &dn);
166 decimal_from_decnumber (r, &dn, &set);
169 /* Encode a real into an IEEE 754 decimal64 type. */
172 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
173 long *buf, const REAL_VALUE_TYPE *r)
180 decContextDefault (&set, DEC_INIT_DECIMAL128);
183 decimal_to_decnumber (r, &dn);
184 decimal64FromNumber (&d64, &dn, &set);
186 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
188 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
190 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
195 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
197 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
202 /* Decode an IEEE 754 decimal64 type into a real. */
205 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
206 REAL_VALUE_TYPE *r, const long *buf)
213 decContextDefault (&set, DEC_INIT_DECIMAL128);
216 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
219 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
221 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
226 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
228 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
231 decimal64ToNumber (&d64, &dn);
232 decimal_from_decnumber (r, &dn, &set);
235 /* Encode a real into an IEEE 754 decimal128 type. */
238 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
239 long *buf, const REAL_VALUE_TYPE *r)
246 decContextDefault (&set, DEC_INIT_DECIMAL128);
249 decimal_to_decnumber (r, &dn);
250 decimal128FromNumber (&d128, &dn, &set);
252 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
254 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
256 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
258 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
260 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
265 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
267 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
269 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
271 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
276 /* Decode an IEEE 754 decimal128 type into a real. */
279 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
280 REAL_VALUE_TYPE *r, const long *buf)
287 decContextDefault (&set, DEC_INIT_DECIMAL128);
290 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
293 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
295 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
297 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
299 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
304 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
306 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
308 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
310 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
313 decimal128ToNumber (&d128, &dn);
314 decimal_from_decnumber (r, &dn, &set);
317 /* Helper function to convert from a binary real internal
321 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
322 enum machine_mode mode)
325 const decimal128 *const d128 = (const decimal128 *) from->sig;
327 decimal128ToString (d128, string);
328 real_from_string3 (to, string, mode);
332 /* Helper function to convert from a binary real internal
336 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
340 /* We convert to string, then to decNumber then to decimal128. */
341 real_to_decimal (string, from, sizeof (string), 0, 1);
342 decimal_real_from_string (to, string);
345 /* Helper function to real.c:do_compare() to handle decimal internal
346 representation including when one of the operands is still in the
347 binary internal representation. */
350 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
354 decNumber dn, dn2, dn3;
355 REAL_VALUE_TYPE a1, b1;
357 /* If either operand is non-decimal, create temporary versions. */
360 decimal_from_binary (&a1, a);
365 decimal_from_binary (&b1, b);
369 /* Convert into decNumber form for comparison operation. */
370 decContextDefault (&set, DEC_INIT_DECIMAL128);
372 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
373 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
375 /* Finally, do the comparison. */
376 decNumberCompare (&dn, &dn2, &dn3, &set);
378 /* Return the comparison result. */
379 if (decNumberIsNaN (&dn))
381 else if (decNumberIsZero (&dn))
383 else if (decNumberIsNegative (&dn))
389 /* Helper to round_for_format, handling decimal float types. */
392 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
397 /* Real encoding occurs later. */
398 if (r->cl != rvc_normal)
401 decContextDefault (&set, DEC_INIT_DECIMAL128);
403 decimal128ToNumber ((decimal128 *) r->sig, &dn);
405 if (fmt == &decimal_quad_format)
407 /* The internal format is already in this format. */
410 else if (fmt == &decimal_single_format)
413 decContextDefault (&set, DEC_INIT_DECIMAL32);
416 decimal32FromNumber (&d32, &dn, &set);
417 decimal32ToNumber (&d32, &dn);
419 else if (fmt == &decimal_double_format)
422 decContextDefault (&set, DEC_INIT_DECIMAL64);
425 decimal64FromNumber (&d64, &dn, &set);
426 decimal64ToNumber (&d64, &dn);
431 decimal_from_decnumber (r, &dn, &set);
434 /* Extend or truncate to a new mode. Handles conversions between
435 binary and decimal types. */
438 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
439 const REAL_VALUE_TYPE *a)
441 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
443 if (a->decimal && fmt->b == 10)
446 decimal_to_binary (r, a, mode);
448 decimal_from_binary (r, a);
451 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
452 significant digits in the result, bounded by BUF_SIZE. If DIGITS
453 is 0, choose the maximum for the representation. If
454 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
455 DIGITS or CROP_TRAILING_ZEROS. */
458 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
460 size_t digits ATTRIBUTE_UNUSED,
461 int crop_trailing_zeros ATTRIBUTE_UNUSED)
463 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
465 /* decimal128ToString requires space for at least 24 characters;
466 Require two more for suffix. */
467 gcc_assert (buf_size >= 24);
468 decimal128ToString (d128, str);
472 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
473 const REAL_VALUE_TYPE *op1, int subtract_p)
479 decimal_to_decnumber (op0, &dn2);
480 decimal_to_decnumber (op1, &dn3);
482 decContextDefault (&set, DEC_INIT_DECIMAL128);
486 decNumberSubtract (&dn, &dn2, &dn3, &set);
488 decNumberAdd (&dn, &dn2, &dn3, &set);
490 decimal_from_decnumber (r, &dn, &set);
492 /* Return true, if inexact. */
493 return (set.status & DEC_Inexact);
496 /* Compute R = OP0 * OP1. */
499 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
500 const REAL_VALUE_TYPE *op1)
503 decNumber dn, dn2, dn3;
505 decimal_to_decnumber (op0, &dn2);
506 decimal_to_decnumber (op1, &dn3);
508 decContextDefault (&set, DEC_INIT_DECIMAL128);
511 decNumberMultiply (&dn, &dn2, &dn3, &set);
512 decimal_from_decnumber (r, &dn, &set);
514 /* Return true, if inexact. */
515 return (set.status & DEC_Inexact);
518 /* Compute R = OP0 / OP1. */
521 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
522 const REAL_VALUE_TYPE *op1)
525 decNumber dn, dn2, dn3;
527 decimal_to_decnumber (op0, &dn2);
528 decimal_to_decnumber (op1, &dn3);
530 decContextDefault (&set, DEC_INIT_DECIMAL128);
533 decNumberDivide (&dn, &dn2, &dn3, &set);
534 decimal_from_decnumber (r, &dn, &set);
536 /* Return true, if inexact. */
537 return (set.status & DEC_Inexact);
540 /* Set R to A truncated to an integral value toward zero (decimal
544 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
549 decContextDefault (&set, DEC_INIT_DECIMAL128);
551 set.round = DEC_ROUND_DOWN;
552 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
554 decNumberToIntegralValue (&dn, &dn2, &set);
555 decimal_from_decnumber (r, &dn, &set);
558 /* Render decimal float value R as an integer. */
561 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
564 decNumber dn, dn2, dn3;
568 decContextDefault (&set, DEC_INIT_DECIMAL128);
570 set.round = DEC_ROUND_DOWN;
571 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
573 decNumberToIntegralValue (&dn2, &dn, &set);
574 decNumberZero (&dn3);
575 decNumberRescale (&dn, &dn2, &dn3, &set);
577 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
579 decNumberToString (&dn, string);
580 real_from_string (&to, string);
581 return real_to_integer (&to);
584 /* Likewise, but to an integer pair, HI+LOW. */
587 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
588 const REAL_VALUE_TYPE *r)
591 decNumber dn, dn2, dn3;
595 decContextDefault (&set, DEC_INIT_DECIMAL128);
597 set.round = DEC_ROUND_DOWN;
598 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
600 decNumberToIntegralValue (&dn2, &dn, &set);
601 decNumberZero (&dn3);
602 decNumberRescale (&dn, &dn2, &dn3, &set);
604 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
606 decNumberToString (&dn, string);
607 real_from_string (&to, string);
608 real_to_integer2 (plow, phigh, &to);
611 /* Perform the decimal floating point operation described by CODE.
612 For a unary operation, OP1 will be NULL. This function returns
613 true if the result may be inexact due to loss of precision. */
616 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
617 const REAL_VALUE_TYPE *op0,
618 const REAL_VALUE_TYPE *op1)
620 REAL_VALUE_TYPE a, b;
622 /* If either operand is non-decimal, create temporaries. */
625 decimal_from_binary (&a, op0);
628 if (op1 && !op1->decimal)
630 decimal_from_binary (&b, op1);
637 return decimal_do_add (r, op0, op1, 0);
640 return decimal_do_add (r, op0, op1, 1);
643 return decimal_do_multiply (r, op0, op1);
646 return decimal_do_divide (r, op0, op1);
649 if (op1->cl == rvc_nan)
651 else if (real_compare (UNLT_EXPR, op0, op1))
658 if (op1->cl == rvc_nan)
660 else if (real_compare (LT_EXPR, op0, op1))
670 decimal128FlipSign ((decimal128 *) r->sig);
671 /* Keep sign field in sync. */
679 /* Clear sign bit. */
680 decimal128ClearSign ((decimal128 *) r->sig);
681 /* Keep sign field in sync. */
687 decimal_do_fix_trunc (r, op0);
695 /* Fills R with the largest finite value representable in mode MODE.
696 If SIGN is nonzero, R is set to the most negative finite value. */
699 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
709 max = "9.999999999999999E384";
712 max = "9.999999999999999999999999999999999E6144";
718 decimal_real_from_string (r, max);
720 decimal128SetSign ((decimal128 *) r->sig, 1);