2006-03-25 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / dfp.c
blob5072264cb509053578b25ce74383eb0180f9ed9a
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006 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 decNumberNegate (dn);
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 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
168 REAL_VALUE_TYPE *r, const long *buf)
170 decNumber dn;
171 decimal32 d32;
172 decContext set;
174 decContextDefault (&set, DEC_INIT_DECIMAL128);
175 set.traps = 0;
177 if (FLOAT_WORDS_BIG_ENDIAN)
178 *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
179 else
180 *((uint32_t *) d32.bytes) = dfp_byte_swap ((uint32_t) buf[0]);
182 decimal32ToNumber (&d32, &dn);
183 decimal_from_decnumber (r, &dn, &set);
186 /* Encode a real into an IEEE 754R decimal64 type. */
188 void
189 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
190 long *buf, const REAL_VALUE_TYPE *r)
192 decNumber dn;
193 decimal64 d64;
194 decContext set;
196 decContextDefault (&set, DEC_INIT_DECIMAL128);
197 set.traps = 0;
199 decimal_to_decnumber (r, &dn);
200 decimal64FromNumber (&d64, &dn, &set);
202 if (FLOAT_WORDS_BIG_ENDIAN)
204 buf[0] = *(uint32_t *) &d64.bytes[0];
205 buf[1] = *(uint32_t *) &d64.bytes[4];
207 else
209 buf[1] = dfp_byte_swap (*(uint32_t *) &d64.bytes[0]);
210 buf[0] = dfp_byte_swap (*(uint32_t *) &d64.bytes[4]);
214 /* Decode an IEEE 754R decimal64 type into a real. */
216 void
217 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
218 REAL_VALUE_TYPE *r, const long *buf)
220 decNumber dn;
221 decimal64 d64;
222 decContext set;
224 decContextDefault (&set, DEC_INIT_DECIMAL128);
225 set.traps = 0;
227 if (FLOAT_WORDS_BIG_ENDIAN)
229 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
230 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
232 else
234 *((uint32_t *) &d64.bytes[0]) = dfp_byte_swap ((uint32_t) buf[1]);
235 *((uint32_t *) &d64.bytes[4]) = dfp_byte_swap ((uint32_t) buf[0]);
238 decimal64ToNumber (&d64, &dn);
239 decimal_from_decnumber (r, &dn, &set);
242 /* Encode a real into an IEEE 754R decimal128 type. */
244 void
245 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
246 long *buf, const REAL_VALUE_TYPE *r)
248 decNumber dn;
249 decContext set;
250 decimal128 d128;
252 decContextDefault (&set, DEC_INIT_DECIMAL128);
253 set.traps = 0;
255 decimal_to_decnumber (r, &dn);
256 decimal128FromNumber (&d128, &dn, &set);
258 if (FLOAT_WORDS_BIG_ENDIAN)
260 buf[0] = *(uint32_t *) &d128.bytes[0];
261 buf[1] = *(uint32_t *) &d128.bytes[4];
262 buf[2] = *(uint32_t *) &d128.bytes[8];
263 buf[3] = *(uint32_t *) &d128.bytes[12];
265 else
267 buf[0] = dfp_byte_swap (*(uint32_t *) &d128.bytes[12]);
268 buf[1] = dfp_byte_swap (*(uint32_t *) &d128.bytes[8]);
269 buf[2] = dfp_byte_swap (*(uint32_t *) &d128.bytes[4]);
270 buf[3] = dfp_byte_swap (*(uint32_t *) &d128.bytes[0]);
274 /* Decode an IEEE 754R decimal128 type into a real. */
276 void
277 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
278 REAL_VALUE_TYPE *r, const long *buf)
280 decNumber dn;
281 decimal128 d128;
282 decContext set;
284 decContextDefault (&set, DEC_INIT_DECIMAL128);
285 set.traps = 0;
287 if (FLOAT_WORDS_BIG_ENDIAN)
289 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
290 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
291 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
292 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
294 else
296 *((uint32_t *) &d128.bytes[0]) = dfp_byte_swap ((uint32_t) buf[3]);
297 *((uint32_t *) &d128.bytes[4]) = dfp_byte_swap ((uint32_t) buf[2]);
298 *((uint32_t *) &d128.bytes[8]) = dfp_byte_swap ((uint32_t) buf[1]);
299 *((uint32_t *) &d128.bytes[12]) = dfp_byte_swap ((uint32_t) buf[0]);
302 decimal128ToNumber (&d128, &dn);
303 decimal_from_decnumber (r, &dn, &set);
306 /* Helper function to convert from a binary real internal
307 representation. */
309 static void
310 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
311 enum machine_mode mode)
313 char string[256];
314 decimal128 *d128;
315 d128 = (decimal128 *) from->sig;
317 decimal128ToString (d128, string);
318 real_from_string3 (to, string, mode);
322 /* Helper function to convert from a binary real internal
323 representation. */
325 static void
326 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
328 char string[256];
330 /* We convert to string, then to decNumber then to decimal128. */
331 real_to_decimal (string, from, sizeof (string), 0, 1);
332 decimal_real_from_string (to, string);
335 /* Helper function to real.c:do_compare() to handle decimal internal
336 representation including when one of the operands is still in the
337 binary internal representation. */
340 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
341 int nan_result)
343 decContext set;
344 decNumber dn, dn2, dn3;
345 REAL_VALUE_TYPE a1, b1;
347 /* If either operand is non-decimal, create temporary versions. */
348 if (!a->decimal)
350 decimal_from_binary (&a1, a);
351 a = &a1;
353 if (!b->decimal)
355 decimal_from_binary (&b1, b);
356 b = &b1;
359 /* Convert into decNumber form for comparison operation. */
360 decContextDefault (&set, DEC_INIT_DECIMAL128);
361 set.traps = 0;
362 decimal128ToNumber ((decimal128 *) a->sig, &dn2);
363 decimal128ToNumber ((decimal128 *) b->sig, &dn3);
365 /* Finally, do the comparison. */
366 decNumberCompare (&dn, &dn2, &dn3, &set);
368 /* Return the comparison result. */
369 if (decNumberIsNaN (&dn))
370 return nan_result;
371 else if (decNumberIsZero (&dn))
372 return 0;
373 else if (decNumberIsNegative (&dn))
374 return -1;
375 else
376 return 1;
379 /* Helper to round_for_format, handling decimal float types. */
381 void
382 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
384 decNumber dn;
385 decContext set;
387 /* Real encoding occurs later. */
388 if (r->cl != rvc_normal)
389 return;
391 decContextDefault (&set, DEC_INIT_DECIMAL128);
392 set.traps = 0;
393 decimal128ToNumber ((decimal128 *) r->sig, &dn);
395 if (fmt == &decimal_quad_format)
397 /* The internal format is already in this format. */
398 return;
400 else if (fmt == &decimal_single_format)
402 decimal32 d32;
403 decContextDefault (&set, DEC_INIT_DECIMAL32);
404 set.traps = 0;
406 decimal32FromNumber (&d32, &dn, &set);
407 decimal32ToNumber (&d32, &dn);
409 else if (fmt == &decimal_double_format)
411 decimal64 d64;
412 decContextDefault (&set, DEC_INIT_DECIMAL64);
413 set.traps = 0;
415 decimal64FromNumber (&d64, &dn, &set);
416 decimal64ToNumber (&d64, &dn);
418 else
419 gcc_unreachable ();
421 decimal_from_decnumber (r, &dn, &set);
424 /* Extend or truncate to a new mode. Handles conversions between
425 binary and decimal types. */
427 void
428 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
429 const REAL_VALUE_TYPE *a)
431 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
433 if (a->decimal && fmt->b == 10)
434 return;
435 if (a->decimal)
436 decimal_to_binary (r, a, mode);
437 else
438 decimal_from_binary (r, a);
441 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
442 significant digits in the result, bounded by BUF_SIZE. If DIGITS
443 is 0, choose the maximum for the representation. If
444 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
445 DIGITS or CROP_TRAILING_ZEROS. */
447 void decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
448 size_t buf_size,
449 size_t digits ATTRIBUTE_UNUSED,
450 int crop_trailing_zeros ATTRIBUTE_UNUSED)
452 decimal128 *d128 = (decimal128*) r_orig->sig;
454 /* decimal128ToString requires space for at least 24 characters;
455 Require two more for suffix. */
456 gcc_assert (buf_size >= 24);
457 decimal128ToString (d128, str);
460 static bool
461 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
462 const REAL_VALUE_TYPE *op1, int subtract_p)
464 decNumber dn;
465 decContext set;
466 decNumber dn2, dn3;
468 decimal_to_decnumber (op0, &dn2);
469 decimal_to_decnumber (op1, &dn3);
471 decContextDefault (&set, DEC_INIT_DECIMAL128);
472 set.traps = 0;
474 if (subtract_p)
475 decNumberSubtract (&dn, &dn2, &dn3, &set);
476 else
477 decNumberAdd (&dn, &dn2, &dn3, &set);
479 decimal_from_decnumber (r, &dn, &set);
481 /* Return true, if inexact. */
482 return (set.status & DEC_Inexact);
485 /* Compute R = OP0 * OP1. */
487 static bool
488 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
489 const REAL_VALUE_TYPE *op1)
491 decContext set;
492 decNumber dn, dn2, dn3;
494 decimal_to_decnumber (op0, &dn2);
495 decimal_to_decnumber (op1, &dn3);
497 decContextDefault (&set, DEC_INIT_DECIMAL128);
498 set.traps = 0;
500 decNumberMultiply (&dn, &dn2, &dn3, &set);
501 decimal_from_decnumber (r, &dn, &set);
503 /* Return true, if inexact. */
504 return (set.status & DEC_Inexact);
507 /* Compute R = OP0 / OP1. */
509 static bool
510 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
511 const REAL_VALUE_TYPE *op1)
513 decContext set;
514 decNumber dn, dn2, dn3;
516 decimal_to_decnumber (op0, &dn2);
517 decimal_to_decnumber (op1, &dn3);
519 decContextDefault (&set, DEC_INIT_DECIMAL128);
520 set.traps = 0;
522 decNumberDivide (&dn, &dn2, &dn3, &set);
523 decimal_from_decnumber (r, &dn, &set);
525 /* Return true, if inexact. */
526 return (set.status & DEC_Inexact);
529 /* Set R to A truncated to an integral value toward zero (decimal
530 floating point). */
532 void
533 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
535 decNumber dn, dn2;
536 decContext set;
538 decContextDefault (&set, DEC_INIT_DECIMAL128);
539 set.traps = 0;
540 set.round = DEC_ROUND_DOWN;
541 decimal128ToNumber ((decimal128 *) a->sig, &dn2);
543 decNumberToIntegralValue (&dn, &dn2, &set);
544 decimal_from_decnumber (r, &dn, &set);
547 /* Render decimal float value R as an integer. */
549 HOST_WIDE_INT
550 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
552 decContext set;
553 decNumber dn, dn2, dn3;
554 REAL_VALUE_TYPE to;
555 char string[256];
557 decContextDefault (&set, DEC_INIT_DECIMAL128);
558 set.traps = 0;
559 set.round = DEC_ROUND_DOWN;
560 decimal128ToNumber ((decimal128 *) r->sig, &dn);
562 decNumberToIntegralValue (&dn2, &dn, &set);
563 decNumberZero (&dn3);
564 decNumberRescale (&dn, &dn2, &dn3, &set);
566 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
567 function. */
568 decNumberToString (&dn, string);
569 real_from_string (&to, string);
570 return real_to_integer (&to);
573 /* Likewise, but to an integer pair, HI+LOW. */
575 void
576 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
577 const REAL_VALUE_TYPE *r)
579 decContext set;
580 decNumber dn, dn2, dn3;
581 REAL_VALUE_TYPE to;
582 char string[256];
584 decContextDefault (&set, DEC_INIT_DECIMAL128);
585 set.traps = 0;
586 set.round = DEC_ROUND_DOWN;
587 decimal128ToNumber ((decimal128 *) r->sig, &dn);
589 decNumberToIntegralValue (&dn2, &dn, &set);
590 decNumberZero (&dn3);
591 decNumberRescale (&dn, &dn2, &dn3, &set);
593 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
594 function. */
595 decNumberToString (&dn, string);
596 real_from_string (&to, string);
597 real_to_integer2 (plow, phigh, &to);
600 /* Perform the decimal floating point operation described by CODE.
601 For a unary operation, OP1 will be NULL. This function returns
602 true if the result may be inexact due to loss of precision. */
604 bool
605 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
606 const REAL_VALUE_TYPE *op0,
607 const REAL_VALUE_TYPE *op1)
609 REAL_VALUE_TYPE a, b;
611 /* If either operand is non-decimal, create temporaries. */
612 if (!op0->decimal)
614 decimal_from_binary (&a, op0);
615 op0 = &a;
617 if (op1 && !op1->decimal)
619 decimal_from_binary (&b, op1);
620 op1 = &b;
623 switch (code)
625 case PLUS_EXPR:
626 return decimal_do_add (r, op0, op1, 0);
628 case MINUS_EXPR:
629 return decimal_do_add (r, op0, op1, 1);
631 case MULT_EXPR:
632 return decimal_do_multiply (r, op0, op1);
634 case RDIV_EXPR:
635 return decimal_do_divide (r, op0, op1);
637 case MIN_EXPR:
638 if (op1->cl == rvc_nan)
639 *r = *op1;
640 else if (real_compare (UNLT_EXPR, op0, op1))
641 *r = *op0;
642 else
643 *r = *op1;
644 return false;
646 case MAX_EXPR:
647 if (op1->cl == rvc_nan)
648 *r = *op1;
649 else if (real_compare (LT_EXPR, op0, op1))
650 *r = *op1;
651 else
652 *r = *op0;
653 return false;
655 case NEGATE_EXPR:
657 decimal128 *d128;
658 *r = *op0;
659 d128 = (decimal128 *) r->sig;
660 /* Flip high bit. */
661 d128->bytes[0] ^= 1 << 7;
662 /* Keep sign field in sync. */
663 r->sign ^= 1;
665 return false;
667 case ABS_EXPR:
669 decimal128 *d128;
670 *r = *op0;
671 d128 = (decimal128 *) r->sig;
672 /* Clear high bit. */
673 d128->bytes[0] &= 0x7f;
674 /* Keep sign field in sync. */
675 r->sign = 0;
677 return false;
679 case FIX_TRUNC_EXPR:
680 decimal_do_fix_trunc (r, op0);
681 return false;
683 default:
684 gcc_unreachable ();
688 /* Fills R with the largest finite value representable in mode MODE.
689 If SIGN is nonzero, R is set to the most negative finite value. */
691 void
692 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
694 char *max;
696 switch (mode)
698 case SDmode:
699 max = (char *) "9.999999E96";
700 break;
701 case DDmode:
702 max = (char *) "9.999999999999999E384";
703 break;
704 case TDmode:
705 max = (char *) "9.999999999999999999999999999999999E6144";
706 break;
707 default:
708 gcc_unreachable ();
711 decimal_real_from_string (r, max);
712 if (sign)
713 r->sig[0] |= 0x80000000;