View unanswered posts | View active topics It is currently Tue Jul 29, 2014 2:57 pm






Reply to topic  [ 4 posts ] 
long subtraction confusion 
Author Message
Rookie

Joined: Sat Feb 08, 2014 4:28 pm
Posts: 3
Post long subtraction confusion
robotc-3.62 on Windows 8.1 64-bit.

Code:
task main()
{
   long a = 100;
   long c = 60000 - a;
}

The result of subtraction according to the PC-Based emulator is -5636. The correct answer, weirdly, ends up in the a variable.
Image

Code:
task main()
{
   long a = 100;
   long b = 60000;
   long c = b - a;
   /* optimizer blockers */
   a++;
   b++;
   c++;
   long d = a + b + c;
}

The values reported by the debugger when running under PC-Based Emulator at the line with the comment is a=59900, b=60000, c=59900. This time, the correct value is surprisingly stored into «c» in addition to being incorrectly stored into «a».
Image

So, I was attempting to use longs in my logic. I guess that I will have to look at what each attempt at long subtraction ends up doing with the debugger and determine, case by case, which variables end up with the right results and which variables get trashed. May someone verify that this is a bug? Are there any reliable workarounds for this behavior?


Attachments:
File comment: itzoDB0.png
itzoDB0.png
itzoDB0.png [ 56.4 KiB | Viewed 718 times ]
File comment: kdQ8kHS.png
kdQ8kHS.png
kdQ8kHS.png [ 50.31 KiB | Viewed 718 times ]
Sat Feb 08, 2014 5:22 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: long subtraction confusion
It does on a real NXT, too, so it's not just the emulator that exhibits this issue.

I'll notify the devs.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Feb 09, 2014 5:38 am
Profile WWW
Site Admin
Site Admin

Joined: Wed Jan 24, 2007 10:42 am
Posts: 601
Post Re: long subtraction confusion
There is a bug where large constants (i.e. those that don’t fit in a signed 16-bit number) are incorrectly optimized into “trinary” opcodes which try to store the constant in a two-byte signed value.

The immediate workaround is to disable “trinary optimization” in the compiler code generation preferences (you may need to switch to super user to do so).

Virtually all opcodes in ROBOTC are “binary” opcodes that take two operands. For example, “X = 60000” and “C -= A. So going back to the example in the referenced web link, the two relevant “C” source lines are.
long a = 100;
long c = 60000 - a;

Without any code optimization, these two lines generate the following VM opcodes:
A = 100
C = 60000
C -= A

This has been fixed for the next release of ROBOTC.

_________________
Timothy Friez
ROBOTC Developer - SW Engineer
tfriez@robotc.net


Tue Feb 11, 2014 3:39 pm
Profile
Rookie

Joined: Sat Feb 08, 2014 4:28 pm
Posts: 3
Post Re: long subtraction confusion
OK, disabling “Collapse Trinary” does cause this code to behave as expected. Thanks for the information about the workaround. Looking forward to 3.63 ;-).


Tue Feb 11, 2014 5:59 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.