Author: Dieter Buerssner
Date: 12:35:21 11/30/00
Go up one level in this thread
On November 29, 2000 at 13:21:35, Eugene Nalimov wrote:
>Fortunately, there are code sequences that is almost as fast as original shift,
>but produce "correct" result and much faster than naive division. For example,
>you can replace
> a / b, b is power of 2
>by
> (a + ((a >> (sizeof(int)*8-1)) & (b-1))) >> log2(b)
Thanks for posting this.
I think it is obvious, but just to ensure myself, a is a signed int and
b is an unsigned int or constant value?
for
int a, b;
b = a/256;
the compiler can produce code as it was written as
if (a < 0)
a += 255;
b = a >> 8; /* Assume arithmetic right shift */
which obviously needs a branch. Will your code snippet generally produce
faster code? Even when the branch is almost allways taken or almost allways not
taken?
>But my original point is still valid -- compiler have to emit different code for
>division by power of 2 and right shift.
I hope, my post didn't indicate, that I thought different.
Regards,
Dieter
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.