Skip to content

Commit c2b4638

Browse files
authored
Merge pull request official-stockfish#1022 from IIvec/master
nm
2 parents 46cc1ad + bad18bc commit c2b4638

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

src/bitboard.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
#include <algorithm>
22+
#include <bitset>
2223

2324
#include "bitboard.h"
2425
#include "misc.h"
@@ -49,14 +50,6 @@ namespace {
4950
Bitboard BishopTable[0x1480]; // To store bishop attacks
5051

5152
void init_magics(Bitboard table[], Magic magics[], Direction directions[]);
52-
53-
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
54-
unsigned popcount16(unsigned u) {
55-
u -= (u >> 1) & 0x5555U;
56-
u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
57-
u = ((u >> 4) + u) & 0x0F0FU;
58-
return (u * 0x0101U) >> 8;
59-
}
6053
}
6154

6255

@@ -85,7 +78,7 @@ const std::string Bitboards::pretty(Bitboard b) {
8578
void Bitboards::init() {
8679

8780
for (unsigned i = 0; i < (1 << 16); ++i)
88-
PopCnt16[i] = (uint8_t)popcount16(i);
81+
PopCnt16[i] = std::bitset<16>(i).count();
8982

9083
for (Square s = SQ_A1; s <= SQ_H8; ++s)
9184
SquareBB[s] = (1ULL << s);

src/search.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ void Thread::search() {
299299
MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr);
300300
double timeReduction = 1.0;
301301
Color us = rootPos.side_to_move();
302-
bool failedLow;
303302

304303
std::memset(ss-7, 0, 10 * sizeof(Stack));
305304
for (int i = 7; i > 0; i--)
@@ -310,7 +309,7 @@ void Thread::search() {
310309
beta = VALUE_INFINITE;
311310

312311
if (mainThread)
313-
mainThread->bestMoveChanges = 0, failedLow = false;
312+
mainThread->bestMoveChanges = 0;
314313

315314
size_t multiPV = Options["MultiPV"];
316315
Skill skill(Options["Skill Level"]);
@@ -351,7 +350,7 @@ void Thread::search() {
351350

352351
// Age out PV variability metric
353352
if (mainThread)
354-
mainThread->bestMoveChanges *= 0.517, failedLow = false;
353+
mainThread->bestMoveChanges *= 0.517;
355354

356355
// Save the last iteration's scores before first PV line is searched and
357356
// all the move scores except the (new) PV are set to -VALUE_INFINITE.
@@ -431,7 +430,6 @@ void Thread::search() {
431430
if (mainThread)
432431
{
433432
failedHighCnt = 0;
434-
failedLow = true;
435433
mainThread->stopOnPonderhit = false;
436434
}
437435
}
@@ -483,19 +481,19 @@ void Thread::search() {
483481
&& !Threads.stop
484482
&& !mainThread->stopOnPonderhit)
485483
{
486-
double fallingEval = (306 + 119 * failedLow + 6 * (mainThread->previousScore - bestValue)) / 581.0;
484+
double fallingEval = (306 + 9 * (mainThread->previousScore - bestValue)) / 581.0;
487485
fallingEval = std::max(0.5, std::min(1.5, fallingEval));
488486

489487
// If the bestMove is stable over several iterations, reduce time accordingly
490488
timeReduction = lastBestMoveDepth + 10 * ONE_PLY < completedDepth ? 1.95 : 1.0;
489+
double reduction = std::pow(mainThread->previousTimeReduction, 0.528) / timeReduction;
491490

492491
// Use part of the gained time from a previous stable move for the current move
493492
double bestMoveInstability = 1.0 + mainThread->bestMoveChanges;
494-
bestMoveInstability *= std::pow(mainThread->previousTimeReduction, 0.528) / timeReduction;
495493

496494
// Stop the search if we have only one legal move, or if available time elapsed
497495
if ( rootMoves.size() == 1
498-
|| Time.elapsed() > Time.optimum() * bestMoveInstability * fallingEval)
496+
|| Time.elapsed() > Time.optimum() * fallingEval * reduction * bestMoveInstability)
499497
{
500498
// If we are allowed to ponder do not stop the search now but
501499
// keep pondering until the GUI sends "ponderhit" or "stop".
@@ -924,12 +922,13 @@ namespace {
924922
&& move == ttMove
925923
&& !rootNode
926924
&& !excludedMove // Avoid recursive singular search
927-
&& ttValue != VALUE_NONE
925+
/* && ttValue != VALUE_NONE Already implicit in the next condition */
926+
&& abs(ttValue) < VALUE_KNOWN_WIN
928927
&& (tte->bound() & BOUND_LOWER)
929928
&& tte->depth() >= depth - 3 * ONE_PLY
930929
&& pos.legal(move))
931930
{
932-
Value singularBeta = std::max(ttValue - 2 * depth / ONE_PLY, -VALUE_MATE);
931+
Value singularBeta = ttValue - 2 * depth / ONE_PLY;
933932
ss->excludedMove = move;
934933
value = search<NonPV>(pos, ss, singularBeta - 1, singularBeta, depth / 2, cutNode);
935934
ss->excludedMove = MOVE_NONE;

src/syzygy/tbprobe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "../movegen.h"
3333
#include "../position.h"
3434
#include "../search.h"
35-
#include "../thread_win32.h"
35+
#include "../thread_win32_osx.h"
3636
#include "../types.h"
3737
#include "../uci.h"
3838

src/thread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "pawns.h"
3333
#include "position.h"
3434
#include "search.h"
35-
#include "thread_win32.h"
35+
#include "thread_win32_osx.h"
3636

3737

3838
/// Thread class keeps together all the thread-related stuff. We use
@@ -46,7 +46,7 @@ class Thread {
4646
ConditionVariable cv;
4747
size_t idx;
4848
bool exit = false, searching = true; // Set before starting std::thread
49-
std::thread stdThread;
49+
NativeThread stdThread;
5050

5151
public:
5252
explicit Thread(size_t);
Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
2020

21-
#ifndef THREAD_WIN32_H_INCLUDED
22-
#define THREAD_WIN32_H_INCLUDED
21+
#ifndef THREAD_WIN32_OSX_H_INCLUDED
22+
#define THREAD_WIN32_OSX_H_INCLUDED
2323

2424
/// STL thread library used by mingw and gcc when cross compiling for Windows
2525
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
@@ -33,6 +33,7 @@
3333

3434
#include <condition_variable>
3535
#include <mutex>
36+
#include <thread>
3637

3738
#if defined(_WIN32) && !defined(_MSC_VER)
3839

@@ -67,4 +68,45 @@ typedef std::condition_variable ConditionVariable;
6768

6869
#endif
6970

70-
#endif // #ifndef THREAD_WIN32_H_INCLUDED
71+
/// On OSX threads other than the main thread are created with a reduced stack
72+
/// size of 512KB by default, this is dangerously low for deep searches, so
73+
/// adjust it to TH_STACK_SIZE. The implementation calls pthread_create() with
74+
/// proper stack size parameter.
75+
76+
#if defined(__APPLE__)
77+
78+
#include <pthread.h>
79+
80+
static const size_t TH_STACK_SIZE = 2 * 1024 * 1024;
81+
82+
template <class T, class P = std::pair<T*, void(T::*)()>>
83+
void* start_routine(void* ptr)
84+
{
85+
P* p = reinterpret_cast<P*>(ptr);
86+
(p->first->*(p->second))(); // Call member function pointer
87+
delete p;
88+
return NULL;
89+
}
90+
91+
class NativeThread {
92+
93+
pthread_t thread;
94+
95+
public:
96+
template<class T, class P = std::pair<T*, void(T::*)()>>
97+
explicit NativeThread(void(T::*fun)(), T* obj) {
98+
pthread_attr_t attr_storage, *attr = &attr_storage;
99+
pthread_attr_init(attr);
100+
pthread_attr_setstacksize(attr, TH_STACK_SIZE);
101+
pthread_create(&thread, attr, start_routine<T>, new P(obj, fun));
102+
}
103+
void join() { pthread_join(thread, NULL); }
104+
};
105+
106+
#else // Default case: use STL classes
107+
108+
typedef std::thread NativeThread;
109+
110+
#endif
111+
112+
#endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED

0 commit comments

Comments
 (0)