caramoan tour package

caramoan tour package

Author Topic: Code Compression (tips and tricks)  (Read 15290 times)

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Code Compression (tips and tricks)
« on: March 13, 2008, 09:52:56 PM »
mga masters/experts/feeling experts,
hihingi sana ako ng tips about code compression sa CCS C..
kapag nagpo-program kayo ng PicMicros,
ano ba mga tricks nyo para maka-save kayo ng RAM saka ROM space???
saka, any tips on how to speed-up processing time?  ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Code Compression (tips and tricks)
« on: March 13, 2008, 09:52:56 PM »

Offline RRcom

  • CR2032 Battery
  • **
  • Posts: 45
  • Pogi/Ganda Points: 3
Re: Code Compression (tips and tricks)
« Reply #1 on: March 14, 2008, 02:36:07 AM »
maraming paraan dyan pero meron ding kapalit ex. matipid sa program memory malakas naman sa system memory in vice versa, kung ang code mo ay laging inuulit gumamit ka ng subroutines ito yung mga function sa C, sa ganitong paraan mapapdali rin ang program ganito ang ginagamit ngayon sa mga software natin katulad ng complex os na windows at mga games yun yung object oriented sa c++ gamit ang class, pero sa c katulad din yun ng function, ex upang mapadali ang yong program, hatiin mo yun sa marami kung kaylangan mo ng mag hahandle sa keypad gumawa ka ng function para dito kalimutan mo muna ang lahat kundi dun ka lang tumutok at pagnatapos mo na yon pwede naman yung sa lcd and so on, at pag nagawa mo na yung lahat pwede mo na silang pagsamasamhin kahit kalimutan mona kung pano mo sila ginawa. ex pag nag push ako ng button sa keypad si kepad function na bahala dyan kahit nalimutan kona kung pano ko sya ginawa at ipasa naman nya kay lcd at bahala narin sya kung ano gagawin nya basta alam ko ginawa ko sya para i display ang letter, yan ang tamang paraan ng program, maganda yan kung marami kayo gumgawa, halimbawa ikaw sa keypad function at ako naman sa lcd, yun nga rin gusto ko sa forum na to i practis yung ganong tecnic pwede tayo dito makabuo ng complex program hati hati lang ang gagawa ex meron isa sa member my project na gusto na gawin pero software nalang kulang pwede hatiin natin dito sa forum at gawin ng gusto matuto sa program habang nadedevelope yung program lahat natututo, at syempre praktis yan sa gustong gumaling sa object oriented programming at dyan makakatipid ka ng code dahil fuction nalang ginagamit.
Made in Calamba City

Philippine Electronics Forum

Re: Code Compression (tips and tricks)
« Reply #1 on: March 14, 2008, 02:36:07 AM »

Offline 0b00000111

  • Technical People
  • Solar Power Satellite
  • *****
  • Posts: 6129
  • Pogi/Ganda Points: 398
  • There is no delight in owning anything unshared.
Re: Code Compression (tips and tricks)
« Reply #2 on: March 14, 2008, 06:15:47 AM »
8-bit micro ba ang gamit mo? tulad ng sabi ni RRcom madaming paraan para mapaliit ang code...

about processing time, madami din techniques. like 8-bit micros can handle efficiently 8-bit data like unsigned char rather than float and double data types.

about RAM saving, may trade-off ang static at global variables versus local variables and vice-versa. sa ANSI C, ang static at global variables ay declared at mag-eexist na na pagkarun pa lang ng program and hanggang matapos ang program .. ang local variables naman ay created upon a code block (usually a bracket) and destroyed upon the end of the code block.  so sa static at global, di na siya magcecreate ng variable everytime na papasok sa isang code (+ the danger of using global variables about accidental modification ng ibang functions), yun nga lang forever siyang allocated (much faster)... while ang local variable naman will be slower pero makaka save ka sa memory... you just have to weigh kung alin ang gagamitin mo sa kanila...

also, yung mga constants pwede mo siya ilagay sa ROM instead sa RAM (you have to check your compiler about this, kasi iba iba sila ng implementation.. tulad ng sa MPLAB C18, meron siyang rom at ram modifiers)

for ROM space, avoid as much as possible yung copy-paste ng block of codes... hanggat maaari igroup mo sila as functions... at flowchart mo muna, doon mo makikita kung paano mo pa icompress yung codes mo.... :)
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Philippine Electronics Forum

Re: Code Compression (tips and tricks)
« Reply #2 on: March 14, 2008, 06:15:47 AM »

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #3 on: March 14, 2008, 06:41:46 AM »
Code compression for PIC, AFAIR:
1. Use do {} while (--var); instead of for() .  This translates to decfsz
2. Use bit variables as much as possible. "if" compares translates this to btfss/btfsc and assignment to bcf/bsf
3. Use "unsigned char" if possible (if not #2 ). Unsigned comparison and operations has smaller code.
4. Sometimes built-in library has large code. If this happens create your own like using printf. printf uses large code in the lib. Printf has formatting include that you may not use at all.
5. Make your code into a function if used many times. Avoid cut-and-paste.
6. Avoid multiplications and divisions.  These add large codes also. Use shifts and add or successive subtraction. Or simplify the equation.
7. Generate assembly listing and look into the generated code. Analyze and make some code changes, for example, make some operation first than the other and recompile. Then check again the .lst.  Sometimes you can gain code size by re-arranging things.  Then you could use INLINE ASSEMBLY if you can make the generated asm smaller.

I can't think of anymore. I will add more to this later if I can remember.
The Cebuano Geek

Philippine Electronics Forum

Re: Code Compression (tips and tricks)
« Reply #3 on: March 14, 2008, 06:41:46 AM »

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #4 on: March 14, 2008, 09:56:07 AM »
8-bit micro ba ang gamit mo?
Oo, 8-bit lang yun.. pic16LF73 gamit ko ngayon..
Quote
about processing time, madami din techniques. like 8-bit micros can handle efficiently 8-bit data like unsigned char rather than float and double data types.
kaya pala yung library file na na-download ko, unsigned char ang ginagamit..
1. Use do {} while (--var); instead of for() .  This translates to decfsz
marami akong ginamit na for().. hehe  ;D
Quote
6. Avoid multiplications and divisions.  These add large codes also. Use shifts and add or successive subtraction. Or simplify the equation.
paano ito gawin sa CCS C?  ???
kelangan ko laging mag-multiply saka mag-divide ng int variables..  :(
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Philippine Electronics Forum

Re: Code Compression (tips and tricks)
« Reply #4 on: March 14, 2008, 09:56:07 AM »

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #5 on: March 14, 2008, 10:28:36 AM »
... ;Dpaano ito gawin sa CCS C?  ???
kelangan ko laging mag-multiply saka mag-divide ng int variables..  :(

It depends on your equation. You could simplify it by shifting and add/sub.
Post your equation here, I will help you simplify and code it.
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #6 on: March 14, 2008, 10:43:54 AM »
Post your equation here, I will help you simplify and code it.
for example meron akong int variable na "input", yung present value nya is galing sa result ng ADC..
yung range nya is {0.. 255}, pero gusto ko syang i-round off sa range na {64.. 0}..
Code: [Select]
input=read_adc();
.
.
input=input*(-1) / 4 + 64 ;
para syang ganito: y = -0.25x + 64
paano ba sya isi-simplify?  ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #7 on: March 14, 2008, 11:11:47 AM »
for example meron akong int variable na "input", yung present value nya is galing sa result ng ADC..
yung range nya is {0.. 255}, pero gusto ko syang i-round off sa range na {64.. 0}..
Code: [Select]
input=read_adc();
.
.
input=input*(-1) / 4 + 64 ;
para syang ganito: y = -0.25x + 64
paano ba sya isi-simplify?  ???


y = x >> 2;
y -= 64;
y = -y;

The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #8 on: March 14, 2008, 11:24:41 AM »
y = x >> 2;
y -= 64;
y = -y;
thanks zer0w1ng! one pogi point for you..  ;D
pero, di ko gets yung first 2 lines..  ??? hehe  ;D
sory po, beginner level pa lang ako s C language..  :(
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #9 on: March 14, 2008, 11:53:17 AM »
Thanks.

For the 1st line is shift right 2 times or divide by 4.
The second line is like y = y - 64.  This is just a C shortcut.
The third line is to negate the value.
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #10 on: March 14, 2008, 12:41:56 PM »
For the 1st line is shift right 2 times or divide by 4.
The second line is like y = y - 64.  This is just a C shortcut.
The third line is to negate the value.
hmmm.. tama ba yung sequence nya?  ???
instead na ganito:
Code: [Select]
y = x >> 2;
y -= 64;
y = -y;
dapat ganito:
Code: [Select]
y = x >> 2;
y = -y;
y -= 64;
???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #11 on: March 14, 2008, 12:54:18 PM »
Same result.
But the last line must be y+=64 on your code.
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #12 on: March 14, 2008, 01:19:07 PM »
Same result.
But the last line must be y+=64 on your code.
ok.. just like this:
Code: [Select]
y = x >> 2;
y = -y;
y += 64;
;D
pwede 2 examples pa? hehe.. plan ko kasi gawin syang parang auto range..
(1)what if ang range ng input ko is {0.. 127}, pero same pa rin range ng result {64.. 0}
bale ang equation nya is y = -0.5X + 64..
(2) what if ang range ng input ko is {0.. 511}, result is {64.. 0}
equation is y = -0.125x + 64
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #13 on: March 14, 2008, 01:48:39 PM »
ok.. just like this:
Code: [Select]
y = x >> 2;
y = -y;
y += 64;
;D
pwede 2 examples pa? hehe.. plan ko kasi gawin syang parang auto range..
(1)what if ang range ng input ko is {0.. 127}, pero same pa rin range ng result {64.. 0}
bale ang equation nya is y = -0.5X + 64..
(2) what if ang range ng input ko is {0.. 511}, result is {64.. 0}
equation is y = -0.125x + 64

#1
y = x >> 1;
y -= y;
y += 64;

#2
y = y >> 3;
y -= y;
y += 64;
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #14 on: March 14, 2008, 02:01:23 PM »
@ zer0w1ng
thanks again! kuha ko na yung patern..  :D

mga ilang percent kaya ang savings nito sa processing time compared dun sa magdi-divide ka directly?  ???
saka iniisip ko, 3 lines ito compared dun sa dati na single line lang..  ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #15 on: March 14, 2008, 03:55:48 PM »
mga ilang percent kaya ang savings nito sa processing time compared dun sa magdi-divide ka directly?  ???
saka iniisip ko, 3 lines ito compared dun sa dati na single line lang..  ???

A single line divide/multiply in C can generate more codes than the above example.
You could also make it into single line but is more readable to split into 3, such as:

y = 64 - (x>>2);

But the resulting ASM code is same and sometimes even lesser the 3 lines I posted because it won't use temporary variables.

To verify this, check the resulting ASM code in the ".lst" file.
You could also check how many bytes lesser the final code than the unmodified code.
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #16 on: March 14, 2008, 04:10:55 PM »
...But the resulting ASM code is same and sometimes even lesser the 3 lines I posted because it won't use temporary variables...
so, expected na mas mabilis sya kasi di na sya gagamit ng extra variables?  ???
by what percent kaya yung decrease sa processing time compared sa dati?  ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #17 on: March 14, 2008, 04:50:54 PM »
so, expected na mas mabilis sya kasi di na sya gagamit ng extra variables?  ???
by what percent kaya yung decrease sa processing time compared sa dati?  ???

Lesser code is much faster. So the 3 liner is faster if it generates lesser code. In pic 1 instruction, except for a change in PCL, is processed in 1 clock cycle or /4 of the Fosc.

I don't know the decrease of the processing time comparing the 3 liner and 1 liner. I cannot test here, I don't have CCS.
But as I said above, you could check the resulting ASM code.
The Cebuano Geek

Offline 'yus

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4251
  • Pogi/Ganda Points: 299
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: Code Compression (tips and tricks)
« Reply #18 on: March 14, 2008, 05:37:47 PM »
4. Sometimes built-in library has large code. If this happens create your own like using printf. printf uses large code in the lib. Printf has formatting include that you may not use at all.
5. Make your code into a function if used many times. Avoid cut-and-paste.
..
kapag nag-declare ba ako ng function tapos di ko naman nagamit,
magko-consume ba sya ng memory?  ???
same as sa variables, kapag nag-define ba ako ng variable tapos di ko naman nagamit,
magko-consume din ba sya ng space?  ???
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • Technical People
  • Gas Turbine
  • *****
  • Posts: 2179
  • Pogi/Ganda Points: 305
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: Code Compression (tips and tricks)
« Reply #19 on: March 14, 2008, 06:17:49 PM »
>kapag nag-declare ba ako ng function tapos di ko naman nagamit,
>magko-consume ba sya ng memory?  ???
On smart compilers, this should be automatically removed. AFAIR, It is called dead code removal. I don't have extensive experience on CCS but on Hitech and GCC removes this.

>same as sa variables, kapag nag-define ba ako ng variable tapos di ko naman nagamit,
>magko-consume din ba sya ng space?  ???
No. Global RAM variables are not removed. The compiler does not know if you used the variable using a pointer. So this is not removed to be safe.

But dynamic variables defined inside the functions are re-used other functions on same level by using a dynamic stack space employed by these new compilers if the micro has no hardware stack.
The Cebuano Geek

Philippine Electronics Forum

Re: Code Compression (tips and tricks)
« Reply #19 on: March 14, 2008, 06:17:49 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com