@@ -37,22 +37,38 @@ public void Update(Piece oldPiece, Piece newPiece, int index)
3737
3838 private void AddPiece ( Piece piece , int squareIndex )
3939 {
40- short color = ( short ) piece . Color ( ) ;
41- int pieceIndex = PieceTableIndex ( piece ) ;
42- int tableIndex = SquareTableIndex ( squareIndex , piece ) ;
40+ int pieceIndex = PieceIndex ( piece ) ;
4341 Phase += PhaseValues [ pieceIndex ] ;
44- MidgameScore += ( short ) ( color * MidgameTables [ pieceIndex , tableIndex ] ) ;
45- EndgameScore += ( short ) ( color * EndgameTables [ pieceIndex , tableIndex ] ) ;
42+
43+ if ( piece . IsWhite ( ) )
44+ AddScore ( pieceIndex , squareIndex ^ 56 ) ;
45+ else
46+ SubtractScore ( pieceIndex , squareIndex ) ;
4647 }
4748
4849 private void RemovePiece ( Piece piece , int squareIndex )
4950 {
50- int color = ( int ) piece . Color ( ) ;
51- int pieceIndex = PieceTableIndex ( piece ) ;
52- int tableIndex = SquareTableIndex ( squareIndex , piece ) ;
51+ int pieceIndex = PieceIndex ( piece ) ;
5352 Phase -= PhaseValues [ pieceIndex ] ;
54- MidgameScore -= ( short ) ( color * MidgameTables [ pieceIndex , tableIndex ] ) ;
55- EndgameScore -= ( short ) ( color * EndgameTables [ pieceIndex , tableIndex ] ) ;
53+
54+ if ( piece . IsWhite ( ) )
55+ SubtractScore ( pieceIndex , squareIndex ^ 56 ) ;
56+ else
57+ AddScore ( pieceIndex , squareIndex ) ;
58+ }
59+
60+ private void AddScore ( int pieceIndex , int squareIndex )
61+ {
62+ int tableIndex = ( pieceIndex << 6 ) | squareIndex ;
63+ MidgameScore += MidgameTables [ tableIndex ] ;
64+ EndgameScore += EndgameTables [ tableIndex ] ;
65+ }
66+
67+ private void SubtractScore ( int pieceIndex , int squareIndex )
68+ {
69+ int tableIndex = ( pieceIndex << 6 ) | squareIndex ;
70+ MidgameScore -= MidgameTables [ tableIndex ] ;
71+ EndgameScore -= EndgameTables [ tableIndex ] ;
5672 }
5773 }
5874
@@ -75,10 +91,8 @@ public static double Linstep(double edge0, double edge1, double value)
7591 return Math . Min ( 1 , Math . Max ( 0 , ( value - edge0 ) / ( edge1 - edge0 ) ) ) ;
7692 }
7793
78- private static int PieceTableIndex ( Piece piece ) => ( ( int ) piece >> 2 ) - 1 ;
94+ private static int PieceIndex ( Piece piece ) => ( ( int ) piece >> 2 ) - 1 ;
7995
80- //a black piece has 2nd bit set, white has not. Square ^ 56 flips the file, so that tables work for black
81- private static int SquareTableIndex ( int square , Piece piece ) => square ^ ( 28 * ( ( int ) piece & 2 ) ) ;
8296
8397 /*
8498 Added dynamic mobility component to 'chillipepper003' from 'mobility13_7e', the same values as in 0.4.3d which have proven best in tests despite a rather high MSE
@@ -91,8 +105,8 @@ public static double Linstep(double edge0, double edge1, double value)
91105 //Pawn = 0, Knight = 155, Bishop = 305; Rook = 405, Queen = 1050, King = 0
92106 static readonly short [ ] PhaseValues = new short [ 6 ] { 0 , 155 , 305 , 405 , 1050 , 0 } ;
93107
94- static readonly short [ , ] MidgameTables = new short [ 6 , 64 ] {
95- { //PAWN MG
108+ static readonly short [ ] MidgameTables = new short [ 6 * 64 ]
109+ { //PAWN MG
96110 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ,
97111 176 , 214 , 147 , 194 , 189 , 214 , 132 , 77 ,
98112 82 , 88 , 106 , 113 , 150 , 146 , 110 , 73 ,
@@ -101,8 +115,7 @@ public static double Linstep(double edge0, double edge1, double value)
101115 55 , 70 , 68 , 69 , 76 , 81 , 101 , 66 ,
102116 52 , 84 , 66 , 60 , 69 , 99 , 117 , 60 ,
103117 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ,
104- } ,
105- { //KNIGHT MG
118+ //KNIGHT MG
106119 116 , 228 , 271 , 270 , 338 , 213 , 278 , 191 ,
107120 225 , 247 , 353 , 331 , 321 , 360 , 300 , 281 ,
108121 258 , 354 , 343 , 362 , 389 , 428 , 375 , 347 ,
@@ -111,8 +124,7 @@ public static double Linstep(double edge0, double edge1, double value)
111124 287 , 297 , 316 , 319 , 327 , 320 , 327 , 294 ,
112125 276 , 259 , 300 , 304 , 308 , 322 , 296 , 292 ,
113126 208 , 290 , 257 , 274 , 296 , 284 , 293 , 284 ,
114- } ,
115- { //BISHOP MG
127+ //BISHOP MG
116128 292 , 338 , 254 , 283 , 299 , 294 , 337 , 323 ,
117129 316 , 342 , 319 , 319 , 360 , 385 , 343 , 295 ,
118130 342 , 377 , 373 , 374 , 368 , 392 , 385 , 363 ,
@@ -121,8 +133,7 @@ public static double Linstep(double edge0, double edge1, double value)
121133 335 , 350 , 351 , 347 , 352 , 361 , 350 , 344 ,
122134 333 , 354 , 354 , 339 , 344 , 353 , 367 , 333 ,
123135 309 , 341 , 342 , 325 , 334 , 332 , 302 , 313 ,
124- } ,
125- { //ROOK MG
136+ //ROOK MG
126137 493 , 511 , 487 , 515 , 514 , 483 , 485 , 495 ,
127138 493 , 498 , 529 , 534 , 546 , 544 , 483 , 508 ,
128139 465 , 490 , 499 , 497 , 483 , 519 , 531 , 480 ,
@@ -131,8 +142,7 @@ public static double Linstep(double edge0, double edge1, double value)
131142 441 , 461 , 468 , 465 , 478 , 481 , 478 , 452 ,
132143 443 , 472 , 467 , 476 , 483 , 500 , 487 , 423 ,
133144 459 , 463 , 470 , 479 , 480 , 480 , 446 , 458 ,
134- } ,
135- { //QUEEN MG
145+ //QUEEN MG
136146 865 , 902 , 922 , 911 , 964 , 948 , 933 , 928 ,
137147 886 , 865 , 903 , 921 , 888 , 951 , 923 , 940 ,
138148 902 , 901 , 907 , 919 , 936 , 978 , 965 , 966 ,
@@ -141,8 +151,7 @@ public static double Linstep(double edge0, double edge1, double value)
141151 895 , 916 , 900 , 902 , 904 , 912 , 924 , 917 ,
142152 874 , 899 , 918 , 908 , 915 , 924 , 911 , 906 ,
143153 906 , 899 , 906 , 918 , 898 , 890 , 878 , 858 ,
144- } ,
145- { //KING MG
154+ //KING MG
146155 - 11 , 70 , 55 , 31 , - 37 , - 16 , 22 , 22 ,
147156 37 , 24 , 25 , 36 , 16 , 8 , - 12 , - 31 ,
148157 33 , 26 , 42 , 11 , 11 , 40 , 35 , - 2 ,
@@ -151,11 +160,10 @@ public static double Linstep(double edge0, double edge1, double value)
151160 7 , - 2 , - 37 , - 77 , - 79 , - 60 , - 23 , - 26 ,
152161 12 , 15 , - 13 , - 72 , - 56 , - 28 , 15 , 17 ,
153162 - 6 , 44 , 29 , - 58 , 8 , - 25 , 34 , 28 ,
154- }
155163 } ;
156164
157- static readonly short [ , ] EndgameTables = new short [ 6 , 64 ] {
158- { //PAWN EG
165+ static readonly short [ ] EndgameTables = new short [ 6 * 64 ]
166+ { //PAWN EG
159167 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ,
160168 277 , 270 , 252 , 229 , 240 , 233 , 264 , 285 ,
161169 190 , 197 , 182 , 168 , 155 , 150 , 180 , 181 ,
@@ -164,8 +172,7 @@ public static double Linstep(double edge0, double edge1, double value)
164172 96 , 96 , 85 , 92 , 88 , 83 , 85 , 82 ,
165173 107 , 99 , 97 , 97 , 100 , 89 , 89 , 84 ,
166174 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 ,
167- } ,
168- { //KNIGHT EG
175+ //KNIGHT EG
169176 229 , 236 , 269 , 250 , 257 , 249 , 219 , 188 ,
170177 252 , 274 , 263 , 281 , 273 , 258 , 260 , 229 ,
171178 253 , 264 , 290 , 289 , 278 , 275 , 263 , 243 ,
@@ -174,8 +181,7 @@ public static double Linstep(double edge0, double edge1, double value)
174181 258 , 276 , 278 , 290 , 287 , 274 , 260 , 255 ,
175182 241 , 259 , 270 , 277 , 276 , 262 , 260 , 237 ,
176183 253 , 233 , 258 , 264 , 261 , 260 , 234 , 215 ,
177- } ,
178- { //BISHOP EG
184+ //BISHOP EG
179185 288 , 278 , 287 , 292 , 293 , 290 , 287 , 277 ,
180186 289 , 294 , 301 , 288 , 296 , 289 , 294 , 281 ,
181187 292 , 289 , 296 , 292 , 296 , 300 , 296 , 293 ,
@@ -184,8 +190,7 @@ public static double Linstep(double edge0, double edge1, double value)
184190 285 , 294 , 304 , 303 , 306 , 294 , 290 , 280 ,
185191 285 , 284 , 291 , 299 , 300 , 290 , 284 , 271 ,
186192 277 , 292 , 286 , 295 , 294 , 288 , 290 , 285 ,
187- } ,
188- { //ROOK EG
193+ //ROOK EG
189194 506 , 500 , 508 , 502 , 504 , 507 , 505 , 503 ,
190195 505 , 506 , 502 , 502 , 491 , 497 , 506 , 501 ,
191196 504 , 503 , 499 , 500 , 500 , 495 , 496 , 496 ,
@@ -194,8 +199,7 @@ public static double Linstep(double edge0, double edge1, double value)
194199 500 , 503 , 500 , 505 , 498 , 498 , 499 , 489 ,
195200 496 , 495 , 502 , 505 , 498 , 498 , 491 , 499 ,
196201 492 , 497 , 498 , 496 , 493 , 493 , 497 , 480 ,
197- } ,
198- { //QUEEN EG
202+ //QUEEN EG
199203 918 , 937 , 943 , 945 , 934 , 926 , 924 , 942 ,
200204 907 , 945 , 946 , 951 , 982 , 933 , 928 , 912 ,
201205 896 , 921 , 926 , 967 , 963 , 937 , 924 , 915 ,
@@ -204,8 +208,7 @@ public static double Linstep(double edge0, double edge1, double value)
204208 911 , 892 , 933 , 928 , 934 , 942 , 934 , 924 ,
205209 907 , 898 , 883 , 903 , 903 , 893 , 886 , 888 ,
206210 886 , 887 , 890 , 872 , 916 , 890 , 906 , 879 ,
207- } ,
208- { //KING EG
211+ //KING EG
209212 - 74 , - 43 , - 23 , - 25 , - 11 , 10 , 1 , - 12 ,
210213 - 18 , 6 , 4 , 9 , 7 , 26 , 14 , 8 ,
211214 - 3 , 6 , 10 , 6 , 8 , 24 , 27 , 3 ,
@@ -214,7 +217,7 @@ public static double Linstep(double edge0, double edge1, double value)
214217 - 27 , - 10 , 9 , 20 , 23 , 14 , 2 , - 12 ,
215218 - 32 , - 17 , 4 , 14 , 15 , 5 , - 10 , - 22 ,
216219 - 55 , - 40 , - 23 , - 6 , - 20 , - 8 , - 28 , - 47 ,
217- } } ;
220+ } ;
218221
219222 public static short [ ] MobilityValues = new short [ 13 * 6 ]
220223 {
@@ -269,7 +272,7 @@ public static int GetMobility(Board board, int square)
269272 private static int GetMobility ( Board board , Piece piece , byte [ ] targets )
270273 {
271274 int result = 0 ;
272- int row = 13 * PieceTableIndex ( piece ) ;
275+ int row = 13 * PieceIndex ( piece ) ;
273276 foreach ( int square in targets )
274277 {
275278 Piece targetPiece = board [ square ] ;
@@ -283,7 +286,7 @@ private static int GetMobility(Board board, Piece piece, byte[] targets)
283286 private static int GetMobilitySlider ( Board board , Piece piece , byte [ ] [ ] targets )
284287 {
285288 int result = 0 ;
286- int row = 13 * PieceTableIndex ( piece ) ;
289+ int row = 13 * PieceIndex ( piece ) ;
287290 foreach ( var axis in targets )
288291 foreach ( var square in axis )
289292 {
0 commit comments