Omnimaga

Calculator Community => TI Calculators => Axe => Topic started by: calc84maniac on June 30, 2010, 12:24:05 am

Title: Axe Optimization Tricks
Post by: calc84maniac on June 30, 2010, 12:24:05 am
NOTE: THIS IS NOT FOR SOURCE CODE OPTIMIZATIONS, JUST THE FINAL EXECUTABLE!

This is a topic to share and comment on Axe optimizations. I'll start out with a very useful one that can decrease code size quite a bit:

Instead of
If A=B
do
!If A-B

And instead of
If A!=B
do
If A-B

This optimization takes advantage of the fact that the If statement checks for non-zero and !If checks for zero. No speed/space is wasted on the boolean expressions returning just a 0 or 1.
Title: Re: Axe Optimization Tricks
Post by: DJ Omnimaga on June 30, 2010, 02:41:07 am
mhmm good idea since the manual and doc might not include everything or that some stuff might not be easily noticeable.

EXPR/2/2/2 is larger than EXP/8, but faster, right?
What about EXPR*2*2*2 vs EXP*8?
Title: Re: Axe Optimization Tricks
Post by: Builderboy on June 30, 2010, 02:57:03 am
Thats true for division, but testing showed no difference in speed or size for multiplication so perhaps its auto optimized.
Title: Re: Axe Optimization Tricks
Post by: Quigibo on June 30, 2010, 03:30:10 am
Yeah, there's no point in doing that for multiplication unless the numbers are larger than the auto-opt numbers, in which case its no longer optimized for size.

I guess another trick is leaving out arguments that are already duplicated.  For instance, Output(0,0,Str1) can be optimized to Output(0,,Str1) since the second argument does not need to be changed from the first argument.  Similarly, Output(0,1,Str1) can be optimized to Output(0,+1,Str1) which is kinda similar to the close variable loading optimization.  There are many more variates of this trick you can use on a lot of commands. Its especially helpful with Copy() and Exch()
Title: Re: Axe Optimization Tricks
Post by: DJ Omnimaga on June 30, 2010, 04:31:17 am
wow I didn't know that! Thanks Quigibo. Does this optimize only for the source or also the executable?
Title: Re: Axe Optimization Tricks
Post by: Quigibo on June 30, 2010, 04:55:55 am
This entire thread is should ONLY be optimizations to the executable.  There would be no reasons to make source code "optimizations" becasue its irrelevant to the final product.  In fact, larger source code often leads to better readability and organization which is why I almost always cap my end parenthesis, leave comments, and sometimes indent when I've got a lot of nested blocks.
Title: Re: Axe Optimization Tricks
Post by: Deep Toaster on June 30, 2010, 05:50:23 am
Yeah, there's no point in doing that for multiplication unless the numbers are larger than the auto-opt numbers, in which case its no longer optimized for size.

I guess another trick is leaving out arguments that are already duplicated.  For instance, Output(0,0,Str1) can be optimized to Output(0,,Str1) since the second argument does not need to be changed from the first argument.  Similarly, Output(0,1,Str1) can be optimized to Output(0,+1,Str1) which is kinda similar to the close variable loading optimization.  There are many more variates of this trick you can use on a lot of commands. Its especially helpful with Copy() and Exch()

Wow, that's new.

When I first looked at the example programs, I noticed things like that (e.g., having →[VAR] on a blank line) that freaked me out at first, being used to BASIC syntax errors as I am :P
Title: Re: Axe Optimization Tricks
Post by: DJ Omnimaga on June 30, 2010, 01:23:26 pm
This entire thread is should ONLY be optimizations to the executable.  There would be no reasons to make source code "optimizations" becasue its irrelevant to the final product.  In fact, larger source code often leads to better readability and organization which is why I almost always cap my end parenthesis, leave comments, and sometimes indent when I've got a lot of nested blocks.
Ah ok thanks for clarifying. I will append this first sentence to Calc84maniac first post to make sure people see it.
Title: Re: Axe Optimization Tricks
Post by: ztrumpet on July 01, 2010, 01:44:44 pm
Instead of
If A=B
do
!If A-B

And instead of
If A!=B
do
If A-B

This optimization takes advantage of the fact that the If statement checks for non-zero and !If checks for zero. No speed/space is wasted on the boolean expressions returning just a 0 or 1.
That's awesome!  Thanks for sharing! ;D

Here's something I've been wondering:
Which is more optimized, Output with one argument or two?  I'm pretty sure they compile to the same code, but can anyone confirm this? :)
Title: Re: Axe Optimization Tricks
Post by: LordConiupiter on July 01, 2010, 02:16:01 pm
I didn't think so: with one argument is shorter, else Quigibo wouldn't have said it in this topic to do it that way

A question: is it quicker to store into Ans, since it is a register, or to a local variable like just A?
Title: Re: Axe Optimization Tricks
Post by: ztrumpet on July 01, 2010, 02:21:23 pm
A question: is it quicker to store into Ans, since it is a register, or to a local variable like just A?
Because this is completely different from basic, it's faster to store to a variable like A.  Ans takes a lot of time to store to. :)
Title: Re: Axe Optimization Tricks
Post by: LordConiupiter on July 01, 2010, 02:23:45 pm
oh, because it has to be converted to basic type number when you store into Ans?
Title: Re: Axe Optimization Tricks
Post by: ztrumpet on July 01, 2010, 03:23:20 pm
oh, because it has to be converted to basic type number when you store into Ans?
Yes, and it is also storing to a TIOS variable, making it larger and slower. :)
If you store to an "Axe Var", then you're just changing the content of two, easy to use bytes. :D
Title: Re: Axe Optimization Tricks
Post by: nemo on July 01, 2010, 04:31:45 pm
as said above, If A=B is the same as !If A-B.
similarly, If A=B xor (C=D) is equivalent to If A-B xor (C-D)


edit: my bad.
Title: Re: Axe Optimization Tricks
Post by: Quigibo on July 01, 2010, 04:41:03 pm
No its not nemo.  I gave a counter example in the other post.
Title: Re: Axe Optimization Tricks
Post by: Deep Toaster on July 01, 2010, 09:49:47 pm
oh, because it has to be converted to basic type number when you store into Ans?
Yes, and it is also storing to a TIOS variable, making it larger and slower. :)
If you store to an "Axe Var", then you're just changing the content of two, easy to use bytes. :D

Ans takes memory for the exponent and for negation, plus seven bytes of data, many of which are unnecessary since Axe only allows storing 2-byte integers (between 0 and 65535). These are first stored to OP1 (I think), then all nine bytes of data (including seven which would otherwise have been unnecessary) have to be moved to Ans, so it's a lot slower. Storing to an Axe variable location is just that: it simply copies the two bytes over there :)

The only reason I can see for storing to Ans within an Axe program is to make the returned value available to the user (for example, as a subroutine for BASIC programs written in Axe).
Title: Re: Axe Optimization Tricks
Post by: ztrumpet on July 01, 2010, 10:35:10 pm
oh, because it has to be converted to basic type number when you store into Ans?
Yes, and it is also storing to a TIOS variable, making it larger and slower. :)
If you store to an "Axe Var", then you're just changing the content of two, easy to use bytes. :D

Ans takes memory for the exponent and for negation, plus seven bytes of data, many of which are unnecessary since Axe only allows storing 2-byte integers (between 0 and 65535). These are first stored to OP1 (I think), then all nine bytes of data (including seven which would otherwise have been unnecessary) have to be moved to Ans, so it's a lot slower. Storing to an Axe variable location is just that: it simply copies the two bytes over there :)

The only reason I can see for storing to Ans within an Axe program is to make the returned value available to the user (for example, as a subroutine for BASIC programs written in Axe).
Almost, but not quite.  For example, in Axe, the number 65535 would be FF FF, but in Ans it looks like $00 $84 $65 $53 $50 $00 $00 $00 $00.  Because the TIOS stores this number so oddly, Axe has to take even more time to convert it. :)
Title: Re: Axe Optimization Tricks
Post by: Deep Toaster on July 01, 2010, 10:47:39 pm
Oh, right, forgot about that. Why does TI store floating-points like that? And what happens if there's a hex-only value there (e.g.
Code: (Z80 Assembly) [Select]
$00 $8D $01 $23 $45 $67 $89 $AB $CD ;notice $AB and $CD don't work as decimal numbers
)?
Title: Re: Axe Optimization Tricks
Post by: DJ Omnimaga on July 02, 2010, 02:32:09 am
wow o.o I guess it can be understood since it supports floating points, but darn that is large and confusing x.x

I think I'm gonna be fine with my little 1 byte and 2 byte integers for a while ;D
Title: Re: Axe Optimization Tricks
Post by: Builderboy on July 02, 2010, 02:46:18 am
Yeah, it does it that way for precision.  For example, if it was stored in Binary, there would be horrible rounding problems (like how there is no way to represent .1 in Binary accurately) so they use this digit approach which may be slower but so much more precise
Title: Re: Axe Optimization Tricks
Post by: Deep Toaster on July 04, 2010, 05:03:44 am
So what would happen if one of the bytes contained non-decimal data, such as $CA?
Title: Re: Axe Optimization Tricks
Post by: calcdude84se on July 04, 2010, 10:18:25 am
You'd have to have modified it by hand using, say, CalcSys. As for what happens, it displays garbage and the calculations are somewhat unpredictable, or at any rate abnormal.