2008-01-22 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / dfp.c
blob88ffdedbcfb41baf5272a9a3616801e495ff070f
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 (decNumberIsZero (dn))
50 r->cl = rvc_zero;
51 if (decNumberIsNaN (dn))
52 r->cl = rvc_nan;
53 if (decNumberIsInfinite (dn))
54 r->cl = rvc_inf;
55 if (context->status & DEC_Overflow)
56 r->cl = rvc_inf;
57 if (decNumberIsNegative (dn))
58 r->sign = 1;
59 r->decimal = 1;
61 if (r->cl != rvc_normal)
62 return;
64 decContextDefault (context, DEC_INIT_DECIMAL128);
65 context->traps = 0;
67 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
70 /* Create decimal encoded R from string S. */
72 void
73 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
75 decNumber dn;
76 decContext set;
77 decContextDefault (&set, DEC_INIT_DECIMAL128);
78 set.traps = 0;
80 decNumberFromString (&dn, s, &set);
82 /* It would be more efficient to store directly in decNumber format,
83 but that is impractical from current data structure size.
84 Encoding as a decimal128 is much more compact. */
85 decimal_from_decnumber (r, &dn, &set);
88 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
90 static void
91 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
93 decContext set;
94 decContextDefault (&set, DEC_INIT_DECIMAL128);
95 set.traps = 0;
97 switch (r->cl)
99 case rvc_zero:
100 decNumberZero (dn);
101 break;
102 case rvc_inf:
103 decNumberFromString (dn, "Infinity", &set);
104 break;
105 case rvc_nan:
106 if (r->signalling)
107 decNumberFromString (dn, "snan", &set);
108 else
109 decNumberFromString (dn, "nan", &set);
110 break;
111 case rvc_normal:
112 gcc_assert (r->decimal);
113 decimal128ToNumber ((const decimal128 *) r->sig, dn);
114 break;
115 default:
116 gcc_unreachable ();
119 /* Fix up sign bit. */
120 if (r->sign != decNumberIsNegative (dn))
121 dn->bits ^= DECNEG;
124 /* Encode a real into an IEEE 754R decimal32 type. */
126 void
127 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
128 long *buf, const REAL_VALUE_TYPE *r)
130 decNumber dn;
131 decimal32 d32;
132 decContext set;
134 decContextDefault (&set, DEC_INIT_DECIMAL128);
135 set.traps = 0;
137 decimal_to_decnumber (r, &dn);
138 decimal32FromNumber (&d32, &dn, &set);
140 buf[0] = *(uint32_t *) d32.bytes;
143 /* Decode an IEEE 754R decimal32 type into a real. */
145 void
146 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
147 REAL_VALUE_TYPE *r, const long *buf)
149 decNumber dn;
150 decimal32 d32;
151 decContext set;
153 decContextDefault (&set, DEC_INIT_DECIMAL128);
154 set.traps = 0;
156 *((uint32_t *) d32.bytes) = (uint32_t) buf[0];
158 decimal32ToNumber (&d32, &dn);
159 decimal_from_decnumber (r, &dn, &set);
162 /* Encode a real into an IEEE 754R decimal64 type. */
164 void
165 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
166 long *buf, const REAL_VALUE_TYPE *r)
168 decNumber dn;
169 decimal64 d64;
170 decContext set;
172 decContextDefault (&set, DEC_INIT_DECIMAL128);
173 set.traps = 0;
175 decimal_to_decnumber (r, &dn);
176 decimal64FromNumber (&d64, &dn, &set);
178 buf[0] = *(uint32_t *) &d64.bytes[0];
179 buf[1] = *(uint32_t *) &d64.bytes[4];
182 /* Decode an IEEE 754R decimal64 type into a real. */
184 void
185 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
186 REAL_VALUE_TYPE *r, const long *buf)
188 decNumber dn;
189 decimal64 d64;
190 decContext set;
192 decContextDefault (&set, DEC_INIT_DECIMAL128);
193 set.traps = 0;
195 *((uint32_t *) &d64.bytes[0]) = (uint32_t) buf[0];
196 *((uint32_t *) &d64.bytes[4]) = (uint32_t) buf[1];
198 decimal64ToNumber (&d64, &dn);
199 decimal_from_decnumber (r, &dn, &set);
202 /* Encode a real into an IEEE 754R decimal128 type. */
204 void
205 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
206 long *buf, const REAL_VALUE_TYPE *r)
208 decNumber dn;
209 decContext set;
210 decimal128 d128;
212 decContextDefault (&set, DEC_INIT_DECIMAL128);
213 set.traps = 0;
215 decimal_to_decnumber (r, &dn);
216 decimal128FromNumber (&d128, &dn, &set);
218 buf[0] = *(uint32_t *) &d128.bytes[0];
219 buf[1] = *(uint32_t *) &d128.bytes[4];
220 buf[2] = *(uint32_t *) &d128.bytes[8];
221 buf[3] = *(uint32_t *) &d128.bytes[12];
224 /* Decode an IEEE 754R decimal128 type into a real. */
226 void
227 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
228 REAL_VALUE_TYPE *r, const long *buf)
230 decNumber dn;
231 decimal128 d128;
232 decContext set;
234 decContextDefault (&set, DEC_INIT_DECIMAL128);
235 set.traps = 0;
237 *((uint32_t *) &d128.bytes[0]) = (uint32_t) buf[0];
238 *((uint32_t *) &d128.bytes[4]) = (uint32_t) buf[1];
239 *((uint32_t *) &d128.bytes[8]) = (uint32_t) buf[2];
240 *((uint32_t *) &d128.bytes[12]) = (uint32_t) buf[3];
242 decimal128ToNumber (&d128, &dn);
243 decimal_from_decnumber (r, &dn, &set);
246 /* Helper function to convert from a binary real internal
247 representation. */
249 static void
250 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
251 enum machine_mode mode)
253 char string[256];
254 const decimal128 *const d128 = (const decimal128 *) from->sig;
256 decimal128ToString (d128, string);
257 real_from_string3 (to, string, mode);
261 /* Helper function to convert from a binary real internal
262 representation. */
264 static void
265 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
267 char string[256];
269 /* We convert to string, then to decNumber then to decimal128. */
270 real_to_decimal (string, from, sizeof (string), 0, 1);
271 decimal_real_from_string (to, string);
274 /* Helper function to real.c:do_compare() to handle decimal internal
275 representation including when one of the operands is still in the
276 binary internal representation. */
279 decimal_do_compare (const REAL_VALUE_TYPE *a, const REAL_VALUE_TYPE *b,
280 int nan_result)
282 decContext set;
283 decNumber dn, dn2, dn3;
284 REAL_VALUE_TYPE a1, b1;
286 /* If either operand is non-decimal, create temporary versions. */
287 if (!a->decimal)
289 decimal_from_binary (&a1, a);
290 a = &a1;
292 if (!b->decimal)
294 decimal_from_binary (&b1, b);
295 b = &b1;
298 /* Convert into decNumber form for comparison operation. */
299 decContextDefault (&set, DEC_INIT_DECIMAL128);
300 set.traps = 0;
301 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
302 decimal128ToNumber ((const decimal128 *) b->sig, &dn3);
304 /* Finally, do the comparison. */
305 decNumberCompare (&dn, &dn2, &dn3, &set);
307 /* Return the comparison result. */
308 if (decNumberIsNaN (&dn))
309 return nan_result;
310 else if (decNumberIsZero (&dn))
311 return 0;
312 else if (decNumberIsNegative (&dn))
313 return -1;
314 else
315 return 1;
318 /* Helper to round_for_format, handling decimal float types. */
320 void
321 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
323 decNumber dn;
324 decContext set;
326 /* Real encoding occurs later. */
327 if (r->cl != rvc_normal)
328 return;
330 decContextDefault (&set, DEC_INIT_DECIMAL128);
331 set.traps = 0;
332 decimal128ToNumber ((decimal128 *) r->sig, &dn);
334 if (fmt == &decimal_quad_format)
336 /* The internal format is already in this format. */
337 return;
339 else if (fmt == &decimal_single_format)
341 decimal32 d32;
342 decContextDefault (&set, DEC_INIT_DECIMAL32);
343 set.traps = 0;
345 decimal32FromNumber (&d32, &dn, &set);
346 decimal32ToNumber (&d32, &dn);
348 else if (fmt == &decimal_double_format)
350 decimal64 d64;
351 decContextDefault (&set, DEC_INIT_DECIMAL64);
352 set.traps = 0;
354 decimal64FromNumber (&d64, &dn, &set);
355 decimal64ToNumber (&d64, &dn);
357 else
358 gcc_unreachable ();
360 decimal_from_decnumber (r, &dn, &set);
363 /* Extend or truncate to a new mode. Handles conversions between
364 binary and decimal types. */
366 void
367 decimal_real_convert (REAL_VALUE_TYPE *r, enum machine_mode mode,
368 const REAL_VALUE_TYPE *a)
370 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
372 if (a->decimal && fmt->b == 10)
373 return;
374 if (a->decimal)
375 decimal_to_binary (r, a, mode);
376 else
377 decimal_from_binary (r, a);
380 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
381 significant digits in the result, bounded by BUF_SIZE. If DIGITS
382 is 0, choose the maximum for the representation. If
383 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
384 DIGITS or CROP_TRAILING_ZEROS. */
386 void
387 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
388 size_t buf_size,
389 size_t digits ATTRIBUTE_UNUSED,
390 int crop_trailing_zeros ATTRIBUTE_UNUSED)
392 const decimal128 *const d128 = (const decimal128*) r_orig->sig;
394 /* decimal128ToString requires space for at least 24 characters;
395 Require two more for suffix. */
396 gcc_assert (buf_size >= 24);
397 decimal128ToString (d128, str);
400 static bool
401 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
402 const REAL_VALUE_TYPE *op1, int subtract_p)
404 decNumber dn;
405 decContext set;
406 decNumber dn2, dn3;
408 decimal_to_decnumber (op0, &dn2);
409 decimal_to_decnumber (op1, &dn3);
411 decContextDefault (&set, DEC_INIT_DECIMAL128);
412 set.traps = 0;
414 if (subtract_p)
415 decNumberSubtract (&dn, &dn2, &dn3, &set);
416 else
417 decNumberAdd (&dn, &dn2, &dn3, &set);
419 decimal_from_decnumber (r, &dn, &set);
421 /* Return true, if inexact. */
422 return (set.status & DEC_Inexact);
425 /* Compute R = OP0 * OP1. */
427 static bool
428 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
429 const REAL_VALUE_TYPE *op1)
431 decContext set;
432 decNumber dn, dn2, dn3;
434 decimal_to_decnumber (op0, &dn2);
435 decimal_to_decnumber (op1, &dn3);
437 decContextDefault (&set, DEC_INIT_DECIMAL128);
438 set.traps = 0;
440 decNumberMultiply (&dn, &dn2, &dn3, &set);
441 decimal_from_decnumber (r, &dn, &set);
443 /* Return true, if inexact. */
444 return (set.status & DEC_Inexact);
447 /* Compute R = OP0 / OP1. */
449 static bool
450 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
451 const REAL_VALUE_TYPE *op1)
453 decContext set;
454 decNumber dn, dn2, dn3;
456 decimal_to_decnumber (op0, &dn2);
457 decimal_to_decnumber (op1, &dn3);
459 decContextDefault (&set, DEC_INIT_DECIMAL128);
460 set.traps = 0;
462 decNumberDivide (&dn, &dn2, &dn3, &set);
463 decimal_from_decnumber (r, &dn, &set);
465 /* Return true, if inexact. */
466 return (set.status & DEC_Inexact);
469 /* Set R to A truncated to an integral value toward zero (decimal
470 floating point). */
472 void
473 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
475 decNumber dn, dn2;
476 decContext set;
478 decContextDefault (&set, DEC_INIT_DECIMAL128);
479 set.traps = 0;
480 set.round = DEC_ROUND_DOWN;
481 decimal128ToNumber ((const decimal128 *) a->sig, &dn2);
483 decNumberToIntegralValue (&dn, &dn2, &set);
484 decimal_from_decnumber (r, &dn, &set);
487 /* Render decimal float value R as an integer. */
489 HOST_WIDE_INT
490 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
492 decContext set;
493 decNumber dn, dn2, dn3;
494 REAL_VALUE_TYPE to;
495 char string[256];
497 decContextDefault (&set, DEC_INIT_DECIMAL128);
498 set.traps = 0;
499 set.round = DEC_ROUND_DOWN;
500 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
502 decNumberToIntegralValue (&dn2, &dn, &set);
503 decNumberZero (&dn3);
504 decNumberRescale (&dn, &dn2, &dn3, &set);
506 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
507 function. */
508 decNumberToString (&dn, string);
509 real_from_string (&to, string);
510 return real_to_integer (&to);
513 /* Likewise, but to an integer pair, HI+LOW. */
515 void
516 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
517 const REAL_VALUE_TYPE *r)
519 decContext set;
520 decNumber dn, dn2, dn3;
521 REAL_VALUE_TYPE to;
522 char string[256];
524 decContextDefault (&set, DEC_INIT_DECIMAL128);
525 set.traps = 0;
526 set.round = DEC_ROUND_DOWN;
527 decimal128ToNumber ((const decimal128 *) r->sig, &dn);
529 decNumberToIntegralValue (&dn2, &dn, &set);
530 decNumberZero (&dn3);
531 decNumberRescale (&dn, &dn2, &dn3, &set);
533 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
534 function. */
535 decNumberToString (&dn, string);
536 real_from_string (&to, string);
537 real_to_integer2 (plow, phigh, &to);
540 /* Perform the decimal floating point operation described by CODE.
541 For a unary operation, OP1 will be NULL. This function returns
542 true if the result may be inexact due to loss of precision. */
544 bool
545 decimal_real_arithmetic (REAL_VALUE_TYPE *r, enum tree_code code,
546 const REAL_VALUE_TYPE *op0,
547 const REAL_VALUE_TYPE *op1)
549 REAL_VALUE_TYPE a, b;
551 /* If either operand is non-decimal, create temporaries. */
552 if (!op0->decimal)
554 decimal_from_binary (&a, op0);
555 op0 = &a;
557 if (op1 && !op1->decimal)
559 decimal_from_binary (&b, op1);
560 op1 = &b;
563 switch (code)
565 case PLUS_EXPR:
566 return decimal_do_add (r, op0, op1, 0);
568 case MINUS_EXPR:
569 return decimal_do_add (r, op0, op1, 1);
571 case MULT_EXPR:
572 return decimal_do_multiply (r, op0, op1);
574 case RDIV_EXPR:
575 return decimal_do_divide (r, op0, op1);
577 case MIN_EXPR:
578 if (op1->cl == rvc_nan)
579 *r = *op1;
580 else if (real_compare (UNLT_EXPR, op0, op1))
581 *r = *op0;
582 else
583 *r = *op1;
584 return false;
586 case MAX_EXPR:
587 if (op1->cl == rvc_nan)
588 *r = *op1;
589 else if (real_compare (LT_EXPR, op0, op1))
590 *r = *op1;
591 else
592 *r = *op0;
593 return false;
595 case NEGATE_EXPR:
597 *r = *op0;
598 /* Flip sign bit. */
599 decimal128FlipSign ((decimal128 *) r->sig);
600 /* Keep sign field in sync. */
601 r->sign ^= 1;
603 return false;
605 case ABS_EXPR:
607 *r = *op0;
608 /* Clear sign bit. */
609 decimal128ClearSign ((decimal128 *) r->sig);
610 /* Keep sign field in sync. */
611 r->sign = 0;
613 return false;
615 case FIX_TRUNC_EXPR:
616 decimal_do_fix_trunc (r, op0);
617 return false;
619 default:
620 gcc_unreachable ();
624 /* Fills R with the largest finite value representable in mode MODE.
625 If SIGN is nonzero, R is set to the most negative finite value. */
627 void
628 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
630 const char *max;
632 switch (mode)
634 case SDmode:
635 max = "9.999999E96";
636 break;
637 case DDmode:
638 max = "9.999999999999999E384";
639 break;
640 case TDmode:
641 max = "9.999999999999999999999999999999999E6144";
642 break;
643 default:
644 gcc_unreachable ();
647 decimal_real_from_string (r, max);
648 if (sign)
649 decimal128SetSign ((decimal128 *) r->sig, 1);