OSDN Git Service

2007-08-05 Andrew Pinski <andrew_pinski@playstation.sony.com>
[pf3gnuchains/gcc-fork.git] / gcc / dfp.c
1 /* Decimal floating point support.
2    Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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 3, or (at your option) any later
9 version.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "toplev.h"
26 #include "real.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 "decimal64.h"
35 #include "decimal32.h"
36 #include "decNumber.h"
37
38 static uint32_t
39 dfp_byte_swap (uint32_t in)
40 {
41   uint32_t out = 0;
42   unsigned char *p = (unsigned char *) &out;
43   union {
44     uint32_t i;
45     unsigned char b[4];
46   } u;
47
48   u.i = in;
49   p[0] = u.b[3];
50   p[1] = u.b[2];
51   p[2] = u.b[1];
52   p[3] = u.b[0];
53
54   return out;
55 }
56
57 /* Initialize R (a real with the decimal flag set) from DN.  Can
58    utilize status passed in via CONTEXT, if a previous operation had
59    interesting status.  */
60
61 static void
62 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
63 {
64   memset (r, 0, sizeof (REAL_VALUE_TYPE));
65
66   r->cl = rvc_normal;
67   if (decNumberIsZero (dn))
68     r->cl = rvc_zero;
69   if (decNumberIsNaN (dn))
70     r->cl = rvc_nan;
71   if (decNumberIsInfinite (dn))
72     r->cl = rvc_inf;
73   if (context->status & DEC_Overflow)
74     r->cl = rvc_inf;
75   if (decNumberIsNegative (dn))
76     r->sign = 1;
77   r->decimal = 1;
78
79   if (r->cl != rvc_normal)
80     return;
81
82   decContextDefault (context, DEC_INIT_DECIMAL128);
83   context->traps = 0;
84
85   decimal128FromNumber ((decimal128 *) r->sig, dn, context);
86 }
87
88 /* Create decimal encoded R from string S.  */
89
90 void
91 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
92 {
93   decNumber dn;
94   decContext set;
95   decContextDefault (&set, DEC_INIT_DECIMAL128);
96   set.traps = 0;
97
98   decNumberFromString (&dn, s, &set);
99
100   /* It would be more efficient to store directly in decNumber format,
101      but that is impractical from current data structure size.
102      Encoding as a decimal128 is much more compact.  */
103   decimal_from_decnumber (r, &dn, &set);
104 }
105
106 /* Initialize a decNumber from a REAL_VALUE_TYPE.  */
107
108 static void
109 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
110 {
111   decContext set;
112   decContextDefault (&set, DEC_INIT_DECIMAL128);
113   set.traps = 0;
114
115   switch (r->cl)
116     {
117     case rvc_zero:
118       decNumberZero (dn);
119       break;
120     case rvc_inf:
121       decNumberFromString (dn, "Infinity", &set);
122       break;
123     case rvc_nan:
124       if (r->signalling)
125         decNumberFromString (dn, "snan", &set);
126       else
127         decNumberFromString (dn, "nan", &set);
128       break;
129     case rvc_normal:
130       gcc_assert (r->decimal);
131       decimal128ToNumber ((const decimal128 *) r->sig, dn);
132       break;
133     default:
134       gcc_unreachable ();
135     }
136
137   /* Fix up sign bit.  */
138   if (r->sign != decNumberIsNegative (dn))
139     dn->bits ^= DECNEG;
140 }
141
142 /* Encode a real into an IEEE 754R decimal32 type.  */
143
144 void
145 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
146                   long *buf, const REAL_VALUE_TYPE *r)
147 {
148   decNumber dn;
149   decimal32 d32;
150   decContext set;
151
152   decContextDefault (&set, DEC_INIT_DECIMAL128);
153   set.traps = 0;
154
155   decimal_to_decnumber (r, &dn); 
156   decimal32FromNumber (&d32, &dn, &set);
157
158   if (FLOAT_WORDS_BIG_ENDIAN)
159     buf[0] = *(uint32_t *) d32.bytes;
160   else
161     buf[0] = dfp_byte_swap (*(uint32_t *) d32.bytes);
162 }
163
164 /* Decode an IEEE 754R decimal32 type into a real.  */
165
166 void
167 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
168                   REAL_VALUE_TYPE *r, const long *buf)
169 {
170   decNumber dn;
171   decimal32 d32;
172   decContext set;
173
174   decContextDefault (&set, DEC_INIT_DECIMAL128);
175   set.traps = 0;
176
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]);
181
182   decimal32ToNumber (&d32, &dn);
183   decimal_from_decnumber (r, &dn, &set); 
184 }
185
186 /* Encode a real into an IEEE 754R decimal64 type.  */
187
188 void
189 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
190                   long *buf, const REAL_VALUE_TYPE *r)
191 {
192   decNumber dn;
193   decimal64 d64;
194   decContext set;
195
196   decContextDefault (&set, DEC_INIT_DECIMAL128);
197   set.traps = 0;
198
199   decimal_to_decnumber (r, &dn);
200   decimal64FromNumber (&d64, &dn, &set);
201
202   if (FLOAT_WORDS_BIG_ENDIAN)
203     {
204       buf[0] = *(uint32_t *) &d64.bytes[0];
205       buf[1] = *(uint32_t *) &d64.bytes[4];
206     }
207   else
208     {
209       buf[1] = dfp_byte_swap (*(uint32_t *) &d64.bytes[0]);
210       buf[0] = dfp_byte_swap (*(uint32_t *) &d64.bytes[4]);
211     }
212 }
213
214 /* Decode an IEEE 754R decimal64 type into a real.  */
215
216 void
217 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
218                   REAL_VALUE_TYPE *r, const long *buf)
219
220   decNumber dn;
221   decimal64 d64;
222   decContext set;
223
224   decContextDefault (&set, DEC_INIT_DECIMAL128);
225   set.traps = 0;
226
227   if (FLOAT_WORDS_BIG_ENDIAN)
228     {
229       *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
230       *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
231     }
232   else
233     {
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]); 
236     }
237
238   decimal64ToNumber (&d64, &dn);
239   decimal_from_decnumber (r, &dn, &set); 
240 }
241
242 /* Encode a real into an IEEE 754R decimal128 type.  */
243
244 void
245 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
246                    long *buf, const REAL_VALUE_TYPE *r)
247 {
248   decNumber dn;
249   decContext set;
250   decimal128 d128;
251
252   decContextDefault (&set, DEC_INIT_DECIMAL128);
253   set.traps = 0;
254
255   decimal_to_decnumber (r, &dn);
256   decimal128FromNumber (&d128, &dn, &set);
257
258   if (FLOAT_WORDS_BIG_ENDIAN)
259     {
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];
264     }
265   else
266     {
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]);
271     }
272 }
273
274 /* Decode an IEEE 754R decimal128 type into a real.  */
275
276 void
277 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
278                    REAL_VALUE_TYPE *r, const long *buf)
279 {
280   decNumber dn;
281   decimal128 d128;
282   decContext set;
283
284   decContextDefault (&set, DEC_INIT_DECIMAL128);
285   set.traps = 0;
286
287   if (FLOAT_WORDS_BIG_ENDIAN)
288     {
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];
293     }
294   else
295     {
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]);
300     }
301
302   decimal128ToNumber (&d128, &dn);
303   decimal_from_decnumber (r, &dn, &set); 
304 }
305
306 /* Helper function to convert from a binary real internal
307    representation.  */
308
309 static void
310 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
311                    enum machine_mode mode)
312 {
313   char string[256];
314   const decimal128 *const d128 = (const decimal128 *) from->sig;
315
316   decimal128ToString (d128, string);
317   real_from_string3 (to, string, mode);
318 }
319
320
321 /* Helper function to convert from a binary real internal
322    representation.  */
323
324 static void
325 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
326 {
327   char string[256];
328
329   /* We convert to string, then to decNumber then to decimal128.  */
330   real_to_decimal (string, from, sizeof (string), 0, 1);
331   decimal_real_from_string (to, string);
332 }
333
334 /* Helper function to real.c:do_compare() to handle decimal internal
335    representation including when one of the operands is still in the
336    binary internal representation.  */
337
338 int
339 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
340                     int nan_result)
341 {
342   decContext set;
343   decNumber dn, dn2, dn3;
344   REAL_VALUE_TYPE a1, b1;
345
346   /* If either operand is non-decimal, create temporary versions.  */
347   if (!a->decimal)
348     {
349       decimal_from_binary (&a1, a);
350       a = &a1;
351     }
352   if (!b->decimal)
353     {
354       decimal_from_binary (&b1, b);
355       b = &b1;
356     }
357     
358   /* Convert into decNumber form for comparison operation.  */
359   decContextDefault (&set, DEC_INIT_DECIMAL128);
360   set.traps = 0;  
361   decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
362   decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
363
364   /* Finally, do the comparison.  */
365   decNumberCompare (&dn, &dn2, &dn3, &set);
366
367   /* Return the comparison result.  */
368   if (decNumberIsNaN (&dn))
369     return nan_result;
370   else if (decNumberIsZero (&dn))
371     return 0;
372   else if (decNumberIsNegative (&dn))
373     return -1;
374   else 
375     return 1;
376 }
377
378 /* Helper to round_for_format, handling decimal float types.  */
379
380 void
381 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
382 {
383   decNumber dn;
384   decContext set;
385
386   /* Real encoding occurs later.  */
387   if (r->cl != rvc_normal)
388     return;
389
390   decContextDefault (&set, DEC_INIT_DECIMAL128);
391   set.traps = 0;
392   decimal128ToNumber ((decimal128 *) r->sig, &dn);
393
394   if (fmt == &decimal_quad_format)
395     {
396       /* The internal format is already in this format.  */
397       return;
398     }
399   else if (fmt == &decimal_single_format)
400     {
401       decimal32 d32;
402       decContextDefault (&set, DEC_INIT_DECIMAL32);
403       set.traps = 0;
404
405       decimal32FromNumber (&d32, &dn, &set);
406       decimal32ToNumber (&d32, &dn);
407     }
408   else if (fmt == &decimal_double_format)
409     {
410       decimal64 d64;
411       decContextDefault (&set, DEC_INIT_DECIMAL64);
412       set.traps = 0;
413
414       decimal64FromNumber (&d64, &dn, &set);
415       decimal64ToNumber (&d64, &dn);
416     }
417   else
418     gcc_unreachable ();
419
420   decimal_from_decnumber (r, &dn, &set);
421 }
422
423 /* Extend or truncate to a new mode.  Handles conversions between
424    binary and decimal types.  */
425
426 void
427 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode, 
428                       const REAL_VALUE_TYPE *a)
429 {
430   const struct real_format *fmt = REAL_MODE_FORMAT (mode);
431
432   if (a->decimal && fmt->b == 10)
433     return;
434   if (a->decimal)
435       decimal_to_binary (r, a, mode);
436   else
437       decimal_from_binary (r, a);
438 }
439
440 /* Render R_ORIG as a decimal floating point constant.  Emit DIGITS
441    significant digits in the result, bounded by BUF_SIZE.  If DIGITS
442    is 0, choose the maximum for the representation.  If
443    CROP_TRAILING_ZEROS, strip trailing zeros.  Currently, not honoring
444    DIGITS or CROP_TRAILING_ZEROS.  */
445
446 void
447 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)
451 {
452   const decimal128 *const d128 = (const decimal128*) r_orig->sig;
453
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);
458 }
459
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)
463 {
464   decNumber dn;
465   decContext set;
466   decNumber dn2, dn3;
467
468   decimal_to_decnumber (op0, &dn2);
469   decimal_to_decnumber (op1, &dn3);
470
471   decContextDefault (&set, DEC_INIT_DECIMAL128);
472   set.traps = 0;
473
474   if (subtract_p)
475     decNumberSubtract (&dn, &dn2, &dn3, &set);
476   else 
477     decNumberAdd (&dn, &dn2, &dn3, &set);
478
479   decimal_from_decnumber (r, &dn, &set);
480
481   /* Return true, if inexact.  */
482   return (set.status & DEC_Inexact);
483 }
484
485 /* Compute R = OP0 * OP1.  */
486
487 static bool
488 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
489                      const REAL_VALUE_TYPE *op1)
490 {
491   decContext set;
492   decNumber dn, dn2, dn3;
493
494   decimal_to_decnumber (op0, &dn2);
495   decimal_to_decnumber (op1, &dn3);
496
497   decContextDefault (&set, DEC_INIT_DECIMAL128);
498   set.traps = 0;
499
500   decNumberMultiply (&dn, &dn2, &dn3, &set);
501   decimal_from_decnumber (r, &dn, &set);
502
503   /* Return true, if inexact.  */
504   return (set.status & DEC_Inexact);
505 }
506
507 /* Compute R = OP0 / OP1.  */
508
509 static bool
510 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
511                    const REAL_VALUE_TYPE *op1)
512 {
513   decContext set;
514   decNumber dn, dn2, dn3;
515
516   decimal_to_decnumber (op0, &dn2);
517   decimal_to_decnumber (op1, &dn3);
518
519   decContextDefault (&set, DEC_INIT_DECIMAL128);
520   set.traps = 0;
521
522   decNumberDivide (&dn, &dn2, &dn3, &set);
523   decimal_from_decnumber (r, &dn, &set);
524
525   /* Return true, if inexact.  */
526   return (set.status & DEC_Inexact);
527 }
528
529 /* Set R to A truncated to an integral value toward zero (decimal
530    floating point).  */
531
532 void
533 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
534 {
535   decNumber dn, dn2;
536   decContext set;
537
538   decContextDefault (&set, DEC_INIT_DECIMAL128);
539   set.traps = 0;
540   set.round = DEC_ROUND_DOWN;
541   decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
542
543   decNumberToIntegralValue (&dn, &dn2, &set);
544   decimal_from_decnumber (r, &dn, &set);
545 }
546
547 /* Render decimal float value R as an integer.  */
548
549 HOST_WIDE_INT
550 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
551 {
552   decContext set;
553   decNumber dn, dn2, dn3;
554   REAL_VALUE_TYPE to;
555   char string[256];
556
557   decContextDefault (&set, DEC_INIT_DECIMAL128);
558   set.traps = 0;
559   set.round = DEC_ROUND_DOWN;
560   decimal128ToNumber ((const decimal128 *) r->sig, &dn);
561
562   decNumberToIntegralValue (&dn2, &dn, &set);
563   decNumberZero (&dn3);
564   decNumberRescale (&dn, &dn2, &dn3, &set);
565
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);
571 }
572
573 /* Likewise, but to an integer pair, HI+LOW.  */
574
575 void
576 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
577                           const REAL_VALUE_TYPE *r)
578 {
579   decContext set;
580   decNumber dn, dn2, dn3;
581   REAL_VALUE_TYPE to;
582   char string[256];
583
584   decContextDefault (&set, DEC_INIT_DECIMAL128);
585   set.traps = 0;
586   set.round = DEC_ROUND_DOWN;
587   decimal128ToNumber ((const decimal128 *) r->sig, &dn);
588
589   decNumberToIntegralValue (&dn2, &dn, &set);
590   decNumberZero (&dn3);
591   decNumberRescale (&dn, &dn2, &dn3, &set);
592
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);
598 }
599
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.  */
603
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)
608 {
609   REAL_VALUE_TYPE a, b;
610
611   /* If either operand is non-decimal, create temporaries.  */
612   if (!op0->decimal)
613     {
614       decimal_from_binary (&a, op0);
615       op0 = &a;
616     }
617   if (op1 && !op1->decimal)
618     {
619       decimal_from_binary (&b, op1);
620       op1 = &b;
621     }
622
623   switch (code)
624     {
625     case PLUS_EXPR:
626       return decimal_do_add (r, op0, op1, 0);
627
628     case MINUS_EXPR:
629       return decimal_do_add (r, op0, op1, 1);
630
631     case MULT_EXPR:
632       return decimal_do_multiply (r, op0, op1);
633
634     case RDIV_EXPR:
635       return decimal_do_divide (r, op0, op1);
636
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;
645
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;
654
655     case NEGATE_EXPR:
656       {
657         *r = *op0;
658         /* Flip sign bit.  */
659         decimal128FlipSign ((decimal128 *) r->sig);
660         /* Keep sign field in sync.  */
661         r->sign ^= 1;
662       }
663       return false;
664
665     case ABS_EXPR:
666       {
667         *r = *op0;
668         /* Clear sign bit.  */
669         decimal128ClearSign ((decimal128 *) r->sig);
670         /* Keep sign field in sync.  */
671         r->sign = 0;
672       }
673       return false;
674
675     case FIX_TRUNC_EXPR:
676       decimal_do_fix_trunc (r, op0);
677       return false;
678
679     default:
680       gcc_unreachable ();
681     }
682 }
683
684 /* Fills R with the largest finite value representable in mode MODE.
685    If SIGN is nonzero, R is set to the most negative finite value.  */
686
687 void
688 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
689
690   const char *max;
691
692   switch (mode)
693     {
694     case SDmode:
695       max = "9.999999E96";
696       break;
697     case DDmode:
698       max = "9.999999999999999E384";
699       break;
700     case TDmode:
701       max = "9.999999999999999999999999999999999E6144";
702       break;
703     default:
704       gcc_unreachable ();
705     }
706
707   decimal_real_from_string (r, max);
708   if (sign)
709     decimal128SetSign ((decimal128 *) r->sig, 1);
710 }