Changes

Jump to: navigation, search

Obstruction Difference

2,082 bytes removed, 17:54, 25 April 2022
no edit summary
'''Obstruction Difference''',<br/>
a subtraction approach to determine sliding piece attacks with bitboards, proposed by [[Michael Hoffmann]] <ref>[http://www.talkchess.com/forum/viewtopic.php?t=29087 DIRECT BITBOARD MOVEGENERATION] by [[Michael Hoffmann]], [[CCC]], July 23, 2009</ref>. The idea is to get disjoint [[On an empty Board#PositiveRays|positive]] and [[On an empty Board#NegativeRays|negative ray masks]] of the [[Occupancy|occupancy]], and to subtract the [[General Setwise Operations#TheMostSignificantOneBitMS1B|most significant one bit]] of the negative ray from the doubled [[General Setwise Operations#TheLeastSignificantOneBitLS1B|least siginificat significant one bit]] (LS1B) of the positive ray. The difference is finally intersected with the linemask, excluding the [[Sliding Pieces|sliding piece]] itself from the attack set. However, in 2022, [[Daniel Infuehr]] found a faster solution to calculate the obstruction difference: subtract the most significant one bit of the negative ray from the the positive ray obstruction, followed by the [[General Setwise Operations#ExclusiveOr|symmetric difference]] to flip all positive ray obstruction bits for the proper result <ref>[https://www.talkchess.com/forum3/viewtopic.php?f=7&t=79701&start=2 Re: Sliding Piece Algorithm by Abstract Syntax Tree Sifting <nowiki>[RELEASE]</nowiki>] by [[Daniel Infuehr]], [[CCC]], April 17, 2022</ref>.
=Empty Sets=
<pre>
/*
* @author Michael Hoffmann, Gerd IsenbergDaniel Infuehr
* @param U64 occ occupancy of the board
* @param SMasks* a mask structure by square and line
*/
U64 lineAttacks(U64 occ, const SMasks *pMask) {
U64 lower, upper, mMS1B, ls1bms1B, odiff;
lower = pMask->lower & occ;
upper = pMask->upper & occ;
mMS1B ms1B = -C64(10x8000000000000000u) << bsr64>> lzcnt(lower | 1); // ms1b of lower (at least bit zero) // ls1b = upper & -upper; // odiff = 2 * ls1b + mMS1B- ms1B; odiff = upper ^ (upper - ms1B); // x86 lea option due to add -ms1bDaniel Infuehr's improvement
return pMask->lineEx & odiff; // (pMask->lower | pMask->upper) & odiff;
}
}
</pre>
Since with two's complement i << x == - (-i << x), 2*high-low is done by add for the option to use [http://www.cs.virginia.edu/%7Eevans/cs216/guides/x86.html x86 lea-instruction]. The routine looks competitive for CPUs with fast [[x86-64#gpinstructions|bsr]] or [[x86-64#gpinstructions|lzcnt]] but has problems on processors with slow bitScan or 32-bit processors. Register usage is moderate and should suffice all x88-64 scratch registers for two lines, even for [https://en.wikipedia.org/wiki/Visual_C%2B%2B Visual C++].
=Java=
<pre>
/*
* @author Michael Hoffmann, Gerd IsenbergDaniel Infuehr
* @param long occ occupancy of the board
* @param int sq 0..63
long lower = longMasks[sq][line][0] & occ;
long upper = longMasks[sq][line][1] & occ;
long mMs1b = 0x8000000000000000 >>> Long.numberOfLeadingZeros (lower | 1); long ls1b odiff = upper & ^ (upper -upper; long odiff = 2*ls1b + mMs1b);
return longMasks[sq][line][2] & odiff;
}
</pre>
One may also use unsigned shift right, to subtract the MS1B.
<pre>
long ms1b = 0x8000000000000000 >>> Long.numberOfLeadingZeros (lower | 1);
long ls1b = upper & -upper;
long odiff = 2*ls1b - ms1b;
</pre>
 
=How it works=
Following bitboard diagrams demonstrate how it works with [[Square Mapping Considerations#LittleEndianRankFileMapping|Little-Endian Rank-File Mapping]], file attacks rook on d4 (27):
<pre>
occupancy fileMaskEx(d4) occupancy d-file
. . . 1 . 1 . . . . . 1 . . . . . . . 1 . . . .
1 . 1 . . 1 1 . . . . 1 . . . . . . . . . . . .
. 1 . 1 . . . 1 . . . 1 . . . . . . . 1 . . . .
. . . . . 1 . . . . . 1 . . . . . . . . . . . .
. 1 . 1 . . . . & . . . . . . . . = . . . R . . . .
. . . . . . 1 . . . . 1 . . . . . . . . . . . .
1 1 . 1 . 1 . 1 . . . 1 . . . . . . . 1 . . . .
. . . 1 1 . 1 . . . . 1 . . . . . . . 1 . . . .
______________________________________
 
occupancy upper occupancy lower
. . . 1 . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . 1 . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . 1 . . . .
. . . . . . . . . . . 1 . . . .
|
LS1B upper v lower | 1
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . 1 . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . 1 . . . .
. . . . . . . . 1 . . 1 . . . .
______________________________________
 
2* LS1B upper - MS1B (lower | 1) = 2*LS1Bhigh-MS1Blow
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . 1 . . . . . . . . . . . 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1
. . . . . . . . - . . . . . . . . = 1 1 1 1 1 1 1 1
. . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1
. . . . . . . . . . . 1 . . . . . . . 1 1 1 1 1
. . . . . . . . . . . . . . . . . . . . . . . .
 
 
2*LS1Bhigh-MS1Blow fileMaskEx = fileAttacks
. . . . . . . . . . . 1 . . . . . . . . . . . .
. . . . . . . . . . . 1 . . . . . . . . . . . .
1 1 1 1 . . . . . . . 1 . . . . . . . 1 . . . .
1 1 1 1 1 1 1 1 . . . 1 . . . . . . . 1 . . . .
1 1 1 1 1 1 1 1 & . . . . . . . . = . . . . . . . .
1 1 1 1 1 1 1 1 . . . 1 . . . . . . . 1 . . . .
. . . 1 1 1 1 1 . . . 1 . . . . . . . 1 . . . .
. . . . . . . . . . . 1 . . . . . . . . . . . .
</pre>
=See also=
* [[SBAMG]]
 
=Forum Posts=
* [http://www.talkchess.com/forum/viewtopic.php?t=29087 DIRECT BITBOARD MOVEGENERATION] by [[Michael Hoffmann]], [[CCC]], July 23, 2009
* [https://www.talkchess.com/forum3/viewtopic.php?f=7&t=79701&start=2 Re: Sliding Piece Algorithm by Abstract Syntax Tree Sifting <nowiki>[RELEASE]</nowiki>] by [[Daniel Infuehr]], [[CCC]], April 17, 2022
* [https://www.talkchess.com/forum3/viewtopic.php?f=7&t=79078&start=56 Re: Comparison of all known Sliding lookup algorithms <nowiki>[CUDA]</nowiki>] by [[Daniel Infuehr]], [[CCC]], April 24, 2022
=References=
<references />
 
'''[[Sliding Piece Attacks|Up one Level]]'''
[[Category:Man Ray]]

Navigation menu