//[MethodImpl(MethodImplOptions.AggressiveInlining)]
 private void BitmaskToMoves(Board board, Bitboard bitmask, Position positionFrom, Piece piece, Move[] moves, ref int moveCount)
 {
     while (bitmask != 0)
     {
         var i    = bitmask.BitScanForward();
         var move = new Move(positionFrom, i, piece, board.ArrayBoard[i]);
         moves[moveCount++] = move;
         bitmask           &= bitmask - 1;
     }
 }
        private ulong GetAttackedByKnights(Bitboard knights)
        {
            ulong allJumps = 0;

            while (knights != 0)
            {
                var   i     = knights.BitScanForward();
                ulong jumps = BitboardConstants.KnightJumps[i];
                allJumps |= jumps;
                knights  &= knights - 1;
            }
            return(allJumps);
        }
        private ulong GetAttackedByKings(Bitboard kings)
        {
            ulong allJumps = 0;

            while (kings != 0)
            {
                var   i     = kings.BitScanForward();
                ulong jumps = BitboardConstants.KingJumps[i];
                allJumps |= jumps;
                kings    &= kings - 1;
            }
            return(allJumps);
        }
        //[MethodImpl(MethodImplOptions.AggressiveInlining)]
        private ulong GetAttackedBySlidingPieces(Bitboard allPieces, Bitboard slidingPieces, bool diagonal)
        {
            var allSlide = 0UL;

            while (slidingPieces != 0)
            {
                var i     = slidingPieces.BitScanForward();
                var slide = diagonal ? SlideMoveGenerator.DiagonalAntidiagonalSlide(allPieces, i) : SlideMoveGenerator.HorizontalVerticalSlide(allPieces, i);
                allSlide      |= slide;
                slidingPieces &= slidingPieces - 1;
            }
            return(allSlide);
        }
        public bool IsBitboardAttacked(Board board, Bitboard bitboard, bool byWhite)
        {
            while (bitboard != 0)
            {
                var position = bitboard.BitScanForward();
                var attacked = IsPositionAttacked(board, position, byWhite);
                if (attacked)
                {
                    return(true);
                }
                bitboard &= bitboard - 1;
            }

            return(false);
        }
        public void GetAllPotentialMoves(Board board, Move[] moves, ref int moveCount, Bitboard checkers, Bitboard pinned)
        {
            var checkCount = checkers.PopCount();

            if (checkCount > 1)
            {
                GetPotentialKingMoves(board, moves, ref moveCount);
                return;
            }

            var allowedFrom = ~0UL;
            var allowedTo   = ~0UL;

            if (checkCount == 1)
            {
                allowedFrom = ~pinned;

                var checkerPos = checkers.BitScanForward();
                allowedTo = BitboardConstants.Between[board.KingPositions[board.ColorToMove]][checkerPos] | checkers;
            }
            else
            {
                GetPotentialCastlingMoves(board, moves, ref moveCount);
            }

            GetPotentialKnightMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            GetPotentialBishopMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            GetPotentialRookMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            GetPotentialQueenMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            GetPotentialKingMoves(board, moves, ref moveCount);

            if (board.WhiteToMove)
            {
                GetPotentialWhitePawnCaptures(board, allowedFrom, allowedTo, moves, ref moveCount);
                GetPotentialWhitePawnMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            }
            else
            {
                GetPotentialBlackPawnCaptures(board, allowedFrom, allowedTo, moves, ref moveCount);
                GetPotentialBlackPawnMoves(board, allowedFrom, allowedTo, moves, ref moveCount);
            }

            //moves.Sort((m1, m2) => m1.Key2.CompareTo(m2.Key2));
        }