]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/dfp.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / dfp.c
CommitLineData
909e2256 1/* Decimal floating point support.
5624e564 2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
909e2256
JG
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
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
909e2256
JG
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
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
909e2256
JG
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
40e23961 24#include "alias.h"
909e2256 25#include "tree.h"
909e2256
JG
26#include "tm_p.h"
27#include "dfp.h"
28
29/* The order of the following headers is important for making sure
30 decNumber structure is large enough to hold decimal128 digits. */
31
32#include "decimal128.h"
2533577f 33#include "decimal128Local.h"
909e2256
JG
34#include "decimal64.h"
35#include "decimal32.h"
36#include "decNumber.h"
37
bc6d4c3f
JM
38#ifndef WORDS_BIGENDIAN
39#define WORDS_BIGENDIAN 0
40#endif
41
909e2256
JG
42/* Initialize R (a real with the decimal flag set) from DN. Can
43 utilize status passed in via CONTEXT, if a previous operation had
44 interesting status. */
45
46static void
47decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
48{
49 memset (r, 0, sizeof (REAL_VALUE_TYPE));
50
51 r->cl = rvc_normal;
909e2256
JG
52 if (decNumberIsNaN (dn))
53 r->cl = rvc_nan;
54 if (decNumberIsInfinite (dn))
55 r->cl = rvc_inf;
56 if (context->status & DEC_Overflow)
57 r->cl = rvc_inf;
58 if (decNumberIsNegative (dn))
59 r->sign = 1;
60 r->decimal = 1;
61
62 if (r->cl != rvc_normal)
63 return;
64
65 decContextDefault (context, DEC_INIT_DECIMAL128);
66 context->traps = 0;
67
68 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
69}
70
71/* Create decimal encoded R from string S. */
72
73void
74decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
75{
76 decNumber dn;
77 decContext set;
78 decContextDefault (&set, DEC_INIT_DECIMAL128);
79 set.traps = 0;
80
5f754896 81 decNumberFromString (&dn, s, &set);
909e2256
JG
82
83 /* It would be more efficient to store directly in decNumber format,
84 but that is impractical from current data structure size.
85 Encoding as a decimal128 is much more compact. */
86 decimal_from_decnumber (r, &dn, &set);
87}
88
89/* Initialize a decNumber from a REAL_VALUE_TYPE. */
90
91static void
92decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
93{
94 decContext set;
95 decContextDefault (&set, DEC_INIT_DECIMAL128);
96 set.traps = 0;
97
98 switch (r->cl)
99 {
100 case rvc_zero:
101 decNumberZero (dn);
102 break;
103 case rvc_inf:
5f754896 104 decNumberFromString (dn, "Infinity", &set);
909e2256
JG
105 break;
106 case rvc_nan:
107 if (r->signalling)
5f754896 108 decNumberFromString (dn, "snan", &set);
909e2256 109 else
5f754896 110 decNumberFromString (dn, "nan", &set);
909e2256
JG
111 break;
112 case rvc_normal:
e7f78021
JJ
113 if (!r->decimal)
114 {
115 /* dconst{1,2,m1,half} are used in various places in
116 the middle-end and optimizers, allow them here
117 as an exception by converting them to decimal. */
118 if (memcmp (r, &dconst1, sizeof (*r)) == 0)
119 {
120 decNumberFromString (dn, "1", &set);
121 break;
122 }
123 if (memcmp (r, &dconst2, sizeof (*r)) == 0)
124 {
125 decNumberFromString (dn, "2", &set);
126 break;
127 }
128 if (memcmp (r, &dconstm1, sizeof (*r)) == 0)
129 {
130 decNumberFromString (dn, "-1", &set);
131 break;
132 }
133 if (memcmp (r, &dconsthalf, sizeof (*r)) == 0)
134 {
135 decNumberFromString (dn, "0.5", &set);
136 break;
137 }
138 gcc_unreachable ();
139 }
5f754896 140 decimal128ToNumber ((const decimal128 *) r->sig, dn);
909e2256
JG
141 break;
142 default:
143 gcc_unreachable ();
144 }
145
146 /* Fix up sign bit. */
147 if (r->sign != decNumberIsNegative (dn))
f64ad1d3 148 dn->bits ^= DECNEG;
909e2256
JG
149}
150
7292b8e4 151/* Encode a real into an IEEE 754 decimal32 type. */
909e2256 152
83f676b3 153void
909e2256
JG
154encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
155 long *buf, const REAL_VALUE_TYPE *r)
156{
157 decNumber dn;
158 decimal32 d32;
159 decContext set;
5a5c6435 160 int32_t image;
909e2256
JG
161
162 decContextDefault (&set, DEC_INIT_DECIMAL128);
163 set.traps = 0;
164
b8698a0f 165 decimal_to_decnumber (r, &dn);
909e2256
JG
166 decimal32FromNumber (&d32, &dn, &set);
167
5a5c6435
JJ
168 memcpy (&image, d32.bytes, sizeof (int32_t));
169 buf[0] = image;
909e2256
JG
170}
171
7292b8e4 172/* Decode an IEEE 754 decimal32 type into a real. */
909e2256 173
83f676b3
RS
174void
175decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
176 REAL_VALUE_TYPE *r, const long *buf)
909e2256
JG
177{
178 decNumber dn;
179 decimal32 d32;
180 decContext set;
5a5c6435 181 int32_t image;
909e2256
JG
182
183 decContextDefault (&set, DEC_INIT_DECIMAL128);
184 set.traps = 0;
185
5a5c6435
JJ
186 image = buf[0];
187 memcpy (&d32.bytes, &image, sizeof (int32_t));
909e2256
JG
188
189 decimal32ToNumber (&d32, &dn);
b8698a0f 190 decimal_from_decnumber (r, &dn, &set);
909e2256
JG
191}
192
7292b8e4 193/* Encode a real into an IEEE 754 decimal64 type. */
909e2256 194
83f676b3 195void
909e2256
JG
196encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
197 long *buf, const REAL_VALUE_TYPE *r)
198{
199 decNumber dn;
200 decimal64 d64;
201 decContext set;
5a5c6435 202 int32_t image;
909e2256
JG
203
204 decContextDefault (&set, DEC_INIT_DECIMAL128);
205 set.traps = 0;
206
207 decimal_to_decnumber (r, &dn);
208 decimal64FromNumber (&d64, &dn, &set);
209
bc6d4c3f
JM
210 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
211 {
5a5c6435
JJ
212 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
213 buf[0] = image;
214 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
215 buf[1] = image;
bc6d4c3f
JM
216 }
217 else
218 {
5a5c6435
JJ
219 memcpy (&image, &d64.bytes[4], sizeof (int32_t));
220 buf[0] = image;
221 memcpy (&image, &d64.bytes[0], sizeof (int32_t));
222 buf[1] = image;
bc6d4c3f 223 }
909e2256
JG
224}
225
7292b8e4 226/* Decode an IEEE 754 decimal64 type into a real. */
909e2256 227
83f676b3 228void
909e2256
JG
229decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
230 REAL_VALUE_TYPE *r, const long *buf)
b8698a0f 231{
909e2256
JG
232 decNumber dn;
233 decimal64 d64;
234 decContext set;
5a5c6435 235 int32_t image;
909e2256
JG
236
237 decContextDefault (&set, DEC_INIT_DECIMAL128);
238 set.traps = 0;
239
bc6d4c3f
JM
240 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
241 {
5a5c6435
JJ
242 image = buf[0];
243 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
244 image = buf[1];
245 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
bc6d4c3f
JM
246 }
247 else
248 {
5a5c6435
JJ
249 image = buf[1];
250 memcpy (&d64.bytes[0], &image, sizeof (int32_t));
251 image = buf[0];
252 memcpy (&d64.bytes[4], &image, sizeof (int32_t));
bc6d4c3f 253 }
909e2256
JG
254
255 decimal64ToNumber (&d64, &dn);
b8698a0f 256 decimal_from_decnumber (r, &dn, &set);
909e2256
JG
257}
258
7292b8e4 259/* Encode a real into an IEEE 754 decimal128 type. */
909e2256 260
83f676b3 261void
909e2256
JG
262encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
263 long *buf, const REAL_VALUE_TYPE *r)
264{
265 decNumber dn;
266 decContext set;
267 decimal128 d128;
5a5c6435 268 int32_t image;
909e2256
JG
269
270 decContextDefault (&set, DEC_INIT_DECIMAL128);
271 set.traps = 0;
272
273 decimal_to_decnumber (r, &dn);
274 decimal128FromNumber (&d128, &dn, &set);
275
bc6d4c3f
JM
276 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
277 {
5a5c6435
JJ
278 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
279 buf[0] = image;
280 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
281 buf[1] = image;
282 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
283 buf[2] = image;
284 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
285 buf[3] = image;
bc6d4c3f
JM
286 }
287 else
288 {
5a5c6435
JJ
289 memcpy (&image, &d128.bytes[12], sizeof (int32_t));
290 buf[0] = image;
291 memcpy (&image, &d128.bytes[8], sizeof (int32_t));
292 buf[1] = image;
293 memcpy (&image, &d128.bytes[4], sizeof (int32_t));
294 buf[2] = image;
295 memcpy (&image, &d128.bytes[0], sizeof (int32_t));
296 buf[3] = image;
bc6d4c3f 297 }
909e2256
JG
298}
299
7292b8e4 300/* Decode an IEEE 754 decimal128 type into a real. */
909e2256 301
83f676b3 302void
909e2256
JG
303decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
304 REAL_VALUE_TYPE *r, const long *buf)
305{
306 decNumber dn;
307 decimal128 d128;
308 decContext set;
5a5c6435 309 int32_t image;
909e2256
JG
310
311 decContextDefault (&set, DEC_INIT_DECIMAL128);
312 set.traps = 0;
313
bc6d4c3f
JM
314 if (WORDS_BIGENDIAN == FLOAT_WORDS_BIG_ENDIAN)
315 {
5a5c6435
JJ
316 image = buf[0];
317 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
318 image = buf[1];
319 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
320 image = buf[2];
321 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
322 image = buf[3];
323 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
bc6d4c3f
JM
324 }
325 else
326 {
5a5c6435
JJ
327 image = buf[3];
328 memcpy (&d128.bytes[0], &image, sizeof (int32_t));
329 image = buf[2];
330 memcpy (&d128.bytes[4], &image, sizeof (int32_t));
331 image = buf[1];
332 memcpy (&d128.bytes[8], &image, sizeof (int32_t));
333 image = buf[0];
334 memcpy (&d128.bytes[12], &image, sizeof (int32_t));
bc6d4c3f 335 }
909e2256
JG
336
337 decimal128ToNumber (&d128, &dn);
b8698a0f 338 decimal_from_decnumber (r, &dn, &set);
909e2256
JG
339}
340
341/* Helper function to convert from a binary real internal
342 representation. */
343
344static void
345decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
ef4bddc2 346 machine_mode mode)
909e2256
JG
347{
348 char string[256];
5f754896 349 const decimal128 *const d128 = (const decimal128 *) from->sig;
909e2256
JG
350
351 decimal128ToString (d128, string);
352 real_from_string3 (to, string, mode);
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
6416ae7f 370 representation including when one of the operands is still in the
909e2256
JG
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 }
b8698a0f 392
909e2256
JG
393 /* Convert into decNumber form for comparison operation. */
394 decContextDefault (&set, DEC_INIT_DECIMAL128);
b8698a0f 395 set.traps = 0;
5f754896
KG
396 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
397 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
909e2256
JG
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;
b8698a0f 409 else
909e2256
JG
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
ef4bddc2 462decimal_real_convert (REAL_VALUE_TYPE *r, machine_mode mode,
909e2256
JG
463 const REAL_VALUE_TYPE *a)
464{
465 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
466
467 if (a->decimal && fmt->b == 10)
468 return;
469 if (a->decimal)
470 decimal_to_binary (r, a, mode);
471 else
472 decimal_from_binary (r, a);
473}
474
475/* Render R_ORIG as a decimal floating point constant. Emit DIGITS
476 significant digits in the result, bounded by BUF_SIZE. If DIGITS
477 is 0, choose the maximum for the representation. If
478 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
479 DIGITS or CROP_TRAILING_ZEROS. */
480
83f676b3
RS
481void
482decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
483 size_t buf_size,
484 size_t digits ATTRIBUTE_UNUSED,
485 int crop_trailing_zeros ATTRIBUTE_UNUSED)
909e2256 486{
5f754896 487 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
909e2256
JG
488
489 /* decimal128ToString requires space for at least 24 characters;
490 Require two more for suffix. */
491 gcc_assert (buf_size >= 24);
492 decimal128ToString (d128, str);
493}
494
495static bool
496decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
497 const REAL_VALUE_TYPE *op1, int subtract_p)
498{
499 decNumber dn;
500 decContext set;
501 decNumber dn2, dn3;
502
503 decimal_to_decnumber (op0, &dn2);
504 decimal_to_decnumber (op1, &dn3);
505
506 decContextDefault (&set, DEC_INIT_DECIMAL128);
507 set.traps = 0;
508
509 if (subtract_p)
510 decNumberSubtract (&dn, &dn2, &dn3, &set);
b8698a0f 511 else
909e2256
JG
512 decNumberAdd (&dn, &dn2, &dn3, &set);
513
514 decimal_from_decnumber (r, &dn, &set);
515
516 /* Return true, if inexact. */
517 return (set.status & DEC_Inexact);
518}
519
520/* Compute R = OP0 * OP1. */
521
522static bool
523decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
524 const REAL_VALUE_TYPE *op1)
525{
526 decContext set;
527 decNumber dn, dn2, dn3;
528
529 decimal_to_decnumber (op0, &dn2);
530 decimal_to_decnumber (op1, &dn3);
531
532 decContextDefault (&set, DEC_INIT_DECIMAL128);
533 set.traps = 0;
534
535 decNumberMultiply (&dn, &dn2, &dn3, &set);
536 decimal_from_decnumber (r, &dn, &set);
537
538 /* Return true, if inexact. */
539 return (set.status & DEC_Inexact);
540}
541
542/* Compute R = OP0 / OP1. */
543
544static bool
545decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
546 const REAL_VALUE_TYPE *op1)
547{
548 decContext set;
549 decNumber dn, dn2, dn3;
550
551 decimal_to_decnumber (op0, &dn2);
552 decimal_to_decnumber (op1, &dn3);
553
554 decContextDefault (&set, DEC_INIT_DECIMAL128);
555 set.traps = 0;
556
557 decNumberDivide (&dn, &dn2, &dn3, &set);
558 decimal_from_decnumber (r, &dn, &set);
559
560 /* Return true, if inexact. */
561 return (set.status & DEC_Inexact);
562}
563
564/* Set R to A truncated to an integral value toward zero (decimal
565 floating point). */
566
567void
568decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
569{
570 decNumber dn, dn2;
571 decContext set;
572
573 decContextDefault (&set, DEC_INIT_DECIMAL128);
574 set.traps = 0;
575 set.round = DEC_ROUND_DOWN;
5f754896 576 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
909e2256
JG
577
578 decNumberToIntegralValue (&dn, &dn2, &set);
579 decimal_from_decnumber (r, &dn, &set);
580}
581
582/* Render decimal float value R as an integer. */
583
584HOST_WIDE_INT
585decimal_real_to_integer (const REAL_VALUE_TYPE *r)
586{
587 decContext set;
588 decNumber dn, dn2, dn3;
589 REAL_VALUE_TYPE to;
590 char string[256];
591
592 decContextDefault (&set, DEC_INIT_DECIMAL128);
593 set.traps = 0;
594 set.round = DEC_ROUND_DOWN;
5f754896 595 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
909e2256
JG
596
597 decNumberToIntegralValue (&dn2, &dn, &set);
598 decNumberZero (&dn3);
599 decNumberRescale (&dn, &dn2, &dn3, &set);
600
601 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
602 function. */
603 decNumberToString (&dn, string);
604 real_from_string (&to, string);
605 return real_to_integer (&to);
606}
607
807e902e
KZ
608/* Likewise, but returns a wide_int with PRECISION. *FAIL is set if the
609 value does not fit. */
909e2256 610
807e902e
KZ
611wide_int
612decimal_real_to_integer (const REAL_VALUE_TYPE *r, bool *fail, int precision)
909e2256
JG
613{
614 decContext set;
615 decNumber dn, dn2, dn3;
616 REAL_VALUE_TYPE to;
617 char string[256];
618
619 decContextDefault (&set, DEC_INIT_DECIMAL128);
620 set.traps = 0;
621 set.round = DEC_ROUND_DOWN;
5f754896 622 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
909e2256
JG
623
624 decNumberToIntegralValue (&dn2, &dn, &set);
625 decNumberZero (&dn3);
626 decNumberRescale (&dn, &dn2, &dn3, &set);
627
fa10beec 628 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
909e2256
JG
629 function. */
630 decNumberToString (&dn, string);
631 real_from_string (&to, string);
807e902e 632 return real_to_integer (&to, fail, precision);
909e2256
JG
633}
634
0b59f49d
BE
635/* Perform the decimal floating point operation described by CODE.
636 For a unary operation, OP1 will be NULL. This function returns
637 true if the result may be inexact due to loss of precision. */
909e2256
JG
638
639bool
0b59f49d 640decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
909e2256
JG
641 const REAL_VALUE_TYPE *op0,
642 const REAL_VALUE_TYPE *op1)
643{
0b59f49d 644 REAL_VALUE_TYPE a, b;
909e2256 645
0b59f49d 646 /* If either operand is non-decimal, create temporaries. */
909e2256
JG
647 if (!op0->decimal)
648 {
0b59f49d
BE
649 decimal_from_binary (&a, op0);
650 op0 = &a;
909e2256
JG
651 }
652 if (op1 && !op1->decimal)
653 {
0b59f49d
BE
654 decimal_from_binary (&b, op1);
655 op1 = &b;
909e2256
JG
656 }
657
658 switch (code)
659 {
660 case PLUS_EXPR:
0b59f49d 661 return decimal_do_add (r, op0, op1, 0);
909e2256
JG
662
663 case MINUS_EXPR:
0b59f49d 664 return decimal_do_add (r, op0, op1, 1);
909e2256
JG
665
666 case MULT_EXPR:
0b59f49d 667 return decimal_do_multiply (r, op0, op1);
909e2256
JG
668
669 case RDIV_EXPR:
0b59f49d 670 return decimal_do_divide (r, op0, op1);
909e2256
JG
671
672 case MIN_EXPR:
673 if (op1->cl == rvc_nan)
674 *r = *op1;
675 else if (real_compare (UNLT_EXPR, op0, op1))
676 *r = *op0;
677 else
678 *r = *op1;
0b59f49d 679 return false;
909e2256
JG
680
681 case MAX_EXPR:
682 if (op1->cl == rvc_nan)
683 *r = *op1;
684 else if (real_compare (LT_EXPR, op0, op1))
685 *r = *op1;
686 else
687 *r = *op0;
0b59f49d 688 return false;
909e2256
JG
689
690 case NEGATE_EXPR:
691 {
909e2256 692 *r = *op0;
79b87c74
MM
693 /* Flip sign bit. */
694 decimal128FlipSign ((decimal128 *) r->sig);
909e2256
JG
695 /* Keep sign field in sync. */
696 r->sign ^= 1;
697 }
0b59f49d 698 return false;
909e2256
JG
699
700 case ABS_EXPR:
701 {
909e2256 702 *r = *op0;
79b87c74
MM
703 /* Clear sign bit. */
704 decimal128ClearSign ((decimal128 *) r->sig);
909e2256
JG
705 /* Keep sign field in sync. */
706 r->sign = 0;
707 }
0b59f49d 708 return false;
909e2256
JG
709
710 case FIX_TRUNC_EXPR:
711 decimal_do_fix_trunc (r, op0);
0b59f49d 712 return false;
909e2256
JG
713
714 default:
715 gcc_unreachable ();
716 }
909e2256
JG
717}
718
719/* Fills R with the largest finite value representable in mode MODE.
720 If SIGN is nonzero, R is set to the most negative finite value. */
721
722void
ef4bddc2 723decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, machine_mode mode)
b8698a0f 724{
5f754896 725 const char *max;
909e2256
JG
726
727 switch (mode)
728 {
729 case SDmode:
5f754896 730 max = "9.999999E96";
909e2256
JG
731 break;
732 case DDmode:
5f754896 733 max = "9.999999999999999E384";
909e2256
JG
734 break;
735 case TDmode:
5f754896 736 max = "9.999999999999999999999999999999999E6144";
909e2256
JG
737 break;
738 default:
739 gcc_unreachable ();
740 }
741
742 decimal_real_from_string (r, max);
743 if (sign)
79b87c74 744 decimal128SetSign ((decimal128 *) r->sig, 1);
909e2256 745}