Author Topic: Axe Optimization Tricks  (Read 12808 times)

0 Members and 1 Guest are viewing this topic.

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Axe Optimization Tricks
« 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.
« Last Edit: June 30, 2010, 01:25:09 pm by DJ Omnimaga »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Axe Optimization Tricks
« Reply #1 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?
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Axe Optimization Tricks
« Reply #2 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.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Axe Optimization Tricks
« Reply #3 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()
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Axe Optimization Tricks
« Reply #4 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?
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Axe Optimization Tricks
« Reply #5 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.
« Last Edit: June 30, 2010, 04:56:10 am by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Axe Optimization Tricks
« Reply #6 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




Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Axe Optimization Tricks
« Reply #7 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.
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Axe Optimization Tricks
« Reply #8 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? :)
« Last Edit: July 01, 2010, 01:45:05 pm by ztrumpet »

Offline LordConiupiter

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 339
  • Rating: +3/-0
  • Just one of the thousands of Axe-fans...
    • View Profile
Re: Axe Optimization Tricks
« Reply #9 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?
everytime that I was down, you would always come around, and get my feedback on the ground. (modified part from 'Seasons in the sun')

No matter how many errors are bothering you, always try to stay rel-Axe!

The HoMM project will be resumed as soon Axe 1.0.0 will be released!
Projects:
Code: [Select]
HoMM:   [==--------]    Project 'resumed': I'm suffering overwhelming new ideas being popped up in my dreams :P
tiDE:   [----------]    Explored and understood the main part of the code: just started writing a Tokenizer.



password of the week: uvanapererubupa (Any pronunciation is the right one ;) )   :D click me, and you'll be raided :D

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Axe Optimization Tricks
« Reply #10 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. :)

Offline LordConiupiter

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 339
  • Rating: +3/-0
  • Just one of the thousands of Axe-fans...
    • View Profile
Re: Axe Optimization Tricks
« Reply #11 on: July 01, 2010, 02:23:45 pm »
oh, because it has to be converted to basic type number when you store into Ans?
everytime that I was down, you would always come around, and get my feedback on the ground. (modified part from 'Seasons in the sun')

No matter how many errors are bothering you, always try to stay rel-Axe!

The HoMM project will be resumed as soon Axe 1.0.0 will be released!
Projects:
Code: [Select]
HoMM:   [==--------]    Project 'resumed': I'm suffering overwhelming new ideas being popped up in my dreams :P
tiDE:   [----------]    Explored and understood the main part of the code: just started writing a Tokenizer.



password of the week: uvanapererubupa (Any pronunciation is the right one ;) )   :D click me, and you'll be raided :D

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Axe Optimization Tricks
« Reply #12 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

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Axe Optimization Tricks
« Reply #13 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.
« Last Edit: July 01, 2010, 04:51:04 pm by nemo »


Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Axe Optimization Tricks
« Reply #14 on: July 01, 2010, 04:41:03 pm »
No its not nemo.  I gave a counter example in the other post.
___Axe_Parser___
Today the calculator, tomorrow the world!