public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero
@ 2020-03-09 21:07 ch3root at openwall dot com
  2020-03-10  9:47 ` [Bug middle-end/94111] Wrong constant folding: " rguenth at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: ch3root at openwall dot com @ 2020-03-09 21:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

            Bug ID: 94111
           Summary: Wrong optimization: decimal floating-point infinity
                    casted to double -> zero
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Cast to double of a decimal floating-point infinity gives zero:

----------------------------------------------------------------------
#include <math.h>
#include <string.h>
#include <stdio.h>

int main()
{
    _Decimal32 d = (_Decimal32)INFINITY;

    unsigned u;
    memcpy(&u, &d, sizeof u);
    printf("repr: %08x\n", u);

    printf("cast: %g\n", (double)d);
}
----------------------------------------------------------------------
$ gcc -std=c2x -pedantic -Wall -Wextra test.c && ./a.out
repr: 78000000
cast: inf
$ gcc -std=c2x -pedantic -Wall -Wextra -O3 test.c && ./a.out
repr: 78000000
cast: 0
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200305 (experimental)
----------------------------------------------------------------------

The representation is right for infinity in _Decimal32.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
@ 2020-03-10  9:47 ` rguenth at gcc dot gnu.org
  2020-03-10  9:58 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-03-10  9:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
                 CC|                            |rguenth at gcc dot gnu.org
           Keywords|                            |wrong-code
   Last reconfirmed|                            |2020-03-10
     Ever confirmed|0                           |1
            Summary|Wrong optimization: decimal |Wrong constant folding:
                   |floating-point infinity     |decimal floating-point
                   |casted to double -> zero    |infinity casted to double
                   |                            |-> zero

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
This goes wrong somewhere in constant folding:

  d.1_2 = d_13;
  _3 = (double) d.1_2;

 ->

  d.1_2 =  Inf;
  _3 = 0.0;

so (double) Inf is computed wrong.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
  2020-03-10  9:47 ` [Bug middle-end/94111] Wrong constant folding: " rguenth at gcc dot gnu.org
@ 2020-03-10  9:58 ` jakub at gcc dot gnu.org
  2020-03-10 10:00 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-10  9:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Indeed, this is a folding bug and simplified testcase is:
int
main ()
{
  _Decimal32 d = (_Decimal32) __builtin_inff ();
  if (!__builtin_isinf ((double) d))
    __builtin_abort ();
  return 0;
}

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
  2020-03-10  9:47 ` [Bug middle-end/94111] Wrong constant folding: " rguenth at gcc dot gnu.org
  2020-03-10  9:58 ` jakub at gcc dot gnu.org
@ 2020-03-10 10:00 ` jakub at gcc dot gnu.org
  2020-03-10 11:48 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-10 10:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
And was miscompiled already in GCC 4.3 where _Decimal32 support was added (at
least on x86_64-linux).  I'll have a look.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
                   ` (2 preceding siblings ...)
  2020-03-10 10:00 ` jakub at gcc dot gnu.org
@ 2020-03-10 11:48 ` jakub at gcc dot gnu.org
  2020-03-11  8:37 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-10 11:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 48009
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48009&action=edit
gcc10-pr94111.patch

Untested fix.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
                   ` (3 preceding siblings ...)
  2020-03-10 11:48 ` jakub at gcc dot gnu.org
@ 2020-03-11  8:37 ` jakub at gcc dot gnu.org
  2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-03-11  8:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
commit r10-7121-g312992f5a07ca25f94d538b08401789c2c764293
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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
                   ` (4 preceding siblings ...)
  2020-03-11  8:37 ` jakub at gcc dot gnu.org
@ 2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
  2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
  2020-09-17 17:16 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-03-17 18:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:343c467ccdc24edb9acd7c60d54914d9656ab499

commit r9-8389-g343c467ccdc24edb9acd7c60d54914d9656ab499
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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
                   ` (5 preceding siblings ...)
  2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
@ 2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
  2020-09-17 17:16 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-17 14:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-8 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:8b53a85254e91903018498d70b59928b0d1dd9f9

commit r8-10460-g8b53a85254e91903018498d70b59928b0d1dd9f9
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)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/94111] Wrong constant folding: decimal floating-point infinity casted to double -> zero
  2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
                   ` (6 preceding siblings ...)
  2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
@ 2020-09-17 17:16 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-17 17:16 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94111

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed for 8.5, 9.4+, 10.1+.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-09-17 17:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09 21:07 [Bug middle-end/94111] New: Wrong optimization: decimal floating-point infinity casted to double -> zero ch3root at openwall dot com
2020-03-10  9:47 ` [Bug middle-end/94111] Wrong constant folding: " rguenth at gcc dot gnu.org
2020-03-10  9:58 ` jakub at gcc dot gnu.org
2020-03-10 10:00 ` jakub at gcc dot gnu.org
2020-03-10 11:48 ` jakub at gcc dot gnu.org
2020-03-11  8:37 ` jakub at gcc dot gnu.org
2020-03-17 18:57 ` cvs-commit at gcc dot gnu.org
2020-09-17 14:24 ` cvs-commit at gcc dot gnu.org
2020-09-17 17:16 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).