ROBOTC.net forums
http://robotc.net/forums/

Interesting Compiler Bug
http://robotc.net/forums/viewtopic.php?f=1&t=2191
Page 1 of 1

Author:  Corun [ Mon Feb 15, 2010 10:56 am ]
Post subject:  Interesting Compiler Bug

Hiya, found an interesting compiler bug. I couldn't find anywhere to submit it. So, here it is:

Code:
#define ABS(a) (((a) > 0)?(a):(-(a)))
#define DEAD_ZONE (0.03)

bool needs_adjustment()
//
//Code segment: needs_adjustment(); Procedure: 1
//
{
  float ld = light_difference();
0000: 1702                     callSub(light_difference)         
0002: 262B4600330000           G70(float) = functionReturnValue    // long/float
  //float ald = ABS(ld);
  return (ABS(ld) > DEAD_ZONE);
0009: 3F013218002B46000B00     TestAndBranchFloatFar(0.000000 >= G70(float), L001C)
0013: AE2B49002B4600           G73 = G70(float)                    // Convert 'float' to 'short'
001A: 2716                     BranchNear(L0031)                 
001C: 262B4A00321800    L001C: G74(float) = 0.000000               // long/float
0023: 392B4A002B4600           G74(float) -= G70(float)            // float
002A: AE2B49002B4A00           G73 = G74(float)                    // Convert 'float' to 'short'
0031: AC2B4A002B4900    L0031: G74(float) = 0.03                   // Convert 'short' to 'float'
0038: 3F01323C002B4A000700     TestAndBranchFloatFar(0.030000 >= G74(float), L0047)
0042: 2B4801                   G72 = true                         
0045: 2704                     BranchNear(L004A)                 
0047: 2B4800            L0047: G72 = false                       
004A: 3633002B48        L004A: functionReturnValue = G72         
}
004F: F6                       Return()                            // needs_adjustment()


So the code is

Code:
#define ABS(a) (((a) > 0)?(a):(-(a)))
#define DEAD_ZONE (0.03)

bool needs_adjustment()
{
  float ld = light_difference();
  return (ABS(ld) > DEAD_ZONE);
}


Which gets compiled in to something like this:

Code:
bool needs_adjustment()
{
  float ld = light_difference();
  float temp;
  short temp1;
  if(0.0 >= ld)
  {
    temp = 0;
    temp -= ld;
    temp1 = (short)temp;
  }
  else
  {
    temp1 = (short)temp;
  }
  temp = 0.03;
  if(0.03 >= temp)
    return false;
  return true;
}


As you can see, this reduces to just the function returning (0.03 >= 0.03) which, assuming no float failure, is always true. So the function always returns false.
I'm pretty sure this is a compiler bug, and a weird one at that. Any ideas what's going on?

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/