Helpful ReplyHot!minimise code size

Author
brownt
Super Member
  • Total Posts : 231
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
2018/04/13 23:55:52 (permalink)
0

minimise code size

Are there any tips or tricks for minimising the compiled code size, aside from using the PRO version of the compiler.
#1
Mysil
Super Member
  • Total Posts : 2693
  • Reward points : 0
  • Joined: 2012/07/01 04:19:50
  • Location: Norway
  • Status: online
Re: minimise code size 2018/04/14 01:04:18 (permalink) ☄ Helpfulby brownt 2018/04/14 19:04:18
+1 (1)
Hi,
Avoid floating point calculations,
avoid printf(...) formatting.
 
   Mysil
#2
RISC
Super Member
  • Total Posts : 5109
  • Reward points : 0
  • Status: offline
Re: minimise code size 2018/04/14 02:01:04 (permalink)
+1 (1)
Hi,
 
Nothing replaces experience in that area. An optimizer will lead to limited optimization if the C code is well written.
 
Depending upon assembly instructions, there are some constructions in C which generate much more code than others.
I personally sometimes tries alternatives ways to see which assembly is generated to decide what is best when I need to do it.
One area I personally always check carefully is bit manipulation instructions.
Due ot the way C handles bitfields, I have noticed that, depending upon the compiler (in particular XC16 and XC32) , it is generally much better to use full register names to perform bit manipulation rather than to use bitfields. Code can be 2 or 3 times bigger for the same C statement. If this is executed within an interrupt this can have really large impact on the maximum interrupt frequency
 
Regards
#3
Ian.M
Super Member
  • Total Posts : 13114
  • Reward points : 0
  • Joined: 2009/07/23 07:02:40
  • Location: UK
  • Status: offline
Re: minimise code size 2018/04/14 02:26:37 (permalink) ☄ Helpfulby brownt 2018/04/14 19:04:02
+1 (1)
On 8 bit chips with only basic ALUs, avoid division by other than powers of two.
On chips without barrel shifters, avoid shifting by a variable number of bits.

--
NEW USERS: Posting images, links and code - workaround for restrictions.
I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
#4
du00000001
Just Some Member
  • Total Posts : 1788
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: online
Re: minimise code size 2018/04/14 03:15:34 (permalink)
0
This is a wide field.
The best approach is to realize a given function(ality) with as little code as possible - a discipline of its own.
Beyond that there are some commonalities between mcu architectures (decrementing an testing for zero is e.g. more efficient than incrementing and testing for some limit), but often the tradeoff is code size vs. speed (vs. RAM "consumption").
And you need to know your compiler as well - plus some more detail common to most (if not all) compilers.
 
Did you know there are 2 "flavors" of switch-case code generation available? A very fast one with constant execution time (occupying slightly more Flash space for a single-digit number of cases, beyond that being fast AND small) and the "usual one" that's getting slower "by the case(s)". But such knowledge comes only with experience and curiosity.
 
As I pride myself in writing extremely fast and small embedded software (e.g. an automotive HVAC controller in1400 bytes (including data)), I might know well what I'm doing.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#5
qɥb
Monolothic Member
  • Total Posts : 2461
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: minimise code size 2018/04/14 04:04:54 (permalink)
+2 (2)
The biggest tip is, examine the LST file output by the compiler after compi1ling your code, so you come to understand what it has to do to implement what you request.
You will quickly see what can be implemented simply, and what takes a lot of work to do.
 

PicForum "it just works"
#6
NKurzman
A Guy on the Net
  • Total Posts : 15825
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: minimise code size 2018/04/14 08:20:44 (permalink) ☄ Helpfulby brownt 2018/04/14 19:03:28
+2 (2)
Use the smalllest unsigned variable that will do the job. Do not just use “int” everywhere And remember that 8 bit PICs have a bit variable for flags.
#7
Jump to:
© 2018 APG vNext Trial Version 4.5