static void bit_works_bitscanLS1B_64_test (void) { g_assert( 0 == bit_works_bitscanLS1B_64(0x0000000000000001)); g_assert( 4 == bit_works_bitscanLS1B_64(0x0000000000000010)); g_assert(63 == bit_works_bitscanLS1B_64(0x8000000000000000)); g_assert( 0 == bit_works_bitscanLS1B_64(0xFFFFFFFFFFFFFFFF)); g_assert( 0 == bit_works_bitscanLS1B_64(0x0000000000000003)); }
/** * @brief Computes the legal move list structure given the set. * * @param [in] legal_move_set the set of legal moves * @param [out] legal_move_list the compuetd list of legal moves */ inline static void legal_move_list_from_set (const SquareSet legal_move_set, LegalMoveList *legal_move_list) { legal_move_list->move_count = 0; legal_move_list->square_set = legal_move_set; SquareSet remaining_moves = legal_move_set; while (remaining_moves) { const Square move = bit_works_bitscanLS1B_64(remaining_moves); legal_move_list->squares[legal_move_list->move_count] = move; legal_move_list->move_count++; remaining_moves ^= 1ULL << move; } return; }
/* * Sorts moves in ascending order of mobility. */ static void sort_moves_by_mobility_count (MoveList *move_list, const GamePosition *const gp) { MoveListElement *curr = NULL; int move_index = 0; const SquareSet moves = game_position_legal_moves(gp); SquareSet moves_to_search = moves; for (int i = 0; i < legal_moves_priority_cluster_count; i++) { moves_to_search = legal_moves_priority_mask[i] & moves; while (moves_to_search) { curr = &move_list->elements[move_index]; const Square move = bit_works_bitscanLS1B_64(moves_to_search); moves_to_search &= ~(1ULL << move); GamePosition *next_gp = game_position_make_move(gp, move); const SquareSet next_moves = game_position_legal_moves(next_gp); game_position_free(next_gp); const int next_move_count = bit_works_bitcount_64(next_moves); curr->sq = move; curr->mobility = next_move_count; for (MoveListElement *element = move_list->head.succ; element != NULL; element = element->succ) { if (curr->mobility < element->mobility) { /* Insert current before element. */ MoveListElement *left = element->pred; MoveListElement *right = element; curr->pred = left; curr->succ = right; left->succ = curr; right->pred = curr; goto out; } } out: move_index++; } } return; }