🔒 Locked
Bitshifting question
Advertisement
Original Post
Hi all,
Just a quick question. If I want to divide a numer by say 4 I could do it the conventional way ( iNumber = iNumber / 4 ) or I could shift every bit of iNumber right two positions (iNumber = iNumber >> 2 . Are there any performance differences or is it just the same speedwise? Thanks.
Stoffel,
With that in mind I found this in gamedev resources:
http://www.gamedev.net/reference/programming/features/performance/page5.asp
quote:
Joris Timmermans
Logically, they are the same. For this simple example, the compiler might even turn the first into the second, but as you get more complex ( i = i << 8 + i << 4 is equivalent to i *= 272 for example )the compiler might not be able to make the conversion.
So is this in error, or is there an advantage using bit shift operators in complex equations.
Guy
As the quote says, the compiler can not ascertain complex deconstructions (how would the compiler "know" that x * 320 is the same as x << 8 + x << 6?) In such situations, the responsibility lies with the programmer.
On a related note, I once tried to find out whether the powers of 2 representation of any number could rapidly be "divined"; it ended up taking longer to figure out the summation of powers than it took to perform the original operations I was trying to optimize. (I was trying to see if I could convert arbitrary multiplications - a * b where both a and b are integer variables - into a << z + a << y + a << x + ... form)
On a related note, I once tried to find out whether the powers of 2 representation of any number could rapidly be "divined"; it ended up taking longer to figure out the summation of powers than it took to perform the original operations I was trying to optimize. (I was trying to see if I could convert arbitrary multiplications - a * b where both a and b are integer variables - into a << z + a << y + a << x + ... form)
Yeah, sure an interesting idea.
I
ve tried this, too, some time ago,
but ended up like you said,
it gets too complex, and wont be an optimization.(I never finished that, gave up once I saw how it will end..)
But maybe it nearly doesn
t matter these days,
if you multiply of shift. How much cycles does
an imul take on a P4, 1..2 cycles?
Dunno, but someday it sure wont matter anymore...
GuyJohnston,
AFAIK, that post is on-target: compilers generally won''t break down a multiply into two shifts and an add. (This is, BTW, why I qualified my response with "for integer multiples of two".) Whether two shifts and an add is faster than an integer multiply, I''m not sure. I didn''t want to bring it up in my first response because these kinds of nitty-gritty optimizations shouldn''t take place unless the coder is sure that the code in question is creating a bottleneck.
AFAIK, that post is on-target: compilers generally won''t break down a multiply into two shifts and an add. (This is, BTW, why I qualified my response with "for integer multiples of two".) Whether two shifts and an add is faster than an integer multiply, I''m not sure. I didn''t want to bring it up in my first response because these kinds of nitty-gritty optimizations shouldn''t take place unless the coder is sure that the code in question is creating a bottleneck.
what is a bitshift? thankx
Edem Attiogbe
Edem Attiogbe
I see. still taking it in. thankx doogle
Edem Attiogbe
Edem Attiogbe
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement