[gcc(refs/vendors/redhat/heads/gcc-8-branch)] dfp: Fix decimal_to_binary [PR94111]
Jakub Jelinek
jakub@gcc.gnu.org
Thu Sep 17 17:19:42 GMT 2020
https://gcc.gnu.org/g:c1bc06bd5a2532ee7409fb2ba601c77f6a1ed773
commit c1bc06bd5a2532ee7409fb2ba601c77f6a1ed773
Author: Jakub Jelinek <jakub@redhat.com>
Date: Wed Mar 11 09:33:52 2020 +0100
dfp: Fix decimal_to_binary [PR94111]
As e.g. decimal_from_decnumber shows, the REAL_VALUE_TYPE representation
contains a decimal128 embedded in ->sig only if it is rvc_normal, for
other kinds like rvc_inf or rvc_nan, ->sig is ignored and everything is
contained in the REAL_VALUE_TYPE flags (cl, sign, signalling and decimal).
decimal_to_binary which is used when folding a decimal{32,64,128} constant
to a binary floating point type ignores this and thus folds infinities and
NaNs into +0.0.
The following patch fixes that by only doing that for rvc_normal.
Similarly to the binary to decimal folding, it goes through a string, in
order to e.g. deal with canonical NaN mantissas, or binary float formats
that don't support infinities and/or NaNs.
2020-03-11 Jakub Jelinek <jakub@redhat.com>
PR middle-end/94111
* dfp.c (decimal_to_binary): Only use decimal128ToString if from->cl
is rvc_normal, otherwise use real_to_decimal to print the number to
string.
* gcc.dg/dfp/pr94111.c: New test.
(cherry picked from commit 343c467ccdc24edb9acd7c60d54914d9656ab499)
Diff:
---
gcc/dfp.c | 10 +++++++---
gcc/testsuite/gcc.dg/dfp/pr94111.c | 12 ++++++++++++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/gcc/dfp.c b/gcc/dfp.c
index 230b1b14eaa..26efdc51901 100644
--- a/gcc/dfp.c
+++ b/gcc/dfp.c
@@ -342,9 +342,13 @@ decimal_to_binary (REAL_VALUE_TYPE *to, const REAL_VALUE_TYPE *from,
const real_format *fmt)
{
char string[256];
- const decimal128 *const d128 = (const decimal128 *) from->sig;
-
- decimal128ToString (d128, string);
+ if (from->cl == rvc_normal)
+ {
+ const decimal128 *const d128 = (const decimal128 *) from->sig;
+ decimal128ToString (d128, string);
+ }
+ else
+ real_to_decimal (string, from, sizeof (string), 0, 1);
real_from_string3 (to, string, fmt);
}
diff --git a/gcc/testsuite/gcc.dg/dfp/pr94111.c b/gcc/testsuite/gcc.dg/dfp/pr94111.c
new file mode 100644
index 00000000000..ea3a132270a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/dfp/pr94111.c
@@ -0,0 +1,12 @@
+/* PR middle-end/94111 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+int
+main ()
+{
+ _Decimal32 d = (_Decimal32) __builtin_inff ();
+ if (!__builtin_isinf ((double) d))
+ __builtin_abort ();
+ return 0;
+}
More information about the Gcc-cvs
mailing list