Changes

Jump to: navigation, search

BitScan

1,420 bytes added, 19:33, 28 April 2022
no edit summary
<span id="TrailingZeroCount"></span>
==Trailing Zero Count==
Bitscan forward is identical with a '''Trailing Zero Count''' for none empty sets, possibly available as machine instruction on some architectures, for instance the [[x86-64]] bit-manipulation expansion set [[BMI1]], with some difficulties to use the [[BMI1#TZCNT|_tzcnt_u64]] intrisic with [https://en.wikipedia.org/wiki/Microsoft_Visual_Studio Visual Studio] <ref>[http://www.talkchess.com/forum3/viewtopic.php?f=7&t=74989 Looking for intrinsic "least significant bit" on Visual Studio] by [[Oliver Brausch]], [[CCC]], September 03, 2020</ref>.
<span id="DeBruijnMultiplation"></span>
==De Bruijn Multiplication==
<span id="MattTaylorsFoldingtrick"></span>
==Matt Taylor's Folding trick==
A 32-bit friendly implementation to find the the bit-index of [[General Setwise Operations#TheLeastSignificantOneBitLS1B|LS1B]] by [[Matt Taylor]] <ref>[httphttps://groups.google.com/groupd/msg/comp.lang.asm.x86/browse_frm3pVGzQGb1ys/fPpKBKNi848J Bit magic] by [[Matt Taylor]], [https://threadgroups.google.com/de9546cd019bd72bforum/f46209f47d2a7ddb?lnk=gst&q=Matt+Taylor+magic#f46209f47d2a7ddb !forum/comp.lang.asm.x86 comp.lang.asm.x86], June 26, 2003</ref> <ref>[https://groups.google.com/d/msg/comp.lang.asm.x86/3pVGzQGb1ys/230qffQJYvQJ Re: Bit magic] by [[Matt Taylor]], [httphttps://groups.google.com/groupforum/#!forum/comp.lang.asm.x86/topics comp.lang.asm.x86], June 2629, 2003</ref>. The [[General Setwise Operations#ExclusiveOr|xor]] with the ones' decrement, [[General Setwise Operations#LS1BSeparation|bb ^ (bb-1)]] contains all bits set including and below the [[General Setwise Operations#TheLeastSignificantOneBitLS1B|LS1B]]. The 32-bit xor-difference of both halves yields either the complement of the upper half, or the lower half otherwise. Some samples:
{| class="wikitable"
|-
<span id="BitscanwithReset"></span>
=Bitscan with Reset=
While [[Bitboard Serialization|traversing sets]], one may combine bitscanning with reset found bit. That implies passing the bitboard per reference or pointer, and tends to confuse compilers to keep all inside registers inside a typical serialization loop <ref>[https://www.stmintz.com/ccc/index.php?id=283655 Bitscan] by [[Matt Taylor]], [[CCC]], February 11, 2003</ref> <ref>[http://www.talkchess.com/forum3/viewtopic.php?f=7&t=70202 CPW bitscan with reset could someone explain this line?] by [[Mahmoud Uthman]], [[CCC]], March 14, 2019</ref> .
<pre>
int bitScanForwardWithReset(U64 &bb) { // also called dropForward
| rowspan="3" |[https://en.wikipedia.org/wiki/Sandy_Bridge Sandy Bridge]<br/>
[https://en.wikipedia.org/wiki/Ivy_Bridge_%28microarchitecture%29 Ivy Bridge]<br/>
[https://en.wikipedia.org/wiki/Haswell_%28microarchitecture%29 Haswell] <ref>[http://users.atw.hu/instlatx64/GenuineIntel00306C3_Haswell_InstLatX64.txt Haswell Instructions Latency]</ref>
| style="text-align:center;" | BSF/BSR
| style="text-align:right;" | 3
| style="text-align:right;" | 1
|-
| style="text-align:center;" | [[BMI1#LZCNT|TZCNT]]
| style="text-align:right;" | 3
| style="text-align:right;" | 1
* [[RedQueen#BitScan|BitScan in RedQueen]]
* [[Spector#BitScan|BitScan in Spector]]
* [[Tucano#BitScan|BitScan in Tucano]]
=See also=
* [http://groups.google.com/group/rec.games.chess.computer/browse_frm/thread/9658009e315021fe# bitboard 2^i mod 67 is unique] by [[Stefan Plenkner]], [[Computer Chess Forums|rgcc]], August 7, 1996
* [http://groups.google.com/group/rec.games.chess.computer/browse_frm/thread/871851f83e2c429f# bitboard 2^i mod 67 is unique] by [[Joël Rivat]], [[Computer Chess Forums|rgcc]], September 2, 1996
* [https://www.stmintz.com/ccc/index.php?id=20057 Question to Bob: Crafty , Alpha and FindBit()] by [[Guido Schimmels]], [[CCC]], June 05, 1998» [[Crafty]], [[DEC Alpha]]
* [https://www.stmintz.com/ccc/index.php?id=39619 To Nalimov and other programmers about BSF/BSR in VC] by [[Dezhi Zhao]], [[CCC]], January 16, 1999
==2000 ...==
* [https://www.stmintz.com/ccc/index.php?id=283655 Bitscan] by [[Matt Taylor]], [[CCC]], February 11, 2003
* [https://www.stmintz.com/ccc/index.php?id=291062 FirstOne for Linux] by [[Sune Fischer]], [[CCC]], March 29, 2003
* [httphttps://groups.google.com/groupd/msg/comp.lang.asm.x86/browse_frm3pVGzQGb1ys/fPpKBKNi848J Bit magic] by [[Matt Taylor]], [https://threadgroups.google.com/de9546cd019bd72bforum/f46209f47d2a7ddb?lnk=gst&q=Matt+Taylor+magic#f46209f47d2a7ddb !forum/comp.lang.asm.x86 comp.lang.asm.x86], June 26, 2003: [https://groups.google.com/d/msg/comp.lang.asm.x86/3pVGzQGb1ys/230qffQJYvQJ Re: Bit magic] by [[Matt Taylor]], [httphttps://groups.google.com/groupforum/#!forum/comp.lang.asm.x86/topics comp.lang.asm.x86], June 2629, 2003
* [https://www.stmintz.com/ccc/index.php?id=339225 Re: De Bruijn Sequence Generator] by [[Dieter Bürßner]], [[CCC]], December 30, 2003 » [[De Bruijn Sequence Generator]]
* [https://www.stmintz.com/ccc/index.php?id=348097 Determining location of LSB/MSB] by [[Jan Renze Steenhuisen|Renze Steenhuisen]], [[CCC]], February 09, 2004
* [http://www.talkchess.com/forum/viewtopic.php?t=46040 Optimizing bitboards for ARM] by [[Martin Sedlak]], [[CCC]], November 17, 2012
* [http://www.talkchess.com/forum/viewtopic.php?t=54704 Symmetric move generation using bitboards] by [[Lasse Hansen]], [[CCC]], December 20, 2014
* [http://www.talkchess.com/forum/viewtopic.php?t=54798 Stockfish 32-bit and hardware instructions on MSVC++] by [[Syed Fahad]], [[CCC]], December 30, 2014 » [[Stockfish]], [[BitScan]], [[Population Count]]
==2015 ...==
* [http://www.talkchess.com/forum/viewtopic.php?t=57400 Fun with De Bruijn] by [[Henk van den Belt]], [[CCC]], August 27, 2015
* [http://www.talkchess.com/forum/viewtopic.php?t=58230&start=3 Re: Linux Version of Maverick 1.5] by [[Michael Dvorkin]], [[CCC]], November 12, 2015 » [[Mac OS|OS X]], [[Maverick]]
* [http://www.talkchess.com/forum/viewtopic.php?t=61559 syzygy users (and Ronald)] by [[Robert Hyatt]], [[CCC]], September 29, 2016 » [[Population Count]]
* [http://www.talkchess.com/forum3/viewtopic.php?f=7&t=70202 CPW bitscan with reset could someone explain this line?] by [[Mahmoud Uthman]], [[CCC]], March 14, 2019
==2020 ...==
* [http://www.talkchess.com/forum3/viewtopic.php?f=7&t=74989 Looking for intrinsic "least significant bit" on Visual Studio] by [[Oliver Brausch]], [[CCC]], September 03, 2020 » [[BitScan#TrailingZeroCount|Trailing Zero Count]]
* [http://www.talkchess.com/forum3/viewtopic.php?f=7&t=75818 C++20 standard bit operations] by [[Jon Dart]], [[CCC]], November 15, 2020 » [[General Setwise Operations]], [[Population Count]], [[Cpp|C++]]
* [https://www.talkchess.com/forum3/viewtopic.php?f=7&t=79739 Optimizing Matt Taylor's Folding trick] by [[Daniel Infuehr]], [[CCC]], April 21, 2022
=External Links=
=References=
<references />
 
'''[[Bitboards|Up one Level]]'''
[[Category:M. C. Escher]]
[[Category:Frank Zappa]]

Navigation menu