Use a const pointer-to-member array for attacks
authorMarco Costalba <mcostalba@gmail.com>
Sat, 18 Oct 2008 06:54:18 +0000 (08:54 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 18 Oct 2008 19:35:34 +0000 (21:35 +0200)
Allow the compiler to optimize member
function access.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/main.cpp
src/movegen.cpp
src/position.cpp
src/position.h

index 9979e8514b8745c4dbcaa7e0e9ae94267e7ce564..e89fee0ed80e0bc2ea49a6789fd79689111b8a1e 100644 (file)
@@ -52,7 +52,6 @@ int main(int argc, char *argv[]) {
 
   // Initialization
 
-  init_piece_attacks_fn();
   init_mersenne();
   init_direction_table();
   init_bitboards();
index b6bf6b24b83f7d578d8757f96e47171f31af140a..2972eeeed9e75f5856362f4095819e40731507dc 100644 (file)
@@ -1004,9 +1004,10 @@ namespace {
 
   int generate_piece_moves(PieceType piece, const Position &pos, MoveStack *mlist, 
                            Color side, Bitboard target) {
+
+    const Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
     Square from, to;
     Bitboard b;
-    Piece_attacks_fn mem_fn = piece_attacks_fn[piece];
     int n = 0;
 
     for (int i = 0; i < pos.piece_count(side, piece); i++)
@@ -1095,5 +1096,4 @@ namespace {
 
     return n;
   }
-    
 }
index f16d498fd065aa38f9261fda7efb6fc32b127afa..6de3f08c89e69b6baf72cc502343e85cfc2e363e 100644 (file)
@@ -48,21 +48,18 @@ Key Position::zobSideToMove;
 Value Position::MgPieceSquareTable[16][64];
 Value Position::EgPieceSquareTable[16][64];
 
-Piece_attacks_fn piece_attacks_fn[7];
+const Piece_attacks_fn piece_attacks_fn[] =
+  { 0, 0, 
+    &Position::knight_attacks,
+    &Position::bishop_attacks,
+    &Position::rook_attacks,
+    &Position::queen_attacks,
+    &Position::king_attacks };
 
 ////
 //// Functions
 ////
 
-void init_piece_attacks_fn() {
-
-  piece_attacks_fn[KNIGHT] = &Position::knight_attacks;
-  piece_attacks_fn[BISHOP] = &Position::bishop_attacks;
-  piece_attacks_fn[ROOK]   = &Position::rook_attacks;
-  piece_attacks_fn[QUEEN]  = &Position::queen_attacks;
-  piece_attacks_fn[KING]   = &Position::king_attacks;
-}
-
 /// Constructors
 
 Position::Position(const Position &pos) {
index 1c422088d27ca0d3aba70c9ce6fd49a2a456abe4..5285c8f928afb0df26f1c50667e4131bad68eaa3 100644 (file)
@@ -372,8 +372,7 @@ private:
 
 /// An array of member functions to dispatch attacks_square
 typedef Bitboard (Position::* Piece_attacks_fn)(Square s) const;
-extern Piece_attacks_fn piece_attacks_fn[7];
-extern void init_piece_attacks_fn();
+extern const Piece_attacks_fn piece_attacks_fn[];
 
 ////
 //// Inline functions