2008-05-15 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / dfp.c
blob2526ef60c0fd2fbdb6ff9730c7566493849f561e
1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "toplev.h"
26 #include "real.h"
27 #include "tm_p.h"
28 #include "dfp.h"
30 /* The order of the following headers is important for making sure
31 decNumber structure is large enough to hold decimal128 digits. */
33 #include "decimal128.h"
34 #include "decimal128Local.h"
35 #include "decimal64.h"
36 #include "decimal32.h"
37 #include "decNumber.h"
39 /* Initialize R (a real with the decimal flag set) from DN. Can
40 utilize status passed in via CONTEXT, if a previous operation had
41 interesting status. */
43 static void
44 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
46 memset (r, 0, sizeof (REAL_VALUE_TYPE));
48 r->cl = rvc_normal;
49 if (decNumberIsNaN (dn))
50 r->cl = rvc_nan;
51 if (decNumberIsInfinite (dn))
52 r->cl = rvc_inf;
53 if (context->status & DEC_Overflow)
54 r->cl = rvc_inf;
55 if (decNumberIsNegative (dn))
56 r->sign = 1;
57 r->decimal = 1;
59 if (r->cl != rvc_normal)
60 return;
62 decContextDefault (context, DEC_INIT_DECIMAL128);
63 context->traps = 0;
65 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
68 /* Create decimal encoded R from string S. */
70 void
71 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
73 decNumber dn;
74 decContext set;
75 decContextDefault (&set, DEC_INIT_DECIMAL128);
76 set.traps = 0;
78 decNumberFromString (&dn, s, &set);
80 /* It would be more efficient to store directly in decNumber format,
81 but that is impractical from current data structure size.
82 Encoding as a decimal128 is much more compact. */
83 decimal_from_decnumber (r, &dn, &set);
86 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
88 static void
89 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
91 decContext set;
92 decContextDefault (&set, DEC_INIT_DECIMAL128);
93 set.traps = 0;
95 switch (r->cl)
97 case rvc_zero:
98 decNumberZero (dn);
99 break;
100 case rvc_inf:
101 decNumberFromString (dn, "Infinity", &set);
102 break;
103 case rvc_nan:
104 if (r->signalling)
105 decNumberFromString (dn, "snan", &set);
106 else
107 decNumberFromString (dn, "nan", &set);
108 break;
109 case rvc_normal:
110 gcc_assert (r->decimal);
111 decimal128ToNumber ((const decimal128 *) r->sig, dn);
112 break;
113 default:
114 gcc_unreachable ();
117 /* Fix up sign bit. */
118 if (r->sign != decNumberIsNegative (dn))
119 dn->bits ^= DECNEG;
122 /* Encode a real into an IEEE 754R decimal32 type. */
124 void
125 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
126 long *buf, const REAL_VALUE_TYPE *r)
128 decNumber dn;
129 decimal32 d32;
130 decContext set;
132 decContextDefault (&set, DEC_INIT_DECIMAL128);
133 set.traps = 0;
135 decimal_to_decnumber (r, &dn);
136 decimal32FromNumber (&d32, &dn, &set);
138 buf[0] = *(uint32_t *) d32.bytes;
141 /* Decode an IEEE 754R decimal32 type into a real. */
143 void
144 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
145 REAL_VALUE_TYPE *r, const long *buf)
147 decNumber dn;
148 decimal32 d32;
149 decContext set;
151 decContextDefault (&set, DEC_INIT_DECIMAL128);
152 set.traps = 0;
154 *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
156 decimal32ToNumber (&d32, &dn);
157 decimal_from_decnumber (r, &dn, &set);
160 /* Encode a real into an IEEE 754R decimal64 type. */
162 void
163 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
164 long *buf, const REAL_VALUE_TYPE *r)
166 decNumber dn;
167 decimal64 d64;
168 decContext set;
170 decContextDefault (&set, DEC_INIT_DECIMAL128);
171 set.traps = 0;
173 decimal_to_decnumber (r, &dn);
174 decimal64FromNumber (&d64, &dn, &set);
176 buf[0] = *(uint32_t *) &d64.bytes[0];
177 buf[1] = *(uint32_t *) &d64.bytes[4];
180 /* Decode an IEEE 754R decimal64 type into a real. */
182 void
183 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
184 REAL_VALUE_TYPE *r, const long *buf)
186 decNumber dn;
187 decimal64 d64;
188 decContext set;
190 decContextDefault (&set, DEC_INIT_DECIMAL128);
191 set.traps = 0;
193 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
194 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
196 decimal64ToNumber (&d64, &dn);
197 decimal_from_decnumber (r, &dn, &set);
200 /* Encode a real into an IEEE 754R decimal128 type. */
202 void
203 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
204 long *buf, const REAL_VALUE_TYPE *r)
206 decNumber dn;
207 decContext set;
208 decimal128 d128;
210 decContextDefault (&set, DEC_INIT_DECIMAL128);
211 set.traps = 0;
213 decimal_to_decnumber (r, &dn);
214 decimal128FromNumber (&d128, &dn, &set);
216 buf[0] = *(uint32_t *) &d128.bytes[0];
217 buf[1] = *(uint32_t *) &d128.bytes[4];
218 buf[2] = *(uint32_t *) &d128.bytes[8];
219 buf[3] = *(uint32_t *) &d128.bytes[12];
222 /* Decode an IEEE 754R decimal128 type into a real. */
224 void
225 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
226 REAL_VALUE_TYPE *r, const long *buf)
228 decNumber dn;
229 decimal128 d128;
230 decContext set;
232 decContextDefault (&set, DEC_INIT_DECIMAL128);
233 set.traps = 0;
235 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
236 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
237 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
238 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
240 decimal128ToNumber (&d128, &dn);
241 decimal_from_decnumber (r, &dn, &set);
244 /* Helper function to convert from a binary real internal
245 representation. */
247 static void
248 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
249 enum machine_mode mode)
251 char string[256];
252 const decimal128 *const d128 = (const decimal128 *) from->sig;
254 decimal128ToString (d128, string);
255 real_from_string3 (to, string, mode);
259 /* Helper function to convert from a binary real internal
260 representation. */
262 static void
263 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
265 char string[256];
267 /* We convert to string, then to decNumber then to decimal128. */
268 real_to_decimal (string, from, sizeof (string), 0, 1);
269 decimal_real_from_string (to, string);
272 /* Helper function to real.c:do_compare() to handle decimal internal
273 representation including when one of the operands is still in the
274 binary internal representation. */
277 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
278 int nan_result)
280 decContext set;
281 decNumber dn, dn2, dn3;
282 REAL_VALUE_TYPE a1, b1;
284 /* If either operand is non-decimal, create temporary versions. */
285 if (!a->decimal)
287 decimal_from_binary (&a1, a);
288 a = &a1;
290 if (!b->decimal)
292 decimal_from_binary (&b1, b);
293 b = &b1;
296 /* Convert into decNumber form for comparison operation. */
297 decContextDefault (&set, DEC_INIT_DECIMAL128);
298 set.traps = 0;
299 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
300 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
302 /* Finally, do the comparison. */
303 decNumberCompare (&dn, &dn2, &dn3, &set);
305 /* Return the comparison result. */
306 if (decNumberIsNaN (&dn))
307 return nan_result;
308 else if (decNumberIsZero (&dn))
309 return 0;
310 else if (decNumberIsNegative (&dn))
311 return -1;
312 else
313 return 1;
316 /* Helper to round_for_format, handling decimal float types. */
318 void
319 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
321 decNumber dn;
322 decContext set;
324 /* Real encoding occurs later. */
325 if (r->cl != rvc_normal)
326 return;
328 decContextDefault (&set, DEC_INIT_DECIMAL128);
329 set.traps = 0;
330 decimal128ToNumber ((decimal128 *) r->sig, &dn);
332 if (fmt == &decimal_quad_format)
334 /* The internal format is already in this format. */
335 return;
337 else if (fmt == &decimal_single_format)
339 decimal32 d32;
340 decContextDefault (&set, DEC_INIT_DECIMAL32);
341 set.traps = 0;
343 decimal32FromNumber (&d32, &dn, &set);
344 decimal32ToNumber (&d32, &dn);
346 else if (fmt == &decimal_double_format)
348 decimal64 d64;
349 decContextDefault (&set, DEC_INIT_DECIMAL64);
350 set.traps = 0;
352 decimal64FromNumber (&d64, &dn, &set);
353 decimal64ToNumber (&d64, &dn);
355 else
356 gcc_unreachable ();
358 decimal_from_decnumber (r, &dn, &set);
361 /* Extend or truncate to a new mode. Handles conversions between
362 binary and decimal types. */
364 void
365 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
366 const REAL_VALUE_TYPE *a)
368 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
370 if (a->decimal && fmt->b == 10)
371 return;
372 if (a->decimal)
373 decimal_to_binary (r, a, mode);
374 else
375 decimal_from_binary (r, a);
378 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
379 significant digits in the result, bounded by BUF_SIZE. If DIGITS
380 is 0, choose the maximum for the representation. If
381 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
382 DIGITS or CROP_TRAILING_ZEROS. */
384 void
385 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
386 size_t buf_size,
387 size_t digits ATTRIBUTE_UNUSED,
388 int crop_trailing_zeros ATTRIBUTE_UNUSED)
390 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
392 /* decimal128ToString requires space for at least 24 characters;
393 Require two more for suffix. */
394 gcc_assert (buf_size >= 24);
395 decimal128ToString (d128, str);
398 static bool
399 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
400 const REAL_VALUE_TYPE *op1, int subtract_p)
402 decNumber dn;
403 decContext set;
404 decNumber dn2, dn3;
406 decimal_to_decnumber (op0, &dn2);
407 decimal_to_decnumber (op1, &dn3);
409 decContextDefault (&set, DEC_INIT_DECIMAL128);
410 set.traps = 0;
412 if (subtract_p)
413 decNumberSubtract (&dn, &dn2, &dn3, &set);
414 else
415 decNumberAdd (&dn, &dn2, &dn3, &set);
417 decimal_from_decnumber (r, &dn, &set);
419 /* Return true, if inexact. */
420 return (set.status & DEC_Inexact);
423 /* Compute R = OP0 * OP1. */
425 static bool
426 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
427 const REAL_VALUE_TYPE *op1)
429 decContext set;
430 decNumber dn, dn2, dn3;
432 decimal_to_decnumber (op0, &dn2);
433 decimal_to_decnumber (op1, &dn3);
435 decContextDefault (&set, DEC_INIT_DECIMAL128);
436 set.traps = 0;
438 decNumberMultiply (&dn, &dn2, &dn3, &set);
439 decimal_from_decnumber (r, &dn, &set);
441 /* Return true, if inexact. */
442 return (set.status & DEC_Inexact);
445 /* Compute R = OP0 / OP1. */
447 static bool
448 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
449 const REAL_VALUE_TYPE *op1)
451 decContext set;
452 decNumber dn, dn2, dn3;
454 decimal_to_decnumber (op0, &dn2);
455 decimal_to_decnumber (op1, &dn3);
457 decContextDefault (&set, DEC_INIT_DECIMAL128);
458 set.traps = 0;
460 decNumberDivide (&dn, &dn2, &dn3, &set);
461 decimal_from_decnumber (r, &dn, &set);
463 /* Return true, if inexact. */
464 return (set.status & DEC_Inexact);
467 /* Set R to A truncated to an integral value toward zero (decimal
468 floating point). */
470 void
471 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
473 decNumber dn, dn2;
474 decContext set;
476 decContextDefault (&set, DEC_INIT_DECIMAL128);
477 set.traps = 0;
478 set.round = DEC_ROUND_DOWN;
479 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
481 decNumberToIntegralValue (&dn, &dn2, &set);
482 decimal_from_decnumber (r, &dn, &set);
485 /* Render decimal float value R as an integer. */
487 HOST_WIDE_INT
488 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
490 decContext set;
491 decNumber dn, dn2, dn3;
492 REAL_VALUE_TYPE to;
493 char string[256];
495 decContextDefault (&set, DEC_INIT_DECIMAL128);
496 set.traps = 0;
497 set.round = DEC_ROUND_DOWN;
498 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
500 decNumberToIntegralValue (&dn2, &dn, &set);
501 decNumberZero (&dn3);
502 decNumberRescale (&dn, &dn2, &dn3, &set);
504 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
505 function. */
506 decNumberToString (&dn, string);
507 real_from_string (&to, string);
508 return real_to_integer (&to);
511 /* Likewise, but to an integer pair, HI+LOW. */
513 void
514 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
515 const REAL_VALUE_TYPE *r)
517 decContext set;
518 decNumber dn, dn2, dn3;
519 REAL_VALUE_TYPE to;
520 char string[256];
522 decContextDefault (&set, DEC_INIT_DECIMAL128);
523 set.traps = 0;
524 set.round = DEC_ROUND_DOWN;
525 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
527 decNumberToIntegralValue (&dn2, &dn, &set);
528 decNumberZero (&dn3);
529 decNumberRescale (&dn, &dn2, &dn3, &set);
531 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
532 function. */
533 decNumberToString (&dn, string);
534 real_from_string (&to, string);
535 real_to_integer2 (plow, phigh, &to);
538 /* Perform the decimal floating point operation described by CODE.
539 For a unary operation, OP1 will be NULL. This function returns
540 true if the result may be inexact due to loss of precision. */
542 bool
543 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
544 const REAL_VALUE_TYPE *op0,
545 const REAL_VALUE_TYPE *op1)
547 REAL_VALUE_TYPE a, b;
549 /* If either operand is non-decimal, create temporaries. */
550 if (!op0->decimal)
552 decimal_from_binary (&a, op0);
553 op0 = &a;
555 if (op1 && !op1->decimal)
557 decimal_from_binary (&b, op1);
558 op1 = &b;
561 switch (code)
563 case PLUS_EXPR:
564 return decimal_do_add (r, op0, op1, 0);
566 case MINUS_EXPR:
567 return decimal_do_add (r, op0, op1, 1);
569 case MULT_EXPR:
570 return decimal_do_multiply (r, op0, op1);
572 case RDIV_EXPR:
573 return decimal_do_divide (r, op0, op1);
575 case MIN_EXPR:
576 if (op1->cl == rvc_nan)
577 *r = *op1;
578 else if (real_compare (UNLT_EXPR, op0, op1))
579 *r = *op0;
580 else
581 *r = *op1;
582 return false;
584 case MAX_EXPR:
585 if (op1->cl == rvc_nan)
586 *r = *op1;
587 else if (real_compare (LT_EXPR, op0, op1))
588 *r = *op1;
589 else
590 *r = *op0;
591 return false;
593 case NEGATE_EXPR:
595 *r = *op0;
596 /* Flip sign bit. */
597 decimal128FlipSign ((decimal128 *) r->sig);
598 /* Keep sign field in sync. */
599 r->sign ^= 1;
601 return false;
603 case ABS_EXPR:
605 *r = *op0;
606 /* Clear sign bit. */
607 decimal128ClearSign ((decimal128 *) r->sig);
608 /* Keep sign field in sync. */
609 r->sign = 0;
611 return false;
613 case FIX_TRUNC_EXPR:
614 decimal_do_fix_trunc (r, op0);
615 return false;
617 default:
618 gcc_unreachable ();
622 /* Fills R with the largest finite value representable in mode MODE.
623 If SIGN is nonzero, R is set to the most negative finite value. */
625 void
626 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
628 const char *max;
630 switch (mode)
632 case SDmode:
633 max = "9.999999E96";
634 break;
635 case DDmode:
636 max = "9.999999999999999E384";
637 break;
638 case TDmode:
639 max = "9.999999999999999999999999999999999E6144";
640 break;
641 default:
642 gcc_unreachable ();
645 decimal_real_from_string (r, max);
646 if (sign)
647 decimal128SetSign ((decimal128 *) r->sig, 1);