]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
dfp: Fix decimal_to_binary [PR94111]
authorJakub Jelinek <jakub@redhat.com>
Wed, 11 Mar 2020 08:33:52 +0000 (09:33 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 17 Mar 2020 17:10:17 +0000 (18:10 +0100)
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.

gcc/ChangeLog
gcc/dfp.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/dfp/pr94111.c [new file with mode: 0644]

index 12bad2c010f9ebdb15414bf3b13803551c257502..1ea3cf183ad32d562fad5665b905666bfa7ca96d 100644 (file)
@@ -3,6 +3,11 @@
        Backported from mainline
        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.
+
        PR tree-optimization/94114
        * tree-loop-distribution.c (generate_memset_builtin): Call
        rewrite_to_non_trapping_overflow even on mem.
index 65d5fcb08452ad9b84a7c082e773d31fd2adc6fc..599f37ea9da5b651487283bf7b03e1822f8f3745 100644 (file)
--- 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);
 }
 
index 579919ecd10c065a03ae105317fa9857e766c452..8fba81a3bdd0e901f3ef4e20879b63590b2dcaf8 100644 (file)
@@ -3,6 +3,9 @@
        Backported from mainline
        2020-03-11  Jakub Jelinek  <jakub@redhat.com>
 
+       PR middle-end/94111
+       * gcc.dg/dfp/pr94111.c: New test.
+
        PR tree-optimization/94114
        * gcc.dg/pr94114.c: New test.
 
diff --git a/gcc/testsuite/gcc.dg/dfp/pr94111.c b/gcc/testsuite/gcc.dg/dfp/pr94111.c
new file mode 100644 (file)
index 0000000..ea3a132
--- /dev/null
@@ -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;
+}