Hyperbola Quintessence
Home * Board Representation * Bitboards * Sliding Piece Attacks * Hyperbola Quintessence
Samuel Bak - Reflexion, 1990 [1] Hyperbola Quintessence applies the o^(o-2r)-trick also for vertical or diagonal negative Rays - by reversing the bit-order of up to one bit per rank or byte with a vertical flip aka x86-64 bswap [2] . It is somehow a resurrection of the reverse bitboards idea of Ryan Mack’s Hyperbola Project on the fly, and was created by Gerd Isenberg. Improvements by Aleks Peshkov [3] made it applicable and competitive.
Cpwmappinghint.JPG | Code samples and bitboard diagrams rely on Little endian file and rank mapping. |
Reverse Math
Assume following masked occupancy on a file, diagonal or anti-diagonal - for simplicity as a flat byte (in a real bitboard with masked files or diagonals you have 6..8 scratch-bits between the bits of this byte). Thus, vertical flip reverses the bits of this byte.
The first subtraction of (o-2r) is done implicitly by masking off the line, removing the slider from the occupied set. The second subtraction borrows a “one” from the next nearest blocker in msb-direction, falling through all unset bits outside the line. Of course, if no blocker is available, it borrows a “one” in usual arithmetical manner from the hidden 2^N. Only the changed bits (from original o, o’) are the appropriate sliding attacks, including the blocker but excluding the slider. The result finally needs to be intersected with the same line mask as previously the occupancy, to clear the wrapped borrow one bits outside the file or diagonal. The fine optimization by Aleks Peshkov covers the final union of positive and negative ray-attacks. Since opposed ray-directions are always disjoint sets, using xor instead of bitwise or safes two instructions per line-attack. That is because bit-reversal or any mirroring or flipping is own inverse and distributive over xor.
thus
and finally
Beside shorter code this reduces register pressure - and clearly outperforms kindergarten bitboards - ipc-wise, in code size and memory requirements.
Source Code
C
The three C-routines only differ by the line-mask applied:
For better locality of the line-attacks on the otherwise empty board, we may use an properly aligned array of structs.
Using x86-64 bswap makes it quite competitive for bishops and files, on AMD K8 or K10 it has a latency of one cycle with a throughput of 1/3, like other cheap instructions. However, Intel is tad slower - while the recent Core 2 duo processors perform 128-bit SIMD-instructions with 128-bit alus, that is bitwise logical instructions with a latency of one cycle and throughput of 1/3, the general purpose bswap-instruction takes four cycles with a throughput of one. In Intel 64 and IA32 Architectures Optimization Reference Manual [4] , it is therefor recommend (5.6.5. endian conversion) to use the SSSE3 pshufb instruction to swap bytes, available through intrinsic [5] , see SSSE3 Hyperbola Quintessence for bishop attacks.
As long there is no fast bit reversal instruction, there is no general solution for all four lines, and the rook attack-getter still needs some standard technique for the rank-attacks. Tim Cooijmans proposed to map the rank to the main diagonal before applying HQ, and to re-map the calculated attacks back to the original rank [6] .
Generalized Set-wise Attacks
Hyperbola quintessence can be generalized to work on whole sets of sliding pieces instead on individual pieces, whose ranks to be masked. The problem arising, when not masking the rank of the piece is that attacks wrap around the board during subtraction. This is shown below:
This is not the intended result. It can be avioded, by bitwise adding an overflow barrier on the right-hand side. Afterwards this barrier needs to be removed from the attack set:
the complete algorithm for the left direction is therefore:
For the right-hand direction, the bits need to be reversed rank-wise.
x86-64 assembly
The VC2005 generated x86-64 assembly of bishopAttacks indicates what ipc-monster Hyperbola Quintessence is:
Java
Java programmer may try Long.reverseBytes:
Long.reverse for a generalized attack getter even for ranks is too expensive, except a JVM can use a machine instruction rather than a bit-reversal routine:
See also
- Reverse Bitboards
- Obstruction Difference
- SBAMG
- SSSE3 Hyperbola Quintessence
- Subtracting a Rook from a Blocking Piece
Forum Posts
- Re: BitBoard Tests Magic v Non-Rotated 32 Bits v 64 Bits by Aleks Peshkov, CCC, August 25, 2007
- Hyperbola Quiesscene: hardly any improvement by trojanfoe, CCC, January 13, 2009
- Comparison of bitboard attack-getter variants by Sven Schüle, CCC, January 04, 2016
- Re: The wrong way by Aleks Peshkov, CCC, January 05, 2016 » SSSE3 Hyperbola Quintessence
- Understanding first rank attack state generation by Kalyankumar Ramaseshan, CCC, July 18, 2019 » First Rank Attacks
External Links
- GitHub - abulmo/hqperft: Chess move generation based on (H)yperbola (Q)uintessence & range attacks by Richard Delorme » Perft
- Sliding Pieces (Part 1) - Advanced Java Chess Engine Tutorial 8 by Jonathan Warkentin
- Hyperbola Quintessence for rooks along ranks by Tim Cooijmans, April 6, 2014 [7]
Hyperbola
Quintessence
- Quintessence from Wikipedia
- Quintessence (physics) from Wikipedia
- Quintessence the Fifth Element from Wikipedia
Misc
- Focus - Hocus Pocus, Pinkpop Festival 1972, Geleen, YouTube Video
References
- ↑ Samuel Bak - represented by Pucker Gallery since 1969
- ↑ _byteswap_uint64 Visual C++ Developer Center - Run-Time Library Reference
- ↑ Re: BitBoard Tests Magic v Non-Rotated 32 Bits v 64 Bits by Aleks Peshkov, CCC, August 25, 2007
- ↑ Intel 64 and IA32 Architectures Optimization Reference Manual (pdf)
- ↑ _mm_shuffle_epi8 Visual C++ Developer Center - Run-Time Library Reference
- ↑ Hyperbola Quintessence for rooks along ranks by Tim Cooijmans, April 6, 2014
- ↑ Re: Comparison of bitboard attack-getter variants by Matthew R. Brades, CCC, January 04, 2016