Archived

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

Lews

64-bit variables in win32

Recommended Posts

Hello, I''m thinking of writing a chess engine and one of the fastest ways to perform move generation involves the manipulation of "bit boards", which are 64 bit integers. Chess programs perform bitwise operations on these ints to get superlative performance in searching for checks and things like that. My question is, what level of support does Win32 provide for 64-bit ints? Is there any? I looked through MSDN and there''s a var type called "__int64". Is this a true 64-bit int? Or is some trickery used to emulate 64-bit math on a 32-bit processor? Are ANDs, ORs, XORs, and bit shifts just as fast on a 64-bit int in Win32 as they would be on a true 64-bit machine? Implementing bit boards seems to be a lot of work, so I want to make sure that I''ll actually see some performance boost in my program before I set about implementing them.

Share this post


Link to post
Share on other sites
Yes, there is some emulation 'trickery' to get the 64-bit integers to work on 32-bit machines, but it's not all that bad. When doing addition, you basically have two ADD instructions instead of one. I would guess division would be quite a bit slower, but shifts, ands, ors, xors, etc. should only take twice as long as their 32-bit counterparts.

You could write your own int64 class if you were not happy with the built-in type's performance, but I seem to think you'll be best off using __int64 (unless you plan on porting your program).

You might even be able to use MMX to accelerate your logic, but I am honestly not educated in its use to know for sure.

I think multiplication and division may be the only integer operations that take more than twice as long to execute, but I could be wrong (I'll have to think about it).

And I'm almost certain that 64-bit operations will be faster on a 64-bit machine. Unfortunately I don't have my Opteron yet in order to test my theory. :-) The fact that they can be performed in a single instruction definately makes me assume this will be true.

EDIT--shifts may take slightly longer than twice as long, as you will have to shift two 32-bit numbers and then adjust for the carry if needed. Same goes for addition, subtraction, etc. And,or,xor,not, however, do not require a carry, and will simply be two 32-bit instructions.

--TheMuuj



[edited by - themuuj on July 16, 2002 4:17:57 PM]

Share this post


Link to post
Share on other sites
I'm not 100% sure what you mean. 8x8 chess fields
are 64, that's the only thing what comes to my mind right
now, what you could mean. But with one bit you obviously can't
store what kind of figure stands on a field...hhmm....
it can't be meant like this...

However, storing several bit sized things in an int
instead one int for each, will be smaller, but not faster.

hhmm... thought about it, but still don't really know
what you mean.


Well, a 64 bit int is a 64 bit int, 64 bits (8 bytes) are used
for storage But AFAIK the pentium machines have no
64 bit registers, so operations will be done with two registers.


Edit:

And, please, could you tell what you mean with bit operations
for chess engines? I'v never heard about it, and really
still have no clue what it could be...

It is now for sure you can throw away your computer

[edited by - UnshavenBastard on July 16, 2002 4:24:18 PM]

Share this post


Link to post
Share on other sites
The fastest operation a C or C++ compiler can accomplish is its assembly analogous. For example: "a = b;" will be compiled into "mov a, b". I think there''s no 64 bits math operations in Intel processor''s instructions set. The compiler would have to add extra code to tweak your desired 64 bit operations. My advice is: If you can do a faster 64 bits functions faster than the C compiler then do it. To start try to use the embedded support of the C compiler for 64 bits values. Finish your game. If you need more speed then create your own 64 bit logic.

Best of luck in your endeavours.

Share this post


Link to post
Share on other sites
There are some 64bit MMX registers. You could look into that, but I would just abstract the low-level of this for now and write the simplest thing that works __int64 or QWORDs. If that method isn''t fast enough for your move calculations then you can go rewrite it using MMX or lord-knows what.

Share this post


Link to post
Share on other sites
Which one is faster?

    
DWORD dwRGB, dwFlag;
dwRGB = RGB(r, g, b);
dwFlag = GOOD_STATE | STATE1 | STATE2 | STATE3;
if (dwFlag & GOOD_STATE)
{
if ( (dwRGB >> 24 & 0xFF) == r1 &&
(dwRGB >> 16 & 0xFF) == g1 &&
(dwRGB & 0xFF) == b1 )
{
// do something

}

// ...

}

or

      
BYTE byR, byG, byB;
bool bGood, bState1, bState2, bState3;
byR = r;
byG = g;
byB = b;
bGood = bState1 = bState2 = bState3 = true;
if (bGood)
{
if (byR == r1 && byG == g1 && byB == b1)
{
// do something...

}
// ...

}

and loop that million times... which one is faster?


[edited by - DerekSaw on July 16, 2002 10:24:54 PM]

Share this post


Link to post
Share on other sites
In response to the UnshavenBastard''s question, more info about bitboards can be found at:

http://www.computerschach.de/freeware/bitboards.htm

and

http://www.xs4all.nl/~verhelst/chess/representations.html


The basic idea is to represent a boolean state of one condition for each tile of the board such as whether there is a piece on a given tile or not or whether a given tile is attacked by a certain piece. It''s very clever. By ORing an attack board with a board with the king''s location, you could tell, for instance, if the king is being checked: in one clock cycle. (ok, maybe more than one with the if logic, but it''s really fast)

Share this post


Link to post
Share on other sites