Skip to content

Commit

Permalink
tighter register usage in Do_Move
Browse files Browse the repository at this point in the history
  • Loading branch information
tthsqe12 committed Mar 15, 2017
1 parent 3af1457 commit 46866a7
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 87 deletions.
2 changes: 1 addition & 1 deletion asmFish/guts/Book.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ end virtual
mov ecx, dword[r13+ExtMove.move]
mov dword[rbx+State.currentMove], ecx
call Move_GivesCheck
mov edx, eax
mov ecx, dword[r13+ExtMove.move]
mov byte[rbx+State.givesCheck], al
call Move_Do__PerftGen_Root
mov rax, qword[rbx-sizeof.State+State.endMoves]
mov qword[rbx+State.cur], rax
Expand Down
2 changes: 2 additions & 0 deletions asmFish/guts/Def.asm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ SEP_CHAR equ ':'
; depths for search
ONE_PLY = 1
MAX_PLY = 128
MAX_SYZYGY_PLY = 20 ; how many times can the syzygy probe recurse?

DEPTH_QS_CHECKS = 0
DEPTH_QS_NO_CHECKS = -1
DEPTH_QS_RECAPTURES = -5
Expand Down
6 changes: 2 additions & 4 deletions asmFish/guts/EasyMoveMng.asm
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ EasyMoveMng_Update:
mov ecx, dword[rsi+EasyMoveMng.pv+4*0]
call Move_GivesCheck
mov ecx, dword[rsi+EasyMoveMng.pv+4*0]
mov edx, eax
add qword[rbp-Thread.rootPos+Thread.nodes], 1
mov byte[rbx+State.givesCheck], al
call Move_Do__EasyMoveMng
call SetCheckInfo
mov ecx, dword[rsi+EasyMoveMng.pv+4*1]
call Move_GivesCheck
mov ecx, dword[rsi+EasyMoveMng.pv+4*1]
mov edx, eax
add qword[rbp-Thread.rootPos+Thread.nodes], 1
mov byte[rbx+State.givesCheck], al
call Move_Do__EasyMoveMng

mov rax, qword[rbx+State.key]
Expand Down
88 changes: 44 additions & 44 deletions asmFish/guts/Move_Do.asm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ Move_Do:
; edx move is check

push rsi rdi r12 r13 r14 r15
push rcx rdx

; stack is unaligned at this point

Expand Down Expand Up @@ -209,8 +208,6 @@ ProfileInc Move_Do
movzx r10d, byte[rbp+Pos.board+r8] ; r10 = FROM PIECE
movzx r11d, byte[rbp+Pos.board+r9] ; r11 = TO PIECE

push r10

vmovq xmm5, qword[rbx+State.key]
vmovq xmm4, qword[rbx+State.pawnKey]
vmovq xmm3, qword[rbx+State.materialKey]
Expand Down Expand Up @@ -252,9 +249,9 @@ ProfileInc Move_Do

xor edx, edx
bts rdx, r8
push rdx
vmovq xmm8, rdx
bts rdx, r9
push rdx
vmovq xmm9, rdx
mov eax, r10d
and eax, 7
mov byte[rbp+Pos.board+r8], 0
Expand Down Expand Up @@ -295,7 +292,9 @@ end if

; write remaining data to next state entry

pop r9 r8 r10 rax rcx
movzx eax, byte[rbx+State.givesCheck]
vmovq r8, xmm8
vmovq r9, xmm9
; r9 = to + from
; r8 = from
; r10 = from piece
Expand Down Expand Up @@ -328,7 +327,7 @@ end if
bsf r15, r15 ; r15 = our king
bsf r14, r14 ; r14 = their king

test rax, rax
test eax, eax
jnz .MoveIsCheck
.CheckersDone:
mov qword[rbx+State.checkersBB], rax
Expand Down Expand Up @@ -410,8 +409,8 @@ end if
.MoveIsCheck:
mov rdx, qword[rbx+State.dcCandidates-sizeof.State]
mov rax, qword[rbx+State.checkSq-sizeof.State+8*r10]
cmp ecx, 1 shl 12
jae .DoFull
test ecx, ecx
jnz .DoFull
and rax, r9
test rdx, r8
jnz .DoFull
Expand Down Expand Up @@ -510,7 +509,7 @@ match =1, DEBUG {
je .EpCapture

.Promotion:
lea ecx, [rcx-MOVE_TYPE_PROM+8*rsi+Knight]
lea r14d, [rcx-MOVE_TYPE_PROM+8*rsi+Knight]


if PEDANTIC
Expand All @@ -523,11 +522,11 @@ if PEDANTIC
mov byte[rbp+Pos.pieceList+rax], dl
mov byte[rbp+Pos.pieceList+rdi], 64

movzx edx, byte[rbp+Pos.pieceEnd+rcx]
movzx edx, byte[rbp+Pos.pieceEnd+r14]
mov byte[rbp+Pos.pieceIdx+r9], dl
mov byte[rbp+Pos.pieceList+rdx], r9l
add edx, 1
mov byte[rbp+Pos.pieceEnd+rcx], dl
mov byte[rbp+Pos.pieceEnd+r14], dl
end if

; remove pawn r10 on square r9
Expand All @@ -544,58 +543,61 @@ end if
vpxor xmm3, xmm3, xmm7
vmovq xmm1, qword[Scores_Pieces+r10+8*r9]
vpsubd xmm6, xmm6, xmm1
; place piece ecx on square r9
mov eax, ecx
shr r10d, 6+3
; place piece r14 on square r9
mov eax, r14d
and eax, 7
mov rdx, qword[rbp+Pos.typeBB+8*rax]
bts rdx, r9
mov qword[rbp+Pos.typeBB+8*rax], rdx
mov byte[rbp+Pos.board+r9], cl
mov byte[rbp+Pos.board+r9], r14l
and rdx, qword[rbp+Pos.typeBB+8*rsi]
popcnt rax, rdx, r8
shl ecx, 6+3
vmovq xmm7, qword[Zobrist_Pieces+rcx+8*r9]
shl r14d, 6+3
vmovq xmm7, qword[Zobrist_Pieces+r14+8*r9]
vpxor xmm5, xmm5, xmm7
vmovq xmm7, qword[Zobrist_Pieces+rcx+8*(rax-1)]
vmovq xmm7, qword[Zobrist_Pieces+r14+8*(rax-1)]
vpxor xmm3, xmm3, xmm7
vmovq xmm1, qword[Scores_Pieces+rcx+8*r9]
vmovq xmm1, qword[Scores_Pieces+r14+8*r9]
vpaddd xmm6, xmm6, xmm1
jmp .SpecialRet



align 8
.EpCapture:
; remove pawn r10^8 on square ecx=r9+8*(2*esi-1)
lea ecx, [2*rsi-1]
lea ecx, [r9+8*rcx]
; remove pawn r10^8 on square r14=r9+8*(2*esi-1)
lea r14d, [2*rsi-1]
lea r14d, [r9+8*r14]
xor r10, 8
xor esi, 1
mov rdx, qword[rbp+Pos.typeBB+8*Pawn]
mov rdi, qword[rbp+Pos.typeBB+8*rsi]
btr rdx, rcx
btr rdi, rcx
btr rdx, r14
btr rdi, r14
mov qword[rbp+Pos.typeBB+8*Pawn], rdx
mov qword[rbp+Pos.typeBB+8*rsi], rdi
mov byte[rbp+Pos.board+rcx], 0
mov byte[rbp+Pos.board+r14], 0
and rdi, rdx
popcnt rdi, rdi, rdx
shl r10d, 6+3
vmovq xmm7, qword[Zobrist_Pieces+r10+8*rcx]
vmovq xmm7, qword[Zobrist_Pieces+r10+8*r14]
vpxor xmm5, xmm5, xmm7
vpxor xmm4, xmm4, xmm7
vmovq xmm7, qword[Zobrist_Pieces+r10+8*rdi]
vpxor xmm3, xmm3, xmm7
vmovq xmm1, qword[Scores_Pieces+r10+8*rcx]
vmovq xmm1, qword[Scores_Pieces+r10+8*r14]
vpsubd xmm6, xmm6, xmm1
shr r10d, 6+3
;xor r10d, 8 ; not needed only care about lower 3 bits
lea eax, [8*rsi+Pawn]
mov word[rbx+sizeof.State+State.rule50], 0
mov byte[rbx+sizeof.State+State.capturedPiece], al
if PEDANTIC
movzx edi, byte[rbp+Pos.pieceEnd+8*rsi+Pawn]
sub edi, 1
movzx edx, byte[rbp+Pos.pieceList+rdi]
movzx eax, byte[rbp+Pos.pieceIdx+rcx]
movzx eax, byte[rbp+Pos.pieceIdx+r14]
mov byte[rbp+Pos.pieceEnd+8*rsi+Pawn], dil
mov byte[rbp+Pos.pieceIdx+rdx], al
mov byte[rbp+Pos.pieceList+rax], dl
Expand All @@ -622,9 +624,6 @@ match =1, DEBUG {
jne Move_Do_badcas
@@:
}
push rax ; these are popped before exit
push rax ; and not used because move type is special

; fix things caused by kingXrook encoding
mov byte[rbx+sizeof.State+State.capturedPiece], 0

Expand All @@ -633,30 +632,30 @@ match =1, DEBUG {
and edx, 56
cmp r9d, r8d
sbb eax, eax
lea ecx, [rdx+4*rax+FILE_G]
lea r14d, [rdx+4*rax+FILE_G]
lea edx, [rdx+2*rax+FILE_F]
lea r11d, [r10-King+Rook]

mov byte[rbp+Pos.board+r8], 0
mov byte[rbp+Pos.board+r9], 0
mov byte[rbp+Pos.board+rcx], r10l
mov byte[rbp+Pos.board+r14], r10l
mov byte[rbp+Pos.board+rdx], r11l

if PEDANTIC
; movzx eax, byte[rbp+Pos.pieceIdx+r8]
; movzx edi, byte[rbp+Pos.pieceIdx+r9]
; mov byte[rbp+Pos.pieceList+rax], cl
; mov byte[rbp+Pos.pieceList+rax], r14l
; mov byte[rbp+Pos.pieceList+rdi], dl
; mov byte[rbp+Pos.pieceIdx+rcx], al
; mov byte[rbp+Pos.pieceIdx+r14], al
; mov byte[rbp+Pos.pieceIdx+rdx], dil
; no! above not enough instructions! official stockfish has
; castling rook moved to the back of the list
; of course this for absolutely no good reason
movzx eax, byte[rbp+Pos.pieceIdx+r8]
movzx edi, byte[rbp+Pos.pieceIdx+r9]
mov byte[rbp+Pos.pieceList+rax], cl
mov byte[rbp+Pos.pieceList+rax], r14l
mov byte[rbp+Pos.pieceList+rdi], dl
mov byte[rbp+Pos.pieceIdx+rcx], al
mov byte[rbp+Pos.pieceIdx+r14], al
mov byte[rbp+Pos.pieceIdx+rdx], dil
; now move rook to the back of the list
movzx eax, byte[rbp+Pos.pieceEnd+r11]
Expand All @@ -680,7 +679,7 @@ end if

mov rax, qword[Zobrist_Pieces+r10+8*r8]
xor rax, qword[Zobrist_Pieces+r11+8*r9]
xor rax, qword[Zobrist_Pieces+r10+8*rcx]
xor rax, qword[Zobrist_Pieces+r10+8*r14]
xor rax, qword[Zobrist_Pieces+r11+8*rdx]
vmovq xmm7, rax
vpxor xmm5, xmm5, xmm7
Expand All @@ -689,24 +688,25 @@ end if
vmovd xmm2, dword[Scores_Pieces+r11+8*r9]
vpsubd xmm6, xmm6, xmm1
vpsubd xmm6, xmm6, xmm2
vmovd xmm1, dword[Scores_Pieces+r10+8*rcx]
vmovd xmm1, dword[Scores_Pieces+r10+8*r14]
vmovd xmm2, dword[Scores_Pieces+r11+8*rdx]
vpaddd xmm6, xmm6, xmm1
vpaddd xmm6, xmm6, xmm2
shr r10d, 6+3

mov rax, qword[rbp+Pos.typeBB+8*rsi]
mov r10, qword[rbp+Pos.typeBB+8*King]
mov r13, qword[rbp+Pos.typeBB+8*King]
mov r11, qword[rbp+Pos.typeBB+8*Rook]
btr rax, r8
btr rax, r9
bts rax, rcx
bts rax, r14
bts rax, rdx
btr r10, r8
bts r10, rcx
btr r13, r8
bts r13, r14
btr r11, r9
bts r11, rdx
mov qword[rbp+Pos.typeBB+8*rsi], rax
mov qword[rbp+Pos.typeBB+8*King], r10
mov qword[rbp+Pos.typeBB+8*King], r13
mov qword[rbp+Pos.typeBB+8*Rook], r11
jmp .SpecialRet

Expand Down
4 changes: 2 additions & 2 deletions asmFish/guts/Perft.asm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ end virtual
jz .MoveLoopDone
mov ecx, dword[rsi]
call Move_GivesCheck
mov edx, eax
mov ecx, dword[rsi]
mov byte[rbx+State.givesCheck], al
call Move_Do__PerftGen_Root
mov eax, 1
lea ecx, [r15-1]
Expand Down Expand Up @@ -154,8 +154,8 @@ end virtual
jz .DepthNDone
.DepthNLoop:
call Move_GivesCheck
mov edx, eax
mov ecx, dword[rsi]
mov byte[rbx+State.givesCheck], al
call Move_Do__PerftGen_Branch
mov ecx, r15d
call Perft_Branch
Expand Down
6 changes: 3 additions & 3 deletions asmFish/guts/Position.asm
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ Position_CopyToSearch:
; if rcx < MAX_PLY+102, we need to realloc
mov rdx, qword[r13+Pos.stateEnd]
sub rdx, r9
cmp rdx, sizeof.State*(100+MAX_PLY+2)
cmp rdx, sizeof.State*(100+MAX_PLY+2+MAX_SYZYGY_PLY)
jb .realloc
.copy_states:
; r9 = address of its state table
Expand Down Expand Up @@ -1443,11 +1443,11 @@ Position_CopyToSearch:
; rdx already has the size
call _VirtualFree
.alloc:
mov ecx, sizeof.State*(100+MAX_PLY+2)
mov ecx, sizeof.State*(100+MAX_PLY+2+MAX_SYZYGY_PLY)
call _VirtualAlloc
mov r9, rax
mov qword[r13+Pos.stateTable], rax
lea rax, [rax+sizeof.State*(100+MAX_PLY+2)]
add rax, sizeof.State*(100+MAX_PLY+2+MAX_SYZYGY_PLY)
mov qword[r13+Pos.stateEnd], rax
jmp .copy_states

Expand Down
9 changes: 3 additions & 6 deletions asmFish/guts/QSearchMacros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ virtual at rsp
.futilityBase rd 1

.inCheck rb 1 ; 104
.givesCheck rb 1 ; 105
rb 1 ; 105
.singularExtensionNode rb 1 ; 106
.improving rb 1 ; 107
.captureOrPromotion rb 1 ; 108
Expand Down Expand Up @@ -288,10 +288,8 @@ end if
; check for check and get address of search function
mov ecx, eax
call Move_GivesCheck


mov byte[.givesCheck], al
movsx r13d, al
mov byte[rbx+State.givesCheck], al
mov r13d, eax
if .PvNode eq 1
lea rdx, [QSearch_Pv_NoCheck]
lea rcx, [QSearch_Pv_InCheck]
Expand Down Expand Up @@ -399,7 +397,6 @@ end if

; make the move
mov ecx, dword[.move]
movzx edx, byte[.givesCheck]
mov dword[rbx+State.currentMove], ecx
mov rsi, qword[.searchFxn]
call Move_Do__QSearch
Expand Down
Loading

0 comments on commit 46866a7

Please sign in to comment.