[Bug c++/25267] New: [3.4 4.0 regression] wrong code with inlining at -O2

pierre dot chatelier at club-internet dot fr gcc-bugzilla@gcc.gnu.org
Mon Dec 5 14:48:00 GMT 2005


Hello,

Consider this code that implements hton and ntoh for float values :

//main.cpp
#include <stdio.h>
#include <netinet/in.h>

inline float hton(float x)
{
  const size_t nb32 = sizeof(float)/sizeof(uint32_t);
  const size_t nb16 = (sizeof(float)/sizeof(uint16_t))%2;
  uint32_t* p32 = (uint32_t*)(&x);
  uint16_t* p16 = 0;
  size_t i = 0;
  for(i = 0 ; i<nb32 ; ++i, ++p32)
    *p32 = htonl(*p32);
  p16 = (uint16_t*)(p32);
  for(i = 0 ; i<nb16 ; ++i, ++p16)
    *p16 = htonl(*p16);
  return x;
}

inline float ntoh(float x)
{
  const size_t nb32 = sizeof(float)/sizeof(uint32_t);
  const size_t nb16 = (sizeof(float)/sizeof(uint16_t))%2;
  uint32_t* p32 = (uint32_t*)(&x);
  uint16_t* p16 = 0;
  size_t i = 0;
  for(i = 0 ; i<nb32 ; ++i, ++p32)
    *p32 = ntohl(*p32);
  p16 = (uint16_t*)(p32);
  for(i = 0 ; i<nb16 ; ++i, ++p16)
    *p16 = ntohl(*p16);
  return x;
}

int main(int argc, char** argv)
{
  float f = 1.3;
  float g = hton(f);
  float h = ntoh(g);
  printf("f=%f\ng=%f\nh=%f\n", f, g, h);
  return 0;
}
///////////////////////////////////////////

When compiled with -O1, it works with gcc 3.3.6, 3.4.5 and 4.0.1
When compiled with -O2, it does not work any more with 3.4.5 and 4.0.1

But if you remove the "inline", it works again at -O2 with 3.4.5 and 4.0.1
In -O3, automatic inlining make it fail again in 3.4.5 and 4.0.1

Regards,

Pierre Chatelier


-- 
           Summary: [3.4 4.0 regression] wrong code with inlining at -O2
           Product: gcc
           Version: 3.4.5
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pierre dot chatelier at club-internet dot fr


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



More information about the Gcc-bugs mailing list