Skip to content

Commit

Permalink
Replace pack_col() with transpose()
Browse files Browse the repository at this point in the history
Profiling indicated that a lot of time was spend in the pack_col() function.
This function is always called 4 times (for all 4 columns). It turns out to be
cheaper to only once transpose the board (turn columns into rows) and then
extract 4 rows.

Conflicts:
	2048.cpp
  • Loading branch information
m9710797 authored and nneonneo committed Apr 13, 2014
1 parent bbc9525 commit 12fd6da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
48 changes: 30 additions & 18 deletions 2048.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@
typedef std::map<board_t, float> trans_table_t;
#endif

// Transpose rows/columns in a board:
// 0123 048c
// 4567 --> 159d
// 89ab 26ae
// cdef 37bf
static inline board_t transpose(board_t x)
{
board_t a1 = x & 0xF0F00F0FF0F00F0FULL;
board_t a2 = x & 0x0000F0F00000F0F0ULL;
board_t a3 = x & 0x0F0F00000F0F0000ULL;
board_t a = a1 | (a2 << 12) | (a3 >> 12);
board_t b1 = a & 0xFF00FF0000FF00FFULL;
board_t b2 = a & 0x00FF00FF00000000ULL;
board_t b3 = a & 0x00000000FF00FF00ULL;
return b1 | (b2 >> 24) | (b3 << 24);
}

/* We can perform state lookups one row at a time by using arrays with 65536 entries. */

/* Move tables. Each row or compressed column is mapped to (oldrow^newrow) assuming row/col 0.
Expand Down Expand Up @@ -74,19 +91,21 @@ void init_move_tables(void) {

static inline board_t execute_move_0(board_t board) {
board_t ret = board;
ret ^= col_up_table[pack_col((board >> 0) & COL_MASK)] << 0;
ret ^= col_up_table[pack_col((board >> 4) & COL_MASK)] << 4;
ret ^= col_up_table[pack_col((board >> 8) & COL_MASK)] << 8;
ret ^= col_up_table[pack_col((board >> 12) & COL_MASK)] << 12;
board_t t = transpose(board);
ret ^= col_up_table[(t >> 0) & ROW_MASK] << 0;
ret ^= col_up_table[(t >> 16) & ROW_MASK] << 4;
ret ^= col_up_table[(t >> 32) & ROW_MASK] << 8;
ret ^= col_up_table[(t >> 48) & ROW_MASK] << 12;
return ret;
}

static inline board_t execute_move_1(board_t board) {
board_t ret = board;
ret ^= col_down_table[pack_col((board >> 0) & COL_MASK)] << 0;
ret ^= col_down_table[pack_col((board >> 4) & COL_MASK)] << 4;
ret ^= col_down_table[pack_col((board >> 8) & COL_MASK)] << 8;
ret ^= col_down_table[pack_col((board >> 12) & COL_MASK)] << 12;
board_t t = transpose(board);
ret ^= col_down_table[(t >> 0) & ROW_MASK] << 0;
ret ^= col_down_table[(t >> 16) & ROW_MASK] << 4;
ret ^= col_down_table[(t >> 32) & ROW_MASK] << 8;
ret ^= col_down_table[(t >> 48) & ROW_MASK] << 12;
return ret;
}

Expand Down Expand Up @@ -219,17 +238,10 @@ static float score_helper(board_t board, const float* table) {
table[(board >> 48) & ROW_MASK];
}

static float score_col_helper(board_t board, const float* table) {
return table[pack_col((board >> 0) & COL_MASK)] +
table[pack_col((board >> 4) & COL_MASK)] +
table[pack_col((board >> 8) & COL_MASK)] +
table[pack_col((board >> 12) & COL_MASK)];
}

static float score_heur_board(board_t board) {
return score_helper (board, line_heur_score_table) +
score_col_helper(board, line_heur_score_table) +
100000;
return score_helper( board , line_heur_score_table) +
score_helper(transpose(board), line_heur_score_table) +
100000.0f;
}

static float score_board(board_t board) {
Expand Down
4 changes: 0 additions & 4 deletions 2048.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ static inline void print_board(board_t board) {
printf("\n");
}

static inline row_t pack_col(board_t col) {
return (row_t)(col | (col >> 12) | (col >> 24) | (col >> 36));
}

static inline board_t unpack_col(row_t row) {
board_t tmp = row;
return (tmp | (tmp << 12ULL) | (tmp << 24ULL) | (tmp << 36ULL)) & COL_MASK;
Expand Down

0 comments on commit 12fd6da

Please sign in to comment.