Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mlang committed Nov 17, 2024
1 parent 5d88fff commit d6645c0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
27 changes: 13 additions & 14 deletions src/Game/Chess/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -660,18 +660,17 @@ bKscm = move E8 G8
bQscm = move E8 C8

attackedBy :: Color -> QuadBitboard -> Word64 -> Square -> Bool
attackedBy White !qbb !occ (Sq sq) =
(unsafeIndex wPawnAttacks sq .&. QBB.wPawns qbb) .|.
(unsafeIndex knightAttacks sq .&. QBB.wKnights qbb) .|.
(diagonal sq occ .&. QBB.wDiagonals qbb) .|.
(orthogonal sq occ .&. QBB.wOrthogonals qbb) .|.
(unsafeIndex kingAttacks sq .&. QBB.wKings qbb) /= 0
attackedBy Black !qbb !occ (Sq sq) =
(unsafeIndex bPawnAttacks sq .&. QBB.bPawns qbb) .|.
(unsafeIndex knightAttacks sq .&. QBB.bKnights qbb) .|.
(diagonal sq occ .&. QBB.bDiagonals qbb) .|.
(orthogonal sq occ .&. QBB.bOrthogonals qbb) .|.
(unsafeIndex kingAttacks sq .&. QBB.bKings qbb) /= 0
attackedBy c !qbb !occ (Sq sq) =
( pawnAttacks sq .&. QBB.pawns qbb
.|. unsafeIndex knightAttacks sq .&. QBB.knights qbb
.|. diagonal sq occ .&. QBB.diagonals qbb
.|. orthogonal sq occ .&. QBB.orthogonals qbb
.|. unsafeIndex kingAttacks sq .&. QBB.kings qbb
) .&. us /= 0
where
(# !pawnAttacks, !us #) = case c of
White -> (# unsafeIndex wPawnAttacks, QBB.white qbb #)
Black -> (# unsafeIndex bPawnAttacks, QBB.black qbb #)

{-# INLINE attackedBy #-}

Expand Down Expand Up @@ -720,7 +719,7 @@ bPawnAttacks = Vector.generate 64 $ \sq -> let b = bit sq in
shiftNE b .|. shiftNW b

orthogonal, diagonal :: Int -> Bitboard -> Bitboard
orthogonal !sq !occ = mask .&. ((up .&. down) .|. (left .&. right)) where
orthogonal !sq !occ = mask .&. (up .&. down .|. left .&. right) where
mask = complement $ unsafeShiftL 1 sq
occ' = occ .&. mask
up = unsafeShiftR hFile $ (63 -) $ bitScanForward $
Expand All @@ -731,7 +730,7 @@ orthogonal !sq !occ = mask .&. ((up .&. down) .|. (left .&. right)) where
unsafeShiftL rank1 sq .&. (occ' .|. hFile)
left = unsafeShiftL rank1 $ bitScanReverse $
unsafeShiftR rank8 (63 - sq) .&. (occ' .|. aFile)
diagonal !sq !occ = mask .&. ((up .&. down) .|. (left .&. right)) where
diagonal !sq !occ = mask .&. (up .&. down .|. left .&. right) where
mask = complement $ unsafeShiftL 1 sq
occ' = occ .&. mask
up = unsafeShiftR a1h8 $ (63 -) $ bitScanForward $
Expand Down
16 changes: 4 additions & 12 deletions src/Game/Chess/Internal/QuadBitboard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Game.Chess.Internal.QuadBitboard (
, pawns, knights, bishops, rooks, queens, kings
, wPawns, wKnights, wBishops, wRooks, wQueens, wKings
, bPawns, bKnights, bBishops, bRooks, bQueens, bKings
, wOrthogonals, bOrthogonals, wDiagonals, bDiagonals
, orthogonals, diagonals
, insufficientMaterial
, toString
-- * Square codes
Expand Down Expand Up @@ -99,13 +99,9 @@ bRooks = liftA2 (.&.) rooks black
bQueens = liftA2 (.&.) queens black
bKings = liftA2 (.&.) kings black

orthogonals, diagonals, wOrthogonals, bOrthogonals, wDiagonals, bDiagonals :: QuadBitboard -> Word64
diagonals = liftA2 (.&.) pbq (complement . pawns)
orthogonals = liftA2 (.&.) rqk (complement . kings)
wOrthogonals = liftA2 (.&.) orthogonals (complement . black)
bOrthogonals = liftA2 (.&.) orthogonals black
wDiagonals = liftA2 (.&.) diagonals (complement . black)
bDiagonals = liftA2 (.&.) diagonals black
orthogonals, diagonals :: QuadBitboard -> Word64
diagonals = liftA2 xor pbq pawns
orthogonals = liftA2 xor rqk kings


{-# INLINE occupied #-}
Expand All @@ -130,10 +126,6 @@ bDiagonals = liftA2 (.&.) diagonals black
{-# INLINE bKings #-}
{-# INLINE diagonals #-}
{-# INLINE orthogonals #-}
{-# INLINE wOrthogonals #-}
{-# INLINE bOrthogonals #-}
{-# INLINE wDiagonals #-}
{-# INLINE bDiagonals #-}



Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-22.38
resolver: lts-22.41
packages:
- .
flags:
Expand Down

0 comments on commit d6645c0

Please sign in to comment.