Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about an efficient way to change the bits of a number.

Author: Gerd Isenberg

Date: 03:30:04 07/28/03

Go up one level in this thread


On July 27, 2003 at 07:22:20, Uri Blass wrote:

>On July 27, 2003 at 07:04:50, Ricardo Gibert wrote:
>
>>On July 27, 2003 at 06:51:51, Uri Blass wrote:
>>
>>>In movei there are cases when I want to change 2 bits of a number to be
>>>identical to the bits of another number.
>>>
>>>Let say I want b=a in the 2^0 bit and the 2^8 bit
>>>
>>>The simple way is
>>>
>>>if (a&256)
>>>b|=256;
>>>else
>>>b&=~256;
>>>if (a&1)
>>>b|=1;
>>>else
>>>b&=~1;
>>>
>>>a faster way that I could think about is:
>>>
>>>b|=(a&257);
>>>b&=~(257-(a&257));
>>>
>>>Is it the best way or maybe it is better to change the last line and have
>>>b|=(a&257);
>>>b&=~(257&(~(a&257)))
>>>
>>>Uri
>>
>>To compute b = a for mask m: b = (b&~m)|(a&m)   [untested]
>>
>>That's 4 operations. 3 if m is a constant when the "~" will optimized away.
>
>Thanks.
>It seems to work
>
>I usually think in terms of b|= or b&= because a and b are long expressions and
>I do not like to write them twice(a and b are varaible but they hide inside
>unions and arrays and I use long names for my arrays.
>
>The relevant line in my case is:
>infoqueensdir[target].u=(infoqueensdir[target].u&~257)|(infoqueensdir[sq].u&257);
>
>There are also other similiar lines.
>
>Can I write it without writing infoqueensdir[target].u twice
>or maybe it is better if I write it in 2 lines.
>
>Uri

May be a matter of taste...
I prefere small inlines for such things - like typesafe macros:

__forceinline
void assignWithMask(int &b, int a, int m)
{
#ifdef RATKO
   b^=(a^b)&m;
#else
   b = (b&~m) | (a&m);
#endif
}

...

assignWithMask(infoqueensdir[target].u, infoqueensdir[sq].u, 257);

or even more specialized:

__forceinline
void assignInfoQueenDir(int target, int source, int m)
{
  assignWithMask(infoqueensdir[target].u, infoqueensdir[source].u, m);
}

....

assignInfoQueenDir(target, sq, 257);

The compiler should be able to optimize the inline incarnations depending on "m"
is constant or not.

Gerd




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.