OSDN Git Service

2010-06-07 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / dfp.c
1 /* Decimal floating point support.
2    Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software
3    Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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"
29
30 /* The order of the following headers is important for making sure
31    decNumber structure is large enough to hold decimal128 digits.  */
32
33 #include "decimal128.h"
34 #include "decimal128Local.h"
35 #include "decimal64.h"
36 #include "decimal32.h"
37 #include "decNumber.h"
38
39 #ifndef WORDS_BIGENDIAN
40 #define WORDS_BIGENDIAN 0
41 #endif
42
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.  */
46
47 static void
48 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
49 {
50   memset (r, 0, sizeof (REAL_VALUE_TYPE));
51
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;
62
63   if (r->cl != rvc_normal)
64     return;
65
66   decContextDefault (context, DEC_INIT_DECIMAL128);
67   context->traps = 0;
68
69   decimal128FromNumber ((decimal128 *) r->sig, dn, context);
70 }
71
72 /* Create decimal encoded R from string S.  */
73
74 void
75 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
76 {
77   decNumber dn;
78   decContext set;
79   decContextDefault (&set, DEC_INIT_DECIMAL128);
80   set.traps = 0;
81
82   decNumberFromString (&dn, s, &set);
83
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);
88 }
89
90 /* Initialize a decNumber from a REAL_VALUE_TYPE.  */
91
92 static void
93 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
94 {
95   decContext set;
96   decContextDefault (&set, DEC_INIT_DECIMAL128);
97   set.traps = 0;
98
99   switch (r->cl)
100     {
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 ();
119     }
120
121   /* Fix up sign bit.  */
122   if (r->sign != decNumberIsNegative (dn))
123     dn->bits ^= DECNEG;
124 }
125
126 /* Encode a real into an IEEE 754 decimal32 type.  */
127
128 void
129 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
130                   long *buf, const REAL_VALUE_TYPE *r)
131 {
132   decNumber dn;
133   decimal32 d32;
134   decContext set;
135   int32_t image;
136
137   decContextDefault (&set, DEC_INIT_DECIMAL128);
138   set.traps = 0;
139
140   decimal_to_decnumber (r, &dn);
141   decimal32FromNumber (&d32, &dn, &set);
142
143   memcpy (&image, d32.bytes, sizeof (int32_t));
144   buf[0] = image;
145 }
146
147 /* Decode an IEEE 754 decimal32 type into a real.  */
148
149 void
150 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
151                   REAL_VALUE_TYPE *r, const long *buf)
152 {
153   decNumber dn;
154   decimal32 d32;
155   decContext set;
156   int32_t image;
157
158   decContextDefault (&set, DEC_INIT_DECIMAL128);
159   set.traps = 0;
160
161   image = buf[0];
162   memcpy (&d32.bytes, &image, sizeof (int32_t));
163
164   decimal32ToNumber (&d32, &dn);
165   decimal_from_decnumber (r, &dn, &set);
166 }
167
168 /* Encode a real into an IEEE 754 decimal64 type.  */
169
170 void
171 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
172                   long *buf, const REAL_VALUE_TYPE *r)
173 {
174   decNumber dn;
175   decimal64 d64;
176   decContext set;
177   int32_t image;
178
179   decContextDefault (&set, DEC_INIT_DECIMAL128);
180   set.traps = 0;
181
182   decimal_to_decnumber (r, &dn);
183   decimal64FromNumber (&d64, &dn, &set);
184
185   if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
186     {
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;
191     }
192   else
193     {
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;
198     }
199 }
200
201 /* Decode an IEEE 754 decimal64 type into a real.  */
202
203 void
204 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
205                   REAL_VALUE_TYPE *r, const long *buf)
206 {
207   decNumber dn;
208   decimal64 d64;
209   decContext set;
210   int32_t image;
211
212   decContextDefault (&set, DEC_INIT_DECIMAL128);
213   set.traps = 0;
214
215   if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
216     {
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));
221     }
222   else
223     {
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));
228     }
229
230   decimal64ToNumber (&d64, &dn);
231   decimal_from_decnumber (r, &dn, &set);
232 }
233
234 /* Encode a real into an IEEE 754 decimal128 type.  */
235
236 void
237 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
238                    long *buf, const REAL_VALUE_TYPE *r)
239 {
240   decNumber dn;
241   decContext set;
242   decimal128 d128;
243   int32_t image;
244
245   decContextDefault (&set, DEC_INIT_DECIMAL128);
246   set.traps = 0;
247
248   decimal_to_decnumber (r, &dn);
249   decimal128FromNumber (&d128, &dn, &set);
250
251   if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
252     {
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;
261     }
262   else
263     {
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;
272     }
273 }
274
275 /* Decode an IEEE 754 decimal128 type into a real.  */
276
277 void
278 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
279                    REAL_VALUE_TYPE *r, const long *buf)
280 {
281   decNumber dn;
282   decimal128 d128;
283   decContext set;
284   int32_t image;
285
286   decContextDefault (&set, DEC_INIT_DECIMAL128);
287   set.traps = 0;
288
289   if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
290     {
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));
299     }
300   else
301     {
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));
310     }
311
312   decimal128ToNumber (&d128, &dn);
313   decimal_from_decnumber (r, &dn, &set);
314 }
315
316 /* Helper function to convert from a binary real internal
317    representation.  */
318
319 static void
320 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
321                    enum machine_mode mode)
322 {
323   char string[256];
324   const decimal128 *const d128 = (const decimal128 *) from->sig;
325
326   decimal128ToString (d128, string);
327   real_from_string3 (to, string, mode);
328 }
329
330
331 /* Helper function to convert from a binary real internal
332    representation.  */
333
334 static void
335 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
336 {
337   char string[256];
338
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);
342 }
343
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.  */
347
348 int
349 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
350                     int nan_result)
351 {
352   decContext set;
353   decNumber dn, dn2, dn3;
354   REAL_VALUE_TYPE a1, b1;
355
356   /* If either operand is non-decimal, create temporary versions.  */
357   if (!a->decimal)
358     {
359       decimal_from_binary (&a1, a);
360       a = &a1;
361     }
362   if (!b->decimal)
363     {
364       decimal_from_binary (&b1, b);
365       b = &b1;
366     }
367
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);
373
374   /* Finally, do the comparison.  */
375   decNumberCompare (&dn, &dn2, &dn3, &set);
376
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;
386 }
387
388 /* Helper to round_for_format, handling decimal float types.  */
389
390 void
391 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
392 {
393   decNumber dn;
394   decContext set;
395
396   /* Real encoding occurs later.  */
397   if (r->cl != rvc_normal)
398     return;
399
400   decContextDefault (&set, DEC_INIT_DECIMAL128);
401   set.traps = 0;
402   decimal128ToNumber ((decimal128 *) r->sig, &dn);
403
404   if (fmt == &decimal_quad_format)
405     {
406       /* The internal format is already in this format.  */
407       return;
408     }
409   else if (fmt == &decimal_single_format)
410     {
411       decimal32 d32;
412       decContextDefault (&set, DEC_INIT_DECIMAL32);
413       set.traps = 0;
414
415       decimal32FromNumber (&d32, &dn, &set);
416       decimal32ToNumber (&d32, &dn);
417     }
418   else if (fmt == &decimal_double_format)
419     {
420       decimal64 d64;
421       decContextDefault (&set, DEC_INIT_DECIMAL64);
422       set.traps = 0;
423
424       decimal64FromNumber (&d64, &dn, &set);
425       decimal64ToNumber (&d64, &dn);
426     }
427   else
428     gcc_unreachable ();
429
430   decimal_from_decnumber (r, &dn, &set);
431 }
432
433 /* Extend or truncate to a new mode.  Handles conversions between
434    binary and decimal types.  */
435
436 void
437 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
438                       const REAL_VALUE_TYPE *a)
439 {
440   const struct real_format *fmt = REAL_MODE_FORMAT (mode);
441
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);
448 }
449
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.  */
455
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)
461 {
462   const decimal128 *const d128 = (const decimal128*) r_orig->sig;
463
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);
468 }
469
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)
473 {
474   decNumber dn;
475   decContext set;
476   decNumber dn2, dn3;
477
478   decimal_to_decnumber (op0, &dn2);
479   decimal_to_decnumber (op1, &dn3);
480
481   decContextDefault (&set, DEC_INIT_DECIMAL128);
482   set.traps = 0;
483
484   if (subtract_p)
485     decNumberSubtract (&dn, &dn2, &dn3, &set);
486   else
487     decNumberAdd (&dn, &dn2, &dn3, &set);
488
489   decimal_from_decnumber (r, &dn, &set);
490
491   /* Return true, if inexact.  */
492   return (set.status & DEC_Inexact);
493 }
494
495 /* Compute R = OP0 * OP1.  */
496
497 static bool
498 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
499                      const REAL_VALUE_TYPE *op1)
500 {
501   decContext set;
502   decNumber dn, dn2, dn3;
503
504   decimal_to_decnumber (op0, &dn2);
505   decimal_to_decnumber (op1, &dn3);
506
507   decContextDefault (&set, DEC_INIT_DECIMAL128);
508   set.traps = 0;
509
510   decNumberMultiply (&dn, &dn2, &dn3, &set);
511   decimal_from_decnumber (r, &dn, &set);
512
513   /* Return true, if inexact.  */
514   return (set.status & DEC_Inexact);
515 }
516
517 /* Compute R = OP0 / OP1.  */
518
519 static bool
520 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
521                    const REAL_VALUE_TYPE *op1)
522 {
523   decContext set;
524   decNumber dn, dn2, dn3;
525
526   decimal_to_decnumber (op0, &dn2);
527   decimal_to_decnumber (op1, &dn3);
528
529   decContextDefault (&set, DEC_INIT_DECIMAL128);
530   set.traps = 0;
531
532   decNumberDivide (&dn, &dn2, &dn3, &set);
533   decimal_from_decnumber (r, &dn, &set);
534
535   /* Return true, if inexact.  */
536   return (set.status & DEC_Inexact);
537 }
538
539 /* Set R to A truncated to an integral value toward zero (decimal
540    floating point).  */
541
542 void
543 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
544 {
545   decNumber dn, dn2;
546   decContext set;
547
548   decContextDefault (&set, DEC_INIT_DECIMAL128);
549   set.traps = 0;
550   set.round = DEC_ROUND_DOWN;
551   decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
552
553   decNumberToIntegralValue (&dn, &dn2, &set);
554   decimal_from_decnumber (r, &dn, &set);
555 }
556
557 /* Render decimal float value R as an integer.  */
558
559 HOST_WIDE_INT
560 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
561 {
562   decContext set;
563   decNumber dn, dn2, dn3;
564   REAL_VALUE_TYPE to;
565   char string[256];
566
567   decContextDefault (&set, DEC_INIT_DECIMAL128);
568   set.traps = 0;
569   set.round = DEC_ROUND_DOWN;
570   decimal128ToNumber ((const decimal128 *) r->sig, &dn);
571
572   decNumberToIntegralValue (&dn2, &dn, &set);
573   decNumberZero (&dn3);
574   decNumberRescale (&dn, &dn2, &dn3, &set);
575
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);
581 }
582
583 /* Likewise, but to an integer pair, HI+LOW.  */
584
585 void
586 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
587                           const REAL_VALUE_TYPE *r)
588 {
589   decContext set;
590   decNumber dn, dn2, dn3;
591   REAL_VALUE_TYPE to;
592   char string[256];
593
594   decContextDefault (&set, DEC_INIT_DECIMAL128);
595   set.traps = 0;
596   set.round = DEC_ROUND_DOWN;
597   decimal128ToNumber ((const decimal128 *) r->sig, &dn);
598
599   decNumberToIntegralValue (&dn2, &dn, &set);
600   decNumberZero (&dn3);
601   decNumberRescale (&dn, &dn2, &dn3, &set);
602
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);
608 }
609
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.  */
613
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)
618 {
619   REAL_VALUE_TYPE a, b;
620
621   /* If either operand is non-decimal, create temporaries.  */
622   if (!op0->decimal)
623     {
624       decimal_from_binary (&a, op0);
625       op0 = &a;
626     }
627   if (op1 && !op1->decimal)
628     {
629       decimal_from_binary (&b, op1);
630       op1 = &b;
631     }
632
633   switch (code)
634     {
635     case PLUS_EXPR:
636       return decimal_do_add (r, op0, op1, 0);
637
638     case MINUS_EXPR:
639       return decimal_do_add (r, op0, op1, 1);
640
641     case MULT_EXPR:
642       return decimal_do_multiply (r, op0, op1);
643
644     case RDIV_EXPR:
645       return decimal_do_divide (r, op0, op1);
646
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;
655
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;
664
665     case NEGATE_EXPR:
666       {
667         *r = *op0;
668         /* Flip sign bit.  */
669         decimal128FlipSign ((decimal128 *) r->sig);
670         /* Keep sign field in sync.  */
671         r->sign ^= 1;
672       }
673       return false;
674
675     case ABS_EXPR:
676       {
677         *r = *op0;
678         /* Clear sign bit.  */
679         decimal128ClearSign ((decimal128 *) r->sig);
680         /* Keep sign field in sync.  */
681         r->sign = 0;
682       }
683       return false;
684
685     case FIX_TRUNC_EXPR:
686       decimal_do_fix_trunc (r, op0);
687       return false;
688
689     default:
690       gcc_unreachable ();
691     }
692 }
693
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.  */
696
697 void
698 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
699 {
700   const char *max;
701
702   switch (mode)
703     {
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 ();
715     }
716
717   decimal_real_from_string (r, max);
718   if (sign)
719     decimal128SetSign ((decimal128 *) r->sig, 1);
720 }