1 /* Decimal floating point support.
2 Copyright (C) 2005, 2006, 2007, 2008 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 #ifndef WORDS_BIGENDIAN
40 #define WORDS_BIGENDIAN 0
43 /* Initialize R (a real with the decimal flag set) from DN. Can
44 utilize status passed in via CONTEXT, if a previous operation had
45 interesting status. */
48 decimal_from_decnumber (REAL_VALUE_TYPE
*r
, decNumber
*dn
, decContext
*context
)
50 memset (r
, 0, sizeof (REAL_VALUE_TYPE
));
53 if (decNumberIsNaN (dn
))
55 if (decNumberIsInfinite (dn
))
57 if (context
->status
& DEC_Overflow
)
59 if (decNumberIsNegative (dn
))
63 if (r
->cl
!= rvc_normal
)
66 decContextDefault (context
, DEC_INIT_DECIMAL128
);
69 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
72 /* Create decimal encoded R from string S. */
75 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
79 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
82 decNumberFromString (&dn
, s
, &set
);
84 /* It would be more efficient to store directly in decNumber format,
85 but that is impractical from current data structure size.
86 Encoding as a decimal128 is much more compact. */
87 decimal_from_decnumber (r
, &dn
, &set
);
90 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
93 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
96 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
105 decNumberFromString (dn
, "Infinity", &set
);
109 decNumberFromString (dn
, "snan", &set
);
111 decNumberFromString (dn
, "nan", &set
);
114 gcc_assert (r
->decimal
);
115 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
121 /* Fix up sign bit. */
122 if (r
->sign
!= decNumberIsNegative (dn
))
126 /* Encode a real into an IEEE 754 decimal32 type. */
129 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
130 long *buf
, const REAL_VALUE_TYPE
*r
)
136 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
139 decimal_to_decnumber (r
, &dn
);
140 decimal32FromNumber (&d32
, &dn
, &set
);
142 buf
[0] = *(uint32_t *) d32
.bytes
;
145 /* Decode an IEEE 754 decimal32 type into a real. */
148 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
149 REAL_VALUE_TYPE
*r
, const long *buf
)
155 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
158 *((uint32_t *) d32
.bytes
) = (uint32_t) buf
[0];
160 decimal32ToNumber (&d32
, &dn
);
161 decimal_from_decnumber (r
, &dn
, &set
);
164 /* Encode a real into an IEEE 754 decimal64 type. */
167 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
168 long *buf
, const REAL_VALUE_TYPE
*r
)
174 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
177 decimal_to_decnumber (r
, &dn
);
178 decimal64FromNumber (&d64
, &dn
, &set
);
180 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
182 buf
[0] = *(uint32_t *) &d64
.bytes
[0];
183 buf
[1] = *(uint32_t *) &d64
.bytes
[4];
187 buf
[0] = *(uint32_t *) &d64
.bytes
[4];
188 buf
[1] = *(uint32_t *) &d64
.bytes
[0];
192 /* Decode an IEEE 754 decimal64 type into a real. */
195 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
196 REAL_VALUE_TYPE
*r
, const long *buf
)
202 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
205 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
207 *((uint32_t *) &d64
.bytes
[0]) = (uint32_t) buf
[0];
208 *((uint32_t *) &d64
.bytes
[4]) = (uint32_t) buf
[1];
212 *((uint32_t *) &d64
.bytes
[0]) = (uint32_t) buf
[1];
213 *((uint32_t *) &d64
.bytes
[4]) = (uint32_t) buf
[0];
216 decimal64ToNumber (&d64
, &dn
);
217 decimal_from_decnumber (r
, &dn
, &set
);
220 /* Encode a real into an IEEE 754 decimal128 type. */
223 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
224 long *buf
, const REAL_VALUE_TYPE
*r
)
230 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
233 decimal_to_decnumber (r
, &dn
);
234 decimal128FromNumber (&d128
, &dn
, &set
);
236 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
238 buf
[0] = *(uint32_t *) &d128
.bytes
[0];
239 buf
[1] = *(uint32_t *) &d128
.bytes
[4];
240 buf
[2] = *(uint32_t *) &d128
.bytes
[8];
241 buf
[3] = *(uint32_t *) &d128
.bytes
[12];
245 buf
[0] = *(uint32_t *) &d128
.bytes
[12];
246 buf
[1] = *(uint32_t *) &d128
.bytes
[8];
247 buf
[2] = *(uint32_t *) &d128
.bytes
[4];
248 buf
[3] = *(uint32_t *) &d128
.bytes
[0];
252 /* Decode an IEEE 754 decimal128 type into a real. */
255 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
256 REAL_VALUE_TYPE
*r
, const long *buf
)
262 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
265 if (WORDS_BIGENDIAN
== FLOAT_WORDS_BIG_ENDIAN
)
267 *((uint32_t *) &d128
.bytes
[0]) = (uint32_t) buf
[0];
268 *((uint32_t *) &d128
.bytes
[4]) = (uint32_t) buf
[1];
269 *((uint32_t *) &d128
.bytes
[8]) = (uint32_t) buf
[2];
270 *((uint32_t *) &d128
.bytes
[12]) = (uint32_t) buf
[3];
274 *((uint32_t *) &d128
.bytes
[0]) = (uint32_t) buf
[3];
275 *((uint32_t *) &d128
.bytes
[4]) = (uint32_t) buf
[2];
276 *((uint32_t *) &d128
.bytes
[8]) = (uint32_t) buf
[1];
277 *((uint32_t *) &d128
.bytes
[12]) = (uint32_t) buf
[0];
280 decimal128ToNumber (&d128
, &dn
);
281 decimal_from_decnumber (r
, &dn
, &set
);
284 /* Helper function to convert from a binary real internal
288 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
289 enum machine_mode mode
)
292 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
294 decimal128ToString (d128
, string
);
295 real_from_string3 (to
, string
, mode
);
299 /* Helper function to convert from a binary real internal
303 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
307 /* We convert to string, then to decNumber then to decimal128. */
308 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
309 decimal_real_from_string (to
, string
);
312 /* Helper function to real.c:do_compare() to handle decimal internal
313 representation including when one of the operands is still in the
314 binary internal representation. */
317 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
321 decNumber dn
, dn2
, dn3
;
322 REAL_VALUE_TYPE a1
, b1
;
324 /* If either operand is non-decimal, create temporary versions. */
327 decimal_from_binary (&a1
, a
);
332 decimal_from_binary (&b1
, b
);
336 /* Convert into decNumber form for comparison operation. */
337 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
339 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
340 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
342 /* Finally, do the comparison. */
343 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
345 /* Return the comparison result. */
346 if (decNumberIsNaN (&dn
))
348 else if (decNumberIsZero (&dn
))
350 else if (decNumberIsNegative (&dn
))
356 /* Helper to round_for_format, handling decimal float types. */
359 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
364 /* Real encoding occurs later. */
365 if (r
->cl
!= rvc_normal
)
368 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
370 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
372 if (fmt
== &decimal_quad_format
)
374 /* The internal format is already in this format. */
377 else if (fmt
== &decimal_single_format
)
380 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
383 decimal32FromNumber (&d32
, &dn
, &set
);
384 decimal32ToNumber (&d32
, &dn
);
386 else if (fmt
== &decimal_double_format
)
389 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
392 decimal64FromNumber (&d64
, &dn
, &set
);
393 decimal64ToNumber (&d64
, &dn
);
398 decimal_from_decnumber (r
, &dn
, &set
);
401 /* Extend or truncate to a new mode. Handles conversions between
402 binary and decimal types. */
405 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
406 const REAL_VALUE_TYPE
*a
)
408 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
410 if (a
->decimal
&& fmt
->b
== 10)
413 decimal_to_binary (r
, a
, mode
);
415 decimal_from_binary (r
, a
);
418 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
419 significant digits in the result, bounded by BUF_SIZE. If DIGITS
420 is 0, choose the maximum for the representation. If
421 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
422 DIGITS or CROP_TRAILING_ZEROS. */
425 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
427 size_t digits ATTRIBUTE_UNUSED
,
428 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
430 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
432 /* decimal128ToString requires space for at least 24 characters;
433 Require two more for suffix. */
434 gcc_assert (buf_size
>= 24);
435 decimal128ToString (d128
, str
);
439 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
440 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
446 decimal_to_decnumber (op0
, &dn2
);
447 decimal_to_decnumber (op1
, &dn3
);
449 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
453 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
455 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
457 decimal_from_decnumber (r
, &dn
, &set
);
459 /* Return true, if inexact. */
460 return (set
.status
& DEC_Inexact
);
463 /* Compute R = OP0 * OP1. */
466 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
467 const REAL_VALUE_TYPE
*op1
)
470 decNumber dn
, dn2
, dn3
;
472 decimal_to_decnumber (op0
, &dn2
);
473 decimal_to_decnumber (op1
, &dn3
);
475 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
478 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
479 decimal_from_decnumber (r
, &dn
, &set
);
481 /* Return true, if inexact. */
482 return (set
.status
& DEC_Inexact
);
485 /* Compute R = OP0 / OP1. */
488 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
489 const REAL_VALUE_TYPE
*op1
)
492 decNumber dn
, dn2
, dn3
;
494 decimal_to_decnumber (op0
, &dn2
);
495 decimal_to_decnumber (op1
, &dn3
);
497 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
500 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
501 decimal_from_decnumber (r
, &dn
, &set
);
503 /* Return true, if inexact. */
504 return (set
.status
& DEC_Inexact
);
507 /* Set R to A truncated to an integral value toward zero (decimal
511 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
516 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
518 set
.round
= DEC_ROUND_DOWN
;
519 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
521 decNumberToIntegralValue (&dn
, &dn2
, &set
);
522 decimal_from_decnumber (r
, &dn
, &set
);
525 /* Render decimal float value R as an integer. */
528 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
531 decNumber dn
, dn2
, dn3
;
535 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
537 set
.round
= DEC_ROUND_DOWN
;
538 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
540 decNumberToIntegralValue (&dn2
, &dn
, &set
);
541 decNumberZero (&dn3
);
542 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
544 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
546 decNumberToString (&dn
, string
);
547 real_from_string (&to
, string
);
548 return real_to_integer (&to
);
551 /* Likewise, but to an integer pair, HI+LOW. */
554 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
555 const REAL_VALUE_TYPE
*r
)
558 decNumber dn
, dn2
, dn3
;
562 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
564 set
.round
= DEC_ROUND_DOWN
;
565 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
567 decNumberToIntegralValue (&dn2
, &dn
, &set
);
568 decNumberZero (&dn3
);
569 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
571 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
573 decNumberToString (&dn
, string
);
574 real_from_string (&to
, string
);
575 real_to_integer2 (plow
, phigh
, &to
);
578 /* Perform the decimal floating point operation described by CODE.
579 For a unary operation, OP1 will be NULL. This function returns
580 true if the result may be inexact due to loss of precision. */
583 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
584 const REAL_VALUE_TYPE
*op0
,
585 const REAL_VALUE_TYPE
*op1
)
587 REAL_VALUE_TYPE a
, b
;
589 /* If either operand is non-decimal, create temporaries. */
592 decimal_from_binary (&a
, op0
);
595 if (op1
&& !op1
->decimal
)
597 decimal_from_binary (&b
, op1
);
604 return decimal_do_add (r
, op0
, op1
, 0);
607 return decimal_do_add (r
, op0
, op1
, 1);
610 return decimal_do_multiply (r
, op0
, op1
);
613 return decimal_do_divide (r
, op0
, op1
);
616 if (op1
->cl
== rvc_nan
)
618 else if (real_compare (UNLT_EXPR
, op0
, op1
))
625 if (op1
->cl
== rvc_nan
)
627 else if (real_compare (LT_EXPR
, op0
, op1
))
637 decimal128FlipSign ((decimal128
*) r
->sig
);
638 /* Keep sign field in sync. */
646 /* Clear sign bit. */
647 decimal128ClearSign ((decimal128
*) r
->sig
);
648 /* Keep sign field in sync. */
654 decimal_do_fix_trunc (r
, op0
);
662 /* Fills R with the largest finite value representable in mode MODE.
663 If SIGN is nonzero, R is set to the most negative finite value. */
666 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
676 max
= "9.999999999999999E384";
679 max
= "9.999999999999999999999999999999999E6144";
685 decimal_real_from_string (r
, max
);
687 decimal128SetSign ((decimal128
*) r
->sig
, 1);