]> git.sesse.net Git - stockfish/blob - src/ucioption.cpp
Allow for VNNI256 compilation with g++-8
[stockfish] / src / ucioption.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <algorithm>
20 #include <cassert>
21 #include <ostream>
22 #include <sstream>
23
24 #include "misc.h"
25 #include "search.h"
26 #include "thread.h"
27 #include "tt.h"
28 #include "uci.h"
29 #include "syzygy/tbprobe.h"
30
31 using std::string;
32
33 UCI::OptionsMap Options; // Global object
34
35 namespace UCI {
36
37 /// 'On change' actions, triggered by an option's value change
38 void on_clear_hash(const Option&) { Search::clear(); }
39 void on_hash_size(const Option& o) { TT.resize(size_t(o)); }
40 void on_logger(const Option& o) { start_logger(o); }
41 void on_threads(const Option& o) { Threads.set(size_t(o)); }
42 void on_tb_path(const Option& o) { Tablebases::init(o); }
43 void on_use_NNUE(const Option& ) { Eval::init_NNUE(); }
44 void on_eval_file(const Option& ) { Eval::init_NNUE(); }
45
46 /// Our case insensitive less() function as required by UCI protocol
47 bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
48
49   return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
50          [](char c1, char c2) { return tolower(c1) < tolower(c2); });
51 }
52
53
54 /// UCI::init() initializes the UCI options to their hard-coded default values
55
56 void init(OptionsMap& o) {
57
58   constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
59
60   o["Debug Log File"]        << Option("", on_logger);
61   o["Contempt"]              << Option(24, -100, 100);
62   o["Analysis Contempt"]     << Option("Both var Off var White var Black var Both", "Both");
63   o["Threads"]               << Option(1, 1, 512, on_threads);
64   o["Hash"]                  << Option(16, 1, MaxHashMB, on_hash_size);
65   o["Clear Hash"]            << Option(on_clear_hash);
66   o["Ponder"]                << Option(false);
67   o["MultiPV"]               << Option(1, 1, 500);
68   o["Skill Level"]           << Option(20, 0, 20);
69   o["Move Overhead"]         << Option(10, 0, 5000);
70   o["Slow Mover"]            << Option(100, 10, 1000);
71   o["nodestime"]             << Option(0, 0, 10000);
72   o["UCI_Chess960"]          << Option(false);
73   o["UCI_AnalyseMode"]       << Option(false);
74   o["UCI_LimitStrength"]     << Option(false);
75   o["UCI_Elo"]               << Option(1350, 1350, 2850);
76   o["UCI_ShowWDL"]           << Option(false);
77   o["SyzygyPath"]            << Option("<empty>", on_tb_path);
78   o["SyzygyProbeDepth"]      << Option(1, 1, 100);
79   o["Syzygy50MoveRule"]      << Option(true);
80   o["SyzygyProbeLimit"]      << Option(7, 0, 7);
81   o["Use NNUE"]              << Option(true, on_use_NNUE);
82   // The default must follow the format nn-[SHA256 first 12 digits].nnue
83   // for the build process (profile-build and fishtest) to work.
84   o["EvalFile"]              << Option("nn-82215d0fd0df.nnue", on_eval_file);
85 }
86
87
88 /// operator<<() is used to print all the options default values in chronological
89 /// insertion order (the idx field) and in the format defined by the UCI protocol.
90
91 std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
92
93   for (size_t idx = 0; idx < om.size(); ++idx)
94       for (const auto& it : om)
95           if (it.second.idx == idx)
96           {
97               const Option& o = it.second;
98               os << "\noption name " << it.first << " type " << o.type;
99
100               if (o.type == "string" || o.type == "check" || o.type == "combo")
101                   os << " default " << o.defaultValue;
102
103               if (o.type == "spin")
104                   os << " default " << int(stof(o.defaultValue))
105                      << " min "     << o.min
106                      << " max "     << o.max;
107
108               break;
109           }
110
111   return os;
112 }
113
114
115 /// Option class constructors and conversion operators
116
117 Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
118 { defaultValue = currentValue = v; }
119
120 Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
121 { defaultValue = currentValue = (v ? "true" : "false"); }
122
123 Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
124 {}
125
126 Option::Option(double v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
127 { defaultValue = currentValue = std::to_string(v); }
128
129 Option::Option(const char* v, const char* cur, OnChange f) : type("combo"), min(0), max(0), on_change(f)
130 { defaultValue = v; currentValue = cur; }
131
132 Option::operator double() const {
133   assert(type == "check" || type == "spin");
134   return (type == "spin" ? stof(currentValue) : currentValue == "true");
135 }
136
137 Option::operator std::string() const {
138   assert(type == "string");
139   return currentValue;
140 }
141
142 bool Option::operator==(const char* s) const {
143   assert(type == "combo");
144   return   !CaseInsensitiveLess()(currentValue, s)
145         && !CaseInsensitiveLess()(s, currentValue);
146 }
147
148
149 /// operator<<() inits options and assigns idx in the correct printing order
150
151 void Option::operator<<(const Option& o) {
152
153   static size_t insert_order = 0;
154
155   *this = o;
156   idx = insert_order++;
157 }
158
159
160 /// operator=() updates currentValue and triggers on_change() action. It's up to
161 /// the GUI to check for option's limits, but we could receive the new value
162 /// from the user by console window, so let's check the bounds anyway.
163
164 Option& Option::operator=(const string& v) {
165
166   assert(!type.empty());
167
168   if (   (type != "button" && v.empty())
169       || (type == "check" && v != "true" && v != "false")
170       || (type == "spin" && (stof(v) < min || stof(v) > max)))
171       return *this;
172
173   if (type == "combo")
174   {
175       OptionsMap comboMap; // To have case insensitive compare
176       string token;
177       std::istringstream ss(defaultValue);
178       while (ss >> token)
179           comboMap[token] << Option();
180       if (!comboMap.count(v) || v == "var")
181           return *this;
182   }
183
184   if (type != "button")
185       currentValue = v;
186
187   if (on_change)
188       on_change(*this);
189
190   return *this;
191 }
192
193 } // namespace UCI