NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by mrblp »

Hello there,

Does anyone use the optimization setting of the nbc compiler? There are the settings "-Z=1" or "-Z=2". I switched to enhanced firmware (finally :-) after I added some changes to it...) and then activated the "-Z=1 -EF" switches to the nbc compiler. Well - it seems to me that they do not work together because the code does not work as expected. If only "-Z=1" or only "-EF" is set everything works fine.

Thank you!

Bye - marvin
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by spillerrec »

I have always been using "Z2" together with "-EF" without any issues. Notice though that there isn't a "=" in this, it might be what that is causing issues. (I do find this strange though, as most others parameters are using "=" but that is apparently how it is...)
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by m-goldberg »

I normally run the compiler using a command line of the form

nbc -sm- -EF -ER=3 -Z2 test.nxc -O=test.rxe -r

I have not had any problems with this. Note, as spillerrec says, the switch is -Z2 not -Z=2.
Regards, Morton
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by muntoo »

-Z2 is the default optimization level in BricxCC. John Hansen recommends you use it, unless you get some insane errors (which you should report).
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by mrblp »

Hello all,

thank you for your answers. You are right, I used it with the "=" in between. Sometimes reading helps... Now I tried "-Z1" and "-Z2" and with "-Z1" everything works (and the files are the same size as with no optimization switch) and with "-Z2" the program behaves insane again.

OK, before I post my program here I will do some studies onto it. I have an idea why the program is behaving insane...

Have a nice day - I will answer this in the evening (european time ;-).

Bye - marvin
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by mrblp »

Hello there,

Now I tried again with optimization -Z2 and some different versions of nbc and things - the code still behaves not as it does with no optimization. To cut a long story short a state machine for running a square does not behave as expected. When compiled without optimization the robot runs a square, it runs forward when compiled with optimization active. If someone takes a view onto this? Thank you :-)

Code: Select all

#define OutSetAngleSyncBrake(oopz, opwrz, oaz, otz, obz) \
          { Yield(); RotateMotorEx(oopz, opwrz, oaz, otz, TRUE, obz); }

bool task_drive=TRUE;

void SR_Curve(int8_t Speed, bool Brake, int16_t Degree)
{
  #define CurveAngle ((360/26)*Degree)
  OutSetAngleSyncBrake(SR_MOTOR_DRIVE, OutPwrRev(Speed), CurveAngle, 35, Brake);
}

void SR_Straight(int8_t Speed, bool Brake, uint32_t Way)
{
  #define StraightAngle (Way*34)
  OutSetAngleSyncBrake(SR_MOTOR_DRIVE, OutPwrRev(Speed), StraightAngle, 0, Brake);
}

task drive()
{
  enum drive_state_t
  {
    drive_state_start = 1,
    drive_state_straight,
    drive_state_curve,
    drive_state_rotate,
  } drive_state;

  // Initialize state variable
  drive_state = drive_state_start;
  // Only drive when task should run
  // Check for task_drive before any driving instruction inside loop!
  while (task_drive!=FALSE)
  {
    switch (drive_state)
    {
    case drive_state_start:
      // Start: Drive a bit back to release the bumper
      SR_Straight(70, FALSE, -1);
      drive_state = drive_state_straight;
      break;
    case drive_state_straight:
      // Straight
      SR_Straight(70, FALSE, 20);
      drive_state = drive_state_curve;
//      drive_state = drive_state_rotate;
      break;
    case drive_state_curve:
      // Curve
      SR_Curve(70, FALSE, 90);
      drive_state = drive_state_straight;
      break;
    case drive_state_rotate:
      // Rotation
      SR_Rotate(75, FALSE, 90);
      drive_state = drive_state_straight;
      break;
    default:
      drive_state = drive_state_straight;
      break;
    }
  }
}
Bye - marvin
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by m-goldberg »

The problem might be with the following function:

Code: Select all

void SR_Curve(int8_t Speed, bool Brake, int16_t Degree)
{
  #define CurveAngle ((360/26)*Degree)
  OutSetAngleSyncBrake(SR_MOTOR_DRIVE, OutPwrRev(Speed), CurveAngle, 35, Brake);
}
The "local" #define (which I doubt the compiler treats as local despite its lexical position in the code) may be confusing the optimizer.

Frankly I think your use of nested macros is just plain bad practice. A simple void function should work fine.

Code: Select all

void SR_Curve(int8_t Speed, bool Brake, int16_t Degree)
{
  Yield();
  RotateMotorEx(SR_MOTOR_DRIVE, OutPwrRev(Speed), (360/26) * Degree,
                35, TRUE, Brake);
}
Regards, Morton
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by mrblp »

Hello,
m-goldberg wrote:The "local" #define (which I doubt the compiler treats as local despite its lexical position in the code) may be confusing the optimizer.
Thank you, I will try if changing this will change anything.

Edit: I tried both - nothing helped :-(
Frankly I think your use of nested macros is just plain bad practice.
I am sorry but I do not agree here. I want to work with a structured API so I am changing it via header files and those macros. The macro at the beginning is just taken from the changed API header file to make you able to understand the code.

There are two alternatives: inline functions or changing the API direct. The latter is no option - I do not want to use a custom nxc. inline functions - OK this may be an alternative - I have to think about this.

The funny thing about the optimization is that it all works without the -EF option. It's just the combination -EF and -Z2. Just -EF or just -Z2 are both working.

Bye - marvin
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by afanofosc »

I would need a sample program to check the generated code from before I could say what might be going wrong. Are you for sure using the latest test release of the compiler? There are a few places where optimization works differently for the enhanced firmware vs the standard firmware. These are usually to work around standard firmware bugs. I assume that you are actually running the code that was compiled with -EF on a brick that actually is running the enhanced NBC/NXC firmware.

Perhaps you could do a diff of the NBC output without -EF against the NBC output with -EF and -Z2 in both cases. I think there will be few differences, but I could be very wrong about that.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mrblp
Posts: 82
Joined: 02 Oct 2010, 14:33

Re: NXC: Optimization setting (-Z=1 / -Z=2) and -EF

Post by mrblp »

Hello John,
afanofosc wrote:Are you for sure using the latest test release of the compiler?
Yes - the one from march, 4th. But I use EF 1.28 - hope that is no problem?
Perhaps you could do a diff of the NBC output without -EF against the NBC output with -EF and -Z2 in both cases. I think there will be few differences, but I could be very wrong about that.
Thank you - I did not have any view on those outputs - why not? The nbc files are exactly the same but the list file and the symbol table changed the order of the functions and things.

Should I send them to you?

Bye - marvin

PS: Without -Z2 everythings works as expected. I just wondered if I did anything completely wrong. This seems not to be the case - perhaps things need not to be perfect ;-)
Bye Marvin

- "I think you ought to know I'm feeling very depressed." - (Android Marvin in "The Hitchhiker's Guide to the Galaxy" by Douglas Adams, 1978)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest