public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/27682]  New: float to int conversion doesn't raise invalid exception
@ 2006-05-19 16:08 janis at gcc dot gnu dot org
  2006-05-19 16:14 ` [Bug target/27682] " janis at gcc dot gnu dot org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: janis at gcc dot gnu dot org @ 2006-05-19 16:08 UTC (permalink / raw)
  To: gcc-bugs

GCC claims to follow C99 Annex F when converting a floating value to an integer
type when the integral part exceeds the range of the integer type (C99 6.3.1.4
and F.4).  Annex F says that in this case the conversion raises the "invalid"
floating-point exception.  On powerpc64-linux and x86_64-linux, however, the
instruction that is used to convert double to int converts to a 64-bit integer,
not a 32-bit register, so the exception is not raised.  On powerpc64-linux with
-m64 the instruction is fctidz (floating convert to integer doubleword with
round toward zero).  If I replace that instruction with fctiwz (convert to
integer word) in the .s file and compile that, the code behaves as expected. 
Here's a small test case:

#include <fenv.h>
extern void abort (void);

double x = 4294967296.0;
unsigned int i;

int
main ()
{
#pragma STDC FENV_ACCESS ON
    feclearexcept (FE_ALL_EXCEPT);
    i = x;    /* value exceeds the range of 32-bit int */
    if (! fetestexcept (FE_INVALID))
        abort ();
    return 0;
}

I see this behavior in all powerpc64-linux compilers I've checked from 3.2
through mainline.  The exception is raised for tests compiled with -m32.

This is a problem for applications that try to detect such invalid conversions
after checking for __STDC_IEC_559__, which claims that the implementation
conforms to Annex F.

A possibly-related issue is that this macro is defined in a header file
provided by glibc, not by GCC.


-- 
           Summary: float to int conversion doesn't raise invalid exception
           Product: gcc
           Version: 4.0.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: janis at gcc dot gnu dot org
GCC target triplet: powerpc64-linux, x86_64-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
@ 2006-05-19 16:14 ` janis at gcc dot gnu dot org
  2006-05-19 16:22 ` pinskia at gcc dot gnu dot org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: janis at gcc dot gnu dot org @ 2006-05-19 16:14 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from janis at gcc dot gnu dot org  2006-05-19 16:13 -------
This is related to PR21360, although that one is about the result of an
out-of-range conversion.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
  2006-05-19 16:14 ` [Bug target/27682] " janis at gcc dot gnu dot org
@ 2006-05-19 16:22 ` pinskia at gcc dot gnu dot org
  2006-05-19 16:36 ` joseph at codesourcery dot com
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-05-19 16:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2006-05-19 16:21 -------
First we don't yet implement FENV_ACCESS.  There is a bug about that.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
  2006-05-19 16:14 ` [Bug target/27682] " janis at gcc dot gnu dot org
  2006-05-19 16:22 ` pinskia at gcc dot gnu dot org
@ 2006-05-19 16:36 ` joseph at codesourcery dot com
  2006-05-19 17:25 ` janis at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: joseph at codesourcery dot com @ 2006-05-19 16:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from joseph at codesourcery dot com  2006-05-19 16:35 -------
Subject: Re:   New: float to int conversion doesn't raise
 invalid exception

On Fri, 19 May 2006, janis at gcc dot gnu dot org wrote:

> GCC claims to follow C99 Annex F when converting a floating value to an integer

It doesn't (in that it doesn't define __STDC_IEC_559__).  But it should 
aim to do so (provided the user doesn't specify options such as 
-fno-trapping-math in this case).

> A possibly-related issue is that this macro is defined in a header file
> provided by glibc, not by GCC.

That's a bug in the combination of glibc and GCC, requiring cooperation 
between them to fix.  The macro must be defined from the start of the 
translation unit without any headers needing to be included; the same also 
applies to __STDC_ISO_10646__ which is definitely a property of the 
library.  My suggested solution is still as at 
<http://gcc.gnu.org/ml/gcc/2004-05/msg01019.html>: an implicitly included 
header <stdc-predef.h> that is provided by the library, makes such 
definitions, *does not include other glibc headers such as features.h* 
(because the source file may define feature test macros later before it 
first explicitly includes a system header), and whose absence is silently 
ignored by the compiler (in order to support older glibc without the 
header).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (2 preceding siblings ...)
  2006-05-19 16:36 ` joseph at codesourcery dot com
@ 2006-05-19 17:25 ` janis at gcc dot gnu dot org
  2006-05-29  2:48 ` amodra at bigpond dot net dot au
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: janis at gcc dot gnu dot org @ 2006-05-19 17:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from janis at gcc dot gnu dot org  2006-05-19 17:25 -------
This has been brought up to the glibc developers in the past:

  http://sourceware.org/ml/libc-alpha/2005-03/msg00196.html

It's unfortunate that they don't agree that "an implementation" of C consists
of a combination of products, and that unless they all support Annex F, the
"implementation" doesn't support it and should not define __STDC_IEC_559__.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (3 preceding siblings ...)
  2006-05-19 17:25 ` janis at gcc dot gnu dot org
@ 2006-05-29  2:48 ` amodra at bigpond dot net dot au
  2006-05-29  2:52 ` amodra at bigpond dot net dot au
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: amodra at bigpond dot net dot au @ 2006-05-29  2:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from amodra at bigpond dot net dot au  2006-05-29 02:48 -------
Note that fctidz is used because we are converting to unsigned.  gcc uses
fctiwz if converting to int.  Of course, it isn't correct to simply replace
fctidz with fctiwz..  To get this right, gcc would need to generate something
like the following

   fctidz 0,0
   stfd 0,temp_slot(1)
   ld 9,temp_slot(1)
   rldicl 0,9,0,32
   cmpd 0,9
   beq .+8
   mtfsb1 23

with the last three insns being the extras needed to set the invalid operation.

Conversion to unsigned int on ppc32 will also need some tweaks.
Hmm, there we are using code generated by optabs.c (search for comment "For an
unsigned conversion, there is one more way to do it.")  Why doesn't this code
always subtract off 2**(n-1), convert, then add 2**(n-1)?  That way we'd get
the  invalid op bits set correctly.


-- 

amodra at bigpond dot net dot au changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amodra at bigpond dot net
                   |                            |dot au
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2006-05-29 02:48:03
               date|                            |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (4 preceding siblings ...)
  2006-05-29  2:48 ` amodra at bigpond dot net dot au
@ 2006-05-29  2:52 ` amodra at bigpond dot net dot au
  2006-05-30 16:59 ` janis at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: amodra at bigpond dot net dot au @ 2006-05-29  2:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from amodra at bigpond dot net dot au  2006-05-29 02:52 -------
Oh, I suppose rounding might change the result if we always subtract off
2**(n-1).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (5 preceding siblings ...)
  2006-05-29  2:52 ` amodra at bigpond dot net dot au
@ 2006-05-30 16:59 ` janis at gcc dot gnu dot org
  2008-06-24  8:23 ` aldot at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: janis at gcc dot gnu dot org @ 2006-05-30 16:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from janis at gcc dot gnu dot org  2006-05-30 16:59 -------
If the value doesn't fit then the result is undefined and doesn't matter;
getting the invalid flag is what's important.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (6 preceding siblings ...)
  2006-05-30 16:59 ` janis at gcc dot gnu dot org
@ 2008-06-24  8:23 ` aldot at gcc dot gnu dot org
  2009-01-31 20:16 ` tydeman at tybor dot com
  2009-05-14  3:47 ` bje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: aldot at gcc dot gnu dot org @ 2008-06-24  8:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from aldot at gcc dot gnu dot org  2008-06-24 08:22 -------
It looks like several improvements were installed since then. Can somebody
perhaps retry with the 4.3 branch and trunk?


-- 

aldot at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aldot at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (7 preceding siblings ...)
  2008-06-24  8:23 ` aldot at gcc dot gnu dot org
@ 2009-01-31 20:16 ` tydeman at tybor dot com
  2009-05-14  3:47 ` bje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: tydeman at tybor dot com @ 2009-01-31 20:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from tydeman at tybor dot com  2009-01-31 20:16 -------
4.3.2-7 still has problems (even with -frounding-math).

A more complete test can be found at:
  http://www.tybor.com/tflt2int.c


-- 

tydeman at tybor dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tydeman at tybor dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

* [Bug target/27682] float to int conversion doesn't raise invalid exception
  2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
                   ` (8 preceding siblings ...)
  2009-01-31 20:16 ` tydeman at tybor dot com
@ 2009-05-14  3:47 ` bje at gcc dot gnu dot org
  9 siblings, 0 replies; 11+ messages in thread
From: bje at gcc dot gnu dot org @ 2009-05-14  3:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from bje at gcc dot gnu dot org  2009-05-14 03:46 -------
Still present in GCC 4.5.0 20090513.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27682


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

end of thread, other threads:[~2009-05-14  3:47 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-19 16:08 [Bug target/27682] New: float to int conversion doesn't raise invalid exception janis at gcc dot gnu dot org
2006-05-19 16:14 ` [Bug target/27682] " janis at gcc dot gnu dot org
2006-05-19 16:22 ` pinskia at gcc dot gnu dot org
2006-05-19 16:36 ` joseph at codesourcery dot com
2006-05-19 17:25 ` janis at gcc dot gnu dot org
2006-05-29  2:48 ` amodra at bigpond dot net dot au
2006-05-29  2:52 ` amodra at bigpond dot net dot au
2006-05-30 16:59 ` janis at gcc dot gnu dot org
2008-06-24  8:23 ` aldot at gcc dot gnu dot org
2009-01-31 20:16 ` tydeman at tybor dot com
2009-05-14  3:47 ` bje at gcc dot gnu dot 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).