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