In gcc/testsuite/: 2010-09-30 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / dfp.c
blobe2e90c4a4c320167c18a7f235c373b23a47c07e2
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software
3 Foundation, Inc.
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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "toplev.h"
27 #include "tm_p.h"
28 #include "dfp.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
41 #endif
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. */
47 static void
48 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
50 memset (r, 0, sizeof (REAL_VALUE_TYPE));
52 r->cl = rvc_normal;
53 if (decNumberIsNaN (dn))
54 r->cl = rvc_nan;
55 if (decNumberIsInfinite (dn))
56 r->cl = rvc_inf;
57 if (context->status & DEC_Overflow)
58 r->cl = rvc_inf;
59 if (decNumberIsNegative (dn))
60 r->sign = 1;
61 r->decimal = 1;
63 if (r->cl != rvc_normal)
64 return;
66 decContextDefault (context, DEC_INIT_DECIMAL128);
67 context->traps = 0;
69 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
72 /* Create decimal encoded R from string S. */
74 void
75 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
77 decNumber dn;
78 decContext set;
79 decContextDefault (&set, DEC_INIT_DECIMAL128);
80 set.traps = 0;
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. */
92 static void
93 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
95 decContext set;
96 decContextDefault (&set, DEC_INIT_DECIMAL128);
97 set.traps = 0;
99 switch (r->cl)
101 case rvc_zero:
102 decNumberZero (dn);
103 break;
104 case rvc_inf:
105 decNumberFromString (dn, "Infinity", &set);
106 break;
107 case rvc_nan:
108 if (r->signalling)
109 decNumberFromString (dn, "snan", &set);
110 else
111 decNumberFromString (dn, "nan", &set);
112 break;
113 case rvc_normal:
114 gcc_assert (r->decimal);
115 decimal128ToNumber ((const decimal128 *) r->sig, dn);
116 break;
117 default:
118 gcc_unreachable ();
121 /* Fix up sign bit. */
122 if (r->sign != decNumberIsNegative (dn))
123 dn->bits ^= DECNEG;
126 /* Encode a real into an IEEE 754 decimal32 type. */
128 void
129 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
130 long *buf, const REAL_VALUE_TYPE *r)
132 decNumber dn;
133 decimal32 d32;
134 decContext set;
135 int32_t image;
137 decContextDefault (&set, DEC_INIT_DECIMAL128);
138 set.traps = 0;
140 decimal_to_decnumber (r, &dn);
141 decimal32FromNumber (&d32, &dn, &set);
143 memcpy (&image, d32.bytes, sizeof (int32_t));
144 buf[0] = image;
147 /* Decode an IEEE 754 decimal32 type into a real. */
149 void
150 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
151 REAL_VALUE_TYPE *r, const long *buf)
153 decNumber dn;
154 decimal32 d32;
155 decContext set;
156 int32_t image;
158 decContextDefault (&set, DEC_INIT_DECIMAL128);
159 set.traps = 0;
161 image = buf[0];
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. */
170 void
171 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
172 long *buf, const REAL_VALUE_TYPE *r)
174 decNumber dn;
175 decimal64 d64;
176 decContext set;
177 int32_t image;
179 decContextDefault (&set, DEC_INIT_DECIMAL128);
180 set.traps = 0;
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));
188 buf[0] = image;
189 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
190 buf[1] = image;
192 else
194 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
195 buf[0] = image;
196 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
197 buf[1] = image;
201 /* Decode an IEEE 754 decimal64 type into a real. */
203 void
204 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
205 REAL_VALUE_TYPE *r, const long *buf)
207 decNumber dn;
208 decimal64 d64;
209 decContext set;
210 int32_t image;
212 decContextDefault (&set, DEC_INIT_DECIMAL128);
213 set.traps = 0;
215 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
217 image = buf[0];
218 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
219 image = buf[1];
220 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
222 else
224 image = buf[1];
225 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
226 image = buf[0];
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. */
236 void
237 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
238 long *buf, const REAL_VALUE_TYPE *r)
240 decNumber dn;
241 decContext set;
242 decimal128 d128;
243 int32_t image;
245 decContextDefault (&set, DEC_INIT_DECIMAL128);
246 set.traps = 0;
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));
254 buf[0] = image;
255 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
256 buf[1] = image;
257 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
258 buf[2] = image;
259 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
260 buf[3] = image;
262 else
264 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
265 buf[0] = image;
266 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
267 buf[1] = image;
268 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
269 buf[2] = image;
270 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
271 buf[3] = image;
275 /* Decode an IEEE 754 decimal128 type into a real. */
277 void
278 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
279 REAL_VALUE_TYPE *r, const long *buf)
281 decNumber dn;
282 decimal128 d128;
283 decContext set;
284 int32_t image;
286 decContextDefault (&set, DEC_INIT_DECIMAL128);
287 set.traps = 0;
289 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
291 image = buf[0];
292 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
293 image = buf[1];
294 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
295 image = buf[2];
296 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
297 image = buf[3];
298 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
300 else
302 image = buf[3];
303 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
304 image = buf[2];
305 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
306 image = buf[1];
307 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
308 image = buf[0];
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
317 representation. */
319 static void
320 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
321 enum machine_mode mode)
323 char string[256];
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
332 representation. */
334 static void
335 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
337 char string[256];
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,
350 int nan_result)
352 decContext set;
353 decNumber dn, dn2, dn3;
354 REAL_VALUE_TYPE a1, b1;
356 /* If either operand is non-decimal, create temporary versions. */
357 if (!a->decimal)
359 decimal_from_binary (&a1, a);
360 a = &a1;
362 if (!b->decimal)
364 decimal_from_binary (&b1, b);
365 b = &b1;
368 /* Convert into decNumber form for comparison operation. */
369 decContextDefault (&set, DEC_INIT_DECIMAL128);
370 set.traps = 0;
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))
379 return nan_result;
380 else if (decNumberIsZero (&dn))
381 return 0;
382 else if (decNumberIsNegative (&dn))
383 return -1;
384 else
385 return 1;
388 /* Helper to round_for_format, handling decimal float types. */
390 void
391 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
393 decNumber dn;
394 decContext set;
396 /* Real encoding occurs later. */
397 if (r->cl != rvc_normal)
398 return;
400 decContextDefault (&set, DEC_INIT_DECIMAL128);
401 set.traps = 0;
402 decimal128ToNumber ((decimal128 *) r->sig, &dn);
404 if (fmt == &decimal_quad_format)
406 /* The internal format is already in this format. */
407 return;
409 else if (fmt == &decimal_single_format)
411 decimal32 d32;
412 decContextDefault (&set, DEC_INIT_DECIMAL32);
413 set.traps = 0;
415 decimal32FromNumber (&d32, &dn, &set);
416 decimal32ToNumber (&d32, &dn);
418 else if (fmt == &decimal_double_format)
420 decimal64 d64;
421 decContextDefault (&set, DEC_INIT_DECIMAL64);
422 set.traps = 0;
424 decimal64FromNumber (&d64, &dn, &set);
425 decimal64ToNumber (&d64, &dn);
427 else
428 gcc_unreachable ();
430 decimal_from_decnumber (r, &dn, &set);
433 /* Extend or truncate to a new mode. Handles conversions between
434 binary and decimal types. */
436 void
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)
443 return;
444 if (a->decimal)
445 decimal_to_binary (r, a, mode);
446 else
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. */
456 void
457 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
458 size_t buf_size,
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);
470 static bool
471 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
472 const REAL_VALUE_TYPE *op1, int subtract_p)
474 decNumber dn;
475 decContext set;
476 decNumber dn2, dn3;
478 decimal_to_decnumber (op0, &dn2);
479 decimal_to_decnumber (op1, &dn3);
481 decContextDefault (&set, DEC_INIT_DECIMAL128);
482 set.traps = 0;
484 if (subtract_p)
485 decNumberSubtract (&dn, &dn2, &dn3, &set);
486 else
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. */
497 static bool
498 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
499 const REAL_VALUE_TYPE *op1)
501 decContext set;
502 decNumber dn, dn2, dn3;
504 decimal_to_decnumber (op0, &dn2);
505 decimal_to_decnumber (op1, &dn3);
507 decContextDefault (&set, DEC_INIT_DECIMAL128);
508 set.traps = 0;
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. */
519 static bool
520 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
521 const REAL_VALUE_TYPE *op1)
523 decContext set;
524 decNumber dn, dn2, dn3;
526 decimal_to_decnumber (op0, &dn2);
527 decimal_to_decnumber (op1, &dn3);
529 decContextDefault (&set, DEC_INIT_DECIMAL128);
530 set.traps = 0;
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
540 floating point). */
542 void
543 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
545 decNumber dn, dn2;
546 decContext set;
548 decContextDefault (&set, DEC_INIT_DECIMAL128);
549 set.traps = 0;
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. */
559 HOST_WIDE_INT
560 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
562 decContext set;
563 decNumber dn, dn2, dn3;
564 REAL_VALUE_TYPE to;
565 char string[256];
567 decContextDefault (&set, DEC_INIT_DECIMAL128);
568 set.traps = 0;
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
577 function. */
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. */
585 void
586 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
587 const REAL_VALUE_TYPE *r)
589 decContext set;
590 decNumber dn, dn2, dn3;
591 REAL_VALUE_TYPE to;
592 char string[256];
594 decContextDefault (&set, DEC_INIT_DECIMAL128);
595 set.traps = 0;
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
604 function. */
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. */
614 bool
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. */
622 if (!op0->decimal)
624 decimal_from_binary (&a, op0);
625 op0 = &a;
627 if (op1 && !op1->decimal)
629 decimal_from_binary (&b, op1);
630 op1 = &b;
633 switch (code)
635 case PLUS_EXPR:
636 return decimal_do_add (r, op0, op1, 0);
638 case MINUS_EXPR:
639 return decimal_do_add (r, op0, op1, 1);
641 case MULT_EXPR:
642 return decimal_do_multiply (r, op0, op1);
644 case RDIV_EXPR:
645 return decimal_do_divide (r, op0, op1);
647 case MIN_EXPR:
648 if (op1->cl == rvc_nan)
649 *r = *op1;
650 else if (real_compare (UNLT_EXPR, op0, op1))
651 *r = *op0;
652 else
653 *r = *op1;
654 return false;
656 case MAX_EXPR:
657 if (op1->cl == rvc_nan)
658 *r = *op1;
659 else if (real_compare (LT_EXPR, op0, op1))
660 *r = *op1;
661 else
662 *r = *op0;
663 return false;
665 case NEGATE_EXPR:
667 *r = *op0;
668 /* Flip sign bit. */
669 decimal128FlipSign ((decimal128 *) r->sig);
670 /* Keep sign field in sync. */
671 r->sign ^= 1;
673 return false;
675 case ABS_EXPR:
677 *r = *op0;
678 /* Clear sign bit. */
679 decimal128ClearSign ((decimal128 *) r->sig);
680 /* Keep sign field in sync. */
681 r->sign = 0;
683 return false;
685 case FIX_TRUNC_EXPR:
686 decimal_do_fix_trunc (r, op0);
687 return false;
689 default:
690 gcc_unreachable ();
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. */
697 void
698 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
700 const char *max;
702 switch (mode)
704 case SDmode:
705 max = "9.999999E96";
706 break;
707 case DDmode:
708 max = "9.999999999999999E384";
709 break;
710 case TDmode:
711 max = "9.999999999999999999999999999999999E6144";
712 break;
713 default:
714 gcc_unreachable ();
717 decimal_real_from_string (r, max);
718 if (sign)
719 decimal128SetSign ((decimal128 *) r->sig, 1);