Difference between revisions of "Bitboards"

From Chessprogramming wiki
Jump to: navigation, search
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''[[Main Page|Home]] * [[Board Representation]] * Bitboards'''
 
'''[[Main Page|Home]] * [[Board Representation]] * Bitboards'''
  
[[File:BoardsMeeting.jpg|border|right|thumb|[[:Category:Samuel Bak|Samuel Bak]] - Boards Meeting <ref> [[:Category:Samuel Bak|Samuel Bak]]  - Boards Meeting, Oil on Canvas, 39 x 32". [http://chgs.elevator.umn.edu/asset/viewAsset/57f3b6787d58ae5f74bf8ba9#57f3b6d77d58ae5574bf8bcf Chess in the Art of Samuel Bak], [http://chgs.elevator.umn.edu/ Center for Holocaust & Genocide Studies], [https://en.wikipedia.org/wiki/University_of_Minnesota University of Minnesota]</ref>]]  
+
[[File:BoardsMeeting.jpg|border|right|thumb|[[:Category:Samuel Bak|Samuel Bak]] - Boards Meeting <ref> [[:Category:Samuel Bak|Samuel Bak]]  - Boards Meeting, Oil on Canvas, 39 x 32". [http://chgs.elevator.umn.edu/asset/viewAsset/57f3b6787d58ae5f74bf8ba9#57f3b6d77d58ae5574bf8bcf Chess in the Art of Samuel Bak], [http://chgs.elevator.umn.edu/ Center for Holocaust & Genocide Studies], [[University of Minnesota]]</ref>]]  
  
 
'''Bitboards''',<br/>
 
'''Bitboards''',<br/>
Line 61: Line 61:
 
* Bitboards can have a high information density.
 
* Bitboards can have a high information density.
 
* Single populated or even empty Bitboards have a low information density.
 
* Single populated or even empty Bitboards have a low information density.
* Bitboards are weak in answering questions like what piece if any resides on square x. One reason to keep a redundant [[Mailbox|mailbox]] board representation with some additional [[Incremental Updates|update]] costs during [[Make Move|make]]/[[Unmake Move|unmake]].
+
* <span id="getPiece"></span>Bitboards are weak in answering questions like what piece if any resides on square x. One reason to keep a redundant [[Mailbox|mailbox]] board representation with some additional [[Incremental Updates|update]] costs during [[Make Move|make]]/[[Unmake Move|unmake]].
 
* Bitboards can operate on all squares in parallel using bitwise instructions. This is one of the main arguments used by proponents of bitboards, because it allows for a flexibility in [[Evaluation|evaluation]].
 
* Bitboards can operate on all squares in parallel using bitwise instructions. This is one of the main arguments used by proponents of bitboards, because it allows for a flexibility in [[Evaluation|evaluation]].
 
* Bitboards are rather handicapped on 32 bit processors, as each bitwise computation must be split into two or more instructions <ref>[http://www.stmintz.com/ccc/index.php?id=30562 Efficient Bitboard Implementation on 32-bit Architecture] by [[Roberto Waldteufel]], [[CCC]], October 25, 1998</ref> . As most modern processors are now 64 bit, this point is somewhat diminished <ref>[http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=6651 Speedup by bitboards] by [[Onno Garms]], [[Computer Chess Forums|Winboard Forum]], July 13, 2007</ref> .
 
* Bitboards are rather handicapped on 32 bit processors, as each bitwise computation must be split into two or more instructions <ref>[http://www.stmintz.com/ccc/index.php?id=30562 Efficient Bitboard Implementation on 32-bit Architecture] by [[Roberto Waldteufel]], [[CCC]], October 25, 1998</ref> . As most modern processors are now 64 bit, this point is somewhat diminished <ref>[http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=6651 Speedup by bitboards] by [[Onno Garms]], [[Computer Chess Forums|Winboard Forum]], July 13, 2007</ref> .
Line 88: Line 88:
 
* [[Reijer Grimbergen]] ('''2007'''). ''Using Bitboards for Move Generation in Shogi''. [[ICGA Journal#30_1|ICGA Journal, Vol. 30, No. 1]], [http://www2.teu.ac.jp/gamelab/RESEARCH/ICGAJournal2007.pdf pdf] » [[Move Generation]], [[Shogi]]
 
* [[Reijer Grimbergen]] ('''2007'''). ''Using Bitboards for Move Generation in Shogi''. [[ICGA Journal#30_1|ICGA Journal, Vol. 30, No. 1]], [http://www2.teu.ac.jp/gamelab/RESEARCH/ICGAJournal2007.pdf pdf] » [[Move Generation]], [[Shogi]]
 
* [[James Glenn]], [http://www.cs.loyola.edu/~binkley/ David Binkley] ('''2008''') ''An Investigation of Hierarchical Bit Vectors''. [https://www.novapublishers.com/catalog/product_info.php?products_id=6555 New Topics in Theoretical Computer Science], [http://www.cs.loyola.edu/~binkley/papers/tcsrt08-hbit-vectors.pdf pdf]
 
* [[James Glenn]], [http://www.cs.loyola.edu/~binkley/ David Binkley] ('''2008''') ''An Investigation of Hierarchical Bit Vectors''. [https://www.novapublishers.com/catalog/product_info.php?products_id=6555 New Topics in Theoretical Computer Science], [http://www.cs.loyola.edu/~binkley/papers/tcsrt08-hbit-vectors.pdf pdf]
* [[Shi-Jim Yen]], [[Jung-Kuei Yang]] ('''2009'''). ''The Bitboard Design and Bitwise Computing in Connect Six''. [[Conferences#GPW|14th Game Programming Workshop]]
+
* [[Shi-Jim Yen]], [[Jung-Kuei Yang]] ('''2009'''). ''The Bitboard Design and Bitwise Computing in Connect Six''. [[Conferences#GPW|14th Game Programming Workshop]] » [[Connect6]]
 
* [[Fritz Reul]] ('''2009'''). ''New Architectures in Computer Chess''. Ph.D. Thesis, [https://pure.uvt.nl/ws/files/1098572/Proefschrift_Fritz_Reul_170609.pdf pdf]
 
* [[Fritz Reul]] ('''2009'''). ''New Architectures in Computer Chess''. Ph.D. Thesis, [https://pure.uvt.nl/ws/files/1098572/Proefschrift_Fritz_Reul_170609.pdf pdf]
 
==2010 ...==  
 
==2010 ...==  
Line 94: Line 94:
 
* [[Shi-Jim Yen]], [[Jung-Kuei Yang]], [[Kuo-Yuan Kao]], [[Tai-Ning Yang]] ('''2012'''). ''[http://www.sciencedirect.com/science/article/pii/S0950705112001293 Bitboard Knowledge Base System and Elegant Search Architectures for Connect6]''. [https://www.journals.elsevier.com/knowledge-based-systems/ Knowledge-Based Systems], Vol. 34 » [[Connect6]]
 
* [[Shi-Jim Yen]], [[Jung-Kuei Yang]], [[Kuo-Yuan Kao]], [[Tai-Ning Yang]] ('''2012'''). ''[http://www.sciencedirect.com/science/article/pii/S0950705112001293 Bitboard Knowledge Base System and Elegant Search Architectures for Connect6]''. [https://www.journals.elsevier.com/knowledge-based-systems/ Knowledge-Based Systems], Vol. 34 » [[Connect6]]
 
* [[Cameron Browne]], [[Stephen Tavener]] ('''2013'''). ''[http://www.aifactory.co.uk/newsletter/2012_02_fast_lane.htm Life in the Fast Lane]''. [[AI Factory]] »  [http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Conway's Game of Life] within a [[Bitboards|Bitboard]]
 
* [[Cameron Browne]], [[Stephen Tavener]] ('''2013'''). ''[http://www.aifactory.co.uk/newsletter/2012_02_fast_lane.htm Life in the Fast Lane]''. [[AI Factory]] »  [http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Conway's Game of Life] within a [[Bitboards|Bitboard]]
 +
* [[Jung-Kuei Yang]], [https://dblp.uni-trier.de/pers/hd/t/Tseng:Ping=Jung Ping-Jung Tseng] ('''2013'''). ''[https://ieeexplore.ieee.org/document/6783902 Bitboard Connection Code Design for Connect6]''. [[TAAI 2013]] » [[Connect6]]
 
* [[Cameron Browne]] ('''2014'''). ''Bitboard Methods for Games''. [[ICGA Journal#37_2|ICGA Journal, Vol. 37, No. 2]]
 
* [[Cameron Browne]] ('''2014'''). ''Bitboard Methods for Games''. [[ICGA Journal#37_2|ICGA Journal, Vol. 37, No. 2]]
* [[Yen-Chi Chen]], [[Shun-Shii Lin]] (''2019'''). ''A fast nonogram solver that won the TAAI 2017 and ICGA 2018 tournaments''. [[ICGA Journal#41_1|ICGA Journal, Vol. 41, No. 1]] » [[Nonogram]]
+
* [[Yen-Chi Chen]], [[Shun-Shii Lin]] ('''2019'''). ''A fast nonogram solver that won the TAAI 2017 and ICGA 2018 tournaments''. [[ICGA Journal#41_1|ICGA Journal, Vol. 41, No. 1]] » [[Nonogram]]
  
 
=Forum Posts=  
 
=Forum Posts=  
Line 117: Line 118:
 
==2005 ...==  
 
==2005 ...==  
 
* [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=4521 Bitboard question] by [[Tord Romstad]], [[Computer Chess Forums|Winboard Forum]], March 14, 2006
 
* [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=4521 Bitboard question] by [[Tord Romstad]], [[Computer Chess Forums|Winboard Forum]], March 14, 2006
 +
* [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=5623 Yet another new bitboard move generation method] by [[Zach Wegner]], [[Computer Chess Forums|Winboard Forum]], September 22, 2006 » [[Titboards]]
 +
: [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=5623&start=6 Re: Yet another new bitboard move generation method] by [[Harm Geert Muller]], [[Computer Chess Forums|Winboard Forum]], October 01, 2006 <ref>[http://www.talkchess.com/forum3/viewtopic.php?f=7&t=52861&start=8 Re: multi-dimensional piece/square tables] by Tony P., [[CCC]], January 28, 2020</ref>
 
* [http://www.talkchess.com/forum/viewtopic.php?t=13426 Speedup with bitboards on 64-bit CPUs] by [[Tord Romstad]], [[CCC]], April 27, 2007
 
* [http://www.talkchess.com/forum/viewtopic.php?t=13426 Speedup with bitboards on 64-bit CPUs] by [[Tord Romstad]], [[CCC]], April 27, 2007
 
* [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=6651 Speedup by bitboards] by [[Onno Garms]], [[Computer Chess Forums|Winboard Forum]], July 13, 2007
 
* [http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=6651 Speedup by bitboards] by [[Onno Garms]], [[Computer Chess Forums|Winboard Forum]], July 13, 2007
Line 132: Line 135:
 
* [http://www.talkchess.com/forum/viewtopic.php?t=64487 Checkers Bitboard representation] by Pranav Deshpande, [[CCC]], July 02, 2017 » [[Checkers]]
 
* [http://www.talkchess.com/forum/viewtopic.php?t=64487 Checkers Bitboard representation] by Pranav Deshpande, [[CCC]], July 02, 2017 » [[Checkers]]
 
* [http://www.talkchess.com/forum/viewtopic.php?t=65724 Bitboards and Java] by [[Fred Hamilton]], [[CCC]], November 14, 2017 » [[Java]]
 
* [http://www.talkchess.com/forum/viewtopic.php?t=65724 Bitboards and Java] by [[Fred Hamilton]], [[CCC]], November 14, 2017 » [[Java]]
 +
* [http://www.talkchess.com/forum3/viewtopic.php?f=7&t=72461&start=3 Re: Pawn move generation in bitboards] by [[Álvaro Begué]], [[CCC]], December 05, 2019 » [[Cpp|C++]], [[Pawn Pattern and Properties]]
  
 
=Viewer & Calculator=  
 
=Viewer & Calculator=  

Revision as of 09:56, 24 February 2020

Home * Board Representation * Bitboards

Samuel Bak - Boards Meeting [1]

Bitboards,
also called bitsets or bitmaps, are among other things used to represent the board inside a chess program in a piece centric manner. Bitboards, are in essence, finite sets of up to 64 elements - all the squares of a chessboard, one bit per square. Other board games with greater board sizes may be use set-wise representations as well [2], but classical chess has the advantage that one 64-bit word or register covers the whole board. Even more bitboard friendly is Checkers with 32-bit bitboards and less piece-types than chess [3] [4].

The Board of Sets

To represent the board we typically need one bitboard for each piece-type and color - likely encapsulated inside a class or structure, or as an array of bitboards as part of a position object. A one-bit inside a bitboard implies the existence of a piece of this piece-type on a certain square - one to one associated by the bit-position.

Bitboard Basics

Of course bitboards are not only about the existence of pieces - it is a general purpose, set-wise data-structure fitting in one 64-bit register. For example, a bitboard can represent things like attack- and defend sets, move-target sets and so on.

General Bitboard Techniques

The fundamental bitboard basics.

Pattern and Attacks

This is basically about chess, how to calculate attack-sets and various pattern for evaluation and move generation purposes.

Move Generation Issues

Bitboard aspects on move generation and static exchange evaluation (SEE).

Miscellaneous

Defining Bitboards

To be aware of their scalar 64-bit origin, we use so far a type defined unsigned integer U64 in our C or C++ source snippets, the scalar 64-bit long in Java. Feel free to define a distinct type or wrap U64 into classes for better abstraction and type-safety during compile time. The macro C64 will append a suffix to 64-bit constants as required by some compilers:

typedef unsigned __int64 U64;    // for the old microsoft compilers 
typedef unsigned long long  U64; // supported by MSC 13.00+ and C99 
#define C64(constantU64) constantU64##ULL

Bitboard-History

The general approach of bitsets was proposed by Mikhail R. Shura-Bura in 1952 [5] [6]. The bitboard method for holding a board game appears to have been invented also in 1952 by Christopher Strachey using White, Black and King bitboards in his checkers program for the Ferranti Mark 1, and in the mid 1950's by Arthur Samuel in his checkers program as well. In computer chess, bitboards were first described by Georgy Adelson-Velsky et al. in 1967 [7], reprinted 1970 [8] . Bitboards were used in Kaissa and in Chess. The invention and publication of Rotated Bitboards by Robert Hyatt [9] and Peter Gillgasch with Ernst A. Heinz in the 90s was another milestone in the history of bitboards. Steffan Westcott's innovations, too expensive on 32-bit x86 processors, should be revisited with x86-64 and SIMD instructions in mind. With the advent of fast 64-bit multiplication along with faster memory, Magic Bitboards as proposed by Lasse Hansen [10] and refined by Pradu Kannan [11] have surpassed Rotated.

Analysis

The use of bitboards has spawned numerous discussions about their costs and benefits. The major points to consider are:

  • Bitboards can have a high information density.
  • Single populated or even empty Bitboards have a low information density.
  • Bitboards are weak in answering questions like what piece if any resides on square x. One reason to keep a redundant mailbox board representation with some additional update costs during make/unmake.
  • Bitboards can operate on all squares in parallel using bitwise instructions. This is one of the main arguments used by proponents of bitboards, because it allows for a flexibility in evaluation.
  • Bitboards are rather handicapped on 32 bit processors, as each bitwise computation must be split into two or more instructions [12] . As most modern processors are now 64 bit, this point is somewhat diminished [13] .
  • Bitboards often rely on bit-twiddling and various optimization tricks and special instructions for certain hardware architectures, such as bitscan and population count. Optimal code requires machine dependent header-files in C/C++. Portable code is likely not optimal for all processors.
  • Some operations on bitboards are less general, f.i. shifts. This requires additional code overhead.

Publications

1970 ...

1980 ...

1990 ...

2000 ...

2010 ...

Forum Posts

1994

1995 ...

2000 ...

2005 ...

Re: Yet another new bitboard move generation method by Harm Geert Muller, Winboard Forum, October 01, 2006 [16]

2010 ...

2015 ...

Viewer & Calculator

External Links

Kerstin Fabry, Christian Ribbe, Elmar Dissinger, Martin Siehoff, Carlotta Ribbe, Ludger Bollinger

References

  1. Samuel Bak - Boards Meeting, Oil on Canvas, 39 x 32". Chess in the Art of Samuel Bak, Center for Holocaust & Genocide Studies, University of Minnesota
  2. Reijer Grimbergen (2007). Using Bitboards for Move Generation in Shogi. ICGA Journal, Vol. 30, No. 1, pdf
  3. Checker Bitboards Tutorial by Jonathan Kreuzer
  4. Checkers Bitboard representation by Pranav Deshpande, CCC, July 02, 2017
  5. Lazar A. Lyusternik, Aleksandr A. Abramov, Victor I. Shestakov, Mikhail R. Shura-Bura (1952). Programming for High-Speed Electronic Computers. (Программирование для электронных счетных машин)
  6. Andrey Ershov, Mikhail R. Shura-Bura (1980). The Early Development of Programming in the USSR. in Nicholas C. Metropolis (ed.) A History of Computing in the Twentieth Century. Academic Press, preprint pp. 43
  7. Early Reference on Bit-Boards by Tony Warnock, rec.games.chess, October 29, 1994
  8. Georgy Adelson-Velsky, Vladimir Arlazarov, Alexander Bitman, Alexander Zhivotovsky, Anatoly Uskov (1970). Programming a Computer to Play Chess. Russian Mathematical Surveys, Vol. 25, pp. 221-262.
  9. Robert Hyatt (1999). Rotated Bitmaps, a New Twist on an Old Idea. ICCA Journal, Vol. 22, No. 4
  10. Fast(er) bitboard move generator by Lasse Hansen, Winboard Forum, June 14, 2006
  11. List of magics for bitboard move generation by Pradu Kannan, Winboard Forum, August 23, 2006
  12. Efficient Bitboard Implementation on 32-bit Architecture by Roberto Waldteufel, CCC, October 25, 1998
  13. Speedup by bitboards by Onno Garms, Winboard Forum, July 13, 2007
  14. Bitboard user's information request by Robert Hyatt, CCC, October 05, 1999
  15. Robert Hyatt (1999). Rotated Bitmaps, a New Twist on an Old Idea. ICCA Journal, Vol. 22, No. 4
  16. Re: multi-dimensional piece/square tables by Tony P., CCC, January 28, 2020
  17. M42 by Syed Fahad
  18. Flottmann-Hallen in Herne, North Rhine-Westphalia, Germany, part of The Industrial Heritage Trail of the Ruhr area

Up one level