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 (decNumberIsNaN (dn
))
51 if (decNumberIsInfinite (dn
))
53 if (context
->status
& DEC_Overflow
)
55 if (decNumberIsNegative (dn
))
59 if (r
->cl
!= rvc_normal
)
62 decContextDefault (context
, DEC_INIT_DECIMAL128
);
65 decimal128FromNumber ((decimal128
*) r
->sig
, dn
, context
);
68 /* Create decimal encoded R from string S. */
71 decimal_real_from_string (REAL_VALUE_TYPE
*r
, const char *s
)
75 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
78 decNumberFromString (&dn
, s
, &set
);
80 /* It would be more efficient to store directly in decNumber format,
81 but that is impractical from current data structure size.
82 Encoding as a decimal128 is much more compact. */
83 decimal_from_decnumber (r
, &dn
, &set
);
86 /* Initialize a decNumber from a REAL_VALUE_TYPE. */
89 decimal_to_decnumber (const REAL_VALUE_TYPE
*r
, decNumber
*dn
)
92 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
101 decNumberFromString (dn
, "Infinity", &set
);
105 decNumberFromString (dn
, "snan", &set
);
107 decNumberFromString (dn
, "nan", &set
);
110 gcc_assert (r
->decimal
);
111 decimal128ToNumber ((const decimal128
*) r
->sig
, dn
);
117 /* Fix up sign bit. */
118 if (r
->sign
!= decNumberIsNegative (dn
))
122 /* Encode a real into an IEEE 754R decimal32 type. */
125 encode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
126 long *buf
, const REAL_VALUE_TYPE
*r
)
132 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
135 decimal_to_decnumber (r
, &dn
);
136 decimal32FromNumber (&d32
, &dn
, &set
);
138 buf
[0] = *(uint32_t *) d32
.bytes
;
141 /* Decode an IEEE 754R decimal32 type into a real. */
144 decode_decimal32 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
145 REAL_VALUE_TYPE
*r
, const long *buf
)
151 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
154 *((uint32_t *) d32
.bytes
) = (uint32_t) buf
[0];
156 decimal32ToNumber (&d32
, &dn
);
157 decimal_from_decnumber (r
, &dn
, &set
);
160 /* Encode a real into an IEEE 754R decimal64 type. */
163 encode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
164 long *buf
, const REAL_VALUE_TYPE
*r
)
170 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
173 decimal_to_decnumber (r
, &dn
);
174 decimal64FromNumber (&d64
, &dn
, &set
);
176 buf
[0] = *(uint32_t *) &d64
.bytes
[0];
177 buf
[1] = *(uint32_t *) &d64
.bytes
[4];
180 /* Decode an IEEE 754R decimal64 type into a real. */
183 decode_decimal64 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
184 REAL_VALUE_TYPE
*r
, const long *buf
)
190 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
193 *((uint32_t *) &d64
.bytes
[0]) = (uint32_t) buf
[0];
194 *((uint32_t *) &d64
.bytes
[4]) = (uint32_t) buf
[1];
196 decimal64ToNumber (&d64
, &dn
);
197 decimal_from_decnumber (r
, &dn
, &set
);
200 /* Encode a real into an IEEE 754R decimal128 type. */
203 encode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
204 long *buf
, const REAL_VALUE_TYPE
*r
)
210 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
213 decimal_to_decnumber (r
, &dn
);
214 decimal128FromNumber (&d128
, &dn
, &set
);
216 buf
[0] = *(uint32_t *) &d128
.bytes
[0];
217 buf
[1] = *(uint32_t *) &d128
.bytes
[4];
218 buf
[2] = *(uint32_t *) &d128
.bytes
[8];
219 buf
[3] = *(uint32_t *) &d128
.bytes
[12];
222 /* Decode an IEEE 754R decimal128 type into a real. */
225 decode_decimal128 (const struct real_format
*fmt ATTRIBUTE_UNUSED
,
226 REAL_VALUE_TYPE
*r
, const long *buf
)
232 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
235 *((uint32_t *) &d128
.bytes
[0]) = (uint32_t) buf
[0];
236 *((uint32_t *) &d128
.bytes
[4]) = (uint32_t) buf
[1];
237 *((uint32_t *) &d128
.bytes
[8]) = (uint32_t) buf
[2];
238 *((uint32_t *) &d128
.bytes
[12]) = (uint32_t) buf
[3];
240 decimal128ToNumber (&d128
, &dn
);
241 decimal_from_decnumber (r
, &dn
, &set
);
244 /* Helper function to convert from a binary real internal
248 decimal_to_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
,
249 enum machine_mode mode
)
252 const decimal128
*const d128
= (const decimal128
*) from
->sig
;
254 decimal128ToString (d128
, string
);
255 real_from_string3 (to
, string
, mode
);
259 /* Helper function to convert from a binary real internal
263 decimal_from_binary (REAL_VALUE_TYPE
*to
, const REAL_VALUE_TYPE
*from
)
267 /* We convert to string, then to decNumber then to decimal128. */
268 real_to_decimal (string
, from
, sizeof (string
), 0, 1);
269 decimal_real_from_string (to
, string
);
272 /* Helper function to real.c:do_compare() to handle decimal internal
273 representation including when one of the operands is still in the
274 binary internal representation. */
277 decimal_do_compare (const REAL_VALUE_TYPE
*a
, const REAL_VALUE_TYPE
*b
,
281 decNumber dn
, dn2
, dn3
;
282 REAL_VALUE_TYPE a1
, b1
;
284 /* If either operand is non-decimal, create temporary versions. */
287 decimal_from_binary (&a1
, a
);
292 decimal_from_binary (&b1
, b
);
296 /* Convert into decNumber form for comparison operation. */
297 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
299 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
300 decimal128ToNumber ((const decimal128
*) b
->sig
, &dn3
);
302 /* Finally, do the comparison. */
303 decNumberCompare (&dn
, &dn2
, &dn3
, &set
);
305 /* Return the comparison result. */
306 if (decNumberIsNaN (&dn
))
308 else if (decNumberIsZero (&dn
))
310 else if (decNumberIsNegative (&dn
))
316 /* Helper to round_for_format, handling decimal float types. */
319 decimal_round_for_format (const struct real_format
*fmt
, REAL_VALUE_TYPE
*r
)
324 /* Real encoding occurs later. */
325 if (r
->cl
!= rvc_normal
)
328 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
330 decimal128ToNumber ((decimal128
*) r
->sig
, &dn
);
332 if (fmt
== &decimal_quad_format
)
334 /* The internal format is already in this format. */
337 else if (fmt
== &decimal_single_format
)
340 decContextDefault (&set
, DEC_INIT_DECIMAL32
);
343 decimal32FromNumber (&d32
, &dn
, &set
);
344 decimal32ToNumber (&d32
, &dn
);
346 else if (fmt
== &decimal_double_format
)
349 decContextDefault (&set
, DEC_INIT_DECIMAL64
);
352 decimal64FromNumber (&d64
, &dn
, &set
);
353 decimal64ToNumber (&d64
, &dn
);
358 decimal_from_decnumber (r
, &dn
, &set
);
361 /* Extend or truncate to a new mode. Handles conversions between
362 binary and decimal types. */
365 decimal_real_convert (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
366 const REAL_VALUE_TYPE
*a
)
368 const struct real_format
*fmt
= REAL_MODE_FORMAT (mode
);
370 if (a
->decimal
&& fmt
->b
== 10)
373 decimal_to_binary (r
, a
, mode
);
375 decimal_from_binary (r
, a
);
378 /* Render R_ORIG as a decimal floating point constant. Emit DIGITS
379 significant digits in the result, bounded by BUF_SIZE. If DIGITS
380 is 0, choose the maximum for the representation. If
381 CROP_TRAILING_ZEROS, strip trailing zeros. Currently, not honoring
382 DIGITS or CROP_TRAILING_ZEROS. */
385 decimal_real_to_decimal (char *str
, const REAL_VALUE_TYPE
*r_orig
,
387 size_t digits ATTRIBUTE_UNUSED
,
388 int crop_trailing_zeros ATTRIBUTE_UNUSED
)
390 const decimal128
*const d128
= (const decimal128
*) r_orig
->sig
;
392 /* decimal128ToString requires space for at least 24 characters;
393 Require two more for suffix. */
394 gcc_assert (buf_size
>= 24);
395 decimal128ToString (d128
, str
);
399 decimal_do_add (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
400 const REAL_VALUE_TYPE
*op1
, int subtract_p
)
406 decimal_to_decnumber (op0
, &dn2
);
407 decimal_to_decnumber (op1
, &dn3
);
409 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
413 decNumberSubtract (&dn
, &dn2
, &dn3
, &set
);
415 decNumberAdd (&dn
, &dn2
, &dn3
, &set
);
417 decimal_from_decnumber (r
, &dn
, &set
);
419 /* Return true, if inexact. */
420 return (set
.status
& DEC_Inexact
);
423 /* Compute R = OP0 * OP1. */
426 decimal_do_multiply (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
427 const REAL_VALUE_TYPE
*op1
)
430 decNumber dn
, dn2
, dn3
;
432 decimal_to_decnumber (op0
, &dn2
);
433 decimal_to_decnumber (op1
, &dn3
);
435 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
438 decNumberMultiply (&dn
, &dn2
, &dn3
, &set
);
439 decimal_from_decnumber (r
, &dn
, &set
);
441 /* Return true, if inexact. */
442 return (set
.status
& DEC_Inexact
);
445 /* Compute R = OP0 / OP1. */
448 decimal_do_divide (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*op0
,
449 const REAL_VALUE_TYPE
*op1
)
452 decNumber dn
, dn2
, dn3
;
454 decimal_to_decnumber (op0
, &dn2
);
455 decimal_to_decnumber (op1
, &dn3
);
457 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
460 decNumberDivide (&dn
, &dn2
, &dn3
, &set
);
461 decimal_from_decnumber (r
, &dn
, &set
);
463 /* Return true, if inexact. */
464 return (set
.status
& DEC_Inexact
);
467 /* Set R to A truncated to an integral value toward zero (decimal
471 decimal_do_fix_trunc (REAL_VALUE_TYPE
*r
, const REAL_VALUE_TYPE
*a
)
476 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
478 set
.round
= DEC_ROUND_DOWN
;
479 decimal128ToNumber ((const decimal128
*) a
->sig
, &dn2
);
481 decNumberToIntegralValue (&dn
, &dn2
, &set
);
482 decimal_from_decnumber (r
, &dn
, &set
);
485 /* Render decimal float value R as an integer. */
488 decimal_real_to_integer (const REAL_VALUE_TYPE
*r
)
491 decNumber dn
, dn2
, dn3
;
495 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
497 set
.round
= DEC_ROUND_DOWN
;
498 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
500 decNumberToIntegralValue (&dn2
, &dn
, &set
);
501 decNumberZero (&dn3
);
502 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
504 /* Convert to REAL_VALUE_TYPE and call appropriate conversion
506 decNumberToString (&dn
, string
);
507 real_from_string (&to
, string
);
508 return real_to_integer (&to
);
511 /* Likewise, but to an integer pair, HI+LOW. */
514 decimal_real_to_integer2 (HOST_WIDE_INT
*plow
, HOST_WIDE_INT
*phigh
,
515 const REAL_VALUE_TYPE
*r
)
518 decNumber dn
, dn2
, dn3
;
522 decContextDefault (&set
, DEC_INIT_DECIMAL128
);
524 set
.round
= DEC_ROUND_DOWN
;
525 decimal128ToNumber ((const decimal128
*) r
->sig
, &dn
);
527 decNumberToIntegralValue (&dn2
, &dn
, &set
);
528 decNumberZero (&dn3
);
529 decNumberRescale (&dn
, &dn2
, &dn3
, &set
);
531 /* Conver to REAL_VALUE_TYPE and call appropriate conversion
533 decNumberToString (&dn
, string
);
534 real_from_string (&to
, string
);
535 real_to_integer2 (plow
, phigh
, &to
);
538 /* Perform the decimal floating point operation described by CODE.
539 For a unary operation, OP1 will be NULL. This function returns
540 true if the result may be inexact due to loss of precision. */
543 decimal_real_arithmetic (REAL_VALUE_TYPE
*r
, enum tree_code code
,
544 const REAL_VALUE_TYPE
*op0
,
545 const REAL_VALUE_TYPE
*op1
)
547 REAL_VALUE_TYPE a
, b
;
549 /* If either operand is non-decimal, create temporaries. */
552 decimal_from_binary (&a
, op0
);
555 if (op1
&& !op1
->decimal
)
557 decimal_from_binary (&b
, op1
);
564 return decimal_do_add (r
, op0
, op1
, 0);
567 return decimal_do_add (r
, op0
, op1
, 1);
570 return decimal_do_multiply (r
, op0
, op1
);
573 return decimal_do_divide (r
, op0
, op1
);
576 if (op1
->cl
== rvc_nan
)
578 else if (real_compare (UNLT_EXPR
, op0
, op1
))
585 if (op1
->cl
== rvc_nan
)
587 else if (real_compare (LT_EXPR
, op0
, op1
))
597 decimal128FlipSign ((decimal128
*) r
->sig
);
598 /* Keep sign field in sync. */
606 /* Clear sign bit. */
607 decimal128ClearSign ((decimal128
*) r
->sig
);
608 /* Keep sign field in sync. */
614 decimal_do_fix_trunc (r
, op0
);
622 /* Fills R with the largest finite value representable in mode MODE.
623 If SIGN is nonzero, R is set to the most negative finite value. */
626 decimal_real_maxval (REAL_VALUE_TYPE
*r
, int sign
, enum machine_mode mode
)
636 max
= "9.999999999999999E384";
639 max
= "9.999999999999999999999999999999999E6144";
645 decimal_real_from_string (r
, max
);
647 decimal128SetSign ((decimal128
*) r
->sig
, 1);