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
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
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/>. */
22 #include "coretypes.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. */
44 decimal_from_decnumber (REAL_VALUE_TYPE *r, decNumber *dn, decContext *context)
46 memset (r, 0, sizeof (REAL_VALUE_TYPE));
49 if (decNumberIsZero (dn))
51 if (decNumberIsNaN (dn))
53 if (decNumberIsInfinite (dn))
55 if (context->status & DEC_Overflow)
57 if (decNumberIsNegative (dn))
61 if (r->cl != rvc_normal)
64 decContextDefault (context, DEC_INIT_DECIMAL128);
67 decimal128FromNumber ((decimal128 *) r->sig, dn, context);
70 /* Create decimal encoded R from string S. */
73 decimal_real_from_string (REAL_VALUE_TYPE *r, const char *s)
77 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
91 decimal_to_decnumber (const REAL_VALUE_TYPE *r, decNumber *dn)
94 decContextDefault (&set, DEC_INIT_DECIMAL128);
103 decNumberFromString (dn, "Infinity", &set);
107 decNumberFromString (dn, "snan", &set);
109 decNumberFromString (dn, "nan", &set);
112 gcc_assert (r->decimal);
113 decimal128ToNumber ((const decimal128 *) r->sig, dn);
119 /* Fix up sign bit. */
120 if (r->sign != decNumberIsNegative (dn))
124 /* Encode a real into an IEEE 754R decimal32 type. */
127 encode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
128 long *buf, const REAL_VALUE_TYPE *r)
134 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
146 decode_decimal32 (const struct real_format *fmt ATTRIBUTE_UNUSED,
147 REAL_VALUE_TYPE *r, const long *buf)
153 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
165 encode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
166 long *buf, const REAL_VALUE_TYPE *r)
172 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
185 decode_decimal64 (const struct real_format *fmt ATTRIBUTE_UNUSED,
186 REAL_VALUE_TYPE *r, const long *buf)
192 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
205 encode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
206 long *buf, const REAL_VALUE_TYPE *r)
212 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
227 decode_decimal128 (const struct real_format *fmt ATTRIBUTE_UNUSED,
228 REAL_VALUE_TYPE *r, const long *buf)
234 decContextDefault (&set, DEC_INIT_DECIMAL128);
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
250 decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
251 enum machine_mode mode)
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
265 decimal_from_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from)
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,
283 decNumber dn, dn2, dn3;
284 REAL_VALUE_TYPE a1, b1;
286 /* If either operand is non-decimal, create temporary versions. */
289 decimal_from_binary (&a1, a);
294 decimal_from_binary (&b1, b);
298 /* Convert into decNumber form for comparison operation. */
299 decContextDefault (&set, DEC_INIT_DECIMAL128);
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))
310 else if (decNumberIsZero (&dn))
312 else if (decNumberIsNegative (&dn))
318 /* Helper to round_for_format, handling decimal float types. */
321 decimal_round_for_format (const struct real_format *fmt, REAL_VALUE_TYPE *r)
326 /* Real encoding occurs later. */
327 if (r->cl != rvc_normal)
330 decContextDefault (&set, DEC_INIT_DECIMAL128);
332 decimal128ToNumber ((decimal128 *) r->sig, &dn);
334 if (fmt == &decimal_quad_format)
336 /* The internal format is already in this format. */
339 else if (fmt == &decimal_single_format)
342 decContextDefault (&set, DEC_INIT_DECIMAL32);
345 decimal32FromNumber (&d32, &dn, &set);
346 decimal32ToNumber (&d32, &dn);
348 else if (fmt == &decimal_double_format)
351 decContextDefault (&set, DEC_INIT_DECIMAL64);
354 decimal64FromNumber (&d64, &dn, &set);
355 decimal64ToNumber (&d64, &dn);
360 decimal_from_decnumber (r, &dn, &set);
363 /* Extend or truncate to a new mode. Handles conversions between
364 binary and decimal types. */
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)
375 decimal_to_binary (r, a, mode);
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. */
387 decimal_real_to_decimal (char *str, const REAL_VALUE_TYPE *r_orig,
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);
401 decimal_do_add (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
402 const REAL_VALUE_TYPE *op1, int subtract_p)
408 decimal_to_decnumber (op0, &dn2);
409 decimal_to_decnumber (op1, &dn3);
411 decContextDefault (&set, DEC_INIT_DECIMAL128);
415 decNumberSubtract (&dn, &dn2, &dn3, &set);
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. */
428 decimal_do_multiply (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
429 const REAL_VALUE_TYPE *op1)
432 decNumber dn, dn2, dn3;
434 decimal_to_decnumber (op0, &dn2);
435 decimal_to_decnumber (op1, &dn3);
437 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
450 decimal_do_divide (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *op0,
451 const REAL_VALUE_TYPE *op1)
454 decNumber dn, dn2, dn3;
456 decimal_to_decnumber (op0, &dn2);
457 decimal_to_decnumber (op1, &dn3);
459 decContextDefault (&set, DEC_INIT_DECIMAL128);
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
473 decimal_do_fix_trunc (REAL_VALUE_TYPE *r, const REAL_VALUE_TYPE *a)
478 decContextDefault (&set, DEC_INIT_DECIMAL128);
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. */
490 decimal_real_to_integer (const REAL_VALUE_TYPE *r)
493 decNumber dn, dn2, dn3;
497 decContextDefault (&set, DEC_INIT_DECIMAL128);
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
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. */
516 decimal_real_to_integer2 (HOST_WIDE_INT *plow, HOST_WIDE_INT *phigh,
517 const REAL_VALUE_TYPE *r)
520 decNumber dn, dn2, dn3;
524 decContextDefault (&set, DEC_INIT_DECIMAL128);
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
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. */
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. */
554 decimal_from_binary (&a, op0);
557 if (op1 && !op1->decimal)
559 decimal_from_binary (&b, op1);
566 return decimal_do_add (r, op0, op1, 0);
569 return decimal_do_add (r, op0, op1, 1);
572 return decimal_do_multiply (r, op0, op1);
575 return decimal_do_divide (r, op0, op1);
578 if (op1->cl == rvc_nan)
580 else if (real_compare (UNLT_EXPR, op0, op1))
587 if (op1->cl == rvc_nan)
589 else if (real_compare (LT_EXPR, op0, op1))
599 decimal128FlipSign ((decimal128 *) r->sig);
600 /* Keep sign field in sync. */
608 /* Clear sign bit. */
609 decimal128ClearSign ((decimal128 *) r->sig);
610 /* Keep sign field in sync. */
616 decimal_do_fix_trunc (r, op0);
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. */
628 decimal_real_maxval (REAL_VALUE_TYPE *r, int sign, enum machine_mode mode)
638 max = "9.999999999999999E384";
641 max = "9.999999999999999999999999999999999E6144";
647 decimal_real_from_string (r, max);
649 decimal128SetSign ((decimal128 *) r->sig, 1);