Couple points:
> bitshifting instead of multiplication can''t hurt,
> especially considering not everyone use VC++ or
> Borland, etc.
and
> although whether VC will do this in the cheaper
> versions of the compiler, I don''t know.
To paraphrase what someone else on this board said recently, even the assworthy compiler that my grandmother hacked together in Swahili will do this optimization. This is the absolute most trivial, brain dead optimization. Do you people actually realize how GOOD compilers are nowadays? Obviously not. I was talking to some of the authors of mipsPro, and they were describing loop reordering optimizations that I wouldn''t be able to do by hand, much less program a compiler to do for me. Bitshifting for mltiplication is an archaic, stupid practice. Tell the compiler what you WANT to do, and let it figure out how. After all, what happens when you''re compiling on a processor where multiplication is faster than bitshifting? (Don''t say it''s impossible, with a superscalar, heavily pipelined machine, it is very likely that a multiplication unit will be more available than a unit capable of shifting.) This discussion has come up far too often, and gone on too long. Please stop misleading everyone... BITSHIFTING IS NOT NECESSARY AS A USER LEVEL OPTIMIZATION.
-Brian
<<>> or /* ??
quote:
Tell the compiler what you WANT to do, and let it figure out how.
Although you are right about the bitshifting, this statement is ass-backwards. The best optimizer is between your ears, not on a computer chip.
well, VC at least replaces all integer multiplications with series of lea [eax*n+eax],bit shifts and add/subs, which are almost always more effective than imul~11 cycles(at least on pentium) because of pairing. You have to be darn good with asm these days to beat the compiler. Its more effective to spend your time trying to do some research and improve your alghorithms than optimizing the code instruction by instruction.
But its a good idea to use VTune or some similar prog to speed up critical parts of your app when its finished and compiled. Its better not to include "instruction level" optimizations in your source code, which in some cases can even make it slower.
-kertropp
C:\Projects\rg_clue\ph_opt.c(185) : error C3142: 'PushAll' :bad idea
C:\Projects\rg_clue\ph_opt.c(207) : error C324: 'TryCnt': missing point
But its a good idea to use VTune or some similar prog to speed up critical parts of your app when its finished and compiled. Its better not to include "instruction level" optimizations in your source code, which in some cases can even make it slower.
-kertropp
C:\Projects\rg_clue\ph_opt.c(185) : error C3142: 'PushAll' :bad idea
C:\Projects\rg_clue\ph_opt.c(207) : error C324: 'TryCnt': missing point
-kertropp C:Projectsrg_clueph_opt.c(185) : error C3142: 'PushAll' :bad ideaC:Projectsrg_clueph_opt.c(207) : error C324: 'TryCnt': missing point
Thanks, Kertropp, that''s what I wanted to know.
A polar bear is a rectangular bear after a coordinate transform.
A polar bear is a rectangular bear after a coordinate transform.
A polar bear is a rectangular bear after a coordinate transform.
i like to make optimizations in my code even though I know the compiler does it anyway. Its a pshycotic thing. Its like I know the compiler will optimize it, but I don''t think it will .
As I had been posting several times, bitshifting is not multiplication and division. Well, multiplication is identical with left shifting, ok ...
but right shifting is right shifting.
If you are right shifting unsigned type, the C/C++ is doing
SHR shifting type, this is same as division by powers of 2.
But if you have signed type to right shift, the C/C++ does
SAR type shifting: i.e. the new uppermost bit is copied from signum bit (so it remains the same, because it''s the same bit ).
So -4 >> 1 = -2 ( -4 / 2 )
-4 >> 2 = -1 ( -4 / 4 )
BUT -4 >> 3 = -1 and -4 / 8 = 0 ...
Be exact dudes... Because the machine IS.
---------------------------------------------------
Ped - Peter Helcmanovsky - 7 Gods demo group
http://7gods.rulez.sk
First Sight Entertainment - http://members.xoom.com/fseteam
---------------------------------------------------
but right shifting is right shifting.
If you are right shifting unsigned type, the C/C++ is doing
SHR shifting type, this is same as division by powers of 2.
But if you have signed type to right shift, the C/C++ does
SAR type shifting: i.e. the new uppermost bit is copied from signum bit (so it remains the same, because it''s the same bit ).
So -4 >> 1 = -2 ( -4 / 2 )
-4 >> 2 = -1 ( -4 / 4 )
BUT -4 >> 3 = -1 and -4 / 8 = 0 ...
Be exact dudes... Because the machine IS.
---------------------------------------------------
Ped - Peter Helcmanovsky - 7 Gods demo group
http://7gods.rulez.sk
First Sight Entertainment - http://members.xoom.com/fseteam
---------------------------------------------------
---------------------------------------------------Ped - Peter Helcmanovsky - 7 Gods demo grouphttp://7gods.rulez.skFirst Sight Entertainment - http://members.xoom.com/fseteam---------------------------------------------------
Caution!
^ is a bitwise-xor operator in c/c++ and should not be used in examples like this:
"For example, 3 << 4 would be like: 3 * (2^4) "
It will compile - but the meaning is totally different.
Use instead:
"For example, 3 << 4 would be like: 3 * pow(2,4) "
Edited by - Osmo Suvisaari on 4/6/00 6:38:56 AM
^ is a bitwise-xor operator in c/c++ and should not be used in examples like this:
"For example, 3 << 4 would be like: 3 * (2^4) "
It will compile - but the meaning is totally different.
Use instead:
"For example, 3 << 4 would be like: 3 * pow(2,4) "
Edited by - Osmo Suvisaari on 4/6/00 6:38:56 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement