Merged with mainline at revision 126347.
[official-gcc.git] / gcc / dfp.c
blobc15afa91c90f6d015d8a13aa71391de057c264e6
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007 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 2, or (at your option) any later
9 version.
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
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
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 "real.h"
28 #include "tm_p.h"
29 #include "dfp.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 "decimal64.h"
36 #include "decimal32.h"
37 #include "decNumber.h"
39 static uint32_t
40 dfp_byte_swap (uint32_t in)
42 uint32_t out = 0;
43 unsigned char *p = (unsigned char *) &out;
44 union {
45 uint32_t i;
46 unsigned char b[4];
47 } u;
49 u.i = in;
50 p[0] = u.b[3];
51 p[1] = u.b[2];
52 p[2] = u.b[1];
53 p[3] = u.b[0];
55 return out;
58 /* Initialize R (a real with the decimal flag set) from DN. Can
59 utilize status passed in via CONTEXT, if a previous operation had
60 interesting status. */
62 static void
63 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
65 memset (r, 0, sizeof (REAL_VALUE_TYPE));
67 r->cl = rvc_normal;
68 if (decNumberIsZero (dn))
69 r->cl = rvc_zero;
70 if (decNumberIsNaN (dn))
71 r->cl = rvc_nan;
72 if (decNumberIsInfinite (dn))
73 r->cl = rvc_inf;
74 if (context->status & DEC_Overflow)
75 r->cl = rvc_inf;
76 if (decNumberIsNegative (dn))
77 r->sign = 1;
78 r->decimal = 1;
80 if (r->cl != rvc_normal)
81 return;
83 decContextDefault (context, DEC_INIT_DECIMAL128);
84 context->traps = 0;
86 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
89 /* Create decimal encoded R from string S. */
91 void
92 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
94 decNumber dn;
95 decContext set;
96 decContextDefault (&set, DEC_INIT_DECIMAL128);
97 set.traps = 0;
99 decNumberFromString (&dn, (char *) s, &set);
101 /* It would be more efficient to store directly in decNumber format,
102 but that is impractical from current data structure size.
103 Encoding as a decimal128 is much more compact. */
104 decimal_from_decnumber (r, &dn, &set);
107 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
109 static void
110 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
112 decContext set;
113 decContextDefault (&set, DEC_INIT_DECIMAL128);
114 set.traps = 0;
116 switch (r->cl)
118 case rvc_zero:
119 decNumberZero (dn);
120 break;
121 case rvc_inf:
122 decNumberFromString (dn, (char *)"Infinity", &set);
123 break;
124 case rvc_nan:
125 if (r->signalling)
126 decNumberFromString (dn, (char *)"snan", &set);
127 else
128 decNumberFromString (dn, (char *)"nan", &set);
129 break;
130 case rvc_normal:
131 gcc_assert (r->decimal);
132 decimal128ToNumber ((decimal128 *) r->sig, dn);
133 break;
134 default:
135 gcc_unreachable ();
138 /* Fix up sign bit. */
139 if (r->sign != decNumberIsNegative (dn))
140 dn->bits ^= DECNEG;
143 /* Encode a real into an IEEE 754R decimal32 type. */
145 void
146 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
147 long *buf, const REAL_VALUE_TYPE *r)
149 decNumber dn;
150 decimal32 d32;
151 decContext set;
153 decContextDefault (&set, DEC_INIT_DECIMAL128);
154 set.traps = 0;
156 decimal_to_decnumber (r, &dn);
157 decimal32FromNumber (&d32, &dn, &set);
159 if (FLOAT_WORDS_BIG_ENDIAN)
160 buf[0] = *(uint32_t *) d32.bytes;
161 else
162 buf[0] = dfp_byte_swap (*(uint32_t *) d32.bytes);
165 /* Decode an IEEE 754R decimal32 type into a real. */
167 void
168 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
169 REAL_VALUE_TYPE *r, const long *buf)
171 decNumber dn;
172 decimal32 d32;
173 decContext set;
175 decContextDefault (&set, DEC_INIT_DECIMAL128);
176 set.traps = 0;
178 if (FLOAT_WORDS_BIG_ENDIAN)
179 *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
180 else
181 *((uint32_t *) d32.bytes) = dfp_byte_swap ((uint32_t) buf[0]);
183 decimal32ToNumber (&d32, &dn);
184 decimal_from_decnumber (r, &dn, &set);
187 /* Encode a real into an IEEE 754R decimal64 type. */
189 void
190 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
191 long *buf, const REAL_VALUE_TYPE *r)
193 decNumber dn;
194 decimal64 d64;
195 decContext set;
197 decContextDefault (&set, DEC_INIT_DECIMAL128);
198 set.traps = 0;
200 decimal_to_decnumber (r, &dn);
201 decimal64FromNumber (&d64, &dn, &set);
203 if (FLOAT_WORDS_BIG_ENDIAN)
205 buf[0] = *(uint32_t *) &d64.bytes[0];
206 buf[1] = *(uint32_t *) &d64.bytes[4];
208 else
210 buf[1] = dfp_byte_swap (*(uint32_t *) &d64.bytes[0]);
211 buf[0] = dfp_byte_swap (*(uint32_t *) &d64.bytes[4]);
215 /* Decode an IEEE 754R decimal64 type into a real. */
217 void
218 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
219 REAL_VALUE_TYPE *r, const long *buf)
221 decNumber dn;
222 decimal64 d64;
223 decContext set;
225 decContextDefault (&set, DEC_INIT_DECIMAL128);
226 set.traps = 0;
228 if (FLOAT_WORDS_BIG_ENDIAN)
230 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
231 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
233 else
235 *((uint32_t *) &d64.bytes[0]) = dfp_byte_swap ((uint32_t) buf[1]);
236 *((uint32_t *) &d64.bytes[4]) = dfp_byte_swap ((uint32_t) buf[0]);
239 decimal64ToNumber (&d64, &dn);
240 decimal_from_decnumber (r, &dn, &set);
243 /* Encode a real into an IEEE 754R decimal128 type. */
245 void
246 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
247 long *buf, const REAL_VALUE_TYPE *r)
249 decNumber dn;
250 decContext set;
251 decimal128 d128;
253 decContextDefault (&set, DEC_INIT_DECIMAL128);
254 set.traps = 0;
256 decimal_to_decnumber (r, &dn);
257 decimal128FromNumber (&d128, &dn, &set);
259 if (FLOAT_WORDS_BIG_ENDIAN)
261 buf[0] = *(uint32_t *) &d128.bytes[0];
262 buf[1] = *(uint32_t *) &d128.bytes[4];
263 buf[2] = *(uint32_t *) &d128.bytes[8];
264 buf[3] = *(uint32_t *) &d128.bytes[12];
266 else
268 buf[0] = dfp_byte_swap (*(uint32_t *) &d128.bytes[12]);
269 buf[1] = dfp_byte_swap (*(uint32_t *) &d128.bytes[8]);
270 buf[2] = dfp_byte_swap (*(uint32_t *) &d128.bytes[4]);
271 buf[3] = dfp_byte_swap (*(uint32_t *) &d128.bytes[0]);
275 /* Decode an IEEE 754R 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;
285 decContextDefault (&set, DEC_INIT_DECIMAL128);
286 set.traps = 0;
288 if (FLOAT_WORDS_BIG_ENDIAN)
290 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
291 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
292 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
293 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
295 else
297 *((uint32_t *) &d128.bytes[0]) = dfp_byte_swap ((uint32_t) buf[3]);
298 *((uint32_t *) &d128.bytes[4]) = dfp_byte_swap ((uint32_t) buf[2]);
299 *((uint32_t *) &d128.bytes[8]) = dfp_byte_swap ((uint32_t) buf[1]);
300 *((uint32_t *) &d128.bytes[12]) = dfp_byte_swap ((uint32_t) buf[0]);
303 decimal128ToNumber (&d128, &dn);
304 decimal_from_decnumber (r, &dn, &set);
307 /* Helper function to convert from a binary real internal
308 representation. */
310 static void
311 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
312 enum machine_mode mode)
314 char string[256];
315 decimal128 *d128;
316 d128 = (decimal128 *) from->sig;
318 decimal128ToString (d128, string);
319 real_from_string3 (to, string, mode);
323 /* Helper function to convert from a binary real internal
324 representation. */
326 static void
327 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
329 char string[256];
331 /* We convert to string, then to decNumber then to decimal128. */
332 real_to_decimal (string, from, sizeof (string), 0, 1);
333 decimal_real_from_string (to, string);
336 /* Helper function to real.c:do_compare() to handle decimal internal
337 representation including when one of the operands is still in the
338 binary internal representation. */
341 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
342 int nan_result)
344 decContext set;
345 decNumber dn, dn2, dn3;
346 REAL_VALUE_TYPE a1, b1;
348 /* If either operand is non-decimal, create temporary versions. */
349 if (!a->decimal)
351 decimal_from_binary (&a1, a);
352 a = &a1;
354 if (!b->decimal)
356 decimal_from_binary (&b1, b);
357 b = &b1;
360 /* Convert into decNumber form for comparison operation. */
361 decContextDefault (&set, DEC_INIT_DECIMAL128);
362 set.traps = 0;
363 decimal128ToNumber ((decimal128 *) a->sig, &dn2);
364 decimal128ToNumber ((decimal128 *) b->sig, &dn3);
366 /* Finally, do the comparison. */
367 decNumberCompare (&dn, &dn2, &dn3, &set);
369 /* Return the comparison result. */
370 if (decNumberIsNaN (&dn))
371 return nan_result;
372 else if (decNumberIsZero (&dn))
373 return 0;
374 else if (decNumberIsNegative (&dn))
375 return -1;
376 else
377 return 1;
380 /* Helper to round_for_format, handling decimal float types. */
382 void
383 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
385 decNumber dn;
386 decContext set;
388 /* Real encoding occurs later. */
389 if (r->cl != rvc_normal)
390 return;
392 decContextDefault (&set, DEC_INIT_DECIMAL128);
393 set.traps = 0;
394 decimal128ToNumber ((decimal128 *) r->sig, &dn);
396 if (fmt == &decimal_quad_format)
398 /* The internal format is already in this format. */
399 return;
401 else if (fmt == &decimal_single_format)
403 decimal32 d32;
404 decContextDefault (&set, DEC_INIT_DECIMAL32);
405 set.traps = 0;
407 decimal32FromNumber (&d32, &dn, &set);
408 decimal32ToNumber (&d32, &dn);
410 else if (fmt == &decimal_double_format)
412 decimal64 d64;
413 decContextDefault (&set, DEC_INIT_DECIMAL64);
414 set.traps = 0;
416 decimal64FromNumber (&d64, &dn, &set);
417 decimal64ToNumber (&d64, &dn);
419 else
420 gcc_unreachable ();
422 decimal_from_decnumber (r, &dn, &set);
425 /* Extend or truncate to a new mode. Handles conversions between
426 binary and decimal types. */
428 void
429 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
430 const REAL_VALUE_TYPE *a)
432 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
434 if (a->decimal && fmt->b == 10)
435 return;
436 if (a->decimal)
437 decimal_to_binary (r, a, mode);
438 else
439 decimal_from_binary (r, a);
442 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
443 significant digits in the result, bounded by BUF_SIZE. If DIGITS
444 is 0, choose the maximum for the representation. If
445 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
446 DIGITS or CROP_TRAILING_ZEROS. */
448 void
449 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
450 size_t buf_size,
451 size_t digits ATTRIBUTE_UNUSED,
452 int crop_trailing_zeros ATTRIBUTE_UNUSED)
454 decimal128 *d128 = (decimal128*) r_orig->sig;
456 /* decimal128ToString requires space for at least 24 characters;
457 Require two more for suffix. */
458 gcc_assert (buf_size >= 24);
459 decimal128ToString (d128, str);
462 static bool
463 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
464 const REAL_VALUE_TYPE *op1, int subtract_p)
466 decNumber dn;
467 decContext set;
468 decNumber dn2, dn3;
470 decimal_to_decnumber (op0, &dn2);
471 decimal_to_decnumber (op1, &dn3);
473 decContextDefault (&set, DEC_INIT_DECIMAL128);
474 set.traps = 0;
476 if (subtract_p)
477 decNumberSubtract (&dn, &dn2, &dn3, &set);
478 else
479 decNumberAdd (&dn, &dn2, &dn3, &set);
481 decimal_from_decnumber (r, &dn, &set);
483 /* Return true, if inexact. */
484 return (set.status & DEC_Inexact);
487 /* Compute R = OP0 * OP1. */
489 static bool
490 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
491 const REAL_VALUE_TYPE *op1)
493 decContext set;
494 decNumber dn, dn2, dn3;
496 decimal_to_decnumber (op0, &dn2);
497 decimal_to_decnumber (op1, &dn3);
499 decContextDefault (&set, DEC_INIT_DECIMAL128);
500 set.traps = 0;
502 decNumberMultiply (&dn, &dn2, &dn3, &set);
503 decimal_from_decnumber (r, &dn, &set);
505 /* Return true, if inexact. */
506 return (set.status & DEC_Inexact);
509 /* Compute R = OP0 / OP1. */
511 static bool
512 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
513 const REAL_VALUE_TYPE *op1)
515 decContext set;
516 decNumber dn, dn2, dn3;
518 decimal_to_decnumber (op0, &dn2);
519 decimal_to_decnumber (op1, &dn3);
521 decContextDefault (&set, DEC_INIT_DECIMAL128);
522 set.traps = 0;
524 decNumberDivide (&dn, &dn2, &dn3, &set);
525 decimal_from_decnumber (r, &dn, &set);
527 /* Return true, if inexact. */
528 return (set.status & DEC_Inexact);
531 /* Set R to A truncated to an integral value toward zero (decimal
532 floating point). */
534 void
535 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
537 decNumber dn, dn2;
538 decContext set;
540 decContextDefault (&set, DEC_INIT_DECIMAL128);
541 set.traps = 0;
542 set.round = DEC_ROUND_DOWN;
543 decimal128ToNumber ((decimal128 *) a->sig, &dn2);
545 decNumberToIntegralValue (&dn, &dn2, &set);
546 decimal_from_decnumber (r, &dn, &set);
549 /* Render decimal float value R as an integer. */
551 HOST_WIDE_INT
552 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
554 decContext set;
555 decNumber dn, dn2, dn3;
556 REAL_VALUE_TYPE to;
557 char string[256];
559 decContextDefault (&set, DEC_INIT_DECIMAL128);
560 set.traps = 0;
561 set.round = DEC_ROUND_DOWN;
562 decimal128ToNumber ((decimal128 *) r->sig, &dn);
564 decNumberToIntegralValue (&dn2, &dn, &set);
565 decNumberZero (&dn3);
566 decNumberRescale (&dn, &dn2, &dn3, &set);
568 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
569 function. */
570 decNumberToString (&dn, string);
571 real_from_string (&to, string);
572 return real_to_integer (&to);
575 /* Likewise, but to an integer pair, HI+LOW. */
577 void
578 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
579 const REAL_VALUE_TYPE *r)
581 decContext set;
582 decNumber dn, dn2, dn3;
583 REAL_VALUE_TYPE to;
584 char string[256];
586 decContextDefault (&set, DEC_INIT_DECIMAL128);
587 set.traps = 0;
588 set.round = DEC_ROUND_DOWN;
589 decimal128ToNumber ((decimal128 *) r->sig, &dn);
591 decNumberToIntegralValue (&dn2, &dn, &set);
592 decNumberZero (&dn3);
593 decNumberRescale (&dn, &dn2, &dn3, &set);
595 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
596 function. */
597 decNumberToString (&dn, string);
598 real_from_string (&to, string);
599 real_to_integer2 (plow, phigh, &to);
602 /* Perform the decimal floating point operation described by CODE.
603 For a unary operation, OP1 will be NULL. This function returns
604 true if the result may be inexact due to loss of precision. */
606 bool
607 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
608 const REAL_VALUE_TYPE *op0,
609 const REAL_VALUE_TYPE *op1)
611 REAL_VALUE_TYPE a, b;
613 /* If either operand is non-decimal, create temporaries. */
614 if (!op0->decimal)
616 decimal_from_binary (&a, op0);
617 op0 = &a;
619 if (op1 && !op1->decimal)
621 decimal_from_binary (&b, op1);
622 op1 = &b;
625 switch (code)
627 case PLUS_EXPR:
628 return decimal_do_add (r, op0, op1, 0);
630 case MINUS_EXPR:
631 return decimal_do_add (r, op0, op1, 1);
633 case MULT_EXPR:
634 return decimal_do_multiply (r, op0, op1);
636 case RDIV_EXPR:
637 return decimal_do_divide (r, op0, op1);
639 case MIN_EXPR:
640 if (op1->cl == rvc_nan)
641 *r = *op1;
642 else if (real_compare (UNLT_EXPR, op0, op1))
643 *r = *op0;
644 else
645 *r = *op1;
646 return false;
648 case MAX_EXPR:
649 if (op1->cl == rvc_nan)
650 *r = *op1;
651 else if (real_compare (LT_EXPR, op0, op1))
652 *r = *op1;
653 else
654 *r = *op0;
655 return false;
657 case NEGATE_EXPR:
659 *r = *op0;
660 /* Flip sign bit. */
661 decimal128FlipSign ((decimal128 *) r->sig);
662 /* Keep sign field in sync. */
663 r->sign ^= 1;
665 return false;
667 case ABS_EXPR:
669 *r = *op0;
670 /* Clear sign bit. */
671 decimal128ClearSign ((decimal128 *) r->sig);
672 /* Keep sign field in sync. */
673 r->sign = 0;
675 return false;
677 case FIX_TRUNC_EXPR:
678 decimal_do_fix_trunc (r, op0);
679 return false;
681 default:
682 gcc_unreachable ();
686 /* Fills R with the largest finite value representable in mode MODE.
687 If SIGN is nonzero, R is set to the most negative finite value. */
689 void
690 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
692 char *max;
694 switch (mode)
696 case SDmode:
697 max = (char *) "9.999999E96";
698 break;
699 case DDmode:
700 max = (char *) "9.999999999999999E384";
701 break;
702 case TDmode:
703 max = (char *) "9.999999999999999999999999999999999E6144";
704 break;
705 default:
706 gcc_unreachable ();
709 decimal_real_from_string (r, max);
710 if (sign)
711 decimal128SetSign ((decimal128 *) r->sig, 1);