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