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

            Bug ID: 79373
           Summary: For loop optimization bug: condition ignored
           Product: gcc
           Version: 5.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: steven.p.pigeon at gmail dot com
  Target Milestone: ---

Created attachment 40668
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40668&action=edit
Collected data for bug (followed collection instructions)

The condition in the following for-loop is ignored when the following program
is optimized with -O3:

  for (int i=-32768; i<32768; i++)
   {
    int w=wstream::_16_to_24(i);
    int j=wstream::_24_to_16(w);
    if (i!=j)
     std::cout << " failed on: " << i << " = " << j << std::endl;
   }

with functions (in class wstream)

      static int32_t _16_to_24(int16_t x)
       {

        int32_t t=x;
        t+=(1u<<15);
        t*=65793;
        t/=256;
        return t-(1u<<23);
       }
      static int16_t _24_to_16(int32_t x)
       {
        return ((x+(1u<<23))*257)/65793-(1u<<15);
       }

with *no* side-effects.

Program works properly when optimizations are disabled (-O0), or functions
modified slightly (ignore the fact that both function aren't exactly inverse of
each other).

Attached is an archive with full program, Makefile used to generate code, and
collected information as directed for a bug report (gcc -v -save-temps ...).

Reply via email to