Bug 57029

Summary: GCC doesn't set the inexact flag on inexact compile-time int-to-float conversion
Product: gcc Reporter: Vincent Lefèvre <vincent-gcc>
Component: cAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: vr5
Priority: P3    
Version: 4.9.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34678
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2013-04-22 00:00:00
Bug Depends on:    
Bug Blocks: 16989    

Description Vincent Lefèvre 2013-04-22 12:46:23 UTC
GCC doesn't set the inexact flag on inexact compile-time int-to-float conversion:

#include <stdio.h>
#include <fenv.h>

#pragma STDC FENV_ACCESS ON

void test1 (void)
{
  volatile float c;

  c = 0x7fffffbf;
  printf ("c = %a, inexact = %d\n", c, fetestexcept (FE_INEXACT));
}

void test2 (void)
{
  volatile float c;
  volatile int i = 0x7fffffbf;

  c = i;
  printf ("c = %a, inexact = %d\n", c, fetestexcept (FE_INEXACT));
}

int main (void)
{
  test1 ();
  test2 ();
  return 0;
}

Under Linux/x86_64:

$ gcc -std=c99 -O3 conv-int-flt-inex.c -o conv-int-flt-inex -lm
$ ./conv-int-flt-inex
c = 0x1.fffffep+30, inexact = 0
c = 0x1.fffffep+30, inexact = 32

Ditto without optimizations.

Note: the STDC FENV_ACCESS pragma is currently not supported (PR 34678), but I don't think it is directly related (this is not an instruction ordering problem...).

This bug has been found from:

  http://gcc.gnu.org/ml/gcc-help/2013-04/msg00164.html
Comment 1 Richard Biener 2013-04-22 13:01:09 UTC
There is no -finexact-math flag similar to -frounding-math we could guard
such transforms with.  Certainly the default of that flag is 'off' by
design, similar to -frounding-math.
Comment 2 Marc Glisse 2013-04-22 13:07:07 UTC
There is -ftrapping-math, which I think is supposed to have an effect here. And if this was implemented properly, I hope it would be turned off by default.
Comment 3 Vincent Lefèvre 2013-04-22 13:32:10 UTC
(In reply to comment #2)
> There is -ftrapping-math, which I think is supposed to have an effect here. And
> if this was implemented properly, I hope it would be turned off by default.

-ftrapping-math is on by default. And whether -ftrapping-math or -fno-trapping-math is used, the behavior is the same.