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