Archived

This topic is now archived and is closed to further replies.

shpook

<<>> or /* ??

Recommended Posts

I may be asking this on the wrong board... if I am, sorry. I am using VC++ 6.0 as a dev environment and was told quite recently that using bit-shifting in place of multiplication and division was "obsolete" in a manner of speaking, because VC++ 6.0 ships with a built in incredibly efficient optimizer that renders such "tricks" useless. So I don''t have to do any research of my own , I was wondering if anyone could verify this. Anyone? Anyone? =) Thanks, -shpook

Share this post


Link to post
Share on other sites
Most modern compilers (including VC++) will optimize multiplication and division by powers of two as simple bit shifts. For that reason they are pretty much obsolete in terms of optimization, but there are still other things they are useful for. For example, if you are shifting bits of a color value to the right position to compose an RGB value, then it is much clearer to use the shift operators than to use mulitplication (IMHO) so they are not completely obsolete.

Check out the GPI project today!

Share this post


Link to post
Share on other sites
An optimization that I''ve seen more than once is the conversion from i * 640 to i * 512 + i * 128, which can be bitshifted nicely. Would the compiler optimize this as well (from 640) or should you still write it in the second form for the compiler to recognize it as a candidate for optimization?

A polar bear is a rectangular bear after a coordinate transform.

Share this post


Link to post
Share on other sites
For JonatanHedborg: if you plan one multiplying or dividing by a number that is a multiple of 2 (2, 4, 8, 16, 32, etc.), you can use bitshift to optimize the operation. for multiplication, use <<, and use >> for division.
For example, 3 << 4 would be like:
3 * (2^4)
hope this helps!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, compilers optimizing code is very frequent in this day and age. VC++ probably does convert them to bitshifts anyway, but bitshifting instead of multiplication can''t hurt, especially considering not everyone use VC++ or Borland, etc. If you feel pretty confident that you can read your code with bitshifting and all (or if you don''t you can comment on what you did) then by all means, bitshift.

Ok now for the question on What is bitshifting.

Bitshifting (in C) is the << operator. It shifts one bit or on/off value one place over. Say you had binary 0010 or 2 in decimal, bitshift would move it 0100 or make it four. Thats only a left shift, A right shift would do the opposite, making it one.

variable << how_many_times_to_shift; /* In C */
(variable << how_many_times_to_shift); // In C++

Get it?

Share this post


Link to post
Share on other sites
I agree with Anon. and I usually use bitshift.

I''m intrested in what Amitage was asking (if anyone knows...), but if you wanted to use that optimisation and you didn''t know if the compiler would do it, you could use the bitshifts yourself of course.

George.

"Who says computer games affect kids, imagine if PacMan affected us as kids, we'd all sit around in a darkened room munching pills and listening to repetitive music....uh oh!"

Share this post


Link to post
Share on other sites
Well, why not try it - run your code in debug mode and view the disassembly. Go to the relevant line and look for something that looks like a shift rather than a multiply. I expect that it will do a fairly good job of optimising many multiplications by constants to shifts. Although whether VC will do this in the cheaper versions of the compiler, I don''t know.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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
---------------------------------------------------

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites