Changes

Jump to: navigation, search

SIMD Techniques

2,612 bytes added, 16:40, 18 May 2018
Created page with "'''Home * Board Representation * Bitboards * Sliding Piece Attacks * SIMD techniques''' x86 MMX- or SSE2 SIMD and SWAR Techniques|SIMD..."
'''[[Main Page|Home]] * [[Board Representation]] * [[Bitboards]] * [[Sliding Piece Attacks]] * SIMD techniques'''

[[x86]] [[MMX]]- or [[SSE2]] [[SIMD and SWAR Techniques|SIMD]]-instruction sets provide a '''Packed Move Mask Byte''', pmovmskb-instruction <ref>[https://x86.puri.sm/html/file_module_x86_id_243.html PMOVMSKB: Move Byte Mask (x86 Instruction Set Reference)]</ref>, available in [[C]]/[[Cpp|C++]] as [[SSE2#_mm_movemask_epi|_mm_movemask_epi]] intrinsic, which moves the most-significant bits of each byte of a MMX- or XMM-register to the lowest 8 or 16 bits of a general purpose register. Thus, this instruction may be used to map [[Files|file]]- or [[Diagonals|diagonal]] occupancies to consecutive [[Bit|bits]].

=Bishop Attacks=
For diagonals one may mask and compare byte-wise to get the occupancy to the sign-bits. With [[SSE2]] and 128-bit XMM-registers one may process both diagonal- and anti-diagonal-occupancies in one run:
<pre>
u64 fillRightAttacks[8][64]; // [file][occupiedIndex]
__m128i xmmBmask[64]; // antidiagonal::diagonal - masks

U64 bishopAttacksSSE2(U64 occ, unsigned int sq) {
__m128 mocc;
mocc = _mm_cvtsi64x_si128(occ); // gp to xmm, 0:occ
mocc = _mm_unpacklo_epi64(mocc, mocc); // occupancy to both xmm-halfs, occ:occ
mocc = _mm_and_si128 (mocc, xmmBmask[sq]); // mask diagonal and antidiagonal
mocc = _mm_cmpeq_epi8(mocc, xmmBmask[sq]); // cmp bytewise equal, FF if set, 00 otherwise
unsigned int o = _mm_movemask_epi(mocc); // get the 16 sign bits
return (xmmBmask[sq].m128i_u64[0] & fillRightAttacks[sq>>3][(o>>1)&63])
| (xmmBmask[sq].m128i_u64[1] & fillRightAttacks[sq>>3][(o>>9)&63]);
}
</pre>
This sample code uses a shared 4KByte fill right lookup similar to fillUpAttacks of [[Kindergarten Bitboards|kindergarten bitboards]]. Of course one may use distinct lookup tables similar to [[Rotated Bitboards|rotated bitboards]] indexed by square and occupied-state without the trailing mask ands.

=See also=
* [[SSSE3#SSSE3Version|SSSE3 Version]] of [[Hyperbola Quintessence]]
* [[SSE2#EastAttacks|Fill Right with SSE2-instructions]]
* [[SSE2#SSE2WrapperinCpp|SSE2-Wrapper in C++]]

=Forum Posts=
* [http://www.talkchess.com/forum/viewtopic.php?t=29296&start=4 Re: Kindergarten bitboards without multiplying] by [[Wylie Garvin]], [[CCC]], August 08, 2009

=External Links=
* [https://en.wikipedia.org/wiki/SIMD SIMD from Wikipedia]
* [https://x86.puri.sm/html/file_module_x86_id_243.html PMOVMSKB: Move Byte Mask (x86 Instruction Set Reference)]

=References=
<references />

'''[[Sliding Piece Attacks|Up one Level]]'''

Navigation menu