Editing BoostC Optimizations

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 6: Line 6:
  
  
= some notes from the forum still to be formatted =
+
== some notes from the forum still to be formatted ==
  
 
The following post yeilded several real and unreal optimizations  
 
The following post yeilded several real and unreal optimizations  
Line 12: Line 12:
  
  
== Incrementing ==
+
==== Incrementing ====
  
 
subtracting/deincrementing is faster than adding/incrementing
 
subtracting/deincrementing is faster than adding/incrementing
Line 18: Line 18:
 
Valid?  believed not to be true
 
Valid?  believed not to be true
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
+
==== Avoid Division ====
 
 
== Avoid Division ==
 
  
 
not using division saves ~35 bytes
 
not using division saves ~35 bytes
  
Valid?  division is slow, but we think not by powers of 2, which are shifts ( if divisor know at compile time )
+
Valid?  division is slow, but we think not by poweres of 2, which are shifts ( if divisor know at compile time )
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
+
==== Left vs Right Shift ====
 
 
== Left vs Right Shift ==
 
  
 
Left Shifting is faster than Right
 
Left Shifting is faster than Right
  
Valid?  believed not to be true.
+
==== Function call in ISR =====
 
 
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
 
 
== Function call in ISR ==
 
  
 
Calling a function in an ISR takes longer than outside an ISR
 
Calling a function in an ISR takes longer than outside an ISR
 
 
Valid?  believed not to be true
 
Valid?  believed not to be true
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
+
==== Function called Only Once =====
  
== Function called Only Once ==
+
-Never use a function for a single operation, you waste time calling and returning
  
Never use a function for a single operation, you waste time calling and returning
+
Valid?  believed not to be true
  
Valid?  believed not to be true.
+
==== Shift for Division =====
 
 
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
 
 
== Shift for Division ==
 
  
 
Use bit shifting instead of division for speed savings and possible ram savings
 
Use bit shifting instead of division for speed savings and possible ram savings
Line 58: Line 45:
 
(shifting one bit right is equal to dividing by two, etc)
 
(shifting one bit right is equal to dividing by two, etc)
  
Valid?  believed not to be true, optimizer is smart enough to do this.
+
Valid?  believed not to be true, optimizer is smart enough to do this
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
== Arrays and Pointers ==
+
==== Arrays and Pointers ====
  
 
Both arrays and pointers generate a lot of code. Use plain variables instead where possible.
 
Both arrays and pointers generate a lot of code. Use plain variables instead where possible.
Line 91: Line 77:
 
something similar happens when you are working with struct variables.
 
something similar happens when you are working with struct variables.
  
Valid? We are pretty sure it is.
 
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
== ROM vs RAM ==
 
  
Write constants into ROM not RAM  
+
==== ROM vs RAM ====
 +
-Write constants into ROM not RAM  
  
 
Valid?  In that you save RAM at the expense of ROM, normally you have more ROM than RAM.
 
Valid?  In that you save RAM at the expense of ROM, normally you have more ROM than RAM.
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
+
==== Return Values ====
 
 
== Return Values ==
 
  
 
Do not return values from functions that you wont use/do not need.
 
Do not return values from functions that you wont use/do not need.
Line 109: Line 91:
 
Valid?  We think so, seems like it must be.
 
Valid?  We think so, seems like it must be.
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
== ROM vs RAM ==
+
==== ROM vs RAM ====
  
Reuse common code or write a common function and call it will save huge amounts of space
+
 
 +
-Reuse common code or write a common function and call it will save huge amounts of space
 
( i suspect agressive opt in BoostC may negate this last but i've yet to test)
 
( i suspect agressive opt in BoostC may negate this last but i've yet to test)
  
Valid?  We think so, but even if not, it good coding pratice.
+
Valid?  We think so, but even if not it good coding pratice.
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
== ()?:; ==
 
  
 
i had forgotten about this one but the good old ()?:; (tri-unary?) function conditionally yields  
 
i had forgotten about this one but the good old ()?:; (tri-unary?) function conditionally yields  
Line 130: Line 110:
 
left empty or there is no value in using it and will actually result in longer code.
 
left empty or there is no value in using it and will actually result in longer code.
  
 
+
CODE
    (test)?(result=1):(result=0);
+
(test)?(result=1):(result=0);
  
 
VS
 
VS
 +
CODE
 +
if(test)
 +
{
 +
  result=1;
 +
}
 +
else
 +
{
 +
  result=0;
 +
}
  
    if(test)
 
    {
 
          result=1;
 
    }
 
    else
 
    {
 
          result=0;
 
    }
 
  
  
 +
i had forgotten about this one but the good old ()?:; (tri-unary?) function conditionally yields slightly smaller/faster code...
  
 +
I tried that one out and there's no gain on SourceBoost C (for PIC16) on any optimization level.
 +
 +
For code like that, I would expect this:
 +
CODE
 +
result = (test ? 1 : 0)
 +
to do better, but it doesn't, either...
 +
 +
 
 +
 +
 +
QUOTE (edt @ Dec 29 2006, 01:23 PM)
 +
QUOTE (emte @ Dec 27 2006, 05:07 PM)
 
i had forgotten about this one but the good old ()?:; (tri-unary?) function conditionally yields slightly smaller/faster code...
 
i had forgotten about this one but the good old ()?:; (tri-unary?) function conditionally yields slightly smaller/faster code...
  
Line 151: Line 145:
  
 
For code like that, I would expect this:
 
For code like that, I would expect this:
 +
CODE
 +
result = (test ? 1 : 0)
 +
to do better, but it doesn't, either...
  
    result = (test ? 1 : 0)
 
  
to do better, but it doesn't, either...
+
 
 +
 
 +
 
 +
Odd, altho my tests were simply:
 +
 
 +
CODE
 +
    (testBit)?(_ONE = 1):(_ONE = 0);
 +
0039  08A1      MOVF main_1_testBit, F
 +
003A  1903      BTFSC STATUS,Z
 +
003B  283E      GOTO    label268438744
 +
003C  1486      BSF gbl__ONE,1
 +
003D  283F      GOTO    label268438746
 +
003E        label268438744
 +
003E  1086      BCF gbl__ONE,1
 +
003F        label268438746
 +
 
 +
        if(testBit)
 +
003F  08A1      MOVF main_1_testBit, F
 +
0040  1903      BTFSC STATUS,Z
 +
0041  2844      GOTO    label268438747
 +
0044        label268438747
 +
 
 +
        {
 +
            _ONE = 1;
 +
0042  1486      BSF gbl__ONE,1
 +
 
 +
        }
 +
        else
 +
0043  2837      GOTO    label268438739
 +
 
 +
        {
 +
            _ONE = 0;
 +
0044  1086      BCF gbl__ONE,1
 +
 
 +
        }
  
  
Line 167: Line 197:
 
that with the new version, it did not even occur to me that it may have changed  
 
that with the new version, it did not even occur to me that it may have changed  
  
 
more in the post  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
 
----------------
 
----------------
 
 
do not get this one
 
  
 
Hmm it looks like the if-else might actually be one line shorter now ... altho it is
 
Hmm it looks like the if-else might actually be one line shorter now ... altho it is
Line 194: Line 219:
 
... hmm maybe i should add that example to handy functions ...
 
... hmm maybe i should add that example to handy functions ...
  
Post:  http://forum.sourceboost.com/index.php?showtopic=2433&pid=9574&mode=threaded&start=#entry9574
 
  
= Still More we are working on =
 
  
== Soft vs Hard Stack ==
+
==== Still More we are working on ====
 +
 
  
 
Software stack is slower than hardware ( but deeper )
 
Software stack is slower than hardware ( but deeper )
Line 204: Line 228:
 
Valid? we think so.
 
Valid? we think so.
  
== Inline Functoion ==
+
Inline function does not really use call and return so is faster, but if called multiple time may take more space ( but for short funtions the call return... can take more space than the function itself.  Would be nice if someone did measurements.
 
 
Inline function does not really use call and return so is faster, but if called multiple time may take more space ( but for short funtions the call return... can take more space than the function itself.  Would be nice if someone did measurements. [[BoostC Inline Functions]]
 
  
 
Valid? we think so.
 
Valid? we think so.
  
== Local vs Global ==
 
  
 
Local values vs global variables.
 
Local values vs global variables.
 
Issue not understood, local may require some stack heap management, but space should be recovered when function exits so local variable saves memory overall.  Probably you should follow good pratices and use variables of th narrowest scope possible.
 
 
== Links ==
 
 
[http://user.it.uu.se/~jakob/publications/engblom-esc-sf-2001.pdf Getting the Least Out of Your C Compiler Class #508, Embedded Systems Conference San Francisco 2001]
 
 
[[Category:BoostC]][[Category:PIC]]
 

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)