Bitboard serialization

Discussion about development of draughts in the time of computer and Internet.
Post Reply
Rein Halbersma
Posts: 1722
Joined: Wed Apr 14, 2004 16:04
Contact:

Bitboard serialization

Post by Rein Halbersma » Tue Mar 05, 2013 23:38

Just to copy you guys in from a discussion on Talkchess. I posted some C++11 code there to iterate (in STL style) over all 1-bits of a 64-bit bitboard. The code is at http://liveworkspace.org/code/41EaZl$165 I will explain for those interested, but it allows you to do move generation with simple code like this:

Code: Select all

typedef bit_set<int64_t, 1> bitset;
int movelist[64]; // predefined storage
int main()
{ 
  int* p = movelist;
  bitset x {17, 31, 61}; // just an example, use any bit-pattern as a result from your move generator
  std::copy(x.begin(), x.end(), p); // equivalent to hand-written loop!
  return 0;
}
The spoil the magic: std::copy calls operator++ on an iterator, and copies that to the movelist pointed to by p. The increment does the usual (mask &= mask - 1), and the iterator operator* dereference calls gcc's compiler intrinsic __builtin_ctzll() (trailing zero count). All in all the generated assembly is identical to the usual handwritten loops and bit-twiddling magic you see on e.g. the Chess Programming Wiki.

Not only can you serialize bitboards using std::copy, you can now also call any other non-mutating STL algorithm that works on sorted ranges. It simply adds a container interface to plain old bitboards that we know and love :-)

The code is still experimental and I need to polish it a bit more to make it an almost drop-in replacement for plain 64-bit integers bitboards.

Rein Halbersma
Posts: 1722
Joined: Wed Apr 14, 2004 16:04
Contact:

Re: Bitboard serialization

Post by Rein Halbersma » Fri Mar 08, 2013 20:10

UPDATE: now also a lemma at the Chess Programming Wiki :-)

Post Reply