stockfish
9 years agoStreamline null search reduction formula
Marco Costalba [Thu, 5 Jul 2012 10:48:17 +0000 (11:48 +0100)]
Streamline null search reduction formula

Although a (little) functional change, we have no ELO change
but formula it is now more clear.

After 13019 games at 30"+0.05
Mod vs Orig 2075 - 2088 - 8856 ELO 0 (+- 3.4)

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoClear transposition table on "ucinewgame"
Marco Costalba [Sat, 30 Jun 2012 07:00:48 +0000 (08:00 +0100)]
Clear transposition table on "ucinewgame"

It seems the standard behaviour as implemented
in most engines although UCI protocol does not
specify what to do upon "ucinewgame" command.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoTempletize make_move() helpers
Marco Costalba [Mon, 25 Jun 2012 06:57:28 +0000 (07:57 +0100)]
Templetize make_move() helpers

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReduce indentation in UCIOption::operator=()
Marco Costalba [Sun, 24 Jun 2012 23:14:42 +0000 (00:14 +0100)]
Reduce indentation in UCIOption::operator=()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMicro-optimize move_to_san()
Marco Costalba [Sun, 24 Jun 2012 11:08:07 +0000 (12:08 +0100)]
Micro-optimize move_to_san()

Calculate the attacks only for the piece to disambiguate,
not for all.

Also some reformatting while there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse type_of() to categorize the moves
Marco Costalba [Sun, 24 Jun 2012 09:08:16 +0000 (10:08 +0100)]
Use type_of() to categorize the moves

Needed to rename old MoveType (used in move generation)
to GenType.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRename move.cpp to notation.cpp
Marco Costalba [Sun, 24 Jun 2012 08:46:08 +0000 (09:46 +0100)]
Rename move.cpp  to notation.cpp

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRename ThreadsManager to ThreadPool
Marco Costalba [Sun, 24 Jun 2012 08:30:40 +0000 (09:30 +0100)]
Rename ThreadsManager to ThreadPool

It is a more standard naming convention.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReplace make_square() with operator|(File, Rank)
Marco Costalba [Sat, 23 Jun 2012 07:49:33 +0000 (08:49 +0100)]
Replace make_square() with operator|(File, Rank)

Be fancy :-)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoCoding style in move.cpp
Marco Costalba [Sat, 23 Jun 2012 06:51:02 +0000 (07:51 +0100)]
Coding style in move.cpp

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoAdd min pawn-king distance to endgame evaluation
Marco Costalba [Sun, 17 Jun 2012 08:18:18 +0000 (09:18 +0100)]
Add min pawn-king distance to endgame evaluation

At endgame time push the king near his pawns (actually
one of them).

Original idea is from Critter (although slightly different),
implementation is mine and is completely different from the
original, in particular it is different the algorithm to
compute the minimum distance from pawns.

After 19895 games at 15"+0.05
Mod vs Orig 3638 - 3248 - 13009 ELO +7

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoCalculate min distance between king and his pawns
Marco Costalba [Sun, 17 Jun 2012 08:03:05 +0000 (09:03 +0100)]
Calculate min distance between king and his pawns

Just added infrastructure.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReformat pick_random() in magics calculation
Marco Costalba [Sun, 10 Jun 2012 10:11:28 +0000 (11:11 +0100)]
Reformat pick_random() in magics calculation

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix a compile error in opposite_colors()
Marco Costalba [Sat, 2 Jun 2012 07:51:58 +0000 (08:51 +0100)]
Fix a compile error in opposite_colors()

Error is due to ambiguous overloading of operator^
because we have both the built-in operator^(int, int)
and the user defined operator^(Bitboard, Square).

This error does not trigger when using Makefile becuase,
due to luck, the user defined operator^(Bitboard, Square)
happens to be always defined _after_ opposite_colors() so
that compiler does not claim. But in case of Microsoft MSVC
we don't have a Makefile and the order of files compilation
is chosen by the compiler (in an unpredictable way). So it
could still happen that error is not detected (as in my case),
but in another case the order of compilation of the files could
be so that at some point both operator^ were defined before
opposite_colors() and this triggers the error.

The fix is much simpler than the explanation :-)

Reported by Quocvuong82.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix an issue when adding a book during the game
Marco Costalba [Sun, 27 May 2012 13:13:14 +0000 (14:13 +0100)]
Fix an issue when adding a book during the game

Currently when we fail to open a book file, for instance
if it doesn't exsist, we leave Book::open() with ifstream
failbit set. If then the book file is added, we correctly
open it at next attempt, but failbit is still set so that
after opening we exit because ifstream::good() returns false.

The fix is to reset failbit upon exiting.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRetire PieceOffset[] in book.cpp
Marco Costalba [Sun, 27 May 2012 12:43:15 +0000 (13:43 +0100)]
Retire PieceOffset[] in book.cpp

And calculate piece offset on the fly. Also
improve comments.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix a possible 'Division by zero'
Marco Costalba [Sat, 26 May 2012 22:14:37 +0000 (23:14 +0100)]
Fix a possible 'Division by zero'

In case a book entry has 'count' field set to 0
we crash. Spotted by Clang's static analyzer.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRename psq_score in ReducedStateInfo
Marco Costalba [Sat, 26 May 2012 21:43:23 +0000 (22:43 +0100)]
Rename psq_score in ReducedStateInfo

So to be fully in sync with StateInfo, and move struct
to position.h, just below StateInfo.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix book file regression
Marco Costalba [Sat, 26 May 2012 09:11:59 +0000 (10:11 +0100)]
Fix book file regression

Revision 2aac860db3d04881103a3b0d0 of 27 / 4 / 2012
changed can_castle() signatures from bool to int and
this broke the code that calculates polyglot hash key
in book.cpp

Instead of directly fixing the code we prefer to change
castling rights definitions to align to the polyglot ones
(as we did in previous patch). After this step we can simply
take internal castle rights as they are and use them
directly to calculate polyglot book hash key, as we do
in this patch that fixes the regression.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRedefine enum CastleRight
Marco Costalba [Sat, 26 May 2012 08:51:16 +0000 (09:51 +0100)]
Redefine enum CastleRight

To be aligned with PolyGlot book castle right definitions.

This will be used by next patch.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoAdd a known draw case in kpk bitbase generation
Marco Costalba [Sat, 19 May 2012 07:38:23 +0000 (08:38 +0100)]
Add a known draw case in kpk bitbase generation

Early classify as known draws the positions
where white king is trapped on the rook file.

Suggested by Dan Honeycutt.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSimplify Position::print()
Marco Costalba [Sat, 12 May 2012 10:35:40 +0000 (11:35 +0100)]
Simplify Position::print()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRevert "Don't split if reduced below min_split_depth"
Marco Costalba [Sat, 12 May 2012 08:57:24 +0000 (09:57 +0100)]
Revert "Don't split if reduced below min_split_depth"

After extensive testing (I was off line this week and let
the test go on) it seems this change is useless:

After 33968 games on a QUAD (4 threads) at 15"+0.05
Mod vs Orig 5425 - 5550 - 22993 ELO -1 (+-2.1)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoImprove previous patch
Marco Costalba [Sat, 12 May 2012 08:21:52 +0000 (09:21 +0100)]
Improve previous patch

Only in case of promotion we care about an upper case
promotion piece char, so std::transform() is overkill
for the task.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoJunior promotion patch
Balint Pfliegel [Fri, 11 May 2012 23:01:33 +0000 (00:01 +0100)]
Junior promotion patch

Assumption: Junior sends promotions according to the side to move (ucase/lcase).
Fact: Stockfish generally handles promotion lcase.
Patch: Handling position fen input moves always with lcase promotions.

Ported back by Portfish. No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix compilation with Android NDK
Marco Costalba [Wed, 9 May 2012 07:33:25 +0000 (09:33 +0200)]
Fix compilation with Android NDK

It seems ADL lookup is broken with the STLPort library. Peter says:

The compiler is gcc 4.4.3, but I don't know how many patches they
have applied to it. I think gcc has had support for Koenig lookup
a long time. I think the problem is the type of the vector iterator.
For example, line 272 in search.cpp:

 if (bookMove && count(RootMoves.begin(), RootMoves.end(), bookMove))

gives the error:

jni/stockfish/search.cpp:272: error: 'count' was not declared in this scope

Here RootMoves is:

 std::vector<RootMove> RootMoves;

If std::vector<T>::iterator is implemented as T*, then Koenig lookup
would fail because RootMove* is not in namespace std.

I compile with the stlport implementation of STL, which in its vector
class has:

 typedef value_type* iterator;

I'm not sure if this is allowed by the C++ standard. I did not find
anything that says the iterator type must belong to namespace std.
The consensus in this thread

http://compgroups.net/comp.lang.c++.moderated/argument-dependent-lookup/433395

is that the stlport iterator type is allowed.

Report and patch by Peter Osterlund.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSimplify attacks_bb()
Marco Costalba [Tue, 1 May 2012 11:17:08 +0000 (12:17 +0100)]
Simplify attacks_bb()

And some formatting while there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoChange pos.pieces() argument order
Marco Costalba [Tue, 1 May 2012 11:01:38 +0000 (12:01 +0100)]
Change pos.pieces() argument order

Let first argument to be the 'color'. This allows to align
pos.pieces() signatures with piece_list(), piece_count() and
all the other functions where 'color' argument is passed as
first one.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoConvert constants to decimal representation
Marco Costalba [Tue, 1 May 2012 10:33:38 +0000 (11:33 +0100)]
Convert constants to decimal representation

Hex representation doesn't add any value in those cases.
Preserve hex representation where more self-documenting
for instance for binary masks values.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoDon't split if reduced depth is below min_split_depth
Marco Costalba [Sun, 22 Apr 2012 11:59:35 +0000 (12:59 +0100)]
Don't split if reduced depth is below min_split_depth

It seems to increase SMP performances. To note that
this patch goes in the opposite direction of "Active
reparenting" where we try to reparent an idle slave
as soon as possible. Instead here we prefer to keep
it idle instead of splitting on a shallow / near the
leaves node.

After 11550 games on a QUAD (4 threads) at 15"+0.05
Mod vs Orig 1972 - 1752 - 7826 ELO +6 (+-3.6)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoIncrease optimization level of Clang
Marco Costalba [Sun, 29 Apr 2012 22:11:23 +0000 (23:11 +0100)]
Increase optimization level of Clang

Set optimization level to 4 and get a 2.564% faster binary:

Stockfish (Clang, Level 4) bench:
$ make build ARCH=osx-x86-64 COMP=clang
(Clang does not support PGO)
Average of 4 trials:
Total time (ms): 5137.5
Nodes searched: 5631135
Nodes/second: 1096084.5

Stockfish (Clang, Level 3) bench:
$ make build ARCH=osx-x86-64 COMP=clang
(Clang does not support PGO)
Average of 4 trials:
Total time (ms): 5269.25
Nodes searched: 5631135
Nodes/second: 1068679.25

Stockfish (GCC, PGO) bench:
$ make profile-build ARCH=osx-x86-64
Average of 4 trials:
Total time (ms): 5286
Nodes searched: 5631135
Nodes/second: 1065292.25

Suggestion and performance tests by Daylen Yang.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRemove unreachable extension condition
Marco Costalba [Sun, 29 Apr 2012 17:58:48 +0000 (18:58 +0100)]
Remove unreachable extension condition

A PvNode that givesCheck has been already granted
an extension of ONE_PLY in previous condition.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoAdd support for Mac clang compiler
Andy Duplain [Sun, 29 Apr 2012 09:14:50 +0000 (10:14 +0100)]
Add support for Mac clang compiler

Makefile modified to support the clang compiler under Mac.
This was tested using clang 4 under Mountain Lion, but should
also work fine under Lion and possibly under Snow Leopard.

It requires the 'Xcode 4.x Command Line Tools' to be installed.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoProper indenting of multiple conditions
Marco Costalba [Sat, 28 Apr 2012 10:08:10 +0000 (11:08 +0100)]
Proper indenting of multiple conditions

Triviality due to a boring saturday morning.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRename square_empty() to is_empty()
Marco Costalba [Sat, 28 Apr 2012 09:49:52 +0000 (10:49 +0100)]
Rename square_empty() to is_empty()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMicro-optimize king evaluation
Marco Costalba [Sat, 28 Apr 2012 09:37:55 +0000 (10:37 +0100)]
Micro-optimize king evaluation

Reuse already calculated value, instead of calling
king_safety() again.

Patch suggested by Balint Pfliegel.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix wrong condition in PawnEntry::king_safety()
Marco Costalba [Fri, 27 Apr 2012 09:30:35 +0000 (10:30 +0100)]
Fix wrong condition in PawnEntry::king_safety()

Since revision 374c9e6b63d0e233371
we use also castling information to calculate king safety.
So before to reuse the cached king safety score we have to
veify not only king position, but also castling rights are
the same of the pre-calculated ones.

This is a very subtle bug, found only becuase even after
previous patch, consecutives runs of 'bench' _still_ showed
different numbers. Pawn tables are not cleared between 'bench'
runs and in the second run a king safety score, previously
evaluated under some castling rights, was reused by another
position with different castling rights instead of being
recalculated from scratch.

Bug spotted and tracked down by Balint Pfliegel

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoClear TT before running 'bench'
Marco Costalba [Thu, 26 Apr 2012 18:01:13 +0000 (19:01 +0100)]
Clear TT before running 'bench'

Now that we can call bench multiple times
from command prompt we need to ensure searched
nodes remain constant across different runs.

Spotted by Bálint Pfliegel.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRetire "Active reparenting"
Marco Costalba [Sun, 22 Apr 2012 16:52:31 +0000 (17:52 +0100)]
Retire "Active reparenting"

After 6K games at 60" + 0.1 on QUAD with 4 threads
this implementation fails to show a measurable increase,
result is well within error bar.

Perhaps with 8 or more threads resut is better but we
don't have the hardware to test. So retire for now and
in case re-add in the future if it proves good on big
machines.

The only good news is that we don't have a regression and
implementation is stable and bug-free, so could be reused
somewhere in the future.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix endless reaparenting loop
Marco Costalba [Tue, 17 Apr 2012 07:11:34 +0000 (09:11 +0200)]
Fix endless reaparenting loop

The check for detecting when a split point has all the
slaves still running is done with:

   slavesMask == allSlavesMask

When a thread reparents, slavesMask is increased, then, if
the same thread finishes, because there are no more moves,
slavesMask returns to original value and the above condition
returns to be true. So that the just finished thread immediately
reparents again with the same split point, then starts and
then immediately exits in a tight loop that ends only when a
second slave finishes, so that slavesMask decrements and the
condition becomes false. This gives a spurious and anomaly
high number of faked reparents.

With this patch, that rewrites the logic to avoid this pitfall,
the reparenting success rate drops to a more realistical 5-10%
for 4 threads case.

As a side effect note that now there is no more the limit of
maxThreadsPerSplitPoint when reparenting.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoImproved cutoff check when reparenting
Marco Costalba [Mon, 16 Apr 2012 12:19:22 +0000 (14:19 +0200)]
Improved cutoff check when reparenting

Check for a cutoff occurred also high in
the tree and not only at current split
point.

This avoids some more wasted reparenting.

No functional chnage.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse more_than_one() instead of single_bit()
Marco Costalba [Sat, 14 Apr 2012 08:16:34 +0000 (09:16 +0100)]
Use more_than_one() instead of single_bit()

It is more correct given what the function does. In
particular single_bit() returns true also in case of
empty bitboards.

Of course also the usual renaming while there :-)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReparent to latest
Marco Costalba [Fri, 13 Apr 2012 05:32:55 +0000 (06:32 +0100)]
Reparent to latest

Instead of reparenting to oldest split point, try to reparent
to latest. The nice thing here is that we can use the YBWC
helpful master condition to allow the reparenting of a split
point master as long as is reparented to one of its slaves.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoDon't reparent if a cutoff is pending
Marco Costalba [Tue, 10 Apr 2012 18:48:57 +0000 (19:48 +0100)]
Don't reparent if a cutoff is pending

And update master->splitPointsCnt under lock
protection. Not stricly necessary because
single_bit() condition takes care of false
positives anyhow, but it is a bit tricky and
moving under lock is the most natural thing
to do to avoid races with "reparenting".

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoActive Reparenting
Marco Costalba [Tue, 10 Apr 2012 17:00:31 +0000 (18:00 +0100)]
Active Reparenting

In Young Brothers Wait Concept (YBWC) available slaves are
booked by the split point master, then start to search below
the assigned split point and, once finished, return in idle
state waiting to be booked by another master.

This patch introduces "Active Reparenting" so that when a
slave finishes its job on the assigned split point, instead
of passively waiting to be booked, searches a suitable active
split point and reprents itselfs to that split point. Then
immediately starts to search below the split point in exactly
the same way of the others split point's slaves. This reduces
to zero the time waiting in idle loop and should increase
scalability especially whit many (8 or more) cores.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSync compute_xxx implementations
Marco Costalba [Mon, 9 Apr 2012 10:35:47 +0000 (11:35 +0100)]
Sync compute_xxx implementations

Also refactored Position::pos_is_ok() while there.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMove Tempo to evaluation
Marco Costalba [Mon, 9 Apr 2012 09:34:17 +0000 (10:34 +0100)]
Move Tempo to evaluation

Apart from the semplification it is now more clear that
the actual Tempo added was half of the indicated score.
This is becuase at start compute_psq_score() added half
Tempo unit and in do_move() white/black were respectively
adding/subtracting one Tempo unit.

Now we have directly halved Tempo constant and everything
is more clear.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMicro-optimize do_castle_move()
Marco Costalba [Mon, 9 Apr 2012 08:23:52 +0000 (09:23 +0100)]
Micro-optimize do_castle_move()

Use the same tables update trick used in do_move().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoDisable book during analysis
Marco Costalba [Mon, 9 Apr 2012 07:09:02 +0000 (08:09 +0100)]
Disable book during analysis

It is still enabled during fixed limit search so to
use it during fixed depth/nodes/time matches.

Bug reported by Daylen.

No functional changes.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoIntroduce make_castle_right() helper
Marco Costalba [Sun, 8 Apr 2012 16:07:56 +0000 (17:07 +0100)]
Introduce make_castle_right() helper

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoShrink castlePath[] and castleRookSquare[] sizes
Marco Costalba [Sun, 8 Apr 2012 10:32:01 +0000 (11:32 +0100)]
Shrink castlePath[] and castleRookSquare[] sizes

Shrinking from [16] to [2][2] is able to speedup
perft of start position of almost 5% !

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReduce stack usage
Marco Costalba [Sun, 8 Apr 2012 07:16:37 +0000 (08:16 +0100)]
Reduce stack usage

Shrink dimensions of the biggest stack consumers arrays.
In particular movesSearched[] can be safely shrinked
without any impact on strenght or risk of crashing.
Also MAX_PLY can be reverted to 100 with almost no impact
so to limit search recursion and hence stack allocation.

A different case is for MAX_MOVES (used by Movepicker's
moves[]), because we know that do exsist some artificial
position with about 220 legal moves, so in those cases SF
will crash. Anyhow these cases are never found in games.
An open risk remains perft, especially run above handcrafted
positions.

This patch originates from a report by Daylen that found
SF crashing on his Mac OS X 10.7.3 while in deep analysys
on the following position:

8/3Q1pk1/5p2/4r3/5K2/8/8/8 w - - 0 1

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix 'bench' for Chess960 case
Marco Costalba [Sat, 7 Apr 2012 13:06:42 +0000 (14:06 +0100)]
Fix 'bench' for Chess960 case

Now a fen file with Chess960 positions is
correctly parsed. But it is mandatory to set
"UCI_Chess960" option _before_ to call bench.

Note that this was not needed/possible before
adding the possibility to call 'bench' from
command prompt.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse bench to implement UI 'perft' command
Marco Costalba [Sat, 7 Apr 2012 12:23:43 +0000 (13:23 +0100)]
Use bench to implement UI 'perft' command

Now that we can call bench on current position
we can directly use it to perform our perft.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoTeach 'bench' to run current position
Marco Costalba [Sat, 7 Apr 2012 11:04:03 +0000 (12:04 +0100)]
Teach 'bench' to run current position

Now that we can call bench from command prompt
has a sense to teach bench to run the current
set position. To do this is enough to call bench
with 'current' as fen source parameter.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoDon't need to wait after a "ponderhit"
Marco Costalba [Sat, 7 Apr 2012 10:47:37 +0000 (11:47 +0100)]
Don't need to wait after a "ponderhit"

It is enough to wake up main thread. This is
a better fix than d033d5e06a604f.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRevert "Penalty for undefended rook"
Marco Costalba [Sat, 7 Apr 2012 10:34:22 +0000 (11:34 +0100)]
Revert "Penalty for undefended rook"

After extensive test Gary says:

"So, after 16k games at 10"+1" on an i7, the undefended rook test
looks to be not good (albeit by a very small margin).
3063 - 3093 - 9844 (-1).

I doubt that is causing the regression, but even so, it looks like
it's not worth keeping, and we can go back to the simpler undefended
minors check."

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReplace Position::copy()
Marco Costalba [Fri, 6 Apr 2012 18:13:48 +0000 (19:13 +0100)]
Replace Position::copy()

With assignment operator. And fix Position::flip().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRevert thread_local stuff
Marco Costalba [Fri, 6 Apr 2012 17:36:46 +0000 (18:36 +0100)]
Revert thread_local stuff

Unfortunatly accessing thread local variable
is much slower than object data (see previous
patch log msg), so we have to revert to old code
to avoid speed regression.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse thread_local compiler specifics
Marco Costalba [Fri, 6 Apr 2012 16:01:41 +0000 (17:01 +0100)]
Use thread_local compiler specifics

Much faster then pthread_getspecific() but still a
speed regression against the original code.

Following are the nps on a bench:

Position
454165
454838
455433

tls
441046
442767
442767

ms (Win)
450521
447510
451105

ms (pthread)
422115
422115
424276

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix a (theoretical) race leading to a crash
Marco Costalba [Fri, 6 Apr 2012 14:22:02 +0000 (15:22 +0100)]
Fix a (theoretical) race leading to a crash

After we release the SplitPoint lock the master, suppose
is main thread, can safely return and if a "quit" command
is pending, main thread exits and associated Thread object
is freed. So when we access master->is_searching a crash
occurs.

I have never found such a race that is of course very rare
becuase assumes that from lock releasing we go to sleep for
a time long enough for the main thread to end the search and
return. But you can never know, and anyhow a race is a race.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoTeach UI thread to use main thread resources
Marco Costalba [Fri, 6 Apr 2012 13:17:08 +0000 (14:17 +0100)]
Teach UI thread to use main thread resources

So to avoid a crash when setting the moves in
UCI "position startpos moves ...." command.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoDon't store Thread info in Position
Marco Costalba [Fri, 6 Apr 2012 12:46:53 +0000 (13:46 +0100)]
Don't store Thread info in Position

But use the newly introduced local storage
for this. A good code semplification and also
the correct way to go.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoIntroduce thread local storage
Marco Costalba [Fri, 6 Apr 2012 10:39:07 +0000 (12:39 +0200)]
Introduce thread local storage

Use thread local storage to store a pointer to the thread we
are running on. This will allow to remove thread info from
Position class.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRewrite pop_1st_bit() to be endian independent
Marco Costalba [Fri, 6 Apr 2012 09:37:07 +0000 (11:37 +0200)]
Rewrite pop_1st_bit() to be endian independent

With this change sources are fully endianess
independent, so we can simplify the Makefile.

Somewhat surprisingly we don't have any speed
regression !

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse a Thread instead of an array index
Marco Costalba [Wed, 4 Apr 2012 06:54:02 +0000 (07:54 +0100)]
Use a Thread instead of an array index

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoBig Position renaming
Marco Costalba [Mon, 2 Apr 2012 17:21:17 +0000 (18:21 +0100)]
Big Position renaming

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMicro-optimize last_1() for 32bits
Marco Costalba [Wed, 4 Apr 2012 05:52:07 +0000 (06:52 +0100)]
Micro-optimize last_1() for 32bits

Verified assembly it is a bit simpler.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix bench with fen files regression
Marco Costalba [Mon, 2 Apr 2012 17:38:50 +0000 (18:38 +0100)]
Fix bench with fen files regression

Erroneusly adds default positions to the fens
loaded from external file.

Bug introduced in adb71b8096436a

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSimplify Endgames::probe()
Marco Costalba [Mon, 2 Apr 2012 06:41:26 +0000 (07:41 +0100)]
Simplify Endgames::probe()

With this API change we simplify both function and caller site.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse polymorphism to resolve map() overloading
Marco Costalba [Sun, 1 Apr 2012 14:12:39 +0000 (15:12 +0100)]
Use polymorphism to resolve map() overloading

The 2 overload functions map() accept a pointer to
EndgameBase<Value> or a pointer to EndgameBase<ScaleFactor>.

Because Endgame<E> is derived from one of them we can
directly use a pointer to this class to resolve the
overload as is needed in Endgames::add().

Also made class Endgames fully parametrized and no more
hardcoded to the types (Value or ScaleFactor) of endgames
stored.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse a std::vector to store searchMoves
Marco Costalba [Sun, 1 Apr 2012 11:26:14 +0000 (12:26 +0100)]
Use a std::vector to store searchMoves

A std::set (that is a rb_tree) seems really
overkill to store at most a handful of moves
and nothing in the common case.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRetire platform specifics include in misc.cpp
Marco Costalba [Sun, 1 Apr 2012 11:00:15 +0000 (12:00 +0100)]
Retire platform specifics include in misc.cpp

Now that platform.h is included in types.h we
don't need this stuff anymore.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoBetter document square flipping helpers
Marco Costalba [Sun, 1 Apr 2012 10:30:54 +0000 (11:30 +0100)]
Better document square flipping helpers

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoIntroduce Bitboards namespace
Marco Costalba [Sun, 1 Apr 2012 09:46:38 +0000 (10:46 +0100)]
Introduce Bitboards namespace

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoProcess 'bench' also from SF prompt
Marco Costalba [Sun, 1 Apr 2012 08:25:07 +0000 (09:25 +0100)]
Process 'bench' also from SF prompt

It is possible to start with 'stockfish', then from
command prompt type 'bench' and SF will do what you expect.
Old behaviour is anyhow preserved. As a bonus we can now
start from command line any UCI command understood by
Stockfish. The difference is that after execution of a
command from arguments SF quits, while at the end of the
same command from prompt SF stays in UCI loop.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse std::vector to implement HashTable
Marco Costalba [Sat, 31 Mar 2012 11:15:57 +0000 (12:15 +0100)]
Use std::vector to implement HashTable

Allows some code semplification and avoids directly
allocation and managing heap memory.

Also the usual renaming while there.

No functional change and no speed regression.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRename Materials and Pawns hash stuff
Marco Costalba [Sat, 31 Mar 2012 08:43:16 +0000 (09:43 +0100)]
Rename Materials and Pawns hash stuff

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoComplete the renaming in Search::LimitsType
Marco Costalba [Sat, 31 Mar 2012 08:16:09 +0000 (09:16 +0100)]
Complete the renaming in Search::LimitsType

This completes the job started with revision
4124c94583c8f61.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReset search time as early as possible
Marco Costalba [Sat, 31 Mar 2012 09:01:31 +0000 (10:01 +0100)]
Reset search time as early as possible

In particualr before to wake up main thread that
could take some random time. Until we don't reset
search time we are not able to correctly track
the elapsed search time and this can be dangerous
under extreme time pressure.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRevert "Call wait_for_search_finished() only when quitting"
Marco Costalba [Fri, 30 Mar 2012 10:44:31 +0000 (12:44 +0200)]
Revert "Call wait_for_search_finished() only when quitting"

We need to wake up main thread if it is sleeping
waiting for stop or ponderhit, so we cannot skip
calling wait_for_search_finished().

Found by Othello1984.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoLast touches to pawns shelter code
Marco Costalba [Thu, 29 Mar 2012 05:04:44 +0000 (06:04 +0100)]
Last touches to pawns shelter code

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix an hang when max depth is reached
Marco Costalba [Thu, 29 Mar 2012 19:04:27 +0000 (20:04 +0100)]
Fix an hang when max depth is reached

In this case SF stop searching and goes sleeping
waiting for a stop / ponderhit before to return
best move. So when a "stop" arrives we need to wake
up the main thread again.

Another regression introduced by 3aa471f2a9cb,
hopefully the last one.

Thanks to Otello1984 to reporting this.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRefactor pawns shelter and storm
Marco Costalba [Wed, 28 Mar 2012 10:44:41 +0000 (12:44 +0200)]
Refactor pawns shelter and storm

Renamed stuff and added comments. The aim is to make more
readable, at least by me ;-) , this newly added part of code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoRestore MS1BTable[]
Marco Costalba [Wed, 28 Mar 2012 18:01:37 +0000 (19:01 +0100)]
Restore MS1BTable[]

Incredible typo from my side!

The 2 tables are completely different, one counts 1s the
other returns the msb position. Even more incredible
the 'stockfish bench' command returns the same number
of nodes!!!

Spotted by Justin Blanchard.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSilently handle "ucinewgame" command
Marco Costalba [Wed, 28 Mar 2012 17:22:40 +0000 (18:22 +0100)]
Silently handle "ucinewgame" command

Avoid returning "Unknown command", it seems some
GUI are misguided by this.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReplace MS1BTable[] with BitCount8Bit[]
Marco Costalba [Wed, 28 Mar 2012 09:03:06 +0000 (11:03 +0200)]
Replace MS1BTable[] with BitCount8Bit[]

We already have the necessary infrastructure
in place.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMerge pull request #11 from glinscott/squash
Marco Costalba [Wed, 28 Mar 2012 05:40:17 +0000 (22:40 -0700)]
Merge pull request #11 from glinscott/squash

Add more detailed pawn shelter/storm evaluation

After 10670 games at 10"+0.05
Mod vs Orig 2277 - 1941 - 6452 ELO +11 !!!

The first real increase since 2.2.2, congratulations Gary !!!

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSet do_sleep out of lock protection
Marco Costalba [Tue, 27 Mar 2012 17:47:41 +0000 (18:47 +0100)]
Set do_sleep out of lock protection

Fixes a not so rare crash (once every 100 games)
newly introduced. Unfortunatly I am still not
able to figure out why :-(

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoUse UCI names in Search::LimitsType
Marco Costalba [Tue, 27 Mar 2012 10:27:57 +0000 (12:27 +0200)]
Use UCI names in Search::LimitsType

There is no need to "invent" different names
from the original UCI parameters.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMerge pull request #9 from glinscott/master
Marco Costalba [Mon, 26 Mar 2012 18:55:32 +0000 (11:55 -0700)]
Merge pull request #9 from glinscott/master

Penalty for undefended rook

Almost no change at longer TC, but perhaps there
is a tiny increase....

After 17522 games at 10"+0.05
Mod vs Orig 3064 - 2967 - 11491 ELO +2

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoSimplify start_searching() signature
Marco Costalba [Mon, 26 Mar 2012 17:56:48 +0000 (18:56 +0100)]
Simplify start_searching() signature

Retire the "sync" behaviour that now is up to
the caller to honour.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoCall wait_for_search_finished() only when quitting
Marco Costalba [Mon, 26 Mar 2012 17:35:23 +0000 (18:35 +0100)]
Call wait_for_search_finished() only when quitting

When quitting we should avoid RootPosition to be
destroyed while threads are still running, leading
to a crash. In case of a "stop" or "ponderhit"
command there is no need for the UI thread to wait.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoIntroduce and use wait_for_search_finished()
Marco Costalba [Mon, 26 Mar 2012 15:19:37 +0000 (17:19 +0200)]
Introduce and use wait_for_search_finished()

Helper function that allows us to simplify
the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoAdd more detailed pawn shelter/storm evaluation
Gary Linscott [Mon, 26 Mar 2012 11:52:10 +0000 (07:52 -0400)]
Add more detailed pawn shelter/storm evaluation

9 years agoMove ThreadsManager::exit() to d'tor
Marco Costalba [Mon, 26 Mar 2012 06:58:10 +0000 (07:58 +0100)]
Move ThreadsManager::exit() to d'tor

And add final touches to this long patch series.

All the series has been verified against regression with
20K games at fast TC.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix compile error with gcc
Marco Costalba [Sun, 25 Mar 2012 22:08:35 +0000 (23:08 +0100)]
Fix compile error with gcc

We have a clash with start_fn defined both as a
Thread memeber and as a function pointer type in
pthread_create().

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoReset Thread::maxPly before a new search
Marco Costalba [Sun, 25 Mar 2012 15:44:55 +0000 (16:44 +0100)]
Reset Thread::maxPly before a new search

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoFix race in ThreadsManager::sleep()
Marco Costalba [Sun, 25 Mar 2012 15:31:50 +0000 (16:31 +0100)]
Fix race in ThreadsManager::sleep()

We cannot set do_sleep flag of main thread before
"bestmove" is sent to GUI, otherwise GUI could send
immediately the next "go" command that triggers
start_thinking() and because do_sleep is set UI
thread resets the flag to launch a new search. But
when shortly after main thread returns to main_loop()
flag is incorrectly reset and main thread goes to sleep
hanging the engine.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
9 years agoMerge remote-tracking branch 'upstream/master'
Gary Linscott [Sun, 25 Mar 2012 14:18:29 +0000 (10:18 -0400)]
Merge remote-tracking branch 'upstream/master'