Optimizing Code With C

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Optimizing Code With C

Post by Wyrmkill on Tue Apr 15, 2008 11:47 pm
([msg=382]see Optimizing Code With C[/msg])

Code: Select all
/*
    Optimize code by shifting
*/

unsigned __int16
    __cdecl printf(const char*, ...),
    __cdecl getch(void);

void main(void) {

Instead of dividing 'a' with 16 (2^4) we shift
its bits four positions to the right.

By using this we can skip the consuming;
mov ecx, 16 <-- 1c
mov eax,dword ptr [a] <-- 1c
cdq <-- 3c
idiv ecx <-- 46c
mov dword ptr [a], eax <-- 1c

And replace it with;
sar dword ptr [a], 4 <-- 3c

Dividing 'a' with 16 will need 52c (cycles),
and shifting bits will only need 3c (cycles)!

That is an impressive optimization

The following code will show you how the
optimizing is done using C code:
Code: Select all
    unsigned __int16 a = 0x64;  // a = 100
    a >>= 4;                    // a = a/16
    printf("%d\x0A",a);         // send result to stout
    getch();                    // wait for kbhit

}
Wyrmkill
New User
New User
 
Posts: 18
Joined: Tue Apr 15, 2008 11:26 pm
Blog: View Blog (0)


Re: Optimizing Code With C

Post by evinyatar on Wed Apr 16, 2008 10:35 am
([msg=425]see Re: Optimizing Code With C[/msg])

As far as i know compilers replace all multiplication and division by powers of two with shifts, so there is no reason to do it manually.
Have you the BRAINWORMS?!
User avatar
evinyatar
New User
New User
 
Posts: 5
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Optimizing Code With C

Post by BhaaL on Wed Apr 16, 2008 11:52 am
([msg=439]see Re: Optimizing Code With C[/msg])

evinyatar wrote:As far as i know compilers replace all multiplication and division by powers of two with shifts, so there is no reason to do it manually.

Not all compilers do that, especially not on their own. GCC afaik only does that on a certain optimization level; the Visual Studio C++ compiler fails completely (at least last time looked, it used a plain idiv).

Still, neat thing.
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Optimizing Code With C

Post by Sonic7145 on Wed Apr 16, 2008 3:19 pm
([msg=464]see Re: Optimizing Code With C[/msg])

Pretty cool, nice work.
Sonic7145
Poster
Poster
 
Posts: 122
Joined: Sun Apr 13, 2008 12:01 pm
Blog: View Blog (0)


Re: Optimizing Code With C

Post by -Pein-God- on Wed Apr 23, 2008 8:48 am
([msg=1055]see Re: Optimizing Code With C[/msg])

yea...this is cool ;) ;)
-Pein-God-
New User
New User
 
Posts: 8
Joined: Wed Apr 23, 2008 8:15 am
Blog: View Blog (0)


Re: Optimizing Code With C

Post by djpitagora on Thu Jun 05, 2008 4:24 pm
([msg=4065]see Re: Optimizing Code With C[/msg])

BhaaL wrote:
evinyatar wrote:As far as i know compilers replace all multiplication and division by powers of two with shifts, so there is no reason to do it manually.

Not all compilers do that, especially not on their own. GCC afaik only does that on a certain optimization level; the Visual Studio C++ compiler fails completely (at least last time looked, it used a plain idiv).

Still, neat thing.


I don't know what visual studio compiler you are using or what options but normaly it does optimize this. Try changing the goal from size to speed. I've seen some pretty incredible optimizations done by the compiler and not only for multiplications or divisions by a power of two: multiplication by 3 can be done with one shift and an add for instance...and so on...
djpitagora
New User
New User
 
Posts: 24
Joined: Sun May 25, 2008 5:49 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests