Haswell New Instructions

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Haswell New Instructions

Post by Gerd Isenberg »

Ok, the magic alternative with ~200 KB (< 10K for Bishops), preserving the input parameters, looks really promising:

Code: Select all

; input&#58;  rcx square index 0..63
;         rdx occupancy
; output&#58; rax rook attacks

  pext    rax, rdx, quad word ptr &#91;rookMaskEx + 8*rcx&#93;  ; 10,11,12 bits
  mov     r10, &#91;rookSquarePointer + 8*rcx&#93; ; pointer to square array
  movzx   rax, word ptr &#91;r10 + 2*rax&#93;
  pdep    rax, rax, quad word ptr &#91;rookMask + 8*rcx&#93;
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: Haswell New Instructions

Post by Gerd Isenberg »

Or pext only with the conventional magic array sizes, pext saves "and", mul, shift, not to mention the lookup of the factor and variable shift:

Code: Select all

; input&#58;  rcx square index 0..63
;         rdx occupancy
; output&#58; rax rook attacks

  pext    rax, rdx, quad word ptr &#91;rookMaskEx + 8*rcx&#93;  ; 10,11,12 bits
  mov     r10, &#91;rookSquarePointer + 8*rcx&#93; ; pointer to square array
  mov     rax, word ptr &#91;r10 + 8*rax&#93;
ibid
Posts: 89
Joined: Mon Jun 13, 2011 12:09 pm

Re: Haswell New Instructions

Post by ibid »

Zach Wegner wrote:The instructions, particularly the bit manipulation instructions, look pretty awesome for chess. Check out PEXT/PDEP: instead of magic bitboards, you can just get the attack mask, extract out the relevant bits with PEXT, do a table lookup of a 2-byte value (just a compressed attack bitboard), and use PDEP to decompress back to an attack bitboard. Too bad there's not a vector version too :)
Excellent. I've been waiting for those two instructions for many years.
Always seemed like an "obvious" way to do it.

You can save a little memory on the 2-byte tables too. For example,
the rook tables for h1 and a8 would be identical. Doesn't seem to work
for many tables though.