** Description changed:

  Binary package hint: g++
  
  This bug is in gcc 4.2.4-lubuntu3, on Ubuntu 8.04.2
  apt-cache policy g++ yields:
  g++:
    Installed: 4:4.2.3-1ubuntu6
    Candidate: 4:4.2.3-1ubuntu6
    Version table:
   *** 4:4.2.3-1ubuntu6 0
          500 http://us.archive.ubuntu.com hardy-updates/main Packages
          100 /var/lib/dpkg/status
       4:4.2.3-1ubuntu3 0
          500 http://us.archive.ubuntu.com hardy/main Packages
  
  I'm using a for loop along with 2-D linear determinants to find the 
intersection between a line and a rectangle.  There should always be either 2 
or 0 valid intersections, so the code finds all 4 intersections between the 
line and the lines of which the 4 sides of the rectangle are segments, and then 
throws away the two that fall outside the bounds of the rectangle when the line 
and rectangle intersect.  
  The code stores the valid intersections in a std::vector; thus after the loop 
iterates 4 times, there should be either 0 or 2 elements in the vector.  If 
there are 0, it zeroes out the rectangle; if there are 2, it proceeds to draw 
based on them.  The rectangle being drawn in this block depicts the 
intersection of a cylindrical coordinate surface with a rectangular coordinate 
prism, which is why all this computation has to take place.
  The following block is the code in question:
  
  // Begin snippet
      double radx = m_cosineTable[GetCurrentAngle()];
      double rady = m_sineTable[GetCurrentAngle()];
      double centerX, centerY;
      m_seg->getCenter(centerX, centerY);
  
-     double minX = (int)(centerX - 
fabs(m_seg->getMaxRadius()*m_cosineTable[GetCurrentAngle()]));
-     double maxX = (int)(centerX + 
fabs(m_seg->getMaxRadius()*m_cosineTable[GetCurrentAngle()]));
-     double minY = (int)(centerY - 
fabs(m_seg->getMaxRadius()*m_sineTable[GetCurrentAngle()]));
-     double maxY = (int)(centerY + 
fabs(m_seg->getMaxRadius()*m_sineTable[GetCurrentAngle()]));
+     double minX = (centerX - 
fabs(m_seg->getMaxRadius()*m_cosineTable[GetCurrentAngle()]));
+     double maxX = (centerX + 
fabs(m_seg->getMaxRadius()*m_cosineTable[GetCurrentAngle()]));
+     double minY = (centerY - 
fabs(m_seg->getMaxRadius()*m_sineTable[GetCurrentAngle()]));
+     double maxY = (centerY + 
fabs(m_seg->getMaxRadius()*m_sineTable[GetCurrentAngle()]));
  
      point planeTL,planeBR;
      planeTL.x = ( radx > 0 ? minX : maxX );
      planeTL.y = ( rady < 0 ? minY : maxY );
      planeBR.x = ( radx > 0 ? maxX : minX );
      planeBR.y = ( rady < 0 ? maxY : minY );
  
      std::vector<point> rectCoords;
      for (int i=0; i<4; i++) {
          double a1 = sides[i].y2 - sides[i].y1;
          double b1 = sides[i].x1 - sides[i].x2;
          double c1 = (a1*sides[i].x1) + (b1*sides[i].y1);
  
          double a2 = planeBR.y - planeTL.y;
          double b2 = planeTL.x - planeBR.x;
          double c2 = (a2*planeTL.x) + (b2*planeTL.y);
  
          double det = a1*b2 - a2*b1;
  
          if (det != 0) {
              double xcoord = (b2*c1 - b1*c2)/det;
              double ycoord = (a1*c2 - a2*c1)/det;
  
              if ( xcoord <= (x+width) && xcoord >= x && ycoord <= (y+height) 
&& ycoord >= y ) {
                  point nextCoord;
                  nextCoord.x = xcoord;
                  nextCoord.y = ycoord;
                  rectCoords.push_back(nextCoord);
              }
  //            else {
  //                // Delete me when gcc fixes it's -O3 bug
  //                std::cerr << "Xcoord = " << xcoord << " and Ycoord = " << 
ycoord << std::endl;
  //            }
          }
      }
  
      if (rectCoords.size() != 2) {
          m_branchROIRect = wxRect(0,0,0,0);
          return;
      }
  // End snippet
  
  I know this is a bug related to optimizations because when I set CXXFLAGS=-g, 
things work correctly; when I set CXXFLAGS=-O3, they break (the actual effect 
manifests itself through screen flicker where the rectangle is only drawn some 
of the time, subject to the order in which the 4 "intersections" are found).
  If I uncomment the "else" block above, that also fixes the bug, even with 
optimizations set to -O3, presumably by causing all 4 iterations of the loop to 
have consequences, thus preventing gcc from truncating the loop.

-- 
gcc truncates for loop when -O3 optimizations enabled
https://bugs.launchpad.net/bugs/341356
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.

-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to