1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 /* The order of the following headers is important for making sure
32 decNumber structure is large enough to hold decimal128 digits. */
34 #include "decimal128.h"
35 #include "decimal128Local.h"
36 #include "decimal64.h"
37 #include "decimal32.h"
38 #include "decNumber.h"
40 #ifndef WORDS_BIGENDIAN
41 #define WORDS_BIGENDIAN 0
44 /* Initialize R (a real with the decimal flag set) from DN. Can
45 utilize status passed in via CONTEXT, if a previous operation had
46 interesting status. */
49 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
51 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
54 if (decNumberIsNaN (dn
))
56 if (decNumberIsInfinite (dn
))
58 if (context
->status
& DEC_Overflow
)
60 if (decNumberIsNegative (dn
))
64 if (r
->cl
!= rvc_normal
)
67 decContextDefault (context
, DEC_INIT_DECIMAL128
);
70 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
73 /* Create decimal encoded R from string S. */
76 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
80 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
83 decNumberFromString (&dn
, s
, &set
);
85 /* It would be more efficient to store directly in decNumber format,
86 but that is impractical from current data structure size.
87 Encoding as a decimal128 is much more compact. */
88 decimal_from_decnumber (r
, &dn
, &set
);
91 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
94 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
97 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
106 decNumberFromString (dn
, "Infinity", &set
);
110 decNumberFromString (dn
, "snan", &set
);
112 decNumberFromString (dn
, "nan", &set
);
115 gcc_assert (r
->decimal
);
116 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
122 /* Fix up sign bit. */
123 if (r
->sign
!= decNumberIsNegative (dn
))
127 /* Encode a real into an IEEE 754 decimal32 type. */
130 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
131 long *buf
, const REAL_VALUE_TYPE
*r
)
137 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
140 decimal_to_decnumber (r
, &dn
);
141 decimal32FromNumber (&d32
, &dn
, &set
);
143 memcpy (&buf
[0], d32
.bytes
, sizeof (uint32_t));
146 /* Decode an IEEE 754 decimal32 type into a real. */
149 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
150 REAL_VALUE_TYPE
*r
, const long *buf
)
156 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
159 memcpy (&d32
.bytes
, &buf
[0], sizeof (uint32_t));
161 decimal32ToNumber (&d32
, &dn
);
162 decimal_from_decnumber (r
, &dn
, &set
);
165 /* Encode a real into an IEEE 754 decimal64 type. */
168 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
169 long *buf
, const REAL_VALUE_TYPE
*r
)
175 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
178 decimal_to_decnumber (r
, &dn
);
179 decimal64FromNumber (&d64
, &dn
, &set
);
181 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
183 memcpy (&buf
[0], &d64
.bytes
[0], sizeof (uint32_t));
184 memcpy (&buf
[1], &d64
.bytes
[4], sizeof (uint32_t));
188 memcpy (&buf
[0], &d64
.bytes
[4], sizeof (uint32_t));
189 memcpy (&buf
[1], &d64
.bytes
[0], sizeof (uint32_t));
193 /* Decode an IEEE 754 decimal64 type into a real. */
196 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
197 REAL_VALUE_TYPE
*r
, const long *buf
)
203 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
206 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
208 memcpy (&d64
.bytes
[0], &buf
[0], sizeof (uint32_t));
209 memcpy (&d64
.bytes
[4], &buf
[1], sizeof (uint32_t));
213 memcpy (&d64
.bytes
[0], &buf
[1], sizeof (uint32_t));
214 memcpy (&d64
.bytes
[4], &buf
[0], sizeof (uint32_t));
217 decimal64ToNumber (&d64
, &dn
);
218 decimal_from_decnumber (r
, &dn
, &set
);
221 /* Encode a real into an IEEE 754 decimal128 type. */
224 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
225 long *buf
, const REAL_VALUE_TYPE
*r
)
231 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
234 decimal_to_decnumber (r
, &dn
);
235 decimal128FromNumber (&d128
, &dn
, &set
);
237 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
239 memcpy (&buf
[0], &d128
.bytes
[0], sizeof (uint32_t));
240 memcpy (&buf
[1], &d128
.bytes
[4], sizeof (uint32_t));
241 memcpy (&buf
[2], &d128
.bytes
[8], sizeof (uint32_t));
242 memcpy (&buf
[3], &d128
.bytes
[12], sizeof (uint32_t));
246 memcpy (&buf
[0], &d128
.bytes
[12], sizeof (uint32_t));
247 memcpy (&buf
[1], &d128
.bytes
[8], sizeof (uint32_t));
248 memcpy (&buf
[2], &d128
.bytes
[4], sizeof (uint32_t));
249 memcpy (&buf
[3], &d128
.bytes
[0], sizeof (uint32_t));
253 /* Decode an IEEE 754 decimal128 type into a real. */
256 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
257 REAL_VALUE_TYPE
*r
, const long *buf
)
263 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
266 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
268 memcpy (&d128
.bytes
[0], &buf
[0], sizeof (uint32_t));
269 memcpy (&d128
.bytes
[4], &buf
[1], sizeof (uint32_t));
270 memcpy (&d128
.bytes
[8], &buf
[2], sizeof (uint32_t));
271 memcpy (&d128
.bytes
[12], &buf
[3], sizeof (uint32_t));
275 memcpy (&d128
.bytes
[0], &buf
[3], sizeof (uint32_t));
276 memcpy (&d128
.bytes
[4], &buf
[2], sizeof (uint32_t));
277 memcpy (&d128
.bytes
[8], &buf
[1], sizeof (uint32_t));
278 memcpy (&d128
.bytes
[12], &buf
[0], sizeof (uint32_t));
281 decimal128ToNumber (&d128
, &dn
);
282 decimal_from_decnumber (r
, &dn
, &set
);
285 /* Helper function to convert from a binary real internal
289 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
290 enum machine_mode mode
)
293 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
295 decimal128ToString (d128
, string
);
296 real_from_string3 (to
, string
, mode
);
300 /* Helper function to convert from a binary real internal
304 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
308 /* We convert to string, then to decNumber then to decimal128. */
309 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
310 decimal_real_from_string (to
, string
);
313 /* Helper function to real.c:do_compare() to handle decimal internal
314 representation including when one of the operands is still in the
315 binary internal representation. */
318 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
322 decNumber dn
, dn2
, dn3
;
323 REAL_VALUE_TYPE a1
, b1
;
325 /* If either operand is non-decimal, create temporary versions. */
328 decimal_from_binary (&a1
, a
);
333 decimal_from_binary (&b1
, b
);
337 /* Convert into decNumber form for comparison operation. */
338 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
340 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
341 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
343 /* Finally, do the comparison. */
344 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
346 /* Return the comparison result. */
347 if (decNumberIsNaN (&dn
))
349 else if (decNumberIsZero (&dn
))
351 else if (decNumberIsNegative (&dn
))
357 /* Helper to round_for_format, handling decimal float types. */
360 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
365 /* Real encoding occurs later. */
366 if (r
->cl
!= rvc_normal
)
369 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
371 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
373 if (fmt
== &decimal_quad_format
)
375 /* The internal format is already in this format. */
378 else if (fmt
== &decimal_single_format
)
381 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
384 decimal32FromNumber (&d32
, &dn
, &set
);
385 decimal32ToNumber (&d32
, &dn
);
387 else if (fmt
== &decimal_double_format
)
390 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
393 decimal64FromNumber (&d64
, &dn
, &set
);
394 decimal64ToNumber (&d64
, &dn
);
399 decimal_from_decnumber (r
, &dn
, &set
);
402 /* Extend or truncate to a new mode. Handles conversions between
403 binary and decimal types. */
406 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
407 const REAL_VALUE_TYPE
*a
)
409 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
411 if (a
->decimal
&& fmt
->b
== 10)
414 decimal_to_binary (r
, a
, mode
);
416 decimal_from_binary (r
, a
);
419 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
420 significant digits in the result, bounded by BUF_SIZE. If DIGITS
421 is 0, choose the maximum for the representation. If
422 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
423 DIGITS or CROP_TRAILING_ZEROS. */
426 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
428 size_t digits ATTRIBUTE_UNUSED
,
429 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
431 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
433 /* decimal128ToString requires space for at least 24 characters;
434 Require two more for suffix. */
435 gcc_assert (buf_size
>= 24);
436 decimal128ToString (d128
, str
);
440 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
441 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
447 decimal_to_decnumber (op0
, &dn2
);
448 decimal_to_decnumber (op1
, &dn3
);
450 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
454 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
456 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
458 decimal_from_decnumber (r
, &dn
, &set
);
460 /* Return true, if inexact. */
461 return (set
.status
& DEC_Inexact
);
464 /* Compute R = OP0 * OP1. */
467 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
468 const REAL_VALUE_TYPE
*op1
)
471 decNumber dn
, dn2
, dn3
;
473 decimal_to_decnumber (op0
, &dn2
);
474 decimal_to_decnumber (op1
, &dn3
);
476 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
479 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
480 decimal_from_decnumber (r
, &dn
, &set
);
482 /* Return true, if inexact. */
483 return (set
.status
& DEC_Inexact
);
486 /* Compute R = OP0 / OP1. */
489 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
490 const REAL_VALUE_TYPE
*op1
)
493 decNumber dn
, dn2
, dn3
;
495 decimal_to_decnumber (op0
, &dn2
);
496 decimal_to_decnumber (op1
, &dn3
);
498 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
501 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
502 decimal_from_decnumber (r
, &dn
, &set
);
504 /* Return true, if inexact. */
505 return (set
.status
& DEC_Inexact
);
508 /* Set R to A truncated to an integral value toward zero (decimal
512 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
517 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
519 set
.round
= DEC_ROUND_DOWN
;
520 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
522 decNumberToIntegralValue (&dn
, &dn2
, &set
);
523 decimal_from_decnumber (r
, &dn
, &set
);
526 /* Render decimal float value R as an integer. */
529 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
532 decNumber dn
, dn2
, dn3
;
536 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
538 set
.round
= DEC_ROUND_DOWN
;
539 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
541 decNumberToIntegralValue (&dn2
, &dn
, &set
);
542 decNumberZero (&dn3
);
543 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
545 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
547 decNumberToString (&dn
, string
);
548 real_from_string (&to
, string
);
549 return real_to_integer (&to
);
552 /* Likewise, but to an integer pair, HI+LOW. */
555 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
556 const REAL_VALUE_TYPE
*r
)
559 decNumber dn
, dn2
, dn3
;
563 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
565 set
.round
= DEC_ROUND_DOWN
;
566 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
568 decNumberToIntegralValue (&dn2
, &dn
, &set
);
569 decNumberZero (&dn3
);
570 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
572 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
574 decNumberToString (&dn
, string
);
575 real_from_string (&to
, string
);
576 real_to_integer2 (plow
, phigh
, &to
);
579 /* Perform the decimal floating point operation described by CODE.
580 For a unary operation, OP1 will be NULL. This function returns
581 true if the result may be inexact due to loss of precision. */
584 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
585 const REAL_VALUE_TYPE
*op0
,
586 const REAL_VALUE_TYPE
*op1
)
588 REAL_VALUE_TYPE a
, b
;
590 /* If either operand is non-decimal, create temporaries. */
593 decimal_from_binary (&a
, op0
);
596 if (op1
&& !op1
->decimal
)
598 decimal_from_binary (&b
, op1
);
605 return decimal_do_add (r
, op0
, op1
, 0);
608 return decimal_do_add (r
, op0
, op1
, 1);
611 return decimal_do_multiply (r
, op0
, op1
);
614 return decimal_do_divide (r
, op0
, op1
);
617 if (op1
->cl
== rvc_nan
)
619 else if (real_compare (UNLT_EXPR
, op0
, op1
))
626 if (op1
->cl
== rvc_nan
)
628 else if (real_compare (LT_EXPR
, op0
, op1
))
638 decimal128FlipSign ((decimal128
*) r
->sig
);
639 /* Keep sign field in sync. */
647 /* Clear sign bit. */
648 decimal128ClearSign ((decimal128
*) r
->sig
);
649 /* Keep sign field in sync. */
655 decimal_do_fix_trunc (r
, op0
);
663 /* Fills R with the largest finite value representable in mode MODE.
664 If SIGN is nonzero, R is set to the most negative finite value. */
667 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
677 max
= "9.999999999999999E384";
680 max
= "9.999999999999999999999999999999999E6144";
686 decimal_real_from_string (r
, max
);
688 decimal128SetSign ((decimal128
*) r
->sig
, 1);