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 "decimal64.h"
35 #include "decimal32.h"
36 #include "decNumber.h"
39 dfp_byte_swap (uint32_t in
)
42 unsigned char *p
= (unsigned char *) &out
;
57 /* Initialize R (a real with the decimal flag set) from DN. Can
58 utilize status passed in via CONTEXT, if a previous operation had
59 interesting status. */
62 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
64 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
67 if (decNumberIsZero (dn
))
69 if (decNumberIsNaN (dn
))
71 if (decNumberIsInfinite (dn
))
73 if (context
->status
& DEC_Overflow
)
75 if (decNumberIsNegative (dn
))
79 if (r
->cl
!= rvc_normal
)
82 decContextDefault (context
, DEC_INIT_DECIMAL128
);
85 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
88 /* Create decimal encoded R from string S. */
91 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
95 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
98 decNumberFromString (&dn
, (char *) s
, &set
);
100 /* It would be more efficient to store directly in decNumber format,
101 but that is impractical from current data structure size.
102 Encoding as a decimal128 is much more compact. */
103 decimal_from_decnumber (r
, &dn
, &set
);
106 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
109 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
112 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
121 decNumberFromString (dn
, (char *)"Infinity", &set
);
125 decNumberFromString (dn
, (char *)"snan", &set
);
127 decNumberFromString (dn
, (char *)"nan", &set
);
130 gcc_assert (r
->decimal
);
131 decimal128ToNumber ((decimal128
*) r
->sig
, dn
);
137 /* Fix up sign bit. */
138 if (r
->sign
!= decNumberIsNegative (dn
))
142 /* Encode a real into an IEEE 754R decimal32 type. */
145 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
146 long *buf
, const REAL_VALUE_TYPE
*r
)
152 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
155 decimal_to_decnumber (r
, &dn
);
156 decimal32FromNumber (&d32
, &dn
, &set
);
158 if (FLOAT_WORDS_BIG_ENDIAN
)
159 buf
[0] = *(uint32_t *) d32
.bytes
;
161 buf
[0] = dfp_byte_swap (*(uint32_t *) d32
.bytes
);
164 /* Decode an IEEE 754R decimal32 type into a real. */
167 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
168 REAL_VALUE_TYPE
*r
, const long *buf
)
174 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
177 if (FLOAT_WORDS_BIG_ENDIAN
)
178 *((uint32_t *) d32
.bytes
) = (uint32_t) buf
[0];
180 *((uint32_t *) d32
.bytes
) = dfp_byte_swap ((uint32_t) buf
[0]);
182 decimal32ToNumber (&d32
, &dn
);
183 decimal_from_decnumber (r
, &dn
, &set
);
186 /* Encode a real into an IEEE 754R decimal64 type. */
189 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
190 long *buf
, const REAL_VALUE_TYPE
*r
)
196 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
199 decimal_to_decnumber (r
, &dn
);
200 decimal64FromNumber (&d64
, &dn
, &set
);
202 if (FLOAT_WORDS_BIG_ENDIAN
)
204 buf
[0] = *(uint32_t *) &d64
.bytes
[0];
205 buf
[1] = *(uint32_t *) &d64
.bytes
[4];
209 buf
[1] = dfp_byte_swap (*(uint32_t *) &d64
.bytes
[0]);
210 buf
[0] = dfp_byte_swap (*(uint32_t *) &d64
.bytes
[4]);
214 /* Decode an IEEE 754R decimal64 type into a real. */
217 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
218 REAL_VALUE_TYPE
*r
, const long *buf
)
224 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
227 if (FLOAT_WORDS_BIG_ENDIAN
)
229 *((uint32_t *) &d64
.bytes
[0]) = (uint32_t) buf
[0];
230 *((uint32_t *) &d64
.bytes
[4]) = (uint32_t) buf
[1];
234 *((uint32_t *) &d64
.bytes
[0]) = dfp_byte_swap ((uint32_t) buf
[1]);
235 *((uint32_t *) &d64
.bytes
[4]) = dfp_byte_swap ((uint32_t) buf
[0]);
238 decimal64ToNumber (&d64
, &dn
);
239 decimal_from_decnumber (r
, &dn
, &set
);
242 /* Encode a real into an IEEE 754R decimal128 type. */
245 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
246 long *buf
, const REAL_VALUE_TYPE
*r
)
252 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
255 decimal_to_decnumber (r
, &dn
);
256 decimal128FromNumber (&d128
, &dn
, &set
);
258 if (FLOAT_WORDS_BIG_ENDIAN
)
260 buf
[0] = *(uint32_t *) &d128
.bytes
[0];
261 buf
[1] = *(uint32_t *) &d128
.bytes
[4];
262 buf
[2] = *(uint32_t *) &d128
.bytes
[8];
263 buf
[3] = *(uint32_t *) &d128
.bytes
[12];
267 buf
[0] = dfp_byte_swap (*(uint32_t *) &d128
.bytes
[12]);
268 buf
[1] = dfp_byte_swap (*(uint32_t *) &d128
.bytes
[8]);
269 buf
[2] = dfp_byte_swap (*(uint32_t *) &d128
.bytes
[4]);
270 buf
[3] = dfp_byte_swap (*(uint32_t *) &d128
.bytes
[0]);
274 /* Decode an IEEE 754R decimal128 type into a real. */
277 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
278 REAL_VALUE_TYPE
*r
, const long *buf
)
284 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
287 if (FLOAT_WORDS_BIG_ENDIAN
)
289 *((uint32_t *) &d128
.bytes
[0]) = (uint32_t) buf
[0];
290 *((uint32_t *) &d128
.bytes
[4]) = (uint32_t) buf
[1];
291 *((uint32_t *) &d128
.bytes
[8]) = (uint32_t) buf
[2];
292 *((uint32_t *) &d128
.bytes
[12]) = (uint32_t) buf
[3];
296 *((uint32_t *) &d128
.bytes
[0]) = dfp_byte_swap ((uint32_t) buf
[3]);
297 *((uint32_t *) &d128
.bytes
[4]) = dfp_byte_swap ((uint32_t) buf
[2]);
298 *((uint32_t *) &d128
.bytes
[8]) = dfp_byte_swap ((uint32_t) buf
[1]);
299 *((uint32_t *) &d128
.bytes
[12]) = dfp_byte_swap ((uint32_t) buf
[0]);
302 decimal128ToNumber (&d128
, &dn
);
303 decimal_from_decnumber (r
, &dn
, &set
);
306 /* Helper function to convert from a binary real internal
310 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
311 enum machine_mode mode
)
315 d128
= (decimal128
*) from
->sig
;
317 decimal128ToString (d128
, string
);
318 real_from_string3 (to
, string
, mode
);
322 /* Helper function to convert from a binary real internal
326 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
330 /* We convert to string, then to decNumber then to decimal128. */
331 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
332 decimal_real_from_string (to
, string
);
335 /* Helper function to real.c:do_compare() to handle decimal internal
336 representation including when one of the operands is still in the
337 binary internal representation. */
340 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
344 decNumber dn
, dn2
, dn3
;
345 REAL_VALUE_TYPE a1
, b1
;
347 /* If either operand is non-decimal, create temporary versions. */
350 decimal_from_binary (&a1
, a
);
355 decimal_from_binary (&b1
, b
);
359 /* Convert into decNumber form for comparison operation. */
360 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
362 decimal128ToNumber ((decimal128
*) a
->sig
, &dn2
);
363 decimal128ToNumber ((decimal128
*) b
->sig
, &dn3
);
365 /* Finally, do the comparison. */
366 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
368 /* Return the comparison result. */
369 if (decNumberIsNaN (&dn
))
371 else if (decNumberIsZero (&dn
))
373 else if (decNumberIsNegative (&dn
))
379 /* Helper to round_for_format, handling decimal float types. */
382 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
387 /* Real encoding occurs later. */
388 if (r
->cl
!= rvc_normal
)
391 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
393 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
395 if (fmt
== &decimal_quad_format
)
397 /* The internal format is already in this format. */
400 else if (fmt
== &decimal_single_format
)
403 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
406 decimal32FromNumber (&d32
, &dn
, &set
);
407 decimal32ToNumber (&d32
, &dn
);
409 else if (fmt
== &decimal_double_format
)
412 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
415 decimal64FromNumber (&d64
, &dn
, &set
);
416 decimal64ToNumber (&d64
, &dn
);
421 decimal_from_decnumber (r
, &dn
, &set
);
424 /* Extend or truncate to a new mode. Handles conversions between
425 binary and decimal types. */
428 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
429 const REAL_VALUE_TYPE
*a
)
431 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
433 if (a
->decimal
&& fmt
->b
== 10)
436 decimal_to_binary (r
, a
, mode
);
438 decimal_from_binary (r
, a
);
441 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
442 significant digits in the result, bounded by BUF_SIZE. If DIGITS
443 is 0, choose the maximum for the representation. If
444 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
445 DIGITS or CROP_TRAILING_ZEROS. */
448 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
450 size_t digits ATTRIBUTE_UNUSED
,
451 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
453 decimal128
*d128
= (decimal128
*) r_orig
->sig
;
455 /* decimal128ToString requires space for at least 24 characters;
456 Require two more for suffix. */
457 gcc_assert (buf_size
>= 24);
458 decimal128ToString (d128
, str
);
462 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
463 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
469 decimal_to_decnumber (op0
, &dn2
);
470 decimal_to_decnumber (op1
, &dn3
);
472 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
476 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
478 decNumberAdd (&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_multiply (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 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
502 decimal_from_decnumber (r
, &dn
, &set
);
504 /* Return true, if inexact. */
505 return (set
.status
& DEC_Inexact
);
508 /* Compute R = OP0 / OP1. */
511 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
512 const REAL_VALUE_TYPE
*op1
)
515 decNumber dn
, dn2
, dn3
;
517 decimal_to_decnumber (op0
, &dn2
);
518 decimal_to_decnumber (op1
, &dn3
);
520 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
523 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
524 decimal_from_decnumber (r
, &dn
, &set
);
526 /* Return true, if inexact. */
527 return (set
.status
& DEC_Inexact
);
530 /* Set R to A truncated to an integral value toward zero (decimal
534 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
539 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
541 set
.round
= DEC_ROUND_DOWN
;
542 decimal128ToNumber ((decimal128
*) a
->sig
, &dn2
);
544 decNumberToIntegralValue (&dn
, &dn2
, &set
);
545 decimal_from_decnumber (r
, &dn
, &set
);
548 /* Render decimal float value R as an integer. */
551 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
554 decNumber dn
, dn2
, dn3
;
558 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
560 set
.round
= DEC_ROUND_DOWN
;
561 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
563 decNumberToIntegralValue (&dn2
, &dn
, &set
);
564 decNumberZero (&dn3
);
565 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
567 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
569 decNumberToString (&dn
, string
);
570 real_from_string (&to
, string
);
571 return real_to_integer (&to
);
574 /* Likewise, but to an integer pair, HI+LOW. */
577 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
578 const REAL_VALUE_TYPE
*r
)
581 decNumber dn
, dn2
, dn3
;
585 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
587 set
.round
= DEC_ROUND_DOWN
;
588 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
590 decNumberToIntegralValue (&dn2
, &dn
, &set
);
591 decNumberZero (&dn3
);
592 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
594 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
596 decNumberToString (&dn
, string
);
597 real_from_string (&to
, string
);
598 real_to_integer2 (plow
, phigh
, &to
);
601 /* Perform the decimal floating point operation described by CODE.
602 For a unary operation, OP1 will be NULL. This function returns
603 true if the result may be inexact due to loss of precision. */
606 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
607 const REAL_VALUE_TYPE
*op0
,
608 const REAL_VALUE_TYPE
*op1
)
610 REAL_VALUE_TYPE a
, b
;
612 /* If either operand is non-decimal, create temporaries. */
615 decimal_from_binary (&a
, op0
);
618 if (op1
&& !op1
->decimal
)
620 decimal_from_binary (&b
, op1
);
627 return decimal_do_add (r
, op0
, op1
, 0);
630 return decimal_do_add (r
, op0
, op1
, 1);
633 return decimal_do_multiply (r
, op0
, op1
);
636 return decimal_do_divide (r
, op0
, op1
);
639 if (op1
->cl
== rvc_nan
)
641 else if (real_compare (UNLT_EXPR
, op0
, op1
))
648 if (op1
->cl
== rvc_nan
)
650 else if (real_compare (LT_EXPR
, op0
, op1
))
660 d128
= (decimal128
*) r
->sig
;
662 d128
->bytes
[0] ^= 1 << 7;
663 /* Keep sign field in sync. */
672 d128
= (decimal128
*) r
->sig
;
673 /* Clear high bit. */
674 d128
->bytes
[0] &= 0x7f;
675 /* Keep sign field in sync. */
681 decimal_do_fix_trunc (r
, op0
);
689 /* Fills R with the largest finite value representable in mode MODE.
690 If SIGN is nonzero, R is set to the most negative finite value. */
693 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
700 max
= (char *) "9.999999E96";
703 max
= (char *) "9.999999999999999E384";
706 max
= (char *) "9.999999999999999999999999999999999E6144";
712 decimal_real_from_string (r
, max
);
714 r
->sig
[0] |= 0x80000000;