dfp.c 18.1 KB
Newer Older
Jon Grimm committed
1
/* Decimal floating point support.
Jakub Jelinek committed
2
   Copyright (C) 2005-2015 Free Software Foundation, Inc.
Jon Grimm committed
3 4 5 6 7

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
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
Jon Grimm committed
9 10 11 12 13 14 15 16
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
17 18
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Jon Grimm committed
19 20 21 22 23

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
24 25 26 27 28 29 30 31 32 33
#include "hash-set.h"
#include "machmode.h"
#include "vec.h"
#include "double-int.h"
#include "input.h"
#include "alias.h"
#include "symtab.h"
#include "wide-int.h"
#include "inchash.h"
#include "real.h"
Jon Grimm committed
34 35 36
#include "tree.h"
#include "tm_p.h"
#include "dfp.h"
Kenneth Zadeck committed
37
#include "wide-int.h"
Jon Grimm committed
38 39 40 41 42

/* The order of the following headers is important for making sure
   decNumber structure is large enough to hold decimal128 digits.  */

#include "decimal128.h"
43
#include "decimal128Local.h"
Jon Grimm committed
44 45 46 47
#include "decimal64.h"
#include "decimal32.h"
#include "decNumber.h"

48 49 50 51
#ifndef WORDS_BIGENDIAN
#define WORDS_BIGENDIAN 0
#endif

Jon Grimm committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/* Initialize R (a real with the decimal flag set) from DN.  Can
   utilize status passed in via CONTEXT, if a previous operation had
   interesting status.  */

static void
decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
{
  memset (r, 0, sizeof (REAL_VALUE_TYPE));

  r->cl = rvc_normal;
  if (decNumberIsNaN (dn))
    r->cl = rvc_nan;
  if (decNumberIsInfinite (dn))
    r->cl = rvc_inf;
  if (context->status & DEC_Overflow)
    r->cl = rvc_inf;
  if (decNumberIsNegative (dn))
    r->sign = 1;
  r->decimal = 1;

  if (r->cl != rvc_normal)
    return;

  decContextDefault (context, DEC_INIT_DECIMAL128);
  context->traps = 0;

  decimal128FromNumber ((decimal128 *) r->sig, dn, context);
}

/* Create decimal encoded R from string S.  */

void
decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
{
  decNumber dn;
  decContext set;
  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

91
  decNumberFromString (&dn, s, &set);
Jon Grimm committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  /* It would be more efficient to store directly in decNumber format,
     but that is impractical from current data structure size.
     Encoding as a decimal128 is much more compact.  */
  decimal_from_decnumber (r, &dn, &set);
}

/* Initialize a decNumber from a REAL_VALUE_TYPE.  */

static void
decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
{
  decContext set;
  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  switch (r->cl)
    {
    case rvc_zero:
      decNumberZero (dn);
      break;
    case rvc_inf:
114
      decNumberFromString (dn, "Infinity", &set);
Jon Grimm committed
115 116 117
      break;
    case rvc_nan:
      if (r->signalling)
118
        decNumberFromString (dn, "snan", &set);
Jon Grimm committed
119
      else
120
        decNumberFromString (dn, "nan", &set);
Jon Grimm committed
121 122
      break;
    case rvc_normal:
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      if (!r->decimal)
	{
	  /* dconst{1,2,m1,half} are used in various places in
	     the middle-end and optimizers, allow them here
	     as an exception by converting them to decimal.  */
	  if (memcmp (r, &dconst1, sizeof (*r)) == 0)
	    {
	      decNumberFromString (dn, "1", &set);
	      break;
	    }
	  if (memcmp (r, &dconst2, sizeof (*r)) == 0)
	    {
	      decNumberFromString (dn, "2", &set);
	      break;
	    }
	  if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
	    {
	      decNumberFromString (dn, "-1", &set);
	      break;
	    }
	  if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
	    {
	      decNumberFromString (dn, "0.5", &set);
	      break;
	    }
	  gcc_unreachable ();
	}
150
      decimal128ToNumber ((const decimal128 *) r->sig, dn);
Jon Grimm committed
151 152 153 154 155 156 157
      break;
    default:
      gcc_unreachable ();
    }

  /* Fix up sign bit.  */
  if (r->sign != decNumberIsNegative (dn))
158
    dn->bits ^= DECNEG;
Jon Grimm committed
159 160
}

161
/* Encode a real into an IEEE 754 decimal32 type.  */
Jon Grimm committed
162

163
void
Jon Grimm committed
164 165 166 167 168 169
encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		  long *buf, const REAL_VALUE_TYPE *r)
{
  decNumber dn;
  decimal32 d32;
  decContext set;
170
  int32_t image;
Jon Grimm committed
171 172 173 174

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

H.J. Lu committed
175
  decimal_to_decnumber (r, &dn);
Jon Grimm committed
176 177
  decimal32FromNumber (&d32, &dn, &set);

178 179
  memcpy (&image, d32.bytes, sizeof (int32_t));
  buf[0] = image;
Jon Grimm committed
180 181
}

182
/* Decode an IEEE 754 decimal32 type into a real.  */
Jon Grimm committed
183

184 185 186
void
decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		  REAL_VALUE_TYPE *r, const long *buf)
Jon Grimm committed
187 188 189 190
{
  decNumber dn;
  decimal32 d32;
  decContext set;
191
  int32_t image;
Jon Grimm committed
192 193 194 195

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

196 197
  image = buf[0];
  memcpy (&d32.bytes, &image, sizeof (int32_t));
Jon Grimm committed
198 199

  decimal32ToNumber (&d32, &dn);
H.J. Lu committed
200
  decimal_from_decnumber (r, &dn, &set);
Jon Grimm committed
201 202
}

203
/* Encode a real into an IEEE 754 decimal64 type.  */
Jon Grimm committed
204

205
void
Jon Grimm committed
206 207 208 209 210 211
encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		  long *buf, const REAL_VALUE_TYPE *r)
{
  decNumber dn;
  decimal64 d64;
  decContext set;
212
  int32_t image;
Jon Grimm committed
213 214 215 216 217 218 219

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  decimal_to_decnumber (r, &dn);
  decimal64FromNumber (&d64, &dn, &set);

220 221
  if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
    {
222 223 224 225
      memcpy (&image, &d64.bytes[0], sizeof (int32_t));
      buf[0] = image;
      memcpy (&image, &d64.bytes[4], sizeof (int32_t));
      buf[1] = image;
226 227 228
    }
  else
    {
229 230 231 232
      memcpy (&image, &d64.bytes[4], sizeof (int32_t));
      buf[0] = image;
      memcpy (&image, &d64.bytes[0], sizeof (int32_t));
      buf[1] = image;
233
    }
Jon Grimm committed
234 235
}

236
/* Decode an IEEE 754 decimal64 type into a real.  */
Jon Grimm committed
237

238
void
Jon Grimm committed
239 240
decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		  REAL_VALUE_TYPE *r, const long *buf)
H.J. Lu committed
241
{
Jon Grimm committed
242 243 244
  decNumber dn;
  decimal64 d64;
  decContext set;
245
  int32_t image;
Jon Grimm committed
246 247 248 249

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

250 251
  if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
    {
252 253 254 255
      image = buf[0];
      memcpy (&d64.bytes[0], &image, sizeof (int32_t));
      image = buf[1];
      memcpy (&d64.bytes[4], &image, sizeof (int32_t));
256 257 258
    }
  else
    {
259 260 261 262
      image = buf[1];
      memcpy (&d64.bytes[0], &image, sizeof (int32_t));
      image = buf[0];
      memcpy (&d64.bytes[4], &image, sizeof (int32_t));
263
    }
Jon Grimm committed
264 265

  decimal64ToNumber (&d64, &dn);
H.J. Lu committed
266
  decimal_from_decnumber (r, &dn, &set);
Jon Grimm committed
267 268
}

269
/* Encode a real into an IEEE 754 decimal128 type.  */
Jon Grimm committed
270

271
void
Jon Grimm committed
272 273 274 275 276 277
encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		   long *buf, const REAL_VALUE_TYPE *r)
{
  decNumber dn;
  decContext set;
  decimal128 d128;
278
  int32_t image;
Jon Grimm committed
279 280 281 282 283 284 285

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  decimal_to_decnumber (r, &dn);
  decimal128FromNumber (&d128, &dn, &set);

286 287
  if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
    {
288 289 290 291 292 293 294 295
      memcpy (&image, &d128.bytes[0], sizeof (int32_t));
      buf[0] = image;
      memcpy (&image, &d128.bytes[4], sizeof (int32_t));
      buf[1] = image;
      memcpy (&image, &d128.bytes[8], sizeof (int32_t));
      buf[2] = image;
      memcpy (&image, &d128.bytes[12], sizeof (int32_t));
      buf[3] = image;
296 297 298
    }
  else
    {
299 300 301 302 303 304 305 306
      memcpy (&image, &d128.bytes[12], sizeof (int32_t));
      buf[0] = image;
      memcpy (&image, &d128.bytes[8], sizeof (int32_t));
      buf[1] = image;
      memcpy (&image, &d128.bytes[4], sizeof (int32_t));
      buf[2] = image;
      memcpy (&image, &d128.bytes[0], sizeof (int32_t));
      buf[3] = image;
307
    }
Jon Grimm committed
308 309
}

310
/* Decode an IEEE 754 decimal128 type into a real.  */
Jon Grimm committed
311

312
void
Jon Grimm committed
313 314 315 316 317 318
decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
		   REAL_VALUE_TYPE *r, const long *buf)
{
  decNumber dn;
  decimal128 d128;
  decContext set;
319
  int32_t image;
Jon Grimm committed
320 321 322 323

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

324 325
  if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
    {
326 327 328 329 330 331 332 333
      image = buf[0];
      memcpy (&d128.bytes[0],  &image, sizeof (int32_t));
      image = buf[1];
      memcpy (&d128.bytes[4],  &image, sizeof (int32_t));
      image = buf[2];
      memcpy (&d128.bytes[8],  &image, sizeof (int32_t));
      image = buf[3];
      memcpy (&d128.bytes[12], &image, sizeof (int32_t));
334 335 336
    }
  else
    {
337 338 339 340 341 342 343 344
      image = buf[3];
      memcpy (&d128.bytes[0],  &image, sizeof (int32_t));
      image = buf[2];
      memcpy (&d128.bytes[4],  &image, sizeof (int32_t));
      image = buf[1];
      memcpy (&d128.bytes[8],  &image, sizeof (int32_t));
      image = buf[0];
      memcpy (&d128.bytes[12], &image, sizeof (int32_t));
345
    }
Jon Grimm committed
346 347

  decimal128ToNumber (&d128, &dn);
H.J. Lu committed
348
  decimal_from_decnumber (r, &dn, &set);
Jon Grimm committed
349 350 351 352 353 354 355
}

/* Helper function to convert from a binary real internal
   representation.  */

static void
decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
356
		   machine_mode mode)
Jon Grimm committed
357 358
{
  char string[256];
359
  const decimal128 *const d128 = (const decimal128 *) from->sig;
Jon Grimm committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

  decimal128ToString (d128, string);
  real_from_string3 (to, string, mode);
}


/* Helper function to convert from a binary real internal
   representation.  */

static void
decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
{
  char string[256];

  /* We convert to string, then to decNumber then to decimal128.  */
  real_to_decimal (string, from, sizeof (string), 0, 1);
  decimal_real_from_string (to, string);
}

/* Helper function to real.c:do_compare() to handle decimal internal
380
   representation including when one of the operands is still in the
Jon Grimm committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
   binary internal representation.  */

int
decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
		    int nan_result)
{
  decContext set;
  decNumber dn, dn2, dn3;
  REAL_VALUE_TYPE a1, b1;

  /* If either operand is non-decimal, create temporary versions.  */
  if (!a->decimal)
    {
      decimal_from_binary (&a1, a);
      a = &a1;
    }
  if (!b->decimal)
    {
      decimal_from_binary (&b1, b);
      b = &b1;
    }
H.J. Lu committed
402

Jon Grimm committed
403 404
  /* Convert into decNumber form for comparison operation.  */
  decContextDefault (&set, DEC_INIT_DECIMAL128);
H.J. Lu committed
405
  set.traps = 0;
406 407
  decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
  decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
Jon Grimm committed
408 409 410 411 412 413 414 415 416 417 418

  /* Finally, do the comparison.  */
  decNumberCompare (&dn, &dn2, &dn3, &set);

  /* Return the comparison result.  */
  if (decNumberIsNaN (&dn))
    return nan_result;
  else if (decNumberIsZero (&dn))
    return 0;
  else if (decNumberIsNegative (&dn))
    return -1;
H.J. Lu committed
419
  else
Jon Grimm committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    return 1;
}

/* Helper to round_for_format, handling decimal float types.  */

void
decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
{
  decNumber dn;
  decContext set;

  /* Real encoding occurs later.  */
  if (r->cl != rvc_normal)
    return;

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;
  decimal128ToNumber ((decimal128 *) r->sig, &dn);

  if (fmt == &decimal_quad_format)
    {
      /* The internal format is already in this format.  */
      return;
    }
  else if (fmt == &decimal_single_format)
    {
      decimal32 d32;
      decContextDefault (&set, DEC_INIT_DECIMAL32);
      set.traps = 0;

      decimal32FromNumber (&d32, &dn, &set);
      decimal32ToNumber (&d32, &dn);
    }
  else if (fmt == &decimal_double_format)
    {
      decimal64 d64;
      decContextDefault (&set, DEC_INIT_DECIMAL64);
      set.traps = 0;

      decimal64FromNumber (&d64, &dn, &set);
      decimal64ToNumber (&d64, &dn);
    }
  else
    gcc_unreachable ();

  decimal_from_decnumber (r, &dn, &set);
}

/* Extend or truncate to a new mode.  Handles conversions between
   binary and decimal types.  */

void
472
decimal_real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
Jon Grimm committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
		      const REAL_VALUE_TYPE *a)
{
  const struct real_format *fmt = REAL_MODE_FORMAT (mode);

  if (a->decimal && fmt->b == 10)
    return;
  if (a->decimal)
      decimal_to_binary (r, a, mode);
  else
      decimal_from_binary (r, a);
}

/* Render R_ORIG as a decimal floating point constant.  Emit DIGITS
   significant digits in the result, bounded by BUF_SIZE.  If DIGITS
   is 0, choose the maximum for the representation.  If
   CROP_TRAILING_ZEROS, strip trailing zeros.  Currently, not honoring
   DIGITS or CROP_TRAILING_ZEROS.  */

491 492 493 494 495
void
decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
			 size_t buf_size,
			 size_t digits ATTRIBUTE_UNUSED,
			 int crop_trailing_zeros ATTRIBUTE_UNUSED)
Jon Grimm committed
496
{
497
  const decimal128 *const d128 = (const decimal128*) r_orig->sig;
Jon Grimm committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

  /* decimal128ToString requires space for at least 24 characters;
     Require two more for suffix.  */
  gcc_assert (buf_size >= 24);
  decimal128ToString (d128, str);
}

static bool
decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
		const REAL_VALUE_TYPE *op1, int subtract_p)
{
  decNumber dn;
  decContext set;
  decNumber dn2, dn3;

  decimal_to_decnumber (op0, &dn2);
  decimal_to_decnumber (op1, &dn3);

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  if (subtract_p)
    decNumberSubtract (&dn, &dn2, &dn3, &set);
H.J. Lu committed
521
  else
Jon Grimm committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
    decNumberAdd (&dn, &dn2, &dn3, &set);

  decimal_from_decnumber (r, &dn, &set);

  /* Return true, if inexact.  */
  return (set.status & DEC_Inexact);
}

/* Compute R = OP0 * OP1.  */

static bool
decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
		     const REAL_VALUE_TYPE *op1)
{
  decContext set;
  decNumber dn, dn2, dn3;

  decimal_to_decnumber (op0, &dn2);
  decimal_to_decnumber (op1, &dn3);

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  decNumberMultiply (&dn, &dn2, &dn3, &set);
  decimal_from_decnumber (r, &dn, &set);

  /* Return true, if inexact.  */
  return (set.status & DEC_Inexact);
}

/* Compute R = OP0 / OP1.  */

static bool
decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
		   const REAL_VALUE_TYPE *op1)
{
  decContext set;
  decNumber dn, dn2, dn3;

  decimal_to_decnumber (op0, &dn2);
  decimal_to_decnumber (op1, &dn3);

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;

  decNumberDivide (&dn, &dn2, &dn3, &set);
  decimal_from_decnumber (r, &dn, &set);

  /* Return true, if inexact.  */
  return (set.status & DEC_Inexact);
}

/* Set R to A truncated to an integral value toward zero (decimal
   floating point).  */

void
decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
{
  decNumber dn, dn2;
  decContext set;

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;
  set.round = DEC_ROUND_DOWN;
586
  decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
Jon Grimm committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

  decNumberToIntegralValue (&dn, &dn2, &set);
  decimal_from_decnumber (r, &dn, &set);
}

/* Render decimal float value R as an integer.  */

HOST_WIDE_INT
decimal_real_to_integer (const REAL_VALUE_TYPE *r)
{
  decContext set;
  decNumber dn, dn2, dn3;
  REAL_VALUE_TYPE to;
  char string[256];

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;
  set.round = DEC_ROUND_DOWN;
605
  decimal128ToNumber ((const decimal128 *) r->sig, &dn);
Jon Grimm committed
606 607 608 609 610 611 612 613 614 615 616 617

  decNumberToIntegralValue (&dn2, &dn, &set);
  decNumberZero (&dn3);
  decNumberRescale (&dn, &dn2, &dn3, &set);

  /* Convert to REAL_VALUE_TYPE and call appropriate conversion
     function.  */
  decNumberToString (&dn, string);
  real_from_string (&to, string);
  return real_to_integer (&to);
}

Kenneth Zadeck committed
618 619
/* Likewise, but returns a wide_int with PRECISION.  *FAIL is set if the
   value does not fit.  */
Jon Grimm committed
620

Kenneth Zadeck committed
621 622
wide_int
decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
Jon Grimm committed
623 624 625 626 627 628 629 630 631
{
  decContext set;
  decNumber dn, dn2, dn3;
  REAL_VALUE_TYPE to;
  char string[256];

  decContextDefault (&set, DEC_INIT_DECIMAL128);
  set.traps = 0;
  set.round = DEC_ROUND_DOWN;
632
  decimal128ToNumber ((const decimal128 *) r->sig, &dn);
Jon Grimm committed
633 634 635 636 637

  decNumberToIntegralValue (&dn2, &dn, &set);
  decNumberZero (&dn3);
  decNumberRescale (&dn, &dn2, &dn3, &set);

638
  /* Convert to REAL_VALUE_TYPE and call appropriate conversion
Jon Grimm committed
639 640 641
     function.  */
  decNumberToString (&dn, string);
  real_from_string (&to, string);
Kenneth Zadeck committed
642
  return real_to_integer (&to, fail, precision);
Jon Grimm committed
643 644
}

645 646 647
/* Perform the decimal floating point operation described by CODE.
   For a unary operation, OP1 will be NULL.  This function returns
   true if the result may be inexact due to loss of precision.  */
Jon Grimm committed
648 649

bool
650
decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
Jon Grimm committed
651 652 653
			 const REAL_VALUE_TYPE *op0,
			 const REAL_VALUE_TYPE *op1)
{
654
  REAL_VALUE_TYPE a, b;
Jon Grimm committed
655

656
  /* If either operand is non-decimal, create temporaries.  */
Jon Grimm committed
657 658
  if (!op0->decimal)
    {
659 660
      decimal_from_binary (&a, op0);
      op0 = &a;
Jon Grimm committed
661 662 663
    }
  if (op1 && !op1->decimal)
    {
664 665
      decimal_from_binary (&b, op1);
      op1 = &b;
Jon Grimm committed
666 667 668 669 670
    }

  switch (code)
    {
    case PLUS_EXPR:
671
      return decimal_do_add (r, op0, op1, 0);
Jon Grimm committed
672 673

    case MINUS_EXPR:
674
      return decimal_do_add (r, op0, op1, 1);
Jon Grimm committed
675 676

    case MULT_EXPR:
677
      return decimal_do_multiply (r, op0, op1);
Jon Grimm committed
678 679

    case RDIV_EXPR:
680
      return decimal_do_divide (r, op0, op1);
Jon Grimm committed
681 682 683 684 685 686 687 688

    case MIN_EXPR:
      if (op1->cl == rvc_nan)
        *r = *op1;
      else if (real_compare (UNLT_EXPR, op0, op1))
        *r = *op0;
      else
        *r = *op1;
689
      return false;
Jon Grimm committed
690 691 692 693 694 695 696 697

    case MAX_EXPR:
      if (op1->cl == rvc_nan)
        *r = *op1;
      else if (real_compare (LT_EXPR, op0, op1))
        *r = *op1;
      else
        *r = *op0;
698
      return false;
Jon Grimm committed
699 700 701 702

    case NEGATE_EXPR:
      {
	*r = *op0;
703 704
	/* Flip sign bit.  */
	decimal128FlipSign ((decimal128 *) r->sig);
Jon Grimm committed
705 706 707
	/* Keep sign field in sync.  */
	r->sign ^= 1;
      }
708
      return false;
Jon Grimm committed
709 710 711 712

    case ABS_EXPR:
      {
        *r = *op0;
713 714
	/* Clear sign bit.  */
	decimal128ClearSign ((decimal128 *) r->sig);
Jon Grimm committed
715 716 717
	/* Keep sign field in sync.  */
	r->sign = 0;
      }
718
      return false;
Jon Grimm committed
719 720 721

    case FIX_TRUNC_EXPR:
      decimal_do_fix_trunc (r, op0);
722
      return false;
Jon Grimm committed
723 724 725 726 727 728 729 730 731 732

    default:
      gcc_unreachable ();
    }
}

/* Fills R with the largest finite value representable in mode MODE.
   If SIGN is nonzero, R is set to the most negative finite value.  */

void
733
decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
H.J. Lu committed
734
{
735
  const char *max;
Jon Grimm committed
736 737 738 739

  switch (mode)
    {
    case SDmode:
740
      max = "9.999999E96";
Jon Grimm committed
741 742
      break;
    case DDmode:
743
      max = "9.999999999999999E384";
Jon Grimm committed
744 745
      break;
    case TDmode:
746
      max = "9.999999999999999999999999999999999E6144";
Jon Grimm committed
747 748 749 750 751 752 753
      break;
    default:
      gcc_unreachable ();
    }

  decimal_real_from_string (r, max);
  if (sign)
754
    decimal128SetSign ((decimal128 *) r->sig, 1);
Jon Grimm committed
755
}